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