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