Annotation of mandoc/mdoc_action.c, Revision 1.31
1.31 ! kristaps 1: /* $Id: mdoc_action.c,v 1.30 2009/07/17 10:56:57 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: */
17: #include <sys/utsname.h>
18:
19: #include <assert.h>
20: #include <errno.h>
21: #include <stdio.h>
22: #include <stdlib.h>
23: #include <string.h>
24:
25: #include "libmdoc.h"
26:
27: #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
28: #define POST_ARGS struct mdoc *m
29:
30: struct actions {
31: int (*pre)(PRE_ARGS);
32: int (*post)(POST_ARGS);
33: };
34:
35: static int post_ar(POST_ARGS);
1.25 kristaps 36: static int post_at(POST_ARGS);
1.1 kristaps 37: static int post_bl(POST_ARGS);
1.14 kristaps 38: static int post_bl_head(POST_ARGS);
1.25 kristaps 39: static int post_bl_tagwidth(POST_ARGS);
1.1 kristaps 40: static int post_bl_width(POST_ARGS);
41: static int post_dd(POST_ARGS);
42: static int post_display(POST_ARGS);
43: static int post_dt(POST_ARGS);
1.27 kristaps 44: static int post_lb(POST_ARGS);
1.1 kristaps 45: static int post_nm(POST_ARGS);
46: static int post_os(POST_ARGS);
47: static int post_prol(POST_ARGS);
48: static int post_sh(POST_ARGS);
1.26 kristaps 49: static int post_st(POST_ARGS);
1.1 kristaps 50: static int post_std(POST_ARGS);
1.31 ! kristaps 51: static int post_tilde(POST_ARGS);
1.1 kristaps 52:
53: static int pre_bd(PRE_ARGS);
54: static int pre_dl(PRE_ARGS);
55:
56: const struct actions mdoc_actions[MDOC_MAX] = {
1.12 kristaps 57: { NULL, NULL }, /* Ap */
1.1 kristaps 58: { NULL, post_dd }, /* Dd */
59: { NULL, post_dt }, /* Dt */
60: { NULL, post_os }, /* Os */
61: { NULL, post_sh }, /* Sh */
62: { NULL, NULL }, /* Ss */
63: { NULL, NULL }, /* Pp */
64: { NULL, NULL }, /* D1 */
65: { pre_dl, post_display }, /* Dl */
66: { pre_bd, post_display }, /* Bd */
67: { NULL, NULL }, /* Ed */
68: { NULL, post_bl }, /* Bl */
69: { NULL, NULL }, /* El */
70: { NULL, NULL }, /* It */
71: { NULL, NULL }, /* Ad */
72: { NULL, NULL }, /* An */
73: { NULL, post_ar }, /* Ar */
1.19 kristaps 74: { NULL, NULL }, /* Cd */ /* FIXME: tabs are accepted! */
1.1 kristaps 75: { NULL, NULL }, /* Cm */
76: { NULL, NULL }, /* Dv */
77: { NULL, NULL }, /* Er */
78: { NULL, NULL }, /* Ev */
79: { NULL, post_std }, /* Ex */
80: { NULL, NULL }, /* Fa */
81: { NULL, NULL }, /* Fd */
82: { NULL, NULL }, /* Fl */
83: { NULL, NULL }, /* Fn */
84: { NULL, NULL }, /* Ft */
85: { NULL, NULL }, /* Ic */
86: { NULL, NULL }, /* In */
87: { NULL, NULL }, /* Li */
88: { NULL, NULL }, /* Nd */
89: { NULL, post_nm }, /* Nm */
90: { NULL, NULL }, /* Op */
91: { NULL, NULL }, /* Ot */
1.31 ! kristaps 92: { NULL, post_tilde }, /* Pa */
1.1 kristaps 93: { NULL, post_std }, /* Rv */
1.26 kristaps 94: { NULL, post_st }, /* St */
1.1 kristaps 95: { NULL, NULL }, /* Va */
96: { NULL, NULL }, /* Vt */
97: { NULL, NULL }, /* Xr */
98: { NULL, NULL }, /* %A */
99: { NULL, NULL }, /* %B */
100: { NULL, NULL }, /* %D */
101: { NULL, NULL }, /* %I */
102: { NULL, NULL }, /* %J */
103: { NULL, NULL }, /* %N */
104: { NULL, NULL }, /* %O */
105: { NULL, NULL }, /* %P */
106: { NULL, NULL }, /* %R */
107: { NULL, NULL }, /* %T */
108: { NULL, NULL }, /* %V */
109: { NULL, NULL }, /* Ac */
110: { NULL, NULL }, /* Ao */
111: { NULL, NULL }, /* Aq */
1.25 kristaps 112: { NULL, post_at }, /* At */
1.1 kristaps 113: { NULL, NULL }, /* Bc */
114: { NULL, NULL }, /* Bf */
115: { NULL, NULL }, /* Bo */
116: { NULL, NULL }, /* Bq */
117: { NULL, NULL }, /* Bsx */
118: { NULL, NULL }, /* Bx */
119: { NULL, NULL }, /* Db */
120: { NULL, NULL }, /* Dc */
121: { NULL, NULL }, /* Do */
122: { NULL, NULL }, /* Dq */
123: { NULL, NULL }, /* Ec */
124: { NULL, NULL }, /* Ef */
125: { NULL, NULL }, /* Em */
126: { NULL, NULL }, /* Eo */
127: { NULL, NULL }, /* Fx */
128: { NULL, NULL }, /* Ms */
129: { NULL, NULL }, /* No */
130: { NULL, NULL }, /* Ns */
131: { NULL, NULL }, /* Nx */
132: { NULL, NULL }, /* Ox */
133: { NULL, NULL }, /* Pc */
134: { NULL, NULL }, /* Pf */
135: { NULL, NULL }, /* Po */
136: { NULL, NULL }, /* Pq */
137: { NULL, NULL }, /* Qc */
138: { NULL, NULL }, /* Ql */
139: { NULL, NULL }, /* Qo */
140: { NULL, NULL }, /* Qq */
141: { NULL, NULL }, /* Re */
142: { NULL, NULL }, /* Rs */
143: { NULL, NULL }, /* Sc */
144: { NULL, NULL }, /* So */
145: { NULL, NULL }, /* Sq */
146: { NULL, NULL }, /* Sm */
147: { NULL, NULL }, /* Sx */
148: { NULL, NULL }, /* Sy */
149: { NULL, NULL }, /* Tn */
150: { NULL, NULL }, /* Ux */
151: { NULL, NULL }, /* Xc */
152: { NULL, NULL }, /* Xo */
153: { NULL, NULL }, /* Fo */
154: { NULL, NULL }, /* Fc */
155: { NULL, NULL }, /* Oo */
156: { NULL, NULL }, /* Oc */
157: { NULL, NULL }, /* Bk */
158: { NULL, NULL }, /* Ek */
159: { NULL, NULL }, /* Bt */
160: { NULL, NULL }, /* Hf */
161: { NULL, NULL }, /* Fr */
162: { NULL, NULL }, /* Ud */
1.27 kristaps 163: { NULL, post_lb }, /* Lb */
1.1 kristaps 164: { NULL, NULL }, /* Lp */
1.31 ! kristaps 165: { NULL, post_tilde }, /* Lk */
1.1 kristaps 166: { NULL, NULL }, /* Mt */
167: { NULL, NULL }, /* Brq */
168: { NULL, NULL }, /* Bro */
169: { NULL, NULL }, /* Brc */
170: { NULL, NULL }, /* %C */
171: { NULL, NULL }, /* Es */
172: { NULL, NULL }, /* En */
173: { NULL, NULL }, /* Dx */
174: { NULL, NULL }, /* %Q */
1.30 kristaps 175: { NULL, NULL }, /* br */
176: { NULL, NULL }, /* sp */
1.1 kristaps 177: };
178:
1.25 kristaps 179: static int concat(struct mdoc *, const struct mdoc_node *,
180: char *, size_t);
1.1 kristaps 181:
1.2 kristaps 182: #ifdef __linux__
1.25 kristaps 183: extern size_t strlcat(char *, const char *, size_t);
1.2 kristaps 184: #endif
185:
186:
1.1 kristaps 187: int
188: mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
189: {
190:
191: switch (n->type) {
192: case (MDOC_ROOT):
1.2 kristaps 193: /* FALLTHROUGH */
1.1 kristaps 194: case (MDOC_TEXT):
1.2 kristaps 195: return(1);
196: default:
1.1 kristaps 197: break;
198: }
1.2 kristaps 199:
1.6 kristaps 200: if (NULL == mdoc_actions[n->tok].pre)
1.2 kristaps 201: return(1);
1.6 kristaps 202: return((*mdoc_actions[n->tok].pre)(m, n));
1.1 kristaps 203: }
204:
205:
206: int
207: mdoc_action_post(struct mdoc *m)
208: {
209:
210: if (MDOC_ACTED & m->last->flags)
211: return(1);
212: m->last->flags |= MDOC_ACTED;
213:
214: switch (m->last->type) {
215: case (MDOC_TEXT):
1.2 kristaps 216: /* FALLTHROUGH */
1.1 kristaps 217: case (MDOC_ROOT):
1.2 kristaps 218: return(1);
219: default:
1.1 kristaps 220: break;
221: }
1.2 kristaps 222:
223: if (NULL == mdoc_actions[m->last->tok].post)
224: return(1);
225: return((*mdoc_actions[m->last->tok].post)(m));
226: }
227:
228:
229: static int
230: concat(struct mdoc *m, const struct mdoc_node *n,
231: char *buf, size_t sz)
232: {
233:
234: for ( ; n; n = n->next) {
235: assert(MDOC_TEXT == n->type);
236: if (strlcat(buf, n->string, sz) >= sz)
1.23 kristaps 237: return(mdoc_nerr(m, n, ETOOLONG));
1.2 kristaps 238: if (NULL == n->next)
239: continue;
240: if (strlcat(buf, " ", sz) >= sz)
1.23 kristaps 241: return(mdoc_nerr(m, n, ETOOLONG));
1.2 kristaps 242: }
243:
1.1 kristaps 244: return(1);
245: }
246:
247:
248: static int
249: post_std(POST_ARGS)
250: {
251:
252: /*
253: * If '-std' is invoked without an argument, fill it in with our
254: * name (if it's been set).
255: */
256:
257: if (NULL == m->last->args)
258: return(1);
259: if (m->last->args->argv[0].sz)
260: return(1);
261:
262: assert(m->meta.name);
263:
264: m->last->args->argv[0].value = calloc(1, sizeof(char *));
265: if (NULL == m->last->args->argv[0].value)
1.23 kristaps 266: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 267:
268: m->last->args->argv[0].sz = 1;
1.2 kristaps 269: m->last->args->argv[0].value[0] = strdup(m->meta.name);
270: if (NULL == m->last->args->argv[0].value[0])
1.23 kristaps 271: return(mdoc_nerr(m, m->last, EMALLOC));
1.2 kristaps 272:
1.1 kristaps 273: return(1);
274: }
275:
276:
277: static int
278: post_nm(POST_ARGS)
279: {
280: char buf[64];
281:
282: if (m->meta.name)
283: return(1);
1.5 kristaps 284:
285: buf[0] = 0;
1.2 kristaps 286: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
287: return(0);
1.6 kristaps 288:
1.2 kristaps 289: if (NULL == (m->meta.name = strdup(buf)))
1.23 kristaps 290: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 291:
292: return(1);
293: }
294:
295:
296: static int
1.27 kristaps 297: post_lb(POST_ARGS)
298: {
299: const char *p;
300: char *buf;
301: size_t sz;
302:
303: assert(MDOC_TEXT == m->last->child->type);
304: p = mdoc_a2lib(m->last->child->string);
305: if (NULL == p) {
306: sz = strlen(m->last->child->string) +
307: 2 + strlen("\\(lqlibrary\\(rq");
308: buf = malloc(sz);
309: if (NULL == buf)
310: return(mdoc_nerr(m, m->last, EMALLOC));
311: (void)snprintf(buf, sz, "library \\(lq%s\\(rq",
312: m->last->child->string);
313: free(m->last->child->string);
314: m->last->child->string = buf;
315: return(1);
316: }
317:
318: free(m->last->child->string);
319: m->last->child->string = strdup(p);
320: if (NULL == m->last->child->string)
321: return(mdoc_nerr(m, m->last, EMALLOC));
322: return(1);
323: }
324:
325:
326: static int
1.26 kristaps 327: post_st(POST_ARGS)
328: {
329: const char *p;
330:
331: assert(MDOC_TEXT == m->last->child->type);
332: p = mdoc_a2st(m->last->child->string);
333: assert(p);
334: free(m->last->child->string);
335: m->last->child->string = strdup(p);
336: if (NULL == m->last->child->string)
337: return(mdoc_nerr(m, m->last, EMALLOC));
338: return(1);
339: }
340:
341:
342: static int
1.25 kristaps 343: post_at(POST_ARGS)
344: {
345: struct mdoc_node *n;
346: const char *p;
347:
348: if (m->last->child) {
349: assert(MDOC_TEXT == m->last->child->type);
350: p = mdoc_a2att(m->last->child->string);
351: assert(p);
352: free(m->last->child->string);
353: m->last->child->string = strdup(p);
354: if (NULL == m->last->child->string)
355: return(mdoc_nerr(m, m->last, EMALLOC));
356: return(1);
357: }
358:
359: n = m->last;
360: m->next = MDOC_NEXT_CHILD;
361:
362: if ( ! mdoc_word_alloc(m, n->line, n->pos, "AT&T UNIX"))
363: return(0);
364:
365: m->last = n;
366: m->next = MDOC_NEXT_SIBLING;
367: return(1);
368: }
369:
370:
371: static int
1.1 kristaps 372: post_sh(POST_ARGS)
373: {
374: enum mdoc_sec sec;
375: char buf[64];
376:
377: /*
378: * We keep track of the current section /and/ the "named"
379: * section, which is one of the conventional ones, in order to
380: * check ordering.
381: */
382:
383: if (MDOC_HEAD != m->last->type)
384: return(1);
1.5 kristaps 385:
386: buf[0] = 0;
1.2 kristaps 387: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
388: return(0);
1.1 kristaps 389: if (SEC_CUSTOM != (sec = mdoc_atosec(buf)))
390: m->lastnamed = sec;
391:
392: switch ((m->lastsec = sec)) {
393: case (SEC_RETURN_VALUES):
394: /* FALLTHROUGH */
395: case (SEC_ERRORS):
396: switch (m->meta.msec) {
397: case (2):
398: /* FALLTHROUGH */
399: case (3):
400: /* FALLTHROUGH */
401: case (9):
402: break;
403: default:
1.23 kristaps 404: return(mdoc_nwarn(m, m->last, EBADSEC));
1.1 kristaps 405: }
406: break;
407: default:
408: break;
409: }
410: return(1);
411: }
412:
413:
414: static int
415: post_dt(POST_ARGS)
416: {
417: struct mdoc_node *n;
418: const char *cp;
419: char *ep;
420: long lval;
421:
422: if (m->meta.title)
423: free(m->meta.title);
424: if (m->meta.vol)
425: free(m->meta.vol);
426: if (m->meta.arch)
427: free(m->meta.arch);
428:
429: m->meta.title = m->meta.vol = m->meta.arch = NULL;
430: m->meta.msec = 0;
431:
432: /* Handles: `.Dt'
433: * --> title = unknown, volume = local, msec = 0, arch = NULL
434: */
435:
436: if (NULL == (n = m->last->child)) {
1.2 kristaps 437: if (NULL == (m->meta.title = strdup("unknown")))
1.23 kristaps 438: return(mdoc_nerr(m, m->last, EMALLOC));
1.2 kristaps 439: if (NULL == (m->meta.vol = strdup("local")))
1.23 kristaps 440: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 441: return(post_prol(m));
442: }
443:
444: /* Handles: `.Dt TITLE'
445: * --> title = TITLE, volume = local, msec = 0, arch = NULL
446: */
447:
1.2 kristaps 448: if (NULL == (m->meta.title = strdup(n->string)))
1.23 kristaps 449: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 450:
451: if (NULL == (n = n->next)) {
1.2 kristaps 452: if (NULL == (m->meta.vol = strdup("local")))
1.23 kristaps 453: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 454: return(post_prol(m));
455: }
456:
457: /* Handles: `.Dt TITLE SEC'
458: * --> title = TITLE, volume = SEC is msec ?
459: * format(msec) : SEC,
460: * msec = SEC is msec ? atoi(msec) : 0,
461: * arch = NULL
462: */
463:
464: cp = mdoc_a2msec(n->string);
465: if (cp) {
1.2 kristaps 466: if (NULL == (m->meta.vol = strdup(cp)))
1.23 kristaps 467: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 468: errno = 0;
469: lval = strtol(n->string, &ep, 10);
470: if (n->string[0] != '\0' && *ep == '\0')
471: m->meta.msec = (int)lval;
1.2 kristaps 472: } else if (NULL == (m->meta.vol = strdup(n->string)))
1.23 kristaps 473: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 474:
475: if (NULL == (n = n->next))
476: return(post_prol(m));
477:
478: /* Handles: `.Dt TITLE SEC VOL'
479: * --> title = TITLE, volume = VOL is vol ?
480: * format(VOL) :
481: * VOL is arch ? format(arch) :
482: * VOL
483: */
484:
485: cp = mdoc_a2vol(n->string);
486: if (cp) {
487: free(m->meta.vol);
1.2 kristaps 488: if (NULL == (m->meta.vol = strdup(cp)))
1.23 kristaps 489: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 490: n = n->next;
491: } else {
492: cp = mdoc_a2arch(n->string);
493: if (NULL == cp) {
494: free(m->meta.vol);
1.2 kristaps 495: if (NULL == (m->meta.vol = strdup(n->string)))
1.23 kristaps 496: return(mdoc_nerr(m, m->last, EMALLOC));
1.2 kristaps 497: } else if (NULL == (m->meta.arch = strdup(cp)))
1.23 kristaps 498: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 499: }
500:
501: /* Ignore any subsequent parameters... */
502:
503: return(post_prol(m));
504: }
505:
506:
507: static int
508: post_os(POST_ARGS)
509: {
510: char buf[64];
511: struct utsname utsname;
512:
513: if (m->meta.os)
514: free(m->meta.os);
1.4 kristaps 515:
516: buf[0] = 0;
1.2 kristaps 517: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
518: return(0);
1.1 kristaps 519:
520: if (0 == buf[0]) {
521: if (-1 == uname(&utsname))
1.23 kristaps 522: return(mdoc_nerr(m, m->last, EUTSNAME));
1.4 kristaps 523: if (strlcat(buf, utsname.sysname, 64) >= 64)
1.23 kristaps 524: return(mdoc_nerr(m, m->last, ETOOLONG));
1.2 kristaps 525: if (strlcat(buf, " ", 64) >= 64)
1.23 kristaps 526: return(mdoc_nerr(m, m->last, ETOOLONG));
1.2 kristaps 527: if (strlcat(buf, utsname.release, 64) >= 64)
1.23 kristaps 528: return(mdoc_nerr(m, m->last, ETOOLONG));
1.1 kristaps 529: }
530:
1.2 kristaps 531: if (NULL == (m->meta.os = strdup(buf)))
1.23 kristaps 532: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 533:
1.13 kristaps 534: m->flags |= MDOC_PBODY;
1.1 kristaps 535: return(post_prol(m));
536: }
537:
538:
539: /*
540: * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
541: * Uses the first head macro.
542: */
543: static int
544: post_bl_tagwidth(struct mdoc *m)
545: {
546: struct mdoc_node *n;
547: int sz;
548: char buf[32];
549:
550: /*
551: * Use the text width, if a text node, or the default macro
552: * width if a macro.
553: */
554:
1.11 kristaps 555: n = m->last->body->child;
556: if (n) {
1.1 kristaps 557: assert(MDOC_BLOCK == n->type);
558: assert(MDOC_It == n->tok);
559: n = n->head->child;
560: }
561:
562: sz = 10; /* Default size. */
563:
564: if (n) {
565: if (MDOC_TEXT != n->type) {
566: if (0 == (sz = (int)mdoc_macro2len(n->tok)))
1.23 kristaps 567: if ( ! mdoc_nwarn(m, m->last, ENOWIDTH))
1.1 kristaps 568: return(0);
569: } else
570: sz = (int)strlen(n->string) + 1;
571: }
572:
1.2 kristaps 573: if (-1 == snprintf(buf, sizeof(buf), "%dn", sz))
1.23 kristaps 574: return(mdoc_nerr(m, m->last, ENUMFMT));
1.1 kristaps 575:
576: /*
577: * We have to dynamically add this to the macro's argument list.
578: * We're guaranteed that a MDOC_Width doesn't already exist.
579: */
580:
581: n = m->last;
582: assert(n->args);
1.2 kristaps 583: sz = (int)(n->args->argc)++;
1.1 kristaps 584:
1.2 kristaps 585: n->args->argv = realloc(n->args->argv,
1.1 kristaps 586: n->args->argc * sizeof(struct mdoc_argv));
587:
1.2 kristaps 588: if (NULL == n->args->argv)
1.23 kristaps 589: return(mdoc_nerr(m, m->last, EMALLOC));
1.2 kristaps 590:
1.1 kristaps 591: n->args->argv[sz].arg = MDOC_Width;
592: n->args->argv[sz].line = m->last->line;
593: n->args->argv[sz].pos = m->last->pos;
594: n->args->argv[sz].sz = 1;
595: n->args->argv[sz].value = calloc(1, sizeof(char *));
1.2 kristaps 596:
1.1 kristaps 597: if (NULL == n->args->argv[sz].value)
1.23 kristaps 598: return(mdoc_nerr(m, m->last, EMALLOC));
1.2 kristaps 599: if (NULL == (n->args->argv[sz].value[0] = strdup(buf)))
1.23 kristaps 600: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 601:
602: return(1);
603: }
604:
605:
606: static int
607: post_bl_width(struct mdoc *m)
608: {
609: size_t width;
610: int i, tok;
611: char buf[32];
612: char *p;
613:
614: if (NULL == m->last->args)
615: return(1);
616:
617: for (i = 0; i < (int)m->last->args->argc; i++)
618: if (MDOC_Width == m->last->args->argv[i].arg)
619: break;
620:
621: if (i == (int)m->last->args->argc)
622: return(1);
623: p = m->last->args->argv[i].value[0];
624:
625: /*
626: * If the value to -width is a macro, then we re-write it to be
627: * the macro's width as set in share/tmac/mdoc/doc-common.
628: */
629:
1.2 kristaps 630: if (0 == strcmp(p, "Ds"))
1.18 kristaps 631: width = 6;
1.3 kristaps 632: else if (MDOC_MAX == (tok = mdoc_hash_find(m->htab, p)))
1.1 kristaps 633: return(1);
634: else if (0 == (width = mdoc_macro2len(tok)))
1.23 kristaps 635: return(mdoc_nwarn(m, m->last, ENOWIDTH));
1.1 kristaps 636:
637: /* The value already exists: free and reallocate it. */
638:
1.2 kristaps 639: if (-1 == snprintf(buf, sizeof(buf), "%zun", width))
1.23 kristaps 640: return(mdoc_nerr(m, m->last, ENUMFMT));
1.1 kristaps 641:
642: free(m->last->args->argv[i].value[0]);
1.2 kristaps 643: m->last->args->argv[i].value[0] = strdup(buf);
644: if (NULL == m->last->args->argv[i].value[0])
1.23 kristaps 645: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 646:
647: return(1);
648: }
649:
650:
651: static int
1.14 kristaps 652: post_bl_head(POST_ARGS)
653: {
654: int i, c;
655: struct mdoc_node *n, *nn, *nnp;
656:
657: if (NULL == m->last->child)
658: return(1);
659:
660: n = m->last->parent;
661: assert(n->args);
662:
663: for (c = 0; c < (int)n->args->argc; c++)
664: if (MDOC_Column == n->args->argv[c].arg)
665: break;
666:
667: /* Only process -column. */
668:
669: if (c == (int)n->args->argc)
670: return(1);
671:
672: assert(0 == n->args->argv[c].sz);
673:
674: /*
675: * Accomodate for new-style groff column syntax. Shuffle the
676: * child nodes, all of which must be TEXT, as arguments for the
677: * column field. Then, delete the head children.
678: */
679:
1.17 kristaps 680: n->args->argv[c].sz = (size_t)m->last->nchild;
681: n->args->argv[c].value = malloc
682: ((size_t)m->last->nchild * sizeof(char *));
1.14 kristaps 683:
684: for (i = 0, nn = m->last->child; nn; i++) {
685: n->args->argv[c].value[i] = nn->string;
686: nn->string = NULL;
687: nnp = nn;
688: nn = nn->next;
689: mdoc_node_free(nnp);
690: }
691:
1.17 kristaps 692: m->last->nchild = 0;
1.14 kristaps 693: m->last->child = NULL;
1.17 kristaps 694:
1.14 kristaps 695: return(1);
696: }
697:
698:
699: static int
1.1 kristaps 700: post_bl(POST_ARGS)
701: {
702: int i, r, len;
703:
1.14 kristaps 704: if (MDOC_HEAD == m->last->type)
705: return(post_bl_head(m));
1.1 kristaps 706: if (MDOC_BLOCK != m->last->type)
707: return(1);
708:
709: /*
710: * These are fairly complicated, so we've broken them into two
711: * functions. post_bl_tagwidth() is called when a -tag is
712: * specified, but no -width (it must be guessed). The second
713: * when a -width is specified (macro indicators must be
714: * rewritten into real lengths).
715: */
716:
717: len = (int)(m->last->args ? m->last->args->argc : 0);
718:
719: for (r = i = 0; i < len; i++) {
720: if (MDOC_Tag == m->last->args->argv[i].arg)
721: r |= 1 << 0;
722: if (MDOC_Width == m->last->args->argv[i].arg)
723: r |= 1 << 1;
724: }
725:
726: if (r & (1 << 0) && ! (r & (1 << 1))) {
727: if ( ! post_bl_tagwidth(m))
728: return(0);
729: } else if (r & (1 << 1))
730: if ( ! post_bl_width(m))
731: return(0);
732:
733: return(1);
734: }
735:
736:
737: static int
1.31 ! kristaps 738: post_tilde(POST_ARGS)
1.10 kristaps 739: {
740: struct mdoc_node *n;
741:
742: if (m->last->child)
743: return(1);
744:
745: n = m->last;
746: m->next = MDOC_NEXT_CHILD;
1.24 kristaps 747:
1.31 ! kristaps 748: /* XXX: not documented for `Lk'. */
1.25 kristaps 749: if ( ! mdoc_word_alloc(m, m->last->line, m->last->pos, "~"))
1.10 kristaps 750: return(0);
751:
752: m->last = n;
753: m->next = MDOC_NEXT_SIBLING;
754: return(1);
755: }
756:
757:
758: static int
1.1 kristaps 759: post_ar(POST_ARGS)
760: {
761: struct mdoc_node *n;
762:
763: if (m->last->child)
764: return(1);
765:
766: n = m->last;
767: m->next = MDOC_NEXT_CHILD;
1.25 kristaps 768: if ( ! mdoc_word_alloc(m, m->last->line, m->last->pos, "file"))
1.1 kristaps 769: return(0);
770: m->next = MDOC_NEXT_SIBLING;
1.25 kristaps 771: if ( ! mdoc_word_alloc(m, m->last->line, m->last->pos, "..."))
1.1 kristaps 772: return(0);
773:
774: m->last = n;
775: m->next = MDOC_NEXT_SIBLING;
776: return(1);
777: }
778:
779:
780: static int
781: post_dd(POST_ARGS)
782: {
783: char buf[64];
784:
1.5 kristaps 785: buf[0] = 0;
1.2 kristaps 786: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
787: return(0);
1.1 kristaps 788:
789: if (0 == (m->meta.date = mdoc_atotime(buf))) {
1.23 kristaps 790: if ( ! mdoc_nwarn(m, m->last, EBADDATE))
1.1 kristaps 791: return(0);
792: m->meta.date = time(NULL);
793: }
794:
795: return(post_prol(m));
796: }
797:
798:
799: static int
800: post_prol(POST_ARGS)
801: {
802: struct mdoc_node *n;
803:
804: /*
805: * The end document shouldn't have the prologue macros as part
806: * of the syntax tree (they encompass only meta-data).
807: */
808:
809: if (m->last->parent->child == m->last)
810: m->last->parent->child = m->last->prev;
811: if (m->last->prev)
812: m->last->prev->next = NULL;
813:
814: n = m->last;
815: assert(NULL == m->last->next);
816:
817: if (m->last->prev) {
818: m->last = m->last->prev;
819: m->next = MDOC_NEXT_SIBLING;
820: } else {
821: m->last = m->last->parent;
822: m->next = MDOC_NEXT_CHILD;
823: }
824:
825: mdoc_node_freelist(n);
826: return(1);
827: }
828:
829:
830: static int
831: pre_dl(PRE_ARGS)
832: {
833:
1.16 kristaps 834: if (MDOC_BODY == n->type)
835: m->flags |= MDOC_LITERAL;
1.1 kristaps 836: return(1);
837: }
838:
839:
840: static int
841: pre_bd(PRE_ARGS)
842: {
843: int i;
844:
845: if (MDOC_BODY != n->type)
846: return(1);
847:
1.2 kristaps 848: /* Enter literal context if `Bd -literal' or * -unfilled'. */
1.1 kristaps 849:
1.2 kristaps 850: for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
1.1 kristaps 851: if (MDOC_Literal == n->args->argv[i].arg)
852: break;
853: else if (MDOC_Unfilled == n->args->argv[i].arg)
854: break;
855:
856: if (i < (int)n->args->argc)
857: m->flags |= MDOC_LITERAL;
858:
859: return(1);
860: }
861:
862:
863: static int
864: post_display(POST_ARGS)
865: {
866:
867: if (MDOC_BODY == m->last->type)
868: m->flags &= ~MDOC_LITERAL;
869: return(1);
870: }
871:
872:
CVSweb