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