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