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