]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
f4cff48694c5b9f5921a3b79f82465167bae13d4
1 /* $Id: mdoc_action.c,v 1.86 2010/11/30 10:32:05 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_tagwidth(POST_ARGS
);
56 static int post_bl_width(POST_ARGS
);
57 static int post_dd(POST_ARGS
);
58 static int post_dt(POST_ARGS
);
59 static int post_os(POST_ARGS
);
60 static int post_prol(POST_ARGS
);
61 static int post_std(POST_ARGS
);
63 static const struct actions mdoc_actions
[MDOC_MAX
] = {
64 { NULL
, NULL
}, /* Ap */
65 { NULL
, post_dd
}, /* Dd */
66 { NULL
, post_dt
}, /* Dt */
67 { NULL
, post_os
}, /* Os */
68 { NULL
, NULL
}, /* Sh */
69 { NULL
, NULL
}, /* Ss */
70 { NULL
, NULL
}, /* Pp */
71 { NULL
, NULL
}, /* D1 */
72 { NULL
, NULL
}, /* Dl */
73 { NULL
, NULL
}, /* Bd */
74 { NULL
, NULL
}, /* Ed */
75 { NULL
, post_bl
}, /* Bl */
76 { NULL
, NULL
}, /* El */
77 { NULL
, NULL
}, /* It */
78 { NULL
, NULL
}, /* Ad */
79 { NULL
, NULL
}, /* An */
80 { NULL
, NULL
}, /* Ar */
81 { NULL
, NULL
}, /* Cd */
82 { NULL
, NULL
}, /* Cm */
83 { NULL
, NULL
}, /* Dv */
84 { NULL
, NULL
}, /* Er */
85 { NULL
, NULL
}, /* Ev */
86 { NULL
, post_std
}, /* Ex */
87 { NULL
, NULL
}, /* Fa */
88 { NULL
, NULL
}, /* Fd */
89 { NULL
, NULL
}, /* Fl */
90 { NULL
, NULL
}, /* Fn */
91 { NULL
, NULL
}, /* Ft */
92 { NULL
, NULL
}, /* Ic */
93 { NULL
, NULL
}, /* In */
94 { NULL
, NULL
}, /* Li */
95 { NULL
, NULL
}, /* Nd */
96 { NULL
, NULL
}, /* Nm */
97 { NULL
, NULL
}, /* Op */
98 { NULL
, NULL
}, /* Ot */
99 { NULL
, NULL
}, /* Pa */
100 { NULL
, post_std
}, /* Rv */
101 { NULL
, NULL
}, /* St */
102 { NULL
, NULL
}, /* Va */
103 { NULL
, NULL
}, /* Vt */
104 { NULL
, NULL
}, /* Xr */
105 { NULL
, NULL
}, /* %A */
106 { NULL
, NULL
}, /* %B */
107 { NULL
, NULL
}, /* %D */
108 { NULL
, NULL
}, /* %I */
109 { NULL
, NULL
}, /* %J */
110 { NULL
, NULL
}, /* %N */
111 { NULL
, NULL
}, /* %O */
112 { NULL
, NULL
}, /* %P */
113 { NULL
, NULL
}, /* %R */
114 { NULL
, NULL
}, /* %T */
115 { NULL
, NULL
}, /* %V */
116 { NULL
, NULL
}, /* Ac */
117 { NULL
, NULL
}, /* Ao */
118 { NULL
, NULL
}, /* Aq */
119 { NULL
, NULL
}, /* At */
120 { NULL
, NULL
}, /* Bc */
121 { NULL
, NULL
}, /* Bf */
122 { NULL
, NULL
}, /* Bo */
123 { NULL
, NULL
}, /* Bq */
124 { NULL
, NULL
}, /* Bsx */
125 { NULL
, NULL
}, /* Bx */
126 { NULL
, NULL
}, /* Db */
127 { NULL
, NULL
}, /* Dc */
128 { NULL
, NULL
}, /* Do */
129 { NULL
, NULL
}, /* Dq */
130 { NULL
, NULL
}, /* Ec */
131 { NULL
, NULL
}, /* Ef */
132 { NULL
, NULL
}, /* Em */
133 { NULL
, NULL
}, /* Eo */
134 { NULL
, NULL
}, /* Fx */
135 { NULL
, NULL
}, /* Ms */
136 { NULL
, NULL
}, /* No */
137 { NULL
, NULL
}, /* Ns */
138 { NULL
, NULL
}, /* Nx */
139 { NULL
, NULL
}, /* Ox */
140 { NULL
, NULL
}, /* Pc */
141 { NULL
, NULL
}, /* Pf */
142 { NULL
, NULL
}, /* Po */
143 { NULL
, NULL
}, /* Pq */
144 { NULL
, NULL
}, /* Qc */
145 { NULL
, NULL
}, /* Ql */
146 { NULL
, NULL
}, /* Qo */
147 { NULL
, NULL
}, /* Qq */
148 { NULL
, NULL
}, /* Re */
149 { NULL
, NULL
}, /* Rs */
150 { NULL
, NULL
}, /* Sc */
151 { NULL
, NULL
}, /* So */
152 { NULL
, NULL
}, /* Sq */
153 { NULL
, NULL
}, /* Sm */
154 { NULL
, NULL
}, /* Sx */
155 { NULL
, NULL
}, /* Sy */
156 { NULL
, NULL
}, /* Tn */
157 { NULL
, NULL
}, /* Ux */
158 { NULL
, NULL
}, /* Xc */
159 { NULL
, NULL
}, /* Xo */
160 { NULL
, NULL
}, /* Fo */
161 { NULL
, NULL
}, /* Fc */
162 { NULL
, NULL
}, /* Oo */
163 { NULL
, NULL
}, /* Oc */
164 { NULL
, NULL
}, /* Bk */
165 { NULL
, NULL
}, /* Ek */
166 { NULL
, NULL
}, /* Bt */
167 { NULL
, NULL
}, /* Hf */
168 { NULL
, NULL
}, /* Fr */
169 { NULL
, NULL
}, /* Ud */
170 { NULL
, NULL
}, /* Lb */
171 { NULL
, NULL
}, /* Lp */
172 { NULL
, NULL
}, /* Lk */
173 { NULL
, NULL
}, /* Mt */
174 { NULL
, NULL
}, /* Brq */
175 { NULL
, NULL
}, /* Bro */
176 { NULL
, NULL
}, /* Brc */
177 { NULL
, NULL
}, /* %C */
178 { NULL
, NULL
}, /* Es */
179 { NULL
, NULL
}, /* En */
180 { NULL
, NULL
}, /* Dx */
181 { NULL
, NULL
}, /* %Q */
182 { NULL
, NULL
}, /* br */
183 { NULL
, NULL
}, /* sp */
184 { NULL
, NULL
}, /* %U */
185 { NULL
, NULL
}, /* Ta */
190 mdoc_action_pre(struct mdoc
*m
, struct mdoc_node
*n
)
202 if (NULL
== mdoc_actions
[n
->tok
].pre
)
204 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
209 mdoc_action_post(struct mdoc
*m
)
212 if (MDOC_ACTED
& m
->last
->flags
)
214 m
->last
->flags
|= MDOC_ACTED
;
216 switch (m
->last
->type
) {
225 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
227 return((*mdoc_actions
[m
->last
->tok
].post
)(m
, m
->last
));
232 * Concatenate sibling nodes together. All siblings must be of type
233 * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
237 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
242 for ( ; n
; n
= n
->next
) {
243 assert(MDOC_TEXT
== n
->type
);
245 * XXX: yes, these can technically be resized, but it's
246 * highly unlikely that we're going to get here, so let
249 if (strlcat(p
, n
->string
, sz
) >= sz
) {
250 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
255 if (strlcat(p
, " ", sz
) >= sz
) {
256 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
266 * Macros accepting `-std' as an argument have the name of the current
267 * document (`Nm') filled in as the argument if it's not provided.
272 struct mdoc_node
*nn
;
276 if (NULL
== m
->meta
.name
)
280 m
->next
= MDOC_NEXT_CHILD
;
282 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, m
->meta
.name
))
289 * Parse out the contents of `Dt'. See in-line documentation for how we
290 * handle the various fields of this macro.
295 struct mdoc_node
*nn
;
305 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
307 * --> title = unknown, volume = local, msec = 0, arch = NULL
310 if (NULL
== (nn
= n
->child
)) {
311 /* XXX: make these macro values. */
312 /* FIXME: warn about missing values. */
313 m
->meta
.title
= mandoc_strdup("UNKNOWN");
314 m
->meta
.vol
= mandoc_strdup("LOCAL");
315 m
->meta
.msec
= mandoc_strdup("1");
316 return(post_prol(m
, n
));
319 /* Handles: `.Dt TITLE'
320 * --> title = TITLE, volume = local, msec = 0, arch = NULL
323 m
->meta
.title
= mandoc_strdup
324 ('\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
326 if (NULL
== (nn
= nn
->next
)) {
327 /* FIXME: warn about missing msec. */
328 /* XXX: make this a macro value. */
329 m
->meta
.vol
= mandoc_strdup("LOCAL");
330 m
->meta
.msec
= mandoc_strdup("1");
331 return(post_prol(m
, n
));
334 /* Handles: `.Dt TITLE SEC'
335 * --> title = TITLE, volume = SEC is msec ?
336 * format(msec) : SEC,
337 * msec = SEC is msec ? atoi(msec) : 0,
341 cp
= mdoc_a2msec(nn
->string
);
343 m
->meta
.vol
= mandoc_strdup(cp
);
344 m
->meta
.msec
= mandoc_strdup(nn
->string
);
345 } else if (mdoc_nmsg(m
, n
, MANDOCERR_BADMSEC
)) {
346 m
->meta
.vol
= mandoc_strdup(nn
->string
);
347 m
->meta
.msec
= mandoc_strdup(nn
->string
);
351 if (NULL
== (nn
= nn
->next
))
352 return(post_prol(m
, n
));
354 /* Handles: `.Dt TITLE SEC VOL'
355 * --> title = TITLE, volume = VOL is vol ?
357 * VOL is arch ? format(arch) :
361 cp
= mdoc_a2vol(nn
->string
);
364 m
->meta
.vol
= mandoc_strdup(cp
);
366 /* FIXME: warn about bad arch. */
367 cp
= mdoc_a2arch(nn
->string
);
370 m
->meta
.vol
= mandoc_strdup(nn
->string
);
372 m
->meta
.arch
= mandoc_strdup(cp
);
375 /* Ignore any subsequent parameters... */
376 /* FIXME: warn about subsequent parameters. */
378 return(post_prol(m
, n
));
383 * Set the operating system by way of the `Os' macro. Note that if an
384 * argument isn't provided and -DOSNAME="\"foo\"" is provided during
385 * compilation, this value will be used instead of filling in "sysname
386 * release" from uname().
393 struct utsname utsname
;
399 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
402 /* XXX: yes, these can all be dynamically-adjusted buffers, but
403 * it's really not worth the extra hackery.
406 if ('\0' == buf
[0]) {
408 if (strlcat(buf
, OSNAME
, BUFSIZ
) >= BUFSIZ
) {
409 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
413 if (-1 == uname(&utsname
))
414 return(mdoc_nmsg(m
, n
, MANDOCERR_UTSNAME
));
416 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
417 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
420 if (strlcat(buf
, " ", 64) >= BUFSIZ
) {
421 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
424 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
425 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
431 m
->meta
.os
= mandoc_strdup(buf
);
432 return(post_prol(m
, n
));
437 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
438 * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
439 * argument has NOT been provided. See post_bl_width() for converting
443 post_bl_tagwidth(POST_ARGS
)
445 struct mdoc_node
*nn
;
452 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
453 if (MDOC_It
!= nn
->tok
)
456 assert(MDOC_BLOCK
== nn
->type
);
457 nn
= nn
->head
->child
;
460 /* No -width for .Bl and first .It is emtpy */
461 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
466 if (MDOC_TEXT
== nn
->type
) {
467 sz
= strlen(nn
->string
) + 1;
471 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
473 else if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
479 /* Defaults to ten ens. */
481 snprintf(buf
, NUMSIZ
, "%zun", sz
);
484 * We have to dynamically add this to the macro's argument list.
485 * We're guaranteed that a MDOC_Width doesn't already exist.
489 i
= (int)(n
->args
->argc
)++;
491 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
492 n
->args
->argc
* sizeof(struct mdoc_argv
));
494 n
->args
->argv
[i
].arg
= MDOC_Width
;
495 n
->args
->argv
[i
].line
= n
->line
;
496 n
->args
->argv
[i
].pos
= n
->pos
;
497 n
->args
->argv
[i
].sz
= 1;
498 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
499 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
502 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
508 * Calculate the real width of a list from the -width string, which may
509 * contain a macro (with a known default width), a literal string, or a
513 post_bl_width(POST_ARGS
)
521 * If the value to -width is a macro, then we re-write it to be
522 * the macro's width as set in share/tmac/mdoc/doc-common.
525 if (0 == strcmp(n
->data
.Bl
->width
, "Ds"))
527 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->data
.Bl
->width
)))
529 else if (0 == (width
= mdoc_macro2len(tok
)))
530 return(mdoc_nmsg(m
, n
, MANDOCERR_BADWIDTH
));
532 /* The value already exists: free and reallocate it. */
536 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
537 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
540 assert(i
< (int)n
->args
->argc
);
542 snprintf(buf
, NUMSIZ
, "%zun", width
);
543 free(n
->args
->argv
[i
].value
[0]);
544 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
547 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
556 if (MDOC_BLOCK
!= n
->type
)
560 * These are fairly complicated, so we've broken them into two
561 * functions. post_bl_tagwidth() is called when a -tag is
562 * specified, but no -width (it must be guessed). The second
563 * when a -width is specified (macro indicators must be
564 * rewritten into real lengths).
567 if (LIST_tag
== n
->data
.Bl
->type
&& NULL
== n
->data
.Bl
->width
) {
568 if ( ! post_bl_tagwidth(m
, n
))
570 } else if (NULL
!= n
->data
.Bl
->width
) {
571 if ( ! post_bl_width(m
, n
))
576 assert(n
->data
.Bl
->width
);
581 * Parse the date field in `Dd'.
588 if (NULL
== n
->child
) {
589 m
->meta
.date
= time(NULL
);
590 return(post_prol(m
, n
));
593 if ( ! concat(m
, buf
, n
->child
, DATESIZ
))
596 m
->meta
.date
= mandoc_a2time
597 (MTIME_MDOCDATE
| MTIME_CANONICAL
, buf
);
599 if (0 == m
->meta
.date
) {
600 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_BADDATE
))
602 m
->meta
.date
= time(NULL
);
605 return(post_prol(m
, n
));
610 * Remove prologue macros from the document after they're processed.
611 * The final document uses mdoc_meta for these values and discards the
618 mdoc_node_delete(m
, n
);
619 if (m
->meta
.title
&& m
->meta
.date
&& m
->meta
.os
)
620 m
->flags
|= MDOC_PBODY
;