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