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