Annotation of mandoc/mdoc_action.c, Revision 1.83
1.83 ! kristaps 1: /* $Id: mdoc_action.c,v 1.82 2010/11/29 14:50:33 kristaps Exp $ */
1.1 kristaps 2: /*
1.76 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.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:
1.59 kristaps 31: #include "mandoc.h"
1.1 kristaps 32: #include "libmdoc.h"
1.46 kristaps 33: #include "libmandoc.h"
1.72 kristaps 34:
35: /*
36: * FIXME: this file is deprecated. All future "actions" should be
37: * pushed into mdoc_validate.c.
38: */
1.1 kristaps 39:
1.35 kristaps 40: #define POST_ARGS struct mdoc *m, struct mdoc_node *n
1.60 kristaps 41: #define PRE_ARGS struct mdoc *m, struct mdoc_node *n
1.1 kristaps 42:
1.46 kristaps 43: #define NUMSIZ 32
44: #define DATESIZ 32
45:
1.1 kristaps 46: struct actions {
47: int (*pre)(PRE_ARGS);
48: int (*post)(POST_ARGS);
49: };
50:
1.46 kristaps 51: static int concat(struct mdoc *, char *,
52: const struct mdoc_node *, size_t);
1.39 kristaps 53:
1.1 kristaps 54: static int post_bl(POST_ARGS);
1.14 kristaps 55: static int post_bl_head(POST_ARGS);
1.25 kristaps 56: static int post_bl_tagwidth(POST_ARGS);
1.70 kristaps 57: static int post_bl_width(POST_ARGS);
1.1 kristaps 58: static int post_dd(POST_ARGS);
59: static int post_display(POST_ARGS);
60: static int post_dt(POST_ARGS);
61: static int post_nm(POST_ARGS);
62: static int post_os(POST_ARGS);
1.46 kristaps 63: static int post_pa(POST_ARGS);
1.1 kristaps 64: static int post_prol(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);
69: static int pre_dl(PRE_ARGS);
70:
1.37 kristaps 71: static const struct actions mdoc_actions[MDOC_MAX] = {
1.12 kristaps 72: { NULL, NULL }, /* Ap */
1.1 kristaps 73: { NULL, post_dd }, /* Dd */
74: { NULL, post_dt }, /* Dt */
75: { NULL, post_os }, /* Os */
1.81 kristaps 76: { NULL, NULL }, /* Sh */
1.1 kristaps 77: { NULL, NULL }, /* Ss */
78: { NULL, NULL }, /* Pp */
79: { NULL, NULL }, /* D1 */
80: { pre_dl, post_display }, /* Dl */
81: { pre_bd, post_display }, /* Bd */
82: { NULL, NULL }, /* Ed */
1.68 kristaps 83: { NULL, post_bl }, /* Bl */
1.1 kristaps 84: { NULL, NULL }, /* El */
85: { NULL, NULL }, /* It */
86: { NULL, NULL }, /* Ad */
87: { NULL, NULL }, /* An */
1.80 kristaps 88: { NULL, NULL }, /* Ar */
1.35 kristaps 89: { NULL, NULL }, /* Cd */
1.1 kristaps 90: { NULL, NULL }, /* Cm */
91: { NULL, NULL }, /* Dv */
92: { NULL, NULL }, /* Er */
93: { NULL, NULL }, /* Ev */
94: { NULL, post_std }, /* Ex */
95: { NULL, NULL }, /* Fa */
96: { NULL, NULL }, /* Fd */
97: { NULL, NULL }, /* Fl */
98: { NULL, NULL }, /* Fn */
99: { NULL, NULL }, /* Ft */
100: { NULL, NULL }, /* Ic */
101: { NULL, NULL }, /* In */
1.80 kristaps 102: { NULL, NULL }, /* Li */
1.1 kristaps 103: { NULL, NULL }, /* Nd */
104: { NULL, post_nm }, /* Nm */
105: { NULL, NULL }, /* Op */
106: { NULL, NULL }, /* Ot */
1.46 kristaps 107: { NULL, post_pa }, /* Pa */
1.1 kristaps 108: { NULL, post_std }, /* Rv */
1.26 kristaps 109: { NULL, post_st }, /* St */
1.1 kristaps 110: { NULL, NULL }, /* Va */
111: { NULL, NULL }, /* Vt */
112: { NULL, NULL }, /* Xr */
113: { NULL, NULL }, /* %A */
114: { NULL, NULL }, /* %B */
115: { NULL, NULL }, /* %D */
116: { NULL, NULL }, /* %I */
117: { NULL, NULL }, /* %J */
118: { NULL, NULL }, /* %N */
119: { NULL, NULL }, /* %O */
120: { NULL, NULL }, /* %P */
121: { NULL, NULL }, /* %R */
122: { NULL, NULL }, /* %T */
123: { NULL, NULL }, /* %V */
124: { NULL, NULL }, /* Ac */
125: { NULL, NULL }, /* Ao */
126: { NULL, NULL }, /* Aq */
1.82 kristaps 127: { NULL, NULL }, /* At */
1.1 kristaps 128: { NULL, NULL }, /* Bc */
129: { NULL, NULL }, /* Bf */
130: { NULL, NULL }, /* Bo */
131: { NULL, NULL }, /* Bq */
132: { NULL, NULL }, /* Bsx */
133: { NULL, NULL }, /* Bx */
134: { NULL, NULL }, /* Db */
135: { NULL, NULL }, /* Dc */
136: { NULL, NULL }, /* Do */
137: { NULL, NULL }, /* Dq */
138: { NULL, NULL }, /* Ec */
139: { NULL, NULL }, /* Ef */
140: { NULL, NULL }, /* Em */
141: { NULL, NULL }, /* Eo */
142: { NULL, NULL }, /* Fx */
143: { NULL, NULL }, /* Ms */
144: { NULL, NULL }, /* No */
145: { NULL, NULL }, /* Ns */
146: { NULL, NULL }, /* Nx */
147: { NULL, NULL }, /* Ox */
148: { NULL, NULL }, /* Pc */
149: { NULL, NULL }, /* Pf */
150: { NULL, NULL }, /* Po */
151: { NULL, NULL }, /* Pq */
152: { NULL, NULL }, /* Qc */
153: { NULL, NULL }, /* Ql */
154: { NULL, NULL }, /* Qo */
155: { NULL, NULL }, /* Qq */
156: { NULL, NULL }, /* Re */
1.79 kristaps 157: { NULL, NULL }, /* Rs */
1.1 kristaps 158: { NULL, NULL }, /* Sc */
159: { NULL, NULL }, /* So */
160: { NULL, NULL }, /* Sq */
161: { NULL, NULL }, /* Sm */
162: { NULL, NULL }, /* Sx */
163: { NULL, NULL }, /* Sy */
164: { NULL, NULL }, /* Tn */
165: { NULL, NULL }, /* Ux */
166: { NULL, NULL }, /* Xc */
167: { NULL, NULL }, /* Xo */
168: { NULL, NULL }, /* Fo */
169: { NULL, NULL }, /* Fc */
170: { NULL, NULL }, /* Oo */
171: { NULL, NULL }, /* Oc */
172: { NULL, NULL }, /* Bk */
173: { NULL, NULL }, /* Ek */
174: { NULL, NULL }, /* Bt */
175: { NULL, NULL }, /* Hf */
176: { NULL, NULL }, /* Fr */
177: { NULL, NULL }, /* Ud */
1.83 ! kristaps 178: { NULL, NULL }, /* Lb */
1.1 kristaps 179: { NULL, NULL }, /* Lp */
1.44 kristaps 180: { NULL, NULL }, /* Lk */
1.1 kristaps 181: { NULL, NULL }, /* Mt */
182: { NULL, NULL }, /* Brq */
183: { NULL, NULL }, /* Bro */
184: { NULL, NULL }, /* Brc */
185: { NULL, NULL }, /* %C */
186: { NULL, NULL }, /* Es */
187: { NULL, NULL }, /* En */
188: { NULL, NULL }, /* Dx */
189: { NULL, NULL }, /* %Q */
1.30 kristaps 190: { NULL, NULL }, /* br */
191: { NULL, NULL }, /* sp */
1.43 kristaps 192: { NULL, NULL }, /* %U */
1.64 kristaps 193: { NULL, NULL }, /* Ta */
1.1 kristaps 194: };
195:
1.45 kristaps 196: #define RSORD_MAX 14
1.1 kristaps 197:
1.54 kristaps 198: static const enum mdoct rsord[RSORD_MAX] = {
1.39 kristaps 199: MDOC__A,
200: MDOC__T,
201: MDOC__B,
202: MDOC__I,
203: MDOC__J,
204: MDOC__R,
205: MDOC__N,
206: MDOC__V,
207: MDOC__P,
208: MDOC__Q,
209: MDOC__D,
210: MDOC__O,
1.45 kristaps 211: MDOC__C,
212: MDOC__U
1.39 kristaps 213: };
1.2 kristaps 214:
215:
1.1 kristaps 216: int
1.60 kristaps 217: mdoc_action_pre(struct mdoc *m, struct mdoc_node *n)
1.1 kristaps 218: {
219:
220: switch (n->type) {
221: case (MDOC_ROOT):
1.2 kristaps 222: /* FALLTHROUGH */
1.1 kristaps 223: case (MDOC_TEXT):
1.2 kristaps 224: return(1);
225: default:
1.1 kristaps 226: break;
227: }
1.2 kristaps 228:
1.6 kristaps 229: if (NULL == mdoc_actions[n->tok].pre)
1.2 kristaps 230: return(1);
1.6 kristaps 231: return((*mdoc_actions[n->tok].pre)(m, n));
1.1 kristaps 232: }
233:
234:
235: int
236: mdoc_action_post(struct mdoc *m)
237: {
238:
239: if (MDOC_ACTED & m->last->flags)
240: return(1);
241: m->last->flags |= MDOC_ACTED;
242:
243: switch (m->last->type) {
244: case (MDOC_TEXT):
1.2 kristaps 245: /* FALLTHROUGH */
1.1 kristaps 246: case (MDOC_ROOT):
1.2 kristaps 247: return(1);
248: default:
1.1 kristaps 249: break;
250: }
1.2 kristaps 251:
252: if (NULL == mdoc_actions[m->last->tok].post)
253: return(1);
1.35 kristaps 254: return((*mdoc_actions[m->last->tok].post)(m, m->last));
1.2 kristaps 255: }
256:
257:
1.46 kristaps 258: /*
259: * Concatenate sibling nodes together. All siblings must be of type
260: * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
261: * single whitespace.
262: */
1.2 kristaps 263: static int
1.46 kristaps 264: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
1.2 kristaps 265: {
266:
1.46 kristaps 267: assert(sz);
268: p[0] = '\0';
1.2 kristaps 269: for ( ; n; n = n->next) {
270: assert(MDOC_TEXT == n->type);
1.59 kristaps 271: /*
272: * XXX: yes, these can technically be resized, but it's
273: * highly unlikely that we're going to get here, so let
274: * it slip for now.
275: */
276: if (strlcat(p, n->string, sz) >= sz) {
277: mdoc_nmsg(m, n, MANDOCERR_MEM);
278: return(0);
279: }
1.2 kristaps 280: if (NULL == n->next)
281: continue;
1.59 kristaps 282: if (strlcat(p, " ", sz) >= sz) {
283: mdoc_nmsg(m, n, MANDOCERR_MEM);
284: return(0);
285: }
1.2 kristaps 286: }
287:
1.1 kristaps 288: return(1);
289: }
290:
291:
1.46 kristaps 292: /*
293: * Macros accepting `-std' as an argument have the name of the current
294: * document (`Nm') filled in as the argument if it's not provided.
295: */
1.1 kristaps 296: static int
297: post_std(POST_ARGS)
298: {
1.59 kristaps 299: struct mdoc_node *nn;
1.1 kristaps 300:
1.35 kristaps 301: if (n->child)
1.1 kristaps 302: return(1);
1.59 kristaps 303: if (NULL == m->meta.name)
304: return(1);
1.35 kristaps 305:
306: nn = n;
307: m->next = MDOC_NEXT_CHILD;
1.59 kristaps 308:
1.35 kristaps 309: if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
310: return(0);
311: m->last = nn;
1.1 kristaps 312: return(1);
313: }
314:
315:
1.46 kristaps 316: /*
317: * The `Nm' macro's first use sets the name of the document. See also
318: * post_std(), etc.
319: */
1.1 kristaps 320: static int
321: post_nm(POST_ARGS)
322: {
1.46 kristaps 323: char buf[BUFSIZ];
1.1 kristaps 324:
325: if (m->meta.name)
326: return(1);
1.46 kristaps 327: if ( ! concat(m, buf, n->child, BUFSIZ))
1.2 kristaps 328: return(0);
1.46 kristaps 329: m->meta.name = mandoc_strdup(buf);
1.1 kristaps 330: return(1);
331: }
1.27 kristaps 332:
1.46 kristaps 333: /*
334: * Substitute the value of `St' for the corresponding formatted string.
335: * We're guaranteed that this exists (it's been verified during the
336: * validation phase).
337: */
338: /* ARGSUSED */
1.27 kristaps 339: static int
1.26 kristaps 340: post_st(POST_ARGS)
341: {
342: const char *p;
343:
1.35 kristaps 344: assert(MDOC_TEXT == n->child->type);
345: p = mdoc_a2st(n->child->string);
1.56 kristaps 346: if (p != NULL) {
347: free(n->child->string);
348: n->child->string = mandoc_strdup(p);
349: }
1.1 kristaps 350: return(1);
351: }
352:
353:
1.46 kristaps 354: /*
355: * Parse out the contents of `Dt'. See in-line documentation for how we
356: * handle the various fields of this macro.
357: */
1.1 kristaps 358: static int
359: post_dt(POST_ARGS)
360: {
1.35 kristaps 361: struct mdoc_node *nn;
1.1 kristaps 362: const char *cp;
363:
364: if (m->meta.title)
365: free(m->meta.title);
366: if (m->meta.vol)
367: free(m->meta.vol);
368: if (m->meta.arch)
369: free(m->meta.arch);
370:
371: m->meta.title = m->meta.vol = m->meta.arch = NULL;
372: /* Handles: `.Dt'
373: * --> title = unknown, volume = local, msec = 0, arch = NULL
374: */
375:
1.35 kristaps 376: if (NULL == (nn = n->child)) {
1.46 kristaps 377: /* XXX: make these macro values. */
1.58 kristaps 378: /* FIXME: warn about missing values. */
1.63 kristaps 379: m->meta.title = mandoc_strdup("UNKNOWN");
380: m->meta.vol = mandoc_strdup("LOCAL");
1.58 kristaps 381: m->meta.msec = mandoc_strdup("1");
1.35 kristaps 382: return(post_prol(m, n));
1.1 kristaps 383: }
384:
385: /* Handles: `.Dt TITLE'
386: * --> title = TITLE, volume = local, msec = 0, arch = NULL
387: */
388:
1.63 kristaps 389: m->meta.title = mandoc_strdup
390: ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
1.1 kristaps 391:
1.35 kristaps 392: if (NULL == (nn = nn->next)) {
1.58 kristaps 393: /* FIXME: warn about missing msec. */
1.46 kristaps 394: /* XXX: make this a macro value. */
1.63 kristaps 395: m->meta.vol = mandoc_strdup("LOCAL");
1.58 kristaps 396: m->meta.msec = mandoc_strdup("1");
1.35 kristaps 397: return(post_prol(m, n));
1.1 kristaps 398: }
399:
400: /* Handles: `.Dt TITLE SEC'
401: * --> title = TITLE, volume = SEC is msec ?
402: * format(msec) : SEC,
403: * msec = SEC is msec ? atoi(msec) : 0,
404: * arch = NULL
405: */
406:
1.35 kristaps 407: cp = mdoc_a2msec(nn->string);
1.1 kristaps 408: if (cp) {
1.46 kristaps 409: m->meta.vol = mandoc_strdup(cp);
1.58 kristaps 410: m->meta.msec = mandoc_strdup(nn->string);
1.59 kristaps 411: } else if (mdoc_nmsg(m, n, MANDOCERR_BADMSEC)) {
1.46 kristaps 412: m->meta.vol = mandoc_strdup(nn->string);
1.58 kristaps 413: m->meta.msec = mandoc_strdup(nn->string);
414: } else
415: return(0);
1.1 kristaps 416:
1.35 kristaps 417: if (NULL == (nn = nn->next))
418: return(post_prol(m, n));
1.1 kristaps 419:
420: /* Handles: `.Dt TITLE SEC VOL'
421: * --> title = TITLE, volume = VOL is vol ?
422: * format(VOL) :
423: * VOL is arch ? format(arch) :
424: * VOL
425: */
426:
1.35 kristaps 427: cp = mdoc_a2vol(nn->string);
1.1 kristaps 428: if (cp) {
429: free(m->meta.vol);
1.46 kristaps 430: m->meta.vol = mandoc_strdup(cp);
1.1 kristaps 431: } else {
1.58 kristaps 432: /* FIXME: warn about bad arch. */
1.35 kristaps 433: cp = mdoc_a2arch(nn->string);
1.1 kristaps 434: if (NULL == cp) {
435: free(m->meta.vol);
1.46 kristaps 436: m->meta.vol = mandoc_strdup(nn->string);
437: } else
438: m->meta.arch = mandoc_strdup(cp);
1.1 kristaps 439: }
440:
441: /* Ignore any subsequent parameters... */
1.46 kristaps 442: /* FIXME: warn about subsequent parameters. */
1.1 kristaps 443:
1.35 kristaps 444: return(post_prol(m, n));
1.1 kristaps 445: }
446:
447:
1.46 kristaps 448: /*
449: * Set the operating system by way of the `Os' macro. Note that if an
450: * argument isn't provided and -DOSNAME="\"foo\"" is provided during
451: * compilation, this value will be used instead of filling in "sysname
452: * release" from uname().
453: */
1.1 kristaps 454: static int
455: post_os(POST_ARGS)
456: {
1.46 kristaps 457: char buf[BUFSIZ];
458: #ifndef OSNAME
1.1 kristaps 459: struct utsname utsname;
1.42 kristaps 460: #endif
461:
1.1 kristaps 462: if (m->meta.os)
463: free(m->meta.os);
1.4 kristaps 464:
1.46 kristaps 465: if ( ! concat(m, buf, n->child, BUFSIZ))
1.2 kristaps 466: return(0);
1.1 kristaps 467:
1.59 kristaps 468: /* XXX: yes, these can all be dynamically-adjusted buffers, but
469: * it's really not worth the extra hackery.
470: */
471:
1.46 kristaps 472: if ('\0' == buf[0]) {
473: #ifdef OSNAME
1.59 kristaps 474: if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
475: mdoc_nmsg(m, n, MANDOCERR_MEM);
476: return(0);
477: }
1.46 kristaps 478: #else /*!OSNAME */
1.1 kristaps 479: if (-1 == uname(&utsname))
1.59 kristaps 480: return(mdoc_nmsg(m, n, MANDOCERR_UTSNAME));
481:
482: if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
483: mdoc_nmsg(m, n, MANDOCERR_MEM);
484: return(0);
485: }
486: if (strlcat(buf, " ", 64) >= BUFSIZ) {
487: mdoc_nmsg(m, n, MANDOCERR_MEM);
488: return(0);
489: }
490: if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
491: mdoc_nmsg(m, n, MANDOCERR_MEM);
492: return(0);
493: }
1.46 kristaps 494: #endif /*!OSNAME*/
1.1 kristaps 495: }
496:
1.46 kristaps 497: m->meta.os = mandoc_strdup(buf);
1.35 kristaps 498: return(post_prol(m, n));
1.1 kristaps 499: }
500:
501:
502: /*
503: * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
1.46 kristaps 504: * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
505: * argument has NOT been provided. See post_bl_width() for converting
506: * the -width string.
1.1 kristaps 507: */
508: static int
1.35 kristaps 509: post_bl_tagwidth(POST_ARGS)
1.1 kristaps 510: {
1.46 kristaps 511: struct mdoc_node *nn;
1.69 kristaps 512: size_t sz, ssz;
1.46 kristaps 513: int i;
514: char buf[NUMSIZ];
1.1 kristaps 515:
1.69 kristaps 516: sz = 10;
1.61 joerg 517:
518: for (nn = n->body->child; nn; nn = nn->next) {
1.69 kristaps 519: if (MDOC_It != nn->tok)
520: continue;
1.46 kristaps 521:
1.35 kristaps 522: assert(MDOC_BLOCK == nn->type);
523: nn = nn->head->child;
1.78 joerg 524:
525: if (nn == NULL) {
526: /* No -width for .Bl and first .It is emtpy */
527: if ( ! mdoc_nmsg(m, n, MANDOCERR_NOWIDTHARG))
528: return(0);
529: break;
530: }
1.69 kristaps 531:
532: if (MDOC_TEXT == nn->type) {
1.46 kristaps 533: sz = strlen(nn->string) + 1;
1.69 kristaps 534: break;
535: }
536:
537: if (0 != (ssz = mdoc_macro2len(nn->tok)))
538: sz = ssz;
539: else if ( ! mdoc_nmsg(m, n, MANDOCERR_NOWIDTHARG))
540: return(0);
541:
542: break;
1.1 kristaps 543: }
544:
1.69 kristaps 545: /* Defaults to ten ens. */
546:
1.46 kristaps 547: snprintf(buf, NUMSIZ, "%zun", sz);
1.1 kristaps 548:
549: /*
550: * We have to dynamically add this to the macro's argument list.
551: * We're guaranteed that a MDOC_Width doesn't already exist.
552: */
553:
1.70 kristaps 554: assert(n->args);
555: i = (int)(n->args->argc)++;
556:
557: n->args->argv = mandoc_realloc(n->args->argv,
558: n->args->argc * sizeof(struct mdoc_argv));
1.1 kristaps 559:
1.70 kristaps 560: n->args->argv[i].arg = MDOC_Width;
561: n->args->argv[i].line = n->line;
562: n->args->argv[i].pos = n->pos;
563: n->args->argv[i].sz = 1;
564: n->args->argv[i].value = mandoc_malloc(sizeof(char *));
565: n->args->argv[i].value[0] = mandoc_strdup(buf);
1.1 kristaps 566:
1.70 kristaps 567: /* Set our width! */
1.74 kristaps 568: n->data.Bl->width = n->args->argv[i].value[0];
1.1 kristaps 569: return(1);
570: }
571:
572:
1.46 kristaps 573: /*
574: * Calculate the real width of a list from the -width string, which may
575: * contain a macro (with a known default width), a literal string, or a
576: * scaling width.
577: */
1.1 kristaps 578: static int
1.70 kristaps 579: post_bl_width(POST_ARGS)
1.1 kristaps 580: {
581: size_t width;
1.70 kristaps 582: int i;
1.51 kristaps 583: enum mdoct tok;
1.46 kristaps 584: char buf[NUMSIZ];
1.1 kristaps 585:
586: /*
587: * If the value to -width is a macro, then we re-write it to be
588: * the macro's width as set in share/tmac/mdoc/doc-common.
589: */
590:
1.74 kristaps 591: if (0 == strcmp(n->data.Bl->width, "Ds"))
1.18 kristaps 592: width = 6;
1.74 kristaps 593: else if (MDOC_MAX == (tok = mdoc_hash_find(n->data.Bl->width)))
1.1 kristaps 594: return(1);
595: else if (0 == (width = mdoc_macro2len(tok)))
1.59 kristaps 596: return(mdoc_nmsg(m, n, MANDOCERR_BADWIDTH));
1.1 kristaps 597:
598: /* The value already exists: free and reallocate it. */
599:
1.70 kristaps 600: assert(n->args);
601:
602: for (i = 0; i < (int)n->args->argc; i++)
603: if (MDOC_Width == n->args->argv[i].arg)
604: break;
605:
606: assert(i < (int)n->args->argc);
607:
1.46 kristaps 608: snprintf(buf, NUMSIZ, "%zun", width);
1.70 kristaps 609: free(n->args->argv[i].value[0]);
610: n->args->argv[i].value[0] = mandoc_strdup(buf);
611:
612: /* Set our width! */
1.74 kristaps 613: n->data.Bl->width = n->args->argv[i].value[0];
1.1 kristaps 614: return(1);
615: }
616:
617:
1.46 kristaps 618: /*
619: * Do processing for -column lists, which can have two distinct styles
620: * of invocation. Merge this two styles into a consistent form.
621: */
1.35 kristaps 622: /* ARGSUSED */
1.1 kristaps 623: static int
1.14 kristaps 624: post_bl_head(POST_ARGS)
625: {
626: int i, c;
1.35 kristaps 627: struct mdoc_node *np, *nn, *nnp;
1.14 kristaps 628:
1.74 kristaps 629: if (LIST_column != n->data.Bl->type)
1.69 kristaps 630: return(1);
631: else if (NULL == n->child)
1.14 kristaps 632: return(1);
633:
1.35 kristaps 634: np = n->parent;
635: assert(np->args);
1.14 kristaps 636:
1.35 kristaps 637: for (c = 0; c < (int)np->args->argc; c++)
638: if (MDOC_Column == np->args->argv[c].arg)
1.14 kristaps 639: break;
640:
1.69 kristaps 641: assert(c < (int)np->args->argc);
1.35 kristaps 642: assert(0 == np->args->argv[c].sz);
1.14 kristaps 643:
644: /*
645: * Accomodate for new-style groff column syntax. Shuffle the
646: * child nodes, all of which must be TEXT, as arguments for the
647: * column field. Then, delete the head children.
648: */
649:
1.35 kristaps 650: np->args->argv[c].sz = (size_t)n->nchild;
1.46 kristaps 651: np->args->argv[c].value = mandoc_malloc
1.35 kristaps 652: ((size_t)n->nchild * sizeof(char *));
1.75 kristaps 653:
654: n->data.Bl->ncols = np->args->argv[c].sz;
655: n->data.Bl->cols = (const char **)np->args->argv[c].value;
1.14 kristaps 656:
1.35 kristaps 657: for (i = 0, nn = n->child; nn; i++) {
658: np->args->argv[c].value[i] = nn->string;
1.14 kristaps 659: nn->string = NULL;
660: nnp = nn;
661: nn = nn->next;
1.53 kristaps 662: mdoc_node_delete(NULL, nnp);
1.14 kristaps 663: }
664:
1.35 kristaps 665: n->nchild = 0;
666: n->child = NULL;
1.14 kristaps 667: return(1);
668: }
669:
670:
671: static int
1.1 kristaps 672: post_bl(POST_ARGS)
673: {
674:
1.35 kristaps 675: if (MDOC_HEAD == n->type)
676: return(post_bl_head(m, n));
677: if (MDOC_BLOCK != n->type)
1.1 kristaps 678: return(1);
679:
680: /*
681: * These are fairly complicated, so we've broken them into two
682: * functions. post_bl_tagwidth() is called when a -tag is
683: * specified, but no -width (it must be guessed). The second
684: * when a -width is specified (macro indicators must be
685: * rewritten into real lengths).
686: */
687:
1.74 kristaps 688: if (LIST_tag == n->data.Bl->type && NULL == n->data.Bl->width) {
1.35 kristaps 689: if ( ! post_bl_tagwidth(m, n))
1.1 kristaps 690: return(0);
1.74 kristaps 691: } else if (NULL != n->data.Bl->width) {
1.70 kristaps 692: if ( ! post_bl_width(m, n))
1.1 kristaps 693: return(0);
1.70 kristaps 694: } else
695: return(1);
696:
1.74 kristaps 697: assert(n->data.Bl->width);
1.1 kristaps 698: return(1);
699: }
700:
701:
1.46 kristaps 702: /*
703: * The `Pa' macro defaults to a tilde if no value is provided as an
704: * argument.
705: */
1.1 kristaps 706: static int
1.46 kristaps 707: post_pa(POST_ARGS)
1.10 kristaps 708: {
1.35 kristaps 709: struct mdoc_node *np;
1.10 kristaps 710:
1.35 kristaps 711: if (n->child)
1.10 kristaps 712: return(1);
713:
1.35 kristaps 714: np = n;
1.10 kristaps 715: m->next = MDOC_NEXT_CHILD;
1.35 kristaps 716: if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
1.1 kristaps 717: return(0);
1.35 kristaps 718: m->last = np;
1.1 kristaps 719: return(1);
720: }
721:
722:
1.46 kristaps 723: /*
1.49 kristaps 724: * Parse the date field in `Dd'.
1.46 kristaps 725: */
1.1 kristaps 726: static int
727: post_dd(POST_ARGS)
728: {
1.46 kristaps 729: char buf[DATESIZ];
1.77 kristaps 730:
731: if (NULL == n->child) {
732: m->meta.date = time(NULL);
733: return(post_prol(m, n));
734: }
1.1 kristaps 735:
1.46 kristaps 736: if ( ! concat(m, buf, n->child, DATESIZ))
1.2 kristaps 737: return(0);
1.1 kristaps 738:
1.49 kristaps 739: m->meta.date = mandoc_a2time
740: (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
741:
1.46 kristaps 742: if (0 == m->meta.date) {
1.59 kristaps 743: if ( ! mdoc_nmsg(m, n, MANDOCERR_BADDATE))
1.1 kristaps 744: return(0);
745: m->meta.date = time(NULL);
746: }
747:
1.35 kristaps 748: return(post_prol(m, n));
1.1 kristaps 749: }
750:
751:
1.46 kristaps 752: /*
753: * Remove prologue macros from the document after they're processed.
754: * The final document uses mdoc_meta for these values and discards the
755: * originals.
756: */
1.1 kristaps 757: static int
758: post_prol(POST_ARGS)
759: {
1.34 kristaps 760:
1.53 kristaps 761: mdoc_node_delete(m, n);
1.34 kristaps 762: if (m->meta.title && m->meta.date && m->meta.os)
763: m->flags |= MDOC_PBODY;
1.1 kristaps 764: return(1);
765: }
766:
767:
1.46 kristaps 768: /*
769: * Trigger a literal context.
770: */
1.1 kristaps 771: static int
772: pre_dl(PRE_ARGS)
773: {
774:
1.16 kristaps 775: if (MDOC_BODY == n->type)
776: m->flags |= MDOC_LITERAL;
1.65 kristaps 777: return(1);
1.38 kristaps 778: }
779:
780:
781: static int
1.1 kristaps 782: pre_bd(PRE_ARGS)
783: {
784:
785: if (MDOC_BODY != n->type)
786: return(1);
787:
1.74 kristaps 788: assert(n->data.Bd);
789: if (DISP_literal == n->data.Bd->type)
1.66 kristaps 790: m->flags |= MDOC_LITERAL;
1.74 kristaps 791: if (DISP_unfilled == n->data.Bd->type)
1.66 kristaps 792: m->flags |= MDOC_LITERAL;
1.1 kristaps 793:
794: return(1);
795: }
796:
797:
798: static int
799: post_display(POST_ARGS)
800: {
801:
1.35 kristaps 802: if (MDOC_BODY == n->type)
1.1 kristaps 803: m->flags &= ~MDOC_LITERAL;
1.39 kristaps 804: return(1);
805: }
CVSweb