]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.82 2010/11/29 14:50:33 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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.
22 #include <sys/utsname.h>
33 #include "libmandoc.h"
36 * FIXME: this file is deprecated. All future "actions" should be
37 * pushed into mdoc_validate.c.
40 #define POST_ARGS struct mdoc *m, struct mdoc_node *n
41 #define PRE_ARGS struct mdoc *m, struct mdoc_node *n
48 int (*post
)(POST_ARGS
);
51 static int concat(struct mdoc
*, char *,
52 const struct mdoc_node
*, size_t);
54 static int post_bl(POST_ARGS
);
55 static int post_bl_head(POST_ARGS
);
56 static int post_bl_tagwidth(POST_ARGS
);
57 static int post_bl_width(POST_ARGS
);
58 static int post_dd(POST_ARGS
);
59 static int post_display(POST_ARGS
);
60 static int post_dt(POST_ARGS
);
61 static int post_lb(POST_ARGS
);
62 static int post_nm(POST_ARGS
);
63 static int post_os(POST_ARGS
);
64 static int post_pa(POST_ARGS
);
65 static int post_prol(POST_ARGS
);
66 static int post_st(POST_ARGS
);
67 static int post_std(POST_ARGS
);
69 static int pre_bd(PRE_ARGS
);
70 static int pre_dl(PRE_ARGS
);
72 static const struct actions mdoc_actions
[MDOC_MAX
] = {
73 { NULL
, NULL
}, /* Ap */
74 { NULL
, post_dd
}, /* Dd */
75 { NULL
, post_dt
}, /* Dt */
76 { NULL
, post_os
}, /* Os */
77 { NULL
, NULL
}, /* Sh */
78 { NULL
, NULL
}, /* Ss */
79 { NULL
, NULL
}, /* Pp */
80 { NULL
, NULL
}, /* D1 */
81 { pre_dl
, post_display
}, /* Dl */
82 { pre_bd
, post_display
}, /* Bd */
83 { NULL
, NULL
}, /* Ed */
84 { NULL
, post_bl
}, /* Bl */
85 { NULL
, NULL
}, /* El */
86 { NULL
, NULL
}, /* It */
87 { NULL
, NULL
}, /* Ad */
88 { NULL
, NULL
}, /* An */
89 { NULL
, NULL
}, /* Ar */
90 { NULL
, NULL
}, /* Cd */
91 { NULL
, NULL
}, /* Cm */
92 { NULL
, NULL
}, /* Dv */
93 { NULL
, NULL
}, /* Er */
94 { NULL
, NULL
}, /* Ev */
95 { NULL
, post_std
}, /* Ex */
96 { NULL
, NULL
}, /* Fa */
97 { NULL
, NULL
}, /* Fd */
98 { NULL
, NULL
}, /* Fl */
99 { NULL
, NULL
}, /* Fn */
100 { NULL
, NULL
}, /* Ft */
101 { NULL
, NULL
}, /* Ic */
102 { NULL
, NULL
}, /* In */
103 { NULL
, NULL
}, /* Li */
104 { NULL
, NULL
}, /* Nd */
105 { NULL
, post_nm
}, /* Nm */
106 { NULL
, NULL
}, /* Op */
107 { NULL
, NULL
}, /* Ot */
108 { NULL
, post_pa
}, /* Pa */
109 { NULL
, post_std
}, /* Rv */
110 { NULL
, post_st
}, /* St */
111 { NULL
, NULL
}, /* Va */
112 { NULL
, NULL
}, /* Vt */
113 { NULL
, NULL
}, /* Xr */
114 { NULL
, NULL
}, /* %A */
115 { NULL
, NULL
}, /* %B */
116 { NULL
, NULL
}, /* %D */
117 { NULL
, NULL
}, /* %I */
118 { NULL
, NULL
}, /* %J */
119 { NULL
, NULL
}, /* %N */
120 { NULL
, NULL
}, /* %O */
121 { NULL
, NULL
}, /* %P */
122 { NULL
, NULL
}, /* %R */
123 { NULL
, NULL
}, /* %T */
124 { NULL
, NULL
}, /* %V */
125 { NULL
, NULL
}, /* Ac */
126 { NULL
, NULL
}, /* Ao */
127 { NULL
, NULL
}, /* Aq */
128 { NULL
, NULL
}, /* At */
129 { NULL
, NULL
}, /* Bc */
130 { NULL
, NULL
}, /* Bf */
131 { NULL
, NULL
}, /* Bo */
132 { NULL
, NULL
}, /* Bq */
133 { NULL
, NULL
}, /* Bsx */
134 { NULL
, NULL
}, /* Bx */
135 { NULL
, NULL
}, /* Db */
136 { NULL
, NULL
}, /* Dc */
137 { NULL
, NULL
}, /* Do */
138 { NULL
, NULL
}, /* Dq */
139 { NULL
, NULL
}, /* Ec */
140 { NULL
, NULL
}, /* Ef */
141 { NULL
, NULL
}, /* Em */
142 { NULL
, NULL
}, /* Eo */
143 { NULL
, NULL
}, /* Fx */
144 { NULL
, NULL
}, /* Ms */
145 { NULL
, NULL
}, /* No */
146 { NULL
, NULL
}, /* Ns */
147 { NULL
, NULL
}, /* Nx */
148 { NULL
, NULL
}, /* Ox */
149 { NULL
, NULL
}, /* Pc */
150 { NULL
, NULL
}, /* Pf */
151 { NULL
, NULL
}, /* Po */
152 { NULL
, NULL
}, /* Pq */
153 { NULL
, NULL
}, /* Qc */
154 { NULL
, NULL
}, /* Ql */
155 { NULL
, NULL
}, /* Qo */
156 { NULL
, NULL
}, /* Qq */
157 { NULL
, NULL
}, /* Re */
158 { NULL
, NULL
}, /* Rs */
159 { NULL
, NULL
}, /* Sc */
160 { NULL
, NULL
}, /* So */
161 { NULL
, NULL
}, /* Sq */
162 { NULL
, NULL
}, /* Sm */
163 { NULL
, NULL
}, /* Sx */
164 { NULL
, NULL
}, /* Sy */
165 { NULL
, NULL
}, /* Tn */
166 { NULL
, NULL
}, /* Ux */
167 { NULL
, NULL
}, /* Xc */
168 { NULL
, NULL
}, /* Xo */
169 { NULL
, NULL
}, /* Fo */
170 { NULL
, NULL
}, /* Fc */
171 { NULL
, NULL
}, /* Oo */
172 { NULL
, NULL
}, /* Oc */
173 { NULL
, NULL
}, /* Bk */
174 { NULL
, NULL
}, /* Ek */
175 { NULL
, NULL
}, /* Bt */
176 { NULL
, NULL
}, /* Hf */
177 { NULL
, NULL
}, /* Fr */
178 { NULL
, NULL
}, /* Ud */
179 { NULL
, post_lb
}, /* Lb */
180 { NULL
, NULL
}, /* Lp */
181 { NULL
, NULL
}, /* Lk */
182 { NULL
, NULL
}, /* Mt */
183 { NULL
, NULL
}, /* Brq */
184 { NULL
, NULL
}, /* Bro */
185 { NULL
, NULL
}, /* Brc */
186 { NULL
, NULL
}, /* %C */
187 { NULL
, NULL
}, /* Es */
188 { NULL
, NULL
}, /* En */
189 { NULL
, NULL
}, /* Dx */
190 { NULL
, NULL
}, /* %Q */
191 { NULL
, NULL
}, /* br */
192 { NULL
, NULL
}, /* sp */
193 { NULL
, NULL
}, /* %U */
194 { NULL
, NULL
}, /* Ta */
199 static const enum mdoct rsord
[RSORD_MAX
] = {
218 mdoc_action_pre(struct mdoc
*m
, struct mdoc_node
*n
)
230 if (NULL
== mdoc_actions
[n
->tok
].pre
)
232 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
237 mdoc_action_post(struct mdoc
*m
)
240 if (MDOC_ACTED
& m
->last
->flags
)
242 m
->last
->flags
|= MDOC_ACTED
;
244 switch (m
->last
->type
) {
253 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
255 return((*mdoc_actions
[m
->last
->tok
].post
)(m
, m
->last
));
260 * Concatenate sibling nodes together. All siblings must be of type
261 * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
265 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
270 for ( ; n
; n
= n
->next
) {
271 assert(MDOC_TEXT
== n
->type
);
273 * XXX: yes, these can technically be resized, but it's
274 * highly unlikely that we're going to get here, so let
277 if (strlcat(p
, n
->string
, sz
) >= sz
) {
278 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
283 if (strlcat(p
, " ", sz
) >= sz
) {
284 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
294 * Macros accepting `-std' as an argument have the name of the current
295 * document (`Nm') filled in as the argument if it's not provided.
300 struct mdoc_node
*nn
;
304 if (NULL
== m
->meta
.name
)
308 m
->next
= MDOC_NEXT_CHILD
;
310 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, m
->meta
.name
))
318 * The `Nm' macro's first use sets the name of the document. See also
328 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
330 m
->meta
.name
= mandoc_strdup(buf
);
336 * Look up the value of `Lb' for matching predefined strings. If it has
337 * one, then substitute the current value for the formatted value. Note
338 * that the lookup may fail (we can provide arbitrary strings).
348 assert(MDOC_TEXT
== n
->child
->type
);
349 p
= mdoc_a2lib(n
->child
->string
);
352 free(n
->child
->string
);
353 n
->child
->string
= mandoc_strdup(p
);
357 sz
= strlen(n
->child
->string
) +
358 2 + strlen("\\(lqlibrary\\(rq");
359 buf
= mandoc_malloc(sz
);
360 snprintf(buf
, sz
, "library \\(lq%s\\(rq", n
->child
->string
);
361 free(n
->child
->string
);
362 n
->child
->string
= buf
;
368 * Substitute the value of `St' for the corresponding formatted string.
369 * We're guaranteed that this exists (it's been verified during the
378 assert(MDOC_TEXT
== n
->child
->type
);
379 p
= mdoc_a2st(n
->child
->string
);
381 free(n
->child
->string
);
382 n
->child
->string
= mandoc_strdup(p
);
389 * Parse out the contents of `Dt'. See in-line documentation for how we
390 * handle the various fields of this macro.
395 struct mdoc_node
*nn
;
405 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
407 * --> title = unknown, volume = local, msec = 0, arch = NULL
410 if (NULL
== (nn
= n
->child
)) {
411 /* XXX: make these macro values. */
412 /* FIXME: warn about missing values. */
413 m
->meta
.title
= mandoc_strdup("UNKNOWN");
414 m
->meta
.vol
= mandoc_strdup("LOCAL");
415 m
->meta
.msec
= mandoc_strdup("1");
416 return(post_prol(m
, n
));
419 /* Handles: `.Dt TITLE'
420 * --> title = TITLE, volume = local, msec = 0, arch = NULL
423 m
->meta
.title
= mandoc_strdup
424 ('\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
426 if (NULL
== (nn
= nn
->next
)) {
427 /* FIXME: warn about missing msec. */
428 /* XXX: make this a macro value. */
429 m
->meta
.vol
= mandoc_strdup("LOCAL");
430 m
->meta
.msec
= mandoc_strdup("1");
431 return(post_prol(m
, n
));
434 /* Handles: `.Dt TITLE SEC'
435 * --> title = TITLE, volume = SEC is msec ?
436 * format(msec) : SEC,
437 * msec = SEC is msec ? atoi(msec) : 0,
441 cp
= mdoc_a2msec(nn
->string
);
443 m
->meta
.vol
= mandoc_strdup(cp
);
444 m
->meta
.msec
= mandoc_strdup(nn
->string
);
445 } else if (mdoc_nmsg(m
, n
, MANDOCERR_BADMSEC
)) {
446 m
->meta
.vol
= mandoc_strdup(nn
->string
);
447 m
->meta
.msec
= mandoc_strdup(nn
->string
);
451 if (NULL
== (nn
= nn
->next
))
452 return(post_prol(m
, n
));
454 /* Handles: `.Dt TITLE SEC VOL'
455 * --> title = TITLE, volume = VOL is vol ?
457 * VOL is arch ? format(arch) :
461 cp
= mdoc_a2vol(nn
->string
);
464 m
->meta
.vol
= mandoc_strdup(cp
);
466 /* FIXME: warn about bad arch. */
467 cp
= mdoc_a2arch(nn
->string
);
470 m
->meta
.vol
= mandoc_strdup(nn
->string
);
472 m
->meta
.arch
= mandoc_strdup(cp
);
475 /* Ignore any subsequent parameters... */
476 /* FIXME: warn about subsequent parameters. */
478 return(post_prol(m
, n
));
483 * Set the operating system by way of the `Os' macro. Note that if an
484 * argument isn't provided and -DOSNAME="\"foo\"" is provided during
485 * compilation, this value will be used instead of filling in "sysname
486 * release" from uname().
493 struct utsname utsname
;
499 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
502 /* XXX: yes, these can all be dynamically-adjusted buffers, but
503 * it's really not worth the extra hackery.
506 if ('\0' == buf
[0]) {
508 if (strlcat(buf
, OSNAME
, BUFSIZ
) >= BUFSIZ
) {
509 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
513 if (-1 == uname(&utsname
))
514 return(mdoc_nmsg(m
, n
, MANDOCERR_UTSNAME
));
516 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
517 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
520 if (strlcat(buf
, " ", 64) >= BUFSIZ
) {
521 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
524 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
525 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
531 m
->meta
.os
= mandoc_strdup(buf
);
532 return(post_prol(m
, n
));
537 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
538 * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
539 * argument has NOT been provided. See post_bl_width() for converting
543 post_bl_tagwidth(POST_ARGS
)
545 struct mdoc_node
*nn
;
552 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
553 if (MDOC_It
!= nn
->tok
)
556 assert(MDOC_BLOCK
== nn
->type
);
557 nn
= nn
->head
->child
;
560 /* No -width for .Bl and first .It is emtpy */
561 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
566 if (MDOC_TEXT
== nn
->type
) {
567 sz
= strlen(nn
->string
) + 1;
571 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
573 else if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
579 /* Defaults to ten ens. */
581 snprintf(buf
, NUMSIZ
, "%zun", sz
);
584 * We have to dynamically add this to the macro's argument list.
585 * We're guaranteed that a MDOC_Width doesn't already exist.
589 i
= (int)(n
->args
->argc
)++;
591 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
592 n
->args
->argc
* sizeof(struct mdoc_argv
));
594 n
->args
->argv
[i
].arg
= MDOC_Width
;
595 n
->args
->argv
[i
].line
= n
->line
;
596 n
->args
->argv
[i
].pos
= n
->pos
;
597 n
->args
->argv
[i
].sz
= 1;
598 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
599 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
602 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
608 * Calculate the real width of a list from the -width string, which may
609 * contain a macro (with a known default width), a literal string, or a
613 post_bl_width(POST_ARGS
)
621 * If the value to -width is a macro, then we re-write it to be
622 * the macro's width as set in share/tmac/mdoc/doc-common.
625 if (0 == strcmp(n
->data
.Bl
->width
, "Ds"))
627 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->data
.Bl
->width
)))
629 else if (0 == (width
= mdoc_macro2len(tok
)))
630 return(mdoc_nmsg(m
, n
, MANDOCERR_BADWIDTH
));
632 /* The value already exists: free and reallocate it. */
636 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
637 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
640 assert(i
< (int)n
->args
->argc
);
642 snprintf(buf
, NUMSIZ
, "%zun", width
);
643 free(n
->args
->argv
[i
].value
[0]);
644 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
647 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
653 * Do processing for -column lists, which can have two distinct styles
654 * of invocation. Merge this two styles into a consistent form.
658 post_bl_head(POST_ARGS
)
661 struct mdoc_node
*np
, *nn
, *nnp
;
663 if (LIST_column
!= n
->data
.Bl
->type
)
665 else if (NULL
== n
->child
)
671 for (c
= 0; c
< (int)np
->args
->argc
; c
++)
672 if (MDOC_Column
== np
->args
->argv
[c
].arg
)
675 assert(c
< (int)np
->args
->argc
);
676 assert(0 == np
->args
->argv
[c
].sz
);
679 * Accomodate for new-style groff column syntax. Shuffle the
680 * child nodes, all of which must be TEXT, as arguments for the
681 * column field. Then, delete the head children.
684 np
->args
->argv
[c
].sz
= (size_t)n
->nchild
;
685 np
->args
->argv
[c
].value
= mandoc_malloc
686 ((size_t)n
->nchild
* sizeof(char *));
688 n
->data
.Bl
->ncols
= np
->args
->argv
[c
].sz
;
689 n
->data
.Bl
->cols
= (const char **)np
->args
->argv
[c
].value
;
691 for (i
= 0, nn
= n
->child
; nn
; i
++) {
692 np
->args
->argv
[c
].value
[i
] = nn
->string
;
696 mdoc_node_delete(NULL
, nnp
);
709 if (MDOC_HEAD
== n
->type
)
710 return(post_bl_head(m
, n
));
711 if (MDOC_BLOCK
!= n
->type
)
715 * These are fairly complicated, so we've broken them into two
716 * functions. post_bl_tagwidth() is called when a -tag is
717 * specified, but no -width (it must be guessed). The second
718 * when a -width is specified (macro indicators must be
719 * rewritten into real lengths).
722 if (LIST_tag
== n
->data
.Bl
->type
&& NULL
== n
->data
.Bl
->width
) {
723 if ( ! post_bl_tagwidth(m
, n
))
725 } else if (NULL
!= n
->data
.Bl
->width
) {
726 if ( ! post_bl_width(m
, n
))
731 assert(n
->data
.Bl
->width
);
737 * The `Pa' macro defaults to a tilde if no value is provided as an
743 struct mdoc_node
*np
;
749 m
->next
= MDOC_NEXT_CHILD
;
750 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, "~"))
758 * Parse the date field in `Dd'.
765 if (NULL
== n
->child
) {
766 m
->meta
.date
= time(NULL
);
767 return(post_prol(m
, n
));
770 if ( ! concat(m
, buf
, n
->child
, DATESIZ
))
773 m
->meta
.date
= mandoc_a2time
774 (MTIME_MDOCDATE
| MTIME_CANONICAL
, buf
);
776 if (0 == m
->meta
.date
) {
777 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_BADDATE
))
779 m
->meta
.date
= time(NULL
);
782 return(post_prol(m
, n
));
787 * Remove prologue macros from the document after they're processed.
788 * The final document uses mdoc_meta for these values and discards the
795 mdoc_node_delete(m
, n
);
796 if (m
->meta
.title
&& m
->meta
.date
&& m
->meta
.os
)
797 m
->flags
|= MDOC_PBODY
;
803 * Trigger a literal context.
809 if (MDOC_BODY
== n
->type
)
810 m
->flags
|= MDOC_LITERAL
;
819 if (MDOC_BODY
!= n
->type
)
823 if (DISP_literal
== n
->data
.Bd
->type
)
824 m
->flags
|= MDOC_LITERAL
;
825 if (DISP_unfilled
== n
->data
.Bd
->type
)
826 m
->flags
|= MDOC_LITERAL
;
833 post_display(POST_ARGS
)
836 if (MDOC_BODY
== n
->type
)
837 m
->flags
&= ~MDOC_LITERAL
;