]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
Magic fix for bogus manuals making -man puke: I don't care if it's magic because...
[mandoc.git] / mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.58 2010/05/15 16:24:38 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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #ifndef OSNAME
22 #include <sys/utsname.h>
23 #endif
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "libmdoc.h"
32 #include "libmandoc.h"
33
34 #define POST_ARGS struct mdoc *m, struct mdoc_node *n
35 #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
36
37 #define NUMSIZ 32
38 #define DATESIZ 32
39
40 struct actions {
41 int (*pre)(PRE_ARGS);
42 int (*post)(POST_ARGS);
43 };
44
45 static int concat(struct mdoc *, char *,
46 const struct mdoc_node *, size_t);
47 static inline int order_rs(enum mdoct);
48
49 static int post_ar(POST_ARGS);
50 static int post_at(POST_ARGS);
51 static int post_bl(POST_ARGS);
52 static int post_bl_head(POST_ARGS);
53 static int post_bl_tagwidth(POST_ARGS);
54 static int post_bl_width(POST_ARGS);
55 static int post_dd(POST_ARGS);
56 static int post_display(POST_ARGS);
57 static int post_dt(POST_ARGS);
58 static int post_lb(POST_ARGS);
59 static int post_nm(POST_ARGS);
60 static int post_os(POST_ARGS);
61 static int post_pa(POST_ARGS);
62 static int post_prol(POST_ARGS);
63 static int post_rs(POST_ARGS);
64 static int post_sh(POST_ARGS);
65 static int post_st(POST_ARGS);
66 static int post_std(POST_ARGS);
67
68 static int pre_bd(PRE_ARGS);
69 static int pre_bl(PRE_ARGS);
70 static int pre_dl(PRE_ARGS);
71 static int pre_offset(PRE_ARGS);
72
73 static const struct actions mdoc_actions[MDOC_MAX] = {
74 { NULL, NULL }, /* Ap */
75 { NULL, post_dd }, /* Dd */
76 { NULL, post_dt }, /* Dt */
77 { NULL, post_os }, /* Os */
78 { NULL, post_sh }, /* Sh */
79 { NULL, NULL }, /* Ss */
80 { NULL, NULL }, /* Pp */
81 { NULL, NULL }, /* D1 */
82 { pre_dl, post_display }, /* Dl */
83 { pre_bd, post_display }, /* Bd */
84 { NULL, NULL }, /* Ed */
85 { pre_bl, post_bl }, /* Bl */
86 { NULL, NULL }, /* El */
87 { NULL, NULL }, /* It */
88 { NULL, NULL }, /* Ad */
89 { NULL, NULL }, /* An */
90 { NULL, post_ar }, /* Ar */
91 { NULL, NULL }, /* Cd */
92 { NULL, NULL }, /* Cm */
93 { NULL, NULL }, /* Dv */
94 { NULL, NULL }, /* Er */
95 { NULL, NULL }, /* Ev */
96 { NULL, post_std }, /* Ex */
97 { NULL, NULL }, /* Fa */
98 { NULL, NULL }, /* Fd */
99 { NULL, NULL }, /* Fl */
100 { NULL, NULL }, /* Fn */
101 { NULL, NULL }, /* Ft */
102 { NULL, NULL }, /* Ic */
103 { NULL, NULL }, /* In */
104 { NULL, NULL }, /* Li */
105 { NULL, NULL }, /* Nd */
106 { NULL, post_nm }, /* Nm */
107 { NULL, NULL }, /* Op */
108 { NULL, NULL }, /* Ot */
109 { NULL, post_pa }, /* Pa */
110 { NULL, post_std }, /* Rv */
111 { NULL, post_st }, /* St */
112 { NULL, NULL }, /* Va */
113 { NULL, NULL }, /* Vt */
114 { NULL, NULL }, /* Xr */
115 { NULL, NULL }, /* %A */
116 { NULL, NULL }, /* %B */
117 { NULL, NULL }, /* %D */
118 { NULL, NULL }, /* %I */
119 { NULL, NULL }, /* %J */
120 { NULL, NULL }, /* %N */
121 { NULL, NULL }, /* %O */
122 { NULL, NULL }, /* %P */
123 { NULL, NULL }, /* %R */
124 { NULL, NULL }, /* %T */
125 { NULL, NULL }, /* %V */
126 { NULL, NULL }, /* Ac */
127 { NULL, NULL }, /* Ao */
128 { NULL, NULL }, /* Aq */
129 { NULL, post_at }, /* At */
130 { NULL, NULL }, /* Bc */
131 { NULL, NULL }, /* Bf */
132 { NULL, NULL }, /* Bo */
133 { NULL, NULL }, /* Bq */
134 { NULL, NULL }, /* Bsx */
135 { NULL, NULL }, /* Bx */
136 { NULL, NULL }, /* Db */
137 { NULL, NULL }, /* Dc */
138 { NULL, NULL }, /* Do */
139 { NULL, NULL }, /* Dq */
140 { NULL, NULL }, /* Ec */
141 { NULL, NULL }, /* Ef */
142 { NULL, NULL }, /* Em */
143 { NULL, NULL }, /* Eo */
144 { NULL, NULL }, /* Fx */
145 { NULL, NULL }, /* Ms */
146 { NULL, NULL }, /* No */
147 { NULL, NULL }, /* Ns */
148 { NULL, NULL }, /* Nx */
149 { NULL, NULL }, /* Ox */
150 { NULL, NULL }, /* Pc */
151 { NULL, NULL }, /* Pf */
152 { NULL, NULL }, /* Po */
153 { NULL, NULL }, /* Pq */
154 { NULL, NULL }, /* Qc */
155 { NULL, NULL }, /* Ql */
156 { NULL, NULL }, /* Qo */
157 { NULL, NULL }, /* Qq */
158 { NULL, NULL }, /* Re */
159 { NULL, post_rs }, /* Rs */
160 { NULL, NULL }, /* Sc */
161 { NULL, NULL }, /* So */
162 { NULL, NULL }, /* Sq */
163 { NULL, NULL }, /* Sm */
164 { NULL, NULL }, /* Sx */
165 { NULL, NULL }, /* Sy */
166 { NULL, NULL }, /* Tn */
167 { NULL, NULL }, /* Ux */
168 { NULL, NULL }, /* Xc */
169 { NULL, NULL }, /* Xo */
170 { NULL, NULL }, /* Fo */
171 { NULL, NULL }, /* Fc */
172 { NULL, NULL }, /* Oo */
173 { NULL, NULL }, /* Oc */
174 { NULL, NULL }, /* Bk */
175 { NULL, NULL }, /* Ek */
176 { NULL, NULL }, /* Bt */
177 { NULL, NULL }, /* Hf */
178 { NULL, NULL }, /* Fr */
179 { NULL, NULL }, /* Ud */
180 { NULL, post_lb }, /* Lb */
181 { NULL, NULL }, /* Lp */
182 { NULL, NULL }, /* Lk */
183 { NULL, NULL }, /* Mt */
184 { NULL, NULL }, /* Brq */
185 { NULL, NULL }, /* Bro */
186 { NULL, NULL }, /* Brc */
187 { NULL, NULL }, /* %C */
188 { NULL, NULL }, /* Es */
189 { NULL, NULL }, /* En */
190 { NULL, NULL }, /* Dx */
191 { NULL, NULL }, /* %Q */
192 { NULL, NULL }, /* br */
193 { NULL, NULL }, /* sp */
194 { NULL, NULL }, /* %U */
195 };
196
197 #define RSORD_MAX 14
198
199 static const enum mdoct rsord[RSORD_MAX] = {
200 MDOC__A,
201 MDOC__T,
202 MDOC__B,
203 MDOC__I,
204 MDOC__J,
205 MDOC__R,
206 MDOC__N,
207 MDOC__V,
208 MDOC__P,
209 MDOC__Q,
210 MDOC__D,
211 MDOC__O,
212 MDOC__C,
213 MDOC__U
214 };
215
216
217 int
218 mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
219 {
220
221 switch (n->type) {
222 case (MDOC_ROOT):
223 /* FALLTHROUGH */
224 case (MDOC_TEXT):
225 return(1);
226 default:
227 break;
228 }
229
230 if (NULL == mdoc_actions[n->tok].pre)
231 return(1);
232 return((*mdoc_actions[n->tok].pre)(m, n));
233 }
234
235
236 int
237 mdoc_action_post(struct mdoc *m)
238 {
239
240 if (MDOC_ACTED & m->last->flags)
241 return(1);
242 m->last->flags |= MDOC_ACTED;
243
244 switch (m->last->type) {
245 case (MDOC_TEXT):
246 /* FALLTHROUGH */
247 case (MDOC_ROOT):
248 return(1);
249 default:
250 break;
251 }
252
253 if (NULL == mdoc_actions[m->last->tok].post)
254 return(1);
255 return((*mdoc_actions[m->last->tok].post)(m, m->last));
256 }
257
258
259 /*
260 * Concatenate sibling nodes together. All siblings must be of type
261 * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
262 * single whitespace.
263 */
264 static int
265 concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
266 {
267
268 assert(sz);
269 p[0] = '\0';
270 for ( ; n; n = n->next) {
271 assert(MDOC_TEXT == n->type);
272 if (strlcat(p, n->string, sz) >= sz)
273 return(mdoc_nerr(m, n, ETOOLONG));
274 if (NULL == n->next)
275 continue;
276 if (strlcat(p, " ", sz) >= sz)
277 return(mdoc_nerr(m, n, ETOOLONG));
278 }
279
280 return(1);
281 }
282
283
284 /*
285 * Macros accepting `-std' as an argument have the name of the current
286 * document (`Nm') filled in as the argument if it's not provided.
287 */
288 static int
289 post_std(POST_ARGS)
290 {
291 struct mdoc_node *nn;
292
293 if (n->child)
294 return(1);
295
296 nn = n;
297 m->next = MDOC_NEXT_CHILD;
298 assert(m->meta.name);
299 if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
300 return(0);
301 m->last = nn;
302 return(1);
303 }
304
305
306 /*
307 * The `Nm' macro's first use sets the name of the document. See also
308 * post_std(), etc.
309 */
310 static int
311 post_nm(POST_ARGS)
312 {
313 char buf[BUFSIZ];
314
315 if (m->meta.name)
316 return(1);
317 if ( ! concat(m, buf, n->child, BUFSIZ))
318 return(0);
319 m->meta.name = mandoc_strdup(buf);
320 return(1);
321 }
322
323
324 /*
325 * Look up the value of `Lb' for matching predefined strings. If it has
326 * one, then substitute the current value for the formatted value. Note
327 * that the lookup may fail (we can provide arbitrary strings).
328 */
329 /* ARGSUSED */
330 static int
331 post_lb(POST_ARGS)
332 {
333 const char *p;
334 char *buf;
335 size_t sz;
336
337 assert(MDOC_TEXT == n->child->type);
338 p = mdoc_a2lib(n->child->string);
339
340 if (p) {
341 free(n->child->string);
342 n->child->string = mandoc_strdup(p);
343 return(1);
344 }
345
346 sz = strlen(n->child->string) +
347 2 + strlen("\\(lqlibrary\\(rq");
348 buf = mandoc_malloc(sz);
349 snprintf(buf, sz, "library \\(lq%s\\(rq", n->child->string);
350 free(n->child->string);
351 n->child->string = buf;
352 return(1);
353 }
354
355
356 /*
357 * Substitute the value of `St' for the corresponding formatted string.
358 * We're guaranteed that this exists (it's been verified during the
359 * validation phase).
360 */
361 /* ARGSUSED */
362 static int
363 post_st(POST_ARGS)
364 {
365 const char *p;
366
367 assert(MDOC_TEXT == n->child->type);
368 p = mdoc_a2st(n->child->string);
369 if (p != NULL) {
370 free(n->child->string);
371 n->child->string = mandoc_strdup(p);
372 }
373 return(1);
374 }
375
376
377 /*
378 * Look up the standard string in a table. We know that it exists from
379 * the validation phase, so assert on failure. If a standard key wasn't
380 * supplied, supply the default ``AT&T UNIX''.
381 */
382 static int
383 post_at(POST_ARGS)
384 {
385 struct mdoc_node *nn;
386 const char *p, *q;
387 char *buf;
388 size_t sz;
389
390 if (n->child) {
391 assert(MDOC_TEXT == n->child->type);
392 p = mdoc_a2att(n->child->string);
393 if (p) {
394 free(n->child->string);
395 n->child->string = mandoc_strdup(p);
396 } else {
397 p = "AT&T UNIX ";
398 q = n->child->string;
399 sz = strlen(p) + strlen(q) + 1;
400 buf = mandoc_malloc(sz);
401 strlcpy(buf, p, sz);
402 strlcat(buf, q, sz);
403 free(n->child->string);
404 n->child->string = buf;
405 }
406 return(1);
407 }
408
409 nn = n;
410 m->next = MDOC_NEXT_CHILD;
411 if ( ! mdoc_word_alloc(m, nn->line, nn->pos, "AT&T UNIX"))
412 return(0);
413 m->last = nn;
414 return(1);
415 }
416
417
418 /*
419 * Mark the current section. The ``named'' section (lastnamed) is set
420 * whenever the current section isn't a custom section--we use this to
421 * keep track of section ordering. Also check that the section is
422 * allowed within the document's manual section.
423 */
424 static int
425 post_sh(POST_ARGS)
426 {
427 enum mdoc_sec sec;
428 char buf[BUFSIZ];
429
430 if (MDOC_HEAD != n->type)
431 return(1);
432
433 if ( ! concat(m, buf, n->child, BUFSIZ))
434 return(0);
435 sec = mdoc_str2sec(buf);
436 /*
437 * The first section should always make us move into a non-new
438 * state.
439 */
440 if (SEC_NONE == m->lastnamed || SEC_CUSTOM != sec)
441 m->lastnamed = sec;
442
443 /* Some sections only live in certain manual sections. */
444
445 switch ((m->lastsec = sec)) {
446 case (SEC_RETURN_VALUES):
447 /* FALLTHROUGH */
448 case (SEC_ERRORS):
449 assert(m->meta.msec);
450 if (*m->meta.msec == '2')
451 break;
452 if (*m->meta.msec == '3')
453 break;
454 if (*m->meta.msec == '9')
455 break;
456 return(mdoc_nwarn(m, n, EWRONGMSEC));
457 default:
458 break;
459 }
460 return(1);
461 }
462
463
464 /*
465 * Parse out the contents of `Dt'. See in-line documentation for how we
466 * handle the various fields of this macro.
467 */
468 static int
469 post_dt(POST_ARGS)
470 {
471 struct mdoc_node *nn;
472 const char *cp;
473
474 if (m->meta.title)
475 free(m->meta.title);
476 if (m->meta.vol)
477 free(m->meta.vol);
478 if (m->meta.arch)
479 free(m->meta.arch);
480
481 m->meta.title = m->meta.vol = m->meta.arch = NULL;
482 /* Handles: `.Dt'
483 * --> title = unknown, volume = local, msec = 0, arch = NULL
484 */
485
486 if (NULL == (nn = n->child)) {
487 /* XXX: make these macro values. */
488 /* FIXME: warn about missing values. */
489 m->meta.title = mandoc_strdup("unknown");
490 m->meta.vol = mandoc_strdup("local");
491 m->meta.msec = mandoc_strdup("1");
492 return(post_prol(m, n));
493 }
494
495 /* Handles: `.Dt TITLE'
496 * --> title = TITLE, volume = local, msec = 0, arch = NULL
497 */
498
499 m->meta.title = mandoc_strdup(nn->string);
500
501 if (NULL == (nn = nn->next)) {
502 /* FIXME: warn about missing msec. */
503 /* XXX: make this a macro value. */
504 m->meta.vol = mandoc_strdup("local");
505 m->meta.msec = mandoc_strdup("1");
506 return(post_prol(m, n));
507 }
508
509 /* Handles: `.Dt TITLE SEC'
510 * --> title = TITLE, volume = SEC is msec ?
511 * format(msec) : SEC,
512 * msec = SEC is msec ? atoi(msec) : 0,
513 * arch = NULL
514 */
515
516 cp = mdoc_a2msec(nn->string);
517 if (cp) {
518 m->meta.vol = mandoc_strdup(cp);
519 m->meta.msec = mandoc_strdup(nn->string);
520 } else if (mdoc_nwarn(m, n, EBADMSEC)) {
521 m->meta.vol = mandoc_strdup(nn->string);
522 m->meta.msec = mandoc_strdup(nn->string);
523 } else
524 return(0);
525
526 if (NULL == (nn = nn->next))
527 return(post_prol(m, n));
528
529 /* Handles: `.Dt TITLE SEC VOL'
530 * --> title = TITLE, volume = VOL is vol ?
531 * format(VOL) :
532 * VOL is arch ? format(arch) :
533 * VOL
534 */
535
536 cp = mdoc_a2vol(nn->string);
537 if (cp) {
538 free(m->meta.vol);
539 m->meta.vol = mandoc_strdup(cp);
540 } else {
541 /* FIXME: warn about bad arch. */
542 cp = mdoc_a2arch(nn->string);
543 if (NULL == cp) {
544 free(m->meta.vol);
545 m->meta.vol = mandoc_strdup(nn->string);
546 } else
547 m->meta.arch = mandoc_strdup(cp);
548 }
549
550 /* Ignore any subsequent parameters... */
551 /* FIXME: warn about subsequent parameters. */
552
553 return(post_prol(m, n));
554 }
555
556
557 /*
558 * Set the operating system by way of the `Os' macro. Note that if an
559 * argument isn't provided and -DOSNAME="\"foo\"" is provided during
560 * compilation, this value will be used instead of filling in "sysname
561 * release" from uname().
562 */
563 static int
564 post_os(POST_ARGS)
565 {
566 char buf[BUFSIZ];
567 #ifndef OSNAME
568 struct utsname utsname;
569 #endif
570
571 if (m->meta.os)
572 free(m->meta.os);
573
574 if ( ! concat(m, buf, n->child, BUFSIZ))
575 return(0);
576
577 if ('\0' == buf[0]) {
578 #ifdef OSNAME
579 if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ)
580 return(mdoc_nerr(m, n, EUTSNAME));
581 #else /*!OSNAME */
582 if (-1 == uname(&utsname))
583 return(mdoc_nerr(m, n, EUTSNAME));
584 if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ)
585 return(mdoc_nerr(m, n, ETOOLONG));
586 if (strlcat(buf, " ", 64) >= BUFSIZ)
587 return(mdoc_nerr(m, n, ETOOLONG));
588 if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ)
589 return(mdoc_nerr(m, n, ETOOLONG));
590 #endif /*!OSNAME*/
591 }
592
593 m->meta.os = mandoc_strdup(buf);
594 return(post_prol(m, n));
595 }
596
597
598 /*
599 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
600 * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
601 * argument has NOT been provided. See post_bl_width() for converting
602 * the -width string.
603 */
604 static int
605 post_bl_tagwidth(POST_ARGS)
606 {
607 struct mdoc_node *nn;
608 size_t sz;
609 int i;
610 char buf[NUMSIZ];
611
612 /* Defaults to ten ens. */
613
614 sz = 10; /* XXX: make this a macro value. */
615 nn = n->body->child;
616
617 if (nn) {
618 assert(MDOC_BLOCK == nn->type);
619 assert(MDOC_It == nn->tok);
620 nn = nn->head->child;
621 if (MDOC_TEXT != nn->type) {
622 sz = mdoc_macro2len(nn->tok);
623 if (sz == 0) {
624 if ( ! mdoc_nwarn(m, n, ENOWIDTH))
625 return(0);
626 sz = 10;
627 }
628 } else
629 sz = strlen(nn->string) + 1;
630 }
631
632 snprintf(buf, NUMSIZ, "%zun", sz);
633
634 /*
635 * We have to dynamically add this to the macro's argument list.
636 * We're guaranteed that a MDOC_Width doesn't already exist.
637 */
638
639 nn = n;
640 assert(nn->args);
641 i = (int)(nn->args->argc)++;
642
643 nn->args->argv = mandoc_realloc(nn->args->argv,
644 nn->args->argc * sizeof(struct mdoc_argv));
645
646 nn->args->argv[i].arg = MDOC_Width;
647 nn->args->argv[i].line = n->line;
648 nn->args->argv[i].pos = n->pos;
649 nn->args->argv[i].sz = 1;
650 nn->args->argv[i].value = mandoc_malloc(sizeof(char *));
651 nn->args->argv[i].value[0] = mandoc_strdup(buf);
652 return(1);
653 }
654
655
656 /*
657 * Calculate the real width of a list from the -width string, which may
658 * contain a macro (with a known default width), a literal string, or a
659 * scaling width.
660 */
661 static int
662 post_bl_width(POST_ARGS)
663 {
664 size_t width;
665 int i;
666 enum mdoct tok;
667 char buf[NUMSIZ];
668 char *p;
669
670 if (NULL == n->args)
671 return(1);
672
673 for (i = 0; i < (int)n->args->argc; i++)
674 if (MDOC_Width == n->args->argv[i].arg)
675 break;
676
677 if (i == (int)n->args->argc)
678 return(1);
679 p = n->args->argv[i].value[0];
680
681 /*
682 * If the value to -width is a macro, then we re-write it to be
683 * the macro's width as set in share/tmac/mdoc/doc-common.
684 */
685
686 if (0 == strcmp(p, "Ds"))
687 /* XXX: make into a macro. */
688 width = 6;
689 else if (MDOC_MAX == (tok = mdoc_hash_find(p)))
690 return(1);
691 else if (0 == (width = mdoc_macro2len(tok)))
692 return(mdoc_nwarn(m, n, ENOWIDTH));
693
694 /* The value already exists: free and reallocate it. */
695
696 snprintf(buf, NUMSIZ, "%zun", width);
697 free(n->args->argv[i].value[0]);
698 n->args->argv[i].value[0] = mandoc_strdup(buf);
699 return(1);
700 }
701
702
703 /*
704 * Do processing for -column lists, which can have two distinct styles
705 * of invocation. Merge this two styles into a consistent form.
706 */
707 /* ARGSUSED */
708 static int
709 post_bl_head(POST_ARGS)
710 {
711 int i, c;
712 struct mdoc_node *np, *nn, *nnp;
713
714 if (NULL == n->child)
715 return(1);
716
717 np = n->parent;
718 assert(np->args);
719
720 for (c = 0; c < (int)np->args->argc; c++)
721 if (MDOC_Column == np->args->argv[c].arg)
722 break;
723
724 if (c == (int)np->args->argc)
725 return(1);
726 assert(0 == np->args->argv[c].sz);
727
728 /*
729 * Accomodate for new-style groff column syntax. Shuffle the
730 * child nodes, all of which must be TEXT, as arguments for the
731 * column field. Then, delete the head children.
732 */
733
734 np->args->argv[c].sz = (size_t)n->nchild;
735 np->args->argv[c].value = mandoc_malloc
736 ((size_t)n->nchild * sizeof(char *));
737
738 for (i = 0, nn = n->child; nn; i++) {
739 np->args->argv[c].value[i] = nn->string;
740 nn->string = NULL;
741 nnp = nn;
742 nn = nn->next;
743 mdoc_node_delete(NULL, nnp);
744 }
745
746 n->nchild = 0;
747 n->child = NULL;
748 return(1);
749 }
750
751
752 static int
753 post_bl(POST_ARGS)
754 {
755 int i, r, len;
756
757 if (MDOC_HEAD == n->type)
758 return(post_bl_head(m, n));
759 if (MDOC_BLOCK != n->type)
760 return(1);
761
762 /*
763 * These are fairly complicated, so we've broken them into two
764 * functions. post_bl_tagwidth() is called when a -tag is
765 * specified, but no -width (it must be guessed). The second
766 * when a -width is specified (macro indicators must be
767 * rewritten into real lengths).
768 */
769
770 len = (int)(n->args ? n->args->argc : 0);
771
772 for (r = i = 0; i < len; i++) {
773 if (MDOC_Tag == n->args->argv[i].arg)
774 r |= 1 << 0;
775 if (MDOC_Width == n->args->argv[i].arg)
776 r |= 1 << 1;
777 }
778
779 if (r & (1 << 0) && ! (r & (1 << 1))) {
780 if ( ! post_bl_tagwidth(m, n))
781 return(0);
782 } else if (r & (1 << 1))
783 if ( ! post_bl_width(m, n))
784 return(0);
785
786 return(1);
787 }
788
789
790 /*
791 * The `Pa' macro defaults to a tilde if no value is provided as an
792 * argument.
793 */
794 static int
795 post_pa(POST_ARGS)
796 {
797 struct mdoc_node *np;
798
799 if (n->child)
800 return(1);
801
802 np = n;
803 m->next = MDOC_NEXT_CHILD;
804 /* XXX: make into macro value. */
805 if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
806 return(0);
807 m->last = np;
808 return(1);
809 }
810
811
812 /*
813 * The `Ar' macro defaults to two strings "file ..." if no value is
814 * provided as an argument.
815 */
816 static int
817 post_ar(POST_ARGS)
818 {
819 struct mdoc_node *np;
820
821 if (n->child)
822 return(1);
823
824 np = n;
825 m->next = MDOC_NEXT_CHILD;
826 /* XXX: make into macro values. */
827 if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
828 return(0);
829 if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
830 return(0);
831 m->last = np;
832 return(1);
833 }
834
835
836 /*
837 * Parse the date field in `Dd'.
838 */
839 static int
840 post_dd(POST_ARGS)
841 {
842 char buf[DATESIZ];
843
844 if ( ! concat(m, buf, n->child, DATESIZ))
845 return(0);
846
847 m->meta.date = mandoc_a2time
848 (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
849
850 if (0 == m->meta.date) {
851 if ( ! mdoc_nwarn(m, n, EBADDATE))
852 return(0);
853 m->meta.date = time(NULL);
854 }
855
856 return(post_prol(m, n));
857 }
858
859
860 /*
861 * Remove prologue macros from the document after they're processed.
862 * The final document uses mdoc_meta for these values and discards the
863 * originals.
864 */
865 static int
866 post_prol(POST_ARGS)
867 {
868
869 mdoc_node_delete(m, n);
870 if (m->meta.title && m->meta.date && m->meta.os)
871 m->flags |= MDOC_PBODY;
872 return(1);
873 }
874
875
876 /*
877 * Trigger a literal context.
878 */
879 static int
880 pre_dl(PRE_ARGS)
881 {
882
883 if (MDOC_BODY == n->type)
884 m->flags |= MDOC_LITERAL;
885 return(1);
886 }
887
888
889 /* ARGSUSED */
890 static int
891 pre_offset(PRE_ARGS)
892 {
893 int i;
894
895 /*
896 * Make sure that an empty offset produces an 8n length space as
897 * stipulated by mdoc.samples.
898 */
899
900 assert(n->args);
901 for (i = 0; i < (int)n->args->argc; i++) {
902 if (MDOC_Offset != n->args->argv[i].arg)
903 continue;
904 if (n->args->argv[i].sz)
905 break;
906 assert(1 == n->args->refcnt);
907 /* If no value set, length of <string>. */
908 n->args->argv[i].sz++;
909 n->args->argv[i].value = mandoc_malloc(sizeof(char *));
910 n->args->argv[i].value[0] = mandoc_strdup("8n");
911 break;
912 }
913
914 return(1);
915 }
916
917
918 static int
919 pre_bl(PRE_ARGS)
920 {
921
922 return(MDOC_BLOCK == n->type ? pre_offset(m, n) : 1);
923 }
924
925
926 static int
927 pre_bd(PRE_ARGS)
928 {
929 int i;
930
931 if (MDOC_BLOCK == n->type)
932 return(pre_offset(m, n));
933 if (MDOC_BODY != n->type)
934 return(1);
935
936 /* Enter literal context if `Bd -literal' or `-unfilled'. */
937
938 for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
939 if (MDOC_Literal == n->args->argv[i].arg)
940 m->flags |= MDOC_LITERAL;
941 else if (MDOC_Unfilled == n->args->argv[i].arg)
942 m->flags |= MDOC_LITERAL;
943
944 return(1);
945 }
946
947
948 static int
949 post_display(POST_ARGS)
950 {
951
952 if (MDOC_BODY == n->type)
953 m->flags &= ~MDOC_LITERAL;
954 return(1);
955 }
956
957
958 static inline int
959 order_rs(enum mdoct t)
960 {
961 int i;
962
963 for (i = 0; i < (int)RSORD_MAX; i++)
964 if (rsord[i] == t)
965 return(i);
966
967 abort();
968 /* NOTREACHED */
969 }
970
971
972 /* ARGSUSED */
973 static int
974 post_rs(POST_ARGS)
975 {
976 struct mdoc_node *nn, *next, *prev;
977 int o;
978
979 if (MDOC_BLOCK != n->type)
980 return(1);
981
982 assert(n->body->child);
983 for (next = NULL, nn = n->body->child->next; nn; nn = next) {
984 o = order_rs(nn->tok);
985
986 /* Remove `nn' from the chain. */
987 next = nn->next;
988 if (next)
989 next->prev = nn->prev;
990
991 prev = nn->prev;
992 if (prev)
993 prev->next = nn->next;
994
995 nn->prev = nn->next = NULL;
996
997 /*
998 * Scan back until we reach a node that's ordered before
999 * us, then set ourselves as being the next.
1000 */
1001 for ( ; prev; prev = prev->prev)
1002 if (order_rs(prev->tok) <= o)
1003 break;
1004
1005 nn->prev = prev;
1006 if (prev) {
1007 if (prev->next)
1008 prev->next->prev = nn;
1009 nn->next = prev->next;
1010 prev->next = nn;
1011 continue;
1012 }
1013
1014 n->body->child->prev = nn;
1015 nn->next = n->body->child;
1016 n->body->child = nn;
1017 }
1018 return(1);
1019 }