1 /* $Id: mdoc_action.c,v 1.50 2010/01/01 17:14:29 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
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.
18 #include <sys/utsname.h>
28 #include "libmandoc.h"
30 #define POST_ARGS struct mdoc *m, struct mdoc_node *n
31 #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
38 int (*post)(POST_ARGS);
41 static int concat(struct mdoc *, char *,
42 const struct mdoc_node *, size_t);
43 static inline int order_rs(int);
45 static int post_ar(POST_ARGS);
46 static int post_at(POST_ARGS);
47 static int post_bl(POST_ARGS);
48 static int post_bl_head(POST_ARGS);
49 static int post_bl_tagwidth(POST_ARGS);
50 static int post_bl_width(POST_ARGS);
51 static int post_dd(POST_ARGS);
52 static int post_display(POST_ARGS);
53 static int post_dt(POST_ARGS);
54 static int post_lb(POST_ARGS);
55 static int post_nm(POST_ARGS);
56 static int post_os(POST_ARGS);
57 static int post_pa(POST_ARGS);
58 static int post_prol(POST_ARGS);
59 static int post_rs(POST_ARGS);
60 static int post_sh(POST_ARGS);
61 static int post_st(POST_ARGS);
62 static int post_std(POST_ARGS);
64 static int pre_bd(PRE_ARGS);
65 static int pre_bl(PRE_ARGS);
66 static int pre_dl(PRE_ARGS);
67 static int pre_offset(PRE_ARGS);
69 static const struct actions mdoc_actions[MDOC_MAX] = {
70 { NULL, NULL }, /* Ap */
71 { NULL, post_dd }, /* Dd */
72 { NULL, post_dt }, /* Dt */
73 { NULL, post_os }, /* Os */
74 { NULL, post_sh }, /* Sh */
75 { NULL, NULL }, /* Ss */
76 { NULL, NULL }, /* Pp */
77 { NULL, NULL }, /* D1 */
78 { pre_dl, post_display }, /* Dl */
79 { pre_bd, post_display }, /* Bd */
80 { NULL, NULL }, /* Ed */
81 { pre_bl, post_bl }, /* Bl */
82 { NULL, NULL }, /* El */
83 { NULL, NULL }, /* It */
84 { NULL, NULL }, /* Ad */
85 { NULL, NULL }, /* An */
86 { NULL, post_ar }, /* Ar */
87 { NULL, NULL }, /* Cd */
88 { NULL, NULL }, /* Cm */
89 { NULL, NULL }, /* Dv */
90 { NULL, NULL }, /* Er */
91 { NULL, NULL }, /* Ev */
92 { NULL, post_std }, /* Ex */
93 { NULL, NULL }, /* Fa */
94 { NULL, NULL }, /* Fd */
95 { NULL, NULL }, /* Fl */
96 { NULL, NULL }, /* Fn */
97 { NULL, NULL }, /* Ft */
98 { NULL, NULL }, /* Ic */
99 { NULL, NULL }, /* In */
100 { NULL, NULL }, /* Li */
101 { NULL, NULL }, /* Nd */
102 { NULL, post_nm }, /* Nm */
103 { NULL, NULL }, /* Op */
104 { NULL, NULL }, /* Ot */
105 { NULL, post_pa }, /* Pa */
106 { NULL, post_std }, /* Rv */
107 { NULL, post_st }, /* St */
108 { NULL, NULL }, /* Va */
109 { NULL, NULL }, /* Vt */
110 { NULL, NULL }, /* Xr */
111 { NULL, NULL }, /* %A */
112 { NULL, NULL }, /* %B */
113 { NULL, NULL }, /* %D */
114 { NULL, NULL }, /* %I */
115 { NULL, NULL }, /* %J */
116 { NULL, NULL }, /* %N */
117 { NULL, NULL }, /* %O */
118 { NULL, NULL }, /* %P */
119 { NULL, NULL }, /* %R */
120 { NULL, NULL }, /* %T */
121 { NULL, NULL }, /* %V */
122 { NULL, NULL }, /* Ac */
123 { NULL, NULL }, /* Ao */
124 { NULL, NULL }, /* Aq */
125 { NULL, post_at }, /* At */
126 { NULL, NULL }, /* Bc */
127 { NULL, NULL }, /* Bf */
128 { NULL, NULL }, /* Bo */
129 { NULL, NULL }, /* Bq */
130 { NULL, NULL }, /* Bsx */
131 { NULL, NULL }, /* Bx */
132 { NULL, NULL }, /* Db */
133 { NULL, NULL }, /* Dc */
134 { NULL, NULL }, /* Do */
135 { NULL, NULL }, /* Dq */
136 { NULL, NULL }, /* Ec */
137 { NULL, NULL }, /* Ef */
138 { NULL, NULL }, /* Em */
139 { NULL, NULL }, /* Eo */
140 { NULL, NULL }, /* Fx */
141 { NULL, NULL }, /* Ms */
142 { NULL, NULL }, /* No */
143 { NULL, NULL }, /* Ns */
144 { NULL, NULL }, /* Nx */
145 { NULL, NULL }, /* Ox */
146 { NULL, NULL }, /* Pc */
147 { NULL, NULL }, /* Pf */
148 { NULL, NULL }, /* Po */
149 { NULL, NULL }, /* Pq */
150 { NULL, NULL }, /* Qc */
151 { NULL, NULL }, /* Ql */
152 { NULL, NULL }, /* Qo */
153 { NULL, NULL }, /* Qq */
154 { NULL, NULL }, /* Re */
155 { NULL, post_rs }, /* Rs */
156 { NULL, NULL }, /* Sc */
157 { NULL, NULL }, /* So */
158 { NULL, NULL }, /* Sq */
159 { NULL, NULL }, /* Sm */
160 { NULL, NULL }, /* Sx */
161 { NULL, NULL }, /* Sy */
162 { NULL, NULL }, /* Tn */
163 { NULL, NULL }, /* Ux */
164 { NULL, NULL }, /* Xc */
165 { NULL, NULL }, /* Xo */
166 { NULL, NULL }, /* Fo */
167 { NULL, NULL }, /* Fc */
168 { NULL, NULL }, /* Oo */
169 { NULL, NULL }, /* Oc */
170 { NULL, NULL }, /* Bk */
171 { NULL, NULL }, /* Ek */
172 { NULL, NULL }, /* Bt */
173 { NULL, NULL }, /* Hf */
174 { NULL, NULL }, /* Fr */
175 { NULL, NULL }, /* Ud */
176 { NULL, post_lb }, /* Lb */
177 { NULL, NULL }, /* Lp */
178 { NULL, NULL }, /* Lk */
179 { NULL, NULL }, /* Mt */
180 { NULL, NULL }, /* Brq */
181 { NULL, NULL }, /* Bro */
182 { NULL, NULL }, /* Brc */
183 { NULL, NULL }, /* %C */
184 { NULL, NULL }, /* Es */
185 { NULL, NULL }, /* En */
186 { NULL, NULL }, /* Dx */
187 { NULL, NULL }, /* %Q */
188 { NULL, NULL }, /* br */
189 { NULL, NULL }, /* sp */
190 { NULL, NULL }, /* %U */
195 static const int rsord[RSORD_MAX] = {
214 mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
226 if (NULL == mdoc_actions[n->tok].pre)
228 return((*mdoc_actions[n->tok].pre)(m, n));
233 mdoc_action_post(struct mdoc *m)
236 if (MDOC_ACTED & m->last->flags)
238 m->last->flags |= MDOC_ACTED;
240 switch (m->last->type) {
249 if (NULL == mdoc_actions[m->last->tok].post)
251 return((*mdoc_actions[m->last->tok].post)(m, m->last));
256 * Concatenate sibling nodes together. All siblings must be of type
257 * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
261 concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
266 for ( ; n; n = n->next) {
267 assert(MDOC_TEXT == n->type);
268 if (strlcat(p, n->string, sz) >= sz)
269 return(mdoc_nerr(m, n, ETOOLONG));
272 if (strlcat(p, " ", sz) >= sz)
273 return(mdoc_nerr(m, n, ETOOLONG));
281 * Macros accepting `-std' as an argument have the name of the current
282 * document (`Nm') filled in as the argument if it's not provided.
287 struct mdoc_node *nn;
293 m->next = MDOC_NEXT_CHILD;
294 assert(m->meta.name);
295 if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
303 * The `Nm' macro's first use sets the name of the document. See also
313 if ( ! concat(m, buf, n->child, BUFSIZ))
315 m->meta.name = mandoc_strdup(buf);
321 * Look up the value of `Lb' for matching predefined strings. If it has
322 * one, then substitute the current value for the formatted value. Note
323 * that the lookup may fail (we can provide arbitrary strings).
333 assert(MDOC_TEXT == n->child->type);
334 p = mdoc_a2lib(n->child->string);
337 free(n->child->string);
338 n->child->string = mandoc_strdup(p);
342 sz = strlen(n->child->string) +
343 2 + strlen("\\(lqlibrary\\(rq");
344 buf = mandoc_malloc(sz);
345 snprintf(buf, sz, "library \\(lq%s\\(rq", n->child->string);
346 free(n->child->string);
347 n->child->string = buf;
353 * Substitute the value of `St' for the corresponding formatted string.
354 * We're guaranteed that this exists (it's been verified during the
363 assert(MDOC_TEXT == n->child->type);
364 p = mdoc_a2st(n->child->string);
366 free(n->child->string);
367 n->child->string = mandoc_strdup(p);
373 * Look up the standard string in a table. We know that it exists from
374 * the validation phase, so assert on failure. If a standard key wasn't
375 * supplied, supply the default ``AT&T UNIX''.
380 struct mdoc_node *nn;
384 assert(MDOC_TEXT == n->child->type);
385 p = mdoc_a2att(n->child->string);
387 free(n->child->string);
388 n->child->string = mandoc_strdup(p);
393 m->next = MDOC_NEXT_CHILD;
394 if ( ! mdoc_word_alloc(m, nn->line, nn->pos, "AT&T UNIX"))
402 * Mark the current section. The ``named'' section (lastnamed) is set
403 * whenever the current section isn't a custom section--we use this to
404 * keep track of section ordering. Also check that the section is
405 * allowed within the document's manual section.
413 if (MDOC_HEAD != n->type)
416 if ( ! concat(m, buf, n->child, BUFSIZ))
418 sec = mdoc_atosec(buf);
419 if (SEC_CUSTOM != sec)
422 /* Some sections only live in certain manual sections. */
424 switch ((m->lastsec = sec)) {
425 case (SEC_RETURN_VALUES):
428 switch (m->meta.msec) {
436 return(mdoc_nwarn(m, n, EBADSEC));
447 * Parse out the contents of `Dt'. See in-line documentation for how we
448 * handle the various fields of this macro.
453 struct mdoc_node *nn;
465 m->meta.title = m->meta.vol = m->meta.arch = NULL;
469 * --> title = unknown, volume = local, msec = 0, arch = NULL
472 if (NULL == (nn = n->child)) {
473 /* XXX: make these macro values. */
474 m->meta.title = mandoc_strdup("unknown");
475 m->meta.vol = mandoc_strdup("local");
476 return(post_prol(m, n));
479 /* Handles: `.Dt TITLE'
480 * --> title = TITLE, volume = local, msec = 0, arch = NULL
483 m->meta.title = mandoc_strdup(nn->string);
485 if (NULL == (nn = nn->next)) {
486 /* XXX: make this a macro value. */
487 m->meta.vol = mandoc_strdup("local");
488 return(post_prol(m, n));
491 /* Handles: `.Dt TITLE SEC'
492 * --> title = TITLE, volume = SEC is msec ?
493 * format(msec) : SEC,
494 * msec = SEC is msec ? atoi(msec) : 0,
498 cp = mdoc_a2msec(nn->string);
500 /* FIXME: where is strtonum!? */
501 m->meta.vol = mandoc_strdup(cp);
502 lval = strtol(nn->string, &ep, 10);
503 if (nn->string[0] != '\0' && *ep == '\0')
504 m->meta.msec = (int)lval;
506 m->meta.vol = mandoc_strdup(nn->string);
508 if (NULL == (nn = nn->next))
509 return(post_prol(m, n));
511 /* Handles: `.Dt TITLE SEC VOL'
512 * --> title = TITLE, volume = VOL is vol ?
514 * VOL is arch ? format(arch) :
518 cp = mdoc_a2vol(nn->string);
521 m->meta.vol = mandoc_strdup(cp);
523 cp = mdoc_a2arch(nn->string);
526 m->meta.vol = mandoc_strdup(nn->string);
528 m->meta.arch = mandoc_strdup(cp);
531 /* Ignore any subsequent parameters... */
532 /* FIXME: warn about subsequent parameters. */
534 return(post_prol(m, n));
539 * Set the operating system by way of the `Os' macro. Note that if an
540 * argument isn't provided and -DOSNAME="\"foo\"" is provided during
541 * compilation, this value will be used instead of filling in "sysname
542 * release" from uname().
549 struct utsname utsname;
555 if ( ! concat(m, buf, n->child, BUFSIZ))
558 if ('\0' == buf[0]) {
560 if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ)
561 return(mdoc_nerr(m, n, EUTSNAME));
563 if (-1 == uname(&utsname))
564 return(mdoc_nerr(m, n, EUTSNAME));
565 if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ)
566 return(mdoc_nerr(m, n, ETOOLONG));
567 if (strlcat(buf, " ", 64) >= BUFSIZ)
568 return(mdoc_nerr(m, n, ETOOLONG));
569 if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ)
570 return(mdoc_nerr(m, n, ETOOLONG));
574 m->meta.os = mandoc_strdup(buf);
575 return(post_prol(m, n));
580 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
581 * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
582 * argument has NOT been provided. See post_bl_width() for converting
586 post_bl_tagwidth(POST_ARGS)
588 struct mdoc_node *nn;
593 /* Defaults to ten ens. */
595 sz = 10; /* XXX: make this a macro value. */
599 assert(MDOC_BLOCK == nn->type);
600 assert(MDOC_It == nn->tok);
601 nn = nn->head->child;
602 if (MDOC_TEXT != nn->type) {
603 sz = mdoc_macro2len(nn->tok);
605 if ( ! mdoc_nwarn(m, n, ENOWIDTH))
610 sz = strlen(nn->string) + 1;
613 snprintf(buf, NUMSIZ, "%zun", sz);
616 * We have to dynamically add this to the macro's argument list.
617 * We're guaranteed that a MDOC_Width doesn't already exist.
622 i = (int)(nn->args->argc)++;
624 nn->args->argv = mandoc_realloc(nn->args->argv,
625 nn->args->argc * sizeof(struct mdoc_argv));
627 nn->args->argv[i].arg = MDOC_Width;
628 nn->args->argv[i].line = n->line;
629 nn->args->argv[i].pos = n->pos;
630 nn->args->argv[i].sz = 1;
631 nn->args->argv[i].value = mandoc_malloc(sizeof(char *));
632 nn->args->argv[i].value[0] = mandoc_strdup(buf);
638 * Calculate the real width of a list from the -width string, which may
639 * contain a macro (with a known default width), a literal string, or a
643 post_bl_width(POST_ARGS)
653 for (i = 0; i < (int)n->args->argc; i++)
654 if (MDOC_Width == n->args->argv[i].arg)
657 if (i == (int)n->args->argc)
659 p = n->args->argv[i].value[0];
662 * If the value to -width is a macro, then we re-write it to be
663 * the macro's width as set in share/tmac/mdoc/doc-common.
666 if (0 == strcmp(p, "Ds"))
667 /* XXX: make into a macro. */
669 else if (MDOC_MAX == (tok = mdoc_hash_find(p)))
671 else if (0 == (width = mdoc_macro2len(tok)))
672 return(mdoc_nwarn(m, n, ENOWIDTH));
674 /* The value already exists: free and reallocate it. */
676 snprintf(buf, NUMSIZ, "%zun", width);
677 free(n->args->argv[i].value[0]);
678 n->args->argv[i].value[0] = mandoc_strdup(buf);
684 * Do processing for -column lists, which can have two distinct styles
685 * of invocation. Merge this two styles into a consistent form.
689 post_bl_head(POST_ARGS)
692 struct mdoc_node *np, *nn, *nnp;
694 if (NULL == n->child)
700 for (c = 0; c < (int)np->args->argc; c++)
701 if (MDOC_Column == np->args->argv[c].arg)
704 if (c == (int)np->args->argc)
706 assert(0 == np->args->argv[c].sz);
709 * Accomodate for new-style groff column syntax. Shuffle the
710 * child nodes, all of which must be TEXT, as arguments for the
711 * column field. Then, delete the head children.
714 np->args->argv[c].sz = (size_t)n->nchild;
715 np->args->argv[c].value = mandoc_malloc
716 ((size_t)n->nchild * sizeof(char *));
718 for (i = 0, nn = n->child; nn; i++) {
719 np->args->argv[c].value[i] = nn->string;
737 if (MDOC_HEAD == n->type)
738 return(post_bl_head(m, n));
739 if (MDOC_BLOCK != n->type)
743 * These are fairly complicated, so we've broken them into two
744 * functions. post_bl_tagwidth() is called when a -tag is
745 * specified, but no -width (it must be guessed). The second
746 * when a -width is specified (macro indicators must be
747 * rewritten into real lengths).
750 len = (int)(n->args ? n->args->argc : 0);
752 for (r = i = 0; i < len; i++) {
753 if (MDOC_Tag == n->args->argv[i].arg)
755 if (MDOC_Width == n->args->argv[i].arg)
759 if (r & (1 << 0) && ! (r & (1 << 1))) {
760 if ( ! post_bl_tagwidth(m, n))
762 } else if (r & (1 << 1))
763 if ( ! post_bl_width(m, n))
771 * The `Pa' macro defaults to a tilde if no value is provided as an
777 struct mdoc_node *np;
783 m->next = MDOC_NEXT_CHILD;
784 /* XXX: make into macro value. */
785 if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
793 * The `Ar' macro defaults to two strings "file ..." if no value is
794 * provided as an argument.
799 struct mdoc_node *np;
805 m->next = MDOC_NEXT_CHILD;
806 /* XXX: make into macro values. */
807 if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
809 if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
817 * Parse the date field in `Dd'.
824 if ( ! concat(m, buf, n->child, DATESIZ))
827 m->meta.date = mandoc_a2time
828 (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
830 if (0 == m->meta.date) {
831 if ( ! mdoc_nwarn(m, n, EBADDATE))
833 m->meta.date = time(NULL);
836 return(post_prol(m, n));
841 * Remove prologue macros from the document after they're processed.
842 * The final document uses mdoc_meta for these values and discards the
848 struct mdoc_node *np;
850 if (n->parent->child == n)
851 n->parent->child = n->prev;
853 n->prev->next = NULL;
856 assert(NULL == n->next);
860 m->next = MDOC_NEXT_SIBLING;
863 m->next = MDOC_NEXT_CHILD;
866 mdoc_node_freelist(np);
868 if (m->meta.title && m->meta.date && m->meta.os)
869 m->flags |= MDOC_PBODY;
876 * Trigger a literal context.
882 if (MDOC_BODY == n->type)
883 m->flags |= MDOC_LITERAL;
895 * Make sure that an empty offset produces an 8n length space as
896 * stipulated by mdoc.samples.
900 for (i = 0; i < (int)n->args->argc; i++) {
901 if (MDOC_Offset != n->args->argv[i].arg)
903 if (n->args->argv[i].sz)
905 assert(1 == n->args->refcnt);
906 /* If no value set, length of <string>. */
907 n->args->argv[i].sz++;
908 n->args->argv[i].value = mandoc_malloc(sizeof(char *));
909 n->args->argv[i].value[0] = mandoc_strdup("8n");
921 return(MDOC_BLOCK == n->type ? pre_offset(m, n) : 1);
930 if (MDOC_BLOCK == n->type)
931 return(pre_offset(m, n));
932 if (MDOC_BODY != n->type)
935 /* Enter literal context if `Bd -literal' or `-unfilled'. */
937 for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
938 if (MDOC_Literal == n->args->argv[i].arg)
939 m->flags |= MDOC_LITERAL;
940 else if (MDOC_Unfilled == n->args->argv[i].arg)
941 m->flags |= MDOC_LITERAL;
948 post_display(POST_ARGS)
951 if (MDOC_BODY == n->type)
952 m->flags &= ~MDOC_LITERAL;
962 for (i = 0; i < RSORD_MAX; i++)
975 struct mdoc_node *nn, *next, *prev;
978 if (MDOC_BLOCK != n->type)
981 assert(n->body->child);
982 for (next = NULL, nn = n->body->child->next; nn; nn = next) {
983 o = order_rs(nn->tok);
985 /* Remove `nn' from the chain. */
988 next->prev = nn->prev;
992 prev->next = nn->next;
994 nn->prev = nn->next = NULL;
997 * Scan back until we reach a node that's ordered before
998 * us, then set ourselves as being the next.
1000 for ( ; prev; prev = prev->prev)
1001 if (order_rs(prev->tok) <= o)
1007 prev->next->prev = nn;
1008 nn->next = prev->next;
1013 n->body->child->prev = nn;
1014 nn->next = n->body->child;
1015 n->body->child = nn;