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