Annotation of mandoc/mdoc_action.c, Revision 1.29
1.29 ! kristaps 1: /* $Id: mdoc_action.c,v 1.28 2009/07/15 15:53: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.10 kristaps 45: static int post_lk(POST_ARGS);
1.1 kristaps 46: static int post_nm(POST_ARGS);
47: static int post_os(POST_ARGS);
48: static int post_prol(POST_ARGS);
49: static int post_sh(POST_ARGS);
1.26 kristaps 50: static int post_st(POST_ARGS);
1.1 kristaps 51: static int post_std(POST_ARGS);
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 */
92: { NULL, NULL }, /* Pa */
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.10 kristaps 165: { NULL, post_lk }, /* 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 */
175: };
176:
1.25 kristaps 177: static int concat(struct mdoc *, const struct mdoc_node *,
178: char *, size_t);
1.1 kristaps 179:
1.2 kristaps 180: #ifdef __linux__
1.25 kristaps 181: extern size_t strlcat(char *, const char *, size_t);
1.2 kristaps 182: #endif
183:
184:
1.1 kristaps 185: int
186: mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
187: {
188:
189: switch (n->type) {
190: case (MDOC_ROOT):
1.2 kristaps 191: /* FALLTHROUGH */
1.1 kristaps 192: case (MDOC_TEXT):
1.2 kristaps 193: return(1);
194: default:
1.1 kristaps 195: break;
196: }
1.2 kristaps 197:
1.6 kristaps 198: if (NULL == mdoc_actions[n->tok].pre)
1.2 kristaps 199: return(1);
1.6 kristaps 200: return((*mdoc_actions[n->tok].pre)(m, n));
1.1 kristaps 201: }
202:
203:
204: int
205: mdoc_action_post(struct mdoc *m)
206: {
207:
208: if (MDOC_ACTED & m->last->flags)
209: return(1);
210: m->last->flags |= MDOC_ACTED;
211:
212: switch (m->last->type) {
213: case (MDOC_TEXT):
1.2 kristaps 214: /* FALLTHROUGH */
1.1 kristaps 215: case (MDOC_ROOT):
1.2 kristaps 216: return(1);
217: default:
1.1 kristaps 218: break;
219: }
1.2 kristaps 220:
221: if (NULL == mdoc_actions[m->last->tok].post)
222: return(1);
223: return((*mdoc_actions[m->last->tok].post)(m));
224: }
225:
226:
227: static int
228: concat(struct mdoc *m, const struct mdoc_node *n,
229: char *buf, size_t sz)
230: {
231:
232: for ( ; n; n = n->next) {
233: assert(MDOC_TEXT == n->type);
234: if (strlcat(buf, n->string, sz) >= sz)
1.23 kristaps 235: return(mdoc_nerr(m, n, ETOOLONG));
1.2 kristaps 236: if (NULL == n->next)
237: continue;
238: if (strlcat(buf, " ", sz) >= sz)
1.23 kristaps 239: return(mdoc_nerr(m, n, ETOOLONG));
1.2 kristaps 240: }
241:
1.1 kristaps 242: return(1);
243: }
244:
245:
246: static int
247: post_std(POST_ARGS)
248: {
249:
250: /*
251: * If '-std' is invoked without an argument, fill it in with our
252: * name (if it's been set).
253: */
254:
255: if (NULL == m->last->args)
256: return(1);
257: if (m->last->args->argv[0].sz)
258: return(1);
259:
260: assert(m->meta.name);
261:
262: m->last->args->argv[0].value = calloc(1, sizeof(char *));
263: if (NULL == m->last->args->argv[0].value)
1.23 kristaps 264: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 265:
266: m->last->args->argv[0].sz = 1;
1.2 kristaps 267: m->last->args->argv[0].value[0] = strdup(m->meta.name);
268: if (NULL == m->last->args->argv[0].value[0])
1.23 kristaps 269: return(mdoc_nerr(m, m->last, EMALLOC));
1.2 kristaps 270:
1.1 kristaps 271: return(1);
272: }
273:
274:
275: static int
276: post_nm(POST_ARGS)
277: {
278: char buf[64];
279:
280: if (m->meta.name)
281: return(1);
1.5 kristaps 282:
283: buf[0] = 0;
1.2 kristaps 284: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
285: return(0);
1.6 kristaps 286:
1.2 kristaps 287: if (NULL == (m->meta.name = strdup(buf)))
1.23 kristaps 288: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 289:
290: return(1);
291: }
292:
293:
294: static int
1.27 kristaps 295: post_lb(POST_ARGS)
296: {
297: const char *p;
298: char *buf;
299: size_t sz;
300:
301: assert(MDOC_TEXT == m->last->child->type);
302: p = mdoc_a2lib(m->last->child->string);
303: if (NULL == p) {
304: sz = strlen(m->last->child->string) +
305: 2 + strlen("\\(lqlibrary\\(rq");
306: buf = malloc(sz);
307: if (NULL == buf)
308: return(mdoc_nerr(m, m->last, EMALLOC));
309: (void)snprintf(buf, sz, "library \\(lq%s\\(rq",
310: m->last->child->string);
311: free(m->last->child->string);
312: m->last->child->string = buf;
313: return(1);
314: }
315:
316: free(m->last->child->string);
317: m->last->child->string = strdup(p);
318: if (NULL == m->last->child->string)
319: return(mdoc_nerr(m, m->last, EMALLOC));
320: return(1);
321: }
322:
323:
324: static int
1.26 kristaps 325: post_st(POST_ARGS)
326: {
327: const char *p;
328:
329: assert(MDOC_TEXT == m->last->child->type);
330: p = mdoc_a2st(m->last->child->string);
331: assert(p);
332: free(m->last->child->string);
333: m->last->child->string = strdup(p);
334: if (NULL == m->last->child->string)
335: return(mdoc_nerr(m, m->last, EMALLOC));
336: return(1);
337: }
338:
339:
340: static int
1.25 kristaps 341: post_at(POST_ARGS)
342: {
343: struct mdoc_node *n;
344: const char *p;
345:
346: if (m->last->child) {
347: assert(MDOC_TEXT == m->last->child->type);
348: p = mdoc_a2att(m->last->child->string);
349: assert(p);
350: free(m->last->child->string);
351: m->last->child->string = strdup(p);
352: if (NULL == m->last->child->string)
353: return(mdoc_nerr(m, m->last, EMALLOC));
354: return(1);
355: }
356:
357: n = m->last;
358: m->next = MDOC_NEXT_CHILD;
359:
360: if ( ! mdoc_word_alloc(m, n->line, n->pos, "AT&T UNIX"))
361: return(0);
362:
363: m->last = n;
364: m->next = MDOC_NEXT_SIBLING;
365: return(1);
366: }
367:
368:
369: static int
1.1 kristaps 370: post_sh(POST_ARGS)
371: {
372: enum mdoc_sec sec;
373: char buf[64];
374:
375: /*
376: * We keep track of the current section /and/ the "named"
377: * section, which is one of the conventional ones, in order to
378: * check ordering.
379: */
380:
381: if (MDOC_HEAD != m->last->type)
382: return(1);
1.5 kristaps 383:
384: buf[0] = 0;
1.2 kristaps 385: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
386: return(0);
1.1 kristaps 387: if (SEC_CUSTOM != (sec = mdoc_atosec(buf)))
388: m->lastnamed = sec;
389:
390: switch ((m->lastsec = sec)) {
391: case (SEC_RETURN_VALUES):
392: /* FALLTHROUGH */
393: case (SEC_ERRORS):
394: switch (m->meta.msec) {
395: case (2):
396: /* FALLTHROUGH */
397: case (3):
398: /* FALLTHROUGH */
399: case (9):
400: break;
401: default:
1.23 kristaps 402: return(mdoc_nwarn(m, m->last, EBADSEC));
1.1 kristaps 403: }
404: break;
405: default:
406: break;
407: }
408: return(1);
409: }
410:
411:
412: static int
413: post_dt(POST_ARGS)
414: {
415: struct mdoc_node *n;
416: const char *cp;
417: char *ep;
418: long lval;
419:
420: if (m->meta.title)
421: free(m->meta.title);
422: if (m->meta.vol)
423: free(m->meta.vol);
424: if (m->meta.arch)
425: free(m->meta.arch);
426:
427: m->meta.title = m->meta.vol = m->meta.arch = NULL;
428: m->meta.msec = 0;
429:
430: /* Handles: `.Dt'
431: * --> title = unknown, volume = local, msec = 0, arch = NULL
432: */
433:
434: if (NULL == (n = m->last->child)) {
1.2 kristaps 435: if (NULL == (m->meta.title = strdup("unknown")))
1.23 kristaps 436: return(mdoc_nerr(m, m->last, EMALLOC));
1.2 kristaps 437: if (NULL == (m->meta.vol = strdup("local")))
1.23 kristaps 438: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 439: return(post_prol(m));
440: }
441:
442: /* Handles: `.Dt TITLE'
443: * --> title = TITLE, volume = local, msec = 0, arch = NULL
444: */
445:
1.2 kristaps 446: if (NULL == (m->meta.title = strdup(n->string)))
1.23 kristaps 447: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 448:
449: if (NULL == (n = n->next)) {
1.2 kristaps 450: if (NULL == (m->meta.vol = strdup("local")))
1.23 kristaps 451: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 452: return(post_prol(m));
453: }
454:
455: /* Handles: `.Dt TITLE SEC'
456: * --> title = TITLE, volume = SEC is msec ?
457: * format(msec) : SEC,
458: * msec = SEC is msec ? atoi(msec) : 0,
459: * arch = NULL
460: */
461:
462: cp = mdoc_a2msec(n->string);
463: if (cp) {
1.2 kristaps 464: if (NULL == (m->meta.vol = strdup(cp)))
1.23 kristaps 465: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 466: errno = 0;
467: lval = strtol(n->string, &ep, 10);
468: if (n->string[0] != '\0' && *ep == '\0')
469: m->meta.msec = (int)lval;
1.2 kristaps 470: } else if (NULL == (m->meta.vol = strdup(n->string)))
1.23 kristaps 471: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 472:
473: if (NULL == (n = n->next))
474: return(post_prol(m));
475:
476: /* Handles: `.Dt TITLE SEC VOL'
477: * --> title = TITLE, volume = VOL is vol ?
478: * format(VOL) :
479: * VOL is arch ? format(arch) :
480: * VOL
481: */
482:
483: cp = mdoc_a2vol(n->string);
484: if (cp) {
485: free(m->meta.vol);
1.2 kristaps 486: if (NULL == (m->meta.vol = strdup(cp)))
1.23 kristaps 487: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 488: n = n->next;
489: } else {
490: cp = mdoc_a2arch(n->string);
491: if (NULL == cp) {
492: free(m->meta.vol);
1.2 kristaps 493: if (NULL == (m->meta.vol = strdup(n->string)))
1.23 kristaps 494: return(mdoc_nerr(m, m->last, EMALLOC));
1.2 kristaps 495: } else if (NULL == (m->meta.arch = strdup(cp)))
1.23 kristaps 496: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 497: }
498:
499: /* Ignore any subsequent parameters... */
500:
501: return(post_prol(m));
502: }
503:
504:
505: static int
506: post_os(POST_ARGS)
507: {
508: char buf[64];
509: struct utsname utsname;
510:
511: if (m->meta.os)
512: free(m->meta.os);
1.4 kristaps 513:
514: buf[0] = 0;
1.2 kristaps 515: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
516: return(0);
1.1 kristaps 517:
518: if (0 == buf[0]) {
519: if (-1 == uname(&utsname))
1.23 kristaps 520: return(mdoc_nerr(m, m->last, EUTSNAME));
1.4 kristaps 521: if (strlcat(buf, utsname.sysname, 64) >= 64)
1.23 kristaps 522: return(mdoc_nerr(m, m->last, ETOOLONG));
1.2 kristaps 523: if (strlcat(buf, " ", 64) >= 64)
1.23 kristaps 524: return(mdoc_nerr(m, m->last, ETOOLONG));
1.2 kristaps 525: if (strlcat(buf, utsname.release, 64) >= 64)
1.23 kristaps 526: return(mdoc_nerr(m, m->last, ETOOLONG));
1.1 kristaps 527: }
528:
1.2 kristaps 529: if (NULL == (m->meta.os = strdup(buf)))
1.23 kristaps 530: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 531:
1.13 kristaps 532: m->flags |= MDOC_PBODY;
1.1 kristaps 533: return(post_prol(m));
534: }
535:
536:
537: /*
538: * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
539: * Uses the first head macro.
540: */
541: static int
542: post_bl_tagwidth(struct mdoc *m)
543: {
544: struct mdoc_node *n;
545: int sz;
546: char buf[32];
547:
548: /*
549: * Use the text width, if a text node, or the default macro
550: * width if a macro.
551: */
552:
1.11 kristaps 553: n = m->last->body->child;
554: if (n) {
1.1 kristaps 555: assert(MDOC_BLOCK == n->type);
556: assert(MDOC_It == n->tok);
557: n = n->head->child;
558: }
559:
560: sz = 10; /* Default size. */
561:
562: if (n) {
563: if (MDOC_TEXT != n->type) {
564: if (0 == (sz = (int)mdoc_macro2len(n->tok)))
1.23 kristaps 565: if ( ! mdoc_nwarn(m, m->last, ENOWIDTH))
1.1 kristaps 566: return(0);
567: } else
568: sz = (int)strlen(n->string) + 1;
569: }
570:
1.2 kristaps 571: if (-1 == snprintf(buf, sizeof(buf), "%dn", sz))
1.23 kristaps 572: return(mdoc_nerr(m, m->last, ENUMFMT));
1.1 kristaps 573:
574: /*
575: * We have to dynamically add this to the macro's argument list.
576: * We're guaranteed that a MDOC_Width doesn't already exist.
577: */
578:
579: n = m->last;
580: assert(n->args);
1.2 kristaps 581: sz = (int)(n->args->argc)++;
1.1 kristaps 582:
1.2 kristaps 583: n->args->argv = realloc(n->args->argv,
1.1 kristaps 584: n->args->argc * sizeof(struct mdoc_argv));
585:
1.2 kristaps 586: if (NULL == n->args->argv)
1.23 kristaps 587: return(mdoc_nerr(m, m->last, EMALLOC));
1.2 kristaps 588:
1.1 kristaps 589: n->args->argv[sz].arg = MDOC_Width;
590: n->args->argv[sz].line = m->last->line;
591: n->args->argv[sz].pos = m->last->pos;
592: n->args->argv[sz].sz = 1;
593: n->args->argv[sz].value = calloc(1, sizeof(char *));
1.2 kristaps 594:
1.1 kristaps 595: if (NULL == n->args->argv[sz].value)
1.23 kristaps 596: return(mdoc_nerr(m, m->last, EMALLOC));
1.2 kristaps 597: if (NULL == (n->args->argv[sz].value[0] = strdup(buf)))
1.23 kristaps 598: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 599:
600: return(1);
601: }
602:
603:
604: static int
605: post_bl_width(struct mdoc *m)
606: {
607: size_t width;
608: int i, tok;
609: char buf[32];
610: char *p;
611:
612: if (NULL == m->last->args)
613: return(1);
614:
615: for (i = 0; i < (int)m->last->args->argc; i++)
616: if (MDOC_Width == m->last->args->argv[i].arg)
617: break;
618:
619: if (i == (int)m->last->args->argc)
620: return(1);
621: p = m->last->args->argv[i].value[0];
622:
623: /*
624: * If the value to -width is a macro, then we re-write it to be
625: * the macro's width as set in share/tmac/mdoc/doc-common.
626: */
627:
1.2 kristaps 628: if (0 == strcmp(p, "Ds"))
1.18 kristaps 629: width = 6;
1.3 kristaps 630: else if (MDOC_MAX == (tok = mdoc_hash_find(m->htab, p)))
1.1 kristaps 631: return(1);
632: else if (0 == (width = mdoc_macro2len(tok)))
1.23 kristaps 633: return(mdoc_nwarn(m, m->last, ENOWIDTH));
1.1 kristaps 634:
635: /* The value already exists: free and reallocate it. */
636:
1.2 kristaps 637: if (-1 == snprintf(buf, sizeof(buf), "%zun", width))
1.23 kristaps 638: return(mdoc_nerr(m, m->last, ENUMFMT));
1.1 kristaps 639:
640: free(m->last->args->argv[i].value[0]);
1.2 kristaps 641: m->last->args->argv[i].value[0] = strdup(buf);
642: if (NULL == m->last->args->argv[i].value[0])
1.23 kristaps 643: return(mdoc_nerr(m, m->last, EMALLOC));
1.1 kristaps 644:
645: return(1);
646: }
647:
648:
649: static int
1.14 kristaps 650: post_bl_head(POST_ARGS)
651: {
652: int i, c;
653: struct mdoc_node *n, *nn, *nnp;
654:
655: if (NULL == m->last->child)
656: return(1);
657:
658: n = m->last->parent;
659: assert(n->args);
660:
661: for (c = 0; c < (int)n->args->argc; c++)
662: if (MDOC_Column == n->args->argv[c].arg)
663: break;
664:
665: /* Only process -column. */
666:
667: if (c == (int)n->args->argc)
668: return(1);
669:
670: assert(0 == n->args->argv[c].sz);
671:
672: /*
673: * Accomodate for new-style groff column syntax. Shuffle the
674: * child nodes, all of which must be TEXT, as arguments for the
675: * column field. Then, delete the head children.
676: */
677:
1.17 kristaps 678: n->args->argv[c].sz = (size_t)m->last->nchild;
679: n->args->argv[c].value = malloc
680: ((size_t)m->last->nchild * sizeof(char *));
1.14 kristaps 681:
682: for (i = 0, nn = m->last->child; nn; i++) {
683: n->args->argv[c].value[i] = nn->string;
684: nn->string = NULL;
685: nnp = nn;
686: nn = nn->next;
687: mdoc_node_free(nnp);
688: }
689:
1.17 kristaps 690: m->last->nchild = 0;
1.14 kristaps 691: m->last->child = NULL;
1.17 kristaps 692:
1.14 kristaps 693: return(1);
694: }
695:
696:
697: static int
1.1 kristaps 698: post_bl(POST_ARGS)
699: {
700: int i, r, len;
701:
1.14 kristaps 702: if (MDOC_HEAD == m->last->type)
703: return(post_bl_head(m));
1.1 kristaps 704: if (MDOC_BLOCK != m->last->type)
705: return(1);
706:
707: /*
708: * These are fairly complicated, so we've broken them into two
709: * functions. post_bl_tagwidth() is called when a -tag is
710: * specified, but no -width (it must be guessed). The second
711: * when a -width is specified (macro indicators must be
712: * rewritten into real lengths).
713: */
714:
715: len = (int)(m->last->args ? m->last->args->argc : 0);
716:
717: for (r = i = 0; i < len; i++) {
718: if (MDOC_Tag == m->last->args->argv[i].arg)
719: r |= 1 << 0;
720: if (MDOC_Width == m->last->args->argv[i].arg)
721: r |= 1 << 1;
722: }
723:
724: if (r & (1 << 0) && ! (r & (1 << 1))) {
725: if ( ! post_bl_tagwidth(m))
726: return(0);
727: } else if (r & (1 << 1))
728: if ( ! post_bl_width(m))
729: return(0);
730:
731: return(1);
732: }
733:
734:
735: static int
1.10 kristaps 736: post_lk(POST_ARGS)
737: {
738: struct mdoc_node *n;
739:
740: if (m->last->child)
741: return(1);
742:
743: n = m->last;
744: m->next = MDOC_NEXT_CHILD;
1.24 kristaps 745:
746: /* XXX: this isn't documented anywhere! */
1.25 kristaps 747: if ( ! mdoc_word_alloc(m, m->last->line, m->last->pos, "~"))
1.10 kristaps 748: return(0);
749:
750: m->last = n;
751: m->next = MDOC_NEXT_SIBLING;
752: return(1);
753: }
754:
755:
756: static int
1.1 kristaps 757: post_ar(POST_ARGS)
758: {
759: struct mdoc_node *n;
760:
761: if (m->last->child)
762: return(1);
763:
764: n = m->last;
765: m->next = MDOC_NEXT_CHILD;
1.25 kristaps 766: if ( ! mdoc_word_alloc(m, m->last->line, m->last->pos, "file"))
1.1 kristaps 767: return(0);
768: m->next = MDOC_NEXT_SIBLING;
1.25 kristaps 769: if ( ! mdoc_word_alloc(m, m->last->line, m->last->pos, "..."))
1.1 kristaps 770: return(0);
771:
772: m->last = n;
773: m->next = MDOC_NEXT_SIBLING;
774: return(1);
775: }
776:
777:
778: static int
779: post_dd(POST_ARGS)
780: {
781: char buf[64];
782:
1.5 kristaps 783: buf[0] = 0;
1.2 kristaps 784: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
785: return(0);
1.1 kristaps 786:
787: if (0 == (m->meta.date = mdoc_atotime(buf))) {
1.23 kristaps 788: if ( ! mdoc_nwarn(m, m->last, EBADDATE))
1.1 kristaps 789: return(0);
790: m->meta.date = time(NULL);
791: }
792:
793: return(post_prol(m));
794: }
795:
796:
797: static int
798: post_prol(POST_ARGS)
799: {
800: struct mdoc_node *n;
801:
802: /*
803: * The end document shouldn't have the prologue macros as part
804: * of the syntax tree (they encompass only meta-data).
805: */
806:
807: if (m->last->parent->child == m->last)
808: m->last->parent->child = m->last->prev;
809: if (m->last->prev)
810: m->last->prev->next = NULL;
811:
812: n = m->last;
813: assert(NULL == m->last->next);
814:
815: if (m->last->prev) {
816: m->last = m->last->prev;
817: m->next = MDOC_NEXT_SIBLING;
818: } else {
819: m->last = m->last->parent;
820: m->next = MDOC_NEXT_CHILD;
821: }
822:
823: mdoc_node_freelist(n);
824: return(1);
825: }
826:
827:
828: static int
829: pre_dl(PRE_ARGS)
830: {
831:
1.16 kristaps 832: if (MDOC_BODY == n->type)
833: m->flags |= MDOC_LITERAL;
1.1 kristaps 834: return(1);
835: }
836:
837:
838: static int
839: pre_bd(PRE_ARGS)
840: {
841: int i;
842:
843: if (MDOC_BODY != n->type)
844: return(1);
845:
1.2 kristaps 846: /* Enter literal context if `Bd -literal' or * -unfilled'. */
1.1 kristaps 847:
1.2 kristaps 848: for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
1.1 kristaps 849: if (MDOC_Literal == n->args->argv[i].arg)
850: break;
851: else if (MDOC_Unfilled == n->args->argv[i].arg)
852: break;
853:
854: if (i < (int)n->args->argc)
855: m->flags |= MDOC_LITERAL;
856:
857: return(1);
858: }
859:
860:
861: static int
862: post_display(POST_ARGS)
863: {
864:
865: if (MDOC_BODY == m->last->type)
866: m->flags &= ~MDOC_LITERAL;
867: return(1);
868: }
869:
870:
CVSweb