Annotation of src/usr.bin/mandoc/mdoc_action.c, Revision 1.42
1.42 ! schwarze 1: /* $Id: mdoc_action.c,v 1.41 2010/06/26 17:56:43 schwarze Exp $ */
1.1 kristaps 2: /*
1.41 schwarze 3: * Copyright (c) 2008, 2009 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.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:
1.36 schwarze 27: #include "mandoc.h"
1.1 kristaps 28: #include "libmdoc.h"
1.25 schwarze 29: #include "libmandoc.h"
1.1 kristaps 30:
1.20 schwarze 31: #define POST_ARGS struct mdoc *m, struct mdoc_node *n
1.37 schwarze 32: #define PRE_ARGS struct mdoc *m, struct mdoc_node *n
1.1 kristaps 33:
1.25 schwarze 34: #define NUMSIZ 32
35: #define DATESIZ 32
36:
1.1 kristaps 37: struct actions {
38: int (*pre)(PRE_ARGS);
39: int (*post)(POST_ARGS);
40: };
41:
1.25 schwarze 42: static int concat(struct mdoc *, char *,
43: const struct mdoc_node *, size_t);
1.31 schwarze 44: static inline int order_rs(enum mdoct);
1.23 schwarze 45:
1.1 kristaps 46: static int post_ar(POST_ARGS);
1.15 schwarze 47: static int post_at(POST_ARGS);
1.1 kristaps 48: static int post_bl(POST_ARGS);
1.6 schwarze 49: static int post_bl_head(POST_ARGS);
1.15 schwarze 50: static int post_bl_tagwidth(POST_ARGS);
1.1 kristaps 51: static int post_bl_width(POST_ARGS);
52: static int post_dd(POST_ARGS);
53: static int post_display(POST_ARGS);
54: static int post_dt(POST_ARGS);
1.15 schwarze 55: static int post_lb(POST_ARGS);
1.39 schwarze 56: static int post_li(POST_ARGS);
1.1 kristaps 57: static int post_nm(POST_ARGS);
58: static int post_os(POST_ARGS);
1.25 schwarze 59: static int post_pa(POST_ARGS);
1.1 kristaps 60: static int post_prol(POST_ARGS);
1.23 schwarze 61: static int post_rs(POST_ARGS);
1.1 kristaps 62: static int post_sh(POST_ARGS);
1.15 schwarze 63: static int post_st(POST_ARGS);
1.1 kristaps 64: static int post_std(POST_ARGS);
65:
66: static int pre_bd(PRE_ARGS);
67: static int pre_dl(PRE_ARGS);
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.41 schwarze 81: { NULL, 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 */
1.39 schwarze 100: { NULL, post_li }, /* Li */
1.1 kristaps 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.40 schwarze 191: { NULL, NULL }, /* Ta */
1.1 kristaps 192: };
193:
1.24 schwarze 194: #define RSORD_MAX 14
1.23 schwarze 195:
1.31 schwarze 196: static const enum mdoct rsord[RSORD_MAX] = {
1.23 schwarze 197: MDOC__A,
198: MDOC__T,
199: MDOC__B,
200: MDOC__I,
201: MDOC__J,
202: MDOC__R,
203: MDOC__N,
204: MDOC__V,
205: MDOC__P,
206: MDOC__Q,
207: MDOC__D,
208: MDOC__O,
1.24 schwarze 209: MDOC__C,
210: MDOC__U
1.23 schwarze 211: };
1.15 schwarze 212:
1.1 kristaps 213:
214: int
1.37 schwarze 215: mdoc_action_pre(struct mdoc *m, struct mdoc_node *n)
1.1 kristaps 216: {
217:
218: switch (n->type) {
219: case (MDOC_ROOT):
220: /* FALLTHROUGH */
221: case (MDOC_TEXT):
222: return(1);
223: default:
224: break;
225: }
226:
227: if (NULL == mdoc_actions[n->tok].pre)
228: return(1);
229: return((*mdoc_actions[n->tok].pre)(m, n));
230: }
231:
232:
233: int
234: mdoc_action_post(struct mdoc *m)
235: {
236:
237: if (MDOC_ACTED & m->last->flags)
238: return(1);
239: m->last->flags |= MDOC_ACTED;
240:
241: switch (m->last->type) {
242: case (MDOC_TEXT):
243: /* FALLTHROUGH */
244: case (MDOC_ROOT):
245: return(1);
246: default:
247: break;
248: }
249:
250: if (NULL == mdoc_actions[m->last->tok].post)
251: return(1);
1.20 schwarze 252: return((*mdoc_actions[m->last->tok].post)(m, m->last));
1.1 kristaps 253: }
254:
255:
1.25 schwarze 256: /*
257: * Concatenate sibling nodes together. All siblings must be of type
258: * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
259: * single whitespace.
260: */
1.1 kristaps 261: static int
1.25 schwarze 262: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
1.1 kristaps 263: {
264:
1.25 schwarze 265: assert(sz);
266: p[0] = '\0';
1.1 kristaps 267: for ( ; n; n = n->next) {
268: assert(MDOC_TEXT == n->type);
1.36 schwarze 269: /*
270: * XXX: yes, these can technically be resized, but it's
271: * highly unlikely that we're going to get here, so let
272: * it slip for now.
273: */
274: if (strlcat(p, n->string, sz) >= sz) {
275: mdoc_nmsg(m, n, MANDOCERR_MEM);
276: return(0);
277: }
1.1 kristaps 278: if (NULL == n->next)
279: continue;
1.36 schwarze 280: if (strlcat(p, " ", sz) >= sz) {
281: mdoc_nmsg(m, n, MANDOCERR_MEM);
282: return(0);
283: }
1.1 kristaps 284: }
285:
286: return(1);
287: }
288:
289:
1.25 schwarze 290: /*
291: * Macros accepting `-std' as an argument have the name of the current
292: * document (`Nm') filled in as the argument if it's not provided.
293: */
1.1 kristaps 294: static int
295: post_std(POST_ARGS)
296: {
1.36 schwarze 297: struct mdoc_node *nn;
1.1 kristaps 298:
1.20 schwarze 299: if (n->child)
1.1 kristaps 300: return(1);
1.36 schwarze 301: if (NULL == m->meta.name)
302: return(1);
1.20 schwarze 303:
304: nn = n;
305: m->next = MDOC_NEXT_CHILD;
1.36 schwarze 306:
1.20 schwarze 307: if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
308: return(0);
309: m->last = nn;
1.1 kristaps 310: return(1);
311: }
312:
313:
1.25 schwarze 314: /*
315: * The `Nm' macro's first use sets the name of the document. See also
316: * post_std(), etc.
317: */
1.1 kristaps 318: static int
319: post_nm(POST_ARGS)
320: {
1.25 schwarze 321: char buf[BUFSIZ];
1.1 kristaps 322:
323: if (m->meta.name)
324: return(1);
1.25 schwarze 325: if ( ! concat(m, buf, n->child, BUFSIZ))
1.1 kristaps 326: return(0);
1.25 schwarze 327: m->meta.name = mandoc_strdup(buf);
1.1 kristaps 328: return(1);
329: }
330:
331:
1.25 schwarze 332: /*
333: * Look up the value of `Lb' for matching predefined strings. If it has
334: * one, then substitute the current value for the formatted value. Note
335: * that the lookup may fail (we can provide arbitrary strings).
336: */
337: /* ARGSUSED */
1.1 kristaps 338: static int
1.15 schwarze 339: post_lb(POST_ARGS)
340: {
341: const char *p;
342: char *buf;
343: size_t sz;
344:
1.20 schwarze 345: assert(MDOC_TEXT == n->child->type);
346: p = mdoc_a2lib(n->child->string);
1.25 schwarze 347:
348: if (p) {
1.20 schwarze 349: free(n->child->string);
1.25 schwarze 350: n->child->string = mandoc_strdup(p);
1.15 schwarze 351: return(1);
352: }
353:
1.25 schwarze 354: sz = strlen(n->child->string) +
355: 2 + strlen("\\(lqlibrary\\(rq");
356: buf = mandoc_malloc(sz);
357: snprintf(buf, sz, "library \\(lq%s\\(rq", n->child->string);
1.20 schwarze 358: free(n->child->string);
1.25 schwarze 359: n->child->string = buf;
1.15 schwarze 360: return(1);
361: }
362:
363:
1.25 schwarze 364: /*
365: * Substitute the value of `St' for the corresponding formatted string.
366: * We're guaranteed that this exists (it's been verified during the
367: * validation phase).
368: */
369: /* ARGSUSED */
1.15 schwarze 370: static int
371: post_st(POST_ARGS)
372: {
373: const char *p;
374:
1.20 schwarze 375: assert(MDOC_TEXT == n->child->type);
376: p = mdoc_a2st(n->child->string);
1.34 schwarze 377: if (p != NULL) {
378: free(n->child->string);
379: n->child->string = mandoc_strdup(p);
380: }
1.15 schwarze 381: return(1);
382: }
383:
384:
1.25 schwarze 385: /*
386: * Look up the standard string in a table. We know that it exists from
387: * the validation phase, so assert on failure. If a standard key wasn't
388: * supplied, supply the default ``AT&T UNIX''.
389: */
1.15 schwarze 390: static int
391: post_at(POST_ARGS)
392: {
1.34 schwarze 393: struct mdoc_node *nn;
394: const char *p, *q;
395: char *buf;
396: size_t sz;
1.15 schwarze 397:
1.20 schwarze 398: if (n->child) {
399: assert(MDOC_TEXT == n->child->type);
400: p = mdoc_a2att(n->child->string);
1.34 schwarze 401: if (p) {
402: free(n->child->string);
403: n->child->string = mandoc_strdup(p);
404: } else {
405: p = "AT&T UNIX ";
406: q = n->child->string;
407: sz = strlen(p) + strlen(q) + 1;
408: buf = mandoc_malloc(sz);
409: strlcpy(buf, p, sz);
410: strlcat(buf, q, sz);
411: free(n->child->string);
412: n->child->string = buf;
413: }
1.15 schwarze 414: return(1);
415: }
416:
1.20 schwarze 417: nn = n;
1.15 schwarze 418: m->next = MDOC_NEXT_CHILD;
1.20 schwarze 419: if ( ! mdoc_word_alloc(m, nn->line, nn->pos, "AT&T UNIX"))
1.15 schwarze 420: return(0);
1.20 schwarze 421: m->last = nn;
1.15 schwarze 422: return(1);
423: }
424:
425:
1.25 schwarze 426: /*
427: * Mark the current section. The ``named'' section (lastnamed) is set
428: * whenever the current section isn't a custom section--we use this to
429: * keep track of section ordering. Also check that the section is
430: * allowed within the document's manual section.
431: */
1.15 schwarze 432: static int
1.1 kristaps 433: post_sh(POST_ARGS)
434: {
435: enum mdoc_sec sec;
1.25 schwarze 436: char buf[BUFSIZ];
1.1 kristaps 437:
1.20 schwarze 438: if (MDOC_HEAD != n->type)
1.1 kristaps 439: return(1);
440:
1.25 schwarze 441: if ( ! concat(m, buf, n->child, BUFSIZ))
1.1 kristaps 442: return(0);
1.32 schwarze 443: sec = mdoc_str2sec(buf);
1.30 schwarze 444: /*
445: * The first section should always make us move into a non-new
446: * state.
447: */
1.29 schwarze 448: if (SEC_NONE == m->lastnamed || SEC_CUSTOM != sec)
1.1 kristaps 449: m->lastnamed = sec;
1.42 ! schwarze 450:
! 451: /*
! 452: * Switch the parser's SYNOPSIS mode, to be copied
! 453: * into individual nodes when creating them.
! 454: * Note that this mode can also be set and unset
! 455: * using the roff nS register.
! 456: */
! 457: if (SEC_SYNOPSIS == sec)
! 458: m->flags |= MDOC_SYNOPSIS;
! 459: else
! 460: m->flags &= ~MDOC_SYNOPSIS;
1.1 kristaps 461:
1.25 schwarze 462: /* Some sections only live in certain manual sections. */
463:
1.1 kristaps 464: switch ((m->lastsec = sec)) {
465: case (SEC_RETURN_VALUES):
466: /* FALLTHROUGH */
467: case (SEC_ERRORS):
1.35 schwarze 468: assert(m->meta.msec);
469: if (*m->meta.msec == '2')
1.1 kristaps 470: break;
1.35 schwarze 471: if (*m->meta.msec == '3')
472: break;
473: if (*m->meta.msec == '9')
474: break;
1.36 schwarze 475: return(mdoc_nmsg(m, n, MANDOCERR_SECMSEC));
1.1 kristaps 476: default:
477: break;
478: }
479: return(1);
480: }
481:
482:
1.25 schwarze 483: /*
484: * Parse out the contents of `Dt'. See in-line documentation for how we
485: * handle the various fields of this macro.
486: */
1.1 kristaps 487: static int
488: post_dt(POST_ARGS)
489: {
1.20 schwarze 490: struct mdoc_node *nn;
1.1 kristaps 491: const char *cp;
492:
493: if (m->meta.title)
494: free(m->meta.title);
495: if (m->meta.vol)
496: free(m->meta.vol);
497: if (m->meta.arch)
498: free(m->meta.arch);
499:
500: m->meta.title = m->meta.vol = m->meta.arch = NULL;
501: /* Handles: `.Dt'
502: * --> title = unknown, volume = local, msec = 0, arch = NULL
503: */
504:
1.20 schwarze 505: if (NULL == (nn = n->child)) {
1.25 schwarze 506: /* XXX: make these macro values. */
1.35 schwarze 507: /* FIXME: warn about missing values. */
1.40 schwarze 508: m->meta.title = mandoc_strdup("UNKNOWN");
509: m->meta.vol = mandoc_strdup("LOCAL");
1.35 schwarze 510: m->meta.msec = mandoc_strdup("1");
1.20 schwarze 511: return(post_prol(m, n));
1.1 kristaps 512: }
513:
514: /* Handles: `.Dt TITLE'
515: * --> title = TITLE, volume = local, msec = 0, arch = NULL
516: */
517:
1.40 schwarze 518: m->meta.title = mandoc_strdup
519: ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
1.1 kristaps 520:
1.20 schwarze 521: if (NULL == (nn = nn->next)) {
1.35 schwarze 522: /* FIXME: warn about missing msec. */
1.25 schwarze 523: /* XXX: make this a macro value. */
1.40 schwarze 524: m->meta.vol = mandoc_strdup("LOCAL");
1.35 schwarze 525: m->meta.msec = mandoc_strdup("1");
1.20 schwarze 526: return(post_prol(m, n));
1.1 kristaps 527: }
528:
529: /* Handles: `.Dt TITLE SEC'
530: * --> title = TITLE, volume = SEC is msec ?
531: * format(msec) : SEC,
532: * msec = SEC is msec ? atoi(msec) : 0,
533: * arch = NULL
534: */
535:
1.20 schwarze 536: cp = mdoc_a2msec(nn->string);
1.1 kristaps 537: if (cp) {
1.25 schwarze 538: m->meta.vol = mandoc_strdup(cp);
1.35 schwarze 539: m->meta.msec = mandoc_strdup(nn->string);
1.36 schwarze 540: } else if (mdoc_nmsg(m, n, MANDOCERR_BADMSEC)) {
1.25 schwarze 541: m->meta.vol = mandoc_strdup(nn->string);
1.35 schwarze 542: m->meta.msec = mandoc_strdup(nn->string);
543: } else
544: return(0);
1.1 kristaps 545:
1.20 schwarze 546: if (NULL == (nn = nn->next))
547: return(post_prol(m, n));
1.1 kristaps 548:
549: /* Handles: `.Dt TITLE SEC VOL'
550: * --> title = TITLE, volume = VOL is vol ?
551: * format(VOL) :
552: * VOL is arch ? format(arch) :
553: * VOL
554: */
555:
1.20 schwarze 556: cp = mdoc_a2vol(nn->string);
1.1 kristaps 557: if (cp) {
558: free(m->meta.vol);
1.25 schwarze 559: m->meta.vol = mandoc_strdup(cp);
1.1 kristaps 560: } else {
1.35 schwarze 561: /* FIXME: warn about bad arch. */
1.20 schwarze 562: cp = mdoc_a2arch(nn->string);
1.1 kristaps 563: if (NULL == cp) {
564: free(m->meta.vol);
1.25 schwarze 565: m->meta.vol = mandoc_strdup(nn->string);
566: } else
567: m->meta.arch = mandoc_strdup(cp);
1.1 kristaps 568: }
569:
570: /* Ignore any subsequent parameters... */
1.25 schwarze 571: /* FIXME: warn about subsequent parameters. */
1.1 kristaps 572:
1.20 schwarze 573: return(post_prol(m, n));
1.1 kristaps 574: }
575:
576:
1.25 schwarze 577: /*
578: * Set the operating system by way of the `Os' macro. Note that if an
579: * argument isn't provided and -DOSNAME="\"foo\"" is provided during
580: * compilation, this value will be used instead of filling in "sysname
581: * release" from uname().
582: */
1.1 kristaps 583: static int
584: post_os(POST_ARGS)
585: {
1.25 schwarze 586: char buf[BUFSIZ];
587: #ifndef OSNAME
1.1 kristaps 588: struct utsname utsname;
1.24 schwarze 589: #endif
590:
1.1 kristaps 591: if (m->meta.os)
592: free(m->meta.os);
593:
1.25 schwarze 594: if ( ! concat(m, buf, n->child, BUFSIZ))
1.1 kristaps 595: return(0);
596:
1.36 schwarze 597: /* XXX: yes, these can all be dynamically-adjusted buffers, but
598: * it's really not worth the extra hackery.
599: */
600:
1.25 schwarze 601: if ('\0' == buf[0]) {
602: #ifdef OSNAME
1.36 schwarze 603: if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
604: mdoc_nmsg(m, n, MANDOCERR_MEM);
605: return(0);
606: }
1.25 schwarze 607: #else /*!OSNAME */
1.1 kristaps 608: if (-1 == uname(&utsname))
1.36 schwarze 609: return(mdoc_nmsg(m, n, MANDOCERR_UTSNAME));
610:
611: if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
612: mdoc_nmsg(m, n, MANDOCERR_MEM);
613: return(0);
614: }
615: if (strlcat(buf, " ", 64) >= BUFSIZ) {
616: mdoc_nmsg(m, n, MANDOCERR_MEM);
617: return(0);
618: }
619: if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
620: mdoc_nmsg(m, n, MANDOCERR_MEM);
621: return(0);
622: }
1.25 schwarze 623: #endif /*!OSNAME*/
1.1 kristaps 624: }
625:
1.25 schwarze 626: m->meta.os = mandoc_strdup(buf);
1.20 schwarze 627: return(post_prol(m, n));
1.1 kristaps 628: }
629:
630:
631: /*
632: * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
1.25 schwarze 633: * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
634: * argument has NOT been provided. See post_bl_width() for converting
635: * the -width string.
1.1 kristaps 636: */
637: static int
1.20 schwarze 638: post_bl_tagwidth(POST_ARGS)
1.1 kristaps 639: {
1.25 schwarze 640: struct mdoc_node *nn;
1.41 schwarze 641: size_t sz, ssz;
1.25 schwarze 642: int i;
643: char buf[NUMSIZ];
1.1 kristaps 644:
1.41 schwarze 645: sz = 10;
1.38 schwarze 646:
647: for (nn = n->body->child; nn; nn = nn->next) {
1.41 schwarze 648: if (MDOC_It != nn->tok)
649: continue;
1.25 schwarze 650:
1.20 schwarze 651: assert(MDOC_BLOCK == nn->type);
652: nn = nn->head->child;
1.41 schwarze 653:
654: if (MDOC_TEXT == nn->type) {
1.25 schwarze 655: sz = strlen(nn->string) + 1;
1.41 schwarze 656: break;
657: }
658:
659: if (0 != (ssz = mdoc_macro2len(nn->tok)))
660: sz = ssz;
661: else if ( ! mdoc_nmsg(m, n, MANDOCERR_NOWIDTHARG))
662: return(0);
663:
664: break;
1.1 kristaps 665: }
666:
1.41 schwarze 667: /* Defaults to ten ens. */
668:
1.25 schwarze 669: snprintf(buf, NUMSIZ, "%zun", sz);
1.1 kristaps 670:
671: /*
672: * We have to dynamically add this to the macro's argument list.
673: * We're guaranteed that a MDOC_Width doesn't already exist.
674: */
675:
1.41 schwarze 676: assert(n->args);
677: i = (int)(n->args->argc)++;
678:
679: n->args->argv = mandoc_realloc(n->args->argv,
680: n->args->argc * sizeof(struct mdoc_argv));
1.1 kristaps 681:
1.41 schwarze 682: n->args->argv[i].arg = MDOC_Width;
683: n->args->argv[i].line = n->line;
684: n->args->argv[i].pos = n->pos;
685: n->args->argv[i].sz = 1;
686: n->args->argv[i].value = mandoc_malloc(sizeof(char *));
687: n->args->argv[i].value[0] = mandoc_strdup(buf);
1.1 kristaps 688:
1.41 schwarze 689: /* Set our width! */
690: n->data.Bl.width = n->args->argv[i].value[0];
1.1 kristaps 691: return(1);
692: }
693:
694:
1.25 schwarze 695: /*
696: * Calculate the real width of a list from the -width string, which may
697: * contain a macro (with a known default width), a literal string, or a
698: * scaling width.
699: */
1.1 kristaps 700: static int
1.20 schwarze 701: post_bl_width(POST_ARGS)
1.1 kristaps 702: {
703: size_t width;
1.28 schwarze 704: int i;
705: enum mdoct tok;
1.25 schwarze 706: char buf[NUMSIZ];
1.1 kristaps 707:
708: /*
709: * If the value to -width is a macro, then we re-write it to be
710: * the macro's width as set in share/tmac/mdoc/doc-common.
711: */
712:
1.41 schwarze 713: if (0 == strcmp(n->data.Bl.width, "Ds"))
1.9 schwarze 714: width = 6;
1.41 schwarze 715: else if (MDOC_MAX == (tok = mdoc_hash_find(n->data.Bl.width)))
1.1 kristaps 716: return(1);
717: else if (0 == (width = mdoc_macro2len(tok)))
1.36 schwarze 718: return(mdoc_nmsg(m, n, MANDOCERR_BADWIDTH));
1.1 kristaps 719:
720: /* The value already exists: free and reallocate it. */
721:
1.41 schwarze 722: assert(n->args);
723:
724: for (i = 0; i < (int)n->args->argc; i++)
725: if (MDOC_Width == n->args->argv[i].arg)
726: break;
727:
728: assert(i < (int)n->args->argc);
729:
1.25 schwarze 730: snprintf(buf, NUMSIZ, "%zun", width);
1.20 schwarze 731: free(n->args->argv[i].value[0]);
1.25 schwarze 732: n->args->argv[i].value[0] = mandoc_strdup(buf);
1.41 schwarze 733:
734: /* Set our width! */
735: n->data.Bl.width = n->args->argv[i].value[0];
1.1 kristaps 736: return(1);
737: }
738:
739:
1.25 schwarze 740: /*
741: * Do processing for -column lists, which can have two distinct styles
742: * of invocation. Merge this two styles into a consistent form.
743: */
1.20 schwarze 744: /* ARGSUSED */
1.1 kristaps 745: static int
1.6 schwarze 746: post_bl_head(POST_ARGS)
747: {
748: int i, c;
1.20 schwarze 749: struct mdoc_node *np, *nn, *nnp;
1.6 schwarze 750:
1.41 schwarze 751: if (LIST_column != n->data.Bl.type)
752: return(1);
753: else if (NULL == n->child)
1.6 schwarze 754: return(1);
755:
1.20 schwarze 756: np = n->parent;
757: assert(np->args);
1.6 schwarze 758:
1.20 schwarze 759: for (c = 0; c < (int)np->args->argc; c++)
760: if (MDOC_Column == np->args->argv[c].arg)
1.6 schwarze 761: break;
762:
1.41 schwarze 763: assert(c < (int)np->args->argc);
1.20 schwarze 764: assert(0 == np->args->argv[c].sz);
1.6 schwarze 765:
766: /*
767: * Accomodate for new-style groff column syntax. Shuffle the
768: * child nodes, all of which must be TEXT, as arguments for the
769: * column field. Then, delete the head children.
770: */
771:
1.20 schwarze 772: np->args->argv[c].sz = (size_t)n->nchild;
1.25 schwarze 773: np->args->argv[c].value = mandoc_malloc
1.20 schwarze 774: ((size_t)n->nchild * sizeof(char *));
1.6 schwarze 775:
1.20 schwarze 776: for (i = 0, nn = n->child; nn; i++) {
777: np->args->argv[c].value[i] = nn->string;
1.6 schwarze 778: nn->string = NULL;
779: nnp = nn;
780: nn = nn->next;
1.30 schwarze 781: mdoc_node_delete(NULL, nnp);
1.6 schwarze 782: }
783:
1.20 schwarze 784: n->nchild = 0;
785: n->child = NULL;
1.6 schwarze 786: return(1);
787: }
788:
789:
790: static int
1.1 kristaps 791: post_bl(POST_ARGS)
792: {
1.41 schwarze 793: struct mdoc_node *nn;
794: const char *ww;
1.1 kristaps 795:
1.20 schwarze 796: if (MDOC_HEAD == n->type)
797: return(post_bl_head(m, n));
798: if (MDOC_BLOCK != n->type)
1.1 kristaps 799: return(1);
800:
801: /*
802: * These are fairly complicated, so we've broken them into two
803: * functions. post_bl_tagwidth() is called when a -tag is
804: * specified, but no -width (it must be guessed). The second
805: * when a -width is specified (macro indicators must be
806: * rewritten into real lengths).
807: */
808:
1.41 schwarze 809: ww = n->data.Bl.width;
1.1 kristaps 810:
1.41 schwarze 811: if (LIST_tag == n->data.Bl.type && NULL == n->data.Bl.width) {
1.20 schwarze 812: if ( ! post_bl_tagwidth(m, n))
1.1 kristaps 813: return(0);
1.41 schwarze 814: } else if (NULL != n->data.Bl.width) {
1.20 schwarze 815: if ( ! post_bl_width(m, n))
1.1 kristaps 816: return(0);
1.41 schwarze 817: } else
818: return(1);
819:
820: assert(n->data.Bl.width);
821:
822: /* If it has changed, propogate new width to children. */
823:
824: if (ww == n->data.Bl.width)
825: return(1);
826:
827: for (nn = n->child; nn; nn = nn->next)
828: if (MDOC_Bl == nn->tok)
829: nn->data.Bl.width = n->data.Bl.width;
1.1 kristaps 830:
1.3 schwarze 831: return(1);
832: }
833:
834:
1.25 schwarze 835: /*
836: * The `Pa' macro defaults to a tilde if no value is provided as an
837: * argument.
838: */
1.3 schwarze 839: static int
1.25 schwarze 840: post_pa(POST_ARGS)
1.3 schwarze 841: {
1.20 schwarze 842: struct mdoc_node *np;
1.3 schwarze 843:
1.20 schwarze 844: if (n->child)
1.3 schwarze 845: return(1);
846:
1.20 schwarze 847: np = n;
1.3 schwarze 848: m->next = MDOC_NEXT_CHILD;
1.20 schwarze 849: if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
1.39 schwarze 850: return(0);
851: m->last = np;
852: return(1);
853: }
854:
855:
856: /*
857: * Empty `Li' macros get an empty string to make front-ends add an extra
858: * space.
859: */
860: static int
861: post_li(POST_ARGS)
862: {
863: struct mdoc_node *np;
864:
865: if (n->child)
866: return(1);
867:
868: np = n;
869: m->next = MDOC_NEXT_CHILD;
870: if ( ! mdoc_word_alloc(m, n->line, n->pos, ""))
1.3 schwarze 871: return(0);
1.20 schwarze 872: m->last = np;
1.1 kristaps 873: return(1);
874: }
875:
876:
1.25 schwarze 877: /*
878: * The `Ar' macro defaults to two strings "file ..." if no value is
879: * provided as an argument.
880: */
1.1 kristaps 881: static int
882: post_ar(POST_ARGS)
883: {
1.20 schwarze 884: struct mdoc_node *np;
1.1 kristaps 885:
1.20 schwarze 886: if (n->child)
1.1 kristaps 887: return(1);
888:
1.20 schwarze 889: np = n;
1.1 kristaps 890: m->next = MDOC_NEXT_CHILD;
1.25 schwarze 891: /* XXX: make into macro values. */
1.20 schwarze 892: if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
1.1 kristaps 893: return(0);
1.20 schwarze 894: if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
1.1 kristaps 895: return(0);
1.20 schwarze 896: m->last = np;
1.1 kristaps 897: return(1);
898: }
899:
900:
1.25 schwarze 901: /*
1.26 schwarze 902: * Parse the date field in `Dd'.
1.25 schwarze 903: */
1.1 kristaps 904: static int
905: post_dd(POST_ARGS)
906: {
1.25 schwarze 907: char buf[DATESIZ];
1.1 kristaps 908:
1.25 schwarze 909: if ( ! concat(m, buf, n->child, DATESIZ))
1.1 kristaps 910: return(0);
911:
1.26 schwarze 912: m->meta.date = mandoc_a2time
913: (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
914:
1.25 schwarze 915: if (0 == m->meta.date) {
1.36 schwarze 916: if ( ! mdoc_nmsg(m, n, MANDOCERR_BADDATE))
1.1 kristaps 917: return(0);
918: m->meta.date = time(NULL);
919: }
920:
1.20 schwarze 921: return(post_prol(m, n));
1.1 kristaps 922: }
923:
924:
1.25 schwarze 925: /*
926: * Remove prologue macros from the document after they're processed.
927: * The final document uses mdoc_meta for these values and discards the
928: * originals.
929: */
1.1 kristaps 930: static int
931: post_prol(POST_ARGS)
932: {
1.18 schwarze 933:
1.30 schwarze 934: mdoc_node_delete(m, n);
1.18 schwarze 935: if (m->meta.title && m->meta.date && m->meta.os)
936: m->flags |= MDOC_PBODY;
1.1 kristaps 937: return(1);
938: }
939:
940:
1.25 schwarze 941: /*
942: * Trigger a literal context.
943: */
1.1 kristaps 944: static int
945: pre_dl(PRE_ARGS)
946: {
947:
1.7 schwarze 948: if (MDOC_BODY == n->type)
949: m->flags |= MDOC_LITERAL;
1.1 kristaps 950: return(1);
951: }
952:
953:
1.22 schwarze 954: static int
1.1 kristaps 955: pre_bd(PRE_ARGS)
956: {
957:
958: if (MDOC_BODY != n->type)
959: return(1);
960:
1.41 schwarze 961: if (DISP_literal == n->data.Bd.type)
962: m->flags |= MDOC_LITERAL;
963: if (DISP_unfilled == n->data.Bd.type)
964: m->flags |= MDOC_LITERAL;
1.1 kristaps 965:
966: return(1);
967: }
968:
969:
970: static int
971: post_display(POST_ARGS)
972: {
973:
1.20 schwarze 974: if (MDOC_BODY == n->type)
1.1 kristaps 975: m->flags &= ~MDOC_LITERAL;
976: return(1);
977: }
978:
979:
1.23 schwarze 980: static inline int
1.31 schwarze 981: order_rs(enum mdoct t)
1.23 schwarze 982: {
983: int i;
984:
1.31 schwarze 985: for (i = 0; i < (int)RSORD_MAX; i++)
1.23 schwarze 986: if (rsord[i] == t)
987: return(i);
988:
989: abort();
990: /* NOTREACHED */
991: }
992:
993:
994: /* ARGSUSED */
995: static int
996: post_rs(POST_ARGS)
997: {
998: struct mdoc_node *nn, *next, *prev;
999: int o;
1000:
1001: if (MDOC_BLOCK != n->type)
1002: return(1);
1003:
1004: assert(n->body->child);
1005: for (next = NULL, nn = n->body->child->next; nn; nn = next) {
1006: o = order_rs(nn->tok);
1007:
1008: /* Remove `nn' from the chain. */
1009: next = nn->next;
1010: if (next)
1011: next->prev = nn->prev;
1012:
1013: prev = nn->prev;
1014: if (prev)
1015: prev->next = nn->next;
1016:
1017: nn->prev = nn->next = NULL;
1018:
1019: /*
1020: * Scan back until we reach a node that's ordered before
1021: * us, then set ourselves as being the next.
1022: */
1023: for ( ; prev; prev = prev->prev)
1024: if (order_rs(prev->tok) <= o)
1025: break;
1026:
1027: nn->prev = prev;
1028: if (prev) {
1029: if (prev->next)
1030: prev->next->prev = nn;
1031: nn->next = prev->next;
1032: prev->next = nn;
1033: continue;
1034: }
1035:
1036: n->body->child->prev = nn;
1037: nn->next = n->body->child;
1038: n->body->child = nn;
1039: }
1040: return(1);
1041: }