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