]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.24 2009/07/07 11:47:17 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 concat(struct mdoc
*, const struct mdoc_node
*,
38 static int post_ar(POST_ARGS
);
39 static int post_bl(POST_ARGS
);
40 static int post_bl_head(POST_ARGS
);
41 static int post_bl_width(POST_ARGS
);
42 static int post_bl_tagwidth(POST_ARGS
);
43 static int post_dd(POST_ARGS
);
44 static int post_display(POST_ARGS
);
45 static int post_dt(POST_ARGS
);
46 static int post_lk(POST_ARGS
);
47 static int post_nm(POST_ARGS
);
48 static int post_os(POST_ARGS
);
49 static int post_prol(POST_ARGS
);
50 static int post_sh(POST_ARGS
);
51 static int post_std(POST_ARGS
);
53 static int pre_bd(PRE_ARGS
);
54 static int pre_dl(PRE_ARGS
);
56 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 */ /* FIXME: tabs are accepted! */
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
, NULL
}, /* Pa */
93 { NULL
, post_std
}, /* Rv */
94 { NULL
, NULL
}, /* 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
, NULL
}, /* 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
, NULL
}, /* Lb */
164 { NULL
, NULL
}, /* Lp */
165 { NULL
, post_lk
}, /* 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 */
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
));
299 * We keep track of the current section /and/ the "named"
300 * section, which is one of the conventional ones, in order to
304 if (MDOC_HEAD
!= m
->last
->type
)
308 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
310 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
313 switch ((m
->lastsec
= sec
)) {
314 case (SEC_RETURN_VALUES
):
317 switch (m
->meta
.msec
) {
325 return(mdoc_nwarn(m
, m
->last
, EBADSEC
));
350 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
354 * --> title = unknown, volume = local, msec = 0, arch = NULL
357 if (NULL
== (n
= m
->last
->child
)) {
358 if (NULL
== (m
->meta
.title
= strdup("unknown")))
359 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
360 if (NULL
== (m
->meta
.vol
= strdup("local")))
361 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
362 return(post_prol(m
));
365 /* Handles: `.Dt TITLE'
366 * --> title = TITLE, volume = local, msec = 0, arch = NULL
369 if (NULL
== (m
->meta
.title
= strdup(n
->string
)))
370 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
372 if (NULL
== (n
= n
->next
)) {
373 if (NULL
== (m
->meta
.vol
= strdup("local")))
374 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
375 return(post_prol(m
));
378 /* Handles: `.Dt TITLE SEC'
379 * --> title = TITLE, volume = SEC is msec ?
380 * format(msec) : SEC,
381 * msec = SEC is msec ? atoi(msec) : 0,
385 cp
= mdoc_a2msec(n
->string
);
387 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
388 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
390 lval
= strtol(n
->string
, &ep
, 10);
391 if (n
->string
[0] != '\0' && *ep
== '\0')
392 m
->meta
.msec
= (int)lval
;
393 } else if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
394 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
396 if (NULL
== (n
= n
->next
))
397 return(post_prol(m
));
399 /* Handles: `.Dt TITLE SEC VOL'
400 * --> title = TITLE, volume = VOL is vol ?
402 * VOL is arch ? format(arch) :
406 cp
= mdoc_a2vol(n
->string
);
409 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
410 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
413 cp
= mdoc_a2arch(n
->string
);
416 if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
417 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
418 } else if (NULL
== (m
->meta
.arch
= strdup(cp
)))
419 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
422 /* Ignore any subsequent parameters... */
424 return(post_prol(m
));
432 struct utsname utsname
;
438 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
442 if (-1 == uname(&utsname
))
443 return(mdoc_nerr(m
, m
->last
, EUTSNAME
));
444 if (strlcat(buf
, utsname
.sysname
, 64) >= 64)
445 return(mdoc_nerr(m
, m
->last
, ETOOLONG
));
446 if (strlcat(buf
, " ", 64) >= 64)
447 return(mdoc_nerr(m
, m
->last
, ETOOLONG
));
448 if (strlcat(buf
, utsname
.release
, 64) >= 64)
449 return(mdoc_nerr(m
, m
->last
, ETOOLONG
));
452 if (NULL
== (m
->meta
.os
= strdup(buf
)))
453 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
455 m
->flags
|= MDOC_PBODY
;
456 return(post_prol(m
));
461 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
462 * Uses the first head macro.
465 post_bl_tagwidth(struct mdoc
*m
)
472 * Use the text width, if a text node, or the default macro
476 n
= m
->last
->body
->child
;
478 assert(MDOC_BLOCK
== n
->type
);
479 assert(MDOC_It
== n
->tok
);
483 sz
= 10; /* Default size. */
486 if (MDOC_TEXT
!= n
->type
) {
487 if (0 == (sz
= (int)mdoc_macro2len(n
->tok
)))
488 if ( ! mdoc_nwarn(m
, m
->last
, ENOWIDTH
))
491 sz
= (int)strlen(n
->string
) + 1;
494 if (-1 == snprintf(buf
, sizeof(buf
), "%dn", sz
))
495 return(mdoc_nerr(m
, m
->last
, ENUMFMT
));
498 * We have to dynamically add this to the macro's argument list.
499 * We're guaranteed that a MDOC_Width doesn't already exist.
504 sz
= (int)(n
->args
->argc
)++;
506 n
->args
->argv
= realloc(n
->args
->argv
,
507 n
->args
->argc
* sizeof(struct mdoc_argv
));
509 if (NULL
== n
->args
->argv
)
510 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
512 n
->args
->argv
[sz
].arg
= MDOC_Width
;
513 n
->args
->argv
[sz
].line
= m
->last
->line
;
514 n
->args
->argv
[sz
].pos
= m
->last
->pos
;
515 n
->args
->argv
[sz
].sz
= 1;
516 n
->args
->argv
[sz
].value
= calloc(1, sizeof(char *));
518 if (NULL
== n
->args
->argv
[sz
].value
)
519 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
520 if (NULL
== (n
->args
->argv
[sz
].value
[0] = strdup(buf
)))
521 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
528 post_bl_width(struct mdoc
*m
)
535 if (NULL
== m
->last
->args
)
538 for (i
= 0; i
< (int)m
->last
->args
->argc
; i
++)
539 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
542 if (i
== (int)m
->last
->args
->argc
)
544 p
= m
->last
->args
->argv
[i
].value
[0];
547 * If the value to -width is a macro, then we re-write it to be
548 * the macro's width as set in share/tmac/mdoc/doc-common.
551 if (0 == strcmp(p
, "Ds"))
553 else if (MDOC_MAX
== (tok
= mdoc_hash_find(m
->htab
, p
)))
555 else if (0 == (width
= mdoc_macro2len(tok
)))
556 return(mdoc_nwarn(m
, m
->last
, ENOWIDTH
));
558 /* The value already exists: free and reallocate it. */
560 if (-1 == snprintf(buf
, sizeof(buf
), "%zun", width
))
561 return(mdoc_nerr(m
, m
->last
, ENUMFMT
));
563 free(m
->last
->args
->argv
[i
].value
[0]);
564 m
->last
->args
->argv
[i
].value
[0] = strdup(buf
);
565 if (NULL
== m
->last
->args
->argv
[i
].value
[0])
566 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
573 post_bl_head(POST_ARGS
)
576 struct mdoc_node
*n
, *nn
, *nnp
;
578 if (NULL
== m
->last
->child
)
584 for (c
= 0; c
< (int)n
->args
->argc
; c
++)
585 if (MDOC_Column
== n
->args
->argv
[c
].arg
)
588 /* Only process -column. */
590 if (c
== (int)n
->args
->argc
)
593 assert(0 == n
->args
->argv
[c
].sz
);
596 * Accomodate for new-style groff column syntax. Shuffle the
597 * child nodes, all of which must be TEXT, as arguments for the
598 * column field. Then, delete the head children.
601 n
->args
->argv
[c
].sz
= (size_t)m
->last
->nchild
;
602 n
->args
->argv
[c
].value
= malloc
603 ((size_t)m
->last
->nchild
* sizeof(char *));
605 for (i
= 0, nn
= m
->last
->child
; nn
; i
++) {
606 n
->args
->argv
[c
].value
[i
] = nn
->string
;
614 m
->last
->child
= NULL
;
625 if (MDOC_HEAD
== m
->last
->type
)
626 return(post_bl_head(m
));
627 if (MDOC_BLOCK
!= m
->last
->type
)
631 * These are fairly complicated, so we've broken them into two
632 * functions. post_bl_tagwidth() is called when a -tag is
633 * specified, but no -width (it must be guessed). The second
634 * when a -width is specified (macro indicators must be
635 * rewritten into real lengths).
638 len
= (int)(m
->last
->args
? m
->last
->args
->argc
: 0);
640 for (r
= i
= 0; i
< len
; i
++) {
641 if (MDOC_Tag
== m
->last
->args
->argv
[i
].arg
)
643 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
647 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
648 if ( ! post_bl_tagwidth(m
))
650 } else if (r
& (1 << 1))
651 if ( ! post_bl_width(m
))
667 m
->next
= MDOC_NEXT_CHILD
;
669 /* XXX: this isn't documented anywhere! */
670 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
675 m
->next
= MDOC_NEXT_SIBLING
;
689 m
->next
= MDOC_NEXT_CHILD
;
690 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
691 m
->last
->pos
, "file"))
693 m
->next
= MDOC_NEXT_SIBLING
;
694 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
695 m
->last
->pos
, "..."))
699 m
->next
= MDOC_NEXT_SIBLING
;
710 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
713 if (0 == (m
->meta
.date
= mdoc_atotime(buf
))) {
714 if ( ! mdoc_nwarn(m
, m
->last
, EBADDATE
))
716 m
->meta
.date
= time(NULL
);
719 return(post_prol(m
));
729 * The end document shouldn't have the prologue macros as part
730 * of the syntax tree (they encompass only meta-data).
733 if (m
->last
->parent
->child
== m
->last
)
734 m
->last
->parent
->child
= m
->last
->prev
;
736 m
->last
->prev
->next
= NULL
;
739 assert(NULL
== m
->last
->next
);
742 m
->last
= m
->last
->prev
;
743 m
->next
= MDOC_NEXT_SIBLING
;
745 m
->last
= m
->last
->parent
;
746 m
->next
= MDOC_NEXT_CHILD
;
749 mdoc_node_freelist(n
);
758 if (MDOC_BODY
== n
->type
)
759 m
->flags
|= MDOC_LITERAL
;
769 if (MDOC_BODY
!= n
->type
)
772 /* Enter literal context if `Bd -literal' or * -unfilled'. */
774 for (n
= n
->parent
, i
= 0; i
< (int)n
->args
->argc
; i
++)
775 if (MDOC_Literal
== n
->args
->argv
[i
].arg
)
777 else if (MDOC_Unfilled
== n
->args
->argv
[i
].arg
)
780 if (i
< (int)n
->args
->argc
)
781 m
->flags
|= MDOC_LITERAL
;
788 post_display(POST_ARGS
)
791 if (MDOC_BODY
== m
->last
->type
)
792 m
->flags
&= ~MDOC_LITERAL
;