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