]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.85 2010/11/29 16:06:46 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_os(POST_ARGS
);
61 static int post_prol(POST_ARGS
);
62 static int post_std(POST_ARGS
);
64 static const struct actions mdoc_actions
[MDOC_MAX
] = {
65 { NULL
, NULL
}, /* Ap */
66 { NULL
, post_dd
}, /* Dd */
67 { NULL
, post_dt
}, /* Dt */
68 { NULL
, post_os
}, /* Os */
69 { NULL
, NULL
}, /* Sh */
70 { NULL
, NULL
}, /* Ss */
71 { NULL
, NULL
}, /* Pp */
72 { NULL
, NULL
}, /* D1 */
73 { NULL
, NULL
}, /* Dl */
74 { NULL
, NULL
}, /* Bd */
75 { NULL
, NULL
}, /* Ed */
76 { NULL
, post_bl
}, /* Bl */
77 { NULL
, NULL
}, /* El */
78 { NULL
, NULL
}, /* It */
79 { NULL
, NULL
}, /* Ad */
80 { NULL
, NULL
}, /* An */
81 { NULL
, NULL
}, /* Ar */
82 { NULL
, NULL
}, /* Cd */
83 { NULL
, NULL
}, /* Cm */
84 { NULL
, NULL
}, /* Dv */
85 { NULL
, NULL
}, /* Er */
86 { NULL
, NULL
}, /* Ev */
87 { NULL
, post_std
}, /* Ex */
88 { NULL
, NULL
}, /* Fa */
89 { NULL
, NULL
}, /* Fd */
90 { NULL
, NULL
}, /* Fl */
91 { NULL
, NULL
}, /* Fn */
92 { NULL
, NULL
}, /* Ft */
93 { NULL
, NULL
}, /* Ic */
94 { NULL
, NULL
}, /* In */
95 { NULL
, NULL
}, /* Li */
96 { NULL
, NULL
}, /* Nd */
97 { NULL
, NULL
}, /* Nm */
98 { NULL
, NULL
}, /* Op */
99 { NULL
, NULL
}, /* Ot */
100 { NULL
, NULL
}, /* Pa */
101 { NULL
, post_std
}, /* Rv */
102 { NULL
, NULL
}, /* St */
103 { NULL
, NULL
}, /* Va */
104 { NULL
, NULL
}, /* Vt */
105 { NULL
, NULL
}, /* Xr */
106 { NULL
, NULL
}, /* %A */
107 { NULL
, NULL
}, /* %B */
108 { NULL
, NULL
}, /* %D */
109 { NULL
, NULL
}, /* %I */
110 { NULL
, NULL
}, /* %J */
111 { NULL
, NULL
}, /* %N */
112 { NULL
, NULL
}, /* %O */
113 { NULL
, NULL
}, /* %P */
114 { NULL
, NULL
}, /* %R */
115 { NULL
, NULL
}, /* %T */
116 { NULL
, NULL
}, /* %V */
117 { NULL
, NULL
}, /* Ac */
118 { NULL
, NULL
}, /* Ao */
119 { NULL
, NULL
}, /* Aq */
120 { NULL
, NULL
}, /* At */
121 { NULL
, NULL
}, /* Bc */
122 { NULL
, NULL
}, /* Bf */
123 { NULL
, NULL
}, /* Bo */
124 { NULL
, NULL
}, /* Bq */
125 { NULL
, NULL
}, /* Bsx */
126 { NULL
, NULL
}, /* Bx */
127 { NULL
, NULL
}, /* Db */
128 { NULL
, NULL
}, /* Dc */
129 { NULL
, NULL
}, /* Do */
130 { NULL
, NULL
}, /* Dq */
131 { NULL
, NULL
}, /* Ec */
132 { NULL
, NULL
}, /* Ef */
133 { NULL
, NULL
}, /* Em */
134 { NULL
, NULL
}, /* Eo */
135 { NULL
, NULL
}, /* Fx */
136 { NULL
, NULL
}, /* Ms */
137 { NULL
, NULL
}, /* No */
138 { NULL
, NULL
}, /* Ns */
139 { NULL
, NULL
}, /* Nx */
140 { NULL
, NULL
}, /* Ox */
141 { NULL
, NULL
}, /* Pc */
142 { NULL
, NULL
}, /* Pf */
143 { NULL
, NULL
}, /* Po */
144 { NULL
, NULL
}, /* Pq */
145 { NULL
, NULL
}, /* Qc */
146 { NULL
, NULL
}, /* Ql */
147 { NULL
, NULL
}, /* Qo */
148 { NULL
, NULL
}, /* Qq */
149 { NULL
, NULL
}, /* Re */
150 { NULL
, NULL
}, /* Rs */
151 { NULL
, NULL
}, /* Sc */
152 { NULL
, NULL
}, /* So */
153 { NULL
, NULL
}, /* Sq */
154 { NULL
, NULL
}, /* Sm */
155 { NULL
, NULL
}, /* Sx */
156 { NULL
, NULL
}, /* Sy */
157 { NULL
, NULL
}, /* Tn */
158 { NULL
, NULL
}, /* Ux */
159 { NULL
, NULL
}, /* Xc */
160 { NULL
, NULL
}, /* Xo */
161 { NULL
, NULL
}, /* Fo */
162 { NULL
, NULL
}, /* Fc */
163 { NULL
, NULL
}, /* Oo */
164 { NULL
, NULL
}, /* Oc */
165 { NULL
, NULL
}, /* Bk */
166 { NULL
, NULL
}, /* Ek */
167 { NULL
, NULL
}, /* Bt */
168 { NULL
, NULL
}, /* Hf */
169 { NULL
, NULL
}, /* Fr */
170 { NULL
, NULL
}, /* Ud */
171 { NULL
, NULL
}, /* Lb */
172 { NULL
, NULL
}, /* Lp */
173 { NULL
, NULL
}, /* Lk */
174 { NULL
, NULL
}, /* Mt */
175 { NULL
, NULL
}, /* Brq */
176 { NULL
, NULL
}, /* Bro */
177 { NULL
, NULL
}, /* Brc */
178 { NULL
, NULL
}, /* %C */
179 { NULL
, NULL
}, /* Es */
180 { NULL
, NULL
}, /* En */
181 { NULL
, NULL
}, /* Dx */
182 { NULL
, NULL
}, /* %Q */
183 { NULL
, NULL
}, /* br */
184 { NULL
, NULL
}, /* sp */
185 { NULL
, NULL
}, /* %U */
186 { NULL
, NULL
}, /* Ta */
191 mdoc_action_pre(struct mdoc
*m
, struct mdoc_node
*n
)
203 if (NULL
== mdoc_actions
[n
->tok
].pre
)
205 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
210 mdoc_action_post(struct mdoc
*m
)
213 if (MDOC_ACTED
& m
->last
->flags
)
215 m
->last
->flags
|= MDOC_ACTED
;
217 switch (m
->last
->type
) {
226 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
228 return((*mdoc_actions
[m
->last
->tok
].post
)(m
, m
->last
));
233 * Concatenate sibling nodes together. All siblings must be of type
234 * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
238 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
243 for ( ; n
; n
= n
->next
) {
244 assert(MDOC_TEXT
== n
->type
);
246 * XXX: yes, these can technically be resized, but it's
247 * highly unlikely that we're going to get here, so let
250 if (strlcat(p
, n
->string
, sz
) >= sz
) {
251 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
256 if (strlcat(p
, " ", sz
) >= sz
) {
257 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
267 * Macros accepting `-std' as an argument have the name of the current
268 * document (`Nm') filled in as the argument if it's not provided.
273 struct mdoc_node
*nn
;
277 if (NULL
== m
->meta
.name
)
281 m
->next
= MDOC_NEXT_CHILD
;
283 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, m
->meta
.name
))
290 * Parse out the contents of `Dt'. See in-line documentation for how we
291 * handle the various fields of this macro.
296 struct mdoc_node
*nn
;
306 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
308 * --> title = unknown, volume = local, msec = 0, arch = NULL
311 if (NULL
== (nn
= n
->child
)) {
312 /* XXX: make these macro values. */
313 /* FIXME: warn about missing values. */
314 m
->meta
.title
= mandoc_strdup("UNKNOWN");
315 m
->meta
.vol
= mandoc_strdup("LOCAL");
316 m
->meta
.msec
= mandoc_strdup("1");
317 return(post_prol(m
, n
));
320 /* Handles: `.Dt TITLE'
321 * --> title = TITLE, volume = local, msec = 0, arch = NULL
324 m
->meta
.title
= mandoc_strdup
325 ('\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
327 if (NULL
== (nn
= nn
->next
)) {
328 /* FIXME: warn about missing msec. */
329 /* XXX: make this a macro value. */
330 m
->meta
.vol
= mandoc_strdup("LOCAL");
331 m
->meta
.msec
= mandoc_strdup("1");
332 return(post_prol(m
, n
));
335 /* Handles: `.Dt TITLE SEC'
336 * --> title = TITLE, volume = SEC is msec ?
337 * format(msec) : SEC,
338 * msec = SEC is msec ? atoi(msec) : 0,
342 cp
= mdoc_a2msec(nn
->string
);
344 m
->meta
.vol
= mandoc_strdup(cp
);
345 m
->meta
.msec
= mandoc_strdup(nn
->string
);
346 } else if (mdoc_nmsg(m
, n
, MANDOCERR_BADMSEC
)) {
347 m
->meta
.vol
= mandoc_strdup(nn
->string
);
348 m
->meta
.msec
= mandoc_strdup(nn
->string
);
352 if (NULL
== (nn
= nn
->next
))
353 return(post_prol(m
, n
));
355 /* Handles: `.Dt TITLE SEC VOL'
356 * --> title = TITLE, volume = VOL is vol ?
358 * VOL is arch ? format(arch) :
362 cp
= mdoc_a2vol(nn
->string
);
365 m
->meta
.vol
= mandoc_strdup(cp
);
367 /* FIXME: warn about bad arch. */
368 cp
= mdoc_a2arch(nn
->string
);
371 m
->meta
.vol
= mandoc_strdup(nn
->string
);
373 m
->meta
.arch
= mandoc_strdup(cp
);
376 /* Ignore any subsequent parameters... */
377 /* FIXME: warn about subsequent parameters. */
379 return(post_prol(m
, n
));
384 * Set the operating system by way of the `Os' macro. Note that if an
385 * argument isn't provided and -DOSNAME="\"foo\"" is provided during
386 * compilation, this value will be used instead of filling in "sysname
387 * release" from uname().
394 struct utsname utsname
;
400 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
403 /* XXX: yes, these can all be dynamically-adjusted buffers, but
404 * it's really not worth the extra hackery.
407 if ('\0' == buf
[0]) {
409 if (strlcat(buf
, OSNAME
, BUFSIZ
) >= BUFSIZ
) {
410 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
414 if (-1 == uname(&utsname
))
415 return(mdoc_nmsg(m
, n
, MANDOCERR_UTSNAME
));
417 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
418 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
421 if (strlcat(buf
, " ", 64) >= BUFSIZ
) {
422 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
425 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
426 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
432 m
->meta
.os
= mandoc_strdup(buf
);
433 return(post_prol(m
, n
));
438 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
439 * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
440 * argument has NOT been provided. See post_bl_width() for converting
444 post_bl_tagwidth(POST_ARGS
)
446 struct mdoc_node
*nn
;
453 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
454 if (MDOC_It
!= nn
->tok
)
457 assert(MDOC_BLOCK
== nn
->type
);
458 nn
= nn
->head
->child
;
461 /* No -width for .Bl and first .It is emtpy */
462 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
467 if (MDOC_TEXT
== nn
->type
) {
468 sz
= strlen(nn
->string
) + 1;
472 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
474 else if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
480 /* Defaults to ten ens. */
482 snprintf(buf
, NUMSIZ
, "%zun", sz
);
485 * We have to dynamically add this to the macro's argument list.
486 * We're guaranteed that a MDOC_Width doesn't already exist.
490 i
= (int)(n
->args
->argc
)++;
492 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
493 n
->args
->argc
* sizeof(struct mdoc_argv
));
495 n
->args
->argv
[i
].arg
= MDOC_Width
;
496 n
->args
->argv
[i
].line
= n
->line
;
497 n
->args
->argv
[i
].pos
= n
->pos
;
498 n
->args
->argv
[i
].sz
= 1;
499 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
500 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
503 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
509 * Calculate the real width of a list from the -width string, which may
510 * contain a macro (with a known default width), a literal string, or a
514 post_bl_width(POST_ARGS
)
522 * If the value to -width is a macro, then we re-write it to be
523 * the macro's width as set in share/tmac/mdoc/doc-common.
526 if (0 == strcmp(n
->data
.Bl
->width
, "Ds"))
528 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->data
.Bl
->width
)))
530 else if (0 == (width
= mdoc_macro2len(tok
)))
531 return(mdoc_nmsg(m
, n
, MANDOCERR_BADWIDTH
));
533 /* The value already exists: free and reallocate it. */
537 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
538 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
541 assert(i
< (int)n
->args
->argc
);
543 snprintf(buf
, NUMSIZ
, "%zun", width
);
544 free(n
->args
->argv
[i
].value
[0]);
545 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
548 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
554 * Do processing for -column lists, which can have two distinct styles
555 * of invocation. Merge this two styles into a consistent form.
559 post_bl_head(POST_ARGS
)
562 struct mdoc_node
*np
, *nn
, *nnp
;
564 if (LIST_column
!= n
->data
.Bl
->type
)
566 else if (NULL
== n
->child
)
572 for (c
= 0; c
< (int)np
->args
->argc
; c
++)
573 if (MDOC_Column
== np
->args
->argv
[c
].arg
)
576 assert(c
< (int)np
->args
->argc
);
577 assert(0 == np
->args
->argv
[c
].sz
);
580 * Accomodate for new-style groff column syntax. Shuffle the
581 * child nodes, all of which must be TEXT, as arguments for the
582 * column field. Then, delete the head children.
585 np
->args
->argv
[c
].sz
= (size_t)n
->nchild
;
586 np
->args
->argv
[c
].value
= mandoc_malloc
587 ((size_t)n
->nchild
* sizeof(char *));
589 n
->data
.Bl
->ncols
= np
->args
->argv
[c
].sz
;
590 n
->data
.Bl
->cols
= (const char **)np
->args
->argv
[c
].value
;
592 for (i
= 0, nn
= n
->child
; nn
; i
++) {
593 np
->args
->argv
[c
].value
[i
] = nn
->string
;
597 mdoc_node_delete(NULL
, nnp
);
610 if (MDOC_HEAD
== n
->type
)
611 return(post_bl_head(m
, n
));
612 if (MDOC_BLOCK
!= n
->type
)
616 * These are fairly complicated, so we've broken them into two
617 * functions. post_bl_tagwidth() is called when a -tag is
618 * specified, but no -width (it must be guessed). The second
619 * when a -width is specified (macro indicators must be
620 * rewritten into real lengths).
623 if (LIST_tag
== n
->data
.Bl
->type
&& NULL
== n
->data
.Bl
->width
) {
624 if ( ! post_bl_tagwidth(m
, n
))
626 } else if (NULL
!= n
->data
.Bl
->width
) {
627 if ( ! post_bl_width(m
, n
))
632 assert(n
->data
.Bl
->width
);
637 * Parse the date field in `Dd'.
644 if (NULL
== n
->child
) {
645 m
->meta
.date
= time(NULL
);
646 return(post_prol(m
, n
));
649 if ( ! concat(m
, buf
, n
->child
, DATESIZ
))
652 m
->meta
.date
= mandoc_a2time
653 (MTIME_MDOCDATE
| MTIME_CANONICAL
, buf
);
655 if (0 == m
->meta
.date
) {
656 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_BADDATE
))
658 m
->meta
.date
= time(NULL
);
661 return(post_prol(m
, n
));
666 * Remove prologue macros from the document after they're processed.
667 * The final document uses mdoc_meta for these values and discards the
674 mdoc_node_delete(m
, n
);
675 if (m
->meta
.title
&& m
->meta
.date
&& m
->meta
.os
)
676 m
->flags
|= MDOC_PBODY
;