]>
git.cameronkatri.com Git - mandoc.git/blob - action.c
1 /* $Id: action.c,v 1.48 2009/03/21 09:48:29 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/utsname.h>
30 * Actions are executed on macros after they've been post-validated: in
31 * other words, a macro will not be "acted upon" until all of its
32 * children have been filled in (post-fix order).
45 #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
46 #define POST_ARGS struct mdoc *m
50 int (*post
)(POST_ARGS
);
53 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
54 static int perr(struct mdoc
*, int, int, enum merr
);
56 static int post_ar(POST_ARGS
);
57 static int post_bl(POST_ARGS
);
58 static int post_bl_width(POST_ARGS
);
59 static int post_bl_tagwidth(POST_ARGS
);
60 static int post_dd(POST_ARGS
);
61 static int post_display(POST_ARGS
);
62 static int post_dt(POST_ARGS
);
63 static int post_nm(POST_ARGS
);
64 static int post_os(POST_ARGS
);
65 static int post_prol(POST_ARGS
);
66 static int post_sh(POST_ARGS
);
67 static int post_std(POST_ARGS
);
69 static int pre_bd(PRE_ARGS
);
70 static int pre_dl(PRE_ARGS
);
72 #define merr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
73 #define mwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t))
75 const struct actions mdoc_actions
[MDOC_MAX
] = {
76 { NULL
, NULL
}, /* \" */
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 */
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
}, /* Ap */
184 { NULL
, NULL
}, /* Lp */
185 { NULL
, NULL
}, /* Lk */
186 { NULL
, NULL
}, /* Mt */
187 { NULL
, NULL
}, /* Brq */
188 { NULL
, NULL
}, /* Bro */
189 { NULL
, NULL
}, /* Brc */
190 { NULL
, NULL
}, /* %C */
191 { NULL
, NULL
}, /* Es */
192 { NULL
, NULL
}, /* En */
193 { NULL
, NULL
}, /* Dx */
194 { NULL
, NULL
}, /* %Q */
199 mdoc_action_pre(struct mdoc
*m
, const struct mdoc_node
*n
)
208 if (NULL
== mdoc_actions
[m
->last
->tok
].pre
)
210 return((*mdoc_actions
[m
->last
->tok
].pre
)(m
, n
));
217 mdoc_action_post(struct mdoc
*m
)
220 if (MDOC_ACTED
& m
->last
->flags
)
222 m
->last
->flags
|= MDOC_ACTED
;
224 switch (m
->last
->type
) {
230 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
232 return((*mdoc_actions
[m
->last
->tok
].post
)(m
));
239 perr(struct mdoc
*m
, int line
, int pos
, enum merr type
)
247 p
= "missing width argument";
252 return(mdoc_perr(m
, line
, pos
, p
));
257 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
267 p
= "inappropriate document section in manual section";
271 p
= "cannot determine default width";
274 p
= "malformed date syntax";
279 return(mdoc_pwarn(m
, line
, pos
, c
, p
));
288 * If '-std' is invoked without an argument, fill it in with our
289 * name (if it's been set).
292 if (NULL
== m
->last
->args
)
294 if (m
->last
->args
->argv
[0].sz
)
297 assert(m
->meta
.name
);
299 m
->last
->args
->argv
[0].value
= xcalloc(1, sizeof(char *));
300 m
->last
->args
->argv
[0].sz
= 1;
301 m
->last
->args
->argv
[0].value
[0] = xstrdup(m
->meta
.name
);
314 (void)xstrlcpys(buf
, m
->last
->child
, sizeof(buf
));
315 m
->meta
.name
= xstrdup(buf
);
328 * We keep track of the current section /and/ the "named"
329 * section, which is one of the conventional ones, in order to
333 if (MDOC_HEAD
!= m
->last
->type
)
336 (void)xstrlcpys(buf
, m
->last
->child
, sizeof(buf
));
337 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
340 switch ((m
->lastsec
= sec
)) {
341 case (SEC_RETURN_VALUES
):
344 switch (m
->meta
.msec
) {
352 return(mwarn(m
, WBADSEC
));
377 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
381 * --> title = unknown, volume = local, msec = 0, arch = NULL
384 if (NULL
== (n
= m
->last
->child
)) {
385 m
->meta
.title
= xstrdup("unknown");
386 m
->meta
.vol
= xstrdup("local");
387 return(post_prol(m
));
390 /* Handles: `.Dt TITLE'
391 * --> title = TITLE, volume = local, msec = 0, arch = NULL
394 m
->meta
.title
= xstrdup(n
->string
);
396 if (NULL
== (n
= n
->next
)) {
397 m
->meta
.vol
= xstrdup("local");
398 return(post_prol(m
));
401 /* Handles: `.Dt TITLE SEC'
402 * --> title = TITLE, volume = SEC is msec ?
403 * format(msec) : SEC,
404 * msec = SEC is msec ? atoi(msec) : 0,
408 cp
= mdoc_a2msec(n
->string
);
410 m
->meta
.vol
= xstrdup(cp
);
412 lval
= strtol(n
->string
, &ep
, 10);
413 if (n
->string
[0] != '\0' && *ep
== '\0')
414 m
->meta
.msec
= (int)lval
;
416 m
->meta
.vol
= xstrdup(n
->string
);
418 if (NULL
== (n
= n
->next
))
419 return(post_prol(m
));
421 /* Handles: `.Dt TITLE SEC VOL'
422 * --> title = TITLE, volume = VOL is vol ?
424 * VOL is arch ? format(arch) :
428 cp
= mdoc_a2vol(n
->string
);
431 m
->meta
.vol
= xstrdup(cp
);
434 cp
= mdoc_a2arch(n
->string
);
437 m
->meta
.vol
= xstrdup(n
->string
);
439 m
->meta
.arch
= xstrdup(cp
);
442 /* Ignore any subsequent parameters... */
444 return(post_prol(m
));
452 struct utsname utsname
;
457 (void)xstrlcpys(buf
, m
->last
->child
, sizeof(buf
));
460 if (-1 == uname(&utsname
))
461 return(mdoc_err(m
, "utsname"));
462 (void)xstrlcpy(buf
, utsname
.sysname
, sizeof(buf
));
463 (void)xstrlcat(buf
, " ", sizeof(buf
));
464 (void)xstrlcat(buf
, utsname
.release
, sizeof(buf
));
467 m
->meta
.os
= xstrdup(buf
);
468 m
->lastnamed
= m
->lastsec
= SEC_BODY
;
470 return(post_prol(m
));
475 post_bl_tagwidth(struct mdoc
*m
)
482 * If -tag has been specified and -width has not been, then try
483 * to intuit our width from the first body element.
486 if (NULL
== (n
= m
->last
->body
->child
))
490 * Use the text width, if a text node, or the default macro
496 if (MDOC_TEXT
!= n
->type
) {
497 if (0 == (sz
= (int)mdoc_macro2len(n
->tok
)))
500 sz
= (int)strlen(n
->string
) + 1;
505 if ( ! mwarn(m
, WNOWIDTH
))
510 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
513 * We have to dynamically add this to the macro's argument list.
514 * We're guaranteed that a MDOC_Width doesn't already exist.
517 if (NULL
== m
->last
->args
) {
518 m
->last
->args
= xcalloc
519 (1, sizeof(struct mdoc_arg
));
520 m
->last
->args
->refcnt
= 1;
524 sz
= (int)n
->args
->argc
;
528 n
->args
->argv
= xrealloc(n
->args
->argv
,
529 n
->args
->argc
* sizeof(struct mdoc_arg
));
531 n
->args
->argv
[sz
- 1].arg
= MDOC_Width
;
532 n
->args
->argv
[sz
- 1].line
= m
->last
->line
;
533 n
->args
->argv
[sz
- 1].pos
= m
->last
->pos
;
534 n
->args
->argv
[sz
- 1].sz
= 1;
535 n
->args
->argv
[sz
- 1].value
= xcalloc(1, sizeof(char *));
536 n
->args
->argv
[sz
- 1].value
[0] = xstrdup(buf
);
543 post_bl_width(struct mdoc
*m
)
550 if (NULL
== m
->last
->args
)
551 return(merr(m
, ENOWIDTH
));
553 for (i
= 0; i
< (int)m
->last
->args
->argc
; i
++)
554 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
557 if (i
== (int)m
->last
->args
->argc
)
558 return(merr(m
, ENOWIDTH
));
560 p
= m
->last
->args
->argv
[i
].value
[0];
563 * If the value to -width is a macro, then we re-write it to be
564 * the macro's width as set in share/tmac/mdoc/doc-common.
567 if (xstrcmp(p
, "Ds"))
569 else if (MDOC_MAX
== (tok
= mdoc_tokhash_find(m
->htab
, p
)))
571 else if (0 == (width
= mdoc_macro2len(tok
)))
572 return(mwarn(m
, WNOWIDTH
));
574 /* The value already exists: free and reallocate it. */
576 (void)snprintf(buf
, sizeof(buf
), "%zun", width
);
578 free(m
->last
->args
->argv
[i
].value
[0]);
579 m
->last
->args
->argv
[i
].value
[0] = xstrdup(buf
);
590 if (MDOC_BLOCK
!= m
->last
->type
)
594 * These are fairly complicated, so we've broken them into two
595 * functions. post_bl_tagwidth() is called when a -tag is
596 * specified, but no -width (it must be guessed). The second
597 * when a -width is specified (macro indicators must be
598 * rewritten into real lengths).
601 len
= (int)(m
->last
->args
? m
->last
->args
->argc
: 0);
603 for (r
= i
= 0; i
< len
; i
++) {
604 if (MDOC_Tag
== m
->last
->args
->argv
[i
].arg
)
606 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
610 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
611 if ( ! post_bl_tagwidth(m
))
613 } else if (r
& (1 << 1))
614 if ( ! post_bl_width(m
))
630 m
->next
= MDOC_NEXT_CHILD
;
631 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
632 m
->last
->pos
, "file"))
634 m
->next
= MDOC_NEXT_SIBLING
;
635 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
636 m
->last
->pos
, "..."))
640 m
->next
= MDOC_NEXT_SIBLING
;
650 (void)xstrlcpys(buf
, m
->last
->child
, sizeof(buf
));
652 if (0 == (m
->meta
.date
= mdoc_atotime(buf
))) {
653 if ( ! mwarn(m
, WBADDATE
))
655 m
->meta
.date
= time(NULL
);
658 return(post_prol(m
));
668 * The end document shouldn't have the prologue macros as part
669 * of the syntax tree (they encompass only meta-data).
672 if (m
->last
->parent
->child
== m
->last
)
673 m
->last
->parent
->child
= m
->last
->prev
;
675 m
->last
->prev
->next
= NULL
;
678 assert(NULL
== m
->last
->next
);
681 m
->last
= m
->last
->prev
;
682 m
->next
= MDOC_NEXT_SIBLING
;
684 m
->last
= m
->last
->parent
;
685 m
->next
= MDOC_NEXT_CHILD
;
688 mdoc_node_freelist(n
);
697 if (MDOC_BODY
!= n
->type
)
699 m
->flags
|= MDOC_LITERAL
;
709 if (MDOC_BODY
!= n
->type
)
713 * We ONLY enter a literal context if `Bd -literal' or `Bd
719 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
720 if (MDOC_Literal
== n
->args
->argv
[i
].arg
)
722 else if (MDOC_Unfilled
== n
->args
->argv
[i
].arg
)
725 if (i
< (int)n
->args
->argc
)
726 m
->flags
|= MDOC_LITERAL
;
733 post_display(POST_ARGS
)
736 if (MDOC_BODY
== m
->last
->type
)
737 m
->flags
&= ~MDOC_LITERAL
;