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