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