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