]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.15 2009/06/17 14:10:09 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>
39 #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
40 #define POST_ARGS struct mdoc *m
44 int (*post
)(POST_ARGS
);
47 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
48 static int perr(struct mdoc
*, int, int, enum merr
);
49 static int concat(struct mdoc
*, const struct mdoc_node
*,
52 static int post_ar(POST_ARGS
);
53 static int post_bl(POST_ARGS
);
54 static int post_bl_head(POST_ARGS
);
55 static int post_bl_width(POST_ARGS
);
56 static int post_bl_tagwidth(POST_ARGS
);
57 static int post_dd(POST_ARGS
);
58 static int post_display(POST_ARGS
);
59 static int post_dt(POST_ARGS
);
60 static int post_lk(POST_ARGS
);
61 static int post_nm(POST_ARGS
);
62 static int post_os(POST_ARGS
);
63 static int post_prol(POST_ARGS
);
64 static int post_sh(POST_ARGS
);
65 static int post_std(POST_ARGS
);
67 static int pre_bd(PRE_ARGS
);
68 static int pre_dl(PRE_ARGS
);
70 #define vwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t))
71 #define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
72 #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
74 const struct actions mdoc_actions
[MDOC_MAX
] = {
75 { NULL
, NULL
}, /* Ap */
76 { NULL
, post_dd
}, /* Dd */
77 { NULL
, post_dt
}, /* Dt */
78 { NULL
, post_os
}, /* Os */
79 { NULL
, post_sh
}, /* Sh */
80 { NULL
, NULL
}, /* Ss */
81 { NULL
, NULL
}, /* Pp */
82 { NULL
, NULL
}, /* D1 */
83 { pre_dl
, post_display
}, /* Dl */
84 { pre_bd
, post_display
}, /* Bd */
85 { NULL
, NULL
}, /* Ed */
86 { NULL
, post_bl
}, /* Bl */
87 { NULL
, NULL
}, /* El */
88 { NULL
, NULL
}, /* It */
89 { NULL
, NULL
}, /* Ad */
90 { NULL
, NULL
}, /* An */
91 { NULL
, post_ar
}, /* Ar */
92 { NULL
, NULL
}, /* Cd */
93 { NULL
, NULL
}, /* Cm */
94 { NULL
, NULL
}, /* Dv */
95 { NULL
, NULL
}, /* Er */
96 { NULL
, NULL
}, /* Ev */
97 { NULL
, post_std
}, /* Ex */
98 { NULL
, NULL
}, /* Fa */
99 { NULL
, NULL
}, /* Fd */
100 { NULL
, NULL
}, /* Fl */
101 { NULL
, NULL
}, /* Fn */
102 { NULL
, NULL
}, /* Ft */
103 { NULL
, NULL
}, /* Ic */
104 { NULL
, NULL
}, /* In */
105 { NULL
, NULL
}, /* Li */
106 { NULL
, NULL
}, /* Nd */
107 { NULL
, post_nm
}, /* Nm */
108 { NULL
, NULL
}, /* Op */
109 { NULL
, NULL
}, /* Ot */
110 { NULL
, NULL
}, /* Pa */
111 { NULL
, post_std
}, /* Rv */
112 { NULL
, NULL
}, /* St */
113 { NULL
, NULL
}, /* Va */
114 { NULL
, NULL
}, /* Vt */
115 { NULL
, NULL
}, /* Xr */
116 { NULL
, NULL
}, /* %A */
117 { NULL
, NULL
}, /* %B */
118 { NULL
, NULL
}, /* %D */
119 { NULL
, NULL
}, /* %I */
120 { NULL
, NULL
}, /* %J */
121 { NULL
, NULL
}, /* %N */
122 { NULL
, NULL
}, /* %O */
123 { NULL
, NULL
}, /* %P */
124 { NULL
, NULL
}, /* %R */
125 { NULL
, NULL
}, /* %T */
126 { NULL
, NULL
}, /* %V */
127 { NULL
, NULL
}, /* Ac */
128 { NULL
, NULL
}, /* Ao */
129 { NULL
, NULL
}, /* Aq */
130 { NULL
, NULL
}, /* At */
131 { NULL
, NULL
}, /* Bc */
132 { NULL
, NULL
}, /* Bf */
133 { NULL
, NULL
}, /* Bo */
134 { NULL
, NULL
}, /* Bq */
135 { NULL
, NULL
}, /* Bsx */
136 { NULL
, NULL
}, /* Bx */
137 { NULL
, NULL
}, /* Db */
138 { NULL
, NULL
}, /* Dc */
139 { NULL
, NULL
}, /* Do */
140 { NULL
, NULL
}, /* Dq */
141 { NULL
, NULL
}, /* Ec */
142 { NULL
, NULL
}, /* Ef */
143 { NULL
, NULL
}, /* Em */
144 { NULL
, NULL
}, /* Eo */
145 { NULL
, NULL
}, /* Fx */
146 { NULL
, NULL
}, /* Ms */
147 { NULL
, NULL
}, /* No */
148 { NULL
, NULL
}, /* Ns */
149 { NULL
, NULL
}, /* Nx */
150 { NULL
, NULL
}, /* Ox */
151 { NULL
, NULL
}, /* Pc */
152 { NULL
, NULL
}, /* Pf */
153 { NULL
, NULL
}, /* Po */
154 { NULL
, NULL
}, /* Pq */
155 { NULL
, NULL
}, /* Qc */
156 { NULL
, NULL
}, /* Ql */
157 { NULL
, NULL
}, /* Qo */
158 { NULL
, NULL
}, /* Qq */
159 { NULL
, NULL
}, /* Re */
160 { NULL
, NULL
}, /* Rs */
161 { NULL
, NULL
}, /* Sc */
162 { NULL
, NULL
}, /* So */
163 { NULL
, NULL
}, /* Sq */
164 { NULL
, NULL
}, /* Sm */
165 { NULL
, NULL
}, /* Sx */
166 { NULL
, NULL
}, /* Sy */
167 { NULL
, NULL
}, /* Tn */
168 { NULL
, NULL
}, /* Ux */
169 { NULL
, NULL
}, /* Xc */
170 { NULL
, NULL
}, /* Xo */
171 { NULL
, NULL
}, /* Fo */
172 { NULL
, NULL
}, /* Fc */
173 { NULL
, NULL
}, /* Oo */
174 { NULL
, NULL
}, /* Oc */
175 { NULL
, NULL
}, /* Bk */
176 { NULL
, NULL
}, /* Ek */
177 { NULL
, NULL
}, /* Bt */
178 { NULL
, NULL
}, /* Hf */
179 { NULL
, NULL
}, /* Fr */
180 { NULL
, NULL
}, /* Ud */
181 { NULL
, NULL
}, /* Lb */
182 { NULL
, NULL
}, /* Lp */
183 { NULL
, post_lk
}, /* Lk */
184 { NULL
, NULL
}, /* Mt */
185 { NULL
, NULL
}, /* Brq */
186 { NULL
, NULL
}, /* Bro */
187 { NULL
, NULL
}, /* Brc */
188 { NULL
, NULL
}, /* %C */
189 { NULL
, NULL
}, /* Es */
190 { NULL
, NULL
}, /* En */
191 { NULL
, NULL
}, /* Dx */
192 { NULL
, NULL
}, /* %Q */
197 extern size_t strlcat(char *, const char *, size_t);
202 mdoc_action_pre(struct mdoc
*m
, const struct mdoc_node
*n
)
214 if (NULL
== mdoc_actions
[n
->tok
].pre
)
216 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
221 mdoc_action_post(struct mdoc
*m
)
224 if (MDOC_ACTED
& m
->last
->flags
)
226 m
->last
->flags
|= MDOC_ACTED
;
228 switch (m
->last
->type
) {
237 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
239 return((*mdoc_actions
[m
->last
->tok
].post
)(m
));
244 concat(struct mdoc
*m
, const struct mdoc_node
*n
,
245 char *buf
, size_t sz
)
248 for ( ; n
; n
= n
->next
) {
249 assert(MDOC_TEXT
== n
->type
);
250 if (strlcat(buf
, n
->string
, sz
) >= sz
)
251 return(nerr(m
, n
, ETOOLONG
));
254 if (strlcat(buf
, " ", sz
) >= sz
)
255 return(nerr(m
, n
, ETOOLONG
));
263 perr(struct mdoc
*m
, int line
, int pos
, enum merr type
)
270 p
= "bad number format";
273 p
= "argument text too long";
276 p
= "memory exhausted";
280 return(mdoc_perr(m
, line
, pos
, p
));
285 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
294 p
= "inappropriate document section in manual section";
298 p
= "cannot determine default width";
301 p
= "malformed date syntax";
305 return(mdoc_pwarn(m
, line
, pos
, c
, p
));
314 * If '-std' is invoked without an argument, fill it in with our
315 * name (if it's been set).
318 if (NULL
== m
->last
->args
)
320 if (m
->last
->args
->argv
[0].sz
)
323 assert(m
->meta
.name
);
325 m
->last
->args
->argv
[0].value
= calloc(1, sizeof(char *));
326 if (NULL
== m
->last
->args
->argv
[0].value
)
327 return(verr(m
, EMALLOC
));
329 m
->last
->args
->argv
[0].sz
= 1;
330 m
->last
->args
->argv
[0].value
[0] = strdup(m
->meta
.name
);
331 if (NULL
== m
->last
->args
->argv
[0].value
[0])
332 return(verr(m
, EMALLOC
));
347 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
350 if (NULL
== (m
->meta
.name
= strdup(buf
)))
351 return(verr(m
, EMALLOC
));
364 * We keep track of the current section /and/ the "named"
365 * section, which is one of the conventional ones, in order to
369 if (MDOC_HEAD
!= m
->last
->type
)
373 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
375 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
378 switch ((m
->lastsec
= sec
)) {
379 case (SEC_RETURN_VALUES
):
382 switch (m
->meta
.msec
) {
390 return(vwarn(m
, WBADSEC
));
415 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
419 * --> title = unknown, volume = local, msec = 0, arch = NULL
422 if (NULL
== (n
= m
->last
->child
)) {
423 if (NULL
== (m
->meta
.title
= strdup("unknown")))
424 return(verr(m
, EMALLOC
));
425 if (NULL
== (m
->meta
.vol
= strdup("local")))
426 return(verr(m
, EMALLOC
));
427 return(post_prol(m
));
430 /* Handles: `.Dt TITLE'
431 * --> title = TITLE, volume = local, msec = 0, arch = NULL
434 if (NULL
== (m
->meta
.title
= strdup(n
->string
)))
435 return(verr(m
, EMALLOC
));
437 if (NULL
== (n
= n
->next
)) {
438 if (NULL
== (m
->meta
.vol
= strdup("local")))
439 return(verr(m
, EMALLOC
));
440 return(post_prol(m
));
443 /* Handles: `.Dt TITLE SEC'
444 * --> title = TITLE, volume = SEC is msec ?
445 * format(msec) : SEC,
446 * msec = SEC is msec ? atoi(msec) : 0,
450 cp
= mdoc_a2msec(n
->string
);
452 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
453 return(verr(m
, EMALLOC
));
455 lval
= strtol(n
->string
, &ep
, 10);
456 if (n
->string
[0] != '\0' && *ep
== '\0')
457 m
->meta
.msec
= (int)lval
;
458 } else if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
459 return(verr(m
, EMALLOC
));
461 if (NULL
== (n
= n
->next
))
462 return(post_prol(m
));
464 /* Handles: `.Dt TITLE SEC VOL'
465 * --> title = TITLE, volume = VOL is vol ?
467 * VOL is arch ? format(arch) :
471 cp
= mdoc_a2vol(n
->string
);
474 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
475 return(verr(m
, EMALLOC
));
478 cp
= mdoc_a2arch(n
->string
);
481 if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
482 return(verr(m
, EMALLOC
));
483 } else if (NULL
== (m
->meta
.arch
= strdup(cp
)))
484 return(verr(m
, EMALLOC
));
487 /* Ignore any subsequent parameters... */
489 return(post_prol(m
));
497 struct utsname utsname
;
503 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
507 if (-1 == uname(&utsname
))
508 return(mdoc_err(m
, "utsname"));
509 if (strlcat(buf
, utsname
.sysname
, 64) >= 64)
510 return(verr(m
, ETOOLONG
));
511 if (strlcat(buf
, " ", 64) >= 64)
512 return(verr(m
, ETOOLONG
));
513 if (strlcat(buf
, utsname
.release
, 64) >= 64)
514 return(verr(m
, ETOOLONG
));
517 if (NULL
== (m
->meta
.os
= strdup(buf
)))
518 return(verr(m
, EMALLOC
));
520 m
->flags
|= MDOC_PBODY
;
521 return(post_prol(m
));
526 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
527 * Uses the first head macro.
530 post_bl_tagwidth(struct mdoc
*m
)
537 * Use the text width, if a text node, or the default macro
541 n
= m
->last
->body
->child
;
543 assert(MDOC_BLOCK
== n
->type
);
544 assert(MDOC_It
== n
->tok
);
548 sz
= 10; /* Default size. */
551 if (MDOC_TEXT
!= n
->type
) {
552 if (0 == (sz
= (int)mdoc_macro2len(n
->tok
)))
553 if ( ! vwarn(m
, WNOWIDTH
))
556 sz
= (int)strlen(n
->string
) + 1;
559 if (-1 == snprintf(buf
, sizeof(buf
), "%dn", sz
))
560 return(verr(m
, ENUMFMT
));
563 * We have to dynamically add this to the macro's argument list.
564 * We're guaranteed that a MDOC_Width doesn't already exist.
569 sz
= (int)(n
->args
->argc
)++;
571 n
->args
->argv
= realloc(n
->args
->argv
,
572 n
->args
->argc
* sizeof(struct mdoc_argv
));
574 if (NULL
== n
->args
->argv
)
575 return(verr(m
, EMALLOC
));
577 n
->args
->argv
[sz
].arg
= MDOC_Width
;
578 n
->args
->argv
[sz
].line
= m
->last
->line
;
579 n
->args
->argv
[sz
].pos
= m
->last
->pos
;
580 n
->args
->argv
[sz
].sz
= 1;
581 n
->args
->argv
[sz
].value
= calloc(1, sizeof(char *));
583 if (NULL
== n
->args
->argv
[sz
].value
)
584 return(verr(m
, EMALLOC
));
585 if (NULL
== (n
->args
->argv
[sz
].value
[0] = strdup(buf
)))
586 return(verr(m
, EMALLOC
));
593 post_bl_width(struct mdoc
*m
)
600 if (NULL
== m
->last
->args
)
603 for (i
= 0; i
< (int)m
->last
->args
->argc
; i
++)
604 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
607 if (i
== (int)m
->last
->args
->argc
)
609 p
= m
->last
->args
->argv
[i
].value
[0];
612 * If the value to -width is a macro, then we re-write it to be
613 * the macro's width as set in share/tmac/mdoc/doc-common.
616 if (0 == strcmp(p
, "Ds"))
618 else if (MDOC_MAX
== (tok
= mdoc_hash_find(m
->htab
, p
)))
620 else if (0 == (width
= mdoc_macro2len(tok
)))
621 return(vwarn(m
, WNOWIDTH
));
623 /* The value already exists: free and reallocate it. */
625 if (-1 == snprintf(buf
, sizeof(buf
), "%zun", width
))
626 return(verr(m
, ENUMFMT
));
628 free(m
->last
->args
->argv
[i
].value
[0]);
629 m
->last
->args
->argv
[i
].value
[0] = strdup(buf
);
630 if (NULL
== m
->last
->args
->argv
[i
].value
[0])
631 return(verr(m
, EMALLOC
));
638 post_bl_head(POST_ARGS
)
641 struct mdoc_node
*n
, *nn
, *nnp
;
643 if (NULL
== m
->last
->child
)
649 for (c
= 0; c
< (int)n
->args
->argc
; c
++)
650 if (MDOC_Column
== n
->args
->argv
[c
].arg
)
653 /* Only process -column. */
655 if (c
== (int)n
->args
->argc
)
658 assert(0 == n
->args
->argv
[c
].sz
);
661 * Accomodate for new-style groff column syntax. Shuffle the
662 * child nodes, all of which must be TEXT, as arguments for the
663 * column field. Then, delete the head children.
666 for (i
= 0, nn
= m
->last
->child
; nn
; nn
= nn
->next
, i
++)
667 /* Count children. */;
669 n
->args
->argv
[c
].sz
= (size_t)i
;
670 n
->args
->argv
[c
].value
= malloc((size_t)i
* sizeof(char *));
672 for (i
= 0, nn
= m
->last
->child
; nn
; i
++) {
673 n
->args
->argv
[c
].value
[i
] = nn
->string
;
680 m
->last
->child
= NULL
;
690 if (MDOC_HEAD
== m
->last
->type
)
691 return(post_bl_head(m
));
692 if (MDOC_BLOCK
!= m
->last
->type
)
696 * These are fairly complicated, so we've broken them into two
697 * functions. post_bl_tagwidth() is called when a -tag is
698 * specified, but no -width (it must be guessed). The second
699 * when a -width is specified (macro indicators must be
700 * rewritten into real lengths).
703 len
= (int)(m
->last
->args
? m
->last
->args
->argc
: 0);
705 for (r
= i
= 0; i
< len
; i
++) {
706 if (MDOC_Tag
== m
->last
->args
->argv
[i
].arg
)
708 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
712 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
713 if ( ! post_bl_tagwidth(m
))
715 } else if (r
& (1 << 1))
716 if ( ! post_bl_width(m
))
732 m
->next
= MDOC_NEXT_CHILD
;
733 /* FIXME: this isn't documented anywhere! */
734 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
739 m
->next
= MDOC_NEXT_SIBLING
;
753 m
->next
= MDOC_NEXT_CHILD
;
754 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
755 m
->last
->pos
, "file"))
757 m
->next
= MDOC_NEXT_SIBLING
;
758 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
759 m
->last
->pos
, "..."))
763 m
->next
= MDOC_NEXT_SIBLING
;
774 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
777 if (0 == (m
->meta
.date
= mdoc_atotime(buf
))) {
778 if ( ! vwarn(m
, WBADDATE
))
780 m
->meta
.date
= time(NULL
);
783 return(post_prol(m
));
793 * The end document shouldn't have the prologue macros as part
794 * of the syntax tree (they encompass only meta-data).
797 if (m
->last
->parent
->child
== m
->last
)
798 m
->last
->parent
->child
= m
->last
->prev
;
800 m
->last
->prev
->next
= NULL
;
803 assert(NULL
== m
->last
->next
);
806 m
->last
= m
->last
->prev
;
807 m
->next
= MDOC_NEXT_SIBLING
;
809 m
->last
= m
->last
->parent
;
810 m
->next
= MDOC_NEXT_CHILD
;
813 mdoc_node_freelist(n
);
822 if (MDOC_BODY
!= n
->type
)
824 m
->flags
|= MDOC_LITERAL
;
834 if (MDOC_BODY
!= n
->type
)
837 /* Enter literal context if `Bd -literal' or * -unfilled'. */
839 for (n
= n
->parent
, i
= 0; i
< (int)n
->args
->argc
; i
++)
840 if (MDOC_Literal
== n
->args
->argv
[i
].arg
)
842 else if (MDOC_Unfilled
== n
->args
->argv
[i
].arg
)
845 if (i
< (int)n
->args
->argc
)
846 m
->flags
|= MDOC_LITERAL
;
853 post_display(POST_ARGS
)
856 if (MDOC_BODY
== m
->last
->type
)
857 m
->flags
&= ~MDOC_LITERAL
;