]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
d7d03ea962cc89c3fdfe1282b7a55666d6a45677
1 /* $Id: mdoc_action.c,v 1.84 2010/11/29 15:45:15 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_dt(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_std(POST_ARGS
);
66 static const struct actions mdoc_actions
[MDOC_MAX
] = {
67 { NULL
, NULL
}, /* Ap */
68 { NULL
, post_dd
}, /* Dd */
69 { NULL
, post_dt
}, /* Dt */
70 { NULL
, post_os
}, /* Os */
71 { NULL
, NULL
}, /* Sh */
72 { NULL
, NULL
}, /* Ss */
73 { NULL
, NULL
}, /* Pp */
74 { NULL
, NULL
}, /* D1 */
75 { NULL
, NULL
}, /* Dl */
76 { NULL
, NULL
}, /* Bd */
77 { NULL
, NULL
}, /* Ed */
78 { NULL
, post_bl
}, /* Bl */
79 { NULL
, NULL
}, /* El */
80 { NULL
, NULL
}, /* It */
81 { NULL
, NULL
}, /* Ad */
82 { NULL
, NULL
}, /* An */
83 { NULL
, NULL
}, /* Ar */
84 { NULL
, NULL
}, /* Cd */
85 { NULL
, NULL
}, /* Cm */
86 { NULL
, NULL
}, /* Dv */
87 { NULL
, NULL
}, /* Er */
88 { NULL
, NULL
}, /* Ev */
89 { NULL
, post_std
}, /* Ex */
90 { NULL
, NULL
}, /* Fa */
91 { NULL
, NULL
}, /* Fd */
92 { NULL
, NULL
}, /* Fl */
93 { NULL
, NULL
}, /* Fn */
94 { NULL
, NULL
}, /* Ft */
95 { NULL
, NULL
}, /* Ic */
96 { NULL
, NULL
}, /* In */
97 { NULL
, NULL
}, /* Li */
98 { NULL
, NULL
}, /* Nd */
99 { NULL
, post_nm
}, /* Nm */
100 { NULL
, NULL
}, /* Op */
101 { NULL
, NULL
}, /* Ot */
102 { NULL
, post_pa
}, /* Pa */
103 { NULL
, post_std
}, /* Rv */
104 { NULL
, NULL
}, /* St */
105 { NULL
, NULL
}, /* Va */
106 { NULL
, NULL
}, /* Vt */
107 { NULL
, NULL
}, /* Xr */
108 { NULL
, NULL
}, /* %A */
109 { NULL
, NULL
}, /* %B */
110 { NULL
, NULL
}, /* %D */
111 { NULL
, NULL
}, /* %I */
112 { NULL
, NULL
}, /* %J */
113 { NULL
, NULL
}, /* %N */
114 { NULL
, NULL
}, /* %O */
115 { NULL
, NULL
}, /* %P */
116 { NULL
, NULL
}, /* %R */
117 { NULL
, NULL
}, /* %T */
118 { NULL
, NULL
}, /* %V */
119 { NULL
, NULL
}, /* Ac */
120 { NULL
, NULL
}, /* Ao */
121 { NULL
, NULL
}, /* Aq */
122 { NULL
, NULL
}, /* At */
123 { NULL
, NULL
}, /* Bc */
124 { NULL
, NULL
}, /* Bf */
125 { NULL
, NULL
}, /* Bo */
126 { NULL
, NULL
}, /* Bq */
127 { NULL
, NULL
}, /* Bsx */
128 { NULL
, NULL
}, /* Bx */
129 { NULL
, NULL
}, /* Db */
130 { NULL
, NULL
}, /* Dc */
131 { NULL
, NULL
}, /* Do */
132 { NULL
, NULL
}, /* Dq */
133 { NULL
, NULL
}, /* Ec */
134 { NULL
, NULL
}, /* Ef */
135 { NULL
, NULL
}, /* Em */
136 { NULL
, NULL
}, /* Eo */
137 { NULL
, NULL
}, /* Fx */
138 { NULL
, NULL
}, /* Ms */
139 { NULL
, NULL
}, /* No */
140 { NULL
, NULL
}, /* Ns */
141 { NULL
, NULL
}, /* Nx */
142 { NULL
, NULL
}, /* Ox */
143 { NULL
, NULL
}, /* Pc */
144 { NULL
, NULL
}, /* Pf */
145 { NULL
, NULL
}, /* Po */
146 { NULL
, NULL
}, /* Pq */
147 { NULL
, NULL
}, /* Qc */
148 { NULL
, NULL
}, /* Ql */
149 { NULL
, NULL
}, /* Qo */
150 { NULL
, NULL
}, /* Qq */
151 { NULL
, NULL
}, /* Re */
152 { NULL
, NULL
}, /* Rs */
153 { NULL
, NULL
}, /* Sc */
154 { NULL
, NULL
}, /* So */
155 { NULL
, NULL
}, /* Sq */
156 { NULL
, NULL
}, /* Sm */
157 { NULL
, NULL
}, /* Sx */
158 { NULL
, NULL
}, /* Sy */
159 { NULL
, NULL
}, /* Tn */
160 { NULL
, NULL
}, /* Ux */
161 { NULL
, NULL
}, /* Xc */
162 { NULL
, NULL
}, /* Xo */
163 { NULL
, NULL
}, /* Fo */
164 { NULL
, NULL
}, /* Fc */
165 { NULL
, NULL
}, /* Oo */
166 { NULL
, NULL
}, /* Oc */
167 { NULL
, NULL
}, /* Bk */
168 { NULL
, NULL
}, /* Ek */
169 { NULL
, NULL
}, /* Bt */
170 { NULL
, NULL
}, /* Hf */
171 { NULL
, NULL
}, /* Fr */
172 { NULL
, NULL
}, /* Ud */
173 { NULL
, NULL
}, /* Lb */
174 { NULL
, NULL
}, /* Lp */
175 { NULL
, NULL
}, /* Lk */
176 { NULL
, NULL
}, /* Mt */
177 { NULL
, NULL
}, /* Brq */
178 { NULL
, NULL
}, /* Bro */
179 { NULL
, NULL
}, /* Brc */
180 { NULL
, NULL
}, /* %C */
181 { NULL
, NULL
}, /* Es */
182 { NULL
, NULL
}, /* En */
183 { NULL
, NULL
}, /* Dx */
184 { NULL
, NULL
}, /* %Q */
185 { NULL
, NULL
}, /* br */
186 { NULL
, NULL
}, /* sp */
187 { NULL
, NULL
}, /* %U */
188 { NULL
, NULL
}, /* Ta */
193 mdoc_action_pre(struct mdoc
*m
, struct mdoc_node
*n
)
205 if (NULL
== mdoc_actions
[n
->tok
].pre
)
207 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
212 mdoc_action_post(struct mdoc
*m
)
215 if (MDOC_ACTED
& m
->last
->flags
)
217 m
->last
->flags
|= MDOC_ACTED
;
219 switch (m
->last
->type
) {
228 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
230 return((*mdoc_actions
[m
->last
->tok
].post
)(m
, m
->last
));
235 * Concatenate sibling nodes together. All siblings must be of type
236 * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
240 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
245 for ( ; n
; n
= n
->next
) {
246 assert(MDOC_TEXT
== n
->type
);
248 * XXX: yes, these can technically be resized, but it's
249 * highly unlikely that we're going to get here, so let
252 if (strlcat(p
, n
->string
, sz
) >= sz
) {
253 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
258 if (strlcat(p
, " ", sz
) >= sz
) {
259 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
269 * Macros accepting `-std' as an argument have the name of the current
270 * document (`Nm') filled in as the argument if it's not provided.
275 struct mdoc_node
*nn
;
279 if (NULL
== m
->meta
.name
)
283 m
->next
= MDOC_NEXT_CHILD
;
285 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, m
->meta
.name
))
293 * The `Nm' macro's first use sets the name of the document. See also
303 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
305 m
->meta
.name
= mandoc_strdup(buf
);
310 * Parse out the contents of `Dt'. See in-line documentation for how we
311 * handle the various fields of this macro.
316 struct mdoc_node
*nn
;
326 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
328 * --> title = unknown, volume = local, msec = 0, arch = NULL
331 if (NULL
== (nn
= n
->child
)) {
332 /* XXX: make these macro values. */
333 /* FIXME: warn about missing values. */
334 m
->meta
.title
= mandoc_strdup("UNKNOWN");
335 m
->meta
.vol
= mandoc_strdup("LOCAL");
336 m
->meta
.msec
= mandoc_strdup("1");
337 return(post_prol(m
, n
));
340 /* Handles: `.Dt TITLE'
341 * --> title = TITLE, volume = local, msec = 0, arch = NULL
344 m
->meta
.title
= mandoc_strdup
345 ('\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
347 if (NULL
== (nn
= nn
->next
)) {
348 /* FIXME: warn about missing msec. */
349 /* XXX: make this a macro value. */
350 m
->meta
.vol
= mandoc_strdup("LOCAL");
351 m
->meta
.msec
= mandoc_strdup("1");
352 return(post_prol(m
, n
));
355 /* Handles: `.Dt TITLE SEC'
356 * --> title = TITLE, volume = SEC is msec ?
357 * format(msec) : SEC,
358 * msec = SEC is msec ? atoi(msec) : 0,
362 cp
= mdoc_a2msec(nn
->string
);
364 m
->meta
.vol
= mandoc_strdup(cp
);
365 m
->meta
.msec
= mandoc_strdup(nn
->string
);
366 } else if (mdoc_nmsg(m
, n
, MANDOCERR_BADMSEC
)) {
367 m
->meta
.vol
= mandoc_strdup(nn
->string
);
368 m
->meta
.msec
= mandoc_strdup(nn
->string
);
372 if (NULL
== (nn
= nn
->next
))
373 return(post_prol(m
, n
));
375 /* Handles: `.Dt TITLE SEC VOL'
376 * --> title = TITLE, volume = VOL is vol ?
378 * VOL is arch ? format(arch) :
382 cp
= mdoc_a2vol(nn
->string
);
385 m
->meta
.vol
= mandoc_strdup(cp
);
387 /* FIXME: warn about bad arch. */
388 cp
= mdoc_a2arch(nn
->string
);
391 m
->meta
.vol
= mandoc_strdup(nn
->string
);
393 m
->meta
.arch
= mandoc_strdup(cp
);
396 /* Ignore any subsequent parameters... */
397 /* FIXME: warn about subsequent parameters. */
399 return(post_prol(m
, n
));
404 * Set the operating system by way of the `Os' macro. Note that if an
405 * argument isn't provided and -DOSNAME="\"foo\"" is provided during
406 * compilation, this value will be used instead of filling in "sysname
407 * release" from uname().
414 struct utsname utsname
;
420 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
423 /* XXX: yes, these can all be dynamically-adjusted buffers, but
424 * it's really not worth the extra hackery.
427 if ('\0' == buf
[0]) {
429 if (strlcat(buf
, OSNAME
, BUFSIZ
) >= BUFSIZ
) {
430 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
434 if (-1 == uname(&utsname
))
435 return(mdoc_nmsg(m
, n
, MANDOCERR_UTSNAME
));
437 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
438 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
441 if (strlcat(buf
, " ", 64) >= BUFSIZ
) {
442 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
445 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
446 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
452 m
->meta
.os
= mandoc_strdup(buf
);
453 return(post_prol(m
, n
));
458 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
459 * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
460 * argument has NOT been provided. See post_bl_width() for converting
464 post_bl_tagwidth(POST_ARGS
)
466 struct mdoc_node
*nn
;
473 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
474 if (MDOC_It
!= nn
->tok
)
477 assert(MDOC_BLOCK
== nn
->type
);
478 nn
= nn
->head
->child
;
481 /* No -width for .Bl and first .It is emtpy */
482 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
487 if (MDOC_TEXT
== nn
->type
) {
488 sz
= strlen(nn
->string
) + 1;
492 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
494 else if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
500 /* Defaults to ten ens. */
502 snprintf(buf
, NUMSIZ
, "%zun", sz
);
505 * We have to dynamically add this to the macro's argument list.
506 * We're guaranteed that a MDOC_Width doesn't already exist.
510 i
= (int)(n
->args
->argc
)++;
512 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
513 n
->args
->argc
* sizeof(struct mdoc_argv
));
515 n
->args
->argv
[i
].arg
= MDOC_Width
;
516 n
->args
->argv
[i
].line
= n
->line
;
517 n
->args
->argv
[i
].pos
= n
->pos
;
518 n
->args
->argv
[i
].sz
= 1;
519 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
520 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
523 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
529 * Calculate the real width of a list from the -width string, which may
530 * contain a macro (with a known default width), a literal string, or a
534 post_bl_width(POST_ARGS
)
542 * If the value to -width is a macro, then we re-write it to be
543 * the macro's width as set in share/tmac/mdoc/doc-common.
546 if (0 == strcmp(n
->data
.Bl
->width
, "Ds"))
548 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->data
.Bl
->width
)))
550 else if (0 == (width
= mdoc_macro2len(tok
)))
551 return(mdoc_nmsg(m
, n
, MANDOCERR_BADWIDTH
));
553 /* The value already exists: free and reallocate it. */
557 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
558 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
561 assert(i
< (int)n
->args
->argc
);
563 snprintf(buf
, NUMSIZ
, "%zun", width
);
564 free(n
->args
->argv
[i
].value
[0]);
565 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
568 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
574 * Do processing for -column lists, which can have two distinct styles
575 * of invocation. Merge this two styles into a consistent form.
579 post_bl_head(POST_ARGS
)
582 struct mdoc_node
*np
, *nn
, *nnp
;
584 if (LIST_column
!= n
->data
.Bl
->type
)
586 else if (NULL
== n
->child
)
592 for (c
= 0; c
< (int)np
->args
->argc
; c
++)
593 if (MDOC_Column
== np
->args
->argv
[c
].arg
)
596 assert(c
< (int)np
->args
->argc
);
597 assert(0 == np
->args
->argv
[c
].sz
);
600 * Accomodate for new-style groff column syntax. Shuffle the
601 * child nodes, all of which must be TEXT, as arguments for the
602 * column field. Then, delete the head children.
605 np
->args
->argv
[c
].sz
= (size_t)n
->nchild
;
606 np
->args
->argv
[c
].value
= mandoc_malloc
607 ((size_t)n
->nchild
* sizeof(char *));
609 n
->data
.Bl
->ncols
= np
->args
->argv
[c
].sz
;
610 n
->data
.Bl
->cols
= (const char **)np
->args
->argv
[c
].value
;
612 for (i
= 0, nn
= n
->child
; nn
; i
++) {
613 np
->args
->argv
[c
].value
[i
] = nn
->string
;
617 mdoc_node_delete(NULL
, nnp
);
630 if (MDOC_HEAD
== n
->type
)
631 return(post_bl_head(m
, n
));
632 if (MDOC_BLOCK
!= n
->type
)
636 * These are fairly complicated, so we've broken them into two
637 * functions. post_bl_tagwidth() is called when a -tag is
638 * specified, but no -width (it must be guessed). The second
639 * when a -width is specified (macro indicators must be
640 * rewritten into real lengths).
643 if (LIST_tag
== n
->data
.Bl
->type
&& NULL
== n
->data
.Bl
->width
) {
644 if ( ! post_bl_tagwidth(m
, n
))
646 } else if (NULL
!= n
->data
.Bl
->width
) {
647 if ( ! post_bl_width(m
, n
))
652 assert(n
->data
.Bl
->width
);
658 * The `Pa' macro defaults to a tilde if no value is provided as an
664 struct mdoc_node
*np
;
670 m
->next
= MDOC_NEXT_CHILD
;
671 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, "~"))
679 * Parse the date field in `Dd'.
686 if (NULL
== n
->child
) {
687 m
->meta
.date
= time(NULL
);
688 return(post_prol(m
, n
));
691 if ( ! concat(m
, buf
, n
->child
, DATESIZ
))
694 m
->meta
.date
= mandoc_a2time
695 (MTIME_MDOCDATE
| MTIME_CANONICAL
, buf
);
697 if (0 == m
->meta
.date
) {
698 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_BADDATE
))
700 m
->meta
.date
= time(NULL
);
703 return(post_prol(m
, n
));
708 * Remove prologue macros from the document after they're processed.
709 * The final document uses mdoc_meta for these values and discards the
716 mdoc_node_delete(m
, n
);
717 if (m
->meta
.title
&& m
->meta
.date
&& m
->meta
.os
)
718 m
->flags
|= MDOC_PBODY
;