]> git.cameronkatri.com Git - mandoc.git/blob - validate.c
Strings abstracted into dynamically-created C files.
[mandoc.git] / validate.c
1 /* $Id: validate.c,v 1.69 2009/03/06 14:13:47 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 if (SEC_CUSTOM == (sec = va_arg(ap, enum mdoc_sed)))
488 break;
489 if (sec != mdoc->lastsec)
490 continue;
491 va_end(ap);
492 return(1);
493 }
494
495 va_end(ap);
496 return(mdoc_nwarn(mdoc, n, WARN_COMPAT,
497 "inappropriate document section for macro"));
498 }
499
500
501 static int
502 check_msec(PRE_ARGS, ...)
503 {
504 va_list ap;
505 int msec;
506
507 va_start(ap, n);
508 for (;;) {
509 if (0 == (msec = va_arg(ap, int)))
510 break;
511 if (msec != mdoc->meta.msec)
512 continue;
513 va_end(ap);
514 return(1);
515 }
516
517 va_end(ap);
518 return(mdoc_nwarn(mdoc, n, WARN_COMPAT,
519 "inappropriate manual section for macro"));
520 }
521
522
523 /*
524 * Check over an argument. When this has more stuff in it, make this
525 * into a table-driven function; until then, a switch is fine.
526 */
527 static int
528 check_argv(struct mdoc *mdoc,
529 const struct mdoc_node *node,
530 const struct mdoc_arg *argv)
531 {
532
533
534 switch (argv->arg) {
535 case (MDOC_Std):
536 switch (node->tok) {
537 case (MDOC_Ex):
538 /*
539 * If the -std does not have an argument, then
540 * set it with the default name (if set). This
541 * only happens with MDOC_Ex.
542 */
543 if (1 == argv->sz)
544 return(1);
545 assert(0 == argv->sz);
546 if (mdoc->meta.name)
547 return(1);
548 return(mdoc_nerr(mdoc, node,
549 "default name not yet set"));
550 default:
551 break;
552 }
553 break;
554 default:
555 break;
556 }
557
558 return(1);
559 }
560
561
562 static int
563 check_text(struct mdoc *mdoc, int line, int pos, const char *p)
564 {
565 size_t c;
566
567 /* XXX - indicate deprecated escapes \*(xx and \*x. */
568
569 for ( ; *p; p++) {
570 if ( ! isprint((u_char)*p) && '\t' != *p)
571 return(mdoc_perr(mdoc, line, pos,
572 "invalid non-printing characters"));
573 if ('\\' != *p)
574 continue;
575 if ((c = mdoc_isescape(p))) {
576 p += (int)c - 1;
577 continue;
578 }
579 return(mdoc_perr(mdoc, line, pos,
580 "invalid escape sequence"));
581 }
582
583 return(1);
584 }
585
586
587
588
589 static int
590 check_parent(PRE_ARGS, int tok, enum mdoc_type t)
591 {
592
593 assert(n->parent);
594 if ((MDOC_ROOT == t || tok == n->parent->tok) &&
595 (t == n->parent->type))
596 return(1);
597
598 return(mdoc_nerr(mdoc, n, "require parent %s",
599 MDOC_ROOT == t ? "<root>" : mdoc_macronames[tok]));
600 }
601
602
603
604 static int
605 pre_display(PRE_ARGS)
606 {
607 struct mdoc_node *node;
608
609 /* Display elements (`Bd', `D1'...) cannot be nested. */
610
611 if (MDOC_BLOCK != n->type)
612 return(1);
613
614 /* LINTED */
615 for (node = mdoc->last->parent; node; node = node->parent)
616 if (MDOC_BLOCK == node->type)
617 if (MDOC_Bd == node->tok)
618 break;
619 if (NULL == node)
620 return(1);
621
622 return(mdoc_nerr(mdoc, n, "displays may not be nested"));
623 }
624
625
626 static int
627 pre_bl(PRE_ARGS)
628 {
629 int type, i, width, offset;
630 struct mdoc_arg *argv;
631 size_t argc;
632
633 if (MDOC_BLOCK != n->type)
634 return(1);
635
636 argc = n->data.block.argc;
637
638 /* Make sure that only one type of list is specified. */
639
640 type = offset = width = -1;
641
642 /* LINTED */
643 for (i = 0; i < (int)argc; i++) {
644 argv = &n->data.block.argv[i];
645
646 switch (argv->arg) {
647 case (MDOC_Bullet):
648 /* FALLTHROUGH */
649 case (MDOC_Dash):
650 /* FALLTHROUGH */
651 case (MDOC_Enum):
652 /* FALLTHROUGH */
653 case (MDOC_Hyphen):
654 /* FALLTHROUGH */
655 case (MDOC_Item):
656 /* FALLTHROUGH */
657 case (MDOC_Tag):
658 /* FALLTHROUGH */
659 case (MDOC_Diag):
660 /* FALLTHROUGH */
661 case (MDOC_Hang):
662 /* FALLTHROUGH */
663 case (MDOC_Ohang):
664 /* FALLTHROUGH */
665 case (MDOC_Inset):
666 /* FALLTHROUGH */
667 case (MDOC_Column):
668 if (-1 == type) {
669 type = argv->arg;
670 break;
671 }
672 return(mdoc_perr(mdoc, argv->line, argv->pos,
673 "multiple types specified"));
674 case (MDOC_Width):
675 if (-1 == width) {
676 width = argv->arg;
677 break;
678 }
679 return(mdoc_perr(mdoc, argv->line, argv->pos,
680 "multiple -%s arguments",
681 mdoc_argnames[MDOC_Width]));
682 case (MDOC_Offset):
683 if (-1 == offset) {
684 offset = argv->arg;
685 break;
686 }
687 return(mdoc_perr(mdoc, argv->line, argv->pos,
688 "multiple -%s arguments",
689 mdoc_argnames[MDOC_Offset]));
690 default:
691 break;
692 }
693 }
694
695 if (-1 == type)
696 return(mdoc_err(mdoc, "no type specified"));
697
698 switch (type) {
699 case (MDOC_Column):
700 /* FALLTHROUGH */
701 case (MDOC_Diag):
702 /* FALLTHROUGH */
703 case (MDOC_Inset):
704 /* FALLTHROUGH */
705 case (MDOC_Item):
706 if (-1 == width)
707 break;
708 return(mdoc_nwarn(mdoc, n, WARN_SYNTAX,
709 "superfluous -%s argument",
710 mdoc_argnames[MDOC_Width]));
711 case (MDOC_Tag):
712 if (-1 == width && ! mdoc_nwarn(mdoc, n, WARN_SYNTAX,
713 "suggest -%s argument",
714 mdoc_argnames[MDOC_Width]))
715 return(0);
716 break;
717 default:
718 break;
719 }
720
721 return(1);
722 }
723
724
725 static int
726 pre_bd(PRE_ARGS)
727 {
728 int type, err, i;
729 struct mdoc_arg *argv;
730 size_t argc;
731
732 if (MDOC_BLOCK != n->type)
733 return(1);
734
735 argc = n->data.block.argc;
736
737 /* Make sure that only one type of display is specified. */
738
739 /* LINTED */
740 for (i = 0, err = type = 0; ! err && i < (int)argc; i++) {
741 argv = &n->data.block.argv[i];
742
743 switch (argv->arg) {
744 case (MDOC_Ragged):
745 /* FALLTHROUGH */
746 case (MDOC_Unfilled):
747 /* FALLTHROUGH */
748 case (MDOC_Filled):
749 /* FALLTHROUGH */
750 case (MDOC_Literal):
751 /* FALLTHROUGH */
752 case (MDOC_File):
753 if (0 == type++)
754 break;
755 return(mdoc_perr(mdoc, argv->line, argv->pos,
756 "multiple types specified"));
757 default:
758 break;
759 }
760 }
761
762 if (type)
763 return(1);
764 return(mdoc_err(mdoc, "no type specified"));
765 }
766
767
768 static int
769 pre_ss(PRE_ARGS)
770 {
771
772 if (MDOC_BLOCK != n->type)
773 return(1);
774 return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
775 }
776
777
778 static int
779 pre_sh(PRE_ARGS)
780 {
781
782 if (MDOC_BLOCK != n->type)
783 return(1);
784 return(check_parent(mdoc, n, -1, MDOC_ROOT));
785 }
786
787
788 static int
789 pre_it(PRE_ARGS)
790 {
791
792 if (MDOC_BLOCK != n->type)
793 return(1);
794 return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
795 }
796
797
798 static int
799 pre_an(PRE_ARGS)
800 {
801
802 if (1 >= n->data.elem.argc)
803 return(1);
804 return(mdoc_nerr(mdoc, n, "one argument allowed"));
805 }
806
807
808 static int
809 pre_lb(PRE_ARGS)
810 {
811
812 return(check_sec(mdoc, n, SEC_LIBRARY, SEC_CUSTOM));
813 }
814
815
816 static int
817 pre_rv(PRE_ARGS)
818 {
819
820 if ( ! check_msec(mdoc, n, 2, 3, 0))
821 return(0);
822 return(check_stdarg(mdoc, n));
823 }
824
825
826 static int
827 pre_ex(PRE_ARGS)
828 {
829
830 if ( ! check_msec(mdoc, n, 1, 6, 8, 0))
831 return(0);
832 return(check_stdarg(mdoc, n));
833 }
834
835
836 static int
837 pre_er(PRE_ARGS)
838 {
839
840 return(check_msec(mdoc, n, 2, 0));
841 }
842
843
844 static int
845 pre_cd(PRE_ARGS)
846 {
847
848 return(check_msec(mdoc, n, 4, 0));
849 }
850
851
852 static int
853 pre_prologue(PRE_ARGS)
854 {
855
856 return(check_sec(mdoc, n, SEC_PROLOGUE, SEC_CUSTOM));
857 }
858
859
860 static int
861 pre_dt(PRE_ARGS)
862 {
863
864 if (0 == mdoc->meta.date || mdoc->meta.os)
865 if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
866 "out-of-order prologue"))
867 return(0);
868 if (mdoc->meta.title)
869 if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
870 "prologue re-invoked"))
871 return(0);
872 return(1);
873 }
874
875
876 static int
877 pre_os(PRE_ARGS)
878 {
879
880 if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
881 if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
882 "out-of-order prologue"))
883 return(0);
884 if (mdoc->meta.os)
885 if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
886 "prologue re-invoked"))
887 return(0);
888 return(1);
889 }
890
891
892 static int
893 pre_dd(PRE_ARGS)
894 {
895
896 if (mdoc->meta.title || mdoc->meta.os)
897 if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
898 "out-of-order prologue"))
899 return(0);
900 if (mdoc->meta.date)
901 if ( ! mdoc_nwarn(mdoc, n, WARN_COMPAT,
902 "prologue re-invoked"))
903 return(0);
904 return(1);
905 }
906
907
908 static int
909 post_bf(POST_ARGS)
910 {
911 char *p;
912 struct mdoc_node *head;
913
914 if (MDOC_BLOCK != mdoc->last->type)
915 return(1);
916
917 head = mdoc->last->data.block.head;
918
919 if (0 == mdoc->last->data.block.argc) {
920 if (NULL == head->child)
921 return(mdoc_err(mdoc, "argument expected"));
922
923 p = head->child->data.text.string;
924 if (xstrcmp(p, "Em"))
925 return(1);
926 else if (xstrcmp(p, "Li"))
927 return(1);
928 else if (xstrcmp(p, "Sm"))
929 return(1);
930 return(mdoc_nerr(mdoc, head->child, "invalid font"));
931 }
932
933 if (head->child)
934 return(mdoc_err(mdoc, "argument expected"));
935
936 if (1 == mdoc->last->data.block.argc)
937 return(1);
938 return(mdoc_err(mdoc, "argument expected"));
939 }
940
941
942 static int
943 post_nm(POST_ARGS)
944 {
945
946 if (mdoc->last->child)
947 return(1);
948 if (mdoc->meta.name)
949 return(1);
950 return(mdoc_err(mdoc, "not yet invoked with name"));
951 }
952
953
954 static int
955 post_at(POST_ARGS)
956 {
957
958 if (NULL == mdoc->last->child)
959 return(1);
960 if (mdoc_a2att(mdoc->last->child->data.text.string))
961 return(1);
962 return(mdoc_err(mdoc, "require valid AT&T symbol"));
963 }
964
965
966 static int
967 post_an(POST_ARGS)
968 {
969
970 if (0 != mdoc->last->data.elem.argc) {
971 if (NULL == mdoc->last->child)
972 return(1);
973 return(mdoc_err(mdoc, "argument(s) expected"));
974 }
975
976 if (mdoc->last->child)
977 return(1);
978 return(mdoc_err(mdoc, "argument(s) expected"));
979 }
980
981
982 static int
983 post_ex(POST_ARGS)
984 {
985
986 if (0 == mdoc->last->data.elem.argc) {
987 if (mdoc->last->child)
988 return(1);
989 return(mdoc_err(mdoc, "argument(s) expected"));
990 }
991 if (mdoc->last->child)
992 return(mdoc_err(mdoc, "argument(s) expected"));
993 if (1 != mdoc->last->data.elem.argc)
994 return(mdoc_err(mdoc, "argument(s) expected"));
995 if (MDOC_Std != mdoc->last->data.elem.argv[0].arg)
996 return(mdoc_err(mdoc, "argument(s) expected"));
997
998 return(1);
999 }
1000
1001
1002 static int
1003 post_it(POST_ARGS)
1004 {
1005 int type, sv, i;
1006 #define TYPE_NONE (0)
1007 #define TYPE_BODY (1)
1008 #define TYPE_HEAD (2)
1009 #define TYPE_OHEAD (3)
1010 size_t argc;
1011 struct mdoc_node *n;
1012
1013 if (MDOC_BLOCK != mdoc->last->type)
1014 return(1);
1015
1016 n = mdoc->last->parent->parent;
1017
1018 argc = n->data.block.argc;
1019 type = TYPE_NONE;
1020 sv = -1;
1021
1022 /* Some types require block-head, some not. */
1023
1024 /* LINTED */
1025 for (i = 0; TYPE_NONE == type && i < (int)argc; i++)
1026 switch (n->data.block.argv[i].arg) {
1027 case (MDOC_Tag):
1028 /* FALLTHROUGH */
1029 case (MDOC_Diag):
1030 /* FALLTHROUGH */
1031 case (MDOC_Hang):
1032 /* FALLTHROUGH */
1033 case (MDOC_Ohang):
1034 /* FALLTHROUGH */
1035 case (MDOC_Inset):
1036 type = TYPE_HEAD;
1037 sv = n->data.block.argv[i].arg;
1038 break;
1039 case (MDOC_Bullet):
1040 /* FALLTHROUGH */
1041 case (MDOC_Dash):
1042 /* FALLTHROUGH */
1043 case (MDOC_Enum):
1044 /* FALLTHROUGH */
1045 case (MDOC_Hyphen):
1046 /* FALLTHROUGH */
1047 case (MDOC_Item):
1048 type = TYPE_BODY;
1049 sv = n->data.block.argv[i].arg;
1050 break;
1051 case (MDOC_Column):
1052 type = TYPE_OHEAD;
1053 sv = n->data.block.argv[i].arg;
1054 break;
1055 default:
1056 break;
1057 }
1058
1059 assert(TYPE_NONE != type);
1060
1061 n = mdoc->last->data.block.head;
1062
1063 if (TYPE_HEAD == type) {
1064 if (NULL == n->child)
1065 if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1066 "argument(s) suggested"))
1067 return(0);
1068
1069 n = mdoc->last->data.block.body;
1070 if (NULL == n->child)
1071 if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1072 "multiline body suggested"))
1073 return(0);
1074
1075 } else if (TYPE_BODY == type) {
1076 if (n->child)
1077 if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1078 "no argument suggested"))
1079 return(0);
1080
1081 n = mdoc->last->data.block.body;
1082 if (NULL == n->child)
1083 if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1084 "multiline body suggested"))
1085 return(0);
1086 } else {
1087 if (NULL == n->child)
1088 if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1089 "argument(s) suggested"))
1090 return(0);
1091
1092 n = mdoc->last->data.block.body;
1093 if (n->child)
1094 if ( ! mdoc_warn(mdoc, WARN_SYNTAX,
1095 "no multiline body suggested"))
1096 return(0);
1097 }
1098
1099 if (MDOC_Column != sv)
1100 return(1);
1101
1102 argc = mdoc->last->parent->parent->data.block.argv->sz;
1103 n = mdoc->last->data.block.head->child;
1104
1105 for (i = 0; n; n = n->next)
1106 i++;
1107
1108 if (i == (int)argc)
1109 return(1);
1110
1111 return(mdoc_err(mdoc, "need %zu columns (have %d)", argc, i));
1112 #undef TYPE_NONE
1113 #undef TYPE_BODY
1114 #undef TYPE_HEAD
1115 #undef TYPE_OHEAD
1116 }
1117
1118
1119 static int
1120 post_bl(POST_ARGS)
1121 {
1122 struct mdoc_node *n;
1123
1124 if (MDOC_BODY != mdoc->last->type)
1125 return(1);
1126 if (NULL == (mdoc->last->child))
1127 return(1);
1128
1129 /*
1130 * Only allow `It' macros to be the immediate descendants of the
1131 * `Bl' list.
1132 */
1133
1134 /* LINTED */
1135 for (n = mdoc->last->child; n; n = n->next) {
1136 if (MDOC_BLOCK == n->type)
1137 if (MDOC_It == n->tok)
1138 continue;
1139
1140 return(mdoc_nerr(mdoc, n, "bad child of parent %s",
1141 mdoc_macronames[mdoc->last->tok]));
1142 }
1143
1144 return(1);
1145 }
1146
1147
1148 static int
1149 ebool(struct mdoc *mdoc)
1150 {
1151 struct mdoc_node *n;
1152
1153 /* LINTED */
1154 for (n = mdoc->last->child; n; n = n->next) {
1155 if (MDOC_TEXT != n->type)
1156 break;
1157 if (xstrcmp(n->data.text.string, "on"))
1158 continue;
1159 if (xstrcmp(n->data.text.string, "off"))
1160 continue;
1161 break;
1162 }
1163
1164 if (NULL == n)
1165 return(1);
1166 return(mdoc_nerr(mdoc, n, "expected boolean"));
1167 }
1168
1169
1170 static int
1171 post_root(POST_ARGS)
1172 {
1173
1174 if (NULL == mdoc->first->child)
1175 return(mdoc_err(mdoc, "document lacks data"));
1176 if (SEC_PROLOGUE == mdoc->lastnamed)
1177 return(mdoc_err(mdoc, "document lacks prologue"));
1178
1179 if (MDOC_BLOCK != mdoc->first->child->type)
1180 return(mdoc_err(mdoc, "lacking post-prologue %s",
1181 mdoc_macronames[MDOC_Sh]));
1182 if (MDOC_Sh != mdoc->first->child->tok)
1183 return(mdoc_err(mdoc, "lacking post-prologue %s",
1184 mdoc_macronames[MDOC_Sh]));
1185
1186 return(1);
1187 }
1188
1189
1190 static int
1191 post_st(POST_ARGS)
1192 {
1193
1194 if (mdoc_a2st(mdoc->last->child->data.text.string))
1195 return(1);
1196
1197 return(mdoc_warn(mdoc, WARN_SYNTAX, "invalid standard"));
1198 }
1199
1200
1201 static int
1202 post_sh(POST_ARGS)
1203 {
1204
1205 if (MDOC_HEAD == mdoc->last->type)
1206 return(post_sh_head(mdoc));
1207 if (MDOC_BODY == mdoc->last->type)
1208 return(post_sh_body(mdoc));
1209
1210 return(1);
1211 }
1212
1213
1214 static int
1215 post_sh_body(POST_ARGS)
1216 {
1217 struct mdoc_node *n;
1218
1219 if (SEC_NAME != mdoc->lastnamed)
1220 return(1);
1221
1222 /*
1223 * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1224 * macros (can have multiple `Nm' and one `Nd'). Note that the
1225 * children of the BODY declaration can also be "text".
1226 */
1227
1228 if (NULL == (n = mdoc->last->child))
1229 return(mdoc_warn(mdoc, WARN_SYNTAX,
1230 "section should have %s and %s",
1231 mdoc_macronames[MDOC_Nm],
1232 mdoc_macronames[MDOC_Nd]));
1233
1234 for ( ; n && n->next; n = n->next) {
1235 if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
1236 continue;
1237 if (MDOC_TEXT == n->type)
1238 continue;
1239 if ( ! (mdoc_nwarn(mdoc, n, WARN_SYNTAX,
1240 "section should have %s first",
1241 mdoc_macronames[MDOC_Nm])))
1242 return(0);
1243 }
1244
1245 if (MDOC_ELEM == n->type && MDOC_Nd == n->tok)
1246 return(1);
1247
1248 return(mdoc_warn(mdoc, WARN_SYNTAX,
1249 "section should have %s last",
1250 mdoc_macronames[MDOC_Nd]));
1251 }
1252
1253
1254 static int
1255 post_sh_head(POST_ARGS)
1256 {
1257 char buf[64];
1258 enum mdoc_sec sec;
1259
1260 /*
1261 * Process a new section. Sections are either "named" or
1262 * "custom"; custom sections are user-defined, while named ones
1263 * usually follow a conventional order and may only appear in
1264 * certain manual sections.
1265 */
1266
1267 assert(MDOC_Sh == mdoc->last->tok);
1268
1269 (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
1270
1271 sec = mdoc_atosec(buf);
1272
1273 /* The NAME section should always be first. */
1274
1275 if (SEC_BODY == mdoc->lastnamed && SEC_NAME != sec)
1276 return(mdoc_warn(mdoc, WARN_SYNTAX,
1277 "section NAME should be first"));
1278 if (SEC_CUSTOM == sec)
1279 return(1);
1280
1281 /* Check for repeated or out-of-order sections. */
1282
1283 if (sec == mdoc->lastnamed)
1284 return(mdoc_warn(mdoc, WARN_SYNTAX,
1285 "section repeated"));
1286 if (sec < mdoc->lastnamed)
1287 return(mdoc_warn(mdoc, WARN_SYNTAX,
1288 "section out of conventional order"));
1289
1290 /* Check particular section/manual section conventions. */
1291
1292 switch (sec) {
1293 case (SEC_LIBRARY):
1294 switch (mdoc->meta.msec) {
1295 case (2):
1296 /* FALLTHROUGH */
1297 case (3):
1298 break;
1299 default:
1300 return(mdoc_warn(mdoc, WARN_COMPAT,
1301 "section in wrong manual section"));
1302 }
1303 break;
1304 default:
1305 break;
1306 }
1307
1308 return(1);
1309 }
1310
1311
1312 static int
1313 pre_fd(PRE_ARGS)
1314 {
1315
1316 return(check_sec(mdoc, n, SEC_SYNOPSIS, SEC_CUSTOM));
1317 }