Annotation of src/usr.bin/mandoc/man_action.c, Revision 1.26
1.26 ! schwarze 1: /* $Id: man_action.c,v 1.25 2010/10/15 20:45:03 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.12 schwarze 74: { NULL }, /* Sp */
75: { post_nf }, /* Vb */
76: { post_fi }, /* Ve */
1.19 schwarze 77: { post_AT }, /* AT */
1.24 schwarze 78: { NULL }, /* in */
1.25 schwarze 79: { post_TS }, /* TS */
80: { NULL }, /* TE */
1.1 kristaps 81: };
82:
83:
84: int
85: man_action_post(struct man *m)
86: {
87:
88: if (MAN_ACTED & m->last->flags)
89: return(1);
90: m->last->flags |= MAN_ACTED;
91:
92: switch (m->last->type) {
93: case (MAN_TEXT):
1.7 schwarze 94: /* FALLTHROUGH */
1.1 kristaps 95: case (MAN_ROOT):
1.7 schwarze 96: return(1);
97: default:
1.1 kristaps 98: break;
99: }
1.7 schwarze 100:
101: if (NULL == man_actions[m->last->tok].post)
102: return(1);
103: return((*man_actions[m->last->tok].post)(m));
104: }
105:
106:
107: static int
108: post_fi(struct man *m)
109: {
110:
111: if ( ! (MAN_LITERAL & m->flags))
1.20 schwarze 112: if ( ! man_nmsg(m, m->last, MANDOCERR_NOSCOPE))
1.7 schwarze 113: return(0);
114: m->flags &= ~MAN_LITERAL;
1.14 schwarze 115: return(1);
116: }
117:
118:
119: static int
1.7 schwarze 120: post_nf(struct man *m)
121: {
122:
123: if (MAN_LITERAL & m->flags)
1.20 schwarze 124: if ( ! man_nmsg(m, m->last, MANDOCERR_SCOPEREP))
1.7 schwarze 125: return(0);
126: m->flags |= MAN_LITERAL;
1.1 kristaps 127: return(1);
128: }
129:
130:
131: static int
132: post_TH(struct man *m)
133: {
134: struct man_node *n;
135:
136: if (m->meta.title)
137: free(m->meta.title);
138: if (m->meta.vol)
139: free(m->meta.vol);
140: if (m->meta.source)
141: free(m->meta.source);
1.16 schwarze 142: if (m->meta.msec)
143: free(m->meta.msec);
1.23 schwarze 144: if (m->meta.rawdate)
145: free(m->meta.rawdate);
1.1 kristaps 146:
1.23 schwarze 147: m->meta.title = m->meta.vol = m->meta.rawdate =
1.16 schwarze 148: m->meta.msec = m->meta.source = NULL;
1.1 kristaps 149: m->meta.date = 0;
150:
151: /* ->TITLE<- MSEC DATE SOURCE VOL */
152:
153: n = m->last->child;
154: assert(n);
1.10 schwarze 155: m->meta.title = mandoc_strdup(n->string);
1.1 kristaps 156:
157: /* TITLE ->MSEC<- DATE SOURCE VOL */
158:
159: n = n->next;
160: assert(n);
1.16 schwarze 161: m->meta.msec = mandoc_strdup(n->string);
1.1 kristaps 162:
163: /* TITLE MSEC ->DATE<- SOURCE VOL */
164:
1.23 schwarze 165: /*
166: * Try to parse the date. If this works, stash the epoch (this
167: * is optimal because we can reformat it in the canonical form).
168: * If it doesn't parse, isn't specified at all, or is an empty
169: * string, then use the current date.
170: */
171:
1.11 schwarze 172: n = n->next;
1.23 schwarze 173: if (n && n->string && *n->string) {
1.11 schwarze 174: m->meta.date = mandoc_a2time
175: (MTIME_ISO_8601, n->string);
176: if (0 == m->meta.date) {
1.20 schwarze 177: if ( ! man_nmsg(m, n, MANDOCERR_BADDATE))
1.11 schwarze 178: return(0);
1.23 schwarze 179: m->meta.rawdate = mandoc_strdup(n->string);
1.11 schwarze 180: }
181: } else
1.1 kristaps 182: m->meta.date = time(NULL);
183:
184: /* TITLE MSEC DATE ->SOURCE<- VOL */
185:
186: if (n && (n = n->next))
1.10 schwarze 187: m->meta.source = mandoc_strdup(n->string);
1.1 kristaps 188:
189: /* TITLE MSEC DATE SOURCE ->VOL<- */
190:
191: if (n && (n = n->next))
1.10 schwarze 192: m->meta.vol = mandoc_strdup(n->string);
1.1 kristaps 193:
1.14 schwarze 194: /*
195: * Remove the `TH' node after we've processed it for our
196: * meta-data.
197: */
198: man_node_delete(m, m->last);
1.19 schwarze 199: return(1);
200: }
201:
202:
203: static int
204: post_AT(struct man *m)
205: {
206: static const char * const unix_versions[] = {
207: "7th Edition",
208: "System III",
209: "System V",
210: "System V Release 2",
211: };
212:
213: const char *p, *s;
214: struct man_node *n, *nn;
215:
216: n = m->last->child;
217:
218: if (NULL == n || MAN_TEXT != n->type)
219: p = unix_versions[0];
220: else {
221: s = n->string;
222: if (0 == strcmp(s, "3"))
223: p = unix_versions[0];
224: else if (0 == strcmp(s, "4"))
225: p = unix_versions[1];
226: else if (0 == strcmp(s, "5")) {
227: nn = n->next;
228: if (nn && MAN_TEXT == nn->type && nn->string[0])
229: p = unix_versions[3];
230: else
231: p = unix_versions[2];
232: } else
233: p = unix_versions[0];
234: }
1.21 schwarze 235:
236: if (m->meta.source)
237: free(m->meta.source);
1.19 schwarze 238:
239: m->meta.source = mandoc_strdup(p);
240:
241: return(1);
242: }
243:
244:
245: static int
246: post_UC(struct man *m)
247: {
248: static const char * const bsd_versions[] = {
249: "3rd Berkeley Distribution",
250: "4th Berkeley Distribution",
251: "4.2 Berkeley Distribution",
252: "4.3 Berkeley Distribution",
253: "4.4 Berkeley Distribution",
254: };
255:
256: const char *p, *s;
257: struct man_node *n;
258:
259: n = m->last->child;
260:
261: if (NULL == n || MAN_TEXT != n->type)
262: p = bsd_versions[0];
263: else {
264: s = n->string;
265: if (0 == strcmp(s, "3"))
266: p = bsd_versions[0];
267: else if (0 == strcmp(s, "4"))
268: p = bsd_versions[1];
269: else if (0 == strcmp(s, "5"))
270: p = bsd_versions[2];
271: else if (0 == strcmp(s, "6"))
272: p = bsd_versions[3];
273: else if (0 == strcmp(s, "7"))
274: p = bsd_versions[4];
275: else
276: p = bsd_versions[0];
277: }
1.22 schwarze 278:
279: if (m->meta.source)
280: free(m->meta.source);
1.19 schwarze 281:
282: m->meta.source = mandoc_strdup(p);
1.25 schwarze 283:
284: return(1);
285: }
286:
287:
288: static int
289: post_TS(struct man *m)
290: {
291:
292: if (MAN_HEAD == m->last->type)
293: m->last->parent->data.TS = tbl_alloc();
1.19 schwarze 294:
1.1 kristaps 295: return(1);
296: }