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