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