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