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