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