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