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