Annotation of mandoc/mdoc_action.c, Revision 1.87
1.87 ! kristaps 1: /* $Id: mdoc_action.c,v 1.86 2010/11/30 10:32:05 kristaps Exp $ */
1.1 kristaps 2: /*
1.76 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.8 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.8 kristaps 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.
1.1 kristaps 16: */
1.50 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.44 kristaps 21: #ifndef OSNAME
1.1 kristaps 22: #include <sys/utsname.h>
1.44 kristaps 23: #endif
1.1 kristaps 24:
25: #include <assert.h>
26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
1.44 kristaps 29: #include <time.h>
1.1 kristaps 30:
1.59 kristaps 31: #include "mandoc.h"
1.1 kristaps 32: #include "libmdoc.h"
1.46 kristaps 33: #include "libmandoc.h"
1.72 kristaps 34:
1.87 ! kristaps 35: #define DATESIZ 32
1.72 kristaps 36: /*
37: * FIXME: this file is deprecated. All future "actions" should be
38: * pushed into mdoc_validate.c.
39: */
1.1 kristaps 40:
1.35 kristaps 41: #define POST_ARGS struct mdoc *m, struct mdoc_node *n
1.60 kristaps 42: #define PRE_ARGS struct mdoc *m, struct mdoc_node *n
1.1 kristaps 43:
44: struct actions {
45: int (*pre)(PRE_ARGS);
46: int (*post)(POST_ARGS);
47: };
48:
1.46 kristaps 49: static int concat(struct mdoc *, char *,
50: const struct mdoc_node *, size_t);
1.39 kristaps 51:
1.1 kristaps 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:
1.37 kristaps 58: static const struct actions mdoc_actions[MDOC_MAX] = {
1.12 kristaps 59: { NULL, NULL }, /* Ap */
1.1 kristaps 60: { NULL, post_dd }, /* Dd */
61: { NULL, post_dt }, /* Dt */
62: { NULL, post_os }, /* Os */
1.81 kristaps 63: { NULL, NULL }, /* Sh */
1.1 kristaps 64: { NULL, NULL }, /* Ss */
65: { NULL, NULL }, /* Pp */
66: { NULL, NULL }, /* D1 */
1.84 kristaps 67: { NULL, NULL }, /* Dl */
68: { NULL, NULL }, /* Bd */
1.1 kristaps 69: { NULL, NULL }, /* Ed */
1.87 ! kristaps 70: { NULL, NULL }, /* Bl */
1.1 kristaps 71: { NULL, NULL }, /* El */
72: { NULL, NULL }, /* It */
73: { NULL, NULL }, /* Ad */
74: { NULL, NULL }, /* An */
1.80 kristaps 75: { NULL, NULL }, /* Ar */
1.35 kristaps 76: { NULL, NULL }, /* Cd */
1.1 kristaps 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 */
1.80 kristaps 89: { NULL, NULL }, /* Li */
1.1 kristaps 90: { NULL, NULL }, /* Nd */
1.85 kristaps 91: { NULL, NULL }, /* Nm */
1.1 kristaps 92: { NULL, NULL }, /* Op */
93: { NULL, NULL }, /* Ot */
1.85 kristaps 94: { NULL, NULL }, /* Pa */
1.1 kristaps 95: { NULL, post_std }, /* Rv */
1.84 kristaps 96: { NULL, NULL }, /* St */
1.1 kristaps 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 */
1.82 kristaps 114: { NULL, NULL }, /* At */
1.1 kristaps 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 */
1.79 kristaps 144: { NULL, NULL }, /* Rs */
1.1 kristaps 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 */
1.83 kristaps 165: { NULL, NULL }, /* Lb */
1.1 kristaps 166: { NULL, NULL }, /* Lp */
1.44 kristaps 167: { NULL, NULL }, /* Lk */
1.1 kristaps 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 */
1.30 kristaps 177: { NULL, NULL }, /* br */
178: { NULL, NULL }, /* sp */
1.43 kristaps 179: { NULL, NULL }, /* %U */
1.64 kristaps 180: { NULL, NULL }, /* Ta */
1.1 kristaps 181: };
182:
1.2 kristaps 183:
1.1 kristaps 184: int
1.60 kristaps 185: mdoc_action_pre(struct mdoc *m, struct mdoc_node *n)
1.1 kristaps 186: {
187:
188: switch (n->type) {
189: case (MDOC_ROOT):
1.2 kristaps 190: /* FALLTHROUGH */
1.1 kristaps 191: case (MDOC_TEXT):
1.2 kristaps 192: return(1);
193: default:
1.1 kristaps 194: break;
195: }
1.2 kristaps 196:
1.6 kristaps 197: if (NULL == mdoc_actions[n->tok].pre)
1.2 kristaps 198: return(1);
1.6 kristaps 199: return((*mdoc_actions[n->tok].pre)(m, n));
1.1 kristaps 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):
1.2 kristaps 213: /* FALLTHROUGH */
1.1 kristaps 214: case (MDOC_ROOT):
1.2 kristaps 215: return(1);
216: default:
1.1 kristaps 217: break;
218: }
1.2 kristaps 219:
220: if (NULL == mdoc_actions[m->last->tok].post)
221: return(1);
1.35 kristaps 222: return((*mdoc_actions[m->last->tok].post)(m, m->last));
1.2 kristaps 223: }
224:
225:
1.46 kristaps 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: */
1.2 kristaps 231: static int
1.46 kristaps 232: concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz)
1.2 kristaps 233: {
234:
1.46 kristaps 235: assert(sz);
236: p[0] = '\0';
1.2 kristaps 237: for ( ; n; n = n->next) {
238: assert(MDOC_TEXT == n->type);
1.59 kristaps 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: }
1.2 kristaps 248: if (NULL == n->next)
249: continue;
1.59 kristaps 250: if (strlcat(p, " ", sz) >= sz) {
251: mdoc_nmsg(m, n, MANDOCERR_MEM);
252: return(0);
253: }
1.2 kristaps 254: }
255:
1.1 kristaps 256: return(1);
257: }
258:
259:
1.46 kristaps 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: */
1.1 kristaps 264: static int
265: post_std(POST_ARGS)
266: {
1.59 kristaps 267: struct mdoc_node *nn;
1.1 kristaps 268:
1.35 kristaps 269: if (n->child)
1.1 kristaps 270: return(1);
1.59 kristaps 271: if (NULL == m->meta.name)
272: return(1);
1.35 kristaps 273:
274: nn = n;
275: m->next = MDOC_NEXT_CHILD;
1.59 kristaps 276:
1.35 kristaps 277: if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
278: return(0);
279: m->last = nn;
1.1 kristaps 280: return(1);
281: }
282:
1.46 kristaps 283: /*
284: * Parse out the contents of `Dt'. See in-line documentation for how we
285: * handle the various fields of this macro.
286: */
1.1 kristaps 287: static int
288: post_dt(POST_ARGS)
289: {
1.35 kristaps 290: struct mdoc_node *nn;
1.1 kristaps 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:
1.35 kristaps 305: if (NULL == (nn = n->child)) {
1.46 kristaps 306: /* XXX: make these macro values. */
1.58 kristaps 307: /* FIXME: warn about missing values. */
1.63 kristaps 308: m->meta.title = mandoc_strdup("UNKNOWN");
309: m->meta.vol = mandoc_strdup("LOCAL");
1.58 kristaps 310: m->meta.msec = mandoc_strdup("1");
1.35 kristaps 311: return(post_prol(m, n));
1.1 kristaps 312: }
313:
314: /* Handles: `.Dt TITLE'
315: * --> title = TITLE, volume = local, msec = 0, arch = NULL
316: */
317:
1.63 kristaps 318: m->meta.title = mandoc_strdup
319: ('\0' == nn->string[0] ? "UNKNOWN" : nn->string);
1.1 kristaps 320:
1.35 kristaps 321: if (NULL == (nn = nn->next)) {
1.58 kristaps 322: /* FIXME: warn about missing msec. */
1.46 kristaps 323: /* XXX: make this a macro value. */
1.63 kristaps 324: m->meta.vol = mandoc_strdup("LOCAL");
1.58 kristaps 325: m->meta.msec = mandoc_strdup("1");
1.35 kristaps 326: return(post_prol(m, n));
1.1 kristaps 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:
1.35 kristaps 336: cp = mdoc_a2msec(nn->string);
1.1 kristaps 337: if (cp) {
1.46 kristaps 338: m->meta.vol = mandoc_strdup(cp);
1.58 kristaps 339: m->meta.msec = mandoc_strdup(nn->string);
1.59 kristaps 340: } else if (mdoc_nmsg(m, n, MANDOCERR_BADMSEC)) {
1.46 kristaps 341: m->meta.vol = mandoc_strdup(nn->string);
1.58 kristaps 342: m->meta.msec = mandoc_strdup(nn->string);
343: } else
344: return(0);
1.1 kristaps 345:
1.35 kristaps 346: if (NULL == (nn = nn->next))
347: return(post_prol(m, n));
1.1 kristaps 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:
1.35 kristaps 356: cp = mdoc_a2vol(nn->string);
1.1 kristaps 357: if (cp) {
358: free(m->meta.vol);
1.46 kristaps 359: m->meta.vol = mandoc_strdup(cp);
1.1 kristaps 360: } else {
1.58 kristaps 361: /* FIXME: warn about bad arch. */
1.35 kristaps 362: cp = mdoc_a2arch(nn->string);
1.1 kristaps 363: if (NULL == cp) {
364: free(m->meta.vol);
1.46 kristaps 365: m->meta.vol = mandoc_strdup(nn->string);
366: } else
367: m->meta.arch = mandoc_strdup(cp);
1.1 kristaps 368: }
369:
370: /* Ignore any subsequent parameters... */
1.46 kristaps 371: /* FIXME: warn about subsequent parameters. */
1.1 kristaps 372:
1.35 kristaps 373: return(post_prol(m, n));
1.1 kristaps 374: }
375:
376:
1.46 kristaps 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: */
1.1 kristaps 383: static int
384: post_os(POST_ARGS)
385: {
1.46 kristaps 386: char buf[BUFSIZ];
387: #ifndef OSNAME
1.1 kristaps 388: struct utsname utsname;
1.42 kristaps 389: #endif
390:
1.1 kristaps 391: if (m->meta.os)
392: free(m->meta.os);
1.4 kristaps 393:
1.46 kristaps 394: if ( ! concat(m, buf, n->child, BUFSIZ))
1.2 kristaps 395: return(0);
1.1 kristaps 396:
1.59 kristaps 397: /* XXX: yes, these can all be dynamically-adjusted buffers, but
398: * it's really not worth the extra hackery.
399: */
400:
1.46 kristaps 401: if ('\0' == buf[0]) {
402: #ifdef OSNAME
1.59 kristaps 403: if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) {
404: mdoc_nmsg(m, n, MANDOCERR_MEM);
405: return(0);
406: }
1.46 kristaps 407: #else /*!OSNAME */
1.1 kristaps 408: if (-1 == uname(&utsname))
1.59 kristaps 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: }
1.46 kristaps 423: #endif /*!OSNAME*/
1.1 kristaps 424: }
425:
1.46 kristaps 426: m->meta.os = mandoc_strdup(buf);
1.35 kristaps 427: return(post_prol(m, n));
1.1 kristaps 428: }
429:
1.46 kristaps 430: /*
1.49 kristaps 431: * Parse the date field in `Dd'.
1.46 kristaps 432: */
1.1 kristaps 433: static int
434: post_dd(POST_ARGS)
435: {
1.46 kristaps 436: char buf[DATESIZ];
1.77 kristaps 437:
438: if (NULL == n->child) {
439: m->meta.date = time(NULL);
440: return(post_prol(m, n));
441: }
1.1 kristaps 442:
1.46 kristaps 443: if ( ! concat(m, buf, n->child, DATESIZ))
1.2 kristaps 444: return(0);
1.1 kristaps 445:
1.49 kristaps 446: m->meta.date = mandoc_a2time
447: (MTIME_MDOCDATE | MTIME_CANONICAL, buf);
448:
1.46 kristaps 449: if (0 == m->meta.date) {
1.59 kristaps 450: if ( ! mdoc_nmsg(m, n, MANDOCERR_BADDATE))
1.1 kristaps 451: return(0);
452: m->meta.date = time(NULL);
453: }
454:
1.35 kristaps 455: return(post_prol(m, n));
1.1 kristaps 456: }
457:
458:
1.46 kristaps 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: */
1.1 kristaps 464: static int
465: post_prol(POST_ARGS)
466: {
1.34 kristaps 467:
1.53 kristaps 468: mdoc_node_delete(m, n);
1.34 kristaps 469: if (m->meta.title && m->meta.date && m->meta.os)
470: m->flags |= MDOC_PBODY;
1.39 kristaps 471: return(1);
472: }
CVSweb