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