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