]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.12 2009/06/16 19:45:51 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_width(POST_ARGS
);
55 static int post_bl_tagwidth(POST_ARGS
);
56 static int post_dd(POST_ARGS
);
57 static int post_display(POST_ARGS
);
58 static int post_dt(POST_ARGS
);
59 static int post_lk(POST_ARGS
);
60 static int post_nm(POST_ARGS
);
61 static int post_os(POST_ARGS
);
62 static int post_prol(POST_ARGS
);
63 static int post_sh(POST_ARGS
);
64 static int post_std(POST_ARGS
);
66 static int pre_bd(PRE_ARGS
);
67 static int pre_dl(PRE_ARGS
);
69 #define vwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t))
70 #define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
71 #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
73 const struct actions mdoc_actions
[MDOC_MAX
] = {
74 { NULL
, NULL
}, /* Ap */
75 { NULL
, post_dd
}, /* Dd */
76 { NULL
, post_dt
}, /* Dt */
77 { NULL
, post_os
}, /* Os */
78 { NULL
, post_sh
}, /* Sh */
79 { NULL
, NULL
}, /* Ss */
80 { NULL
, NULL
}, /* Pp */
81 { NULL
, NULL
}, /* D1 */
82 { pre_dl
, post_display
}, /* Dl */
83 { pre_bd
, post_display
}, /* Bd */
84 { NULL
, NULL
}, /* Ed */
85 { NULL
, post_bl
}, /* Bl */
86 { NULL
, NULL
}, /* El */
87 { NULL
, NULL
}, /* It */
88 { NULL
, NULL
}, /* Ad */
89 { NULL
, NULL
}, /* An */
90 { NULL
, post_ar
}, /* Ar */
91 { NULL
, NULL
}, /* Cd */
92 { NULL
, NULL
}, /* Cm */
93 { NULL
, NULL
}, /* Dv */
94 { NULL
, NULL
}, /* Er */
95 { NULL
, NULL
}, /* Ev */
96 { NULL
, post_std
}, /* Ex */
97 { NULL
, NULL
}, /* Fa */
98 { NULL
, NULL
}, /* Fd */
99 { NULL
, NULL
}, /* Fl */
100 { NULL
, NULL
}, /* Fn */
101 { NULL
, NULL
}, /* Ft */
102 { NULL
, NULL
}, /* Ic */
103 { NULL
, NULL
}, /* In */
104 { NULL
, NULL
}, /* Li */
105 { NULL
, NULL
}, /* Nd */
106 { NULL
, post_nm
}, /* Nm */
107 { NULL
, NULL
}, /* Op */
108 { NULL
, NULL
}, /* Ot */
109 { NULL
, NULL
}, /* Pa */
110 { NULL
, post_std
}, /* Rv */
111 { NULL
, NULL
}, /* St */
112 { NULL
, NULL
}, /* Va */
113 { NULL
, NULL
}, /* Vt */
114 { NULL
, NULL
}, /* Xr */
115 { NULL
, NULL
}, /* %A */
116 { NULL
, NULL
}, /* %B */
117 { NULL
, NULL
}, /* %D */
118 { NULL
, NULL
}, /* %I */
119 { NULL
, NULL
}, /* %J */
120 { NULL
, NULL
}, /* %N */
121 { NULL
, NULL
}, /* %O */
122 { NULL
, NULL
}, /* %P */
123 { NULL
, NULL
}, /* %R */
124 { NULL
, NULL
}, /* %T */
125 { NULL
, NULL
}, /* %V */
126 { NULL
, NULL
}, /* Ac */
127 { NULL
, NULL
}, /* Ao */
128 { NULL
, NULL
}, /* Aq */
129 { NULL
, NULL
}, /* At */
130 { NULL
, NULL
}, /* Bc */
131 { NULL
, NULL
}, /* Bf */
132 { NULL
, NULL
}, /* Bo */
133 { NULL
, NULL
}, /* Bq */
134 { NULL
, NULL
}, /* Bsx */
135 { NULL
, NULL
}, /* Bx */
136 { NULL
, NULL
}, /* Db */
137 { NULL
, NULL
}, /* Dc */
138 { NULL
, NULL
}, /* Do */
139 { NULL
, NULL
}, /* Dq */
140 { NULL
, NULL
}, /* Ec */
141 { NULL
, NULL
}, /* Ef */
142 { NULL
, NULL
}, /* Em */
143 { NULL
, NULL
}, /* Eo */
144 { NULL
, NULL
}, /* Fx */
145 { NULL
, NULL
}, /* Ms */
146 { NULL
, NULL
}, /* No */
147 { NULL
, NULL
}, /* Ns */
148 { NULL
, NULL
}, /* Nx */
149 { NULL
, NULL
}, /* Ox */
150 { NULL
, NULL
}, /* Pc */
151 { NULL
, NULL
}, /* Pf */
152 { NULL
, NULL
}, /* Po */
153 { NULL
, NULL
}, /* Pq */
154 { NULL
, NULL
}, /* Qc */
155 { NULL
, NULL
}, /* Ql */
156 { NULL
, NULL
}, /* Qo */
157 { NULL
, NULL
}, /* Qq */
158 { NULL
, NULL
}, /* Re */
159 { NULL
, NULL
}, /* Rs */
160 { NULL
, NULL
}, /* Sc */
161 { NULL
, NULL
}, /* So */
162 { NULL
, NULL
}, /* Sq */
163 { NULL
, NULL
}, /* Sm */
164 { NULL
, NULL
}, /* Sx */
165 { NULL
, NULL
}, /* Sy */
166 { NULL
, NULL
}, /* Tn */
167 { NULL
, NULL
}, /* Ux */
168 { NULL
, NULL
}, /* Xc */
169 { NULL
, NULL
}, /* Xo */
170 { NULL
, NULL
}, /* Fo */
171 { NULL
, NULL
}, /* Fc */
172 { NULL
, NULL
}, /* Oo */
173 { NULL
, NULL
}, /* Oc */
174 { NULL
, NULL
}, /* Bk */
175 { NULL
, NULL
}, /* Ek */
176 { NULL
, NULL
}, /* Bt */
177 { NULL
, NULL
}, /* Hf */
178 { NULL
, NULL
}, /* Fr */
179 { NULL
, NULL
}, /* Ud */
180 { NULL
, NULL
}, /* Lb */
181 { NULL
, NULL
}, /* Lp */
182 { NULL
, post_lk
}, /* Lk */
183 { NULL
, NULL
}, /* Mt */
184 { NULL
, NULL
}, /* Brq */
185 { NULL
, NULL
}, /* Bro */
186 { NULL
, NULL
}, /* Brc */
187 { NULL
, NULL
}, /* %C */
188 { NULL
, NULL
}, /* Es */
189 { NULL
, NULL
}, /* En */
190 { NULL
, NULL
}, /* Dx */
191 { NULL
, NULL
}, /* %Q */
196 extern size_t strlcat(char *, const char *, size_t);
201 mdoc_action_pre(struct mdoc
*m
, const struct mdoc_node
*n
)
213 if (NULL
== mdoc_actions
[n
->tok
].pre
)
215 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
220 mdoc_action_post(struct mdoc
*m
)
223 if (MDOC_ACTED
& m
->last
->flags
)
225 m
->last
->flags
|= MDOC_ACTED
;
227 switch (m
->last
->type
) {
236 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
238 return((*mdoc_actions
[m
->last
->tok
].post
)(m
));
243 concat(struct mdoc
*m
, const struct mdoc_node
*n
,
244 char *buf
, size_t sz
)
247 for ( ; n
; n
= n
->next
) {
248 assert(MDOC_TEXT
== n
->type
);
249 if (strlcat(buf
, n
->string
, sz
) >= sz
)
250 return(nerr(m
, n
, ETOOLONG
));
253 if (strlcat(buf
, " ", sz
) >= sz
)
254 return(nerr(m
, n
, ETOOLONG
));
262 perr(struct mdoc
*m
, int line
, int pos
, enum merr type
)
269 p
= "bad number format";
272 p
= "argument text too long";
275 p
= "memory exhausted";
279 return(mdoc_perr(m
, line
, pos
, p
));
284 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
293 p
= "inappropriate document section in manual section";
297 p
= "cannot determine default width";
300 p
= "malformed date syntax";
304 return(mdoc_pwarn(m
, line
, pos
, c
, p
));
313 * If '-std' is invoked without an argument, fill it in with our
314 * name (if it's been set).
317 if (NULL
== m
->last
->args
)
319 if (m
->last
->args
->argv
[0].sz
)
322 assert(m
->meta
.name
);
324 m
->last
->args
->argv
[0].value
= calloc(1, sizeof(char *));
325 if (NULL
== m
->last
->args
->argv
[0].value
)
326 return(verr(m
, EMALLOC
));
328 m
->last
->args
->argv
[0].sz
= 1;
329 m
->last
->args
->argv
[0].value
[0] = strdup(m
->meta
.name
);
330 if (NULL
== m
->last
->args
->argv
[0].value
[0])
331 return(verr(m
, EMALLOC
));
346 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
349 if (NULL
== (m
->meta
.name
= strdup(buf
)))
350 return(verr(m
, EMALLOC
));
363 * We keep track of the current section /and/ the "named"
364 * section, which is one of the conventional ones, in order to
368 if (MDOC_HEAD
!= m
->last
->type
)
372 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
374 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
377 switch ((m
->lastsec
= sec
)) {
378 case (SEC_RETURN_VALUES
):
381 switch (m
->meta
.msec
) {
389 return(vwarn(m
, WBADSEC
));
414 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
418 * --> title = unknown, volume = local, msec = 0, arch = NULL
421 if (NULL
== (n
= m
->last
->child
)) {
422 if (NULL
== (m
->meta
.title
= strdup("unknown")))
423 return(verr(m
, EMALLOC
));
424 if (NULL
== (m
->meta
.vol
= strdup("local")))
425 return(verr(m
, EMALLOC
));
426 return(post_prol(m
));
429 /* Handles: `.Dt TITLE'
430 * --> title = TITLE, volume = local, msec = 0, arch = NULL
433 if (NULL
== (m
->meta
.title
= strdup(n
->string
)))
434 return(verr(m
, EMALLOC
));
436 if (NULL
== (n
= n
->next
)) {
437 if (NULL
== (m
->meta
.vol
= strdup("local")))
438 return(verr(m
, EMALLOC
));
439 return(post_prol(m
));
442 /* Handles: `.Dt TITLE SEC'
443 * --> title = TITLE, volume = SEC is msec ?
444 * format(msec) : SEC,
445 * msec = SEC is msec ? atoi(msec) : 0,
449 cp
= mdoc_a2msec(n
->string
);
451 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
452 return(verr(m
, EMALLOC
));
454 lval
= strtol(n
->string
, &ep
, 10);
455 if (n
->string
[0] != '\0' && *ep
== '\0')
456 m
->meta
.msec
= (int)lval
;
457 } else if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
458 return(verr(m
, EMALLOC
));
460 if (NULL
== (n
= n
->next
))
461 return(post_prol(m
));
463 /* Handles: `.Dt TITLE SEC VOL'
464 * --> title = TITLE, volume = VOL is vol ?
466 * VOL is arch ? format(arch) :
470 cp
= mdoc_a2vol(n
->string
);
473 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
474 return(verr(m
, EMALLOC
));
477 cp
= mdoc_a2arch(n
->string
);
480 if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
481 return(verr(m
, EMALLOC
));
482 } else if (NULL
== (m
->meta
.arch
= strdup(cp
)))
483 return(verr(m
, EMALLOC
));
486 /* Ignore any subsequent parameters... */
488 return(post_prol(m
));
496 struct utsname utsname
;
502 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
506 if (-1 == uname(&utsname
))
507 return(mdoc_err(m
, "utsname"));
508 if (strlcat(buf
, utsname
.sysname
, 64) >= 64)
509 return(verr(m
, ETOOLONG
));
510 if (strlcat(buf
, " ", 64) >= 64)
511 return(verr(m
, ETOOLONG
));
512 if (strlcat(buf
, utsname
.release
, 64) >= 64)
513 return(verr(m
, ETOOLONG
));
516 if (NULL
== (m
->meta
.os
= strdup(buf
)))
517 return(verr(m
, EMALLOC
));
518 m
->lastnamed
= m
->lastsec
= SEC_BODY
;
520 return(post_prol(m
));
525 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
526 * Uses the first head macro.
529 post_bl_tagwidth(struct mdoc
*m
)
536 * Use the text width, if a text node, or the default macro
540 n
= m
->last
->body
->child
;
542 assert(MDOC_BLOCK
== n
->type
);
543 assert(MDOC_It
== n
->tok
);
547 sz
= 10; /* Default size. */
550 if (MDOC_TEXT
!= n
->type
) {
551 if (0 == (sz
= (int)mdoc_macro2len(n
->tok
)))
552 if ( ! vwarn(m
, WNOWIDTH
))
555 sz
= (int)strlen(n
->string
) + 1;
558 if (-1 == snprintf(buf
, sizeof(buf
), "%dn", sz
))
559 return(verr(m
, ENUMFMT
));
562 * We have to dynamically add this to the macro's argument list.
563 * We're guaranteed that a MDOC_Width doesn't already exist.
568 sz
= (int)(n
->args
->argc
)++;
570 n
->args
->argv
= realloc(n
->args
->argv
,
571 n
->args
->argc
* sizeof(struct mdoc_argv
));
573 if (NULL
== n
->args
->argv
)
574 return(verr(m
, EMALLOC
));
576 n
->args
->argv
[sz
].arg
= MDOC_Width
;
577 n
->args
->argv
[sz
].line
= m
->last
->line
;
578 n
->args
->argv
[sz
].pos
= m
->last
->pos
;
579 n
->args
->argv
[sz
].sz
= 1;
580 n
->args
->argv
[sz
].value
= calloc(1, sizeof(char *));
582 if (NULL
== n
->args
->argv
[sz
].value
)
583 return(verr(m
, EMALLOC
));
584 if (NULL
== (n
->args
->argv
[sz
].value
[0] = strdup(buf
)))
585 return(verr(m
, EMALLOC
));
592 post_bl_width(struct mdoc
*m
)
599 if (NULL
== m
->last
->args
)
602 for (i
= 0; i
< (int)m
->last
->args
->argc
; i
++)
603 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
606 if (i
== (int)m
->last
->args
->argc
)
608 p
= m
->last
->args
->argv
[i
].value
[0];
611 * If the value to -width is a macro, then we re-write it to be
612 * the macro's width as set in share/tmac/mdoc/doc-common.
615 if (0 == strcmp(p
, "Ds"))
617 else if (MDOC_MAX
== (tok
= mdoc_hash_find(m
->htab
, p
)))
619 else if (0 == (width
= mdoc_macro2len(tok
)))
620 return(vwarn(m
, WNOWIDTH
));
622 /* The value already exists: free and reallocate it. */
624 if (-1 == snprintf(buf
, sizeof(buf
), "%zun", width
))
625 return(verr(m
, ENUMFMT
));
627 free(m
->last
->args
->argv
[i
].value
[0]);
628 m
->last
->args
->argv
[i
].value
[0] = strdup(buf
);
629 if (NULL
== m
->last
->args
->argv
[i
].value
[0])
630 return(verr(m
, EMALLOC
));
641 if (MDOC_BLOCK
!= m
->last
->type
)
645 * These are fairly complicated, so we've broken them into two
646 * functions. post_bl_tagwidth() is called when a -tag is
647 * specified, but no -width (it must be guessed). The second
648 * when a -width is specified (macro indicators must be
649 * rewritten into real lengths).
652 len
= (int)(m
->last
->args
? m
->last
->args
->argc
: 0);
654 for (r
= i
= 0; i
< len
; i
++) {
655 if (MDOC_Tag
== m
->last
->args
->argv
[i
].arg
)
657 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
661 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
662 if ( ! post_bl_tagwidth(m
))
664 } else if (r
& (1 << 1))
665 if ( ! post_bl_width(m
))
681 m
->next
= MDOC_NEXT_CHILD
;
682 /* FIXME: this isn't documented anywhere! */
683 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
688 m
->next
= MDOC_NEXT_SIBLING
;
702 m
->next
= MDOC_NEXT_CHILD
;
703 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
704 m
->last
->pos
, "file"))
706 m
->next
= MDOC_NEXT_SIBLING
;
707 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
708 m
->last
->pos
, "..."))
712 m
->next
= MDOC_NEXT_SIBLING
;
723 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
726 if (0 == (m
->meta
.date
= mdoc_atotime(buf
))) {
727 if ( ! vwarn(m
, WBADDATE
))
729 m
->meta
.date
= time(NULL
);
732 return(post_prol(m
));
742 * The end document shouldn't have the prologue macros as part
743 * of the syntax tree (they encompass only meta-data).
746 if (m
->last
->parent
->child
== m
->last
)
747 m
->last
->parent
->child
= m
->last
->prev
;
749 m
->last
->prev
->next
= NULL
;
752 assert(NULL
== m
->last
->next
);
755 m
->last
= m
->last
->prev
;
756 m
->next
= MDOC_NEXT_SIBLING
;
758 m
->last
= m
->last
->parent
;
759 m
->next
= MDOC_NEXT_CHILD
;
762 mdoc_node_freelist(n
);
771 if (MDOC_BODY
!= n
->type
)
773 m
->flags
|= MDOC_LITERAL
;
783 if (MDOC_BODY
!= n
->type
)
786 /* Enter literal context if `Bd -literal' or * -unfilled'. */
788 for (n
= n
->parent
, i
= 0; i
< (int)n
->args
->argc
; i
++)
789 if (MDOC_Literal
== n
->args
->argv
[i
].arg
)
791 else if (MDOC_Unfilled
== n
->args
->argv
[i
].arg
)
794 if (i
< (int)n
->args
->argc
)
795 m
->flags
|= MDOC_LITERAL
;
802 post_display(POST_ARGS
)
805 if (MDOC_BODY
== m
->last
->type
)
806 m
->flags
&= ~MDOC_LITERAL
;