]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_validate.c
Lifted 64-byte max width for Sh (now BUFSIZ).
[mandoc.git] / mdoc_validate.c
1 /* $Id: mdoc_validate.c,v 1.81 2010/05/24 12:31:16 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "mandoc.h"
31 #include "libmdoc.h"
32 #include "libmandoc.h"
33
34 /* FIXME: .Bl -diag can't have non-text children in HEAD. */
35 /* TODO: ignoring Pp (it's superfluous in some invocations). */
36
37 #define PRE_ARGS struct mdoc *mdoc, const struct mdoc_node *n
38 #define POST_ARGS struct mdoc *mdoc
39
40 typedef int (*v_pre)(PRE_ARGS);
41 typedef int (*v_post)(POST_ARGS);
42
43 struct valids {
44 v_pre *pre;
45 v_post *post;
46 };
47
48 static int check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
49 static int check_stdarg(PRE_ARGS);
50 static int check_text(struct mdoc *, int, int, const char *);
51 static int check_argv(struct mdoc *,
52 const struct mdoc_node *,
53 const struct mdoc_argv *);
54 static int check_args(struct mdoc *,
55 const struct mdoc_node *);
56 static int err_child_lt(struct mdoc *, const char *, int);
57 static int warn_child_lt(struct mdoc *, const char *, int);
58 static int err_child_gt(struct mdoc *, const char *, int);
59 static int warn_child_gt(struct mdoc *, const char *, int);
60 static int err_child_eq(struct mdoc *, const char *, int);
61 static int warn_child_eq(struct mdoc *, const char *, int);
62 static int warn_count(struct mdoc *, const char *,
63 int, const char *, int);
64 static int err_count(struct mdoc *, const char *,
65 int, const char *, int);
66
67 static int berr_ge1(POST_ARGS);
68 static int bwarn_ge1(POST_ARGS);
69 static int ebool(POST_ARGS);
70 static int eerr_eq0(POST_ARGS);
71 static int eerr_eq1(POST_ARGS);
72 static int eerr_ge1(POST_ARGS);
73 static int eerr_le1(POST_ARGS);
74 static int ewarn_ge1(POST_ARGS);
75 static int herr_eq0(POST_ARGS);
76 static int herr_ge1(POST_ARGS);
77 static int hwarn_eq1(POST_ARGS);
78 static int hwarn_eq0(POST_ARGS);
79 static int hwarn_le1(POST_ARGS);
80
81 static int post_an(POST_ARGS);
82 static int post_at(POST_ARGS);
83 static int post_bf(POST_ARGS);
84 static int post_bl(POST_ARGS);
85 static int post_bl_head(POST_ARGS);
86 static int post_it(POST_ARGS);
87 static int post_lb(POST_ARGS);
88 static int post_nm(POST_ARGS);
89 static int post_root(POST_ARGS);
90 static int post_rs(POST_ARGS);
91 static int post_sh(POST_ARGS);
92 static int post_sh_body(POST_ARGS);
93 static int post_sh_head(POST_ARGS);
94 static int post_st(POST_ARGS);
95 static int post_vt(POST_ARGS);
96 static int pre_an(PRE_ARGS);
97 static int pre_bd(PRE_ARGS);
98 static int pre_bl(PRE_ARGS);
99 static int pre_dd(PRE_ARGS);
100 static int pre_display(PRE_ARGS);
101 static int pre_dt(PRE_ARGS);
102 static int pre_it(PRE_ARGS);
103 static int pre_os(PRE_ARGS);
104 static int pre_rv(PRE_ARGS);
105 static int pre_sh(PRE_ARGS);
106 static int pre_ss(PRE_ARGS);
107
108 static v_post posts_an[] = { post_an, NULL };
109 static v_post posts_at[] = { post_at, NULL };
110 static v_post posts_bd[] = { hwarn_eq0, bwarn_ge1, NULL };
111 static v_post posts_bf[] = { hwarn_le1, post_bf, NULL };
112 static v_post posts_bl[] = { bwarn_ge1, post_bl, NULL };
113 static v_post posts_bool[] = { eerr_eq1, ebool, NULL };
114 static v_post posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
115 static v_post posts_it[] = { post_it, NULL };
116 static v_post posts_lb[] = { eerr_eq1, post_lb, NULL };
117 static v_post posts_nd[] = { berr_ge1, NULL };
118 static v_post posts_nm[] = { post_nm, NULL };
119 static v_post posts_notext[] = { eerr_eq0, NULL };
120 static v_post posts_rs[] = { berr_ge1, herr_eq0, post_rs, NULL };
121 static v_post posts_sh[] = { herr_ge1, bwarn_ge1, post_sh, NULL };
122 static v_post posts_sp[] = { eerr_le1, NULL };
123 static v_post posts_ss[] = { herr_ge1, NULL };
124 static v_post posts_st[] = { eerr_eq1, post_st, NULL };
125 static v_post posts_text[] = { eerr_ge1, NULL };
126 static v_post posts_text1[] = { eerr_eq1, NULL };
127 static v_post posts_vt[] = { post_vt, NULL };
128 static v_post posts_wline[] = { bwarn_ge1, herr_eq0, NULL };
129 static v_post posts_wtext[] = { ewarn_ge1, NULL };
130 static v_post posts_xr[] = { ewarn_ge1, NULL };
131 static v_pre pres_an[] = { pre_an, NULL };
132 static v_pre pres_bd[] = { pre_display, pre_bd, NULL };
133 static v_pre pres_bl[] = { pre_bl, NULL };
134 static v_pre pres_d1[] = { pre_display, NULL };
135 static v_pre pres_dd[] = { pre_dd, NULL };
136 static v_pre pres_dt[] = { pre_dt, NULL };
137 static v_pre pres_er[] = { NULL, NULL };
138 static v_pre pres_ex[] = { NULL, NULL };
139 static v_pre pres_fd[] = { NULL, NULL };
140 static v_pre pres_it[] = { pre_it, NULL };
141 static v_pre pres_lb[] = { NULL, NULL };
142 static v_pre pres_os[] = { pre_os, NULL };
143 static v_pre pres_rv[] = { pre_rv, NULL };
144 static v_pre pres_sh[] = { pre_sh, NULL };
145 static v_pre pres_ss[] = { pre_ss, NULL };
146
147 const struct valids mdoc_valids[MDOC_MAX] = {
148 { NULL, NULL }, /* Ap */
149 { pres_dd, posts_text }, /* Dd */
150 { pres_dt, NULL }, /* Dt */
151 { pres_os, NULL }, /* Os */
152 { pres_sh, posts_sh }, /* Sh */
153 { pres_ss, posts_ss }, /* Ss */
154 { NULL, posts_notext }, /* Pp */
155 { pres_d1, posts_wline }, /* D1 */
156 { pres_d1, posts_wline }, /* Dl */
157 { pres_bd, posts_bd }, /* Bd */
158 { NULL, NULL }, /* Ed */
159 { pres_bl, posts_bl }, /* Bl */
160 { NULL, NULL }, /* El */
161 { pres_it, posts_it }, /* It */
162 { NULL, posts_text }, /* Ad */
163 { pres_an, posts_an }, /* An */
164 { NULL, NULL }, /* Ar */
165 { NULL, posts_text }, /* Cd */
166 { NULL, NULL }, /* Cm */
167 { NULL, NULL }, /* Dv */
168 { pres_er, posts_text }, /* Er */
169 { NULL, NULL }, /* Ev */
170 { pres_ex, NULL }, /* Ex */
171 { NULL, NULL }, /* Fa */
172 { pres_fd, posts_wtext }, /* Fd */
173 { NULL, NULL }, /* Fl */
174 { NULL, posts_text }, /* Fn */
175 { NULL, posts_wtext }, /* Ft */
176 { NULL, posts_text }, /* Ic */
177 { NULL, posts_text1 }, /* In */
178 { NULL, NULL }, /* Li */
179 { NULL, posts_nd }, /* Nd */
180 { NULL, posts_nm }, /* Nm */
181 { NULL, posts_wline }, /* Op */
182 { NULL, NULL }, /* Ot */
183 { NULL, NULL }, /* Pa */
184 { pres_rv, NULL }, /* Rv */
185 { NULL, posts_st }, /* St */
186 { NULL, NULL }, /* Va */
187 { NULL, posts_vt }, /* Vt */
188 { NULL, posts_xr }, /* Xr */
189 { NULL, posts_text }, /* %A */
190 { NULL, posts_text }, /* %B */ /* FIXME: can be used outside Rs/Re. */
191 { NULL, posts_text }, /* %D */ /* FIXME: check date with mandoc_a2time(). */
192 { NULL, posts_text }, /* %I */
193 { NULL, posts_text }, /* %J */
194 { NULL, posts_text }, /* %N */
195 { NULL, posts_text }, /* %O */
196 { NULL, posts_text }, /* %P */
197 { NULL, posts_text }, /* %R */
198 { NULL, posts_text }, /* %T */ /* FIXME: can be used outside Rs/Re. */
199 { NULL, posts_text }, /* %V */
200 { NULL, NULL }, /* Ac */
201 { NULL, NULL }, /* Ao */
202 { NULL, posts_wline }, /* Aq */
203 { NULL, posts_at }, /* At */
204 { NULL, NULL }, /* Bc */
205 { NULL, posts_bf }, /* Bf */
206 { NULL, NULL }, /* Bo */
207 { NULL, posts_wline }, /* Bq */
208 { NULL, NULL }, /* Bsx */
209 { NULL, NULL }, /* Bx */
210 { NULL, posts_bool }, /* Db */
211 { NULL, NULL }, /* Dc */
212 { NULL, NULL }, /* Do */
213 { NULL, posts_wline }, /* Dq */
214 { NULL, NULL }, /* Ec */
215 { NULL, NULL }, /* Ef */
216 { NULL, NULL }, /* Em */
217 { NULL, NULL }, /* Eo */
218 { NULL, NULL }, /* Fx */
219 { NULL, posts_text }, /* Ms */
220 { NULL, posts_notext }, /* No */
221 { NULL, posts_notext }, /* Ns */
222 { NULL, NULL }, /* Nx */
223 { NULL, NULL }, /* Ox */
224 { NULL, NULL }, /* Pc */
225 { NULL, posts_text1 }, /* Pf */
226 { NULL, NULL }, /* Po */
227 { NULL, posts_wline }, /* Pq */
228 { NULL, NULL }, /* Qc */
229 { NULL, posts_wline }, /* Ql */
230 { NULL, NULL }, /* Qo */
231 { NULL, posts_wline }, /* Qq */
232 { NULL, NULL }, /* Re */
233 { NULL, posts_rs }, /* Rs */
234 { NULL, NULL }, /* Sc */
235 { NULL, NULL }, /* So */
236 { NULL, posts_wline }, /* Sq */
237 { NULL, posts_bool }, /* Sm */
238 { NULL, posts_text }, /* Sx */
239 { NULL, posts_text }, /* Sy */
240 { NULL, posts_text }, /* Tn */
241 { NULL, NULL }, /* Ux */
242 { NULL, NULL }, /* Xc */
243 { NULL, NULL }, /* Xo */
244 { NULL, posts_fo }, /* Fo */
245 { NULL, NULL }, /* Fc */
246 { NULL, NULL }, /* Oo */
247 { NULL, NULL }, /* Oc */
248 { NULL, posts_wline }, /* Bk */
249 { NULL, NULL }, /* Ek */
250 { NULL, posts_notext }, /* Bt */
251 { NULL, NULL }, /* Hf */
252 { NULL, NULL }, /* Fr */
253 { NULL, posts_notext }, /* Ud */
254 { pres_lb, posts_lb }, /* Lb */
255 { NULL, posts_notext }, /* Lp */
256 { NULL, posts_text }, /* Lk */
257 { NULL, posts_text }, /* Mt */
258 { NULL, posts_wline }, /* Brq */
259 { NULL, NULL }, /* Bro */
260 { NULL, NULL }, /* Brc */
261 { NULL, posts_text }, /* %C */
262 { NULL, NULL }, /* Es */
263 { NULL, NULL }, /* En */
264 { NULL, NULL }, /* Dx */
265 { NULL, posts_text }, /* %Q */
266 { NULL, posts_notext }, /* br */
267 { NULL, posts_sp }, /* sp */
268 { NULL, posts_text1 }, /* %U */
269 };
270
271
272 int
273 mdoc_valid_pre(struct mdoc *mdoc, const struct mdoc_node *n)
274 {
275 v_pre *p;
276 int line, pos;
277 const char *tp;
278
279 if (MDOC_TEXT == n->type) {
280 tp = n->string;
281 line = n->line;
282 pos = n->pos;
283 return(check_text(mdoc, line, pos, tp));
284 }
285
286 if ( ! check_args(mdoc, n))
287 return(0);
288 if (NULL == mdoc_valids[n->tok].pre)
289 return(1);
290 for (p = mdoc_valids[n->tok].pre; *p; p++)
291 if ( ! (*p)(mdoc, n))
292 return(0);
293 return(1);
294 }
295
296
297 int
298 mdoc_valid_post(struct mdoc *mdoc)
299 {
300 v_post *p;
301
302 if (MDOC_VALID & mdoc->last->flags)
303 return(1);
304 mdoc->last->flags |= MDOC_VALID;
305
306 if (MDOC_TEXT == mdoc->last->type)
307 return(1);
308 if (MDOC_ROOT == mdoc->last->type)
309 return(post_root(mdoc));
310
311 if (NULL == mdoc_valids[mdoc->last->tok].post)
312 return(1);
313 for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
314 if ( ! (*p)(mdoc))
315 return(0);
316
317 return(1);
318 }
319
320
321 static inline int
322 warn_count(struct mdoc *m, const char *k,
323 int want, const char *v, int has)
324 {
325
326 return(mdoc_vmsg(m, MANDOCERR_ARGCOUNT,
327 m->last->line, m->last->pos,
328 "%s %s %d (have %d)", v, k, want, has));
329 }
330
331
332 static inline int
333 err_count(struct mdoc *m, const char *k,
334 int want, const char *v, int has)
335 {
336
337 mdoc_vmsg(m, MANDOCERR_SYNTARGCOUNT,
338 m->last->line, m->last->pos,
339 "%s %s %d (have %d)",
340 v, k, want, has);
341 return(0);
342 }
343
344
345 /*
346 * Build these up with macros because they're basically the same check
347 * for different inequalities. Yes, this could be done with functions,
348 * but this is reasonable for now.
349 */
350
351 #define CHECK_CHILD_DEFN(lvl, name, ineq) \
352 static int \
353 lvl##_child_##name(struct mdoc *mdoc, const char *p, int sz) \
354 { \
355 if (mdoc->last->nchild ineq sz) \
356 return(1); \
357 return(lvl##_count(mdoc, #ineq, sz, p, mdoc->last->nchild)); \
358 }
359
360 #define CHECK_BODY_DEFN(name, lvl, func, num) \
361 static int \
362 b##lvl##_##name(POST_ARGS) \
363 { \
364 if (MDOC_BODY != mdoc->last->type) \
365 return(1); \
366 return(func(mdoc, "multi-line arguments", (num))); \
367 }
368
369 #define CHECK_ELEM_DEFN(name, lvl, func, num) \
370 static int \
371 e##lvl##_##name(POST_ARGS) \
372 { \
373 assert(MDOC_ELEM == mdoc->last->type); \
374 return(func(mdoc, "line arguments", (num))); \
375 }
376
377 #define CHECK_HEAD_DEFN(name, lvl, func, num) \
378 static int \
379 h##lvl##_##name(POST_ARGS) \
380 { \
381 if (MDOC_HEAD != mdoc->last->type) \
382 return(1); \
383 return(func(mdoc, "line arguments", (num))); \
384 }
385
386
387 CHECK_CHILD_DEFN(warn, gt, >) /* warn_child_gt() */
388 CHECK_CHILD_DEFN(err, gt, >) /* err_child_gt() */
389 CHECK_CHILD_DEFN(warn, eq, ==) /* warn_child_eq() */
390 CHECK_CHILD_DEFN(err, eq, ==) /* err_child_eq() */
391 CHECK_CHILD_DEFN(err, lt, <) /* err_child_lt() */
392 CHECK_CHILD_DEFN(warn, lt, <) /* warn_child_lt() */
393 CHECK_BODY_DEFN(ge1, warn, warn_child_gt, 0) /* bwarn_ge1() */
394 CHECK_BODY_DEFN(ge1, err, err_child_gt, 0) /* berr_ge1() */
395 CHECK_ELEM_DEFN(ge1, warn, warn_child_gt, 0) /* ewarn_gt1() */
396 CHECK_ELEM_DEFN(eq1, err, err_child_eq, 1) /* eerr_eq1() */
397 CHECK_ELEM_DEFN(le1, err, err_child_lt, 2) /* eerr_le1() */
398 CHECK_ELEM_DEFN(eq0, err, err_child_eq, 0) /* eerr_eq0() */
399 CHECK_ELEM_DEFN(ge1, err, err_child_gt, 0) /* eerr_ge1() */
400 CHECK_HEAD_DEFN(eq0, err, err_child_eq, 0) /* herr_eq0() */
401 CHECK_HEAD_DEFN(le1, warn, warn_child_lt, 2) /* hwarn_le1() */
402 CHECK_HEAD_DEFN(ge1, err, err_child_gt, 0) /* herr_ge1() */
403 CHECK_HEAD_DEFN(eq1, warn, warn_child_eq, 1) /* hwarn_eq1() */
404 CHECK_HEAD_DEFN(eq0, warn, warn_child_eq, 0) /* hwarn_eq0() */
405
406
407 static int
408 check_stdarg(PRE_ARGS)
409 {
410
411 if (n->args && 1 == n->args->argc)
412 if (MDOC_Std == n->args->argv[0].arg)
413 return(1);
414 return(mdoc_nmsg(mdoc, n, MANDOCERR_NOARGV));
415 }
416
417
418 static int
419 check_args(struct mdoc *m, const struct mdoc_node *n)
420 {
421 int i;
422
423 if (NULL == n->args)
424 return(1);
425
426 assert(n->args->argc);
427 for (i = 0; i < (int)n->args->argc; i++)
428 if ( ! check_argv(m, n, &n->args->argv[i]))
429 return(0);
430
431 return(1);
432 }
433
434
435 static int
436 check_argv(struct mdoc *m, const struct mdoc_node *n,
437 const struct mdoc_argv *v)
438 {
439 int i;
440
441 for (i = 0; i < (int)v->sz; i++)
442 if ( ! check_text(m, v->line, v->pos, v->value[i]))
443 return(0);
444
445 if (MDOC_Std == v->arg) {
446 if (v->sz || m->meta.name)
447 return(1);
448 if ( ! mdoc_nmsg(m, n, MANDOCERR_NONAME))
449 return(0);
450 }
451
452 return(1);
453 }
454
455
456 static int
457 check_text(struct mdoc *mdoc, int line, int pos, const char *p)
458 {
459 int c;
460
461 for ( ; *p; p++, pos++) {
462 if ('\t' == *p) {
463 if ( ! (MDOC_LITERAL & mdoc->flags))
464 if ( ! mdoc_pmsg(mdoc, line, pos, MANDOCERR_BADCHAR))
465 return(0);
466 } else if ( ! isprint((u_char)*p))
467 if ( ! mdoc_pmsg(mdoc, line, pos, MANDOCERR_BADCHAR))
468 return(0);
469
470 if ('\\' != *p)
471 continue;
472
473 c = mandoc_special(p);
474 if (c) {
475 p += c - 1;
476 pos += c - 1;
477 continue;
478 }
479
480 c = mdoc_pmsg(mdoc, line, pos, MANDOCERR_BADESCAPE);
481 if ( ! (MDOC_IGN_ESCAPE & mdoc->pflags) && ! c)
482 return(c);
483 }
484
485 return(1);
486 }
487
488
489
490
491 static int
492 check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
493 {
494
495 assert(n->parent);
496 if ((MDOC_ROOT == t || tok == n->parent->tok) &&
497 (t == n->parent->type))
498 return(1);
499
500 mdoc_vmsg(mdoc, MANDOCERR_SYNTCHILD,
501 n->line, n->pos, "want parent %s",
502 MDOC_ROOT == t ? "<root>" :
503 mdoc_macronames[tok]);
504 return(0);
505 }
506
507
508
509 static int
510 pre_display(PRE_ARGS)
511 {
512 struct mdoc_node *node;
513
514 /* Display elements (`Bd', `D1'...) cannot be nested. */
515
516 if (MDOC_BLOCK != n->type)
517 return(1);
518
519 /* LINTED */
520 for (node = mdoc->last->parent; node; node = node->parent)
521 if (MDOC_BLOCK == node->type)
522 if (MDOC_Bd == node->tok)
523 break;
524 if (NULL == node)
525 return(1);
526
527 mdoc_nmsg(mdoc, n, MANDOCERR_NESTEDDISP);
528 return(0);
529 }
530
531
532 static int
533 pre_bl(PRE_ARGS)
534 {
535 int pos, type, width, offset;
536
537 if (MDOC_BLOCK != n->type)
538 return(1);
539 if (NULL == n->args) {
540 mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE);
541 return(0);
542 }
543
544 /* Make sure that only one type of list is specified. */
545
546 type = offset = width = -1;
547
548 /* LINTED */
549 for (pos = 0; pos < (int)n->args->argc; pos++)
550 switch (n->args->argv[pos].arg) {
551 case (MDOC_Bullet):
552 /* FALLTHROUGH */
553 case (MDOC_Dash):
554 /* FALLTHROUGH */
555 case (MDOC_Enum):
556 /* FALLTHROUGH */
557 case (MDOC_Hyphen):
558 /* FALLTHROUGH */
559 case (MDOC_Item):
560 /* FALLTHROUGH */
561 case (MDOC_Tag):
562 /* FALLTHROUGH */
563 case (MDOC_Diag):
564 /* FALLTHROUGH */
565 case (MDOC_Hang):
566 /* FALLTHROUGH */
567 case (MDOC_Ohang):
568 /* FALLTHROUGH */
569 case (MDOC_Inset):
570 /* FALLTHROUGH */
571 case (MDOC_Column):
572 if (type < 0) {
573 type = n->args->argv[pos].arg;
574 break;
575 }
576 if (mdoc_nmsg(mdoc, n, MANDOCERR_LISTREP))
577 break;
578 return(0);
579 case (MDOC_Compact):
580 if (type >= 0)
581 break;
582 if (mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST))
583 break;
584 return(0);
585 case (MDOC_Width):
586 if (width >= 0)
587 if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP))
588 return(0);
589 if (type < 0 && ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST))
590 return(0);
591 width = n->args->argv[pos].arg;
592 break;
593 case (MDOC_Offset):
594 if (offset >= 0)
595 if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP))
596 return(0);
597 if (type < 0 && ! mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST))
598 return(0);
599 offset = n->args->argv[pos].arg;
600 break;
601 default:
602 break;
603 }
604
605 if (type < 0) {
606 mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE);
607 return(0);
608 }
609
610 /*
611 * Validate the width field. Some list types don't need width
612 * types and should be warned about them. Others should have it
613 * and must also be warned.
614 */
615
616 switch (type) {
617 case (MDOC_Tag):
618 if (width < 0 && ! mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG))
619 return(0);
620 break;
621 case (MDOC_Column):
622 /* FALLTHROUGH */
623 case (MDOC_Diag):
624 /* FALLTHROUGH */
625 case (MDOC_Ohang):
626 /* FALLTHROUGH */
627 case (MDOC_Inset):
628 /* FALLTHROUGH */
629 case (MDOC_Item):
630 if (width >= 0 && ! mdoc_nmsg(mdoc, n, MANDOCERR_WIDTHARG))
631 return(0);
632 break;
633 default:
634 break;
635 }
636
637 return(1);
638 }
639
640
641 static int
642 pre_bd(PRE_ARGS)
643 {
644 int i, type, err;
645
646 if (MDOC_BLOCK != n->type)
647 return(1);
648 if (NULL == n->args) {
649 mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE);
650 return(0);
651 }
652
653 /* Make sure that only one type of display is specified. */
654
655 /* LINTED */
656 for (i = 0, err = type = 0; ! err &&
657 i < (int)n->args->argc; i++)
658 switch (n->args->argv[i].arg) {
659 case (MDOC_Centred):
660 /* FALLTHROUGH */
661 case (MDOC_Ragged):
662 /* FALLTHROUGH */
663 case (MDOC_Unfilled):
664 /* FALLTHROUGH */
665 case (MDOC_Filled):
666 /* FALLTHROUGH */
667 case (MDOC_Literal):
668 if (0 == type++)
669 break;
670 if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_DISPREP))
671 return(0);
672 break;
673 default:
674 break;
675 }
676
677 if (type)
678 return(1);
679 mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE);
680 return(0);
681 }
682
683
684 static int
685 pre_ss(PRE_ARGS)
686 {
687
688 if (MDOC_BLOCK != n->type)
689 return(1);
690 return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
691 }
692
693
694 static int
695 pre_sh(PRE_ARGS)
696 {
697
698 if (MDOC_BLOCK != n->type)
699 return(1);
700 return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
701 }
702
703
704 static int
705 pre_it(PRE_ARGS)
706 {
707
708 if (MDOC_BLOCK != n->type)
709 return(1);
710 /*
711 * FIXME: this can probably be lifted if we make the It into
712 * something else on-the-fly?
713 */
714 return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
715 }
716
717
718 static int
719 pre_an(PRE_ARGS)
720 {
721
722 if (NULL == n->args || 1 == n->args->argc)
723 return(1);
724 mdoc_vmsg(mdoc, MANDOCERR_SYNTARGCOUNT,
725 n->line, n->pos,
726 "line arguments == 1 (have %d)",
727 n->args->argc);
728 return(0);
729 }
730
731
732 static int
733 pre_rv(PRE_ARGS)
734 {
735
736 return(check_stdarg(mdoc, n));
737 }
738
739
740 static int
741 pre_dt(PRE_ARGS)
742 {
743
744 /* FIXME: make sure is capitalised. */
745
746 if (0 == mdoc->meta.date || mdoc->meta.os)
747 if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
748 return(0);
749 if (mdoc->meta.title)
750 if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
751 return(0);
752 return(1);
753 }
754
755
756 static int
757 pre_os(PRE_ARGS)
758 {
759
760 if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
761 if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
762 return(0);
763 if (mdoc->meta.os)
764 if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
765 return(0);
766 return(1);
767 }
768
769
770 static int
771 pre_dd(PRE_ARGS)
772 {
773
774 if (mdoc->meta.title || mdoc->meta.os)
775 if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO))
776 return(0);
777 if (mdoc->meta.date)
778 if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP))
779 return(0);
780 return(1);
781 }
782
783
784 static int
785 post_bf(POST_ARGS)
786 {
787 char *p;
788 struct mdoc_node *head;
789
790 if (MDOC_BLOCK != mdoc->last->type)
791 return(1);
792
793 head = mdoc->last->head;
794
795 if (mdoc->last->args && head->child) {
796 /* FIXME: this should provide a default. */
797 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SYNTARGVCOUNT);
798 return(0);
799 } else if (mdoc->last->args)
800 return(1);
801
802 if (NULL == head->child || MDOC_TEXT != head->child->type) {
803 /* FIXME: this should provide a default. */
804 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SYNTARGVCOUNT);
805 return(0);
806 }
807
808 p = head->child->string;
809
810 if (0 == strcmp(p, "Em"))
811 return(1);
812 else if (0 == strcmp(p, "Li"))
813 return(1);
814 else if (0 == strcmp(p, "Sy"))
815 return(1);
816
817 mdoc_nmsg(mdoc, head, MANDOCERR_FONTTYPE);
818 return(0);
819 }
820
821
822 static int
823 post_lb(POST_ARGS)
824 {
825
826 if (mdoc_a2lib(mdoc->last->child->string))
827 return(1);
828 return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADLIB));
829 }
830
831
832 static int
833 post_vt(POST_ARGS)
834 {
835 const struct mdoc_node *n;
836
837 /*
838 * The Vt macro comes in both ELEM and BLOCK form, both of which
839 * have different syntaxes (yet more context-sensitive
840 * behaviour). ELEM types must have a child; BLOCK types,
841 * specifically the BODY, should only have TEXT children.
842 */
843
844 if (MDOC_ELEM == mdoc->last->type)
845 return(eerr_ge1(mdoc));
846 if (MDOC_BODY != mdoc->last->type)
847 return(1);
848
849 for (n = mdoc->last->child; n; n = n->next)
850 if (MDOC_TEXT != n->type)
851 if ( ! mdoc_nmsg(mdoc, n, MANDOCERR_CHILD))
852 return(0);
853
854 return(1);
855 }
856
857
858 static int
859 post_nm(POST_ARGS)
860 {
861
862 if (mdoc->last->child)
863 return(1);
864 if (mdoc->meta.name)
865 return(1);
866 return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME));
867 }
868
869
870 static int
871 post_at(POST_ARGS)
872 {
873
874 if (NULL == mdoc->last->child)
875 return(1);
876 assert(MDOC_TEXT == mdoc->last->child->type);
877 if (mdoc_a2att(mdoc->last->child->string))
878 return(1);
879 return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT));
880 }
881
882
883 static int
884 post_an(POST_ARGS)
885 {
886
887 if (mdoc->last->args) {
888 if (NULL == mdoc->last->child)
889 return(1);
890 return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGCOUNT));
891 }
892
893 if (mdoc->last->child)
894 return(1);
895 return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS));
896 }
897
898
899 static int
900 post_it(POST_ARGS)
901 {
902 int type, i, cols;
903 struct mdoc_node *n, *c;
904
905 if (MDOC_BLOCK != mdoc->last->type)
906 return(1);
907
908 n = mdoc->last->parent->parent;
909 if (NULL == n->args) {
910 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
911 return(0);
912 }
913
914 /* Some types require block-head, some not. */
915
916 /* LINTED */
917 for (cols = type = -1, i = 0; -1 == type &&
918 i < (int)n->args->argc; i++)
919 switch (n->args->argv[i].arg) {
920 case (MDOC_Tag):
921 /* FALLTHROUGH */
922 case (MDOC_Diag):
923 /* FALLTHROUGH */
924 case (MDOC_Hang):
925 /* FALLTHROUGH */
926 case (MDOC_Ohang):
927 /* FALLTHROUGH */
928 case (MDOC_Inset):
929 /* FALLTHROUGH */
930 case (MDOC_Bullet):
931 /* FALLTHROUGH */
932 case (MDOC_Dash):
933 /* FALLTHROUGH */
934 case (MDOC_Enum):
935 /* FALLTHROUGH */
936 case (MDOC_Hyphen):
937 /* FALLTHROUGH */
938 case (MDOC_Item):
939 type = n->args->argv[i].arg;
940 break;
941 case (MDOC_Column):
942 type = n->args->argv[i].arg;
943 cols = (int)n->args->argv[i].sz;
944 break;
945 default:
946 break;
947 }
948
949 if (-1 == type) {
950 mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
951 return(0);
952 }
953
954 switch (type) {
955 case (MDOC_Tag):
956 if (NULL == mdoc->last->head->child)
957 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS))
958 return(0);
959 break;
960 case (MDOC_Hang):
961 /* FALLTHROUGH */
962 case (MDOC_Ohang):
963 /* FALLTHROUGH */
964 case (MDOC_Inset):
965 /* FALLTHROUGH */
966 case (MDOC_Diag):
967 if (NULL == mdoc->last->head->child)
968 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS))
969 return(0);
970 if (NULL == mdoc->last->body->child)
971 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
972 return(0);
973 break;
974 case (MDOC_Bullet):
975 /* FALLTHROUGH */
976 case (MDOC_Dash):
977 /* FALLTHROUGH */
978 case (MDOC_Enum):
979 /* FALLTHROUGH */
980 case (MDOC_Hyphen):
981 /* FALLTHROUGH */
982 case (MDOC_Item):
983 if (mdoc->last->head->child)
984 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST))
985 return(0);
986 if (NULL == mdoc->last->body->child)
987 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY))
988 return(0);
989 break;
990 case (MDOC_Column):
991 if (NULL == mdoc->last->head->child)
992 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS))
993 return(0);
994 if (mdoc->last->body->child)
995 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BODYLOST))
996 return(0);
997 c = mdoc->last->child;
998 for (i = 0; c && MDOC_HEAD == c->type; c = c->next)
999 i++;
1000
1001 if (i < cols) {
1002 if ( ! mdoc_vmsg(mdoc, MANDOCERR_ARGCOUNT,
1003 mdoc->last->line,
1004 mdoc->last->pos,
1005 "columns == %d (have %d)",
1006 cols, i))
1007 return(0);
1008 break;
1009 } else if (i == cols || i == cols + 1)
1010 break;
1011
1012 mdoc_vmsg(mdoc, MANDOCERR_SYNTARGCOUNT,
1013 mdoc->last->line, mdoc->last->pos,
1014 "columns == %d (have %d)", cols, i);
1015 return(0);
1016 default:
1017 break;
1018 }
1019
1020 return(1);
1021 }
1022
1023
1024 static int
1025 post_bl_head(POST_ARGS)
1026 {
1027 int i;
1028 const struct mdoc_node *n;
1029 const struct mdoc_argv *a;
1030
1031 n = mdoc->last->parent;
1032 assert(n->args);
1033
1034 for (i = 0; i < (int)n->args->argc; i++) {
1035 a = &n->args->argv[i];
1036 if (a->arg == MDOC_Column) {
1037 if (a->sz && mdoc->last->nchild)
1038 return(mdoc_nmsg(mdoc, n, MANDOCERR_COLUMNS));
1039 return(1);
1040 }
1041 }
1042
1043 if (0 == (i = mdoc->last->nchild))
1044 return(1);
1045 return(warn_count(mdoc, "==", 0, "line arguments", i));
1046 }
1047
1048
1049 static int
1050 post_bl(POST_ARGS)
1051 {
1052 struct mdoc_node *n;
1053
1054 if (MDOC_HEAD == mdoc->last->type)
1055 return(post_bl_head(mdoc));
1056 if (MDOC_BODY != mdoc->last->type)
1057 return(1);
1058 if (NULL == mdoc->last->child)
1059 return(1);
1060
1061 /*
1062 * We only allow certain children of `Bl'. This is usually on
1063 * `It', but apparently `Sm' occurs here and there, so we let
1064 * that one through, too.
1065 */
1066
1067 /* LINTED */
1068 for (n = mdoc->last->child; n; n = n->next) {
1069 if (MDOC_BLOCK == n->type && MDOC_It == n->tok)
1070 continue;
1071 if (MDOC_Sm == n->tok)
1072 continue;
1073 mdoc_nmsg(mdoc, n, MANDOCERR_SYNTCHILD);
1074 return(0);
1075 }
1076
1077 return(1);
1078 }
1079
1080
1081 static int
1082 ebool(struct mdoc *mdoc)
1083 {
1084 struct mdoc_node *n;
1085
1086 /* LINTED */
1087 for (n = mdoc->last->child; n; n = n->next) {
1088 if (MDOC_TEXT != n->type)
1089 break;
1090 if (0 == strcmp(n->string, "on"))
1091 continue;
1092 if (0 == strcmp(n->string, "off"))
1093 continue;
1094 break;
1095 }
1096
1097 if (NULL == n)
1098 return(1);
1099 return(mdoc_nmsg(mdoc, n, MANDOCERR_BADBOOL));
1100 }
1101
1102
1103 static int
1104 post_root(POST_ARGS)
1105 {
1106
1107 if (NULL == mdoc->first->child)
1108 mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
1109 else if ( ! (MDOC_PBODY & mdoc->flags))
1110 mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCPROLOG);
1111 else if (MDOC_BLOCK != mdoc->first->child->type)
1112 mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
1113 else if (MDOC_Sh != mdoc->first->child->tok)
1114 mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCBODY);
1115 else
1116 return(1);
1117
1118 return(0);
1119 }
1120
1121
1122 static int
1123 post_st(POST_ARGS)
1124 {
1125
1126 if (mdoc_a2st(mdoc->last->child->string))
1127 return(1);
1128 return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADSTANDARD));
1129 }
1130
1131
1132 static int
1133 post_rs(POST_ARGS)
1134 {
1135 struct mdoc_node *nn;
1136
1137 if (MDOC_BODY != mdoc->last->type)
1138 return(1);
1139
1140 for (nn = mdoc->last->child; nn; nn = nn->next)
1141 switch (nn->tok) {
1142 case(MDOC__U):
1143 /* FALLTHROUGH */
1144 case(MDOC__Q):
1145 /* FALLTHROUGH */
1146 case(MDOC__C):
1147 /* FALLTHROUGH */
1148 case(MDOC__A):
1149 /* FALLTHROUGH */
1150 case(MDOC__B):
1151 /* FALLTHROUGH */
1152 case(MDOC__D):
1153 /* FALLTHROUGH */
1154 case(MDOC__I):
1155 /* FALLTHROUGH */
1156 case(MDOC__J):
1157 /* FALLTHROUGH */
1158 case(MDOC__N):
1159 /* FALLTHROUGH */
1160 case(MDOC__O):
1161 /* FALLTHROUGH */
1162 case(MDOC__P):
1163 /* FALLTHROUGH */
1164 case(MDOC__R):
1165 /* FALLTHROUGH */
1166 case(MDOC__T):
1167 /* FALLTHROUGH */
1168 case(MDOC__V):
1169 break;
1170 default:
1171 mdoc_nmsg(mdoc, nn, MANDOCERR_SYNTCHILD);
1172 return(0);
1173 }
1174
1175 return(1);
1176 }
1177
1178
1179 static int
1180 post_sh(POST_ARGS)
1181 {
1182
1183 if (MDOC_HEAD == mdoc->last->type)
1184 return(post_sh_head(mdoc));
1185 if (MDOC_BODY == mdoc->last->type)
1186 return(post_sh_body(mdoc));
1187
1188 return(1);
1189 }
1190
1191
1192 static int
1193 post_sh_body(POST_ARGS)
1194 {
1195 struct mdoc_node *n;
1196
1197 if (SEC_NAME != mdoc->lastsec)
1198 return(1);
1199
1200 /*
1201 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1202 * macros (can have multiple `Nm' and one `Nd'). Note that the
1203 * children of the BODY declaration can also be "text".
1204 */
1205
1206 if (NULL == (n = mdoc->last->child))
1207 return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC));
1208
1209 for ( ; n && n->next; n = n->next) {
1210 if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
1211 continue;
1212 if (MDOC_TEXT == n->type)
1213 continue;
1214 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC))
1215 return(0);
1216 }
1217
1218 assert(n);
1219 if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1220 return(1);
1221 return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC));
1222 }
1223
1224
1225 static int
1226 post_sh_head(POST_ARGS)
1227 {
1228 char buf[BUFSIZ];
1229 enum mdoc_sec sec;
1230 const struct mdoc_node *n;
1231
1232 /*
1233 * Process a new section. Sections are either "named" or
1234 * "custom"; custom sections are user-defined, while named ones
1235 * usually follow a conventional order and may only appear in
1236 * certain manual sections.
1237 */
1238
1239 buf[0] = '\0';
1240
1241 /*
1242 * FIXME: yes, these can use a dynamic buffer, but I don't do so
1243 * in the interests of simplicity.
1244 */
1245
1246 for (n = mdoc->last->child; n; n = n->next) {
1247 /* XXX - copied from compact(). */
1248 assert(MDOC_TEXT == n->type);
1249
1250 if (strlcat(buf, n->string, BUFSIZ) >= BUFSIZ) {
1251 mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
1252 return(0);
1253 }
1254 if (NULL == n->next)
1255 continue;
1256 if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) {
1257 mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
1258 return(0);
1259 }
1260 }
1261
1262 sec = mdoc_str2sec(buf);
1263
1264 /*
1265 * Check: NAME should always be first, CUSTOM has no roles,
1266 * non-CUSTOM has a conventional order to be followed.
1267 */
1268
1269 if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1270 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NAMESECFIRST))
1271 return(0);
1272
1273 if (SEC_CUSTOM == sec)
1274 return(1);
1275
1276 if (sec == mdoc->lastnamed)
1277 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECREP))
1278 return(0);
1279
1280 if (sec < mdoc->lastnamed)
1281 if ( ! mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECOOO))
1282 return(0);
1283
1284 /*
1285 * Check particular section/manual conventions. LIBRARY can
1286 * only occur in manual section 2, 3, and 9.
1287 */
1288
1289 switch (sec) {
1290 case (SEC_LIBRARY):
1291 assert(mdoc->meta.msec);
1292 if (*mdoc->meta.msec == '2')
1293 break;
1294 if (*mdoc->meta.msec == '3')
1295 break;
1296 if (*mdoc->meta.msec == '9')
1297 break;
1298 return(mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECMSEC));
1299 default:
1300 break;
1301 }
1302
1303 return(1);
1304 }