]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.83 2010/11/29 14:56:43 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_nm(POST_ARGS
);
62 static int post_os(POST_ARGS
);
63 static int post_pa(POST_ARGS
);
64 static int post_prol(POST_ARGS
);
65 static int post_st(POST_ARGS
);
66 static int post_std(POST_ARGS
);
68 static int pre_bd(PRE_ARGS
);
69 static int pre_dl(PRE_ARGS
);
71 static const struct actions mdoc_actions
[MDOC_MAX
] = {
72 { NULL
, NULL
}, /* Ap */
73 { NULL
, post_dd
}, /* Dd */
74 { NULL
, post_dt
}, /* Dt */
75 { NULL
, post_os
}, /* Os */
76 { NULL
, NULL
}, /* Sh */
77 { NULL
, NULL
}, /* Ss */
78 { NULL
, NULL
}, /* Pp */
79 { NULL
, NULL
}, /* D1 */
80 { pre_dl
, post_display
}, /* Dl */
81 { pre_bd
, post_display
}, /* Bd */
82 { NULL
, NULL
}, /* Ed */
83 { NULL
, post_bl
}, /* Bl */
84 { NULL
, NULL
}, /* El */
85 { NULL
, NULL
}, /* It */
86 { NULL
, NULL
}, /* Ad */
87 { NULL
, NULL
}, /* An */
88 { NULL
, NULL
}, /* Ar */
89 { NULL
, NULL
}, /* Cd */
90 { NULL
, NULL
}, /* Cm */
91 { NULL
, NULL
}, /* Dv */
92 { NULL
, NULL
}, /* Er */
93 { NULL
, NULL
}, /* Ev */
94 { NULL
, post_std
}, /* Ex */
95 { NULL
, NULL
}, /* Fa */
96 { NULL
, NULL
}, /* Fd */
97 { NULL
, NULL
}, /* Fl */
98 { NULL
, NULL
}, /* Fn */
99 { NULL
, NULL
}, /* Ft */
100 { NULL
, NULL
}, /* Ic */
101 { NULL
, NULL
}, /* In */
102 { NULL
, NULL
}, /* Li */
103 { NULL
, NULL
}, /* Nd */
104 { NULL
, post_nm
}, /* Nm */
105 { NULL
, NULL
}, /* Op */
106 { NULL
, NULL
}, /* Ot */
107 { NULL
, post_pa
}, /* Pa */
108 { NULL
, post_std
}, /* Rv */
109 { NULL
, post_st
}, /* St */
110 { NULL
, NULL
}, /* Va */
111 { NULL
, NULL
}, /* Vt */
112 { NULL
, NULL
}, /* Xr */
113 { NULL
, NULL
}, /* %A */
114 { NULL
, NULL
}, /* %B */
115 { NULL
, NULL
}, /* %D */
116 { NULL
, NULL
}, /* %I */
117 { NULL
, NULL
}, /* %J */
118 { NULL
, NULL
}, /* %N */
119 { NULL
, NULL
}, /* %O */
120 { NULL
, NULL
}, /* %P */
121 { NULL
, NULL
}, /* %R */
122 { NULL
, NULL
}, /* %T */
123 { NULL
, NULL
}, /* %V */
124 { NULL
, NULL
}, /* Ac */
125 { NULL
, NULL
}, /* Ao */
126 { NULL
, NULL
}, /* Aq */
127 { NULL
, NULL
}, /* At */
128 { NULL
, NULL
}, /* Bc */
129 { NULL
, NULL
}, /* Bf */
130 { NULL
, NULL
}, /* Bo */
131 { NULL
, NULL
}, /* Bq */
132 { NULL
, NULL
}, /* Bsx */
133 { NULL
, NULL
}, /* Bx */
134 { NULL
, NULL
}, /* Db */
135 { NULL
, NULL
}, /* Dc */
136 { NULL
, NULL
}, /* Do */
137 { NULL
, NULL
}, /* Dq */
138 { NULL
, NULL
}, /* Ec */
139 { NULL
, NULL
}, /* Ef */
140 { NULL
, NULL
}, /* Em */
141 { NULL
, NULL
}, /* Eo */
142 { NULL
, NULL
}, /* Fx */
143 { NULL
, NULL
}, /* Ms */
144 { NULL
, NULL
}, /* No */
145 { NULL
, NULL
}, /* Ns */
146 { NULL
, NULL
}, /* Nx */
147 { NULL
, NULL
}, /* Ox */
148 { NULL
, NULL
}, /* Pc */
149 { NULL
, NULL
}, /* Pf */
150 { NULL
, NULL
}, /* Po */
151 { NULL
, NULL
}, /* Pq */
152 { NULL
, NULL
}, /* Qc */
153 { NULL
, NULL
}, /* Ql */
154 { NULL
, NULL
}, /* Qo */
155 { NULL
, NULL
}, /* Qq */
156 { NULL
, NULL
}, /* Re */
157 { NULL
, NULL
}, /* Rs */
158 { NULL
, NULL
}, /* Sc */
159 { NULL
, NULL
}, /* So */
160 { NULL
, NULL
}, /* Sq */
161 { NULL
, NULL
}, /* Sm */
162 { NULL
, NULL
}, /* Sx */
163 { NULL
, NULL
}, /* Sy */
164 { NULL
, NULL
}, /* Tn */
165 { NULL
, NULL
}, /* Ux */
166 { NULL
, NULL
}, /* Xc */
167 { NULL
, NULL
}, /* Xo */
168 { NULL
, NULL
}, /* Fo */
169 { NULL
, NULL
}, /* Fc */
170 { NULL
, NULL
}, /* Oo */
171 { NULL
, NULL
}, /* Oc */
172 { NULL
, NULL
}, /* Bk */
173 { NULL
, NULL
}, /* Ek */
174 { NULL
, NULL
}, /* Bt */
175 { NULL
, NULL
}, /* Hf */
176 { NULL
, NULL
}, /* Fr */
177 { NULL
, NULL
}, /* Ud */
178 { NULL
, NULL
}, /* Lb */
179 { NULL
, NULL
}, /* Lp */
180 { NULL
, NULL
}, /* Lk */
181 { NULL
, NULL
}, /* Mt */
182 { NULL
, NULL
}, /* Brq */
183 { NULL
, NULL
}, /* Bro */
184 { NULL
, NULL
}, /* Brc */
185 { NULL
, NULL
}, /* %C */
186 { NULL
, NULL
}, /* Es */
187 { NULL
, NULL
}, /* En */
188 { NULL
, NULL
}, /* Dx */
189 { NULL
, NULL
}, /* %Q */
190 { NULL
, NULL
}, /* br */
191 { NULL
, NULL
}, /* sp */
192 { NULL
, NULL
}, /* %U */
193 { NULL
, NULL
}, /* Ta */
198 static const enum mdoct rsord
[RSORD_MAX
] = {
217 mdoc_action_pre(struct mdoc
*m
, struct mdoc_node
*n
)
229 if (NULL
== mdoc_actions
[n
->tok
].pre
)
231 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
236 mdoc_action_post(struct mdoc
*m
)
239 if (MDOC_ACTED
& m
->last
->flags
)
241 m
->last
->flags
|= MDOC_ACTED
;
243 switch (m
->last
->type
) {
252 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
254 return((*mdoc_actions
[m
->last
->tok
].post
)(m
, m
->last
));
259 * Concatenate sibling nodes together. All siblings must be of type
260 * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
264 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
269 for ( ; n
; n
= n
->next
) {
270 assert(MDOC_TEXT
== n
->type
);
272 * XXX: yes, these can technically be resized, but it's
273 * highly unlikely that we're going to get here, so let
276 if (strlcat(p
, n
->string
, sz
) >= sz
) {
277 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
282 if (strlcat(p
, " ", sz
) >= sz
) {
283 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
293 * Macros accepting `-std' as an argument have the name of the current
294 * document (`Nm') filled in as the argument if it's not provided.
299 struct mdoc_node
*nn
;
303 if (NULL
== m
->meta
.name
)
307 m
->next
= MDOC_NEXT_CHILD
;
309 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, m
->meta
.name
))
317 * The `Nm' macro's first use sets the name of the document. See also
327 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
329 m
->meta
.name
= mandoc_strdup(buf
);
334 * Substitute the value of `St' for the corresponding formatted string.
335 * We're guaranteed that this exists (it's been verified during the
344 assert(MDOC_TEXT
== n
->child
->type
);
345 p
= mdoc_a2st(n
->child
->string
);
347 free(n
->child
->string
);
348 n
->child
->string
= mandoc_strdup(p
);
355 * Parse out the contents of `Dt'. See in-line documentation for how we
356 * handle the various fields of this macro.
361 struct mdoc_node
*nn
;
371 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
373 * --> title = unknown, volume = local, msec = 0, arch = NULL
376 if (NULL
== (nn
= n
->child
)) {
377 /* XXX: make these macro values. */
378 /* FIXME: warn about missing values. */
379 m
->meta
.title
= mandoc_strdup("UNKNOWN");
380 m
->meta
.vol
= mandoc_strdup("LOCAL");
381 m
->meta
.msec
= mandoc_strdup("1");
382 return(post_prol(m
, n
));
385 /* Handles: `.Dt TITLE'
386 * --> title = TITLE, volume = local, msec = 0, arch = NULL
389 m
->meta
.title
= mandoc_strdup
390 ('\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
392 if (NULL
== (nn
= nn
->next
)) {
393 /* FIXME: warn about missing msec. */
394 /* XXX: make this a macro value. */
395 m
->meta
.vol
= mandoc_strdup("LOCAL");
396 m
->meta
.msec
= mandoc_strdup("1");
397 return(post_prol(m
, n
));
400 /* Handles: `.Dt TITLE SEC'
401 * --> title = TITLE, volume = SEC is msec ?
402 * format(msec) : SEC,
403 * msec = SEC is msec ? atoi(msec) : 0,
407 cp
= mdoc_a2msec(nn
->string
);
409 m
->meta
.vol
= mandoc_strdup(cp
);
410 m
->meta
.msec
= mandoc_strdup(nn
->string
);
411 } else if (mdoc_nmsg(m
, n
, MANDOCERR_BADMSEC
)) {
412 m
->meta
.vol
= mandoc_strdup(nn
->string
);
413 m
->meta
.msec
= mandoc_strdup(nn
->string
);
417 if (NULL
== (nn
= nn
->next
))
418 return(post_prol(m
, n
));
420 /* Handles: `.Dt TITLE SEC VOL'
421 * --> title = TITLE, volume = VOL is vol ?
423 * VOL is arch ? format(arch) :
427 cp
= mdoc_a2vol(nn
->string
);
430 m
->meta
.vol
= mandoc_strdup(cp
);
432 /* FIXME: warn about bad arch. */
433 cp
= mdoc_a2arch(nn
->string
);
436 m
->meta
.vol
= mandoc_strdup(nn
->string
);
438 m
->meta
.arch
= mandoc_strdup(cp
);
441 /* Ignore any subsequent parameters... */
442 /* FIXME: warn about subsequent parameters. */
444 return(post_prol(m
, n
));
449 * Set the operating system by way of the `Os' macro. Note that if an
450 * argument isn't provided and -DOSNAME="\"foo\"" is provided during
451 * compilation, this value will be used instead of filling in "sysname
452 * release" from uname().
459 struct utsname utsname
;
465 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
468 /* XXX: yes, these can all be dynamically-adjusted buffers, but
469 * it's really not worth the extra hackery.
472 if ('\0' == buf
[0]) {
474 if (strlcat(buf
, OSNAME
, BUFSIZ
) >= BUFSIZ
) {
475 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
479 if (-1 == uname(&utsname
))
480 return(mdoc_nmsg(m
, n
, MANDOCERR_UTSNAME
));
482 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
483 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
486 if (strlcat(buf
, " ", 64) >= BUFSIZ
) {
487 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
490 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
491 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
497 m
->meta
.os
= mandoc_strdup(buf
);
498 return(post_prol(m
, n
));
503 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
504 * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
505 * argument has NOT been provided. See post_bl_width() for converting
509 post_bl_tagwidth(POST_ARGS
)
511 struct mdoc_node
*nn
;
518 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
519 if (MDOC_It
!= nn
->tok
)
522 assert(MDOC_BLOCK
== nn
->type
);
523 nn
= nn
->head
->child
;
526 /* No -width for .Bl and first .It is emtpy */
527 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
532 if (MDOC_TEXT
== nn
->type
) {
533 sz
= strlen(nn
->string
) + 1;
537 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
539 else if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
545 /* Defaults to ten ens. */
547 snprintf(buf
, NUMSIZ
, "%zun", sz
);
550 * We have to dynamically add this to the macro's argument list.
551 * We're guaranteed that a MDOC_Width doesn't already exist.
555 i
= (int)(n
->args
->argc
)++;
557 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
558 n
->args
->argc
* sizeof(struct mdoc_argv
));
560 n
->args
->argv
[i
].arg
= MDOC_Width
;
561 n
->args
->argv
[i
].line
= n
->line
;
562 n
->args
->argv
[i
].pos
= n
->pos
;
563 n
->args
->argv
[i
].sz
= 1;
564 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
565 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
568 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
574 * Calculate the real width of a list from the -width string, which may
575 * contain a macro (with a known default width), a literal string, or a
579 post_bl_width(POST_ARGS
)
587 * If the value to -width is a macro, then we re-write it to be
588 * the macro's width as set in share/tmac/mdoc/doc-common.
591 if (0 == strcmp(n
->data
.Bl
->width
, "Ds"))
593 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->data
.Bl
->width
)))
595 else if (0 == (width
= mdoc_macro2len(tok
)))
596 return(mdoc_nmsg(m
, n
, MANDOCERR_BADWIDTH
));
598 /* The value already exists: free and reallocate it. */
602 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
603 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
606 assert(i
< (int)n
->args
->argc
);
608 snprintf(buf
, NUMSIZ
, "%zun", width
);
609 free(n
->args
->argv
[i
].value
[0]);
610 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
613 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
619 * Do processing for -column lists, which can have two distinct styles
620 * of invocation. Merge this two styles into a consistent form.
624 post_bl_head(POST_ARGS
)
627 struct mdoc_node
*np
, *nn
, *nnp
;
629 if (LIST_column
!= n
->data
.Bl
->type
)
631 else if (NULL
== n
->child
)
637 for (c
= 0; c
< (int)np
->args
->argc
; c
++)
638 if (MDOC_Column
== np
->args
->argv
[c
].arg
)
641 assert(c
< (int)np
->args
->argc
);
642 assert(0 == np
->args
->argv
[c
].sz
);
645 * Accomodate for new-style groff column syntax. Shuffle the
646 * child nodes, all of which must be TEXT, as arguments for the
647 * column field. Then, delete the head children.
650 np
->args
->argv
[c
].sz
= (size_t)n
->nchild
;
651 np
->args
->argv
[c
].value
= mandoc_malloc
652 ((size_t)n
->nchild
* sizeof(char *));
654 n
->data
.Bl
->ncols
= np
->args
->argv
[c
].sz
;
655 n
->data
.Bl
->cols
= (const char **)np
->args
->argv
[c
].value
;
657 for (i
= 0, nn
= n
->child
; nn
; i
++) {
658 np
->args
->argv
[c
].value
[i
] = nn
->string
;
662 mdoc_node_delete(NULL
, nnp
);
675 if (MDOC_HEAD
== n
->type
)
676 return(post_bl_head(m
, n
));
677 if (MDOC_BLOCK
!= n
->type
)
681 * These are fairly complicated, so we've broken them into two
682 * functions. post_bl_tagwidth() is called when a -tag is
683 * specified, but no -width (it must be guessed). The second
684 * when a -width is specified (macro indicators must be
685 * rewritten into real lengths).
688 if (LIST_tag
== n
->data
.Bl
->type
&& NULL
== n
->data
.Bl
->width
) {
689 if ( ! post_bl_tagwidth(m
, n
))
691 } else if (NULL
!= n
->data
.Bl
->width
) {
692 if ( ! post_bl_width(m
, n
))
697 assert(n
->data
.Bl
->width
);
703 * The `Pa' macro defaults to a tilde if no value is provided as an
709 struct mdoc_node
*np
;
715 m
->next
= MDOC_NEXT_CHILD
;
716 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, "~"))
724 * Parse the date field in `Dd'.
731 if (NULL
== n
->child
) {
732 m
->meta
.date
= time(NULL
);
733 return(post_prol(m
, n
));
736 if ( ! concat(m
, buf
, n
->child
, DATESIZ
))
739 m
->meta
.date
= mandoc_a2time
740 (MTIME_MDOCDATE
| MTIME_CANONICAL
, buf
);
742 if (0 == m
->meta
.date
) {
743 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_BADDATE
))
745 m
->meta
.date
= time(NULL
);
748 return(post_prol(m
, n
));
753 * Remove prologue macros from the document after they're processed.
754 * The final document uses mdoc_meta for these values and discards the
761 mdoc_node_delete(m
, n
);
762 if (m
->meta
.title
&& m
->meta
.date
&& m
->meta
.os
)
763 m
->flags
|= MDOC_PBODY
;
769 * Trigger a literal context.
775 if (MDOC_BODY
== n
->type
)
776 m
->flags
|= MDOC_LITERAL
;
785 if (MDOC_BODY
!= n
->type
)
789 if (DISP_literal
== n
->data
.Bd
->type
)
790 m
->flags
|= MDOC_LITERAL
;
791 if (DISP_unfilled
== n
->data
.Bd
->type
)
792 m
->flags
|= MDOC_LITERAL
;
799 post_display(POST_ARGS
)
802 if (MDOC_BODY
== n
->type
)
803 m
->flags
&= ~MDOC_LITERAL
;