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