]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
Move post_bl() and subfunctions from mdoc_action.c into mdoc_validate.c.
[mandoc.git] / mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.87 2010/11/30 12:35:10 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
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.
8 *
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.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #ifndef OSNAME
22 #include <sys/utsname.h>
23 #endif
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "mandoc.h"
32 #include "libmdoc.h"
33 #include "libmandoc.h"
34
35 #define DATESIZ 32
36 /*
37 * FIXME: this file is deprecated. All future "actions" should be
38 * pushed into mdoc_validate.c.
39 */
40
41 #define POST_ARGS struct mdoc *m, struct mdoc_node *n
42 #define PRE_ARGS struct mdoc *m, struct mdoc_node *n
43
44 struct actions {
45 int (*pre)(PRE_ARGS);
46 int (*post)(POST_ARGS);
47 };
48
49 static int concat(struct mdoc *, char *,
50 const struct mdoc_node *, size_t);
51
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);
57
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 */
181 };
182
183
184 int
185 mdoc_action_pre(struct mdoc *m, struct mdoc_node *n)
186 {
187
188 switch (n->type) {
189 case (MDOC_ROOT):
190 /* FALLTHROUGH */
191 case (MDOC_TEXT):
192 return(1);
193 default:
194 break;
195 }
196
197 if (NULL == mdoc_actions[n->tok].pre)
198 return(1);
199 return((*mdoc_actions[n->tok].pre)(m, n));
200 }
201
202
203 int
204 mdoc_action_post(struct mdoc *m)
205 {
206
207 if (MDOC_ACTED & m->last->flags)
208 return(1);
209 m->last->flags |= MDOC_ACTED;
210
211 switch (m->last->type) {
212 case (MDOC_TEXT):
213 /* FALLTHROUGH */
214 case (MDOC_ROOT):
215 return(1);
216 default:
217 break;
218 }
219
220 if (NULL == mdoc_actions[m->last->tok].post)
221 return(1);
222 return((*mdoc_actions[m->last->tok].post)(m, m->last));
223 }
224
225
226 /*
227 * Concatenate sibling nodes together. All siblings must be of type
228 * MDOC_TEXT or an assertion is raised. Concatenation is separated by a
229 * single whitespace.
230 */
231 static int
232 concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
233 {
234
235 assert(sz);
236 p[0] = '\0';
237 for ( ; n; n = n->next) {
238 assert(MDOC_TEXT == n->type);
239 /*
240 * XXX: yes, these can technically be resized, but it's
241 * highly unlikely that we're going to get here, so let
242 * it slip for now.
243 */
244 if (strlcat(p, n->string, sz) >= sz) {
245 mdoc_nmsg(m, n, MANDOCERR_MEM);
246 return(0);
247 }
248 if (NULL == n->next)
249 continue;
250 if (strlcat(p, " ", sz) >= sz) {
251 mdoc_nmsg(m, n, MANDOCERR_MEM);
252 return(0);
253 }
254 }
255
256 return(1);
257 }
258
259
260 /*
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.
263 */
264 static int
265 post_std(POST_ARGS)
266 {
267 struct mdoc_node *nn;
268
269 if (n->child)
270 return(1);
271 if (NULL == m->meta.name)
272 return(1);
273
274 nn = n;
275 m->next = MDOC_NEXT_CHILD;
276
277 if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
278 return(0);
279 m->last = nn;
280 return(1);
281 }
282
283 /*
284 * Parse out the contents of `Dt'. See in-line documentation for how we
285 * handle the various fields of this macro.
286 */
287 static int
288 post_dt(POST_ARGS)
289 {
290 struct mdoc_node *nn;
291 const char *cp;
292
293 if (m->meta.title)
294 free(m->meta.title);
295 if (m->meta.vol)
296 free(m->meta.vol);
297 if (m->meta.arch)
298 free(m->meta.arch);
299
300 m->meta.title = m->meta.vol = m->meta.arch = NULL;
301 /* Handles: `.Dt'
302 * --> title = unknown, volume = local, msec = 0, arch = NULL
303 */
304
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));
312 }
313
314 /* Handles: `.Dt TITLE'
315 * --> title = TITLE, volume = local, msec = 0, arch = NULL
316 */
317
318 m->meta.title = mandoc_strdup
319 ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
320
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));
327 }
328
329 /* Handles: `.Dt TITLE SEC'
330 * --> title = TITLE, volume = SEC is msec ?
331 * format(msec) : SEC,
332 * msec = SEC is msec ? atoi(msec) : 0,
333 * arch = NULL
334 */
335
336 cp = mdoc_a2msec(nn->string);
337 if (cp) {
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);
343 } else
344 return(0);
345
346 if (NULL == (nn = nn->next))
347 return(post_prol(m, n));
348
349 /* Handles: `.Dt TITLE SEC VOL'
350 * --> title = TITLE, volume = VOL is vol ?
351 * format(VOL) :
352 * VOL is arch ? format(arch) :
353 * VOL
354 */
355
356 cp = mdoc_a2vol(nn->string);
357 if (cp) {
358 free(m->meta.vol);
359 m->meta.vol = mandoc_strdup(cp);
360 } else {
361 /* FIXME: warn about bad arch. */
362 cp = mdoc_a2arch(nn->string);
363 if (NULL == cp) {
364 free(m->meta.vol);
365 m->meta.vol = mandoc_strdup(nn->string);
366 } else
367 m->meta.arch = mandoc_strdup(cp);
368 }
369
370 /* Ignore any subsequent parameters... */
371 /* FIXME: warn about subsequent parameters. */
372
373 return(post_prol(m, n));
374 }
375
376
377 /*
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().
382 */
383 static int
384 post_os(POST_ARGS)
385 {
386 char buf[BUFSIZ];
387 #ifndef OSNAME
388 struct utsname utsname;
389 #endif
390
391 if (m->meta.os)
392 free(m->meta.os);
393
394 if ( ! concat(m, buf, n->child, BUFSIZ))
395 return(0);
396
397 /* XXX: yes, these can all be dynamically-adjusted buffers, but
398 * it's really not worth the extra hackery.
399 */
400
401 if ('\0' == buf[0]) {
402 #ifdef OSNAME
403 if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
404 mdoc_nmsg(m, n, MANDOCERR_MEM);
405 return(0);
406 }
407 #else /*!OSNAME */
408 if (-1 == uname(&utsname))
409 return(mdoc_nmsg(m, n, MANDOCERR_UTSNAME));
410
411 if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) {
412 mdoc_nmsg(m, n, MANDOCERR_MEM);
413 return(0);
414 }
415 if (strlcat(buf, " ", 64) >= BUFSIZ) {
416 mdoc_nmsg(m, n, MANDOCERR_MEM);
417 return(0);
418 }
419 if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) {
420 mdoc_nmsg(m, n, MANDOCERR_MEM);
421 return(0);
422 }
423 #endif /*!OSNAME*/
424 }
425
426 m->meta.os = mandoc_strdup(buf);
427 return(post_prol(m, n));
428 }
429
430 /*
431 * Parse the date field in `Dd'.
432 */
433 static int
434 post_dd(POST_ARGS)
435 {
436 char buf[DATESIZ];
437
438 if (NULL == n->child) {
439 m->meta.date = time(NULL);
440 return(post_prol(m, n));
441 }
442
443 if ( ! concat(m, buf, n->child, DATESIZ))
444 return(0);
445
446 m->meta.date = mandoc_a2time
447 (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
448
449 if (0 == m->meta.date) {
450 if ( ! mdoc_nmsg(m, n, MANDOCERR_BADDATE))
451 return(0);
452 m->meta.date = time(NULL);
453 }
454
455 return(post_prol(m, n));
456 }
457
458
459 /*
460 * Remove prologue macros from the document after they're processed.
461 * The final document uses mdoc_meta for these values and discards the
462 * originals.
463 */
464 static int
465 post_prol(POST_ARGS)
466 {
467
468 mdoc_node_delete(m, n);
469 if (m->meta.title && m->meta.date && m->meta.os)
470 m->flags |= MDOC_PBODY;
471 return(1);
472 }