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