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