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