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