]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.21 2009/07/06 09:21: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>
40 #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
41 #define POST_ARGS struct mdoc *m
45 int (*post
)(POST_ARGS
);
48 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
49 static int perr(struct mdoc
*, int, int, enum merr
);
50 static int concat(struct mdoc
*, const struct mdoc_node
*,
53 static int post_ar(POST_ARGS
);
54 static int post_bl(POST_ARGS
);
55 static int post_bl_head(POST_ARGS
);
56 static int post_bl_width(POST_ARGS
);
57 static int post_bl_tagwidth(POST_ARGS
);
58 static int post_dd(POST_ARGS
);
59 static int post_display(POST_ARGS
);
60 static int post_dt(POST_ARGS
);
61 static int post_lk(POST_ARGS
);
62 static int post_nm(POST_ARGS
);
63 static int post_os(POST_ARGS
);
64 static int post_prol(POST_ARGS
);
65 static int post_sh(POST_ARGS
);
66 static int post_std(POST_ARGS
);
68 static int pre_bd(PRE_ARGS
);
69 static int pre_dl(PRE_ARGS
);
71 #define vwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t))
72 #define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
73 #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
75 const struct actions mdoc_actions
[MDOC_MAX
] = {
76 { NULL
, NULL
}, /* Ap */
77 { NULL
, post_dd
}, /* Dd */
78 { NULL
, post_dt
}, /* Dt */
79 { NULL
, post_os
}, /* Os */
80 { NULL
, post_sh
}, /* Sh */
81 { NULL
, NULL
}, /* Ss */
82 { NULL
, NULL
}, /* Pp */
83 { NULL
, NULL
}, /* D1 */
84 { pre_dl
, post_display
}, /* Dl */
85 { pre_bd
, post_display
}, /* Bd */
86 { NULL
, NULL
}, /* Ed */
87 { NULL
, post_bl
}, /* Bl */
88 { NULL
, NULL
}, /* El */
89 { NULL
, NULL
}, /* It */
90 { NULL
, NULL
}, /* Ad */
91 { NULL
, NULL
}, /* An */
92 { NULL
, post_ar
}, /* Ar */
93 { NULL
, NULL
}, /* Cd */ /* FIXME: tabs are accepted! */
94 { NULL
, NULL
}, /* Cm */
95 { NULL
, NULL
}, /* Dv */
96 { NULL
, NULL
}, /* Er */
97 { NULL
, NULL
}, /* Ev */
98 { NULL
, post_std
}, /* Ex */
99 { NULL
, NULL
}, /* Fa */
100 { NULL
, NULL
}, /* Fd */
101 { NULL
, NULL
}, /* Fl */
102 { NULL
, NULL
}, /* Fn */
103 { NULL
, NULL
}, /* Ft */
104 { NULL
, NULL
}, /* Ic */
105 { NULL
, NULL
}, /* In */
106 { NULL
, NULL
}, /* Li */
107 { NULL
, NULL
}, /* Nd */
108 { NULL
, post_nm
}, /* Nm */
109 { NULL
, NULL
}, /* Op */
110 { NULL
, NULL
}, /* Ot */
111 { NULL
, NULL
}, /* Pa */
112 { NULL
, post_std
}, /* Rv */
113 { NULL
, NULL
}, /* St */
114 { NULL
, NULL
}, /* Va */
115 { NULL
, NULL
}, /* Vt */
116 { NULL
, NULL
}, /* Xr */
117 { NULL
, NULL
}, /* %A */
118 { NULL
, NULL
}, /* %B */
119 { NULL
, NULL
}, /* %D */
120 { NULL
, NULL
}, /* %I */
121 { NULL
, NULL
}, /* %J */
122 { NULL
, NULL
}, /* %N */
123 { NULL
, NULL
}, /* %O */
124 { NULL
, NULL
}, /* %P */
125 { NULL
, NULL
}, /* %R */
126 { NULL
, NULL
}, /* %T */
127 { NULL
, NULL
}, /* %V */
128 { NULL
, NULL
}, /* Ac */
129 { NULL
, NULL
}, /* Ao */
130 { NULL
, NULL
}, /* Aq */
131 { NULL
, NULL
}, /* At */
132 { NULL
, NULL
}, /* Bc */
133 { NULL
, NULL
}, /* Bf */
134 { NULL
, NULL
}, /* Bo */
135 { NULL
, NULL
}, /* Bq */
136 { NULL
, NULL
}, /* Bsx */
137 { NULL
, NULL
}, /* Bx */
138 { NULL
, NULL
}, /* Db */
139 { NULL
, NULL
}, /* Dc */
140 { NULL
, NULL
}, /* Do */
141 { NULL
, NULL
}, /* Dq */
142 { NULL
, NULL
}, /* Ec */
143 { NULL
, NULL
}, /* Ef */
144 { NULL
, NULL
}, /* Em */
145 { NULL
, NULL
}, /* Eo */
146 { NULL
, NULL
}, /* Fx */
147 { NULL
, NULL
}, /* Ms */
148 { NULL
, NULL
}, /* No */
149 { NULL
, NULL
}, /* Ns */
150 { NULL
, NULL
}, /* Nx */
151 { NULL
, NULL
}, /* Ox */
152 { NULL
, NULL
}, /* Pc */
153 { NULL
, NULL
}, /* Pf */
154 { NULL
, NULL
}, /* Po */
155 { NULL
, NULL
}, /* Pq */
156 { NULL
, NULL
}, /* Qc */
157 { NULL
, NULL
}, /* Ql */
158 { NULL
, NULL
}, /* Qo */
159 { NULL
, NULL
}, /* Qq */
160 { NULL
, NULL
}, /* Re */
161 { NULL
, NULL
}, /* Rs */
162 { NULL
, NULL
}, /* Sc */
163 { NULL
, NULL
}, /* So */
164 { NULL
, NULL
}, /* Sq */
165 { NULL
, NULL
}, /* Sm */
166 { NULL
, NULL
}, /* Sx */
167 { NULL
, NULL
}, /* Sy */
168 { NULL
, NULL
}, /* Tn */
169 { NULL
, NULL
}, /* Ux */
170 { NULL
, NULL
}, /* Xc */
171 { NULL
, NULL
}, /* Xo */
172 { NULL
, NULL
}, /* Fo */
173 { NULL
, NULL
}, /* Fc */
174 { NULL
, NULL
}, /* Oo */
175 { NULL
, NULL
}, /* Oc */
176 { NULL
, NULL
}, /* Bk */
177 { NULL
, NULL
}, /* Ek */
178 { NULL
, NULL
}, /* Bt */
179 { NULL
, NULL
}, /* Hf */
180 { NULL
, NULL
}, /* Fr */
181 { NULL
, NULL
}, /* Ud */
182 { NULL
, NULL
}, /* Lb */
183 { NULL
, NULL
}, /* Lp */
184 { NULL
, post_lk
}, /* Lk */
185 { NULL
, NULL
}, /* Mt */
186 { NULL
, NULL
}, /* Brq */
187 { NULL
, NULL
}, /* Bro */
188 { NULL
, NULL
}, /* Brc */
189 { NULL
, NULL
}, /* %C */
190 { NULL
, NULL
}, /* Es */
191 { NULL
, NULL
}, /* En */
192 { NULL
, NULL
}, /* Dx */
193 { NULL
, NULL
}, /* %Q */
198 extern size_t strlcat(char *, const char *, size_t);
203 mdoc_action_pre(struct mdoc
*m
, const struct mdoc_node
*n
)
215 if (NULL
== mdoc_actions
[n
->tok
].pre
)
217 return((*mdoc_actions
[n
->tok
].pre
)(m
, n
));
222 mdoc_action_post(struct mdoc
*m
)
225 if (MDOC_ACTED
& m
->last
->flags
)
227 m
->last
->flags
|= MDOC_ACTED
;
229 switch (m
->last
->type
) {
238 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
240 return((*mdoc_actions
[m
->last
->tok
].post
)(m
));
245 concat(struct mdoc
*m
, const struct mdoc_node
*n
,
246 char *buf
, size_t sz
)
249 for ( ; n
; n
= n
->next
) {
250 assert(MDOC_TEXT
== n
->type
);
251 if (strlcat(buf
, n
->string
, sz
) >= sz
)
252 return(nerr(m
, n
, ETOOLONG
));
255 if (strlcat(buf
, " ", sz
) >= sz
)
256 return(nerr(m
, n
, ETOOLONG
));
264 perr(struct mdoc
*m
, int line
, int pos
, enum merr type
)
271 p
= "bad number format";
274 p
= "argument text too long";
280 p
= "memory exhausted";
284 return(mdoc_perr(m
, line
, pos
, p
));
289 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
297 p
= "inappropriate document section in manual section";
300 p
= "cannot determine default width";
303 p
= "malformed date syntax";
308 return(mdoc_pwarn(m
, line
, pos
, p
));
317 * If '-std' is invoked without an argument, fill it in with our
318 * name (if it's been set).
321 if (NULL
== m
->last
->args
)
323 if (m
->last
->args
->argv
[0].sz
)
326 assert(m
->meta
.name
);
328 m
->last
->args
->argv
[0].value
= calloc(1, sizeof(char *));
329 if (NULL
== m
->last
->args
->argv
[0].value
)
330 return(verr(m
, EMALLOC
));
332 m
->last
->args
->argv
[0].sz
= 1;
333 m
->last
->args
->argv
[0].value
[0] = strdup(m
->meta
.name
);
334 if (NULL
== m
->last
->args
->argv
[0].value
[0])
335 return(verr(m
, EMALLOC
));
350 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
353 if (NULL
== (m
->meta
.name
= strdup(buf
)))
354 return(verr(m
, EMALLOC
));
367 * We keep track of the current section /and/ the "named"
368 * section, which is one of the conventional ones, in order to
372 if (MDOC_HEAD
!= m
->last
->type
)
376 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
378 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
381 switch ((m
->lastsec
= sec
)) {
382 case (SEC_RETURN_VALUES
):
385 switch (m
->meta
.msec
) {
393 return(vwarn(m
, WBADSEC
));
418 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
422 * --> title = unknown, volume = local, msec = 0, arch = NULL
425 if (NULL
== (n
= m
->last
->child
)) {
426 if (NULL
== (m
->meta
.title
= strdup("unknown")))
427 return(verr(m
, EMALLOC
));
428 if (NULL
== (m
->meta
.vol
= strdup("local")))
429 return(verr(m
, EMALLOC
));
430 return(post_prol(m
));
433 /* Handles: `.Dt TITLE'
434 * --> title = TITLE, volume = local, msec = 0, arch = NULL
437 if (NULL
== (m
->meta
.title
= strdup(n
->string
)))
438 return(verr(m
, EMALLOC
));
440 if (NULL
== (n
= n
->next
)) {
441 if (NULL
== (m
->meta
.vol
= strdup("local")))
442 return(verr(m
, EMALLOC
));
443 return(post_prol(m
));
446 /* Handles: `.Dt TITLE SEC'
447 * --> title = TITLE, volume = SEC is msec ?
448 * format(msec) : SEC,
449 * msec = SEC is msec ? atoi(msec) : 0,
453 cp
= mdoc_a2msec(n
->string
);
455 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
456 return(verr(m
, EMALLOC
));
458 lval
= strtol(n
->string
, &ep
, 10);
459 if (n
->string
[0] != '\0' && *ep
== '\0')
460 m
->meta
.msec
= (int)lval
;
461 } else if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
462 return(verr(m
, EMALLOC
));
464 if (NULL
== (n
= n
->next
))
465 return(post_prol(m
));
467 /* Handles: `.Dt TITLE SEC VOL'
468 * --> title = TITLE, volume = VOL is vol ?
470 * VOL is arch ? format(arch) :
474 cp
= mdoc_a2vol(n
->string
);
477 if (NULL
== (m
->meta
.vol
= strdup(cp
)))
478 return(verr(m
, EMALLOC
));
481 cp
= mdoc_a2arch(n
->string
);
484 if (NULL
== (m
->meta
.vol
= strdup(n
->string
)))
485 return(verr(m
, EMALLOC
));
486 } else if (NULL
== (m
->meta
.arch
= strdup(cp
)))
487 return(verr(m
, EMALLOC
));
490 /* Ignore any subsequent parameters... */
492 return(post_prol(m
));
500 struct utsname utsname
;
506 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
510 if (-1 == uname(&utsname
))
511 return(verr(m
, EUTSNAME
));
512 if (strlcat(buf
, utsname
.sysname
, 64) >= 64)
513 return(verr(m
, ETOOLONG
));
514 if (strlcat(buf
, " ", 64) >= 64)
515 return(verr(m
, ETOOLONG
));
516 if (strlcat(buf
, utsname
.release
, 64) >= 64)
517 return(verr(m
, ETOOLONG
));
520 if (NULL
== (m
->meta
.os
= strdup(buf
)))
521 return(verr(m
, EMALLOC
));
523 m
->flags
|= MDOC_PBODY
;
524 return(post_prol(m
));
529 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
530 * Uses the first head macro.
533 post_bl_tagwidth(struct mdoc
*m
)
540 * Use the text width, if a text node, or the default macro
544 n
= m
->last
->body
->child
;
546 assert(MDOC_BLOCK
== n
->type
);
547 assert(MDOC_It
== n
->tok
);
551 sz
= 10; /* Default size. */
554 if (MDOC_TEXT
!= n
->type
) {
555 if (0 == (sz
= (int)mdoc_macro2len(n
->tok
)))
556 if ( ! vwarn(m
, WNOWIDTH
))
559 sz
= (int)strlen(n
->string
) + 1;
562 if (-1 == snprintf(buf
, sizeof(buf
), "%dn", sz
))
563 return(verr(m
, ENUMFMT
));
566 * We have to dynamically add this to the macro's argument list.
567 * We're guaranteed that a MDOC_Width doesn't already exist.
572 sz
= (int)(n
->args
->argc
)++;
574 n
->args
->argv
= realloc(n
->args
->argv
,
575 n
->args
->argc
* sizeof(struct mdoc_argv
));
577 if (NULL
== n
->args
->argv
)
578 return(verr(m
, EMALLOC
));
580 n
->args
->argv
[sz
].arg
= MDOC_Width
;
581 n
->args
->argv
[sz
].line
= m
->last
->line
;
582 n
->args
->argv
[sz
].pos
= m
->last
->pos
;
583 n
->args
->argv
[sz
].sz
= 1;
584 n
->args
->argv
[sz
].value
= calloc(1, sizeof(char *));
586 if (NULL
== n
->args
->argv
[sz
].value
)
587 return(verr(m
, EMALLOC
));
588 if (NULL
== (n
->args
->argv
[sz
].value
[0] = strdup(buf
)))
589 return(verr(m
, EMALLOC
));
596 post_bl_width(struct mdoc
*m
)
603 if (NULL
== m
->last
->args
)
606 for (i
= 0; i
< (int)m
->last
->args
->argc
; i
++)
607 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
610 if (i
== (int)m
->last
->args
->argc
)
612 p
= m
->last
->args
->argv
[i
].value
[0];
615 * If the value to -width is a macro, then we re-write it to be
616 * the macro's width as set in share/tmac/mdoc/doc-common.
619 if (0 == strcmp(p
, "Ds"))
621 else if (MDOC_MAX
== (tok
= mdoc_hash_find(m
->htab
, p
)))
623 else if (0 == (width
= mdoc_macro2len(tok
)))
624 return(vwarn(m
, WNOWIDTH
));
626 /* The value already exists: free and reallocate it. */
628 if (-1 == snprintf(buf
, sizeof(buf
), "%zun", width
))
629 return(verr(m
, ENUMFMT
));
631 free(m
->last
->args
->argv
[i
].value
[0]);
632 m
->last
->args
->argv
[i
].value
[0] = strdup(buf
);
633 if (NULL
== m
->last
->args
->argv
[i
].value
[0])
634 return(verr(m
, EMALLOC
));
641 post_bl_head(POST_ARGS
)
644 struct mdoc_node
*n
, *nn
, *nnp
;
646 if (NULL
== m
->last
->child
)
652 for (c
= 0; c
< (int)n
->args
->argc
; c
++)
653 if (MDOC_Column
== n
->args
->argv
[c
].arg
)
656 /* Only process -column. */
658 if (c
== (int)n
->args
->argc
)
661 assert(0 == n
->args
->argv
[c
].sz
);
664 * Accomodate for new-style groff column syntax. Shuffle the
665 * child nodes, all of which must be TEXT, as arguments for the
666 * column field. Then, delete the head children.
669 n
->args
->argv
[c
].sz
= (size_t)m
->last
->nchild
;
670 n
->args
->argv
[c
].value
= malloc
671 ((size_t)m
->last
->nchild
* sizeof(char *));
673 for (i
= 0, nn
= m
->last
->child
; nn
; i
++) {
674 n
->args
->argv
[c
].value
[i
] = nn
->string
;
682 m
->last
->child
= NULL
;
693 if (MDOC_HEAD
== m
->last
->type
)
694 return(post_bl_head(m
));
695 if (MDOC_BLOCK
!= m
->last
->type
)
699 * These are fairly complicated, so we've broken them into two
700 * functions. post_bl_tagwidth() is called when a -tag is
701 * specified, but no -width (it must be guessed). The second
702 * when a -width is specified (macro indicators must be
703 * rewritten into real lengths).
706 len
= (int)(m
->last
->args
? m
->last
->args
->argc
: 0);
708 for (r
= i
= 0; i
< len
; i
++) {
709 if (MDOC_Tag
== m
->last
->args
->argv
[i
].arg
)
711 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
715 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
716 if ( ! post_bl_tagwidth(m
))
718 } else if (r
& (1 << 1))
719 if ( ! post_bl_width(m
))
735 m
->next
= MDOC_NEXT_CHILD
;
736 /* FIXME: this isn't documented anywhere! */
737 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
742 m
->next
= MDOC_NEXT_SIBLING
;
756 m
->next
= MDOC_NEXT_CHILD
;
757 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
758 m
->last
->pos
, "file"))
760 m
->next
= MDOC_NEXT_SIBLING
;
761 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
762 m
->last
->pos
, "..."))
766 m
->next
= MDOC_NEXT_SIBLING
;
777 if ( ! concat(m
, m
->last
->child
, buf
, sizeof(buf
)))
780 if (0 == (m
->meta
.date
= mdoc_atotime(buf
))) {
781 if ( ! vwarn(m
, WBADDATE
))
783 m
->meta
.date
= time(NULL
);
786 return(post_prol(m
));
796 * The end document shouldn't have the prologue macros as part
797 * of the syntax tree (they encompass only meta-data).
800 if (m
->last
->parent
->child
== m
->last
)
801 m
->last
->parent
->child
= m
->last
->prev
;
803 m
->last
->prev
->next
= NULL
;
806 assert(NULL
== m
->last
->next
);
809 m
->last
= m
->last
->prev
;
810 m
->next
= MDOC_NEXT_SIBLING
;
812 m
->last
= m
->last
->parent
;
813 m
->next
= MDOC_NEXT_CHILD
;
816 mdoc_node_freelist(n
);
825 if (MDOC_BODY
== n
->type
)
826 m
->flags
|= MDOC_LITERAL
;
836 if (MDOC_BODY
!= n
->type
)
839 /* Enter literal context if `Bd -literal' or * -unfilled'. */
841 for (n
= n
->parent
, i
= 0; i
< (int)n
->args
->argc
; i
++)
842 if (MDOC_Literal
== n
->args
->argv
[i
].arg
)
844 else if (MDOC_Unfilled
== n
->args
->argv
[i
].arg
)
847 if (i
< (int)n
->args
->argc
)
848 m
->flags
|= MDOC_LITERAL
;
855 post_display(POST_ARGS
)
858 if (MDOC_BODY
== m
->last
->type
)
859 m
->flags
&= ~MDOC_LITERAL
;