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