]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.37 2009/09/16 20:49:06 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
17 #include <sys/utsname.h>
27 #define POST_ARGS struct mdoc *m, struct mdoc_node *n
28 #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
32 int (*post
)(POST_ARGS
);
35 static int post_ar(POST_ARGS
);
36 static int post_at(POST_ARGS
);
37 static int post_bl(POST_ARGS
);
38 static int post_bl_head(POST_ARGS
);
39 static int post_bl_tagwidth(POST_ARGS
);
40 static int post_bl_width(POST_ARGS
);
41 static int post_dd(POST_ARGS
);
42 static int post_display(POST_ARGS
);
43 static int post_dt(POST_ARGS
);
44 static int post_lb(POST_ARGS
);
45 static int post_nm(POST_ARGS
);
46 static int post_os(POST_ARGS
);
47 static int post_prol(POST_ARGS
);
48 static int post_sh(POST_ARGS
);
49 static int post_st(POST_ARGS
);
50 static int post_std(POST_ARGS
);
51 static int post_tilde(POST_ARGS
);
53 static int pre_bd(PRE_ARGS
);
54 static int pre_dl(PRE_ARGS
);
56 static const struct actions mdoc_actions
[MDOC_MAX
] = {
57 { NULL
, NULL
}, /* Ap */
58 { NULL
, post_dd
}, /* Dd */
59 { NULL
, post_dt
}, /* Dt */
60 { NULL
, post_os
}, /* Os */
61 { NULL
, post_sh
}, /* Sh */
62 { NULL
, NULL
}, /* Ss */
63 { NULL
, NULL
}, /* Pp */
64 { NULL
, NULL
}, /* D1 */
65 { pre_dl
, post_display
}, /* Dl */
66 { pre_bd
, post_display
}, /* Bd */
67 { NULL
, NULL
}, /* Ed */
68 { NULL
, post_bl
}, /* Bl */
69 { NULL
, NULL
}, /* El */
70 { NULL
, NULL
}, /* It */
71 { NULL
, NULL
}, /* Ad */
72 { NULL
, NULL
}, /* An */
73 { NULL
, post_ar
}, /* Ar */
74 { NULL
, NULL
}, /* Cd */
75 { NULL
, NULL
}, /* Cm */
76 { NULL
, NULL
}, /* Dv */
77 { NULL
, NULL
}, /* Er */
78 { NULL
, NULL
}, /* Ev */
79 { NULL
, post_std
}, /* Ex */
80 { NULL
, NULL
}, /* Fa */
81 { NULL
, NULL
}, /* Fd */
82 { NULL
, NULL
}, /* Fl */
83 { NULL
, NULL
}, /* Fn */
84 { NULL
, NULL
}, /* Ft */
85 { NULL
, NULL
}, /* Ic */
86 { NULL
, NULL
}, /* In */
87 { NULL
, NULL
}, /* Li */
88 { NULL
, NULL
}, /* Nd */
89 { NULL
, post_nm
}, /* Nm */
90 { NULL
, NULL
}, /* Op */
91 { NULL
, NULL
}, /* Ot */
92 { NULL
, post_tilde
}, /* Pa */
93 { NULL
, post_std
}, /* Rv */
94 { NULL
, post_st
}, /* St */
95 { NULL
, NULL
}, /* Va */
96 { NULL
, NULL
}, /* Vt */
97 { NULL
, NULL
}, /* Xr */
98 { NULL
, NULL
}, /* %A */
99 { NULL
, NULL
}, /* %B */
100 { NULL
, NULL
}, /* %D */
101 { NULL
, NULL
}, /* %I */
102 { NULL
, NULL
}, /* %J */
103 { NULL
, NULL
}, /* %N */
104 { NULL
, NULL
}, /* %O */
105 { NULL
, NULL
}, /* %P */
106 { NULL
, NULL
}, /* %R */
107 { NULL
, NULL
}, /* %T */
108 { NULL
, NULL
}, /* %V */
109 { NULL
, NULL
}, /* Ac */
110 { NULL
, NULL
}, /* Ao */
111 { NULL
, NULL
}, /* Aq */
112 { NULL
, post_at
}, /* At */
113 { NULL
, NULL
}, /* Bc */
114 { NULL
, NULL
}, /* Bf */
115 { NULL
, NULL
}, /* Bo */
116 { NULL
, NULL
}, /* Bq */
117 { NULL
, NULL
}, /* Bsx */
118 { NULL
, NULL
}, /* Bx */
119 { NULL
, NULL
}, /* Db */
120 { NULL
, NULL
}, /* Dc */
121 { NULL
, NULL
}, /* Do */
122 { NULL
, NULL
}, /* Dq */
123 { NULL
, NULL
}, /* Ec */
124 { NULL
, NULL
}, /* Ef */
125 { NULL
, NULL
}, /* Em */
126 { NULL
, NULL
}, /* Eo */
127 { NULL
, NULL
}, /* Fx */
128 { NULL
, NULL
}, /* Ms */
129 { NULL
, NULL
}, /* No */
130 { NULL
, NULL
}, /* Ns */
131 { NULL
, NULL
}, /* Nx */
132 { NULL
, NULL
}, /* Ox */
133 { NULL
, NULL
}, /* Pc */
134 { NULL
, NULL
}, /* Pf */
135 { NULL
, NULL
}, /* Po */
136 { NULL
, NULL
}, /* Pq */
137 { NULL
, NULL
}, /* Qc */
138 { NULL
, NULL
}, /* Ql */
139 { NULL
, NULL
}, /* Qo */
140 { NULL
, NULL
}, /* Qq */
141 { NULL
, NULL
}, /* Re */
142 { NULL
, NULL
}, /* Rs */
143 { NULL
, NULL
}, /* Sc */
144 { NULL
, NULL
}, /* So */
145 { NULL
, NULL
}, /* Sq */
146 { NULL
, NULL
}, /* Sm */
147 { NULL
, NULL
}, /* Sx */
148 { NULL
, NULL
}, /* Sy */
149 { NULL
, NULL
}, /* Tn */
150 { NULL
, NULL
}, /* Ux */
151 { NULL
, NULL
}, /* Xc */
152 { NULL
, NULL
}, /* Xo */
153 { NULL
, NULL
}, /* Fo */
154 { NULL
, NULL
}, /* Fc */
155 { NULL
, NULL
}, /* Oo */
156 { NULL
, NULL
}, /* Oc */
157 { NULL
, NULL
}, /* Bk */
158 { NULL
, NULL
}, /* Ek */
159 { NULL
, NULL
}, /* Bt */
160 { NULL
, NULL
}, /* Hf */
161 { NULL
, NULL
}, /* Fr */
162 { NULL
, NULL
}, /* Ud */
163 { NULL
, post_lb
}, /* Lb */
164 { NULL
, NULL
}, /* Lp */
165 { NULL
, post_tilde
}, /* Lk */
166 { NULL
, NULL
}, /* Mt */
167 { NULL
, NULL
}, /* Brq */
168 { NULL
, NULL
}, /* Bro */
169 { NULL
, NULL
}, /* Brc */
170 { NULL
, NULL
}, /* %C */
171 { NULL
, NULL
}, /* Es */
172 { NULL
, NULL
}, /* En */
173 { NULL
, NULL
}, /* Dx */
174 { NULL
, NULL
}, /* %Q */
175 { NULL
, NULL
}, /* br */
176 { NULL
, NULL
}, /* sp */
179 static int concat(struct mdoc
*, const struct mdoc_node
*,
183 extern size_t strlcat(char *, const char *, size_t);
188 mdoc_action_pre(struct mdoc
*m
, const struct mdoc_node
*n
)
200 if (NULL
== mdoc_actions
[n
->tok
].pre
)
202 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
207 mdoc_action_post(struct mdoc
*m
)
210 if (MDOC_ACTED
& m
->last
->flags
)
212 m
->last
->flags
|= MDOC_ACTED
;
214 switch (m
->last
->type
) {
223 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
225 return((*mdoc_actions
[m
->last
->tok
].post
)(m
, m
->last
));
230 concat(struct mdoc
*m
, const struct mdoc_node
*n
,
231 char *buf
, size_t sz
)
234 for ( ; n
; n
= n
->next
) {
235 assert(MDOC_TEXT
== n
->type
);
236 if (strlcat(buf
, n
->string
, sz
) >= sz
)
237 return(mdoc_nerr(m
, n
, ETOOLONG
));
240 if (strlcat(buf
, " ", sz
) >= sz
)
241 return(mdoc_nerr(m
, n
, ETOOLONG
));
251 struct mdoc_node
*nn
;
257 m
->next
= MDOC_NEXT_CHILD
;
258 assert(m
->meta
.name
);
259 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, m
->meta
.name
))
276 if ( ! concat(m
, n
->child
, buf
, sizeof(buf
)))
278 if (NULL
== (m
->meta
.name
= strdup(buf
)))
279 return(mdoc_nerr(m
, n
, EMALLOC
));
292 assert(MDOC_TEXT
== n
->child
->type
);
293 p
= mdoc_a2lib(n
->child
->string
);
295 sz
= strlen(n
->child
->string
) +
296 2 + strlen("\\(lqlibrary\\(rq");
299 return(mdoc_nerr(m
, n
, EMALLOC
));
300 (void)snprintf(buf
, sz
, "library \\(lq%s\\(rq",
302 free(n
->child
->string
);
303 n
->child
->string
= buf
;
307 free(n
->child
->string
);
308 n
->child
->string
= strdup(p
);
309 if (NULL
== n
->child
->string
)
310 return(mdoc_nerr(m
, n
, EMALLOC
));
321 assert(MDOC_TEXT
== n
->child
->type
);
322 p
= mdoc_a2st(n
->child
->string
);
324 free(n
->child
->string
);
325 n
->child
->string
= strdup(p
);
326 if (NULL
== n
->child
->string
)
327 return(mdoc_nerr(m
, n
, EMALLOC
));
336 struct mdoc_node
*nn
;
340 assert(MDOC_TEXT
== n
->child
->type
);
341 p
= mdoc_a2att(n
->child
->string
);
343 free(n
->child
->string
);
344 n
->child
->string
= strdup(p
);
345 if (NULL
== n
->child
->string
)
346 return(mdoc_nerr(m
, n
, EMALLOC
));
351 m
->next
= MDOC_NEXT_CHILD
;
353 if ( ! mdoc_word_alloc(m
, nn
->line
, nn
->pos
, "AT&T UNIX"))
368 * We keep track of the current section /and/ the "named"
369 * section, which is one of the conventional ones, in order to
373 if (MDOC_HEAD
!= n
->type
)
377 if ( ! concat(m
, n
->child
, buf
, sizeof(buf
)))
379 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
382 switch ((m
->lastsec
= sec
)) {
383 case (SEC_RETURN_VALUES
):
386 switch (m
->meta
.msec
) {
394 return(mdoc_nwarn(m
, n
, EBADSEC
));
407 struct mdoc_node
*nn
;
419 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
423 * --> title = unknown, volume = local, msec = 0, arch = NULL
426 if (NULL
== (nn
= n
->child
)) {
427 if (NULL
== (m
->meta
.title
= strdup("unknown")))
428 return(mdoc_nerr(m
, n
, EMALLOC
));
429 if (NULL
== (m
->meta
.vol
= strdup("local")))
430 return(mdoc_nerr(m
, n
, EMALLOC
));
431 return(post_prol(m
, n
));
434 /* Handles: `.Dt TITLE'
435 * --> title = TITLE, volume = local, msec = 0, arch = NULL
438 if (NULL
== (m
->meta
.title
= strdup(nn
->string
)))
439 return(mdoc_nerr(m
, n
, EMALLOC
));
441 if (NULL
== (nn
= nn
->next
)) {
442 if (NULL
== (m
->meta
.vol
= strdup("local")))
443 return(mdoc_nerr(m
, n
, EMALLOC
));
444 return(post_prol(m
, n
));
447 /* Handles: `.Dt TITLE SEC'
448 * --> title = TITLE, volume = SEC is msec ?
449 * format(msec) : SEC,
450 * msec = SEC is msec ? atoi(msec) : 0,
454 cp
= mdoc_a2msec(nn
->string
);
456 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
457 return(mdoc_nerr(m
, n
, EMALLOC
));
459 lval
= strtol(nn
->string
, &ep
, 10);
460 if (nn
->string
[0] != '\0' && *ep
== '\0')
461 m
->meta
.msec
= (int)lval
;
462 } else if (NULL
== (m
->meta
.vol
= strdup(nn
->string
)))
463 return(mdoc_nerr(m
, n
, EMALLOC
));
465 if (NULL
== (nn
= nn
->next
))
466 return(post_prol(m
, n
));
468 /* Handles: `.Dt TITLE SEC VOL'
469 * --> title = TITLE, volume = VOL is vol ?
471 * VOL is arch ? format(arch) :
475 cp
= mdoc_a2vol(nn
->string
);
478 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
479 return(mdoc_nerr(m
, n
, EMALLOC
));
481 cp
= mdoc_a2arch(nn
->string
);
484 if (NULL
== (m
->meta
.vol
= strdup(nn
->string
)))
485 return(mdoc_nerr(m
, n
, EMALLOC
));
486 } else if (NULL
== (m
->meta
.arch
= strdup(cp
)))
487 return(mdoc_nerr(m
, n
, EMALLOC
));
490 /* Ignore any subsequent parameters... */
492 return(post_prol(m
, n
));
500 struct utsname utsname
;
506 if ( ! concat(m
, n
->child
, buf
, sizeof(buf
)))
510 if (-1 == uname(&utsname
))
511 return(mdoc_nerr(m
, n
, EUTSNAME
));
512 if (strlcat(buf
, utsname
.sysname
, 64) >= 64)
513 return(mdoc_nerr(m
, n
, ETOOLONG
));
514 if (strlcat(buf
, " ", 64) >= 64)
515 return(mdoc_nerr(m
, n
, ETOOLONG
));
516 if (strlcat(buf
, utsname
.release
, 64) >= 64)
517 return(mdoc_nerr(m
, n
, ETOOLONG
));
520 if (NULL
== (m
->meta
.os
= strdup(buf
)))
521 return(mdoc_nerr(m
, n
, EMALLOC
));
523 return(post_prol(m
, n
));
528 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
529 * Uses the first head macro.
532 post_bl_tagwidth(POST_ARGS
)
534 struct mdoc_node
*nn
;
539 * Use the text width, if a text node, or the default macro
545 assert(MDOC_BLOCK
== nn
->type
);
546 assert(MDOC_It
== nn
->tok
);
547 nn
= nn
->head
->child
;
550 sz
= 10; /* Default size. */
553 if (MDOC_TEXT
!= nn
->type
) {
554 if (0 == (sz
= (int)mdoc_macro2len(nn
->tok
)))
555 if ( ! mdoc_nwarn(m
, n
, ENOWIDTH
))
558 sz
= (int)strlen(nn
->string
) + 1;
561 if (-1 == snprintf(buf
, sizeof(buf
), "%dn", sz
))
562 return(mdoc_nerr(m
, n
, ENUMFMT
));
565 * We have to dynamically add this to the macro's argument list.
566 * We're guaranteed that a MDOC_Width doesn't already exist.
571 sz
= (int)(nn
->args
->argc
)++;
573 nn
->args
->argv
= realloc(nn
->args
->argv
,
574 nn
->args
->argc
* sizeof(struct mdoc_argv
));
576 if (NULL
== nn
->args
->argv
)
577 return(mdoc_nerr(m
, n
, EMALLOC
));
579 nn
->args
->argv
[sz
].arg
= MDOC_Width
;
580 nn
->args
->argv
[sz
].line
= n
->line
;
581 nn
->args
->argv
[sz
].pos
= n
->pos
;
582 nn
->args
->argv
[sz
].sz
= 1;
583 nn
->args
->argv
[sz
].value
= calloc(1, sizeof(char *));
585 if (NULL
== nn
->args
->argv
[sz
].value
)
586 return(mdoc_nerr(m
, n
, EMALLOC
));
587 if (NULL
== (nn
->args
->argv
[sz
].value
[0] = strdup(buf
)))
588 return(mdoc_nerr(m
, n
, EMALLOC
));
595 post_bl_width(POST_ARGS
)
605 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
606 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
609 if (i
== (int)n
->args
->argc
)
611 p
= n
->args
->argv
[i
].value
[0];
614 * If the value to -width is a macro, then we re-write it to be
615 * the macro's width as set in share/tmac/mdoc/doc-common.
618 if (0 == strcmp(p
, "Ds"))
620 else if (MDOC_MAX
== (tok
= mdoc_hash_find(p
)))
622 else if (0 == (width
= mdoc_macro2len(tok
)))
623 return(mdoc_nwarn(m
, n
, ENOWIDTH
));
625 /* The value already exists: free and reallocate it. */
627 if (-1 == snprintf(buf
, sizeof(buf
), "%zun", width
))
628 return(mdoc_nerr(m
, n
, ENUMFMT
));
630 free(n
->args
->argv
[i
].value
[0]);
631 n
->args
->argv
[i
].value
[0] = strdup(buf
);
632 if (NULL
== n
->args
->argv
[i
].value
[0])
633 return(mdoc_nerr(m
, n
, EMALLOC
));
641 post_bl_head(POST_ARGS
)
644 struct mdoc_node
*np
, *nn
, *nnp
;
646 if (NULL
== n
->child
)
652 for (c
= 0; c
< (int)np
->args
->argc
; c
++)
653 if (MDOC_Column
== np
->args
->argv
[c
].arg
)
656 /* Only process -column. */
658 if (c
== (int)np
->args
->argc
)
661 assert(0 == np
->args
->argv
[c
].sz
);
664 * Accomodate for new-style groff column syntax. Shuffle the
665 * child nodes, all of which must be TEXT, as arguments for the
666 * column field. Then, delete the head children.
669 np
->args
->argv
[c
].sz
= (size_t)n
->nchild
;
670 np
->args
->argv
[c
].value
= malloc
671 ((size_t)n
->nchild
* sizeof(char *));
673 for (i
= 0, nn
= n
->child
; nn
; i
++) {
674 np
->args
->argv
[c
].value
[i
] = nn
->string
;
693 if (MDOC_HEAD
== n
->type
)
694 return(post_bl_head(m
, n
));
695 if (MDOC_BLOCK
!= n
->type
)
699 * These are fairly complicated, so we've broken them into two
700 * functions. post_bl_tagwidth() is called when a -tag is
701 * specified, but no -width (it must be guessed). The second
702 * when a -width is specified (macro indicators must be
703 * rewritten into real lengths).
706 len
= (int)(n
->args
? n
->args
->argc
: 0);
708 for (r
= i
= 0; i
< len
; i
++) {
709 if (MDOC_Tag
== n
->args
->argv
[i
].arg
)
711 if (MDOC_Width
== n
->args
->argv
[i
].arg
)
715 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
716 if ( ! post_bl_tagwidth(m
, n
))
718 } else if (r
& (1 << 1))
719 if ( ! post_bl_width(m
, n
))
727 post_tilde(POST_ARGS
)
729 struct mdoc_node
*np
;
735 m
->next
= MDOC_NEXT_CHILD
;
737 /* XXX: not documented for `Lk'. */
738 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, "~"))
749 struct mdoc_node
*np
;
755 m
->next
= MDOC_NEXT_CHILD
;
756 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, "file"))
758 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, "..."))
772 if ( ! concat(m
, n
->child
, buf
, sizeof(buf
)))
775 if (0 == (m
->meta
.date
= mdoc_atotime(buf
))) {
776 if ( ! mdoc_nwarn(m
, n
, EBADDATE
))
778 m
->meta
.date
= time(NULL
);
781 return(post_prol(m
, n
));
788 struct mdoc_node
*np
;
790 /* Remove prologue macros from AST. */
792 if (n
->parent
->child
== n
)
793 n
->parent
->child
= n
->prev
;
795 n
->prev
->next
= NULL
;
798 assert(NULL
== n
->next
);
802 m
->next
= MDOC_NEXT_SIBLING
;
805 m
->next
= MDOC_NEXT_CHILD
;
808 mdoc_node_freelist(np
);
810 if (m
->meta
.title
&& m
->meta
.date
&& m
->meta
.os
)
811 m
->flags
|= MDOC_PBODY
;
821 if (MDOC_BODY
== n
->type
)
822 m
->flags
|= MDOC_LITERAL
;
832 if (MDOC_BODY
!= n
->type
)
835 /* Enter literal context if `Bd -literal' or `-unfilled'. */
838 * TODO: `-offset' without an argument should be the width of
839 * the literal "<string>".
842 for (n
= n
->parent
, i
= 0; i
< (int)n
->args
->argc
; i
++)
843 if (MDOC_Literal
== n
->args
->argv
[i
].arg
)
845 else if (MDOC_Unfilled
== n
->args
->argv
[i
].arg
)
848 if (i
< (int)n
->args
->argc
)
849 m
->flags
|= MDOC_LITERAL
;
856 post_display(POST_ARGS
)
859 if (MDOC_BODY
== n
->type
)
860 m
->flags
&= ~MDOC_LITERAL
;