]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
Cached `Bl -offset' into mdoc_bl. Removed erroneous "-offset defaults
[mandoc.git] / mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.68 2010/06/12 12:38:01 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_li(POST_ARGS);
61 static int post_nm(POST_ARGS);
62 static int post_os(POST_ARGS);
63 static int post_pa(POST_ARGS);
64 static int post_prol(POST_ARGS);
65 static int post_rs(POST_ARGS);
66 static int post_sh(POST_ARGS);
67 static int post_st(POST_ARGS);
68 static int post_std(POST_ARGS);
69
70 static int pre_bd(PRE_ARGS);
71 static int pre_dl(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 { NULL, 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, post_li }, /* 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 { NULL, NULL }, /* Ta */
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
512 ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
513
514 if (NULL == (nn = nn->next)) {
515 /* FIXME: warn about missing msec. */
516 /* XXX: make this a macro value. */
517 m->meta.vol = mandoc_strdup("LOCAL");
518 m->meta.msec = mandoc_strdup("1");
519 return(post_prol(m, n));
520 }
521
522 /* Handles: `.Dt TITLE SEC'
523 * --> title = TITLE, volume = SEC is msec ?
524 * format(msec) : SEC,
525 * msec = SEC is msec ? atoi(msec) : 0,
526 * arch = NULL
527 */
528
529 cp = mdoc_a2msec(nn->string);
530 if (cp) {
531 m->meta.vol = mandoc_strdup(cp);
532 m->meta.msec = mandoc_strdup(nn->string);
533 } else if (mdoc_nmsg(m, n, MANDOCERR_BADMSEC)) {
534 m->meta.vol = mandoc_strdup(nn->string);
535 m->meta.msec = mandoc_strdup(nn->string);
536 } else
537 return(0);
538
539 if (NULL == (nn = nn->next))
540 return(post_prol(m, n));
541
542 /* Handles: `.Dt TITLE SEC VOL'
543 * --> title = TITLE, volume = VOL is vol ?
544 * format(VOL) :
545 * VOL is arch ? format(arch) :
546 * VOL
547 */
548
549 cp = mdoc_a2vol(nn->string);
550 if (cp) {
551 free(m->meta.vol);
552 m->meta.vol = mandoc_strdup(cp);
553 } else {
554 /* FIXME: warn about bad arch. */
555 cp = mdoc_a2arch(nn->string);
556 if (NULL == cp) {
557 free(m->meta.vol);
558 m->meta.vol = mandoc_strdup(nn->string);
559 } else
560 m->meta.arch = mandoc_strdup(cp);
561 }
562
563 /* Ignore any subsequent parameters... */
564 /* FIXME: warn about subsequent parameters. */
565
566 return(post_prol(m, n));
567 }
568
569
570 /*
571 * Set the operating system by way of the `Os' macro. Note that if an
572 * argument isn't provided and -DOSNAME="\"foo\"" is provided during
573 * compilation, this value will be used instead of filling in "sysname
574 * release" from uname().
575 */
576 static int
577 post_os(POST_ARGS)
578 {
579 char buf[BUFSIZ];
580 #ifndef OSNAME
581 struct utsname utsname;
582 #endif
583
584 if (m->meta.os)
585 free(m->meta.os);
586
587 if ( ! concat(m, buf, n->child, BUFSIZ))
588 return(0);
589
590 /* XXX: yes, these can all be dynamically-adjusted buffers, but
591 * it's really not worth the extra hackery.
592 */
593
594 if ('\0' == buf[0]) {
595 #ifdef OSNAME
596 if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
597 mdoc_nmsg(m, n, MANDOCERR_MEM);
598 return(0);
599 }
600 #else /*!OSNAME */
601 if (-1 == uname(&utsname))
602 return(mdoc_nmsg(m, n, MANDOCERR_UTSNAME));
603
604 if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
605 mdoc_nmsg(m, n, MANDOCERR_MEM);
606 return(0);
607 }
608 if (strlcat(buf, " ", 64) >= BUFSIZ) {
609 mdoc_nmsg(m, n, MANDOCERR_MEM);
610 return(0);
611 }
612 if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
613 mdoc_nmsg(m, n, MANDOCERR_MEM);
614 return(0);
615 }
616 #endif /*!OSNAME*/
617 }
618
619 m->meta.os = mandoc_strdup(buf);
620 return(post_prol(m, n));
621 }
622
623
624 /*
625 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
626 * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
627 * argument has NOT been provided. See post_bl_width() for converting
628 * the -width string.
629 */
630 static int
631 post_bl_tagwidth(POST_ARGS)
632 {
633 struct mdoc_node *nn;
634 size_t sz;
635 int i;
636 char buf[NUMSIZ];
637
638 /* Defaults to ten ens. */
639
640 sz = 10; /* XXX: make this a macro value. */
641
642 for (nn = n->body->child; nn; nn = nn->next) {
643 if (MDOC_It == nn->tok)
644 break;
645 }
646
647 if (nn) {
648 assert(MDOC_BLOCK == nn->type);
649 nn = nn->head->child;
650 if (MDOC_TEXT != nn->type) {
651 sz = mdoc_macro2len(nn->tok);
652 if (sz == 0) {
653 if ( ! mdoc_nmsg(m, n, MANDOCERR_NOWIDTHARG))
654 return(0);
655 sz = 10;
656 }
657 } else
658 sz = strlen(nn->string) + 1;
659 }
660
661 snprintf(buf, NUMSIZ, "%zun", sz);
662
663 /*
664 * We have to dynamically add this to the macro's argument list.
665 * We're guaranteed that a MDOC_Width doesn't already exist.
666 */
667
668 nn = n;
669 assert(nn->args);
670 i = (int)(nn->args->argc)++;
671
672 nn->args->argv = mandoc_realloc(nn->args->argv,
673 nn->args->argc * sizeof(struct mdoc_argv));
674
675 nn->args->argv[i].arg = MDOC_Width;
676 nn->args->argv[i].line = n->line;
677 nn->args->argv[i].pos = n->pos;
678 nn->args->argv[i].sz = 1;
679 nn->args->argv[i].value = mandoc_malloc(sizeof(char *));
680 nn->args->argv[i].value[0] = mandoc_strdup(buf);
681 return(1);
682 }
683
684
685 /*
686 * Calculate the real width of a list from the -width string, which may
687 * contain a macro (with a known default width), a literal string, or a
688 * scaling width.
689 */
690 static int
691 post_bl_width(POST_ARGS)
692 {
693 size_t width;
694 int i;
695 enum mdoct tok;
696 char buf[NUMSIZ];
697 char *p;
698
699 if (NULL == n->args)
700 return(1);
701
702 for (i = 0; i < (int)n->args->argc; i++)
703 if (MDOC_Width == n->args->argv[i].arg)
704 break;
705
706 if (i == (int)n->args->argc)
707 return(1);
708 p = n->args->argv[i].value[0];
709
710 /*
711 * If the value to -width is a macro, then we re-write it to be
712 * the macro's width as set in share/tmac/mdoc/doc-common.
713 */
714
715 if (0 == strcmp(p, "Ds"))
716 width = 6;
717 else if (MDOC_MAX == (tok = mdoc_hash_find(p)))
718 return(1);
719 else if (0 == (width = mdoc_macro2len(tok)))
720 return(mdoc_nmsg(m, n, MANDOCERR_BADWIDTH));
721
722 /* The value already exists: free and reallocate it. */
723
724 snprintf(buf, NUMSIZ, "%zun", width);
725 free(n->args->argv[i].value[0]);
726 n->args->argv[i].value[0] = mandoc_strdup(buf);
727 return(1);
728 }
729
730
731 /*
732 * Do processing for -column lists, which can have two distinct styles
733 * of invocation. Merge this two styles into a consistent form.
734 */
735 /* ARGSUSED */
736 static int
737 post_bl_head(POST_ARGS)
738 {
739 int i, c;
740 struct mdoc_node *np, *nn, *nnp;
741
742 if (NULL == n->child)
743 return(1);
744
745 np = n->parent;
746 assert(np->args);
747
748 for (c = 0; c < (int)np->args->argc; c++)
749 if (MDOC_Column == np->args->argv[c].arg)
750 break;
751
752 if (c == (int)np->args->argc)
753 return(1);
754 assert(0 == np->args->argv[c].sz);
755
756 /*
757 * Accomodate for new-style groff column syntax. Shuffle the
758 * child nodes, all of which must be TEXT, as arguments for the
759 * column field. Then, delete the head children.
760 */
761
762 np->args->argv[c].sz = (size_t)n->nchild;
763 np->args->argv[c].value = mandoc_malloc
764 ((size_t)n->nchild * sizeof(char *));
765
766 for (i = 0, nn = n->child; nn; i++) {
767 np->args->argv[c].value[i] = nn->string;
768 nn->string = NULL;
769 nnp = nn;
770 nn = nn->next;
771 mdoc_node_delete(NULL, nnp);
772 }
773
774 n->nchild = 0;
775 n->child = NULL;
776 return(1);
777 }
778
779
780 static int
781 post_bl(POST_ARGS)
782 {
783 int i, r, len;
784
785 if (MDOC_HEAD == n->type)
786 return(post_bl_head(m, n));
787 if (MDOC_BLOCK != n->type)
788 return(1);
789
790 /*
791 * These are fairly complicated, so we've broken them into two
792 * functions. post_bl_tagwidth() is called when a -tag is
793 * specified, but no -width (it must be guessed). The second
794 * when a -width is specified (macro indicators must be
795 * rewritten into real lengths).
796 */
797
798 len = (int)(n->args ? n->args->argc : 0);
799
800 for (r = i = 0; i < len; i++) {
801 if (MDOC_Tag == n->args->argv[i].arg)
802 r |= 1 << 0;
803 if (MDOC_Width == n->args->argv[i].arg)
804 r |= 1 << 1;
805 }
806
807 if (r & (1 << 0) && ! (r & (1 << 1))) {
808 if ( ! post_bl_tagwidth(m, n))
809 return(0);
810 } else if (r & (1 << 1))
811 if ( ! post_bl_width(m, n))
812 return(0);
813
814 return(1);
815 }
816
817
818 /*
819 * The `Pa' macro defaults to a tilde if no value is provided as an
820 * argument.
821 */
822 static int
823 post_pa(POST_ARGS)
824 {
825 struct mdoc_node *np;
826
827 if (n->child)
828 return(1);
829
830 np = n;
831 m->next = MDOC_NEXT_CHILD;
832 /* XXX: make into macro value. */
833 if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
834 return(0);
835 m->last = np;
836 return(1);
837 }
838
839
840 /*
841 * Empty `Li' macros get an empty string to make front-ends add an extra
842 * space.
843 */
844 static int
845 post_li(POST_ARGS)
846 {
847 struct mdoc_node *np;
848
849 if (n->child)
850 return(1);
851
852 np = n;
853 m->next = MDOC_NEXT_CHILD;
854 if ( ! mdoc_word_alloc(m, n->line, n->pos, ""))
855 return(0);
856 m->last = np;
857 return(1);
858 }
859
860
861 /*
862 * The `Ar' macro defaults to two strings "file ..." if no value is
863 * provided as an argument.
864 */
865 static int
866 post_ar(POST_ARGS)
867 {
868 struct mdoc_node *np;
869
870 if (n->child)
871 return(1);
872
873 np = n;
874 m->next = MDOC_NEXT_CHILD;
875 /* XXX: make into macro values. */
876 if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
877 return(0);
878 if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
879 return(0);
880 m->last = np;
881 return(1);
882 }
883
884
885 /*
886 * Parse the date field in `Dd'.
887 */
888 static int
889 post_dd(POST_ARGS)
890 {
891 char buf[DATESIZ];
892
893 if ( ! concat(m, buf, n->child, DATESIZ))
894 return(0);
895
896 m->meta.date = mandoc_a2time
897 (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
898
899 if (0 == m->meta.date) {
900 if ( ! mdoc_nmsg(m, n, MANDOCERR_BADDATE))
901 return(0);
902 m->meta.date = time(NULL);
903 }
904
905 return(post_prol(m, n));
906 }
907
908
909 /*
910 * Remove prologue macros from the document after they're processed.
911 * The final document uses mdoc_meta for these values and discards the
912 * originals.
913 */
914 static int
915 post_prol(POST_ARGS)
916 {
917
918 mdoc_node_delete(m, n);
919 if (m->meta.title && m->meta.date && m->meta.os)
920 m->flags |= MDOC_PBODY;
921 return(1);
922 }
923
924
925 /*
926 * Trigger a literal context.
927 */
928 static int
929 pre_dl(PRE_ARGS)
930 {
931
932 if (MDOC_BODY == n->type)
933 m->flags |= MDOC_LITERAL;
934 return(1);
935 }
936
937
938 static int
939 pre_bd(PRE_ARGS)
940 {
941
942 if (MDOC_BODY != n->type)
943 return(1);
944
945 if (DISP_literal == n->data.Bd.type)
946 m->flags |= MDOC_LITERAL;
947 if (DISP_unfilled == n->data.Bd.type)
948 m->flags |= MDOC_LITERAL;
949
950 return(1);
951 }
952
953
954 static int
955 post_display(POST_ARGS)
956 {
957
958 if (MDOC_BODY == n->type)
959 m->flags &= ~MDOC_LITERAL;
960 return(1);
961 }
962
963
964 static inline int
965 order_rs(enum mdoct t)
966 {
967 int i;
968
969 for (i = 0; i < (int)RSORD_MAX; i++)
970 if (rsord[i] == t)
971 return(i);
972
973 abort();
974 /* NOTREACHED */
975 }
976
977
978 /* ARGSUSED */
979 static int
980 post_rs(POST_ARGS)
981 {
982 struct mdoc_node *nn, *next, *prev;
983 int o;
984
985 if (MDOC_BLOCK != n->type)
986 return(1);
987
988 assert(n->body->child);
989 for (next = NULL, nn = n->body->child->next; nn; nn = next) {
990 o = order_rs(nn->tok);
991
992 /* Remove `nn' from the chain. */
993 next = nn->next;
994 if (next)
995 next->prev = nn->prev;
996
997 prev = nn->prev;
998 if (prev)
999 prev->next = nn->next;
1000
1001 nn->prev = nn->next = NULL;
1002
1003 /*
1004 * Scan back until we reach a node that's ordered before
1005 * us, then set ourselves as being the next.
1006 */
1007 for ( ; prev; prev = prev->prev)
1008 if (order_rs(prev->tok) <= o)
1009 break;
1010
1011 nn->prev = prev;
1012 if (prev) {
1013 if (prev->next)
1014 prev->next->prev = nn;
1015 nn->next = prev->next;
1016 prev->next = nn;
1017 continue;
1018 }
1019
1020 n->body->child->prev = nn;
1021 nn->next = n->body->child;
1022 n->body->child = nn;
1023 }
1024 return(1);
1025 }