Annotation of mandoc/man_action.c, Revision 1.42
1.42 ! kristaps 1: /* $Id: man_action.c,v 1.41 2010/07/31 23:52:58 schwarze Exp $ */
1.1 kristaps 2: /*
1.41 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.10 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.10 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.25 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
1.1 kristaps 20:
21: #include <assert.h>
22: #include <stdlib.h>
23: #include <string.h>
1.42 ! kristaps 24: #include <time.h>
1.1 kristaps 25:
1.36 kristaps 26: #include "mandoc.h"
1.1 kristaps 27: #include "libman.h"
1.22 kristaps 28: #include "libmandoc.h"
1.1 kristaps 29:
30: struct actions {
31: int (*post)(struct man *);
32: };
33:
1.2 kristaps 34: static int post_TH(struct man *);
1.18 kristaps 35: static int post_fi(struct man *);
36: static int post_nf(struct man *);
1.35 joerg 37: static int post_AT(struct man *);
38: static int post_UC(struct man *);
1.2 kristaps 39:
1.1 kristaps 40: const struct actions man_actions[MAN_MAX] = {
1.12 kristaps 41: { NULL }, /* br */
1.2 kristaps 42: { post_TH }, /* TH */
1.1 kristaps 43: { NULL }, /* SH */
44: { NULL }, /* SS */
45: { NULL }, /* TP */
46: { NULL }, /* LP */
47: { NULL }, /* PP */
48: { NULL }, /* P */
49: { NULL }, /* IP */
50: { NULL }, /* HP */
51: { NULL }, /* SM */
52: { NULL }, /* SB */
53: { NULL }, /* BI */
54: { NULL }, /* IB */
55: { NULL }, /* BR */
56: { NULL }, /* RB */
57: { NULL }, /* R */
58: { NULL }, /* B */
59: { NULL }, /* I */
60: { NULL }, /* IR */
1.5 kristaps 61: { NULL }, /* RI */
1.8 kristaps 62: { NULL }, /* na */
1.9 kristaps 63: { NULL }, /* i */
1.14 kristaps 64: { NULL }, /* sp */
1.18 kristaps 65: { post_nf }, /* nf */
66: { post_fi }, /* fi */
1.16 kristaps 67: { NULL }, /* r */
68: { NULL }, /* RE */
69: { NULL }, /* RS */
1.17 kristaps 70: { NULL }, /* DT */
1.35 joerg 71: { post_UC }, /* UC */
1.20 kristaps 72: { NULL }, /* PD */
1.27 kristaps 73: { NULL }, /* Sp */
74: { post_nf }, /* Vb */
75: { post_fi }, /* Ve */
1.35 joerg 76: { post_AT }, /* AT */
1.40 kristaps 77: { NULL }, /* in */
1.1 kristaps 78: };
79:
80:
81: int
82: man_action_post(struct man *m)
83: {
84:
85: if (MAN_ACTED & m->last->flags)
86: return(1);
87: m->last->flags |= MAN_ACTED;
88:
89: switch (m->last->type) {
90: case (MAN_TEXT):
1.18 kristaps 91: /* FALLTHROUGH */
1.1 kristaps 92: case (MAN_ROOT):
1.18 kristaps 93: return(1);
94: default:
1.1 kristaps 95: break;
96: }
1.18 kristaps 97:
98: if (NULL == man_actions[m->last->tok].post)
99: return(1);
100: return((*man_actions[m->last->tok].post)(m));
101: }
102:
103:
104: static int
105: post_fi(struct man *m)
106: {
107:
108: if ( ! (MAN_LITERAL & m->flags))
1.36 kristaps 109: if ( ! man_nmsg(m, m->last, MANDOCERR_NOSCOPE))
1.18 kristaps 110: return(0);
111: m->flags &= ~MAN_LITERAL;
1.29 kristaps 112: return(1);
113: }
114:
115:
116: static int
1.18 kristaps 117: post_nf(struct man *m)
118: {
119:
120: if (MAN_LITERAL & m->flags)
1.36 kristaps 121: if ( ! man_nmsg(m, m->last, MANDOCERR_SCOPEREP))
1.18 kristaps 122: return(0);
123: m->flags |= MAN_LITERAL;
1.1 kristaps 124: return(1);
125: }
126:
1.2 kristaps 127:
1.1 kristaps 128: static int
1.2 kristaps 129: post_TH(struct man *m)
1.1 kristaps 130: {
1.2 kristaps 131: struct man_node *n;
1.1 kristaps 132:
133: if (m->meta.title)
134: free(m->meta.title);
135: if (m->meta.vol)
136: free(m->meta.vol);
1.2 kristaps 137: if (m->meta.source)
138: free(m->meta.source);
1.32 kristaps 139: if (m->meta.msec)
140: free(m->meta.msec);
1.39 kristaps 141: if (m->meta.rawdate)
142: free(m->meta.rawdate);
1.1 kristaps 143:
1.39 kristaps 144: m->meta.title = m->meta.vol = m->meta.rawdate =
1.31 kristaps 145: m->meta.msec = m->meta.source = NULL;
1.2 kristaps 146: m->meta.date = 0;
147:
148: /* ->TITLE<- MSEC DATE SOURCE VOL */
149:
150: n = m->last->child;
151: assert(n);
1.22 kristaps 152: m->meta.title = mandoc_strdup(n->string);
1.2 kristaps 153:
154: /* TITLE ->MSEC<- DATE SOURCE VOL */
1.1 kristaps 155:
1.2 kristaps 156: n = n->next;
157: assert(n);
1.31 kristaps 158: m->meta.msec = mandoc_strdup(n->string);
1.1 kristaps 159:
1.2 kristaps 160: /* TITLE MSEC ->DATE<- SOURCE VOL */
1.1 kristaps 161:
1.39 kristaps 162: /*
163: * Try to parse the date. If this works, stash the epoch (this
164: * is optimal because we can reformat it in the canonical form).
165: * If it doesn't parse, isn't specified at all, or is an empty
166: * string, then use the current date.
167: */
168:
1.24 kristaps 169: n = n->next;
1.39 kristaps 170: if (n && n->string && *n->string) {
1.24 kristaps 171: m->meta.date = mandoc_a2time
172: (MTIME_ISO_8601, n->string);
173: if (0 == m->meta.date) {
1.36 kristaps 174: if ( ! man_nmsg(m, n, MANDOCERR_BADDATE))
1.24 kristaps 175: return(0);
1.39 kristaps 176: m->meta.rawdate = mandoc_strdup(n->string);
1.24 kristaps 177: }
178: } else
1.2 kristaps 179: m->meta.date = time(NULL);
1.1 kristaps 180:
1.2 kristaps 181: /* TITLE MSEC DATE ->SOURCE<- VOL */
1.1 kristaps 182:
1.5 kristaps 183: if (n && (n = n->next))
1.22 kristaps 184: m->meta.source = mandoc_strdup(n->string);
1.1 kristaps 185:
1.2 kristaps 186: /* TITLE MSEC DATE SOURCE ->VOL<- */
1.1 kristaps 187:
1.5 kristaps 188: if (n && (n = n->next))
1.22 kristaps 189: m->meta.vol = mandoc_strdup(n->string);
1.1 kristaps 190:
1.29 kristaps 191: /*
192: * Remove the `TH' node after we've processed it for our
193: * meta-data.
194: */
195: man_node_delete(m, m->last);
1.35 joerg 196: return(1);
197: }
198:
199:
200: static int
201: post_AT(struct man *m)
202: {
203: static const char * const unix_versions[] = {
204: "7th Edition",
205: "System III",
206: "System V",
207: "System V Release 2",
208: };
209:
210: const char *p, *s;
211: struct man_node *n, *nn;
212:
213: n = m->last->child;
214:
215: if (NULL == n || MAN_TEXT != n->type)
216: p = unix_versions[0];
217: else {
218: s = n->string;
219: if (0 == strcmp(s, "3"))
220: p = unix_versions[0];
221: else if (0 == strcmp(s, "4"))
222: p = unix_versions[1];
223: else if (0 == strcmp(s, "5")) {
224: nn = n->next;
225: if (nn && MAN_TEXT == nn->type && nn->string[0])
226: p = unix_versions[3];
227: else
228: p = unix_versions[2];
229: } else
230: p = unix_versions[0];
231: }
1.37 kristaps 232:
233: if (m->meta.source)
234: free(m->meta.source);
1.35 joerg 235:
236: m->meta.source = mandoc_strdup(p);
237:
238: return(1);
239: }
240:
241:
242: static int
243: post_UC(struct man *m)
244: {
245: static const char * const bsd_versions[] = {
246: "3rd Berkeley Distribution",
247: "4th Berkeley Distribution",
248: "4.2 Berkeley Distribution",
249: "4.3 Berkeley Distribution",
250: "4.4 Berkeley Distribution",
251: };
252:
253: const char *p, *s;
254: struct man_node *n;
255:
256: n = m->last->child;
257:
258: if (NULL == n || MAN_TEXT != n->type)
259: p = bsd_versions[0];
260: else {
261: s = n->string;
262: if (0 == strcmp(s, "3"))
263: p = bsd_versions[0];
264: else if (0 == strcmp(s, "4"))
265: p = bsd_versions[1];
266: else if (0 == strcmp(s, "5"))
267: p = bsd_versions[2];
268: else if (0 == strcmp(s, "6"))
269: p = bsd_versions[3];
270: else if (0 == strcmp(s, "7"))
271: p = bsd_versions[4];
272: else
273: p = bsd_versions[0];
274: }
1.38 kristaps 275:
276: if (m->meta.source)
277: free(m->meta.source);
1.35 joerg 278:
279: m->meta.source = mandoc_strdup(p);
280:
1.2 kristaps 281: return(1);
282: }
CVSweb