]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.87 2010/11/30 12:35:10 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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.
22 #include <sys/utsname.h>
33 #include "libmandoc.h"
37 * FIXME: this file is deprecated. All future "actions" should be
38 * pushed into mdoc_validate.c.
41 #define POST_ARGS struct mdoc *m, struct mdoc_node *n
42 #define PRE_ARGS struct mdoc *m, struct mdoc_node *n
46 int (*post
)(POST_ARGS
);
49 static int concat(struct mdoc
*, char *,
50 const struct mdoc_node
*, size_t);
52 static int post_dd(POST_ARGS
);
53 static int post_dt(POST_ARGS
);
54 static int post_os(POST_ARGS
);
55 static int post_prol(POST_ARGS
);
56 static int post_std(POST_ARGS
);
58 static const struct actions mdoc_actions
[MDOC_MAX
] = {
59 { NULL
, NULL
}, /* Ap */
60 { NULL
, post_dd
}, /* Dd */
61 { NULL
, post_dt
}, /* Dt */
62 { NULL
, post_os
}, /* Os */
63 { NULL
, NULL
}, /* Sh */
64 { NULL
, NULL
}, /* Ss */
65 { NULL
, NULL
}, /* Pp */
66 { NULL
, NULL
}, /* D1 */
67 { NULL
, NULL
}, /* Dl */
68 { NULL
, NULL
}, /* Bd */
69 { NULL
, NULL
}, /* Ed */
70 { NULL
, NULL
}, /* Bl */
71 { NULL
, NULL
}, /* El */
72 { NULL
, NULL
}, /* It */
73 { NULL
, NULL
}, /* Ad */
74 { NULL
, NULL
}, /* An */
75 { NULL
, NULL
}, /* Ar */
76 { NULL
, NULL
}, /* Cd */
77 { NULL
, NULL
}, /* Cm */
78 { NULL
, NULL
}, /* Dv */
79 { NULL
, NULL
}, /* Er */
80 { NULL
, NULL
}, /* Ev */
81 { NULL
, post_std
}, /* Ex */
82 { NULL
, NULL
}, /* Fa */
83 { NULL
, NULL
}, /* Fd */
84 { NULL
, NULL
}, /* Fl */
85 { NULL
, NULL
}, /* Fn */
86 { NULL
, NULL
}, /* Ft */
87 { NULL
, NULL
}, /* Ic */
88 { NULL
, NULL
}, /* In */
89 { NULL
, NULL
}, /* Li */
90 { NULL
, NULL
}, /* Nd */
91 { NULL
, NULL
}, /* Nm */
92 { NULL
, NULL
}, /* Op */
93 { NULL
, NULL
}, /* Ot */
94 { NULL
, NULL
}, /* Pa */
95 { NULL
, post_std
}, /* Rv */
96 { NULL
, NULL
}, /* St */
97 { NULL
, NULL
}, /* Va */
98 { NULL
, NULL
}, /* Vt */
99 { NULL
, NULL
}, /* Xr */
100 { NULL
, NULL
}, /* %A */
101 { NULL
, NULL
}, /* %B */
102 { NULL
, NULL
}, /* %D */
103 { NULL
, NULL
}, /* %I */
104 { NULL
, NULL
}, /* %J */
105 { NULL
, NULL
}, /* %N */
106 { NULL
, NULL
}, /* %O */
107 { NULL
, NULL
}, /* %P */
108 { NULL
, NULL
}, /* %R */
109 { NULL
, NULL
}, /* %T */
110 { NULL
, NULL
}, /* %V */
111 { NULL
, NULL
}, /* Ac */
112 { NULL
, NULL
}, /* Ao */
113 { NULL
, NULL
}, /* Aq */
114 { NULL
, NULL
}, /* At */
115 { NULL
, NULL
}, /* Bc */
116 { NULL
, NULL
}, /* Bf */
117 { NULL
, NULL
}, /* Bo */
118 { NULL
, NULL
}, /* Bq */
119 { NULL
, NULL
}, /* Bsx */
120 { NULL
, NULL
}, /* Bx */
121 { NULL
, NULL
}, /* Db */
122 { NULL
, NULL
}, /* Dc */
123 { NULL
, NULL
}, /* Do */
124 { NULL
, NULL
}, /* Dq */
125 { NULL
, NULL
}, /* Ec */
126 { NULL
, NULL
}, /* Ef */
127 { NULL
, NULL
}, /* Em */
128 { NULL
, NULL
}, /* Eo */
129 { NULL
, NULL
}, /* Fx */
130 { NULL
, NULL
}, /* Ms */
131 { NULL
, NULL
}, /* No */
132 { NULL
, NULL
}, /* Ns */
133 { NULL
, NULL
}, /* Nx */
134 { NULL
, NULL
}, /* Ox */
135 { NULL
, NULL
}, /* Pc */
136 { NULL
, NULL
}, /* Pf */
137 { NULL
, NULL
}, /* Po */
138 { NULL
, NULL
}, /* Pq */
139 { NULL
, NULL
}, /* Qc */
140 { NULL
, NULL
}, /* Ql */
141 { NULL
, NULL
}, /* Qo */
142 { NULL
, NULL
}, /* Qq */
143 { NULL
, NULL
}, /* Re */
144 { NULL
, NULL
}, /* Rs */
145 { NULL
, NULL
}, /* Sc */
146 { NULL
, NULL
}, /* So */
147 { NULL
, NULL
}, /* Sq */
148 { NULL
, NULL
}, /* Sm */
149 { NULL
, NULL
}, /* Sx */
150 { NULL
, NULL
}, /* Sy */
151 { NULL
, NULL
}, /* Tn */
152 { NULL
, NULL
}, /* Ux */
153 { NULL
, NULL
}, /* Xc */
154 { NULL
, NULL
}, /* Xo */
155 { NULL
, NULL
}, /* Fo */
156 { NULL
, NULL
}, /* Fc */
157 { NULL
, NULL
}, /* Oo */
158 { NULL
, NULL
}, /* Oc */
159 { NULL
, NULL
}, /* Bk */
160 { NULL
, NULL
}, /* Ek */
161 { NULL
, NULL
}, /* Bt */
162 { NULL
, NULL
}, /* Hf */
163 { NULL
, NULL
}, /* Fr */
164 { NULL
, NULL
}, /* Ud */
165 { NULL
, NULL
}, /* Lb */
166 { NULL
, NULL
}, /* Lp */
167 { NULL
, NULL
}, /* Lk */
168 { NULL
, NULL
}, /* Mt */
169 { NULL
, NULL
}, /* Brq */
170 { NULL
, NULL
}, /* Bro */
171 { NULL
, NULL
}, /* Brc */
172 { NULL
, NULL
}, /* %C */
173 { NULL
, NULL
}, /* Es */
174 { NULL
, NULL
}, /* En */
175 { NULL
, NULL
}, /* Dx */
176 { NULL
, NULL
}, /* %Q */
177 { NULL
, NULL
}, /* br */
178 { NULL
, NULL
}, /* sp */
179 { NULL
, NULL
}, /* %U */
180 { NULL
, NULL
}, /* Ta */
185 mdoc_action_pre(struct mdoc
*m
, 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
, m
->last
));
227 * Concatenate sibling nodes together. All siblings must be of type
228 * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
232 concat(struct mdoc
*m
, char *p
, const struct mdoc_node
*n
, size_t sz
)
237 for ( ; n
; n
= n
->next
) {
238 assert(MDOC_TEXT
== n
->type
);
240 * XXX: yes, these can technically be resized, but it's
241 * highly unlikely that we're going to get here, so let
244 if (strlcat(p
, n
->string
, sz
) >= sz
) {
245 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
250 if (strlcat(p
, " ", sz
) >= sz
) {
251 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
261 * Macros accepting `-std' as an argument have the name of the current
262 * document (`Nm') filled in as the argument if it's not provided.
267 struct mdoc_node
*nn
;
271 if (NULL
== m
->meta
.name
)
275 m
->next
= MDOC_NEXT_CHILD
;
277 if ( ! mdoc_word_alloc(m
, n
->line
, n
->pos
, m
->meta
.name
))
284 * Parse out the contents of `Dt'. See in-line documentation for how we
285 * handle the various fields of this macro.
290 struct mdoc_node
*nn
;
300 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
302 * --> title = unknown, volume = local, msec = 0, arch = NULL
305 if (NULL
== (nn
= n
->child
)) {
306 /* XXX: make these macro values. */
307 /* FIXME: warn about missing values. */
308 m
->meta
.title
= mandoc_strdup("UNKNOWN");
309 m
->meta
.vol
= mandoc_strdup("LOCAL");
310 m
->meta
.msec
= mandoc_strdup("1");
311 return(post_prol(m
, n
));
314 /* Handles: `.Dt TITLE'
315 * --> title = TITLE, volume = local, msec = 0, arch = NULL
318 m
->meta
.title
= mandoc_strdup
319 ('\0' == nn
->string
[0] ? "UNKNOWN" : nn
->string
);
321 if (NULL
== (nn
= nn
->next
)) {
322 /* FIXME: warn about missing msec. */
323 /* XXX: make this a macro value. */
324 m
->meta
.vol
= mandoc_strdup("LOCAL");
325 m
->meta
.msec
= mandoc_strdup("1");
326 return(post_prol(m
, n
));
329 /* Handles: `.Dt TITLE SEC'
330 * --> title = TITLE, volume = SEC is msec ?
331 * format(msec) : SEC,
332 * msec = SEC is msec ? atoi(msec) : 0,
336 cp
= mdoc_a2msec(nn
->string
);
338 m
->meta
.vol
= mandoc_strdup(cp
);
339 m
->meta
.msec
= mandoc_strdup(nn
->string
);
340 } else if (mdoc_nmsg(m
, n
, MANDOCERR_BADMSEC
)) {
341 m
->meta
.vol
= mandoc_strdup(nn
->string
);
342 m
->meta
.msec
= mandoc_strdup(nn
->string
);
346 if (NULL
== (nn
= nn
->next
))
347 return(post_prol(m
, n
));
349 /* Handles: `.Dt TITLE SEC VOL'
350 * --> title = TITLE, volume = VOL is vol ?
352 * VOL is arch ? format(arch) :
356 cp
= mdoc_a2vol(nn
->string
);
359 m
->meta
.vol
= mandoc_strdup(cp
);
361 /* FIXME: warn about bad arch. */
362 cp
= mdoc_a2arch(nn
->string
);
365 m
->meta
.vol
= mandoc_strdup(nn
->string
);
367 m
->meta
.arch
= mandoc_strdup(cp
);
370 /* Ignore any subsequent parameters... */
371 /* FIXME: warn about subsequent parameters. */
373 return(post_prol(m
, n
));
378 * Set the operating system by way of the `Os' macro. Note that if an
379 * argument isn't provided and -DOSNAME="\"foo\"" is provided during
380 * compilation, this value will be used instead of filling in "sysname
381 * release" from uname().
388 struct utsname utsname
;
394 if ( ! concat(m
, buf
, n
->child
, BUFSIZ
))
397 /* XXX: yes, these can all be dynamically-adjusted buffers, but
398 * it's really not worth the extra hackery.
401 if ('\0' == buf
[0]) {
403 if (strlcat(buf
, OSNAME
, BUFSIZ
) >= BUFSIZ
) {
404 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
408 if (-1 == uname(&utsname
))
409 return(mdoc_nmsg(m
, n
, MANDOCERR_UTSNAME
));
411 if (strlcat(buf
, utsname
.sysname
, BUFSIZ
) >= BUFSIZ
) {
412 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
415 if (strlcat(buf
, " ", 64) >= BUFSIZ
) {
416 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
419 if (strlcat(buf
, utsname
.release
, BUFSIZ
) >= BUFSIZ
) {
420 mdoc_nmsg(m
, n
, MANDOCERR_MEM
);
426 m
->meta
.os
= mandoc_strdup(buf
);
427 return(post_prol(m
, n
));
431 * Parse the date field in `Dd'.
438 if (NULL
== n
->child
) {
439 m
->meta
.date
= time(NULL
);
440 return(post_prol(m
, n
));
443 if ( ! concat(m
, buf
, n
->child
, DATESIZ
))
446 m
->meta
.date
= mandoc_a2time
447 (MTIME_MDOCDATE
| MTIME_CANONICAL
, buf
);
449 if (0 == m
->meta
.date
) {
450 if ( ! mdoc_nmsg(m
, n
, MANDOCERR_BADDATE
))
452 m
->meta
.date
= time(NULL
);
455 return(post_prol(m
, n
));
460 * Remove prologue macros from the document after they're processed.
461 * The final document uses mdoc_meta for these values and discards the
468 mdoc_node_delete(m
, n
);
469 if (m
->meta
.title
&& m
->meta
.date
&& m
->meta
.os
)
470 m
->flags
|= MDOC_PBODY
;