Annotation of mandoc/mdoc_validate.c, Revision 1.154
1.154 ! kristaps 1: /* $Id: mdoc_validate.c,v 1.153 2011/01/25 10:37:49 kristaps Exp $ */
1.1 kristaps 2: /*
1.151 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.5 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.5 kristaps 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 17: */
1.56 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.132 kristaps 22: #ifndef OSNAME
23: #include <sys/utsname.h>
24: #endif
25:
1.1 kristaps 26: #include <sys/types.h>
27:
28: #include <assert.h>
29: #include <ctype.h>
1.35 kristaps 30: #include <limits.h>
1.81 kristaps 31: #include <stdio.h>
1.1 kristaps 32: #include <stdlib.h>
1.2 kristaps 33: #include <string.h>
1.132 kristaps 34: #include <time.h>
1.1 kristaps 35:
1.79 kristaps 36: #include "mandoc.h"
1.1 kristaps 37: #include "libmdoc.h"
1.18 kristaps 38: #include "libmandoc.h"
1.1 kristaps 39:
40: /* FIXME: .Bl -diag can't have non-text children in HEAD. */
41:
1.91 kristaps 42: #define PRE_ARGS struct mdoc *mdoc, struct mdoc_node *n
1.32 kristaps 43: #define POST_ARGS struct mdoc *mdoc
1.1 kristaps 44:
1.131 kristaps 45: #define NUMSIZ 32
1.135 kristaps 46: #define DATESIZE 32
1.131 kristaps 47:
1.120 kristaps 48: enum check_ineq {
49: CHECK_LT,
50: CHECK_GT,
51: CHECK_EQ
52: };
53:
54: enum check_lvl {
55: CHECK_WARN,
56: CHECK_ERROR,
57: };
58:
1.1 kristaps 59: typedef int (*v_pre)(PRE_ARGS);
60: typedef int (*v_post)(POST_ARGS);
61:
62: struct valids {
63: v_pre *pre;
64: v_post *post;
65: };
66:
1.120 kristaps 67: static int check_count(struct mdoc *, enum mdoc_type,
68: enum check_lvl, enum check_ineq, int);
1.59 kristaps 69: static int check_parent(PRE_ARGS, enum mdoct, enum mdoc_type);
1.133 kristaps 70: static void check_text(struct mdoc *, int, int, char *);
71: static void check_argv(struct mdoc *,
1.92 kristaps 72: struct mdoc_node *, struct mdoc_argv *);
1.133 kristaps 73: static void check_args(struct mdoc *, struct mdoc_node *);
1.32 kristaps 74:
1.132 kristaps 75: static int concat(struct mdoc *, char *,
76: const struct mdoc_node *, size_t);
77:
1.120 kristaps 78: static int ebool(POST_ARGS);
1.32 kristaps 79: static int berr_ge1(POST_ARGS);
80: static int bwarn_ge1(POST_ARGS);
1.116 kristaps 81: static int ewarn_eq0(POST_ARGS);
1.151 schwarze 82: static int ewarn_eq1(POST_ARGS);
1.32 kristaps 83: static int ewarn_ge1(POST_ARGS);
1.151 schwarze 84: static int ewarn_le1(POST_ARGS);
1.120 kristaps 85: static int hwarn_eq0(POST_ARGS);
1.32 kristaps 86: static int hwarn_eq1(POST_ARGS);
1.151 schwarze 87: static int hwarn_ge1(POST_ARGS);
1.32 kristaps 88: static int hwarn_le1(POST_ARGS);
89:
90: static int post_an(POST_ARGS);
91: static int post_at(POST_ARGS);
92: static int post_bf(POST_ARGS);
93: static int post_bl(POST_ARGS);
1.131 kristaps 94: static int post_bl_block(POST_ARGS);
95: static int post_bl_block_width(POST_ARGS);
96: static int post_bl_block_tag(POST_ARGS);
1.32 kristaps 97: static int post_bl_head(POST_ARGS);
1.154 ! kristaps 98: static int post_bx(POST_ARGS);
1.132 kristaps 99: static int post_dd(POST_ARGS);
100: static int post_dt(POST_ARGS);
1.123 kristaps 101: static int post_defaults(POST_ARGS);
1.128 kristaps 102: static int post_literal(POST_ARGS);
1.123 kristaps 103: static int post_eoln(POST_ARGS);
1.32 kristaps 104: static int post_it(POST_ARGS);
105: static int post_lb(POST_ARGS);
106: static int post_nm(POST_ARGS);
1.132 kristaps 107: static int post_os(POST_ARGS);
1.140 kristaps 108: static int post_ignpar(POST_ARGS);
1.132 kristaps 109: static int post_prol(POST_ARGS);
1.32 kristaps 110: static int post_root(POST_ARGS);
1.43 kristaps 111: static int post_rs(POST_ARGS);
1.32 kristaps 112: static int post_sh(POST_ARGS);
113: static int post_sh_body(POST_ARGS);
114: static int post_sh_head(POST_ARGS);
115: static int post_st(POST_ARGS);
1.132 kristaps 116: static int post_std(POST_ARGS);
1.57 kristaps 117: static int post_vt(POST_ARGS);
1.32 kristaps 118: static int pre_an(PRE_ARGS);
119: static int pre_bd(PRE_ARGS);
120: static int pre_bl(PRE_ARGS);
121: static int pre_dd(PRE_ARGS);
122: static int pre_display(PRE_ARGS);
123: static int pre_dt(PRE_ARGS);
124: static int pre_it(PRE_ARGS);
1.128 kristaps 125: static int pre_literal(PRE_ARGS);
1.32 kristaps 126: static int pre_os(PRE_ARGS);
1.124 kristaps 127: static int pre_par(PRE_ARGS);
1.32 kristaps 128: static int pre_sh(PRE_ARGS);
129: static int pre_ss(PRE_ARGS);
1.133 kristaps 130: static int pre_std(PRE_ARGS);
1.32 kristaps 131:
132: static v_post posts_an[] = { post_an, NULL };
1.126 kristaps 133: static v_post posts_at[] = { post_at, post_defaults, NULL };
1.128 kristaps 134: static v_post posts_bd[] = { post_literal, hwarn_eq0, bwarn_ge1, NULL };
1.32 kristaps 135: static v_post posts_bf[] = { hwarn_le1, post_bf, NULL };
1.128 kristaps 136: static v_post posts_bk[] = { hwarn_eq0, bwarn_ge1, NULL };
1.32 kristaps 137: static v_post posts_bl[] = { bwarn_ge1, post_bl, NULL };
1.154 ! kristaps 138: static v_post posts_bx[] = { post_bx, NULL };
1.151 schwarze 139: static v_post posts_bool[] = { ebool, NULL };
1.84 kristaps 140: static v_post posts_eoln[] = { post_eoln, NULL };
1.123 kristaps 141: static v_post posts_defaults[] = { post_defaults, NULL };
1.132 kristaps 142: static v_post posts_dd[] = { ewarn_ge1, post_dd, post_prol, NULL };
1.151 schwarze 143: static v_post posts_dl[] = { post_literal, bwarn_ge1, NULL };
1.132 kristaps 144: static v_post posts_dt[] = { post_dt, post_prol, NULL };
1.32 kristaps 145: static v_post posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL };
1.143 kristaps 146: static v_post posts_it[] = { post_it, NULL };
1.151 schwarze 147: static v_post posts_lb[] = { post_lb, NULL };
1.32 kristaps 148: static v_post posts_nd[] = { berr_ge1, NULL };
149: static v_post posts_nm[] = { post_nm, NULL };
1.116 kristaps 150: static v_post posts_notext[] = { ewarn_eq0, NULL };
1.132 kristaps 151: static v_post posts_os[] = { post_os, post_prol, NULL };
1.151 schwarze 152: static v_post posts_rs[] = { post_rs, NULL };
153: static v_post posts_sh[] = { post_ignpar, hwarn_ge1, bwarn_ge1, post_sh, NULL };
154: static v_post posts_sp[] = { ewarn_le1, NULL };
155: static v_post posts_ss[] = { post_ignpar, hwarn_ge1, bwarn_ge1, NULL };
156: static v_post posts_st[] = { post_st, NULL };
1.132 kristaps 157: static v_post posts_std[] = { post_std, NULL };
1.152 schwarze 158: static v_post posts_text[] = { ewarn_ge1, NULL };
1.151 schwarze 159: static v_post posts_text1[] = { ewarn_eq1, NULL };
1.57 kristaps 160: static v_post posts_vt[] = { post_vt, NULL };
1.151 schwarze 161: static v_post posts_wline[] = { bwarn_ge1, NULL };
1.32 kristaps 162: static v_pre pres_an[] = { pre_an, NULL };
1.128 kristaps 163: static v_pre pres_bd[] = { pre_display, pre_bd, pre_literal, pre_par, NULL };
1.124 kristaps 164: static v_pre pres_bl[] = { pre_bl, pre_par, NULL };
1.32 kristaps 165: static v_pre pres_d1[] = { pre_display, NULL };
1.128 kristaps 166: static v_pre pres_dl[] = { pre_literal, pre_display, NULL };
1.32 kristaps 167: static v_pre pres_dd[] = { pre_dd, NULL };
168: static v_pre pres_dt[] = { pre_dt, NULL };
1.67 kristaps 169: static v_pre pres_er[] = { NULL, NULL };
1.77 kristaps 170: static v_pre pres_fd[] = { NULL, NULL };
1.141 kristaps 171: static v_pre pres_it[] = { pre_it, pre_par, NULL };
1.32 kristaps 172: static v_pre pres_os[] = { pre_os, NULL };
1.124 kristaps 173: static v_pre pres_pp[] = { pre_par, NULL };
1.32 kristaps 174: static v_pre pres_sh[] = { pre_sh, NULL };
175: static v_pre pres_ss[] = { pre_ss, NULL };
1.133 kristaps 176: static v_pre pres_std[] = { pre_std, NULL };
1.1 kristaps 177:
178: const struct valids mdoc_valids[MDOC_MAX] = {
1.10 kristaps 179: { NULL, NULL }, /* Ap */
1.132 kristaps 180: { pres_dd, posts_dd }, /* Dd */
1.85 kristaps 181: { pres_dt, posts_dt }, /* Dt */
1.132 kristaps 182: { pres_os, posts_os }, /* Os */
1.1 kristaps 183: { pres_sh, posts_sh }, /* Sh */
184: { pres_ss, posts_ss }, /* Ss */
1.117 kristaps 185: { pres_pp, posts_notext }, /* Pp */
1.1 kristaps 186: { pres_d1, posts_wline }, /* D1 */
1.128 kristaps 187: { pres_dl, posts_dl }, /* Dl */
188: { pres_bd, posts_bd }, /* Bd */
1.1 kristaps 189: { NULL, NULL }, /* Ed */
190: { pres_bl, posts_bl }, /* Bl */
191: { NULL, NULL }, /* El */
192: { pres_it, posts_it }, /* It */
1.152 schwarze 193: { NULL, NULL }, /* Ad */
1.1 kristaps 194: { pres_an, posts_an }, /* An */
1.123 kristaps 195: { NULL, posts_defaults }, /* Ar */
1.152 schwarze 196: { NULL, NULL }, /* Cd */
1.1 kristaps 197: { NULL, NULL }, /* Cm */
198: { NULL, NULL }, /* Dv */
1.152 schwarze 199: { pres_er, NULL }, /* Er */
1.1 kristaps 200: { NULL, NULL }, /* Ev */
1.133 kristaps 201: { pres_std, posts_std }, /* Ex */
1.1 kristaps 202: { NULL, NULL }, /* Fa */
1.152 schwarze 203: { pres_fd, posts_text }, /* Fd */
1.1 kristaps 204: { NULL, NULL }, /* Fl */
1.152 schwarze 205: { NULL, NULL }, /* Fn */
206: { NULL, NULL }, /* Ft */
207: { NULL, NULL }, /* Ic */
1.51 kristaps 208: { NULL, posts_text1 }, /* In */
1.123 kristaps 209: { NULL, posts_defaults }, /* Li */
1.27 kristaps 210: { NULL, posts_nd }, /* Nd */
1.1 kristaps 211: { NULL, posts_nm }, /* Nm */
1.139 kristaps 212: { NULL, NULL }, /* Op */
1.1 kristaps 213: { NULL, NULL }, /* Ot */
1.129 kristaps 214: { NULL, posts_defaults }, /* Pa */
1.133 kristaps 215: { pres_std, posts_std }, /* Rv */
1.1 kristaps 216: { NULL, posts_st }, /* St */
217: { NULL, NULL }, /* Va */
1.57 kristaps 218: { NULL, posts_vt }, /* Vt */
1.152 schwarze 219: { NULL, posts_text }, /* Xr */
1.1 kristaps 220: { NULL, posts_text }, /* %A */
1.47 kristaps 221: { NULL, posts_text }, /* %B */ /* FIXME: can be used outside Rs/Re. */
1.54 kristaps 222: { NULL, posts_text }, /* %D */ /* FIXME: check date with mandoc_a2time(). */
1.1 kristaps 223: { NULL, posts_text }, /* %I */
224: { NULL, posts_text }, /* %J */
225: { NULL, posts_text }, /* %N */
226: { NULL, posts_text }, /* %O */
227: { NULL, posts_text }, /* %P */
228: { NULL, posts_text }, /* %R */
1.47 kristaps 229: { NULL, posts_text }, /* %T */ /* FIXME: can be used outside Rs/Re. */
1.1 kristaps 230: { NULL, posts_text }, /* %V */
231: { NULL, NULL }, /* Ac */
232: { NULL, NULL }, /* Ao */
1.139 kristaps 233: { NULL, NULL }, /* Aq */
1.1 kristaps 234: { NULL, posts_at }, /* At */
235: { NULL, NULL }, /* Bc */
236: { NULL, posts_bf }, /* Bf */
237: { NULL, NULL }, /* Bo */
1.139 kristaps 238: { NULL, NULL }, /* Bq */
1.1 kristaps 239: { NULL, NULL }, /* Bsx */
1.154 ! kristaps 240: { NULL, posts_bx }, /* Bx */
1.1 kristaps 241: { NULL, posts_bool }, /* Db */
242: { NULL, NULL }, /* Dc */
243: { NULL, NULL }, /* Do */
1.139 kristaps 244: { NULL, NULL }, /* Dq */
1.1 kristaps 245: { NULL, NULL }, /* Ec */
246: { NULL, NULL }, /* Ef */
247: { NULL, NULL }, /* Em */
248: { NULL, NULL }, /* Eo */
249: { NULL, NULL }, /* Fx */
1.152 schwarze 250: { NULL, NULL }, /* Ms */
1.1 kristaps 251: { NULL, posts_notext }, /* No */
252: { NULL, posts_notext }, /* Ns */
253: { NULL, NULL }, /* Nx */
254: { NULL, NULL }, /* Ox */
255: { NULL, NULL }, /* Pc */
1.51 kristaps 256: { NULL, posts_text1 }, /* Pf */
1.1 kristaps 257: { NULL, NULL }, /* Po */
1.139 kristaps 258: { NULL, NULL }, /* Pq */
1.1 kristaps 259: { NULL, NULL }, /* Qc */
1.139 kristaps 260: { NULL, NULL }, /* Ql */
1.1 kristaps 261: { NULL, NULL }, /* Qo */
1.139 kristaps 262: { NULL, NULL }, /* Qq */
1.1 kristaps 263: { NULL, NULL }, /* Re */
1.43 kristaps 264: { NULL, posts_rs }, /* Rs */
1.1 kristaps 265: { NULL, NULL }, /* Sc */
266: { NULL, NULL }, /* So */
1.139 kristaps 267: { NULL, NULL }, /* Sq */
1.1 kristaps 268: { NULL, posts_bool }, /* Sm */
1.152 schwarze 269: { NULL, NULL }, /* Sx */
270: { NULL, NULL }, /* Sy */
271: { NULL, NULL }, /* Tn */
1.1 kristaps 272: { NULL, NULL }, /* Ux */
273: { NULL, NULL }, /* Xc */
274: { NULL, NULL }, /* Xo */
275: { NULL, posts_fo }, /* Fo */
276: { NULL, NULL }, /* Fc */
277: { NULL, NULL }, /* Oo */
278: { NULL, NULL }, /* Oc */
1.128 kristaps 279: { NULL, posts_bk }, /* Bk */
1.1 kristaps 280: { NULL, NULL }, /* Ek */
1.84 kristaps 281: { NULL, posts_eoln }, /* Bt */
1.1 kristaps 282: { NULL, NULL }, /* Hf */
283: { NULL, NULL }, /* Fr */
1.84 kristaps 284: { NULL, posts_eoln }, /* Ud */
1.83 kristaps 285: { NULL, posts_lb }, /* Lb */
1.34 kristaps 286: { NULL, posts_notext }, /* Lp */
1.152 schwarze 287: { NULL, NULL }, /* Lk */
1.123 kristaps 288: { NULL, posts_defaults }, /* Mt */
1.139 kristaps 289: { NULL, NULL }, /* Brq */
1.1 kristaps 290: { NULL, NULL }, /* Bro */
291: { NULL, NULL }, /* Brc */
292: { NULL, posts_text }, /* %C */
293: { NULL, NULL }, /* Es */
294: { NULL, NULL }, /* En */
295: { NULL, NULL }, /* Dx */
296: { NULL, posts_text }, /* %Q */
1.33 kristaps 297: { NULL, posts_notext }, /* br */
1.119 schwarze 298: { pres_pp, posts_sp }, /* sp */
1.51 kristaps 299: { NULL, posts_text1 }, /* %U */
1.88 kristaps 300: { NULL, NULL }, /* Ta */
1.1 kristaps 301: };
302:
1.122 kristaps 303: #define RSORD_MAX 14 /* Number of `Rs' blocks. */
304:
305: static const enum mdoct rsord[RSORD_MAX] = {
306: MDOC__A,
307: MDOC__T,
308: MDOC__B,
309: MDOC__I,
310: MDOC__J,
311: MDOC__R,
312: MDOC__N,
313: MDOC__V,
314: MDOC__P,
315: MDOC__Q,
316: MDOC__D,
317: MDOC__O,
318: MDOC__C,
319: MDOC__U
320: };
321:
1.1 kristaps 322:
323: int
1.91 kristaps 324: mdoc_valid_pre(struct mdoc *mdoc, struct mdoc_node *n)
1.1 kristaps 325: {
326: v_pre *p;
327: int line, pos;
1.92 kristaps 328: char *tp;
1.1 kristaps 329:
1.150 kristaps 330: switch (n->type) {
331: case (MDOC_TEXT):
1.1 kristaps 332: tp = n->string;
333: line = n->line;
334: pos = n->pos;
1.133 kristaps 335: check_text(mdoc, line, pos, tp);
1.150 kristaps 336: /* FALLTHROUGH */
337: case (MDOC_TBL):
338: /* FALLTHROUGH */
339: case (MDOC_ROOT):
1.133 kristaps 340: return(1);
1.150 kristaps 341: default:
342: break;
1.1 kristaps 343: }
344:
1.133 kristaps 345: check_args(mdoc, n);
346:
1.1 kristaps 347: if (NULL == mdoc_valids[n->tok].pre)
348: return(1);
349: for (p = mdoc_valids[n->tok].pre; *p; p++)
350: if ( ! (*p)(mdoc, n))
351: return(0);
352: return(1);
353: }
354:
355:
356: int
357: mdoc_valid_post(struct mdoc *mdoc)
358: {
359: v_post *p;
360:
361: if (MDOC_VALID & mdoc->last->flags)
362: return(1);
363: mdoc->last->flags |= MDOC_VALID;
364:
1.150 kristaps 365: switch (mdoc->last->type) {
366: case (MDOC_TEXT):
367: /* FALLTHROUGH */
368: case (MDOC_TBL):
1.1 kristaps 369: return(1);
1.150 kristaps 370: case (MDOC_ROOT):
1.1 kristaps 371: return(post_root(mdoc));
1.150 kristaps 372: default:
373: break;
374: }
1.1 kristaps 375:
376: if (NULL == mdoc_valids[mdoc->last->tok].post)
377: return(1);
378: for (p = mdoc_valids[mdoc->last->tok].post; *p; p++)
379: if ( ! (*p)(mdoc))
380: return(0);
381:
382: return(1);
383: }
384:
1.120 kristaps 385: static int
386: check_count(struct mdoc *m, enum mdoc_type type,
387: enum check_lvl lvl, enum check_ineq ineq, int val)
388: {
389: const char *p;
1.151 schwarze 390: enum mandocerr t;
1.120 kristaps 391:
392: if (m->last->type != type)
393: return(1);
394:
395: switch (ineq) {
396: case (CHECK_LT):
397: p = "less than ";
398: if (m->last->nchild < val)
399: return(1);
400: break;
401: case (CHECK_GT):
1.151 schwarze 402: p = "more than ";
1.120 kristaps 403: if (m->last->nchild > val)
404: return(1);
405: break;
406: case (CHECK_EQ):
407: p = "";
408: if (val == m->last->nchild)
409: return(1);
410: break;
1.133 kristaps 411: default:
412: abort();
413: /* NOTREACHED */
1.120 kristaps 414: }
415:
1.151 schwarze 416: t = lvl == CHECK_WARN ? MANDOCERR_ARGCWARN : MANDOCERR_ARGCOUNT;
1.120 kristaps 417:
1.151 schwarze 418: return(mdoc_vmsg(m, t, m->last->line, m->last->pos,
419: "want %s%d children (have %d)",
1.120 kristaps 420: p, val, m->last->nchild));
421: }
422:
423: static int
424: berr_ge1(POST_ARGS)
425: {
426:
1.151 schwarze 427: return(check_count(mdoc, MDOC_BODY, CHECK_ERROR, CHECK_GT, 0));
1.120 kristaps 428: }
429:
430: static int
431: bwarn_ge1(POST_ARGS)
432: {
433: return(check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0));
434: }
435:
436: static int
1.151 schwarze 437: ewarn_eq0(POST_ARGS)
1.120 kristaps 438: {
1.151 schwarze 439: return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0));
1.120 kristaps 440: }
1.1 kristaps 441:
1.120 kristaps 442: static int
1.151 schwarze 443: ewarn_eq1(POST_ARGS)
1.1 kristaps 444: {
1.151 schwarze 445: return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1));
1.120 kristaps 446: }
1.1 kristaps 447:
1.120 kristaps 448: static int
449: ewarn_ge1(POST_ARGS)
450: {
451: return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0));
1.1 kristaps 452: }
453:
1.120 kristaps 454: static int
1.151 schwarze 455: ewarn_le1(POST_ARGS)
1.120 kristaps 456: {
1.151 schwarze 457: return(check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_LT, 2));
1.120 kristaps 458: }
1.1 kristaps 459:
1.120 kristaps 460: static int
1.151 schwarze 461: hwarn_eq0(POST_ARGS)
1.120 kristaps 462: {
1.151 schwarze 463: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0));
1.120 kristaps 464: }
1.1 kristaps 465:
1.120 kristaps 466: static int
1.151 schwarze 467: hwarn_eq1(POST_ARGS)
1.120 kristaps 468: {
1.151 schwarze 469: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 1));
1.1 kristaps 470: }
471:
1.120 kristaps 472: static int
1.151 schwarze 473: hwarn_ge1(POST_ARGS)
1.120 kristaps 474: {
1.151 schwarze 475: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_GT, 0));
1.120 kristaps 476: }
1.1 kristaps 477:
1.120 kristaps 478: static int
479: hwarn_le1(POST_ARGS)
480: {
481: return(check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_LT, 2));
482: }
1.1 kristaps 483:
1.133 kristaps 484: static void
1.92 kristaps 485: check_args(struct mdoc *m, struct mdoc_node *n)
1.1 kristaps 486: {
487: int i;
488:
489: if (NULL == n->args)
1.133 kristaps 490: return;
1.1 kristaps 491:
492: assert(n->args->argc);
493: for (i = 0; i < (int)n->args->argc; i++)
1.133 kristaps 494: check_argv(m, n, &n->args->argv[i]);
1.1 kristaps 495: }
496:
1.133 kristaps 497: static void
1.92 kristaps 498: check_argv(struct mdoc *m, struct mdoc_node *n, struct mdoc_argv *v)
1.1 kristaps 499: {
500: int i;
501:
502: for (i = 0; i < (int)v->sz; i++)
1.133 kristaps 503: check_text(m, v->line, v->pos, v->value[i]);
1.1 kristaps 504:
1.133 kristaps 505: /* FIXME: move to post_std(). */
1.1 kristaps 506:
1.133 kristaps 507: if (MDOC_Std == v->arg)
508: if ( ! (v->sz || m->meta.name))
509: mdoc_nmsg(m, n, MANDOCERR_NONAME);
1.1 kristaps 510: }
511:
1.133 kristaps 512: static void
1.112 kristaps 513: check_text(struct mdoc *m, int ln, int pos, char *p)
1.1 kristaps 514: {
1.18 kristaps 515: int c;
1.112 kristaps 516: size_t sz;
1.102 kristaps 517:
1.112 kristaps 518: for ( ; *p; p++, pos++) {
519: sz = strcspn(p, "\t\\");
520: p += (int)sz;
521:
522: if ('\0' == *p)
523: break;
524:
525: pos += (int)sz;
1.1 kristaps 526:
527: if ('\t' == *p) {
1.133 kristaps 528: if ( ! (MDOC_LITERAL & m->flags))
529: mdoc_pmsg(m, ln, pos, MANDOCERR_BADTAB);
530: continue;
1.112 kristaps 531: }
1.1 kristaps 532:
1.133 kristaps 533: if (0 == (c = mandoc_special(p))) {
534: mdoc_pmsg(m, ln, pos, MANDOCERR_BADESCAPE);
535: continue;
536: }
1.1 kristaps 537:
1.133 kristaps 538: p += c - 1;
539: pos += c - 1;
1.1 kristaps 540: }
541: }
542:
543: static int
1.59 kristaps 544: check_parent(PRE_ARGS, enum mdoct tok, enum mdoc_type t)
1.1 kristaps 545: {
546:
547: assert(n->parent);
548: if ((MDOC_ROOT == t || tok == n->parent->tok) &&
549: (t == n->parent->type))
550: return(1);
551:
1.79 kristaps 552: mdoc_vmsg(mdoc, MANDOCERR_SYNTCHILD,
553: n->line, n->pos, "want parent %s",
554: MDOC_ROOT == t ? "<root>" :
555: mdoc_macronames[tok]);
556: return(0);
1.1 kristaps 557: }
558:
559:
560: static int
561: pre_display(PRE_ARGS)
562: {
563: struct mdoc_node *node;
564:
565: if (MDOC_BLOCK != n->type)
566: return(1);
567:
568: for (node = mdoc->last->parent; node; node = node->parent)
569: if (MDOC_BLOCK == node->type)
570: if (MDOC_Bd == node->tok)
571: break;
1.128 kristaps 572:
1.135 kristaps 573: if (node)
574: mdoc_nmsg(mdoc, n, MANDOCERR_NESTEDDISP);
1.1 kristaps 575:
1.135 kristaps 576: return(1);
1.1 kristaps 577: }
578:
579:
580: static int
581: pre_bl(PRE_ARGS)
582: {
1.104 kristaps 583: int i, comp, dup;
584: const char *offs, *width;
585: enum mdoc_list lt;
586: struct mdoc_node *np;
1.1 kristaps 587:
1.91 kristaps 588: if (MDOC_BLOCK != n->type) {
1.104 kristaps 589: if (ENDBODY_NOT != n->end) {
590: assert(n->pending);
591: np = n->pending->parent;
592: } else
593: np = n->parent;
594:
595: assert(np);
596: assert(MDOC_BLOCK == np->type);
597: assert(MDOC_Bl == np->tok);
1.1 kristaps 598: return(1);
1.79 kristaps 599: }
1.1 kristaps 600:
1.91 kristaps 601: /*
602: * First figure out which kind of list to use: bind ourselves to
603: * the first mentioned list type and warn about any remaining
604: * ones. If we find no list type, we default to LIST_item.
605: */
1.1 kristaps 606:
607: /* LINTED */
1.91 kristaps 608: for (i = 0; n->args && i < (int)n->args->argc; i++) {
609: lt = LIST__NONE;
1.97 kristaps 610: dup = comp = 0;
1.99 kristaps 611: width = offs = NULL;
1.91 kristaps 612: switch (n->args->argv[i].arg) {
613: /* Set list types. */
1.1 kristaps 614: case (MDOC_Bullet):
1.91 kristaps 615: lt = LIST_bullet;
616: break;
1.1 kristaps 617: case (MDOC_Dash):
1.91 kristaps 618: lt = LIST_dash;
619: break;
1.1 kristaps 620: case (MDOC_Enum):
1.91 kristaps 621: lt = LIST_enum;
622: break;
1.1 kristaps 623: case (MDOC_Hyphen):
1.91 kristaps 624: lt = LIST_hyphen;
625: break;
1.1 kristaps 626: case (MDOC_Item):
1.91 kristaps 627: lt = LIST_item;
628: break;
1.1 kristaps 629: case (MDOC_Tag):
1.91 kristaps 630: lt = LIST_tag;
631: break;
1.1 kristaps 632: case (MDOC_Diag):
1.91 kristaps 633: lt = LIST_diag;
634: break;
1.1 kristaps 635: case (MDOC_Hang):
1.91 kristaps 636: lt = LIST_hang;
637: break;
1.1 kristaps 638: case (MDOC_Ohang):
1.91 kristaps 639: lt = LIST_ohang;
640: break;
1.1 kristaps 641: case (MDOC_Inset):
1.91 kristaps 642: lt = LIST_inset;
643: break;
1.1 kristaps 644: case (MDOC_Column):
1.91 kristaps 645: lt = LIST_column;
646: break;
647: /* Set list arguments. */
1.45 kristaps 648: case (MDOC_Compact):
1.147 kristaps 649: dup = n->norm->Bl.comp;
1.97 kristaps 650: comp = 1;
1.91 kristaps 651: break;
1.1 kristaps 652: case (MDOC_Width):
1.147 kristaps 653: dup = (NULL != n->norm->Bl.width);
1.99 kristaps 654: width = n->args->argv[i].value[0];
1.8 kristaps 655: break;
1.1 kristaps 656: case (MDOC_Offset):
1.98 kristaps 657: /* NB: this can be empty! */
658: if (n->args->argv[i].sz) {
659: offs = n->args->argv[i].value[0];
1.147 kristaps 660: dup = (NULL != n->norm->Bl.offs);
1.98 kristaps 661: break;
662: }
1.133 kristaps 663: mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.1 kristaps 664: break;
1.113 kristaps 665: default:
666: continue;
1.1 kristaps 667: }
668:
1.91 kristaps 669: /* Check: duplicate auxiliary arguments. */
670:
1.133 kristaps 671: if (dup)
672: mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
1.97 kristaps 673:
674: if (comp && ! dup)
1.147 kristaps 675: n->norm->Bl.comp = comp;
1.98 kristaps 676: if (offs && ! dup)
1.147 kristaps 677: n->norm->Bl.offs = offs;
1.99 kristaps 678: if (width && ! dup)
1.147 kristaps 679: n->norm->Bl.width = width;
1.91 kristaps 680:
681: /* Check: multiple list types. */
682:
1.147 kristaps 683: if (LIST__NONE != lt && n->norm->Bl.type != LIST__NONE)
1.133 kristaps 684: mdoc_nmsg(mdoc, n, MANDOCERR_LISTREP);
1.91 kristaps 685:
686: /* Assign list type. */
687:
1.147 kristaps 688: if (LIST__NONE != lt && n->norm->Bl.type == LIST__NONE) {
689: n->norm->Bl.type = lt;
1.109 kristaps 690: /* Set column information, too. */
691: if (LIST_column == lt) {
1.147 kristaps 692: n->norm->Bl.ncols =
1.109 kristaps 693: n->args->argv[i].sz;
1.147 kristaps 694: n->norm->Bl.cols = (const char **)
1.109 kristaps 695: n->args->argv[i].value;
696: }
697: }
1.91 kristaps 698:
699: /* The list type should come first. */
700:
1.147 kristaps 701: if (n->norm->Bl.type == LIST__NONE)
702: if (n->norm->Bl.width ||
703: n->norm->Bl.offs ||
704: n->norm->Bl.comp)
1.133 kristaps 705: mdoc_nmsg(mdoc, n, MANDOCERR_LISTFIRST);
1.91 kristaps 706:
707: continue;
708: }
709:
710: /* Allow lists to default to LIST_item. */
711:
1.147 kristaps 712: if (LIST__NONE == n->norm->Bl.type) {
1.133 kristaps 713: mdoc_nmsg(mdoc, n, MANDOCERR_LISTTYPE);
1.147 kristaps 714: n->norm->Bl.type = LIST_item;
1.79 kristaps 715: }
1.1 kristaps 716:
1.8 kristaps 717: /*
718: * Validate the width field. Some list types don't need width
719: * types and should be warned about them. Others should have it
720: * and must also be warned.
721: */
722:
1.147 kristaps 723: switch (n->norm->Bl.type) {
1.91 kristaps 724: case (LIST_tag):
1.147 kristaps 725: if (n->norm->Bl.width)
1.91 kristaps 726: break;
1.133 kristaps 727: mdoc_nmsg(mdoc, n, MANDOCERR_NOWIDTHARG);
728: break;
1.91 kristaps 729: case (LIST_column):
1.1 kristaps 730: /* FALLTHROUGH */
1.91 kristaps 731: case (LIST_diag):
1.1 kristaps 732: /* FALLTHROUGH */
1.91 kristaps 733: case (LIST_ohang):
1.53 kristaps 734: /* FALLTHROUGH */
1.91 kristaps 735: case (LIST_inset):
1.1 kristaps 736: /* FALLTHROUGH */
1.91 kristaps 737: case (LIST_item):
1.147 kristaps 738: if (n->norm->Bl.width)
1.137 kristaps 739: mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.133 kristaps 740: break;
1.8 kristaps 741: default:
742: break;
743: }
744:
1.1 kristaps 745: return(1);
746: }
747:
748:
749: static int
750: pre_bd(PRE_ARGS)
751: {
1.104 kristaps 752: int i, dup, comp;
753: enum mdoc_disp dt;
754: const char *offs;
755: struct mdoc_node *np;
1.1 kristaps 756:
1.93 kristaps 757: if (MDOC_BLOCK != n->type) {
1.104 kristaps 758: if (ENDBODY_NOT != n->end) {
759: assert(n->pending);
760: np = n->pending->parent;
761: } else
762: np = n->parent;
763:
764: assert(np);
765: assert(MDOC_BLOCK == np->type);
766: assert(MDOC_Bd == np->tok);
1.1 kristaps 767: return(1);
1.79 kristaps 768: }
1.1 kristaps 769:
770: /* LINTED */
1.93 kristaps 771: for (i = 0; n->args && i < (int)n->args->argc; i++) {
772: dt = DISP__NONE;
1.94 kristaps 773: dup = comp = 0;
774: offs = NULL;
775:
1.1 kristaps 776: switch (n->args->argv[i].arg) {
1.49 kristaps 777: case (MDOC_Centred):
1.93 kristaps 778: dt = DISP_centred;
779: break;
1.1 kristaps 780: case (MDOC_Ragged):
1.93 kristaps 781: dt = DISP_ragged;
782: break;
1.1 kristaps 783: case (MDOC_Unfilled):
1.93 kristaps 784: dt = DISP_unfilled;
785: break;
1.1 kristaps 786: case (MDOC_Filled):
1.93 kristaps 787: dt = DISP_filled;
788: break;
1.1 kristaps 789: case (MDOC_Literal):
1.93 kristaps 790: dt = DISP_literal;
1.79 kristaps 791: break;
1.93 kristaps 792: case (MDOC_File):
793: mdoc_nmsg(mdoc, n, MANDOCERR_BADDISP);
794: return(0);
795: case (MDOC_Offset):
1.94 kristaps 796: /* NB: this can be empty! */
797: if (n->args->argv[i].sz) {
798: offs = n->args->argv[i].value[0];
1.147 kristaps 799: dup = (NULL != n->norm->Bd.offs);
1.94 kristaps 800: break;
801: }
1.133 kristaps 802: mdoc_nmsg(mdoc, n, MANDOCERR_IGNARGV);
1.94 kristaps 803: break;
1.93 kristaps 804: case (MDOC_Compact):
1.94 kristaps 805: comp = 1;
1.147 kristaps 806: dup = n->norm->Bd.comp;
1.94 kristaps 807: break;
1.1 kristaps 808: default:
1.94 kristaps 809: abort();
810: /* NOTREACHED */
1.1 kristaps 811: }
812:
1.94 kristaps 813: /* Check whether we have duplicates. */
814:
1.133 kristaps 815: if (dup)
816: mdoc_nmsg(mdoc, n, MANDOCERR_ARGVREP);
1.94 kristaps 817:
818: /* Make our auxiliary assignments. */
819:
820: if (offs && ! dup)
1.147 kristaps 821: n->norm->Bd.offs = offs;
1.94 kristaps 822: if (comp && ! dup)
1.147 kristaps 823: n->norm->Bd.comp = comp;
1.94 kristaps 824:
825: /* Check whether a type has already been assigned. */
826:
1.147 kristaps 827: if (DISP__NONE != dt && n->norm->Bd.type != DISP__NONE)
1.133 kristaps 828: mdoc_nmsg(mdoc, n, MANDOCERR_DISPREP);
1.93 kristaps 829:
1.94 kristaps 830: /* Make our type assignment. */
831:
1.147 kristaps 832: if (DISP__NONE != dt && n->norm->Bd.type == DISP__NONE)
833: n->norm->Bd.type = dt;
1.93 kristaps 834: }
835:
1.147 kristaps 836: if (DISP__NONE == n->norm->Bd.type) {
1.133 kristaps 837: mdoc_nmsg(mdoc, n, MANDOCERR_DISPTYPE);
1.147 kristaps 838: n->norm->Bd.type = DISP_ragged;
1.93 kristaps 839: }
840:
841: return(1);
1.1 kristaps 842: }
843:
844:
845: static int
846: pre_ss(PRE_ARGS)
847: {
848:
849: if (MDOC_BLOCK != n->type)
850: return(1);
851: return(check_parent(mdoc, n, MDOC_Sh, MDOC_BODY));
852: }
853:
854:
855: static int
856: pre_sh(PRE_ARGS)
857: {
858:
859: if (MDOC_BLOCK != n->type)
860: return(1);
1.100 kristaps 861:
862: mdoc->regs->regs[(int)REG_nS].set = 0;
1.70 kristaps 863: return(check_parent(mdoc, n, MDOC_MAX, MDOC_ROOT));
1.1 kristaps 864: }
865:
866:
867: static int
868: pre_it(PRE_ARGS)
869: {
870:
871: if (MDOC_BLOCK != n->type)
872: return(1);
1.133 kristaps 873:
1.1 kristaps 874: return(check_parent(mdoc, n, MDOC_Bl, MDOC_BODY));
875: }
876:
877:
878: static int
879: pre_an(PRE_ARGS)
880: {
1.121 kristaps 881: int i;
1.1 kristaps 882:
1.107 kristaps 883: if (NULL == n->args)
1.1 kristaps 884: return(1);
1.121 kristaps 885:
886: for (i = 1; i < (int)n->args->argc; i++)
1.133 kristaps 887: mdoc_pmsg(mdoc, n->args->argv[i].line,
888: n->args->argv[i].pos, MANDOCERR_IGNARGV);
1.120 kristaps 889:
1.107 kristaps 890: if (MDOC_Split == n->args->argv[0].arg)
1.147 kristaps 891: n->norm->An.auth = AUTH_split;
1.107 kristaps 892: else if (MDOC_Nosplit == n->args->argv[0].arg)
1.147 kristaps 893: n->norm->An.auth = AUTH_nosplit;
1.107 kristaps 894: else
895: abort();
896:
897: return(1);
1.1 kristaps 898: }
899:
900: static int
1.133 kristaps 901: pre_std(PRE_ARGS)
1.1 kristaps 902: {
903:
1.133 kristaps 904: if (n->args && 1 == n->args->argc)
905: if (MDOC_Std == n->args->argv[0].arg)
906: return(1);
907:
908: mdoc_nmsg(mdoc, n, MANDOCERR_NOARGV);
909: return(1);
1.1 kristaps 910: }
911:
1.85 kristaps 912: static int
1.1 kristaps 913: pre_dt(PRE_ARGS)
914: {
1.54 kristaps 915:
1.1 kristaps 916: if (0 == mdoc->meta.date || mdoc->meta.os)
1.133 kristaps 917: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
918:
1.1 kristaps 919: if (mdoc->meta.title)
1.133 kristaps 920: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
921:
1.1 kristaps 922: return(1);
923: }
924:
925: static int
926: pre_os(PRE_ARGS)
927: {
928:
929: if (NULL == mdoc->meta.title || 0 == mdoc->meta.date)
1.133 kristaps 930: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
931:
1.1 kristaps 932: if (mdoc->meta.os)
1.133 kristaps 933: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
934:
1.1 kristaps 935: return(1);
936: }
937:
938: static int
939: pre_dd(PRE_ARGS)
940: {
941:
942: if (mdoc->meta.title || mdoc->meta.os)
1.133 kristaps 943: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGOOO);
944:
1.1 kristaps 945: if (mdoc->meta.date)
1.133 kristaps 946: mdoc_nmsg(mdoc, n, MANDOCERR_PROLOGREP);
947:
1.1 kristaps 948: return(1);
949: }
950:
951:
952: static int
953: post_bf(POST_ARGS)
954: {
1.105 kristaps 955: struct mdoc_node *np;
1.113 kristaps 956: enum mdocargt arg;
1.105 kristaps 957:
958: /*
959: * Unlike other data pointers, these are "housed" by the HEAD
960: * element, which contains the goods.
961: */
1.1 kristaps 962:
1.105 kristaps 963: if (MDOC_HEAD != mdoc->last->type) {
964: if (ENDBODY_NOT != mdoc->last->end) {
965: assert(mdoc->last->pending);
966: np = mdoc->last->pending->parent->head;
967: } else if (MDOC_BLOCK != mdoc->last->type) {
968: np = mdoc->last->parent->head;
969: } else
970: np = mdoc->last->head;
971:
972: assert(np);
973: assert(MDOC_HEAD == np->type);
974: assert(MDOC_Bf == np->tok);
1.1 kristaps 975: return(1);
1.105 kristaps 976: }
1.1 kristaps 977:
1.105 kristaps 978: np = mdoc->last;
1.106 kristaps 979: assert(MDOC_BLOCK == np->parent->type);
980: assert(MDOC_Bf == np->parent->tok);
1.1 kristaps 981:
1.105 kristaps 982: /*
983: * Cannot have both argument and parameter.
984: * If neither is specified, let it through with a warning.
985: */
986:
1.106 kristaps 987: if (np->parent->args && np->child) {
1.105 kristaps 988: mdoc_nmsg(mdoc, np, MANDOCERR_SYNTARGVCOUNT);
1.79 kristaps 989: return(0);
1.133 kristaps 990: } else if (NULL == np->parent->args && NULL == np->child) {
991: mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
992: return(1);
993: }
1.105 kristaps 994:
995: /* Extract argument into data. */
996:
1.106 kristaps 997: if (np->parent->args) {
998: arg = np->parent->args->argv[0].arg;
1.105 kristaps 999: if (MDOC_Emphasis == arg)
1.147 kristaps 1000: np->norm->Bf.font = FONT_Em;
1.105 kristaps 1001: else if (MDOC_Literal == arg)
1.147 kristaps 1002: np->norm->Bf.font = FONT_Li;
1.105 kristaps 1003: else if (MDOC_Symbolic == arg)
1.147 kristaps 1004: np->norm->Bf.font = FONT_Sy;
1.105 kristaps 1005: else
1006: abort();
1.13 kristaps 1007: return(1);
1.79 kristaps 1008: }
1.1 kristaps 1009:
1.105 kristaps 1010: /* Extract parameter into data. */
1.1 kristaps 1011:
1.105 kristaps 1012: if (0 == strcmp(np->child->string, "Em"))
1.147 kristaps 1013: np->norm->Bf.font = FONT_Em;
1.105 kristaps 1014: else if (0 == strcmp(np->child->string, "Li"))
1.147 kristaps 1015: np->norm->Bf.font = FONT_Li;
1.105 kristaps 1016: else if (0 == strcmp(np->child->string, "Sy"))
1.147 kristaps 1017: np->norm->Bf.font = FONT_Sy;
1.133 kristaps 1018: else
1019: mdoc_nmsg(mdoc, np, MANDOCERR_FONTTYPE);
1.1 kristaps 1020:
1.105 kristaps 1021: return(1);
1.1 kristaps 1022: }
1023:
1024: static int
1.31 kristaps 1025: post_lb(POST_ARGS)
1026: {
1.127 kristaps 1027: const char *p;
1028: char *buf;
1029: size_t sz;
1030:
1.151 schwarze 1031: check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1);
1032:
1.127 kristaps 1033: assert(mdoc->last->child);
1034: assert(MDOC_TEXT == mdoc->last->child->type);
1035:
1036: p = mdoc_a2lib(mdoc->last->child->string);
1037:
1038: /* If lookup ok, replace with table value. */
1.31 kristaps 1039:
1.127 kristaps 1040: if (p) {
1041: free(mdoc->last->child->string);
1042: mdoc->last->child->string = mandoc_strdup(p);
1.31 kristaps 1043: return(1);
1.127 kristaps 1044: }
1045:
1046: /* If not, use "library ``xxxx''. */
1047:
1048: sz = strlen(mdoc->last->child->string) +
1049: 2 + strlen("\\(lqlibrary\\(rq");
1050: buf = mandoc_malloc(sz);
1051: snprintf(buf, sz, "library \\(lq%s\\(rq",
1052: mdoc->last->child->string);
1053: free(mdoc->last->child->string);
1054: mdoc->last->child->string = buf;
1055: return(1);
1.84 kristaps 1056: }
1057:
1058: static int
1059: post_eoln(POST_ARGS)
1060: {
1061:
1.133 kristaps 1062: if (mdoc->last->child)
1063: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
1064: return(1);
1.31 kristaps 1065: }
1066:
1067:
1068: static int
1.57 kristaps 1069: post_vt(POST_ARGS)
1070: {
1071: const struct mdoc_node *n;
1072:
1073: /*
1074: * The Vt macro comes in both ELEM and BLOCK form, both of which
1075: * have different syntaxes (yet more context-sensitive
1.152 schwarze 1076: * behaviour). ELEM types must have a child, which is already
1077: * guaranteed by the in_line parsing routine; BLOCK types,
1.57 kristaps 1078: * specifically the BODY, should only have TEXT children.
1079: */
1080:
1081: if (MDOC_BODY != mdoc->last->type)
1082: return(1);
1083:
1084: for (n = mdoc->last->child; n; n = n->next)
1085: if (MDOC_TEXT != n->type)
1.133 kristaps 1086: mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
1.57 kristaps 1087:
1088: return(1);
1089: }
1090:
1091:
1092: static int
1.1 kristaps 1093: post_nm(POST_ARGS)
1094: {
1.132 kristaps 1095: char buf[BUFSIZ];
1.129 kristaps 1096:
1097: /* If no child specified, make sure we have the meta name. */
1.1 kristaps 1098:
1.129 kristaps 1099: if (NULL == mdoc->last->child && NULL == mdoc->meta.name) {
1100: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME);
1.1 kristaps 1101: return(1);
1.129 kristaps 1102: } else if (mdoc->meta.name)
1.1 kristaps 1103: return(1);
1.129 kristaps 1104:
1105: /* If no meta name, set it from the child. */
1106:
1.132 kristaps 1107: if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
1108: return(0);
1.129 kristaps 1109:
1.132 kristaps 1110: mdoc->meta.name = mandoc_strdup(buf);
1.129 kristaps 1111:
1112: return(1);
1.1 kristaps 1113: }
1114:
1.123 kristaps 1115: static int
1.128 kristaps 1116: post_literal(POST_ARGS)
1117: {
1118:
1119: /*
1120: * The `Dl' (note "el" not "one") and `Bd' macros unset the
1121: * MDOC_LITERAL flag as they leave. Note that `Bd' only sets
1122: * this in literal mode, but it doesn't hurt to just switch it
1123: * off in general since displays can't be nested.
1124: */
1125:
1126: if (MDOC_BODY == mdoc->last->type)
1.134 kristaps 1127: mdoc->flags &= ~MDOC_LITERAL;
1.128 kristaps 1128:
1129: return(1);
1130: }
1131:
1132: static int
1.123 kristaps 1133: post_defaults(POST_ARGS)
1134: {
1135: struct mdoc_node *nn;
1136:
1137: /*
1138: * The `Ar' defaults to "file ..." if no value is provided as an
1.129 kristaps 1139: * argument; the `Mt' and `Pa' macros use "~"; the `Li' just
1140: * gets an empty string.
1.123 kristaps 1141: */
1142:
1143: if (mdoc->last->child)
1144: return(1);
1145:
1146: nn = mdoc->last;
1147: mdoc->next = MDOC_NEXT_CHILD;
1148:
1149: switch (nn->tok) {
1150: case (MDOC_Ar):
1151: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "file"))
1152: return(0);
1153: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "..."))
1154: return(0);
1155: break;
1.126 kristaps 1156: case (MDOC_At):
1157: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "AT&T"))
1158: return(0);
1159: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "UNIX"))
1160: return(0);
1161: break;
1.123 kristaps 1162: case (MDOC_Li):
1163: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, ""))
1164: return(0);
1165: break;
1.129 kristaps 1166: case (MDOC_Pa):
1167: /* FALLTHROUGH */
1.123 kristaps 1168: case (MDOC_Mt):
1169: if ( ! mdoc_word_alloc(mdoc, nn->line, nn->pos, "~"))
1170: return(0);
1171: break;
1172: default:
1173: abort();
1174: /* NOTREACHED */
1175: }
1176:
1177: mdoc->last = nn;
1178: return(1);
1179: }
1.1 kristaps 1180:
1181: static int
1182: post_at(POST_ARGS)
1183: {
1.126 kristaps 1184: const char *p, *q;
1185: char *buf;
1186: size_t sz;
1.1 kristaps 1187:
1.126 kristaps 1188: /*
1189: * If we have a child, look it up in the standard keys. If a
1190: * key exist, use that instead of the child; if it doesn't,
1191: * prefix "AT&T UNIX " to the existing data.
1192: */
1193:
1.1 kristaps 1194: if (NULL == mdoc->last->child)
1195: return(1);
1.126 kristaps 1196:
1.79 kristaps 1197: assert(MDOC_TEXT == mdoc->last->child->type);
1.126 kristaps 1198: p = mdoc_a2att(mdoc->last->child->string);
1199:
1200: if (p) {
1201: free(mdoc->last->child->string);
1202: mdoc->last->child->string = mandoc_strdup(p);
1203: } else {
1204: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADATT);
1205: p = "AT&T UNIX ";
1206: q = mdoc->last->child->string;
1207: sz = strlen(p) + strlen(q) + 1;
1208: buf = mandoc_malloc(sz);
1209: strlcpy(buf, p, sz);
1210: strlcat(buf, q, sz);
1211: free(mdoc->last->child->string);
1212: mdoc->last->child->string = buf;
1213: }
1214:
1215: return(1);
1.1 kristaps 1216: }
1217:
1218: static int
1219: post_an(POST_ARGS)
1220: {
1.107 kristaps 1221: struct mdoc_node *np;
1.1 kristaps 1222:
1.107 kristaps 1223: np = mdoc->last;
1.152 schwarze 1224: if (AUTH__NONE == np->norm->An.auth) {
1225: if (0 == np->child)
1226: check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_GT, 0);
1227: } else if (np->child)
1.151 schwarze 1228: check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 0);
1.133 kristaps 1229:
1230: return(1);
1.1 kristaps 1231: }
1232:
1233:
1234: static int
1235: post_it(POST_ARGS)
1236: {
1.89 kristaps 1237: int i, cols, rc;
1238: enum mdoc_list lt;
1.1 kristaps 1239: struct mdoc_node *n, *c;
1.89 kristaps 1240: enum mandocerr er;
1.1 kristaps 1241:
1242: if (MDOC_BLOCK != mdoc->last->type)
1243: return(1);
1244:
1245: n = mdoc->last->parent->parent;
1.147 kristaps 1246: lt = n->norm->Bl.type;
1.89 kristaps 1247:
1248: if (LIST__NONE == lt) {
1.79 kristaps 1249: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_LISTTYPE);
1.133 kristaps 1250: return(1);
1.79 kristaps 1251: }
1.1 kristaps 1252:
1.89 kristaps 1253: switch (lt) {
1254: case (LIST_tag):
1255: if (mdoc->last->head->child)
1.1 kristaps 1256: break;
1.89 kristaps 1257: /* FIXME: give this a dummy value. */
1.133 kristaps 1258: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1.1 kristaps 1259: break;
1.89 kristaps 1260: case (LIST_hang):
1.1 kristaps 1261: /* FALLTHROUGH */
1.89 kristaps 1262: case (LIST_ohang):
1.1 kristaps 1263: /* FALLTHROUGH */
1.89 kristaps 1264: case (LIST_inset):
1.1 kristaps 1265: /* FALLTHROUGH */
1.89 kristaps 1266: case (LIST_diag):
1.1 kristaps 1267: if (NULL == mdoc->last->head->child)
1.133 kristaps 1268: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOARGS);
1.1 kristaps 1269: break;
1.89 kristaps 1270: case (LIST_bullet):
1.1 kristaps 1271: /* FALLTHROUGH */
1.89 kristaps 1272: case (LIST_dash):
1.1 kristaps 1273: /* FALLTHROUGH */
1.89 kristaps 1274: case (LIST_enum):
1.1 kristaps 1275: /* FALLTHROUGH */
1.89 kristaps 1276: case (LIST_hyphen):
1.108 schwarze 1277: if (NULL == mdoc->last->body->child)
1.133 kristaps 1278: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1.1 kristaps 1279: /* FALLTHROUGH */
1.89 kristaps 1280: case (LIST_item):
1.1 kristaps 1281: if (mdoc->last->head->child)
1.133 kristaps 1282: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_ARGSLOST);
1.1 kristaps 1283: break;
1.89 kristaps 1284: case (LIST_column):
1.147 kristaps 1285: cols = (int)n->norm->Bl.ncols;
1.89 kristaps 1286:
1.87 kristaps 1287: assert(NULL == mdoc->last->head->child);
1.89 kristaps 1288:
1.87 kristaps 1289: if (NULL == mdoc->last->body->child)
1.133 kristaps 1290: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NOBODY);
1.87 kristaps 1291:
1.89 kristaps 1292: for (i = 0, c = mdoc->last->child; c; c = c->next)
1.87 kristaps 1293: if (MDOC_BODY == c->type)
1294: i++;
1.38 kristaps 1295:
1.89 kristaps 1296: if (i < cols)
1297: er = MANDOCERR_ARGCOUNT;
1298: else if (i == cols || i == cols + 1)
1.38 kristaps 1299: break;
1.89 kristaps 1300: else
1301: er = MANDOCERR_SYNTARGCOUNT;
1.38 kristaps 1302:
1.89 kristaps 1303: rc = mdoc_vmsg(mdoc, er,
1.79 kristaps 1304: mdoc->last->line, mdoc->last->pos,
1305: "columns == %d (have %d)", cols, i);
1.89 kristaps 1306: return(rc);
1.1 kristaps 1307: default:
1308: break;
1309: }
1310:
1311: return(1);
1312: }
1313:
1.131 kristaps 1314: static int
1315: post_bl_block(POST_ARGS)
1316: {
1317: struct mdoc_node *n;
1318:
1319: /*
1320: * These are fairly complicated, so we've broken them into two
1321: * functions. post_bl_block_tag() is called when a -tag is
1322: * specified, but no -width (it must be guessed). The second
1323: * when a -width is specified (macro indicators must be
1324: * rewritten into real lengths).
1325: */
1326:
1327: n = mdoc->last;
1328:
1.147 kristaps 1329: if (LIST_tag == n->norm->Bl.type &&
1330: NULL == n->norm->Bl.width) {
1.131 kristaps 1331: if ( ! post_bl_block_tag(mdoc))
1332: return(0);
1.147 kristaps 1333: } else if (NULL != n->norm->Bl.width) {
1.131 kristaps 1334: if ( ! post_bl_block_width(mdoc))
1335: return(0);
1336: } else
1337: return(1);
1338:
1.147 kristaps 1339: assert(n->norm->Bl.width);
1.131 kristaps 1340: return(1);
1341: }
1342:
1343: static int
1344: post_bl_block_width(POST_ARGS)
1345: {
1346: size_t width;
1347: int i;
1348: enum mdoct tok;
1349: struct mdoc_node *n;
1350: char buf[NUMSIZ];
1351:
1352: n = mdoc->last;
1353:
1354: /*
1355: * Calculate the real width of a list from the -width string,
1356: * which may contain a macro (with a known default width), a
1357: * literal string, or a scaling width.
1358: *
1359: * If the value to -width is a macro, then we re-write it to be
1360: * the macro's width as set in share/tmac/mdoc/doc-common.
1361: */
1362:
1.147 kristaps 1363: if (0 == strcmp(n->norm->Bl.width, "Ds"))
1.131 kristaps 1364: width = 6;
1.147 kristaps 1365: else if (MDOC_MAX == (tok = mdoc_hash_find(n->norm->Bl.width)))
1.131 kristaps 1366: return(1);
1.133 kristaps 1367: else if (0 == (width = mdoc_macro2len(tok))) {
1368: mdoc_nmsg(mdoc, n, MANDOCERR_BADWIDTH);
1369: return(1);
1370: }
1.131 kristaps 1371:
1372: /* The value already exists: free and reallocate it. */
1373:
1374: assert(n->args);
1375:
1376: for (i = 0; i < (int)n->args->argc; i++)
1377: if (MDOC_Width == n->args->argv[i].arg)
1378: break;
1379:
1380: assert(i < (int)n->args->argc);
1381:
1382: snprintf(buf, NUMSIZ, "%zun", width);
1383: free(n->args->argv[i].value[0]);
1384: n->args->argv[i].value[0] = mandoc_strdup(buf);
1385:
1386: /* Set our width! */
1.147 kristaps 1387: n->norm->Bl.width = n->args->argv[i].value[0];
1.131 kristaps 1388: return(1);
1389: }
1390:
1391: static int
1392: post_bl_block_tag(POST_ARGS)
1393: {
1394: struct mdoc_node *n, *nn;
1395: size_t sz, ssz;
1396: int i;
1397: char buf[NUMSIZ];
1398:
1399: /*
1400: * Calculate the -width for a `Bl -tag' list if it hasn't been
1401: * provided. Uses the first head macro. NOTE AGAIN: this is
1402: * ONLY if the -width argument has NOT been provided. See
1403: * post_bl_block_width() for converting the -width string.
1404: */
1405:
1406: sz = 10;
1407: n = mdoc->last;
1408:
1409: for (nn = n->body->child; nn; nn = nn->next) {
1410: if (MDOC_It != nn->tok)
1411: continue;
1412:
1413: assert(MDOC_BLOCK == nn->type);
1414: nn = nn->head->child;
1415:
1.138 kristaps 1416: if (nn == NULL)
1.131 kristaps 1417: break;
1418:
1419: if (MDOC_TEXT == nn->type) {
1420: sz = strlen(nn->string) + 1;
1421: break;
1422: }
1423:
1424: if (0 != (ssz = mdoc_macro2len(nn->tok)))
1425: sz = ssz;
1426:
1427: break;
1428: }
1429:
1430: /* Defaults to ten ens. */
1431:
1432: snprintf(buf, NUMSIZ, "%zun", sz);
1433:
1434: /*
1435: * We have to dynamically add this to the macro's argument list.
1436: * We're guaranteed that a MDOC_Width doesn't already exist.
1437: */
1438:
1439: assert(n->args);
1440: i = (int)(n->args->argc)++;
1441:
1442: n->args->argv = mandoc_realloc(n->args->argv,
1443: n->args->argc * sizeof(struct mdoc_argv));
1444:
1445: n->args->argv[i].arg = MDOC_Width;
1446: n->args->argv[i].line = n->line;
1447: n->args->argv[i].pos = n->pos;
1448: n->args->argv[i].sz = 1;
1449: n->args->argv[i].value = mandoc_malloc(sizeof(char *));
1450: n->args->argv[i].value[0] = mandoc_strdup(buf);
1451:
1452: /* Set our width! */
1.147 kristaps 1453: n->norm->Bl.width = n->args->argv[i].value[0];
1.131 kristaps 1454: return(1);
1455: }
1456:
1.1 kristaps 1457:
1458: static int
1.14 kristaps 1459: post_bl_head(POST_ARGS)
1460: {
1.130 kristaps 1461: struct mdoc_node *np, *nn, *nnp;
1462: int i, j;
1.14 kristaps 1463:
1.147 kristaps 1464: if (LIST_column != mdoc->last->norm->Bl.type)
1.130 kristaps 1465: /* FIXME: this should be ERROR class... */
1466: return(hwarn_eq0(mdoc));
1.14 kristaps 1467:
1.130 kristaps 1468: /*
1469: * Convert old-style lists, where the column width specifiers
1470: * trail as macro parameters, to the new-style ("normal-form")
1471: * lists where they're argument values following -column.
1472: */
1473:
1474: /* First, disallow both types and allow normal-form. */
1475:
1476: /*
1477: * TODO: technically, we can accept both and just merge the two
1478: * lists, but I'll leave that for another day.
1479: */
1480:
1.147 kristaps 1481: if (mdoc->last->norm->Bl.ncols && mdoc->last->nchild) {
1.130 kristaps 1482: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_COLUMNS);
1483: return(0);
1484: } else if (NULL == mdoc->last->child)
1.90 kristaps 1485: return(1);
1.130 kristaps 1486:
1487: np = mdoc->last->parent;
1488: assert(np->args);
1489:
1490: for (j = 0; j < (int)np->args->argc; j++)
1491: if (MDOC_Column == np->args->argv[j].arg)
1492: break;
1493:
1494: assert(j < (int)np->args->argc);
1495: assert(0 == np->args->argv[j].sz);
1496:
1497: /*
1498: * Accomodate for new-style groff column syntax. Shuffle the
1499: * child nodes, all of which must be TEXT, as arguments for the
1500: * column field. Then, delete the head children.
1501: */
1502:
1503: np->args->argv[j].sz = (size_t)mdoc->last->nchild;
1504: np->args->argv[j].value = mandoc_malloc
1505: ((size_t)mdoc->last->nchild * sizeof(char *));
1506:
1.147 kristaps 1507: mdoc->last->norm->Bl.ncols = np->args->argv[j].sz;
1508: mdoc->last->norm->Bl.cols = (const char **)np->args->argv[j].value;
1.130 kristaps 1509:
1510: for (i = 0, nn = mdoc->last->child; nn; i++) {
1511: np->args->argv[j].value[i] = nn->string;
1512: nn->string = NULL;
1513: nnp = nn;
1514: nn = nn->next;
1515: mdoc_node_delete(NULL, nnp);
1.65 kristaps 1516: }
1.14 kristaps 1517:
1.130 kristaps 1518: mdoc->last->nchild = 0;
1519: mdoc->last->child = NULL;
1520:
1521: return(1);
1.14 kristaps 1522: }
1523:
1524: static int
1.1 kristaps 1525: post_bl(POST_ARGS)
1526: {
1527: struct mdoc_node *n;
1528:
1.14 kristaps 1529: if (MDOC_HEAD == mdoc->last->type)
1530: return(post_bl_head(mdoc));
1.131 kristaps 1531: if (MDOC_BLOCK == mdoc->last->type)
1532: return(post_bl_block(mdoc));
1.1 kristaps 1533: if (MDOC_BODY != mdoc->last->type)
1534: return(1);
1535:
1536: for (n = mdoc->last->child; n; n = n->next) {
1.141 kristaps 1537: switch (n->tok) {
1.146 schwarze 1538: case (MDOC_Lp):
1.141 kristaps 1539: /* FALLTHROUGH */
1540: case (MDOC_Pp):
1541: mdoc_nmsg(mdoc, n, MANDOCERR_CHILD);
1.146 schwarze 1542: /* FALLTHROUGH */
1543: case (MDOC_It):
1544: /* FALLTHROUGH */
1545: case (MDOC_Sm):
1.55 kristaps 1546: continue;
1.141 kristaps 1547: default:
1548: break;
1549: }
1550:
1.79 kristaps 1551: mdoc_nmsg(mdoc, n, MANDOCERR_SYNTCHILD);
1552: return(0);
1.1 kristaps 1553: }
1554:
1555: return(1);
1556: }
1557:
1558: static int
1559: ebool(struct mdoc *mdoc)
1560: {
1561:
1.151 schwarze 1562: if (NULL == mdoc->last->child) {
1563: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_MACROEMPTY);
1564: mdoc_node_delete(mdoc, mdoc->last);
1.133 kristaps 1565: return(1);
1.151 schwarze 1566: }
1567: check_count(mdoc, MDOC_ELEM, CHECK_WARN, CHECK_EQ, 1);
1.133 kristaps 1568:
1569: assert(MDOC_TEXT == mdoc->last->child->type);
1.1 kristaps 1570:
1.133 kristaps 1571: if (0 == strcmp(mdoc->last->child->string, "on"))
1.1 kristaps 1572: return(1);
1.133 kristaps 1573: if (0 == strcmp(mdoc->last->child->string, "off"))
1574: return(1);
1575:
1576: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADBOOL);
1577: return(1);
1.1 kristaps 1578: }
1579:
1580: static int
1581: post_root(POST_ARGS)
1582: {
1.133 kristaps 1583: int erc;
1584: struct mdoc_node *n;
1.1 kristaps 1585:
1.133 kristaps 1586: erc = 0;
1587:
1588: /* Check that we have a finished prologue. */
1589:
1590: if ( ! (MDOC_PBODY & mdoc->flags)) {
1591: erc++;
1.79 kristaps 1592: mdoc_nmsg(mdoc, mdoc->first, MANDOCERR_NODOCPROLOG);
1.133 kristaps 1593: }
1594:
1595: n = mdoc->first;
1596: assert(n);
1597:
1598: /* Check that we begin with a proper `Sh'. */
1599:
1600: if (NULL == n->child) {
1601: erc++;
1602: mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
1603: } else if (MDOC_BLOCK != n->child->type ||
1604: MDOC_Sh != n->child->tok) {
1605: erc++;
1606: /* Can this be lifted? See rxdebug.1 for example. */
1607: mdoc_nmsg(mdoc, n, MANDOCERR_NODOCBODY);
1608: }
1.1 kristaps 1609:
1.133 kristaps 1610: return(erc ? 0 : 1);
1.1 kristaps 1611: }
1612:
1613: static int
1614: post_st(POST_ARGS)
1615: {
1.151 schwarze 1616: struct mdoc_node *ch;
1617: const char *p;
1.128 kristaps 1618:
1.151 schwarze 1619: if (NULL == (ch = mdoc->last->child)) {
1620: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_MACROEMPTY);
1621: mdoc_node_delete(mdoc, mdoc->last);
1622: return(1);
1623: }
1.128 kristaps 1624:
1.151 schwarze 1625: assert(MDOC_TEXT == ch->type);
1.128 kristaps 1626:
1.151 schwarze 1627: if (NULL == (p = mdoc_a2st(ch->string))) {
1.128 kristaps 1628: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADSTANDARD);
1629: mdoc_node_delete(mdoc, mdoc->last);
1630: } else {
1.151 schwarze 1631: free(ch->string);
1632: ch->string = mandoc_strdup(p);
1.128 kristaps 1633: }
1.1 kristaps 1634:
1.128 kristaps 1635: return(1);
1.1 kristaps 1636: }
1637:
1638: static int
1.43 kristaps 1639: post_rs(POST_ARGS)
1640: {
1.122 kristaps 1641: struct mdoc_node *nn, *next, *prev;
1642: int i, j;
1.43 kristaps 1643:
1.151 schwarze 1644: switch (mdoc->last->type) {
1645: case (MDOC_HEAD):
1646: check_count(mdoc, MDOC_HEAD, CHECK_WARN, CHECK_EQ, 0);
1647: return(1);
1648: case (MDOC_BODY):
1649: if (mdoc->last->child)
1650: break;
1651: check_count(mdoc, MDOC_BODY, CHECK_WARN, CHECK_GT, 0);
1652: return(1);
1653: default:
1.43 kristaps 1654: return(1);
1.151 schwarze 1655: }
1.43 kristaps 1656:
1.122 kristaps 1657: /*
1658: * Make sure only certain types of nodes are allowed within the
1659: * the `Rs' body. Delete offending nodes and raise a warning.
1660: * Do this before re-ordering for the sake of clarity.
1661: */
1662:
1663: next = NULL;
1664: for (nn = mdoc->last->child; nn; nn = next) {
1665: for (i = 0; i < RSORD_MAX; i++)
1666: if (nn->tok == rsord[i])
1667: break;
1668:
1669: if (i < RSORD_MAX) {
1.153 kristaps 1670: if (MDOC__J == rsord[i] || MDOC__B == rsord[i])
1671: mdoc->last->norm->Rs.quote_T++;
1.122 kristaps 1672: next = nn->next;
1673: continue;
1674: }
1675:
1676: next = nn->next;
1677: mdoc_nmsg(mdoc, nn, MANDOCERR_CHILD);
1678: mdoc_node_delete(mdoc, nn);
1679: }
1680:
1681: /*
1682: * The full `Rs' block needs special handling to order the
1683: * sub-elements according to `rsord'. Pick through each element
1684: * and correctly order it. This is a insertion sort.
1685: */
1686:
1687: next = NULL;
1688: for (nn = mdoc->last->child->next; nn; nn = next) {
1689: /* Determine order of `nn'. */
1690: for (i = 0; i < RSORD_MAX; i++)
1691: if (rsord[i] == nn->tok)
1692: break;
1693:
1694: /*
1695: * Remove `nn' from the chain. This somewhat
1696: * repeats mdoc_node_unlink(), but since we're
1697: * just re-ordering, there's no need for the
1698: * full unlink process.
1699: */
1700:
1701: if (NULL != (next = nn->next))
1702: next->prev = nn->prev;
1703:
1704: if (NULL != (prev = nn->prev))
1705: prev->next = nn->next;
1706:
1707: nn->prev = nn->next = NULL;
1708:
1709: /*
1710: * Scan back until we reach a node that's
1711: * ordered before `nn'.
1712: */
1713:
1714: for ( ; prev ; prev = prev->prev) {
1715: /* Determine order of `prev'. */
1716: for (j = 0; j < RSORD_MAX; j++)
1717: if (rsord[j] == prev->tok)
1718: break;
1719:
1720: if (j <= i)
1721: break;
1722: }
1723:
1724: /*
1725: * Set `nn' back into its correct place in front
1726: * of the `prev' node.
1727: */
1728:
1729: nn->prev = prev;
1730:
1731: if (prev) {
1732: if (prev->next)
1733: prev->next->prev = nn;
1734: nn->next = prev->next;
1735: prev->next = nn;
1736: } else {
1737: mdoc->last->child->prev = nn;
1738: nn->next = mdoc->last->child;
1739: mdoc->last->child = nn;
1.43 kristaps 1740: }
1.122 kristaps 1741: }
1.43 kristaps 1742:
1743: return(1);
1744: }
1745:
1746: static int
1.1 kristaps 1747: post_sh(POST_ARGS)
1748: {
1749:
1750: if (MDOC_HEAD == mdoc->last->type)
1751: return(post_sh_head(mdoc));
1752: if (MDOC_BODY == mdoc->last->type)
1753: return(post_sh_body(mdoc));
1754:
1755: return(1);
1756: }
1757:
1758: static int
1759: post_sh_body(POST_ARGS)
1760: {
1761: struct mdoc_node *n;
1762:
1.29 kristaps 1763: if (SEC_NAME != mdoc->lastsec)
1.1 kristaps 1764: return(1);
1765:
1766: /*
1767: * Warn if the NAME section doesn't contain the `Nm' and `Nd'
1768: * macros (can have multiple `Nm' and one `Nd'). Note that the
1769: * children of the BODY declaration can also be "text".
1770: */
1771:
1.133 kristaps 1772: if (NULL == (n = mdoc->last->child)) {
1773: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1774: return(1);
1775: }
1.1 kristaps 1776:
1777: for ( ; n && n->next; n = n->next) {
1778: if (MDOC_ELEM == n->type && MDOC_Nm == n->tok)
1779: continue;
1.28 kristaps 1780: if (MDOC_TEXT == n->type)
1781: continue;
1.133 kristaps 1782: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1.1 kristaps 1783: }
1784:
1.41 kristaps 1785: assert(n);
1.27 kristaps 1786: if (MDOC_BLOCK == n->type && MDOC_Nd == n->tok)
1.1 kristaps 1787: return(1);
1.133 kristaps 1788:
1789: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_BADNAMESEC);
1790: return(1);
1.1 kristaps 1791: }
1792:
1793: static int
1794: post_sh_head(POST_ARGS)
1795: {
1.132 kristaps 1796: char buf[BUFSIZ];
1797: enum mdoc_sec sec;
1.1 kristaps 1798:
1799: /*
1800: * Process a new section. Sections are either "named" or
1.125 kristaps 1801: * "custom". Custom sections are user-defined, while named ones
1802: * follow a conventional order and may only appear in certain
1803: * manual sections.
1.1 kristaps 1804: */
1805:
1.132 kristaps 1806: if ( ! concat(mdoc, buf, mdoc->last->child, BUFSIZ))
1807: return(0);
1.1 kristaps 1808:
1.72 kristaps 1809: sec = mdoc_str2sec(buf);
1.1 kristaps 1810:
1.125 kristaps 1811: /* The NAME should be first. */
1.1 kristaps 1812:
1.72 kristaps 1813: if (SEC_NAME != sec && SEC_NONE == mdoc->lastnamed)
1.125 kristaps 1814: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NAMESECFIRST);
1815:
1816: /* The SYNOPSIS gets special attention in other areas. */
1817:
1818: if (SEC_SYNOPSIS == sec)
1819: mdoc->flags |= MDOC_SYNOPSIS;
1820: else
1821: mdoc->flags &= ~MDOC_SYNOPSIS;
1822:
1823: /* Mark our last section. */
1824:
1825: mdoc->lastsec = sec;
1826:
1827: /* We don't care about custom sections after this. */
1.72 kristaps 1828:
1.1 kristaps 1829: if (SEC_CUSTOM == sec)
1830: return(1);
1.72 kristaps 1831:
1.125 kristaps 1832: /*
1833: * Check whether our non-custom section is being repeated or is
1834: * out of order.
1835: */
1836:
1.1 kristaps 1837: if (sec == mdoc->lastnamed)
1.125 kristaps 1838: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECREP);
1.72 kristaps 1839:
1.1 kristaps 1840: if (sec < mdoc->lastnamed)
1.125 kristaps 1841: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECOOO);
1842:
1843: /* Mark the last named section. */
1844:
1845: mdoc->lastnamed = sec;
1846:
1847: /* Check particular section/manual conventions. */
1.1 kristaps 1848:
1.125 kristaps 1849: assert(mdoc->meta.msec);
1.1 kristaps 1850:
1851: switch (sec) {
1.125 kristaps 1852: case (SEC_RETURN_VALUES):
1853: /* FALLTHROUGH */
1854: case (SEC_ERRORS):
1855: /* FALLTHROUGH */
1.1 kristaps 1856: case (SEC_LIBRARY):
1.78 kristaps 1857: if (*mdoc->meta.msec == '2')
1858: break;
1859: if (*mdoc->meta.msec == '3')
1860: break;
1861: if (*mdoc->meta.msec == '9')
1.1 kristaps 1862: break;
1.125 kristaps 1863: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_SECMSEC);
1864: break;
1.1 kristaps 1865: default:
1866: break;
1867: }
1.140 kristaps 1868:
1869: return(1);
1870: }
1871:
1872: static int
1873: post_ignpar(POST_ARGS)
1874: {
1875: struct mdoc_node *np;
1876:
1877: if (MDOC_BODY != mdoc->last->type)
1878: return(1);
1879:
1.143 kristaps 1880: if (NULL != (np = mdoc->last->child))
1.140 kristaps 1881: if (MDOC_Pp == np->tok || MDOC_Lp == np->tok) {
1882: mdoc_nmsg(mdoc, np, MANDOCERR_IGNPAR);
1883: mdoc_node_delete(mdoc, np);
1884: }
1885:
1886: if (NULL != (np = mdoc->last->last))
1887: if (MDOC_Pp == np->tok || MDOC_Lp == np->tok) {
1888: mdoc_nmsg(mdoc, np, MANDOCERR_IGNPAR);
1889: mdoc_node_delete(mdoc, np);
1890: }
1.1 kristaps 1891:
1.117 kristaps 1892: return(1);
1893: }
1894:
1895: static int
1.124 kristaps 1896: pre_par(PRE_ARGS)
1.117 kristaps 1897: {
1898:
1.118 kristaps 1899: if (NULL == mdoc->last)
1900: return(1);
1.141 kristaps 1901: if (MDOC_ELEM != n->type && MDOC_BLOCK != n->type)
1902: return(1);
1.118 kristaps 1903:
1.124 kristaps 1904: /*
1905: * Don't allow prior `Lp' or `Pp' prior to a paragraph-type
1906: * block: `Lp', `Pp', or non-compact `Bd' or `Bl'.
1907: */
1.118 kristaps 1908:
1909: if (MDOC_Pp != mdoc->last->tok && MDOC_Lp != mdoc->last->tok)
1.117 kristaps 1910: return(1);
1.147 kristaps 1911: if (MDOC_Bl == n->tok && n->norm->Bl.comp)
1.117 kristaps 1912: return(1);
1.147 kristaps 1913: if (MDOC_Bd == n->tok && n->norm->Bd.comp)
1.141 kristaps 1914: return(1);
1.147 kristaps 1915: if (MDOC_It == n->tok && n->parent->norm->Bl.comp)
1.117 kristaps 1916: return(1);
1917:
1918: mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_IGNPAR);
1919: mdoc_node_delete(mdoc, mdoc->last);
1.128 kristaps 1920: return(1);
1921: }
1922:
1923: static int
1924: pre_literal(PRE_ARGS)
1925: {
1926:
1927: if (MDOC_BODY != n->type)
1928: return(1);
1929:
1930: /*
1931: * The `Dl' (note "el" not "one") and `Bd -literal' and `Bd
1932: * -unfilled' macros set MDOC_LITERAL on entrance to the body.
1933: */
1934:
1935: switch (n->tok) {
1936: case (MDOC_Dl):
1937: mdoc->flags |= MDOC_LITERAL;
1938: break;
1939: case (MDOC_Bd):
1.147 kristaps 1940: if (DISP_literal == n->norm->Bd.type)
1.128 kristaps 1941: mdoc->flags |= MDOC_LITERAL;
1.147 kristaps 1942: if (DISP_unfilled == n->norm->Bd.type)
1.128 kristaps 1943: mdoc->flags |= MDOC_LITERAL;
1944: break;
1945: default:
1946: abort();
1947: /* NOTREACHED */
1948: }
1949:
1.1 kristaps 1950: return(1);
1951: }
1.132 kristaps 1952:
1953: static int
1954: post_dd(POST_ARGS)
1955: {
1.135 kristaps 1956: char buf[DATESIZE];
1.132 kristaps 1957: struct mdoc_node *n;
1958:
1959: n = mdoc->last;
1960:
1961: if (NULL == n->child) {
1962: mdoc->meta.date = time(NULL);
1.133 kristaps 1963: return(1);
1.132 kristaps 1964: }
1965:
1.135 kristaps 1966: if ( ! concat(mdoc, buf, n->child, DATESIZE))
1.132 kristaps 1967: return(0);
1968:
1969: mdoc->meta.date = mandoc_a2time
1970: (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
1971:
1972: if (0 == mdoc->meta.date) {
1.133 kristaps 1973: mdoc_nmsg(mdoc, n, MANDOCERR_BADDATE);
1.132 kristaps 1974: mdoc->meta.date = time(NULL);
1975: }
1976:
1977: return(1);
1978: }
1979:
1980: static int
1981: post_dt(POST_ARGS)
1982: {
1983: struct mdoc_node *nn, *n;
1984: const char *cp;
1985: char *p;
1986:
1987: n = mdoc->last;
1988:
1989: if (mdoc->meta.title)
1990: free(mdoc->meta.title);
1991: if (mdoc->meta.vol)
1992: free(mdoc->meta.vol);
1993: if (mdoc->meta.arch)
1994: free(mdoc->meta.arch);
1995:
1996: mdoc->meta.title = mdoc->meta.vol = mdoc->meta.arch = NULL;
1997:
1998: /* First make all characters uppercase. */
1999:
2000: if (NULL != (nn = n->child))
2001: for (p = nn->string; *p; p++) {
2002: if (toupper((u_char)*p) == *p)
2003: continue;
1.133 kristaps 2004:
2005: /*
2006: * FIXME: don't be lazy: have this make all
2007: * characters be uppercase and just warn once.
2008: */
2009: mdoc_nmsg(mdoc, nn, MANDOCERR_UPPERCASE);
1.132 kristaps 2010: break;
2011: }
2012:
2013: /* Handles: `.Dt'
2014: * --> title = unknown, volume = local, msec = 0, arch = NULL
2015: */
2016:
2017: if (NULL == (nn = n->child)) {
2018: /* XXX: make these macro values. */
2019: /* FIXME: warn about missing values. */
2020: mdoc->meta.title = mandoc_strdup("UNKNOWN");
2021: mdoc->meta.vol = mandoc_strdup("LOCAL");
2022: mdoc->meta.msec = mandoc_strdup("1");
2023: return(1);
2024: }
2025:
2026: /* Handles: `.Dt TITLE'
2027: * --> title = TITLE, volume = local, msec = 0, arch = NULL
2028: */
2029:
2030: mdoc->meta.title = mandoc_strdup
2031: ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
2032:
2033: if (NULL == (nn = nn->next)) {
2034: /* FIXME: warn about missing msec. */
2035: /* XXX: make this a macro value. */
2036: mdoc->meta.vol = mandoc_strdup("LOCAL");
2037: mdoc->meta.msec = mandoc_strdup("1");
2038: return(1);
2039: }
2040:
2041: /* Handles: `.Dt TITLE SEC'
2042: * --> title = TITLE, volume = SEC is msec ?
2043: * format(msec) : SEC,
2044: * msec = SEC is msec ? atoi(msec) : 0,
2045: * arch = NULL
2046: */
2047:
2048: cp = mdoc_a2msec(nn->string);
2049: if (cp) {
2050: mdoc->meta.vol = mandoc_strdup(cp);
2051: mdoc->meta.msec = mandoc_strdup(nn->string);
1.133 kristaps 2052: } else {
2053: mdoc_nmsg(mdoc, n, MANDOCERR_BADMSEC);
1.132 kristaps 2054: mdoc->meta.vol = mandoc_strdup(nn->string);
2055: mdoc->meta.msec = mandoc_strdup(nn->string);
1.133 kristaps 2056: }
1.132 kristaps 2057:
2058: if (NULL == (nn = nn->next))
2059: return(1);
2060:
2061: /* Handles: `.Dt TITLE SEC VOL'
2062: * --> title = TITLE, volume = VOL is vol ?
2063: * format(VOL) :
2064: * VOL is arch ? format(arch) :
2065: * VOL
2066: */
2067:
2068: cp = mdoc_a2vol(nn->string);
2069: if (cp) {
2070: free(mdoc->meta.vol);
2071: mdoc->meta.vol = mandoc_strdup(cp);
2072: } else {
2073: /* FIXME: warn about bad arch. */
2074: cp = mdoc_a2arch(nn->string);
2075: if (NULL == cp) {
2076: free(mdoc->meta.vol);
2077: mdoc->meta.vol = mandoc_strdup(nn->string);
2078: } else
2079: mdoc->meta.arch = mandoc_strdup(cp);
2080: }
2081:
2082: /* Ignore any subsequent parameters... */
2083: /* FIXME: warn about subsequent parameters. */
2084:
2085: return(1);
2086: }
2087:
2088: static int
2089: post_prol(POST_ARGS)
2090: {
2091: /*
2092: * Remove prologue macros from the document after they're
2093: * processed. The final document uses mdoc_meta for these
2094: * values and discards the originals.
2095: */
2096:
2097: mdoc_node_delete(mdoc, mdoc->last);
2098: if (mdoc->meta.title && mdoc->meta.date && mdoc->meta.os)
2099: mdoc->flags |= MDOC_PBODY;
1.154 ! kristaps 2100:
! 2101: return(1);
! 2102: }
! 2103:
! 2104: static int
! 2105: post_bx(POST_ARGS)
! 2106: {
! 2107: struct mdoc_node *n;
! 2108:
! 2109: /*
! 2110: * Make `Bx's second argument always start with an uppercase
! 2111: * letter. Groff checks if it's an "accepted" term, but we just
! 2112: * uppercase blindly.
! 2113: */
! 2114:
! 2115: n = mdoc->last->child;
! 2116: if (n && NULL != (n = n->next))
! 2117: *n->string = toupper((unsigned char)*n->string);
1.132 kristaps 2118:
2119: return(1);
2120: }
2121:
2122: static int
2123: post_os(POST_ARGS)
2124: {
2125: struct mdoc_node *n;
2126: char buf[BUFSIZ];
2127: #ifndef OSNAME
2128: struct utsname utsname;
2129: #endif
2130:
2131: n = mdoc->last;
2132:
2133: /*
2134: * Set the operating system by way of the `Os' macro. Note that
2135: * if an argument isn't provided and -DOSNAME="\"foo\"" is
2136: * provided during compilation, this value will be used instead
2137: * of filling in "sysname release" from uname().
2138: */
2139:
2140: if (mdoc->meta.os)
2141: free(mdoc->meta.os);
2142:
2143: if ( ! concat(mdoc, buf, n->child, BUFSIZ))
2144: return(0);
2145:
2146: /* XXX: yes, these can all be dynamically-adjusted buffers, but
2147: * it's really not worth the extra hackery.
2148: */
2149:
2150: if ('\0' == buf[0]) {
2151: #ifdef OSNAME
2152: if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
2153: mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2154: return(0);
2155: }
2156: #else /*!OSNAME */
1.136 kristaps 2157: if (uname(&utsname)) {
2158: mdoc_nmsg(mdoc, n, MANDOCERR_UNAME);
2159: mdoc->meta.os = mandoc_strdup("UNKNOWN");
2160: return(post_prol(mdoc));
2161: }
1.132 kristaps 2162:
2163: if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
2164: mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2165: return(0);
2166: }
1.136 kristaps 2167: if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) {
1.132 kristaps 2168: mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2169: return(0);
2170: }
2171: if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
2172: mdoc_nmsg(mdoc, n, MANDOCERR_MEM);
2173: return(0);
2174: }
2175: #endif /*!OSNAME*/
2176: }
2177:
2178: mdoc->meta.os = mandoc_strdup(buf);
2179: return(1);
2180: }
2181:
2182: static int
2183: post_std(POST_ARGS)
2184: {
2185: struct mdoc_node *nn, *n;
2186:
2187: n = mdoc->last;
2188:
2189: /*
2190: * Macros accepting `-std' as an argument have the name of the
2191: * current document (`Nm') filled in as the argument if it's not
2192: * provided.
2193: */
2194:
2195: if (n->child)
2196: return(1);
1.133 kristaps 2197:
1.132 kristaps 2198: if (NULL == mdoc->meta.name)
2199: return(1);
2200:
2201: nn = n;
2202: mdoc->next = MDOC_NEXT_CHILD;
2203:
2204: if ( ! mdoc_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name))
2205: return(0);
1.133 kristaps 2206:
1.132 kristaps 2207: mdoc->last = nn;
2208: return(1);
2209: }
2210:
2211: static int
2212: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
2213: {
2214:
2215: p[0] = '\0';
2216:
2217: /*
2218: * Concatenate sibling nodes together. All siblings must be of
2219: * type MDOC_TEXT or an assertion is raised. Concatenation is
1.133 kristaps 2220: * separated by a single whitespace. Returns 0 on fatal (string
2221: * overrun) error.
1.132 kristaps 2222: */
2223:
2224: for ( ; n; n = n->next) {
2225: assert(MDOC_TEXT == n->type);
2226:
2227: if (strlcat(p, n->string, sz) >= sz) {
2228: mdoc_nmsg(m, n, MANDOCERR_MEM);
2229: return(0);
2230: }
2231:
2232: if (NULL == n->next)
2233: continue;
2234:
2235: if (strlcat(p, " ", sz) >= sz) {
2236: mdoc_nmsg(m, n, MANDOCERR_MEM);
2237: return(0);
2238: }
2239: }
2240:
2241: return(1);
2242: }
2243:
CVSweb