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