]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
176dd19548a12ace760a299039b20c9c280bc721
1 /* $Id: mdoc_action.c,v 1.25 2009/07/12 20:24:24 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 PRE_ARGS struct mdoc *m, const struct mdoc_node *n
28 #define POST_ARGS struct mdoc *m
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_lk(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_std(POST_ARGS
);
51 static int pre_bd(PRE_ARGS
);
52 static int pre_dl(PRE_ARGS
);
54 const struct actions mdoc_actions
[MDOC_MAX
] = {
55 { NULL
, NULL
}, /* Ap */
56 { NULL
, post_dd
}, /* Dd */
57 { NULL
, post_dt
}, /* Dt */
58 { NULL
, post_os
}, /* Os */
59 { NULL
, post_sh
}, /* Sh */
60 { NULL
, NULL
}, /* Ss */
61 { NULL
, NULL
}, /* Pp */
62 { NULL
, NULL
}, /* D1 */
63 { pre_dl
, post_display
}, /* Dl */
64 { pre_bd
, post_display
}, /* Bd */
65 { NULL
, NULL
}, /* Ed */
66 { NULL
, post_bl
}, /* Bl */
67 { NULL
, NULL
}, /* El */
68 { NULL
, NULL
}, /* It */
69 { NULL
, NULL
}, /* Ad */
70 { NULL
, NULL
}, /* An */
71 { NULL
, post_ar
}, /* Ar */
72 { NULL
, NULL
}, /* Cd */ /* FIXME: tabs are accepted! */
73 { NULL
, NULL
}, /* Cm */
74 { NULL
, NULL
}, /* Dv */
75 { NULL
, NULL
}, /* Er */
76 { NULL
, NULL
}, /* Ev */
77 { NULL
, post_std
}, /* Ex */
78 { NULL
, NULL
}, /* Fa */
79 { NULL
, NULL
}, /* Fd */
80 { NULL
, NULL
}, /* Fl */
81 { NULL
, NULL
}, /* Fn */
82 { NULL
, NULL
}, /* Ft */
83 { NULL
, NULL
}, /* Ic */
84 { NULL
, NULL
}, /* In */
85 { NULL
, NULL
}, /* Li */
86 { NULL
, NULL
}, /* Nd */
87 { NULL
, post_nm
}, /* Nm */
88 { NULL
, NULL
}, /* Op */
89 { NULL
, NULL
}, /* Ot */
90 { NULL
, NULL
}, /* Pa */
91 { NULL
, post_std
}, /* Rv */
92 { NULL
, NULL
}, /* St */
93 { NULL
, NULL
}, /* Va */
94 { NULL
, NULL
}, /* Vt */
95 { NULL
, NULL
}, /* Xr */
96 { NULL
, NULL
}, /* %A */
97 { NULL
, NULL
}, /* %B */
98 { NULL
, NULL
}, /* %D */
99 { NULL
, NULL
}, /* %I */
100 { NULL
, NULL
}, /* %J */
101 { NULL
, NULL
}, /* %N */
102 { NULL
, NULL
}, /* %O */
103 { NULL
, NULL
}, /* %P */
104 { NULL
, NULL
}, /* %R */
105 { NULL
, NULL
}, /* %T */
106 { NULL
, NULL
}, /* %V */
107 { NULL
, NULL
}, /* Ac */
108 { NULL
, NULL
}, /* Ao */
109 { NULL
, NULL
}, /* Aq */
110 { NULL
, post_at
}, /* At */
111 { NULL
, NULL
}, /* Bc */
112 { NULL
, NULL
}, /* Bf */
113 { NULL
, NULL
}, /* Bo */
114 { NULL
, NULL
}, /* Bq */
115 { NULL
, NULL
}, /* Bsx */
116 { NULL
, NULL
}, /* Bx */
117 { NULL
, NULL
}, /* Db */
118 { NULL
, NULL
}, /* Dc */
119 { NULL
, NULL
}, /* Do */
120 { NULL
, NULL
}, /* Dq */
121 { NULL
, NULL
}, /* Ec */
122 { NULL
, NULL
}, /* Ef */
123 { NULL
, NULL
}, /* Em */
124 { NULL
, NULL
}, /* Eo */
125 { NULL
, NULL
}, /* Fx */
126 { NULL
, NULL
}, /* Ms */
127 { NULL
, NULL
}, /* No */
128 { NULL
, NULL
}, /* Ns */
129 { NULL
, NULL
}, /* Nx */
130 { NULL
, NULL
}, /* Ox */
131 { NULL
, NULL
}, /* Pc */
132 { NULL
, NULL
}, /* Pf */
133 { NULL
, NULL
}, /* Po */
134 { NULL
, NULL
}, /* Pq */
135 { NULL
, NULL
}, /* Qc */
136 { NULL
, NULL
}, /* Ql */
137 { NULL
, NULL
}, /* Qo */
138 { NULL
, NULL
}, /* Qq */
139 { NULL
, NULL
}, /* Re */
140 { NULL
, NULL
}, /* Rs */
141 { NULL
, NULL
}, /* Sc */
142 { NULL
, NULL
}, /* So */
143 { NULL
, NULL
}, /* Sq */
144 { NULL
, NULL
}, /* Sm */
145 { NULL
, NULL
}, /* Sx */
146 { NULL
, NULL
}, /* Sy */
147 { NULL
, NULL
}, /* Tn */
148 { NULL
, NULL
}, /* Ux */
149 { NULL
, NULL
}, /* Xc */
150 { NULL
, NULL
}, /* Xo */
151 { NULL
, NULL
}, /* Fo */
152 { NULL
, NULL
}, /* Fc */
153 { NULL
, NULL
}, /* Oo */
154 { NULL
, NULL
}, /* Oc */
155 { NULL
, NULL
}, /* Bk */
156 { NULL
, NULL
}, /* Ek */
157 { NULL
, NULL
}, /* Bt */
158 { NULL
, NULL
}, /* Hf */
159 { NULL
, NULL
}, /* Fr */
160 { NULL
, NULL
}, /* Ud */
161 { NULL
, NULL
}, /* Lb */
162 { NULL
, NULL
}, /* Lp */
163 { NULL
, post_lk
}, /* Lk */
164 { NULL
, NULL
}, /* Mt */
165 { NULL
, NULL
}, /* Brq */
166 { NULL
, NULL
}, /* Bro */
167 { NULL
, NULL
}, /* Brc */
168 { NULL
, NULL
}, /* %C */
169 { NULL
, NULL
}, /* Es */
170 { NULL
, NULL
}, /* En */
171 { NULL
, NULL
}, /* Dx */
172 { NULL
, NULL
}, /* %Q */
175 static int concat(struct mdoc
*, const struct mdoc_node
*,
179 extern size_t strlcat(char *, const char *, size_t);
184 mdoc_action_pre(struct mdoc
*m
, const struct mdoc_node
*n
)
196 if (NULL
== mdoc_actions
[n
->tok
].pre
)
198 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
203 mdoc_action_post(struct mdoc
*m
)
206 if (MDOC_ACTED
& m
->last
->flags
)
208 m
->last
->flags
|= MDOC_ACTED
;
210 switch (m
->last
->type
) {
219 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
221 return((*mdoc_actions
[m
->last
->tok
].post
)(m
));
226 concat(struct mdoc
*m
, const struct mdoc_node
*n
,
227 char *buf
, size_t sz
)
230 for ( ; n
; n
= n
->next
) {
231 assert(MDOC_TEXT
== n
->type
);
232 if (strlcat(buf
, n
->string
, sz
) >= sz
)
233 return(mdoc_nerr(m
, n
, ETOOLONG
));
236 if (strlcat(buf
, " ", sz
) >= sz
)
237 return(mdoc_nerr(m
, n
, ETOOLONG
));
249 * If '-std' is invoked without an argument, fill it in with our
250 * name (if it's been set).
253 if (NULL
== m
->last
->args
)
255 if (m
->last
->args
->argv
[0].sz
)
258 assert(m
->meta
.name
);
260 m
->last
->args
->argv
[0].value
= calloc(1, sizeof(char *));
261 if (NULL
== m
->last
->args
->argv
[0].value
)
262 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
264 m
->last
->args
->argv
[0].sz
= 1;
265 m
->last
->args
->argv
[0].value
[0] = strdup(m
->meta
.name
);
266 if (NULL
== m
->last
->args
->argv
[0].value
[0])
267 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
282 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
285 if (NULL
== (m
->meta
.name
= strdup(buf
)))
286 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
298 if (m
->last
->child
) {
299 assert(MDOC_TEXT
== m
->last
->child
->type
);
300 p
= mdoc_a2att(m
->last
->child
->string
);
302 free(m
->last
->child
->string
);
303 m
->last
->child
->string
= strdup(p
);
304 if (NULL
== m
->last
->child
->string
)
305 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
310 m
->next
= MDOC_NEXT_CHILD
;
312 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, "AT&T UNIX"))
316 m
->next
= MDOC_NEXT_SIBLING
;
328 * We keep track of the current section /and/ the "named"
329 * section, which is one of the conventional ones, in order to
333 if (MDOC_HEAD
!= m
->last
->type
)
337 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
339 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
342 switch ((m
->lastsec
= sec
)) {
343 case (SEC_RETURN_VALUES
):
346 switch (m
->meta
.msec
) {
354 return(mdoc_nwarn(m
, m
->last
, EBADSEC
));
379 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
383 * --> title = unknown, volume = local, msec = 0, arch = NULL
386 if (NULL
== (n
= m
->last
->child
)) {
387 if (NULL
== (m
->meta
.title
= strdup("unknown")))
388 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
389 if (NULL
== (m
->meta
.vol
= strdup("local")))
390 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
391 return(post_prol(m
));
394 /* Handles: `.Dt TITLE'
395 * --> title = TITLE, volume = local, msec = 0, arch = NULL
398 if (NULL
== (m
->meta
.title
= strdup(n
->string
)))
399 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
401 if (NULL
== (n
= n
->next
)) {
402 if (NULL
== (m
->meta
.vol
= strdup("local")))
403 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
404 return(post_prol(m
));
407 /* Handles: `.Dt TITLE SEC'
408 * --> title = TITLE, volume = SEC is msec ?
409 * format(msec) : SEC,
410 * msec = SEC is msec ? atoi(msec) : 0,
414 cp
= mdoc_a2msec(n
->string
);
416 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
417 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
419 lval
= strtol(n
->string
, &ep
, 10);
420 if (n
->string
[0] != '\0' && *ep
== '\0')
421 m
->meta
.msec
= (int)lval
;
422 } else if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
423 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
425 if (NULL
== (n
= n
->next
))
426 return(post_prol(m
));
428 /* Handles: `.Dt TITLE SEC VOL'
429 * --> title = TITLE, volume = VOL is vol ?
431 * VOL is arch ? format(arch) :
435 cp
= mdoc_a2vol(n
->string
);
438 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
439 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
442 cp
= mdoc_a2arch(n
->string
);
445 if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
446 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
447 } else if (NULL
== (m
->meta
.arch
= strdup(cp
)))
448 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
451 /* Ignore any subsequent parameters... */
453 return(post_prol(m
));
461 struct utsname utsname
;
467 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
471 if (-1 == uname(&utsname
))
472 return(mdoc_nerr(m
, m
->last
, EUTSNAME
));
473 if (strlcat(buf
, utsname
.sysname
, 64) >= 64)
474 return(mdoc_nerr(m
, m
->last
, ETOOLONG
));
475 if (strlcat(buf
, " ", 64) >= 64)
476 return(mdoc_nerr(m
, m
->last
, ETOOLONG
));
477 if (strlcat(buf
, utsname
.release
, 64) >= 64)
478 return(mdoc_nerr(m
, m
->last
, ETOOLONG
));
481 if (NULL
== (m
->meta
.os
= strdup(buf
)))
482 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
484 m
->flags
|= MDOC_PBODY
;
485 return(post_prol(m
));
490 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
491 * Uses the first head macro.
494 post_bl_tagwidth(struct mdoc
*m
)
501 * Use the text width, if a text node, or the default macro
505 n
= m
->last
->body
->child
;
507 assert(MDOC_BLOCK
== n
->type
);
508 assert(MDOC_It
== n
->tok
);
512 sz
= 10; /* Default size. */
515 if (MDOC_TEXT
!= n
->type
) {
516 if (0 == (sz
= (int)mdoc_macro2len(n
->tok
)))
517 if ( ! mdoc_nwarn(m
, m
->last
, ENOWIDTH
))
520 sz
= (int)strlen(n
->string
) + 1;
523 if (-1 == snprintf(buf
, sizeof(buf
), "%dn", sz
))
524 return(mdoc_nerr(m
, m
->last
, ENUMFMT
));
527 * We have to dynamically add this to the macro's argument list.
528 * We're guaranteed that a MDOC_Width doesn't already exist.
533 sz
= (int)(n
->args
->argc
)++;
535 n
->args
->argv
= realloc(n
->args
->argv
,
536 n
->args
->argc
* sizeof(struct mdoc_argv
));
538 if (NULL
== n
->args
->argv
)
539 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
541 n
->args
->argv
[sz
].arg
= MDOC_Width
;
542 n
->args
->argv
[sz
].line
= m
->last
->line
;
543 n
->args
->argv
[sz
].pos
= m
->last
->pos
;
544 n
->args
->argv
[sz
].sz
= 1;
545 n
->args
->argv
[sz
].value
= calloc(1, sizeof(char *));
547 if (NULL
== n
->args
->argv
[sz
].value
)
548 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
549 if (NULL
== (n
->args
->argv
[sz
].value
[0] = strdup(buf
)))
550 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
557 post_bl_width(struct mdoc
*m
)
564 if (NULL
== m
->last
->args
)
567 for (i
= 0; i
< (int)m
->last
->args
->argc
; i
++)
568 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
571 if (i
== (int)m
->last
->args
->argc
)
573 p
= m
->last
->args
->argv
[i
].value
[0];
576 * If the value to -width is a macro, then we re-write it to be
577 * the macro's width as set in share/tmac/mdoc/doc-common.
580 if (0 == strcmp(p
, "Ds"))
582 else if (MDOC_MAX
== (tok
= mdoc_hash_find(m
->htab
, p
)))
584 else if (0 == (width
= mdoc_macro2len(tok
)))
585 return(mdoc_nwarn(m
, m
->last
, ENOWIDTH
));
587 /* The value already exists: free and reallocate it. */
589 if (-1 == snprintf(buf
, sizeof(buf
), "%zun", width
))
590 return(mdoc_nerr(m
, m
->last
, ENUMFMT
));
592 free(m
->last
->args
->argv
[i
].value
[0]);
593 m
->last
->args
->argv
[i
].value
[0] = strdup(buf
);
594 if (NULL
== m
->last
->args
->argv
[i
].value
[0])
595 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
602 post_bl_head(POST_ARGS
)
605 struct mdoc_node
*n
, *nn
, *nnp
;
607 if (NULL
== m
->last
->child
)
613 for (c
= 0; c
< (int)n
->args
->argc
; c
++)
614 if (MDOC_Column
== n
->args
->argv
[c
].arg
)
617 /* Only process -column. */
619 if (c
== (int)n
->args
->argc
)
622 assert(0 == n
->args
->argv
[c
].sz
);
625 * Accomodate for new-style groff column syntax. Shuffle the
626 * child nodes, all of which must be TEXT, as arguments for the
627 * column field. Then, delete the head children.
630 n
->args
->argv
[c
].sz
= (size_t)m
->last
->nchild
;
631 n
->args
->argv
[c
].value
= malloc
632 ((size_t)m
->last
->nchild
* sizeof(char *));
634 for (i
= 0, nn
= m
->last
->child
; nn
; i
++) {
635 n
->args
->argv
[c
].value
[i
] = nn
->string
;
643 m
->last
->child
= NULL
;
654 if (MDOC_HEAD
== m
->last
->type
)
655 return(post_bl_head(m
));
656 if (MDOC_BLOCK
!= m
->last
->type
)
660 * These are fairly complicated, so we've broken them into two
661 * functions. post_bl_tagwidth() is called when a -tag is
662 * specified, but no -width (it must be guessed). The second
663 * when a -width is specified (macro indicators must be
664 * rewritten into real lengths).
667 len
= (int)(m
->last
->args
? m
->last
->args
->argc
: 0);
669 for (r
= i
= 0; i
< len
; i
++) {
670 if (MDOC_Tag
== m
->last
->args
->argv
[i
].arg
)
672 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
676 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
677 if ( ! post_bl_tagwidth(m
))
679 } else if (r
& (1 << 1))
680 if ( ! post_bl_width(m
))
696 m
->next
= MDOC_NEXT_CHILD
;
698 /* XXX: this isn't documented anywhere! */
699 if ( ! mdoc_word_alloc(m
, m
->last
->line
, m
->last
->pos
, "~"))
703 m
->next
= MDOC_NEXT_SIBLING
;
717 m
->next
= MDOC_NEXT_CHILD
;
718 if ( ! mdoc_word_alloc(m
, m
->last
->line
, m
->last
->pos
, "file"))
720 m
->next
= MDOC_NEXT_SIBLING
;
721 if ( ! mdoc_word_alloc(m
, m
->last
->line
, m
->last
->pos
, "..."))
725 m
->next
= MDOC_NEXT_SIBLING
;
736 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
739 if (0 == (m
->meta
.date
= mdoc_atotime(buf
))) {
740 if ( ! mdoc_nwarn(m
, m
->last
, EBADDATE
))
742 m
->meta
.date
= time(NULL
);
745 return(post_prol(m
));
755 * The end document shouldn't have the prologue macros as part
756 * of the syntax tree (they encompass only meta-data).
759 if (m
->last
->parent
->child
== m
->last
)
760 m
->last
->parent
->child
= m
->last
->prev
;
762 m
->last
->prev
->next
= NULL
;
765 assert(NULL
== m
->last
->next
);
768 m
->last
= m
->last
->prev
;
769 m
->next
= MDOC_NEXT_SIBLING
;
771 m
->last
= m
->last
->parent
;
772 m
->next
= MDOC_NEXT_CHILD
;
775 mdoc_node_freelist(n
);
784 if (MDOC_BODY
== n
->type
)
785 m
->flags
|= MDOC_LITERAL
;
795 if (MDOC_BODY
!= n
->type
)
798 /* Enter literal context if `Bd -literal' or * -unfilled'. */
800 for (n
= n
->parent
, i
= 0; i
< (int)n
->args
->argc
; i
++)
801 if (MDOC_Literal
== n
->args
->argv
[i
].arg
)
803 else if (MDOC_Unfilled
== n
->args
->argv
[i
].arg
)
806 if (i
< (int)n
->args
->argc
)
807 m
->flags
|= MDOC_LITERAL
;
814 post_display(POST_ARGS
)
817 if (MDOC_BODY
== m
->last
->type
)
818 m
->flags
&= ~MDOC_LITERAL
;