]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.81 2010/11/29 13:51:03 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_at(POST_ARGS
);
55 static int post_bl(POST_ARGS
);
56 static int post_bl_head(POST_ARGS
);
57 static int post_bl_tagwidth(POST_ARGS
);
58 static int post_bl_width(POST_ARGS
);
59 static int post_dd(POST_ARGS
);
60 static int post_display(POST_ARGS
);
61 static int post_dt(POST_ARGS
);
62 static int post_lb(POST_ARGS
);
63 static int post_nm(POST_ARGS
);
64 static int post_os(POST_ARGS
);
65 static int post_pa(POST_ARGS
);
66 static int post_prol(POST_ARGS
);
67 static int post_st(POST_ARGS
);
68 static int post_std(POST_ARGS
);
70 static int pre_bd(PRE_ARGS
);
71 static int pre_dl(PRE_ARGS
);
73 static const struct actions mdoc_actions
[MDOC_MAX
] = {
74 { NULL
, NULL
}, /* Ap */
75 { NULL
, post_dd
}, /* Dd */
76 { NULL
, post_dt
}, /* Dt */
77 { NULL
, post_os
}, /* Os */
78 { NULL
, NULL
}, /* Sh */
79 { NULL
, NULL
}, /* Ss */
80 { NULL
, NULL
}, /* Pp */
81 { NULL
, NULL
}, /* D1 */
82 { pre_dl
, post_display
}, /* Dl */
83 { pre_bd
, post_display
}, /* Bd */
84 { NULL
, NULL
}, /* Ed */
85 { NULL
, post_bl
}, /* Bl */
86 { NULL
, NULL
}, /* El */
87 { NULL
, NULL
}, /* It */
88 { NULL
, NULL
}, /* Ad */
89 { NULL
, NULL
}, /* An */
90 { NULL
, NULL
}, /* Ar */
91 { NULL
, NULL
}, /* Cd */
92 { NULL
, NULL
}, /* Cm */
93 { NULL
, NULL
}, /* Dv */
94 { NULL
, NULL
}, /* Er */
95 { NULL
, NULL
}, /* Ev */
96 { NULL
, post_std
}, /* Ex */
97 { NULL
, NULL
}, /* Fa */
98 { NULL
, NULL
}, /* Fd */
99 { NULL
, NULL
}, /* Fl */
100 { NULL
, NULL
}, /* Fn */
101 { NULL
, NULL
}, /* Ft */
102 { NULL
, NULL
}, /* Ic */
103 { NULL
, NULL
}, /* In */
104 { NULL
, NULL
}, /* Li */
105 { NULL
, NULL
}, /* Nd */
106 { NULL
, post_nm
}, /* Nm */
107 { NULL
, NULL
}, /* Op */
108 { NULL
, NULL
}, /* Ot */
109 { NULL
, post_pa
}, /* Pa */
110 { NULL
, post_std
}, /* Rv */
111 { NULL
, post_st
}, /* St */
112 { NULL
, NULL
}, /* Va */
113 { NULL
, NULL
}, /* Vt */
114 { NULL
, NULL
}, /* Xr */
115 { NULL
, NULL
}, /* %A */
116 { NULL
, NULL
}, /* %B */
117 { NULL
, NULL
}, /* %D */
118 { NULL
, NULL
}, /* %I */
119 { NULL
, NULL
}, /* %J */
120 { NULL
, NULL
}, /* %N */
121 { NULL
, NULL
}, /* %O */
122 { NULL
, NULL
}, /* %P */
123 { NULL
, NULL
}, /* %R */
124 { NULL
, NULL
}, /* %T */
125 { NULL
, NULL
}, /* %V */
126 { NULL
, NULL
}, /* Ac */
127 { NULL
, NULL
}, /* Ao */
128 { NULL
, NULL
}, /* Aq */
129 { NULL
, post_at
}, /* At */
130 { NULL
, NULL
}, /* Bc */
131 { NULL
, NULL
}, /* Bf */
132 { NULL
, NULL
}, /* Bo */
133 { NULL
, NULL
}, /* Bq */
134 { NULL
, NULL
}, /* Bsx */
135 { NULL
, NULL
}, /* Bx */
136 { NULL
, NULL
}, /* Db */
137 { NULL
, NULL
}, /* Dc */
138 { NULL
, NULL
}, /* Do */
139 { NULL
, NULL
}, /* Dq */
140 { NULL
, NULL
}, /* Ec */
141 { NULL
, NULL
}, /* Ef */
142 { NULL
, NULL
}, /* Em */
143 { NULL
, NULL
}, /* Eo */
144 { NULL
, NULL
}, /* Fx */
145 { NULL
, NULL
}, /* Ms */
146 { NULL
, NULL
}, /* No */
147 { NULL
, NULL
}, /* Ns */
148 { NULL
, NULL
}, /* Nx */
149 { NULL
, NULL
}, /* Ox */
150 { NULL
, NULL
}, /* Pc */
151 { NULL
, NULL
}, /* Pf */
152 { NULL
, NULL
}, /* Po */
153 { NULL
, NULL
}, /* Pq */
154 { NULL
, NULL
}, /* Qc */
155 { NULL
, NULL
}, /* Ql */
156 { NULL
, NULL
}, /* Qo */
157 { NULL
, NULL
}, /* Qq */
158 { NULL
, NULL
}, /* Re */
159 { NULL
, NULL
}, /* Rs */
160 { NULL
, NULL
}, /* Sc */
161 { NULL
, NULL
}, /* So */
162 { NULL
, NULL
}, /* Sq */
163 { NULL
, NULL
}, /* Sm */
164 { NULL
, NULL
}, /* Sx */
165 { NULL
, NULL
}, /* Sy */
166 { NULL
, NULL
}, /* Tn */
167 { NULL
, NULL
}, /* Ux */
168 { NULL
, NULL
}, /* Xc */
169 { NULL
, NULL
}, /* Xo */
170 { NULL
, NULL
}, /* Fo */
171 { NULL
, NULL
}, /* Fc */
172 { NULL
, NULL
}, /* Oo */
173 { NULL
, NULL
}, /* Oc */
174 { NULL
, NULL
}, /* Bk */
175 { NULL
, NULL
}, /* Ek */
176 { NULL
, NULL
}, /* Bt */
177 { NULL
, NULL
}, /* Hf */
178 { NULL
, NULL
}, /* Fr */
179 { NULL
, NULL
}, /* Ud */
180 { NULL
, post_lb
}, /* Lb */
181 { NULL
, NULL
}, /* Lp */
182 { NULL
, NULL
}, /* Lk */
183 { NULL
, NULL
}, /* Mt */
184 { NULL
, NULL
}, /* Brq */
185 { NULL
, NULL
}, /* Bro */
186 { NULL
, NULL
}, /* Brc */
187 { NULL
, NULL
}, /* %C */
188 { NULL
, NULL
}, /* Es */
189 { NULL
, NULL
}, /* En */
190 { NULL
, NULL
}, /* Dx */
191 { NULL
, NULL
}, /* %Q */
192 { NULL
, NULL
}, /* br */
193 { NULL
, NULL
}, /* sp */
194 { NULL
, NULL
}, /* %U */
195 { NULL
, NULL
}, /* Ta */
200 static const enum mdoct rsord
[RSORD_MAX
] = {
219 mdoc_action_pre(struct mdoc
*m
, struct mdoc_node
*n
)
231 if (NULL
== mdoc_actions
[n
->tok
].pre
)
233 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
238 mdoc_action_post(struct mdoc
*m
)
241 if (MDOC_ACTED
& m
->last
->flags
)
243 m
->last
->flags
|= MDOC_ACTED
;
245 switch (m
->last
->type
) {
254 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
256 return((*mdoc_actions
[m
->last
->tok
].post
)(m
, m
->last
));
261 * Concatenate sibling nodes together. All siblings must be of type
262 * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
266 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
271 for ( ; n
; n
= n
->next
) {
272 assert(MDOC_TEXT
== n
->type
);
274 * XXX: yes, these can technically be resized, but it's
275 * highly unlikely that we're going to get here, so let
278 if (strlcat(p
, n
->string
, sz
) >= sz
) {
279 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
284 if (strlcat(p
, " ", sz
) >= sz
) {
285 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
295 * Macros accepting `-std' as an argument have the name of the current
296 * document (`Nm') filled in as the argument if it's not provided.
301 struct mdoc_node
*nn
;
305 if (NULL
== m
->meta
.name
)
309 m
->next
= MDOC_NEXT_CHILD
;
311 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, m
->meta
.name
))
319 * The `Nm' macro's first use sets the name of the document. See also
329 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
331 m
->meta
.name
= mandoc_strdup(buf
);
337 * Look up the value of `Lb' for matching predefined strings. If it has
338 * one, then substitute the current value for the formatted value. Note
339 * that the lookup may fail (we can provide arbitrary strings).
349 assert(MDOC_TEXT
== n
->child
->type
);
350 p
= mdoc_a2lib(n
->child
->string
);
353 free(n
->child
->string
);
354 n
->child
->string
= mandoc_strdup(p
);
358 sz
= strlen(n
->child
->string
) +
359 2 + strlen("\\(lqlibrary\\(rq");
360 buf
= mandoc_malloc(sz
);
361 snprintf(buf
, sz
, "library \\(lq%s\\(rq", n
->child
->string
);
362 free(n
->child
->string
);
363 n
->child
->string
= buf
;
369 * Substitute the value of `St' for the corresponding formatted string.
370 * We're guaranteed that this exists (it's been verified during the
379 assert(MDOC_TEXT
== n
->child
->type
);
380 p
= mdoc_a2st(n
->child
->string
);
382 free(n
->child
->string
);
383 n
->child
->string
= mandoc_strdup(p
);
390 * Look up the standard string in a table. We know that it exists from
391 * the validation phase, so assert on failure. If a standard key wasn't
392 * supplied, supply the default ``AT&T UNIX''.
397 struct mdoc_node
*nn
;
403 assert(MDOC_TEXT
== n
->child
->type
);
404 p
= mdoc_a2att(n
->child
->string
);
406 free(n
->child
->string
);
407 n
->child
->string
= mandoc_strdup(p
);
410 q
= n
->child
->string
;
411 sz
= strlen(p
) + strlen(q
) + 1;
412 buf
= mandoc_malloc(sz
);
415 free(n
->child
->string
);
416 n
->child
->string
= buf
;
422 m
->next
= MDOC_NEXT_CHILD
;
423 if ( ! mdoc_word_alloc(m
, nn
->line
, nn
->pos
, "AT&T UNIX"))
431 * Parse out the contents of `Dt'. See in-line documentation for how we
432 * handle the various fields of this macro.
437 struct mdoc_node
*nn
;
447 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
449 * --> title = unknown, volume = local, msec = 0, arch = NULL
452 if (NULL
== (nn
= n
->child
)) {
453 /* XXX: make these macro values. */
454 /* FIXME: warn about missing values. */
455 m
->meta
.title
= mandoc_strdup("UNKNOWN");
456 m
->meta
.vol
= mandoc_strdup("LOCAL");
457 m
->meta
.msec
= mandoc_strdup("1");
458 return(post_prol(m
, n
));
461 /* Handles: `.Dt TITLE'
462 * --> title = TITLE, volume = local, msec = 0, arch = NULL
465 m
->meta
.title
= mandoc_strdup
466 ('\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
468 if (NULL
== (nn
= nn
->next
)) {
469 /* FIXME: warn about missing msec. */
470 /* XXX: make this a macro value. */
471 m
->meta
.vol
= mandoc_strdup("LOCAL");
472 m
->meta
.msec
= mandoc_strdup("1");
473 return(post_prol(m
, n
));
476 /* Handles: `.Dt TITLE SEC'
477 * --> title = TITLE, volume = SEC is msec ?
478 * format(msec) : SEC,
479 * msec = SEC is msec ? atoi(msec) : 0,
483 cp
= mdoc_a2msec(nn
->string
);
485 m
->meta
.vol
= mandoc_strdup(cp
);
486 m
->meta
.msec
= mandoc_strdup(nn
->string
);
487 } else if (mdoc_nmsg(m
, n
, MANDOCERR_BADMSEC
)) {
488 m
->meta
.vol
= mandoc_strdup(nn
->string
);
489 m
->meta
.msec
= mandoc_strdup(nn
->string
);
493 if (NULL
== (nn
= nn
->next
))
494 return(post_prol(m
, n
));
496 /* Handles: `.Dt TITLE SEC VOL'
497 * --> title = TITLE, volume = VOL is vol ?
499 * VOL is arch ? format(arch) :
503 cp
= mdoc_a2vol(nn
->string
);
506 m
->meta
.vol
= mandoc_strdup(cp
);
508 /* FIXME: warn about bad arch. */
509 cp
= mdoc_a2arch(nn
->string
);
512 m
->meta
.vol
= mandoc_strdup(nn
->string
);
514 m
->meta
.arch
= mandoc_strdup(cp
);
517 /* Ignore any subsequent parameters... */
518 /* FIXME: warn about subsequent parameters. */
520 return(post_prol(m
, n
));
525 * Set the operating system by way of the `Os' macro. Note that if an
526 * argument isn't provided and -DOSNAME="\"foo\"" is provided during
527 * compilation, this value will be used instead of filling in "sysname
528 * release" from uname().
535 struct utsname utsname
;
541 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
544 /* XXX: yes, these can all be dynamically-adjusted buffers, but
545 * it's really not worth the extra hackery.
548 if ('\0' == buf
[0]) {
550 if (strlcat(buf
, OSNAME
, BUFSIZ
) >= BUFSIZ
) {
551 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
555 if (-1 == uname(&utsname
))
556 return(mdoc_nmsg(m
, n
, MANDOCERR_UTSNAME
));
558 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
559 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
562 if (strlcat(buf
, " ", 64) >= BUFSIZ
) {
563 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
566 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
567 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
573 m
->meta
.os
= mandoc_strdup(buf
);
574 return(post_prol(m
, n
));
579 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
580 * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width
581 * argument has NOT been provided. See post_bl_width() for converting
585 post_bl_tagwidth(POST_ARGS
)
587 struct mdoc_node
*nn
;
594 for (nn
= n
->body
->child
; nn
; nn
= nn
->next
) {
595 if (MDOC_It
!= nn
->tok
)
598 assert(MDOC_BLOCK
== nn
->type
);
599 nn
= nn
->head
->child
;
602 /* No -width for .Bl and first .It is emtpy */
603 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
608 if (MDOC_TEXT
== nn
->type
) {
609 sz
= strlen(nn
->string
) + 1;
613 if (0 != (ssz
= mdoc_macro2len(nn
->tok
)))
615 else if ( ! mdoc_nmsg(m
, n
, MANDOCERR_NOWIDTHARG
))
621 /* Defaults to ten ens. */
623 snprintf(buf
, NUMSIZ
, "%zun", sz
);
626 * We have to dynamically add this to the macro's argument list.
627 * We're guaranteed that a MDOC_Width doesn't already exist.
631 i
= (int)(n
->args
->argc
)++;
633 n
->args
->argv
= mandoc_realloc(n
->args
->argv
,
634 n
->args
->argc
* sizeof(struct mdoc_argv
));
636 n
->args
->argv
[i
].arg
= MDOC_Width
;
637 n
->args
->argv
[i
].line
= n
->line
;
638 n
->args
->argv
[i
].pos
= n
->pos
;
639 n
->args
->argv
[i
].sz
= 1;
640 n
->args
->argv
[i
].value
= mandoc_malloc(sizeof(char *));
641 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
644 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
650 * Calculate the real width of a list from the -width string, which may
651 * contain a macro (with a known default width), a literal string, or a
655 post_bl_width(POST_ARGS
)
663 * If the value to -width is a macro, then we re-write it to be
664 * the macro's width as set in share/tmac/mdoc/doc-common.
667 if (0 == strcmp(n
->data
.Bl
->width
, "Ds"))
669 else if (MDOC_MAX
== (tok
= mdoc_hash_find(n
->data
.Bl
->width
)))
671 else if (0 == (width
= mdoc_macro2len(tok
)))
672 return(mdoc_nmsg(m
, n
, MANDOCERR_BADWIDTH
));
674 /* The value already exists: free and reallocate it. */
678 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
679 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
682 assert(i
< (int)n
->args
->argc
);
684 snprintf(buf
, NUMSIZ
, "%zun", width
);
685 free(n
->args
->argv
[i
].value
[0]);
686 n
->args
->argv
[i
].value
[0] = mandoc_strdup(buf
);
689 n
->data
.Bl
->width
= n
->args
->argv
[i
].value
[0];
695 * Do processing for -column lists, which can have two distinct styles
696 * of invocation. Merge this two styles into a consistent form.
700 post_bl_head(POST_ARGS
)
703 struct mdoc_node
*np
, *nn
, *nnp
;
705 if (LIST_column
!= n
->data
.Bl
->type
)
707 else if (NULL
== n
->child
)
713 for (c
= 0; c
< (int)np
->args
->argc
; c
++)
714 if (MDOC_Column
== np
->args
->argv
[c
].arg
)
717 assert(c
< (int)np
->args
->argc
);
718 assert(0 == np
->args
->argv
[c
].sz
);
721 * Accomodate for new-style groff column syntax. Shuffle the
722 * child nodes, all of which must be TEXT, as arguments for the
723 * column field. Then, delete the head children.
726 np
->args
->argv
[c
].sz
= (size_t)n
->nchild
;
727 np
->args
->argv
[c
].value
= mandoc_malloc
728 ((size_t)n
->nchild
* sizeof(char *));
730 n
->data
.Bl
->ncols
= np
->args
->argv
[c
].sz
;
731 n
->data
.Bl
->cols
= (const char **)np
->args
->argv
[c
].value
;
733 for (i
= 0, nn
= n
->child
; nn
; i
++) {
734 np
->args
->argv
[c
].value
[i
] = nn
->string
;
738 mdoc_node_delete(NULL
, nnp
);
751 if (MDOC_HEAD
== n
->type
)
752 return(post_bl_head(m
, n
));
753 if (MDOC_BLOCK
!= n
->type
)
757 * These are fairly complicated, so we've broken them into two
758 * functions. post_bl_tagwidth() is called when a -tag is
759 * specified, but no -width (it must be guessed). The second
760 * when a -width is specified (macro indicators must be
761 * rewritten into real lengths).
764 if (LIST_tag
== n
->data
.Bl
->type
&& NULL
== n
->data
.Bl
->width
) {
765 if ( ! post_bl_tagwidth(m
, n
))
767 } else if (NULL
!= n
->data
.Bl
->width
) {
768 if ( ! post_bl_width(m
, n
))
773 assert(n
->data
.Bl
->width
);
779 * The `Pa' macro defaults to a tilde if no value is provided as an
785 struct mdoc_node
*np
;
791 m
->next
= MDOC_NEXT_CHILD
;
792 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, "~"))
800 * Parse the date field in `Dd'.
807 if (NULL
== n
->child
) {
808 m
->meta
.date
= time(NULL
);
809 return(post_prol(m
, n
));
812 if ( ! concat(m
, buf
, n
->child
, DATESIZ
))
815 m
->meta
.date
= mandoc_a2time
816 (MTIME_MDOCDATE
| MTIME_CANONICAL
, buf
);
818 if (0 == m
->meta
.date
) {
819 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_BADDATE
))
821 m
->meta
.date
= time(NULL
);
824 return(post_prol(m
, n
));
829 * Remove prologue macros from the document after they're processed.
830 * The final document uses mdoc_meta for these values and discards the
837 mdoc_node_delete(m
, n
);
838 if (m
->meta
.title
&& m
->meta
.date
&& m
->meta
.os
)
839 m
->flags
|= MDOC_PBODY
;
845 * Trigger a literal context.
851 if (MDOC_BODY
== n
->type
)
852 m
->flags
|= MDOC_LITERAL
;
861 if (MDOC_BODY
!= n
->type
)
865 if (DISP_literal
== n
->data
.Bd
->type
)
866 m
->flags
|= MDOC_LITERAL
;
867 if (DISP_unfilled
== n
->data
.Bd
->type
)
868 m
->flags
|= MDOC_LITERAL
;
875 post_display(POST_ARGS
)
878 if (MDOC_BODY
== n
->type
)
879 m
->flags
&= ~MDOC_LITERAL
;