Annotation of src/usr.bin/mandoc/mdoc_action.c, Revision 1.35
1.35 ! schwarze 1: /* $Id: mdoc_action.c,v 1.34 2010/05/15 16:48:12 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):
1.35 ! schwarze 445: assert(m->meta.msec);
! 446: if (*m->meta.msec == '2')
1.1 kristaps 447: break;
1.35 ! schwarze 448: if (*m->meta.msec == '3')
! 449: break;
! 450: if (*m->meta.msec == '9')
! 451: break;
! 452: return(mdoc_nwarn(m, n, EWRONGMSEC));
1.1 kristaps 453: default:
454: break;
455: }
456: return(1);
457: }
458:
459:
1.25 schwarze 460: /*
461: * Parse out the contents of `Dt'. See in-line documentation for how we
462: * handle the various fields of this macro.
463: */
1.1 kristaps 464: static int
465: post_dt(POST_ARGS)
466: {
1.20 schwarze 467: struct mdoc_node *nn;
1.1 kristaps 468: const char *cp;
469:
470: if (m->meta.title)
471: free(m->meta.title);
472: if (m->meta.vol)
473: free(m->meta.vol);
474: if (m->meta.arch)
475: free(m->meta.arch);
476:
477: m->meta.title = m->meta.vol = m->meta.arch = NULL;
478: /* Handles: `.Dt'
479: * --> title = unknown, volume = local, msec = 0, arch = NULL
480: */
481:
1.20 schwarze 482: if (NULL == (nn = n->child)) {
1.25 schwarze 483: /* XXX: make these macro values. */
1.35 ! schwarze 484: /* FIXME: warn about missing values. */
1.25 schwarze 485: m->meta.title = mandoc_strdup("unknown");
486: m->meta.vol = mandoc_strdup("local");
1.35 ! schwarze 487: m->meta.msec = mandoc_strdup("1");
1.20 schwarze 488: return(post_prol(m, n));
1.1 kristaps 489: }
490:
491: /* Handles: `.Dt TITLE'
492: * --> title = TITLE, volume = local, msec = 0, arch = NULL
493: */
494:
1.25 schwarze 495: m->meta.title = mandoc_strdup(nn->string);
1.1 kristaps 496:
1.20 schwarze 497: if (NULL == (nn = nn->next)) {
1.35 ! schwarze 498: /* FIXME: warn about missing msec. */
1.25 schwarze 499: /* XXX: make this a macro value. */
500: m->meta.vol = mandoc_strdup("local");
1.35 ! schwarze 501: m->meta.msec = mandoc_strdup("1");
1.20 schwarze 502: return(post_prol(m, n));
1.1 kristaps 503: }
504:
505: /* Handles: `.Dt TITLE SEC'
506: * --> title = TITLE, volume = SEC is msec ?
507: * format(msec) : SEC,
508: * msec = SEC is msec ? atoi(msec) : 0,
509: * arch = NULL
510: */
511:
1.20 schwarze 512: cp = mdoc_a2msec(nn->string);
1.1 kristaps 513: if (cp) {
1.25 schwarze 514: m->meta.vol = mandoc_strdup(cp);
1.35 ! schwarze 515: m->meta.msec = mandoc_strdup(nn->string);
! 516: } else if (mdoc_nwarn(m, n, EBADMSEC)) {
1.25 schwarze 517: m->meta.vol = mandoc_strdup(nn->string);
1.35 ! schwarze 518: m->meta.msec = mandoc_strdup(nn->string);
! 519: } else
! 520: return(0);
1.1 kristaps 521:
1.20 schwarze 522: if (NULL == (nn = nn->next))
523: return(post_prol(m, n));
1.1 kristaps 524:
525: /* Handles: `.Dt TITLE SEC VOL'
526: * --> title = TITLE, volume = VOL is vol ?
527: * format(VOL) :
528: * VOL is arch ? format(arch) :
529: * VOL
530: */
531:
1.20 schwarze 532: cp = mdoc_a2vol(nn->string);
1.1 kristaps 533: if (cp) {
534: free(m->meta.vol);
1.25 schwarze 535: m->meta.vol = mandoc_strdup(cp);
1.1 kristaps 536: } else {
1.35 ! schwarze 537: /* FIXME: warn about bad arch. */
1.20 schwarze 538: cp = mdoc_a2arch(nn->string);
1.1 kristaps 539: if (NULL == cp) {
540: free(m->meta.vol);
1.25 schwarze 541: m->meta.vol = mandoc_strdup(nn->string);
542: } else
543: m->meta.arch = mandoc_strdup(cp);
1.1 kristaps 544: }
545:
546: /* Ignore any subsequent parameters... */
1.25 schwarze 547: /* FIXME: warn about subsequent parameters. */
1.1 kristaps 548:
1.20 schwarze 549: return(post_prol(m, n));
1.1 kristaps 550: }
551:
552:
1.25 schwarze 553: /*
554: * Set the operating system by way of the `Os' macro. Note that if an
555: * argument isn't provided and -DOSNAME="\"foo\"" is provided during
556: * compilation, this value will be used instead of filling in "sysname
557: * release" from uname().
558: */
1.1 kristaps 559: static int
560: post_os(POST_ARGS)
561: {
1.25 schwarze 562: char buf[BUFSIZ];
563: #ifndef OSNAME
1.1 kristaps 564: struct utsname utsname;
1.24 schwarze 565: #endif
566:
1.1 kristaps 567: if (m->meta.os)
568: free(m->meta.os);
569:
1.25 schwarze 570: if ( ! concat(m, buf, n->child, BUFSIZ))
1.1 kristaps 571: return(0);
572:
1.25 schwarze 573: if ('\0' == buf[0]) {
574: #ifdef OSNAME
575: if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ)
1.24 schwarze 576: return(mdoc_nerr(m, n, EUTSNAME));
1.25 schwarze 577: #else /*!OSNAME */
1.1 kristaps 578: if (-1 == uname(&utsname))
1.20 schwarze 579: return(mdoc_nerr(m, n, EUTSNAME));
1.25 schwarze 580: if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ)
1.20 schwarze 581: return(mdoc_nerr(m, n, ETOOLONG));
1.25 schwarze 582: if (strlcat(buf, " ", 64) >= BUFSIZ)
1.20 schwarze 583: return(mdoc_nerr(m, n, ETOOLONG));
1.25 schwarze 584: if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ)
1.20 schwarze 585: return(mdoc_nerr(m, n, ETOOLONG));
1.25 schwarze 586: #endif /*!OSNAME*/
1.1 kristaps 587: }
588:
1.25 schwarze 589: m->meta.os = mandoc_strdup(buf);
1.20 schwarze 590: return(post_prol(m, n));
1.1 kristaps 591: }
592:
593:
594: /*
595: * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
1.25 schwarze 596: * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
597: * argument has NOT been provided. See post_bl_width() for converting
598: * the -width string.
1.1 kristaps 599: */
600: static int
1.20 schwarze 601: post_bl_tagwidth(POST_ARGS)
1.1 kristaps 602: {
1.25 schwarze 603: struct mdoc_node *nn;
604: size_t sz;
605: int i;
606: char buf[NUMSIZ];
1.1 kristaps 607:
1.25 schwarze 608: /* Defaults to ten ens. */
1.1 kristaps 609:
1.25 schwarze 610: sz = 10; /* XXX: make this a macro value. */
1.20 schwarze 611: nn = n->body->child;
1.25 schwarze 612:
1.20 schwarze 613: if (nn) {
614: assert(MDOC_BLOCK == nn->type);
615: assert(MDOC_It == nn->tok);
616: nn = nn->head->child;
617: if (MDOC_TEXT != nn->type) {
1.25 schwarze 618: sz = mdoc_macro2len(nn->tok);
619: if (sz == 0) {
1.20 schwarze 620: if ( ! mdoc_nwarn(m, n, ENOWIDTH))
1.1 kristaps 621: return(0);
1.25 schwarze 622: sz = 10;
623: }
1.1 kristaps 624: } else
1.25 schwarze 625: sz = strlen(nn->string) + 1;
1.1 kristaps 626: }
627:
1.25 schwarze 628: snprintf(buf, NUMSIZ, "%zun", sz);
1.1 kristaps 629:
630: /*
631: * We have to dynamically add this to the macro's argument list.
632: * We're guaranteed that a MDOC_Width doesn't already exist.
633: */
634:
1.20 schwarze 635: nn = n;
636: assert(nn->args);
1.25 schwarze 637: i = (int)(nn->args->argc)++;
1.1 kristaps 638:
1.25 schwarze 639: nn->args->argv = mandoc_realloc(nn->args->argv,
1.20 schwarze 640: nn->args->argc * sizeof(struct mdoc_argv));
1.1 kristaps 641:
1.25 schwarze 642: nn->args->argv[i].arg = MDOC_Width;
643: nn->args->argv[i].line = n->line;
644: nn->args->argv[i].pos = n->pos;
645: nn->args->argv[i].sz = 1;
646: nn->args->argv[i].value = mandoc_malloc(sizeof(char *));
647: nn->args->argv[i].value[0] = mandoc_strdup(buf);
1.1 kristaps 648: return(1);
649: }
650:
651:
1.25 schwarze 652: /*
653: * Calculate the real width of a list from the -width string, which may
654: * contain a macro (with a known default width), a literal string, or a
655: * scaling width.
656: */
1.1 kristaps 657: static int
1.20 schwarze 658: post_bl_width(POST_ARGS)
1.1 kristaps 659: {
660: size_t width;
1.28 schwarze 661: int i;
662: enum mdoct tok;
1.25 schwarze 663: char buf[NUMSIZ];
1.1 kristaps 664: char *p;
665:
1.20 schwarze 666: if (NULL == n->args)
1.1 kristaps 667: return(1);
668:
1.20 schwarze 669: for (i = 0; i < (int)n->args->argc; i++)
670: if (MDOC_Width == n->args->argv[i].arg)
1.1 kristaps 671: break;
672:
1.20 schwarze 673: if (i == (int)n->args->argc)
1.1 kristaps 674: return(1);
1.20 schwarze 675: p = n->args->argv[i].value[0];
1.1 kristaps 676:
677: /*
678: * If the value to -width is a macro, then we re-write it to be
679: * the macro's width as set in share/tmac/mdoc/doc-common.
680: */
681:
682: if (0 == strcmp(p, "Ds"))
1.25 schwarze 683: /* XXX: make into a macro. */
1.9 schwarze 684: width = 6;
1.21 schwarze 685: else if (MDOC_MAX == (tok = mdoc_hash_find(p)))
1.1 kristaps 686: return(1);
687: else if (0 == (width = mdoc_macro2len(tok)))
1.20 schwarze 688: return(mdoc_nwarn(m, n, ENOWIDTH));
1.1 kristaps 689:
690: /* The value already exists: free and reallocate it. */
691:
1.25 schwarze 692: snprintf(buf, NUMSIZ, "%zun", width);
1.20 schwarze 693: free(n->args->argv[i].value[0]);
1.25 schwarze 694: n->args->argv[i].value[0] = mandoc_strdup(buf);
1.1 kristaps 695: return(1);
696: }
697:
698:
1.25 schwarze 699: /*
700: * Do processing for -column lists, which can have two distinct styles
701: * of invocation. Merge this two styles into a consistent form.
702: */
1.20 schwarze 703: /* ARGSUSED */
1.1 kristaps 704: static int
1.6 schwarze 705: post_bl_head(POST_ARGS)
706: {
707: int i, c;
1.20 schwarze 708: struct mdoc_node *np, *nn, *nnp;
1.6 schwarze 709:
1.20 schwarze 710: if (NULL == n->child)
1.6 schwarze 711: return(1);
712:
1.20 schwarze 713: np = n->parent;
714: assert(np->args);
1.6 schwarze 715:
1.20 schwarze 716: for (c = 0; c < (int)np->args->argc; c++)
717: if (MDOC_Column == np->args->argv[c].arg)
1.6 schwarze 718: break;
719:
1.20 schwarze 720: if (c == (int)np->args->argc)
1.6 schwarze 721: return(1);
1.20 schwarze 722: assert(0 == np->args->argv[c].sz);
1.6 schwarze 723:
724: /*
725: * Accomodate for new-style groff column syntax. Shuffle the
726: * child nodes, all of which must be TEXT, as arguments for the
727: * column field. Then, delete the head children.
728: */
729:
1.20 schwarze 730: np->args->argv[c].sz = (size_t)n->nchild;
1.25 schwarze 731: np->args->argv[c].value = mandoc_malloc
1.20 schwarze 732: ((size_t)n->nchild * sizeof(char *));
1.6 schwarze 733:
1.20 schwarze 734: for (i = 0, nn = n->child; nn; i++) {
735: np->args->argv[c].value[i] = nn->string;
1.6 schwarze 736: nn->string = NULL;
737: nnp = nn;
738: nn = nn->next;
1.30 schwarze 739: mdoc_node_delete(NULL, nnp);
1.6 schwarze 740: }
741:
1.20 schwarze 742: n->nchild = 0;
743: n->child = NULL;
1.6 schwarze 744: return(1);
745: }
746:
747:
748: static int
1.1 kristaps 749: post_bl(POST_ARGS)
750: {
751: int i, r, len;
752:
1.20 schwarze 753: if (MDOC_HEAD == n->type)
754: return(post_bl_head(m, n));
755: if (MDOC_BLOCK != n->type)
1.1 kristaps 756: return(1);
757:
758: /*
759: * These are fairly complicated, so we've broken them into two
760: * functions. post_bl_tagwidth() is called when a -tag is
761: * specified, but no -width (it must be guessed). The second
762: * when a -width is specified (macro indicators must be
763: * rewritten into real lengths).
764: */
765:
1.20 schwarze 766: len = (int)(n->args ? n->args->argc : 0);
1.1 kristaps 767:
768: for (r = i = 0; i < len; i++) {
1.20 schwarze 769: if (MDOC_Tag == n->args->argv[i].arg)
1.1 kristaps 770: r |= 1 << 0;
1.20 schwarze 771: if (MDOC_Width == n->args->argv[i].arg)
1.1 kristaps 772: r |= 1 << 1;
773: }
774:
775: if (r & (1 << 0) && ! (r & (1 << 1))) {
1.20 schwarze 776: if ( ! post_bl_tagwidth(m, n))
1.1 kristaps 777: return(0);
778: } else if (r & (1 << 1))
1.20 schwarze 779: if ( ! post_bl_width(m, n))
1.1 kristaps 780: return(0);
781:
1.3 schwarze 782: return(1);
783: }
784:
785:
1.25 schwarze 786: /*
787: * The `Pa' macro defaults to a tilde if no value is provided as an
788: * argument.
789: */
1.3 schwarze 790: static int
1.25 schwarze 791: post_pa(POST_ARGS)
1.3 schwarze 792: {
1.20 schwarze 793: struct mdoc_node *np;
1.3 schwarze 794:
1.20 schwarze 795: if (n->child)
1.3 schwarze 796: return(1);
797:
1.20 schwarze 798: np = n;
1.3 schwarze 799: m->next = MDOC_NEXT_CHILD;
1.25 schwarze 800: /* XXX: make into macro value. */
1.20 schwarze 801: if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
1.3 schwarze 802: return(0);
1.20 schwarze 803: m->last = np;
1.1 kristaps 804: return(1);
805: }
806:
807:
1.25 schwarze 808: /*
809: * The `Ar' macro defaults to two strings "file ..." if no value is
810: * provided as an argument.
811: */
1.1 kristaps 812: static int
813: post_ar(POST_ARGS)
814: {
1.20 schwarze 815: struct mdoc_node *np;
1.1 kristaps 816:
1.20 schwarze 817: if (n->child)
1.1 kristaps 818: return(1);
819:
1.20 schwarze 820: np = n;
1.1 kristaps 821: m->next = MDOC_NEXT_CHILD;
1.25 schwarze 822: /* XXX: make into macro values. */
1.20 schwarze 823: if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
1.1 kristaps 824: return(0);
1.20 schwarze 825: if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
1.1 kristaps 826: return(0);
1.20 schwarze 827: m->last = np;
1.1 kristaps 828: return(1);
829: }
830:
831:
1.25 schwarze 832: /*
1.26 schwarze 833: * Parse the date field in `Dd'.
1.25 schwarze 834: */
1.1 kristaps 835: static int
836: post_dd(POST_ARGS)
837: {
1.25 schwarze 838: char buf[DATESIZ];
1.1 kristaps 839:
1.25 schwarze 840: if ( ! concat(m, buf, n->child, DATESIZ))
1.1 kristaps 841: return(0);
842:
1.26 schwarze 843: m->meta.date = mandoc_a2time
844: (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
845:
1.25 schwarze 846: if (0 == m->meta.date) {
1.20 schwarze 847: if ( ! mdoc_nwarn(m, n, EBADDATE))
1.1 kristaps 848: return(0);
849: m->meta.date = time(NULL);
850: }
851:
1.20 schwarze 852: return(post_prol(m, n));
1.1 kristaps 853: }
854:
855:
1.25 schwarze 856: /*
857: * Remove prologue macros from the document after they're processed.
858: * The final document uses mdoc_meta for these values and discards the
859: * originals.
860: */
1.1 kristaps 861: static int
862: post_prol(POST_ARGS)
863: {
1.18 schwarze 864:
1.30 schwarze 865: mdoc_node_delete(m, n);
1.18 schwarze 866: if (m->meta.title && m->meta.date && m->meta.os)
867: m->flags |= MDOC_PBODY;
1.1 kristaps 868: return(1);
869: }
870:
871:
1.25 schwarze 872: /*
873: * Trigger a literal context.
874: */
1.1 kristaps 875: static int
876: pre_dl(PRE_ARGS)
877: {
878:
1.7 schwarze 879: if (MDOC_BODY == n->type)
880: m->flags |= MDOC_LITERAL;
1.1 kristaps 881: return(1);
882: }
883:
884:
1.25 schwarze 885: /* ARGSUSED */
1.1 kristaps 886: static int
1.22 schwarze 887: pre_offset(PRE_ARGS)
888: {
889: int i;
890:
891: /*
892: * Make sure that an empty offset produces an 8n length space as
893: * stipulated by mdoc.samples.
894: */
895:
896: assert(n->args);
897: for (i = 0; i < (int)n->args->argc; i++) {
898: if (MDOC_Offset != n->args->argv[i].arg)
899: continue;
900: if (n->args->argv[i].sz)
901: break;
902: assert(1 == n->args->refcnt);
903: /* If no value set, length of <string>. */
904: n->args->argv[i].sz++;
1.25 schwarze 905: n->args->argv[i].value = mandoc_malloc(sizeof(char *));
906: n->args->argv[i].value[0] = mandoc_strdup("8n");
1.22 schwarze 907: break;
908: }
909:
910: return(1);
911: }
912:
913:
914: static int
915: pre_bl(PRE_ARGS)
916: {
917:
918: return(MDOC_BLOCK == n->type ? pre_offset(m, n) : 1);
919: }
920:
921:
922: static int
1.1 kristaps 923: pre_bd(PRE_ARGS)
924: {
925: int i;
926:
1.22 schwarze 927: if (MDOC_BLOCK == n->type)
928: return(pre_offset(m, n));
1.1 kristaps 929: if (MDOC_BODY != n->type)
930: return(1);
931:
1.19 schwarze 932: /* Enter literal context if `Bd -literal' or `-unfilled'. */
933:
1.1 kristaps 934: for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
935: if (MDOC_Literal == n->args->argv[i].arg)
1.22 schwarze 936: m->flags |= MDOC_LITERAL;
1.1 kristaps 937: else if (MDOC_Unfilled == n->args->argv[i].arg)
1.22 schwarze 938: m->flags |= MDOC_LITERAL;
1.1 kristaps 939:
940: return(1);
941: }
942:
943:
944: static int
945: post_display(POST_ARGS)
946: {
947:
1.20 schwarze 948: if (MDOC_BODY == n->type)
1.1 kristaps 949: m->flags &= ~MDOC_LITERAL;
950: return(1);
951: }
952:
953:
1.23 schwarze 954: static inline int
1.31 schwarze 955: order_rs(enum mdoct t)
1.23 schwarze 956: {
957: int i;
958:
1.31 schwarze 959: for (i = 0; i < (int)RSORD_MAX; i++)
1.23 schwarze 960: if (rsord[i] == t)
961: return(i);
962:
963: abort();
964: /* NOTREACHED */
965: }
966:
967:
968: /* ARGSUSED */
969: static int
970: post_rs(POST_ARGS)
971: {
972: struct mdoc_node *nn, *next, *prev;
973: int o;
974:
975: if (MDOC_BLOCK != n->type)
976: return(1);
977:
978: assert(n->body->child);
979: for (next = NULL, nn = n->body->child->next; nn; nn = next) {
980: o = order_rs(nn->tok);
981:
982: /* Remove `nn' from the chain. */
983: next = nn->next;
984: if (next)
985: next->prev = nn->prev;
986:
987: prev = nn->prev;
988: if (prev)
989: prev->next = nn->next;
990:
991: nn->prev = nn->next = NULL;
992:
993: /*
994: * Scan back until we reach a node that's ordered before
995: * us, then set ourselves as being the next.
996: */
997: for ( ; prev; prev = prev->prev)
998: if (order_rs(prev->tok) <= o)
999: break;
1000:
1001: nn->prev = prev;
1002: if (prev) {
1003: if (prev->next)
1004: prev->next->prev = nn;
1005: nn->next = prev->next;
1006: prev->next = nn;
1007: continue;
1008: }
1009:
1010: n->body->child->prev = nn;
1011: nn->next = n->body->child;
1012: n->body->child = nn;
1013: }
1014: return(1);
1015: }