Annotation of src/usr.bin/mandoc/mdoc_action.c, Revision 1.34
1.34 ! schwarze 1: /* $Id: mdoc_action.c,v 1.33 2010/05/14 19:52:43 schwarze Exp $ */
1.1 kristaps 2: /*
1.2 schwarze 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.24 schwarze 17: #ifndef OSNAME
1.1 kristaps 18: #include <sys/utsname.h>
1.24 schwarze 19: #endif
1.1 kristaps 20:
21: #include <assert.h>
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include <string.h>
1.24 schwarze 25: #include <time.h>
1.1 kristaps 26:
27: #include "libmdoc.h"
1.25 schwarze 28: #include "libmandoc.h"
1.1 kristaps 29:
1.20 schwarze 30: #define POST_ARGS struct mdoc *m, struct mdoc_node *n
1.1 kristaps 31: #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
32:
1.25 schwarze 33: #define NUMSIZ 32
34: #define DATESIZ 32
35:
1.1 kristaps 36: struct actions {
37: int (*pre)(PRE_ARGS);
38: int (*post)(POST_ARGS);
39: };
40:
1.25 schwarze 41: static int concat(struct mdoc *, char *,
42: const struct mdoc_node *, size_t);
1.31 schwarze 43: static inline int order_rs(enum mdoct);
1.23 schwarze 44:
1.1 kristaps 45: static int post_ar(POST_ARGS);
1.15 schwarze 46: static int post_at(POST_ARGS);
1.1 kristaps 47: static int post_bl(POST_ARGS);
1.6 schwarze 48: static int post_bl_head(POST_ARGS);
1.15 schwarze 49: static int post_bl_tagwidth(POST_ARGS);
1.1 kristaps 50: static int post_bl_width(POST_ARGS);
51: static int post_dd(POST_ARGS);
52: static int post_display(POST_ARGS);
53: static int post_dt(POST_ARGS);
1.15 schwarze 54: static int post_lb(POST_ARGS);
1.1 kristaps 55: static int post_nm(POST_ARGS);
56: static int post_os(POST_ARGS);
1.25 schwarze 57: static int post_pa(POST_ARGS);
1.1 kristaps 58: static int post_prol(POST_ARGS);
1.23 schwarze 59: static int post_rs(POST_ARGS);
1.1 kristaps 60: static int post_sh(POST_ARGS);
1.15 schwarze 61: static int post_st(POST_ARGS);
1.1 kristaps 62: static int post_std(POST_ARGS);
63:
64: static int pre_bd(PRE_ARGS);
1.22 schwarze 65: static int pre_bl(PRE_ARGS);
1.1 kristaps 66: static int pre_dl(PRE_ARGS);
1.22 schwarze 67: static int pre_offset(PRE_ARGS);
1.1 kristaps 68:
1.21 schwarze 69: static const struct actions mdoc_actions[MDOC_MAX] = {
1.4 schwarze 70: { NULL, NULL }, /* Ap */
1.1 kristaps 71: { NULL, post_dd }, /* Dd */
72: { NULL, post_dt }, /* Dt */
73: { NULL, post_os }, /* Os */
74: { NULL, post_sh }, /* Sh */
75: { NULL, NULL }, /* Ss */
76: { NULL, NULL }, /* Pp */
77: { NULL, NULL }, /* D1 */
78: { pre_dl, post_display }, /* Dl */
79: { pre_bd, post_display }, /* Bd */
80: { NULL, NULL }, /* Ed */
1.22 schwarze 81: { pre_bl, post_bl }, /* Bl */
1.1 kristaps 82: { NULL, NULL }, /* El */
83: { NULL, NULL }, /* It */
84: { NULL, NULL }, /* Ad */
85: { NULL, NULL }, /* An */
86: { NULL, post_ar }, /* Ar */
1.20 schwarze 87: { NULL, NULL }, /* Cd */
1.1 kristaps 88: { NULL, NULL }, /* Cm */
89: { NULL, NULL }, /* Dv */
90: { NULL, NULL }, /* Er */
91: { NULL, NULL }, /* Ev */
92: { NULL, post_std }, /* Ex */
93: { NULL, NULL }, /* Fa */
94: { NULL, NULL }, /* Fd */
95: { NULL, NULL }, /* Fl */
96: { NULL, NULL }, /* Fn */
97: { NULL, NULL }, /* Ft */
98: { NULL, NULL }, /* Ic */
99: { NULL, NULL }, /* In */
100: { NULL, NULL }, /* Li */
101: { NULL, NULL }, /* Nd */
102: { NULL, post_nm }, /* Nm */
103: { NULL, NULL }, /* Op */
104: { NULL, NULL }, /* Ot */
1.25 schwarze 105: { NULL, post_pa }, /* Pa */
1.1 kristaps 106: { NULL, post_std }, /* Rv */
1.15 schwarze 107: { NULL, post_st }, /* St */
1.1 kristaps 108: { NULL, NULL }, /* Va */
109: { NULL, NULL }, /* Vt */
110: { NULL, NULL }, /* Xr */
111: { NULL, NULL }, /* %A */
112: { NULL, NULL }, /* %B */
113: { NULL, NULL }, /* %D */
114: { NULL, NULL }, /* %I */
115: { NULL, NULL }, /* %J */
116: { NULL, NULL }, /* %N */
117: { NULL, NULL }, /* %O */
118: { NULL, NULL }, /* %P */
119: { NULL, NULL }, /* %R */
120: { NULL, NULL }, /* %T */
121: { NULL, NULL }, /* %V */
122: { NULL, NULL }, /* Ac */
123: { NULL, NULL }, /* Ao */
124: { NULL, NULL }, /* Aq */
1.15 schwarze 125: { NULL, post_at }, /* At */
1.1 kristaps 126: { NULL, NULL }, /* Bc */
127: { NULL, NULL }, /* Bf */
128: { NULL, NULL }, /* Bo */
129: { NULL, NULL }, /* Bq */
130: { NULL, NULL }, /* Bsx */
131: { NULL, NULL }, /* Bx */
132: { NULL, NULL }, /* Db */
133: { NULL, NULL }, /* Dc */
134: { NULL, NULL }, /* Do */
135: { NULL, NULL }, /* Dq */
136: { NULL, NULL }, /* Ec */
137: { NULL, NULL }, /* Ef */
138: { NULL, NULL }, /* Em */
139: { NULL, NULL }, /* Eo */
140: { NULL, NULL }, /* Fx */
141: { NULL, NULL }, /* Ms */
142: { NULL, NULL }, /* No */
143: { NULL, NULL }, /* Ns */
144: { NULL, NULL }, /* Nx */
145: { NULL, NULL }, /* Ox */
146: { NULL, NULL }, /* Pc */
147: { NULL, NULL }, /* Pf */
148: { NULL, NULL }, /* Po */
149: { NULL, NULL }, /* Pq */
150: { NULL, NULL }, /* Qc */
151: { NULL, NULL }, /* Ql */
152: { NULL, NULL }, /* Qo */
153: { NULL, NULL }, /* Qq */
154: { NULL, NULL }, /* Re */
1.23 schwarze 155: { NULL, post_rs }, /* Rs */
1.1 kristaps 156: { NULL, NULL }, /* Sc */
157: { NULL, NULL }, /* So */
158: { NULL, NULL }, /* Sq */
159: { NULL, NULL }, /* Sm */
160: { NULL, NULL }, /* Sx */
161: { NULL, NULL }, /* Sy */
162: { NULL, NULL }, /* Tn */
163: { NULL, NULL }, /* Ux */
164: { NULL, NULL }, /* Xc */
165: { NULL, NULL }, /* Xo */
166: { NULL, NULL }, /* Fo */
167: { NULL, NULL }, /* Fc */
168: { NULL, NULL }, /* Oo */
169: { NULL, NULL }, /* Oc */
170: { NULL, NULL }, /* Bk */
171: { NULL, NULL }, /* Ek */
172: { NULL, NULL }, /* Bt */
173: { NULL, NULL }, /* Hf */
174: { NULL, NULL }, /* Fr */
175: { NULL, NULL }, /* Ud */
1.15 schwarze 176: { NULL, post_lb }, /* Lb */
1.1 kristaps 177: { NULL, NULL }, /* Lp */
1.24 schwarze 178: { NULL, NULL }, /* Lk */
1.1 kristaps 179: { NULL, NULL }, /* Mt */
180: { NULL, NULL }, /* Brq */
181: { NULL, NULL }, /* Bro */
182: { NULL, NULL }, /* Brc */
183: { NULL, NULL }, /* %C */
184: { NULL, NULL }, /* Es */
185: { NULL, NULL }, /* En */
186: { NULL, NULL }, /* Dx */
187: { NULL, NULL }, /* %Q */
1.16 schwarze 188: { NULL, NULL }, /* br */
189: { NULL, NULL }, /* sp */
1.24 schwarze 190: { NULL, NULL }, /* %U */
1.1 kristaps 191: };
192:
1.24 schwarze 193: #define RSORD_MAX 14
1.23 schwarze 194:
1.31 schwarze 195: static const enum mdoct rsord[RSORD_MAX] = {
1.23 schwarze 196: MDOC__A,
197: MDOC__T,
198: MDOC__B,
199: MDOC__I,
200: MDOC__J,
201: MDOC__R,
202: MDOC__N,
203: MDOC__V,
204: MDOC__P,
205: MDOC__Q,
206: MDOC__D,
207: MDOC__O,
1.24 schwarze 208: MDOC__C,
209: MDOC__U
1.23 schwarze 210: };
1.15 schwarze 211:
1.1 kristaps 212:
213: int
214: mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
215: {
216:
217: switch (n->type) {
218: case (MDOC_ROOT):
219: /* FALLTHROUGH */
220: case (MDOC_TEXT):
221: return(1);
222: default:
223: break;
224: }
225:
226: if (NULL == mdoc_actions[n->tok].pre)
227: return(1);
228: return((*mdoc_actions[n->tok].pre)(m, n));
229: }
230:
231:
232: int
233: mdoc_action_post(struct mdoc *m)
234: {
235:
236: if (MDOC_ACTED & m->last->flags)
237: return(1);
238: m->last->flags |= MDOC_ACTED;
239:
240: switch (m->last->type) {
241: case (MDOC_TEXT):
242: /* FALLTHROUGH */
243: case (MDOC_ROOT):
244: return(1);
245: default:
246: break;
247: }
248:
249: if (NULL == mdoc_actions[m->last->tok].post)
250: return(1);
1.20 schwarze 251: return((*mdoc_actions[m->last->tok].post)(m, m->last));
1.1 kristaps 252: }
253:
254:
1.25 schwarze 255: /*
256: * Concatenate sibling nodes together. All siblings must be of type
257: * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
258: * single whitespace.
259: */
1.1 kristaps 260: static int
1.25 schwarze 261: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
1.1 kristaps 262: {
263:
1.25 schwarze 264: assert(sz);
265: p[0] = '\0';
1.1 kristaps 266: for ( ; n; n = n->next) {
267: assert(MDOC_TEXT == n->type);
1.25 schwarze 268: if (strlcat(p, n->string, sz) >= sz)
1.12 schwarze 269: return(mdoc_nerr(m, n, ETOOLONG));
1.1 kristaps 270: if (NULL == n->next)
271: continue;
1.25 schwarze 272: if (strlcat(p, " ", sz) >= sz)
1.12 schwarze 273: return(mdoc_nerr(m, n, ETOOLONG));
1.1 kristaps 274: }
275:
276: return(1);
277: }
278:
279:
1.25 schwarze 280: /*
281: * Macros accepting `-std' as an argument have the name of the current
282: * document (`Nm') filled in as the argument if it's not provided.
283: */
1.1 kristaps 284: static int
285: post_std(POST_ARGS)
286: {
1.20 schwarze 287: struct mdoc_node *nn;
1.1 kristaps 288:
1.20 schwarze 289: if (n->child)
1.1 kristaps 290: return(1);
1.20 schwarze 291:
292: nn = n;
293: m->next = MDOC_NEXT_CHILD;
1.1 kristaps 294: assert(m->meta.name);
1.20 schwarze 295: if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
296: return(0);
297: m->last = nn;
1.1 kristaps 298: return(1);
299: }
300:
301:
1.25 schwarze 302: /*
303: * The `Nm' macro's first use sets the name of the document. See also
304: * post_std(), etc.
305: */
1.1 kristaps 306: static int
307: post_nm(POST_ARGS)
308: {
1.25 schwarze 309: char buf[BUFSIZ];
1.1 kristaps 310:
311: if (m->meta.name)
312: return(1);
1.25 schwarze 313: if ( ! concat(m, buf, n->child, BUFSIZ))
1.1 kristaps 314: return(0);
1.25 schwarze 315: m->meta.name = mandoc_strdup(buf);
1.1 kristaps 316: return(1);
317: }
318:
319:
1.25 schwarze 320: /*
321: * Look up the value of `Lb' for matching predefined strings. If it has
322: * one, then substitute the current value for the formatted value. Note
323: * that the lookup may fail (we can provide arbitrary strings).
324: */
325: /* ARGSUSED */
1.1 kristaps 326: static int
1.15 schwarze 327: post_lb(POST_ARGS)
328: {
329: const char *p;
330: char *buf;
331: size_t sz;
332:
1.20 schwarze 333: assert(MDOC_TEXT == n->child->type);
334: p = mdoc_a2lib(n->child->string);
1.25 schwarze 335:
336: if (p) {
1.20 schwarze 337: free(n->child->string);
1.25 schwarze 338: n->child->string = mandoc_strdup(p);
1.15 schwarze 339: return(1);
340: }
341:
1.25 schwarze 342: sz = strlen(n->child->string) +
343: 2 + strlen("\\(lqlibrary\\(rq");
344: buf = mandoc_malloc(sz);
345: snprintf(buf, sz, "library \\(lq%s\\(rq", n->child->string);
1.20 schwarze 346: free(n->child->string);
1.25 schwarze 347: n->child->string = buf;
1.15 schwarze 348: return(1);
349: }
350:
351:
1.25 schwarze 352: /*
353: * Substitute the value of `St' for the corresponding formatted string.
354: * We're guaranteed that this exists (it's been verified during the
355: * validation phase).
356: */
357: /* ARGSUSED */
1.15 schwarze 358: static int
359: post_st(POST_ARGS)
360: {
361: const char *p;
362:
1.20 schwarze 363: assert(MDOC_TEXT == n->child->type);
364: p = mdoc_a2st(n->child->string);
1.34 ! schwarze 365: if (p != NULL) {
! 366: free(n->child->string);
! 367: n->child->string = mandoc_strdup(p);
! 368: }
1.15 schwarze 369: return(1);
370: }
371:
372:
1.25 schwarze 373: /*
374: * Look up the standard string in a table. We know that it exists from
375: * the validation phase, so assert on failure. If a standard key wasn't
376: * supplied, supply the default ``AT&T UNIX''.
377: */
1.15 schwarze 378: static int
379: post_at(POST_ARGS)
380: {
1.34 ! schwarze 381: struct mdoc_node *nn;
! 382: const char *p, *q;
! 383: char *buf;
! 384: size_t sz;
1.15 schwarze 385:
1.20 schwarze 386: if (n->child) {
387: assert(MDOC_TEXT == n->child->type);
388: p = mdoc_a2att(n->child->string);
1.34 ! schwarze 389: if (p) {
! 390: free(n->child->string);
! 391: n->child->string = mandoc_strdup(p);
! 392: } else {
! 393: p = "AT&T UNIX ";
! 394: q = n->child->string;
! 395: sz = strlen(p) + strlen(q) + 1;
! 396: buf = mandoc_malloc(sz);
! 397: strlcpy(buf, p, sz);
! 398: strlcat(buf, q, sz);
! 399: free(n->child->string);
! 400: n->child->string = buf;
! 401: }
1.15 schwarze 402: return(1);
403: }
404:
1.20 schwarze 405: nn = n;
1.15 schwarze 406: m->next = MDOC_NEXT_CHILD;
1.20 schwarze 407: if ( ! mdoc_word_alloc(m, nn->line, nn->pos, "AT&T UNIX"))
1.15 schwarze 408: return(0);
1.20 schwarze 409: m->last = nn;
1.15 schwarze 410: return(1);
411: }
412:
413:
1.25 schwarze 414: /*
415: * Mark the current section. The ``named'' section (lastnamed) is set
416: * whenever the current section isn't a custom section--we use this to
417: * keep track of section ordering. Also check that the section is
418: * allowed within the document's manual section.
419: */
1.15 schwarze 420: static int
1.1 kristaps 421: post_sh(POST_ARGS)
422: {
423: enum mdoc_sec sec;
1.25 schwarze 424: char buf[BUFSIZ];
1.1 kristaps 425:
1.20 schwarze 426: if (MDOC_HEAD != n->type)
1.1 kristaps 427: return(1);
428:
1.25 schwarze 429: if ( ! concat(m, buf, n->child, BUFSIZ))
1.1 kristaps 430: return(0);
1.32 schwarze 431: sec = mdoc_str2sec(buf);
1.30 schwarze 432: /*
433: * The first section should always make us move into a non-new
434: * state.
435: */
1.29 schwarze 436: if (SEC_NONE == m->lastnamed || SEC_CUSTOM != sec)
1.1 kristaps 437: m->lastnamed = sec;
438:
1.25 schwarze 439: /* Some sections only live in certain manual sections. */
440:
1.1 kristaps 441: switch ((m->lastsec = sec)) {
442: case (SEC_RETURN_VALUES):
443: /* FALLTHROUGH */
444: case (SEC_ERRORS):
445: switch (m->meta.msec) {
446: case (2):
447: /* FALLTHROUGH */
448: case (3):
449: /* FALLTHROUGH */
450: case (9):
451: break;
452: default:
1.20 schwarze 453: return(mdoc_nwarn(m, n, EBADSEC));
1.1 kristaps 454: }
455: break;
456: default:
457: break;
458: }
459: return(1);
460: }
461:
462:
1.25 schwarze 463: /*
464: * Parse out the contents of `Dt'. See in-line documentation for how we
465: * handle the various fields of this macro.
466: */
1.1 kristaps 467: static int
468: post_dt(POST_ARGS)
469: {
1.20 schwarze 470: struct mdoc_node *nn;
1.1 kristaps 471: const char *cp;
472: char *ep;
473: long lval;
474:
475: if (m->meta.title)
476: free(m->meta.title);
477: if (m->meta.vol)
478: free(m->meta.vol);
479: if (m->meta.arch)
480: free(m->meta.arch);
481:
482: m->meta.title = m->meta.vol = m->meta.arch = NULL;
483: m->meta.msec = 0;
484:
485: /* Handles: `.Dt'
486: * --> title = unknown, volume = local, msec = 0, arch = NULL
487: */
488:
1.20 schwarze 489: if (NULL == (nn = n->child)) {
1.25 schwarze 490: /* XXX: make these macro values. */
491: m->meta.title = mandoc_strdup("unknown");
492: m->meta.vol = mandoc_strdup("local");
1.20 schwarze 493: return(post_prol(m, n));
1.1 kristaps 494: }
495:
496: /* Handles: `.Dt TITLE'
497: * --> title = TITLE, volume = local, msec = 0, arch = NULL
498: */
499:
1.25 schwarze 500: m->meta.title = mandoc_strdup(nn->string);
1.1 kristaps 501:
1.20 schwarze 502: if (NULL == (nn = nn->next)) {
1.25 schwarze 503: /* XXX: make this a macro value. */
504: m->meta.vol = mandoc_strdup("local");
1.20 schwarze 505: return(post_prol(m, n));
1.1 kristaps 506: }
507:
508: /* Handles: `.Dt TITLE SEC'
509: * --> title = TITLE, volume = SEC is msec ?
510: * format(msec) : SEC,
511: * msec = SEC is msec ? atoi(msec) : 0,
512: * arch = NULL
513: */
514:
1.20 schwarze 515: cp = mdoc_a2msec(nn->string);
1.1 kristaps 516: if (cp) {
1.25 schwarze 517: /* FIXME: where is strtonum!? */
518: m->meta.vol = mandoc_strdup(cp);
1.20 schwarze 519: lval = strtol(nn->string, &ep, 10);
520: if (nn->string[0] != '\0' && *ep == '\0')
1.1 kristaps 521: m->meta.msec = (int)lval;
1.25 schwarze 522: } else
523: m->meta.vol = mandoc_strdup(nn->string);
1.1 kristaps 524:
1.20 schwarze 525: if (NULL == (nn = nn->next))
526: return(post_prol(m, n));
1.1 kristaps 527:
528: /* Handles: `.Dt TITLE SEC VOL'
529: * --> title = TITLE, volume = VOL is vol ?
530: * format(VOL) :
531: * VOL is arch ? format(arch) :
532: * VOL
533: */
534:
1.20 schwarze 535: cp = mdoc_a2vol(nn->string);
1.1 kristaps 536: if (cp) {
537: free(m->meta.vol);
1.25 schwarze 538: m->meta.vol = mandoc_strdup(cp);
1.1 kristaps 539: } else {
1.20 schwarze 540: cp = mdoc_a2arch(nn->string);
1.1 kristaps 541: if (NULL == cp) {
542: free(m->meta.vol);
1.25 schwarze 543: m->meta.vol = mandoc_strdup(nn->string);
544: } else
545: m->meta.arch = mandoc_strdup(cp);
1.1 kristaps 546: }
547:
548: /* Ignore any subsequent parameters... */
1.25 schwarze 549: /* FIXME: warn about subsequent parameters. */
1.1 kristaps 550:
1.20 schwarze 551: return(post_prol(m, n));
1.1 kristaps 552: }
553:
554:
1.25 schwarze 555: /*
556: * Set the operating system by way of the `Os' macro. Note that if an
557: * argument isn't provided and -DOSNAME="\"foo\"" is provided during
558: * compilation, this value will be used instead of filling in "sysname
559: * release" from uname().
560: */
1.1 kristaps 561: static int
562: post_os(POST_ARGS)
563: {
1.25 schwarze 564: char buf[BUFSIZ];
565: #ifndef OSNAME
1.1 kristaps 566: struct utsname utsname;
1.24 schwarze 567: #endif
568:
1.1 kristaps 569: if (m->meta.os)
570: free(m->meta.os);
571:
1.25 schwarze 572: if ( ! concat(m, buf, n->child, BUFSIZ))
1.1 kristaps 573: return(0);
574:
1.25 schwarze 575: if ('\0' == buf[0]) {
576: #ifdef OSNAME
577: if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ)
1.24 schwarze 578: return(mdoc_nerr(m, n, EUTSNAME));
1.25 schwarze 579: #else /*!OSNAME */
1.1 kristaps 580: if (-1 == uname(&utsname))
1.20 schwarze 581: return(mdoc_nerr(m, n, EUTSNAME));
1.25 schwarze 582: if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ)
1.20 schwarze 583: return(mdoc_nerr(m, n, ETOOLONG));
1.25 schwarze 584: if (strlcat(buf, " ", 64) >= BUFSIZ)
1.20 schwarze 585: return(mdoc_nerr(m, n, ETOOLONG));
1.25 schwarze 586: if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ)
1.20 schwarze 587: return(mdoc_nerr(m, n, ETOOLONG));
1.25 schwarze 588: #endif /*!OSNAME*/
1.1 kristaps 589: }
590:
1.25 schwarze 591: m->meta.os = mandoc_strdup(buf);
1.20 schwarze 592: return(post_prol(m, n));
1.1 kristaps 593: }
594:
595:
596: /*
597: * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
1.25 schwarze 598: * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
599: * argument has NOT been provided. See post_bl_width() for converting
600: * the -width string.
1.1 kristaps 601: */
602: static int
1.20 schwarze 603: post_bl_tagwidth(POST_ARGS)
1.1 kristaps 604: {
1.25 schwarze 605: struct mdoc_node *nn;
606: size_t sz;
607: int i;
608: char buf[NUMSIZ];
1.1 kristaps 609:
1.25 schwarze 610: /* Defaults to ten ens. */
1.1 kristaps 611:
1.25 schwarze 612: sz = 10; /* XXX: make this a macro value. */
1.20 schwarze 613: nn = n->body->child;
1.25 schwarze 614:
1.20 schwarze 615: if (nn) {
616: assert(MDOC_BLOCK == nn->type);
617: assert(MDOC_It == nn->tok);
618: nn = nn->head->child;
619: if (MDOC_TEXT != nn->type) {
1.25 schwarze 620: sz = mdoc_macro2len(nn->tok);
621: if (sz == 0) {
1.20 schwarze 622: if ( ! mdoc_nwarn(m, n, ENOWIDTH))
1.1 kristaps 623: return(0);
1.25 schwarze 624: sz = 10;
625: }
1.1 kristaps 626: } else
1.25 schwarze 627: sz = strlen(nn->string) + 1;
1.1 kristaps 628: }
629:
1.25 schwarze 630: snprintf(buf, NUMSIZ, "%zun", sz);
1.1 kristaps 631:
632: /*
633: * We have to dynamically add this to the macro's argument list.
634: * We're guaranteed that a MDOC_Width doesn't already exist.
635: */
636:
1.20 schwarze 637: nn = n;
638: assert(nn->args);
1.25 schwarze 639: i = (int)(nn->args->argc)++;
1.1 kristaps 640:
1.25 schwarze 641: nn->args->argv = mandoc_realloc(nn->args->argv,
1.20 schwarze 642: nn->args->argc * sizeof(struct mdoc_argv));
1.1 kristaps 643:
1.25 schwarze 644: nn->args->argv[i].arg = MDOC_Width;
645: nn->args->argv[i].line = n->line;
646: nn->args->argv[i].pos = n->pos;
647: nn->args->argv[i].sz = 1;
648: nn->args->argv[i].value = mandoc_malloc(sizeof(char *));
649: nn->args->argv[i].value[0] = mandoc_strdup(buf);
1.1 kristaps 650: return(1);
651: }
652:
653:
1.25 schwarze 654: /*
655: * Calculate the real width of a list from the -width string, which may
656: * contain a macro (with a known default width), a literal string, or a
657: * scaling width.
658: */
1.1 kristaps 659: static int
1.20 schwarze 660: post_bl_width(POST_ARGS)
1.1 kristaps 661: {
662: size_t width;
1.28 schwarze 663: int i;
664: enum mdoct tok;
1.25 schwarze 665: char buf[NUMSIZ];
1.1 kristaps 666: char *p;
667:
1.20 schwarze 668: if (NULL == n->args)
1.1 kristaps 669: return(1);
670:
1.20 schwarze 671: for (i = 0; i < (int)n->args->argc; i++)
672: if (MDOC_Width == n->args->argv[i].arg)
1.1 kristaps 673: break;
674:
1.20 schwarze 675: if (i == (int)n->args->argc)
1.1 kristaps 676: return(1);
1.20 schwarze 677: p = n->args->argv[i].value[0];
1.1 kristaps 678:
679: /*
680: * If the value to -width is a macro, then we re-write it to be
681: * the macro's width as set in share/tmac/mdoc/doc-common.
682: */
683:
684: if (0 == strcmp(p, "Ds"))
1.25 schwarze 685: /* XXX: make into a macro. */
1.9 schwarze 686: width = 6;
1.21 schwarze 687: else if (MDOC_MAX == (tok = mdoc_hash_find(p)))
1.1 kristaps 688: return(1);
689: else if (0 == (width = mdoc_macro2len(tok)))
1.20 schwarze 690: return(mdoc_nwarn(m, n, ENOWIDTH));
1.1 kristaps 691:
692: /* The value already exists: free and reallocate it. */
693:
1.25 schwarze 694: snprintf(buf, NUMSIZ, "%zun", width);
1.20 schwarze 695: free(n->args->argv[i].value[0]);
1.25 schwarze 696: n->args->argv[i].value[0] = mandoc_strdup(buf);
1.1 kristaps 697: return(1);
698: }
699:
700:
1.25 schwarze 701: /*
702: * Do processing for -column lists, which can have two distinct styles
703: * of invocation. Merge this two styles into a consistent form.
704: */
1.20 schwarze 705: /* ARGSUSED */
1.1 kristaps 706: static int
1.6 schwarze 707: post_bl_head(POST_ARGS)
708: {
709: int i, c;
1.20 schwarze 710: struct mdoc_node *np, *nn, *nnp;
1.6 schwarze 711:
1.20 schwarze 712: if (NULL == n->child)
1.6 schwarze 713: return(1);
714:
1.20 schwarze 715: np = n->parent;
716: assert(np->args);
1.6 schwarze 717:
1.20 schwarze 718: for (c = 0; c < (int)np->args->argc; c++)
719: if (MDOC_Column == np->args->argv[c].arg)
1.6 schwarze 720: break;
721:
1.20 schwarze 722: if (c == (int)np->args->argc)
1.6 schwarze 723: return(1);
1.20 schwarze 724: assert(0 == np->args->argv[c].sz);
1.6 schwarze 725:
726: /*
727: * Accomodate for new-style groff column syntax. Shuffle the
728: * child nodes, all of which must be TEXT, as arguments for the
729: * column field. Then, delete the head children.
730: */
731:
1.20 schwarze 732: np->args->argv[c].sz = (size_t)n->nchild;
1.25 schwarze 733: np->args->argv[c].value = mandoc_malloc
1.20 schwarze 734: ((size_t)n->nchild * sizeof(char *));
1.6 schwarze 735:
1.20 schwarze 736: for (i = 0, nn = n->child; nn; i++) {
737: np->args->argv[c].value[i] = nn->string;
1.6 schwarze 738: nn->string = NULL;
739: nnp = nn;
740: nn = nn->next;
1.30 schwarze 741: mdoc_node_delete(NULL, nnp);
1.6 schwarze 742: }
743:
1.20 schwarze 744: n->nchild = 0;
745: n->child = NULL;
1.6 schwarze 746: return(1);
747: }
748:
749:
750: static int
1.1 kristaps 751: post_bl(POST_ARGS)
752: {
753: int i, r, len;
754:
1.20 schwarze 755: if (MDOC_HEAD == n->type)
756: return(post_bl_head(m, n));
757: if (MDOC_BLOCK != n->type)
1.1 kristaps 758: return(1);
759:
760: /*
761: * These are fairly complicated, so we've broken them into two
762: * functions. post_bl_tagwidth() is called when a -tag is
763: * specified, but no -width (it must be guessed). The second
764: * when a -width is specified (macro indicators must be
765: * rewritten into real lengths).
766: */
767:
1.20 schwarze 768: len = (int)(n->args ? n->args->argc : 0);
1.1 kristaps 769:
770: for (r = i = 0; i < len; i++) {
1.20 schwarze 771: if (MDOC_Tag == n->args->argv[i].arg)
1.1 kristaps 772: r |= 1 << 0;
1.20 schwarze 773: if (MDOC_Width == n->args->argv[i].arg)
1.1 kristaps 774: r |= 1 << 1;
775: }
776:
777: if (r & (1 << 0) && ! (r & (1 << 1))) {
1.20 schwarze 778: if ( ! post_bl_tagwidth(m, n))
1.1 kristaps 779: return(0);
780: } else if (r & (1 << 1))
1.20 schwarze 781: if ( ! post_bl_width(m, n))
1.1 kristaps 782: return(0);
783:
1.3 schwarze 784: return(1);
785: }
786:
787:
1.25 schwarze 788: /*
789: * The `Pa' macro defaults to a tilde if no value is provided as an
790: * argument.
791: */
1.3 schwarze 792: static int
1.25 schwarze 793: post_pa(POST_ARGS)
1.3 schwarze 794: {
1.20 schwarze 795: struct mdoc_node *np;
1.3 schwarze 796:
1.20 schwarze 797: if (n->child)
1.3 schwarze 798: return(1);
799:
1.20 schwarze 800: np = n;
1.3 schwarze 801: m->next = MDOC_NEXT_CHILD;
1.25 schwarze 802: /* XXX: make into macro value. */
1.20 schwarze 803: if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
1.3 schwarze 804: return(0);
1.20 schwarze 805: m->last = np;
1.1 kristaps 806: return(1);
807: }
808:
809:
1.25 schwarze 810: /*
811: * The `Ar' macro defaults to two strings "file ..." if no value is
812: * provided as an argument.
813: */
1.1 kristaps 814: static int
815: post_ar(POST_ARGS)
816: {
1.20 schwarze 817: struct mdoc_node *np;
1.1 kristaps 818:
1.20 schwarze 819: if (n->child)
1.1 kristaps 820: return(1);
821:
1.20 schwarze 822: np = n;
1.1 kristaps 823: m->next = MDOC_NEXT_CHILD;
1.25 schwarze 824: /* XXX: make into macro values. */
1.20 schwarze 825: if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
1.1 kristaps 826: return(0);
1.20 schwarze 827: if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
1.1 kristaps 828: return(0);
1.20 schwarze 829: m->last = np;
1.1 kristaps 830: return(1);
831: }
832:
833:
1.25 schwarze 834: /*
1.26 schwarze 835: * Parse the date field in `Dd'.
1.25 schwarze 836: */
1.1 kristaps 837: static int
838: post_dd(POST_ARGS)
839: {
1.25 schwarze 840: char buf[DATESIZ];
1.1 kristaps 841:
1.25 schwarze 842: if ( ! concat(m, buf, n->child, DATESIZ))
1.1 kristaps 843: return(0);
844:
1.26 schwarze 845: m->meta.date = mandoc_a2time
846: (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
847:
1.25 schwarze 848: if (0 == m->meta.date) {
1.20 schwarze 849: if ( ! mdoc_nwarn(m, n, EBADDATE))
1.1 kristaps 850: return(0);
851: m->meta.date = time(NULL);
852: }
853:
1.20 schwarze 854: return(post_prol(m, n));
1.1 kristaps 855: }
856:
857:
1.25 schwarze 858: /*
859: * Remove prologue macros from the document after they're processed.
860: * The final document uses mdoc_meta for these values and discards the
861: * originals.
862: */
1.1 kristaps 863: static int
864: post_prol(POST_ARGS)
865: {
1.18 schwarze 866:
1.30 schwarze 867: mdoc_node_delete(m, n);
1.18 schwarze 868: if (m->meta.title && m->meta.date && m->meta.os)
869: m->flags |= MDOC_PBODY;
1.1 kristaps 870: return(1);
871: }
872:
873:
1.25 schwarze 874: /*
875: * Trigger a literal context.
876: */
1.1 kristaps 877: static int
878: pre_dl(PRE_ARGS)
879: {
880:
1.7 schwarze 881: if (MDOC_BODY == n->type)
882: m->flags |= MDOC_LITERAL;
1.1 kristaps 883: return(1);
884: }
885:
886:
1.25 schwarze 887: /* ARGSUSED */
1.1 kristaps 888: static int
1.22 schwarze 889: pre_offset(PRE_ARGS)
890: {
891: int i;
892:
893: /*
894: * Make sure that an empty offset produces an 8n length space as
895: * stipulated by mdoc.samples.
896: */
897:
898: assert(n->args);
899: for (i = 0; i < (int)n->args->argc; i++) {
900: if (MDOC_Offset != n->args->argv[i].arg)
901: continue;
902: if (n->args->argv[i].sz)
903: break;
904: assert(1 == n->args->refcnt);
905: /* If no value set, length of <string>. */
906: n->args->argv[i].sz++;
1.25 schwarze 907: n->args->argv[i].value = mandoc_malloc(sizeof(char *));
908: n->args->argv[i].value[0] = mandoc_strdup("8n");
1.22 schwarze 909: break;
910: }
911:
912: return(1);
913: }
914:
915:
916: static int
917: pre_bl(PRE_ARGS)
918: {
919:
920: return(MDOC_BLOCK == n->type ? pre_offset(m, n) : 1);
921: }
922:
923:
924: static int
1.1 kristaps 925: pre_bd(PRE_ARGS)
926: {
927: int i;
928:
1.22 schwarze 929: if (MDOC_BLOCK == n->type)
930: return(pre_offset(m, n));
1.1 kristaps 931: if (MDOC_BODY != n->type)
932: return(1);
933:
1.19 schwarze 934: /* Enter literal context if `Bd -literal' or `-unfilled'. */
935:
1.1 kristaps 936: for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
937: if (MDOC_Literal == n->args->argv[i].arg)
1.22 schwarze 938: m->flags |= MDOC_LITERAL;
1.1 kristaps 939: else if (MDOC_Unfilled == n->args->argv[i].arg)
1.22 schwarze 940: m->flags |= MDOC_LITERAL;
1.1 kristaps 941:
942: return(1);
943: }
944:
945:
946: static int
947: post_display(POST_ARGS)
948: {
949:
1.20 schwarze 950: if (MDOC_BODY == n->type)
1.1 kristaps 951: m->flags &= ~MDOC_LITERAL;
952: return(1);
953: }
954:
955:
1.23 schwarze 956: static inline int
1.31 schwarze 957: order_rs(enum mdoct t)
1.23 schwarze 958: {
959: int i;
960:
1.31 schwarze 961: for (i = 0; i < (int)RSORD_MAX; i++)
1.23 schwarze 962: if (rsord[i] == t)
963: return(i);
964:
965: abort();
966: /* NOTREACHED */
967: }
968:
969:
970: /* ARGSUSED */
971: static int
972: post_rs(POST_ARGS)
973: {
974: struct mdoc_node *nn, *next, *prev;
975: int o;
976:
977: if (MDOC_BLOCK != n->type)
978: return(1);
979:
980: assert(n->body->child);
981: for (next = NULL, nn = n->body->child->next; nn; nn = next) {
982: o = order_rs(nn->tok);
983:
984: /* Remove `nn' from the chain. */
985: next = nn->next;
986: if (next)
987: next->prev = nn->prev;
988:
989: prev = nn->prev;
990: if (prev)
991: prev->next = nn->next;
992:
993: nn->prev = nn->next = NULL;
994:
995: /*
996: * Scan back until we reach a node that's ordered before
997: * us, then set ourselves as being the next.
998: */
999: for ( ; prev; prev = prev->prev)
1000: if (order_rs(prev->tok) <= o)
1001: break;
1002:
1003: nn->prev = prev;
1004: if (prev) {
1005: if (prev->next)
1006: prev->next->prev = nn;
1007: nn->next = prev->next;
1008: prev->next = nn;
1009: continue;
1010: }
1011:
1012: n->body->child->prev = nn;
1013: nn->next = n->body->child;
1014: n->body->child = nn;
1015: }
1016: return(1);
1017: }