]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
30a758528667e6d9214b9b44d695c7d57acaca8b
1 /* $Id: mdoc_action.c,v 1.26 2009/07/12 20:30:35 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_st(POST_ARGS
);
50 static int post_std(POST_ARGS
);
52 static int pre_bd(PRE_ARGS
);
53 static int pre_dl(PRE_ARGS
);
55 const struct actions mdoc_actions
[MDOC_MAX
] = {
56 { NULL
, NULL
}, /* Ap */
57 { NULL
, post_dd
}, /* Dd */
58 { NULL
, post_dt
}, /* Dt */
59 { NULL
, post_os
}, /* Os */
60 { NULL
, post_sh
}, /* Sh */
61 { NULL
, NULL
}, /* Ss */
62 { NULL
, NULL
}, /* Pp */
63 { NULL
, NULL
}, /* D1 */
64 { pre_dl
, post_display
}, /* Dl */
65 { pre_bd
, post_display
}, /* Bd */
66 { NULL
, NULL
}, /* Ed */
67 { NULL
, post_bl
}, /* Bl */
68 { NULL
, NULL
}, /* El */
69 { NULL
, NULL
}, /* It */
70 { NULL
, NULL
}, /* Ad */
71 { NULL
, NULL
}, /* An */
72 { NULL
, post_ar
}, /* Ar */
73 { NULL
, NULL
}, /* Cd */ /* FIXME: tabs are accepted! */
74 { NULL
, NULL
}, /* Cm */
75 { NULL
, NULL
}, /* Dv */
76 { NULL
, NULL
}, /* Er */
77 { NULL
, NULL
}, /* Ev */
78 { NULL
, post_std
}, /* Ex */
79 { NULL
, NULL
}, /* Fa */
80 { NULL
, NULL
}, /* Fd */
81 { NULL
, NULL
}, /* Fl */
82 { NULL
, NULL
}, /* Fn */
83 { NULL
, NULL
}, /* Ft */
84 { NULL
, NULL
}, /* Ic */
85 { NULL
, NULL
}, /* In */
86 { NULL
, NULL
}, /* Li */
87 { NULL
, NULL
}, /* Nd */
88 { NULL
, post_nm
}, /* Nm */
89 { NULL
, NULL
}, /* Op */
90 { NULL
, NULL
}, /* Ot */
91 { NULL
, NULL
}, /* Pa */
92 { NULL
, post_std
}, /* Rv */
93 { NULL
, post_st
}, /* St */
94 { NULL
, NULL
}, /* Va */
95 { NULL
, NULL
}, /* Vt */
96 { NULL
, NULL
}, /* Xr */
97 { NULL
, NULL
}, /* %A */
98 { NULL
, NULL
}, /* %B */
99 { NULL
, NULL
}, /* %D */
100 { NULL
, NULL
}, /* %I */
101 { NULL
, NULL
}, /* %J */
102 { NULL
, NULL
}, /* %N */
103 { NULL
, NULL
}, /* %O */
104 { NULL
, NULL
}, /* %P */
105 { NULL
, NULL
}, /* %R */
106 { NULL
, NULL
}, /* %T */
107 { NULL
, NULL
}, /* %V */
108 { NULL
, NULL
}, /* Ac */
109 { NULL
, NULL
}, /* Ao */
110 { NULL
, NULL
}, /* Aq */
111 { NULL
, post_at
}, /* At */
112 { NULL
, NULL
}, /* Bc */
113 { NULL
, NULL
}, /* Bf */
114 { NULL
, NULL
}, /* Bo */
115 { NULL
, NULL
}, /* Bq */
116 { NULL
, NULL
}, /* Bsx */
117 { NULL
, NULL
}, /* Bx */
118 { NULL
, NULL
}, /* Db */
119 { NULL
, NULL
}, /* Dc */
120 { NULL
, NULL
}, /* Do */
121 { NULL
, NULL
}, /* Dq */
122 { NULL
, NULL
}, /* Ec */
123 { NULL
, NULL
}, /* Ef */
124 { NULL
, NULL
}, /* Em */
125 { NULL
, NULL
}, /* Eo */
126 { NULL
, NULL
}, /* Fx */
127 { NULL
, NULL
}, /* Ms */
128 { NULL
, NULL
}, /* No */
129 { NULL
, NULL
}, /* Ns */
130 { NULL
, NULL
}, /* Nx */
131 { NULL
, NULL
}, /* Ox */
132 { NULL
, NULL
}, /* Pc */
133 { NULL
, NULL
}, /* Pf */
134 { NULL
, NULL
}, /* Po */
135 { NULL
, NULL
}, /* Pq */
136 { NULL
, NULL
}, /* Qc */
137 { NULL
, NULL
}, /* Ql */
138 { NULL
, NULL
}, /* Qo */
139 { NULL
, NULL
}, /* Qq */
140 { NULL
, NULL
}, /* Re */
141 { NULL
, NULL
}, /* Rs */
142 { NULL
, NULL
}, /* Sc */
143 { NULL
, NULL
}, /* So */
144 { NULL
, NULL
}, /* Sq */
145 { NULL
, NULL
}, /* Sm */
146 { NULL
, NULL
}, /* Sx */
147 { NULL
, NULL
}, /* Sy */
148 { NULL
, NULL
}, /* Tn */
149 { NULL
, NULL
}, /* Ux */
150 { NULL
, NULL
}, /* Xc */
151 { NULL
, NULL
}, /* Xo */
152 { NULL
, NULL
}, /* Fo */
153 { NULL
, NULL
}, /* Fc */
154 { NULL
, NULL
}, /* Oo */
155 { NULL
, NULL
}, /* Oc */
156 { NULL
, NULL
}, /* Bk */
157 { NULL
, NULL
}, /* Ek */
158 { NULL
, NULL
}, /* Bt */
159 { NULL
, NULL
}, /* Hf */
160 { NULL
, NULL
}, /* Fr */
161 { NULL
, NULL
}, /* Ud */
162 { NULL
, NULL
}, /* Lb */
163 { NULL
, NULL
}, /* Lp */
164 { NULL
, post_lk
}, /* Lk */
165 { NULL
, NULL
}, /* Mt */
166 { NULL
, NULL
}, /* Brq */
167 { NULL
, NULL
}, /* Bro */
168 { NULL
, NULL
}, /* Brc */
169 { NULL
, NULL
}, /* %C */
170 { NULL
, NULL
}, /* Es */
171 { NULL
, NULL
}, /* En */
172 { NULL
, NULL
}, /* Dx */
173 { NULL
, NULL
}, /* %Q */
176 static int concat(struct mdoc
*, const struct mdoc_node
*,
180 extern size_t strlcat(char *, const char *, size_t);
185 mdoc_action_pre(struct mdoc
*m
, const struct mdoc_node
*n
)
197 if (NULL
== mdoc_actions
[n
->tok
].pre
)
199 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
204 mdoc_action_post(struct mdoc
*m
)
207 if (MDOC_ACTED
& m
->last
->flags
)
209 m
->last
->flags
|= MDOC_ACTED
;
211 switch (m
->last
->type
) {
220 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
222 return((*mdoc_actions
[m
->last
->tok
].post
)(m
));
227 concat(struct mdoc
*m
, const struct mdoc_node
*n
,
228 char *buf
, size_t sz
)
231 for ( ; n
; n
= n
->next
) {
232 assert(MDOC_TEXT
== n
->type
);
233 if (strlcat(buf
, n
->string
, sz
) >= sz
)
234 return(mdoc_nerr(m
, n
, ETOOLONG
));
237 if (strlcat(buf
, " ", sz
) >= sz
)
238 return(mdoc_nerr(m
, n
, ETOOLONG
));
250 * If '-std' is invoked without an argument, fill it in with our
251 * name (if it's been set).
254 if (NULL
== m
->last
->args
)
256 if (m
->last
->args
->argv
[0].sz
)
259 assert(m
->meta
.name
);
261 m
->last
->args
->argv
[0].value
= calloc(1, sizeof(char *));
262 if (NULL
== m
->last
->args
->argv
[0].value
)
263 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
265 m
->last
->args
->argv
[0].sz
= 1;
266 m
->last
->args
->argv
[0].value
[0] = strdup(m
->meta
.name
);
267 if (NULL
== m
->last
->args
->argv
[0].value
[0])
268 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
283 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
286 if (NULL
== (m
->meta
.name
= strdup(buf
)))
287 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
298 assert(MDOC_TEXT
== m
->last
->child
->type
);
299 p
= mdoc_a2st(m
->last
->child
->string
);
301 free(m
->last
->child
->string
);
302 m
->last
->child
->string
= strdup(p
);
303 if (NULL
== m
->last
->child
->string
)
304 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
315 if (m
->last
->child
) {
316 assert(MDOC_TEXT
== m
->last
->child
->type
);
317 p
= mdoc_a2att(m
->last
->child
->string
);
319 free(m
->last
->child
->string
);
320 m
->last
->child
->string
= strdup(p
);
321 if (NULL
== m
->last
->child
->string
)
322 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
327 m
->next
= MDOC_NEXT_CHILD
;
329 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, "AT&T UNIX"))
333 m
->next
= MDOC_NEXT_SIBLING
;
345 * We keep track of the current section /and/ the "named"
346 * section, which is one of the conventional ones, in order to
350 if (MDOC_HEAD
!= m
->last
->type
)
354 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
356 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
359 switch ((m
->lastsec
= sec
)) {
360 case (SEC_RETURN_VALUES
):
363 switch (m
->meta
.msec
) {
371 return(mdoc_nwarn(m
, m
->last
, EBADSEC
));
396 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
400 * --> title = unknown, volume = local, msec = 0, arch = NULL
403 if (NULL
== (n
= m
->last
->child
)) {
404 if (NULL
== (m
->meta
.title
= strdup("unknown")))
405 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
406 if (NULL
== (m
->meta
.vol
= strdup("local")))
407 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
408 return(post_prol(m
));
411 /* Handles: `.Dt TITLE'
412 * --> title = TITLE, volume = local, msec = 0, arch = NULL
415 if (NULL
== (m
->meta
.title
= strdup(n
->string
)))
416 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
418 if (NULL
== (n
= n
->next
)) {
419 if (NULL
== (m
->meta
.vol
= strdup("local")))
420 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
421 return(post_prol(m
));
424 /* Handles: `.Dt TITLE SEC'
425 * --> title = TITLE, volume = SEC is msec ?
426 * format(msec) : SEC,
427 * msec = SEC is msec ? atoi(msec) : 0,
431 cp
= mdoc_a2msec(n
->string
);
433 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
434 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
436 lval
= strtol(n
->string
, &ep
, 10);
437 if (n
->string
[0] != '\0' && *ep
== '\0')
438 m
->meta
.msec
= (int)lval
;
439 } else if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
440 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
442 if (NULL
== (n
= n
->next
))
443 return(post_prol(m
));
445 /* Handles: `.Dt TITLE SEC VOL'
446 * --> title = TITLE, volume = VOL is vol ?
448 * VOL is arch ? format(arch) :
452 cp
= mdoc_a2vol(n
->string
);
455 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
456 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
459 cp
= mdoc_a2arch(n
->string
);
462 if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
463 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
464 } else if (NULL
== (m
->meta
.arch
= strdup(cp
)))
465 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
468 /* Ignore any subsequent parameters... */
470 return(post_prol(m
));
478 struct utsname utsname
;
484 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
488 if (-1 == uname(&utsname
))
489 return(mdoc_nerr(m
, m
->last
, EUTSNAME
));
490 if (strlcat(buf
, utsname
.sysname
, 64) >= 64)
491 return(mdoc_nerr(m
, m
->last
, ETOOLONG
));
492 if (strlcat(buf
, " ", 64) >= 64)
493 return(mdoc_nerr(m
, m
->last
, ETOOLONG
));
494 if (strlcat(buf
, utsname
.release
, 64) >= 64)
495 return(mdoc_nerr(m
, m
->last
, ETOOLONG
));
498 if (NULL
== (m
->meta
.os
= strdup(buf
)))
499 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
501 m
->flags
|= MDOC_PBODY
;
502 return(post_prol(m
));
507 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
508 * Uses the first head macro.
511 post_bl_tagwidth(struct mdoc
*m
)
518 * Use the text width, if a text node, or the default macro
522 n
= m
->last
->body
->child
;
524 assert(MDOC_BLOCK
== n
->type
);
525 assert(MDOC_It
== n
->tok
);
529 sz
= 10; /* Default size. */
532 if (MDOC_TEXT
!= n
->type
) {
533 if (0 == (sz
= (int)mdoc_macro2len(n
->tok
)))
534 if ( ! mdoc_nwarn(m
, m
->last
, ENOWIDTH
))
537 sz
= (int)strlen(n
->string
) + 1;
540 if (-1 == snprintf(buf
, sizeof(buf
), "%dn", sz
))
541 return(mdoc_nerr(m
, m
->last
, ENUMFMT
));
544 * We have to dynamically add this to the macro's argument list.
545 * We're guaranteed that a MDOC_Width doesn't already exist.
550 sz
= (int)(n
->args
->argc
)++;
552 n
->args
->argv
= realloc(n
->args
->argv
,
553 n
->args
->argc
* sizeof(struct mdoc_argv
));
555 if (NULL
== n
->args
->argv
)
556 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
558 n
->args
->argv
[sz
].arg
= MDOC_Width
;
559 n
->args
->argv
[sz
].line
= m
->last
->line
;
560 n
->args
->argv
[sz
].pos
= m
->last
->pos
;
561 n
->args
->argv
[sz
].sz
= 1;
562 n
->args
->argv
[sz
].value
= calloc(1, sizeof(char *));
564 if (NULL
== n
->args
->argv
[sz
].value
)
565 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
566 if (NULL
== (n
->args
->argv
[sz
].value
[0] = strdup(buf
)))
567 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
574 post_bl_width(struct mdoc
*m
)
581 if (NULL
== m
->last
->args
)
584 for (i
= 0; i
< (int)m
->last
->args
->argc
; i
++)
585 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
588 if (i
== (int)m
->last
->args
->argc
)
590 p
= m
->last
->args
->argv
[i
].value
[0];
593 * If the value to -width is a macro, then we re-write it to be
594 * the macro's width as set in share/tmac/mdoc/doc-common.
597 if (0 == strcmp(p
, "Ds"))
599 else if (MDOC_MAX
== (tok
= mdoc_hash_find(m
->htab
, p
)))
601 else if (0 == (width
= mdoc_macro2len(tok
)))
602 return(mdoc_nwarn(m
, m
->last
, ENOWIDTH
));
604 /* The value already exists: free and reallocate it. */
606 if (-1 == snprintf(buf
, sizeof(buf
), "%zun", width
))
607 return(mdoc_nerr(m
, m
->last
, ENUMFMT
));
609 free(m
->last
->args
->argv
[i
].value
[0]);
610 m
->last
->args
->argv
[i
].value
[0] = strdup(buf
);
611 if (NULL
== m
->last
->args
->argv
[i
].value
[0])
612 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
619 post_bl_head(POST_ARGS
)
622 struct mdoc_node
*n
, *nn
, *nnp
;
624 if (NULL
== m
->last
->child
)
630 for (c
= 0; c
< (int)n
->args
->argc
; c
++)
631 if (MDOC_Column
== n
->args
->argv
[c
].arg
)
634 /* Only process -column. */
636 if (c
== (int)n
->args
->argc
)
639 assert(0 == n
->args
->argv
[c
].sz
);
642 * Accomodate for new-style groff column syntax. Shuffle the
643 * child nodes, all of which must be TEXT, as arguments for the
644 * column field. Then, delete the head children.
647 n
->args
->argv
[c
].sz
= (size_t)m
->last
->nchild
;
648 n
->args
->argv
[c
].value
= malloc
649 ((size_t)m
->last
->nchild
* sizeof(char *));
651 for (i
= 0, nn
= m
->last
->child
; nn
; i
++) {
652 n
->args
->argv
[c
].value
[i
] = nn
->string
;
660 m
->last
->child
= NULL
;
671 if (MDOC_HEAD
== m
->last
->type
)
672 return(post_bl_head(m
));
673 if (MDOC_BLOCK
!= m
->last
->type
)
677 * These are fairly complicated, so we've broken them into two
678 * functions. post_bl_tagwidth() is called when a -tag is
679 * specified, but no -width (it must be guessed). The second
680 * when a -width is specified (macro indicators must be
681 * rewritten into real lengths).
684 len
= (int)(m
->last
->args
? m
->last
->args
->argc
: 0);
686 for (r
= i
= 0; i
< len
; i
++) {
687 if (MDOC_Tag
== m
->last
->args
->argv
[i
].arg
)
689 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
693 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
694 if ( ! post_bl_tagwidth(m
))
696 } else if (r
& (1 << 1))
697 if ( ! post_bl_width(m
))
713 m
->next
= MDOC_NEXT_CHILD
;
715 /* XXX: this isn't documented anywhere! */
716 if ( ! mdoc_word_alloc(m
, m
->last
->line
, m
->last
->pos
, "~"))
720 m
->next
= MDOC_NEXT_SIBLING
;
734 m
->next
= MDOC_NEXT_CHILD
;
735 if ( ! mdoc_word_alloc(m
, m
->last
->line
, m
->last
->pos
, "file"))
737 m
->next
= MDOC_NEXT_SIBLING
;
738 if ( ! mdoc_word_alloc(m
, m
->last
->line
, m
->last
->pos
, "..."))
742 m
->next
= MDOC_NEXT_SIBLING
;
753 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
756 if (0 == (m
->meta
.date
= mdoc_atotime(buf
))) {
757 if ( ! mdoc_nwarn(m
, m
->last
, EBADDATE
))
759 m
->meta
.date
= time(NULL
);
762 return(post_prol(m
));
772 * The end document shouldn't have the prologue macros as part
773 * of the syntax tree (they encompass only meta-data).
776 if (m
->last
->parent
->child
== m
->last
)
777 m
->last
->parent
->child
= m
->last
->prev
;
779 m
->last
->prev
->next
= NULL
;
782 assert(NULL
== m
->last
->next
);
785 m
->last
= m
->last
->prev
;
786 m
->next
= MDOC_NEXT_SIBLING
;
788 m
->last
= m
->last
->parent
;
789 m
->next
= MDOC_NEXT_CHILD
;
792 mdoc_node_freelist(n
);
801 if (MDOC_BODY
== n
->type
)
802 m
->flags
|= MDOC_LITERAL
;
812 if (MDOC_BODY
!= n
->type
)
815 /* Enter literal context if `Bd -literal' or * -unfilled'. */
817 for (n
= n
->parent
, i
= 0; i
< (int)n
->args
->argc
; i
++)
818 if (MDOC_Literal
== n
->args
->argv
[i
].arg
)
820 else if (MDOC_Unfilled
== n
->args
->argv
[i
].arg
)
823 if (i
< (int)n
->args
->argc
)
824 m
->flags
|= MDOC_LITERAL
;
831 post_display(POST_ARGS
)
834 if (MDOC_BODY
== m
->last
->type
)
835 m
->flags
&= ~MDOC_LITERAL
;