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