]> git.cameronkatri.com Git - mandoc.git/blob - strings.c
Broken build (changing err/warn/msg function prototypes for clarity).
[mandoc.git] / strings.c
1 /* $Id: strings.c,v 1.6 2009/01/14 11:58:24 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
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
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #ifdef __linux__
25 #include <time.h>
26 #endif
27
28 #include "private.h"
29
30 #ifdef __linux__
31 extern char *strptime(const char *, const char *, struct tm *);
32 #endif
33
34 int
35 mdoc_iscdelim(char p)
36 {
37
38 switch (p) {
39 case('{'):
40 /* FALLTHROUGH */
41 case('.'):
42 /* FALLTHROUGH */
43 case(','):
44 /* FALLTHROUGH */
45 case(';'):
46 /* FALLTHROUGH */
47 case(':'):
48 /* FALLTHROUGH */
49 case('?'):
50 /* FALLTHROUGH */
51 case('!'):
52 /* FALLTHROUGH */
53 case('('):
54 /* FALLTHROUGH */
55 case(')'):
56 /* FALLTHROUGH */
57 case('['):
58 /* FALLTHROUGH */
59 case(']'):
60 /* FALLTHROUGH */
61 case('}'):
62 return(1);
63 default:
64 break;
65 }
66
67 return(0);
68 }
69
70
71 int
72 mdoc_isdelim(const char *p)
73 {
74
75 if (0 == *p)
76 return(0);
77 if (0 != *(p + 1))
78 return(0);
79 return(mdoc_iscdelim(*p));
80 }
81
82
83 enum mdoc_sec
84 mdoc_atosec(size_t sz, const char **p)
85 {
86
87 assert(sz > 0);
88 if (sz > 2)
89 return(SEC_CUSTOM);
90 if (sz == 2) {
91 if (0 == strcmp(*p, "RETURN") &&
92 0 == strcmp(*(p + 1), "VALUES"))
93 return(SEC_RETURN_VALUES);
94 if (0 == strcmp(*p, "SEE") &&
95 0 == strcmp(*(p + 1), "ALSO"))
96 return(SEC_SEE_ALSO);
97 return(SEC_CUSTOM);
98 }
99
100 if (0 == strcmp(*p, "NAME"))
101 return(SEC_NAME);
102 else if (0 == strcmp(*p, "SYNOPSIS"))
103 return(SEC_SYNOPSIS);
104 else if (0 == strcmp(*p, "DESCRIPTION"))
105 return(SEC_DESCRIPTION);
106 else if (0 == strcmp(*p, "ENVIRONMENT"))
107 return(SEC_ENVIRONMENT);
108 else if (0 == strcmp(*p, "FILES"))
109 return(SEC_FILES);
110 else if (0 == strcmp(*p, "EXAMPLES"))
111 return(SEC_EXAMPLES);
112 else if (0 == strcmp(*p, "DIAGNOSTICS"))
113 return(SEC_DIAGNOSTICS);
114 else if (0 == strcmp(*p, "ERRORS"))
115 return(SEC_ERRORS);
116 else if (0 == strcmp(*p, "STANDARDS"))
117 return(SEC_STANDARDS);
118 else if (0 == strcmp(*p, "HISTORY"))
119 return(SEC_HISTORY);
120 else if (0 == strcmp(*p, "AUTHORS"))
121 return(SEC_AUTHORS);
122 else if (0 == strcmp(*p, "CAVEATS"))
123 return(SEC_CAVEATS);
124 else if (0 == strcmp(*p, "BUGS"))
125 return(SEC_BUGS);
126
127 return(SEC_CUSTOM);
128 }
129
130
131 time_t
132 mdoc_atotime(const char *p)
133 {
134 struct tm tm;
135
136 (void)memset(&tm, 0, sizeof(struct tm));
137
138 if (strptime(p, "%b %d %Y", &tm))
139 return(mktime(&tm));
140 if (strptime(p, "%b %d, %Y", &tm))
141 return(mktime(&tm));
142
143 return(0);
144 }
145
146
147 enum mdoc_msec
148 mdoc_atomsec(const char *p)
149 {
150
151 if (0 == strcmp(p, "1"))
152 return(MSEC_1);
153 else if (0 == strcmp(p, "2"))
154 return(MSEC_2);
155 else if (0 == strcmp(p, "3"))
156 return(MSEC_3);
157 else if (0 == strcmp(p, "3f"))
158 return(MSEC_3f);
159 else if (0 == strcmp(p, "3p"))
160 return(MSEC_3p);
161 else if (0 == strcmp(p, "4"))
162 return(MSEC_4);
163 else if (0 == strcmp(p, "5"))
164 return(MSEC_5);
165 else if (0 == strcmp(p, "6"))
166 return(MSEC_6);
167 else if (0 == strcmp(p, "7"))
168 return(MSEC_7);
169 else if (0 == strcmp(p, "8"))
170 return(MSEC_8);
171 else if (0 == strcmp(p, "9"))
172 return(MSEC_9);
173 else if (0 == strcmp(p, "X11"))
174 return(MSEC_X11);
175 else if (0 == strcmp(p, "X11R6"))
176 return(MSEC_X11R6);
177 else if (0 == strcmp(p, "local"))
178 return(MSEC_local);
179 else if (0 == strcmp(p, "n"))
180 return(MSEC_n);
181 else if (0 == strcmp(p, "unass"))
182 return(MSEC_unass);
183 else if (0 == strcmp(p, "draft"))
184 return(MSEC_draft);
185 else if (0 == strcmp(p, "paper"))
186 return(MSEC_paper);
187
188 return(MSEC_DEFAULT);
189 }
190
191
192 enum mdoc_vol
193 mdoc_atovol(const char *p)
194 {
195
196 if (0 == strcmp(p, "AMD"))
197 return(VOL_AMD);
198 else if (0 == strcmp(p, "IND"))
199 return(VOL_IND);
200 else if (0 == strcmp(p, "KM"))
201 return(VOL_KM);
202 else if (0 == strcmp(p, "LOCAL"))
203 return(VOL_LOCAL);
204 else if (0 == strcmp(p, "PRM"))
205 return(VOL_PRM);
206 else if (0 == strcmp(p, "PS1"))
207 return(VOL_PS1);
208 else if (0 == strcmp(p, "SMM"))
209 return(VOL_SMM);
210 else if (0 == strcmp(p, "URM"))
211 return(VOL_URM);
212 else if (0 == strcmp(p, "USD"))
213 return(VOL_USD);
214
215 return(VOL_DEFAULT);
216 }
217
218
219 enum mdoc_arch
220 mdoc_atoarch(const char *p)
221 {
222
223 if (0 == strcmp(p, "alpha"))
224 return(ARCH_alpha);
225 else if (0 == strcmp(p, "amd64"))
226 return(ARCH_amd64);
227 else if (0 == strcmp(p, "amiga"))
228 return(ARCH_amiga);
229 else if (0 == strcmp(p, "arc"))
230 return(ARCH_arc);
231 else if (0 == strcmp(p, "armish"))
232 return(ARCH_armish);
233 else if (0 == strcmp(p, "aviion"))
234 return(ARCH_aviion);
235 else if (0 == strcmp(p, "hp300"))
236 return(ARCH_hp300);
237 else if (0 == strcmp(p, "hppa"))
238 return(ARCH_hppa);
239 else if (0 == strcmp(p, "hppa64"))
240 return(ARCH_hppa64);
241 else if (0 == strcmp(p, "i386"))
242 return(ARCH_i386);
243 else if (0 == strcmp(p, "landisk"))
244 return(ARCH_landisk);
245 else if (0 == strcmp(p, "luna88k"))
246 return(ARCH_luna88k);
247 else if (0 == strcmp(p, "mac68k"))
248 return(ARCH_mac68k);
249 else if (0 == strcmp(p, "macppc"))
250 return(ARCH_macppc);
251 else if (0 == strcmp(p, "mvme68k"))
252 return(ARCH_mvme68k);
253 else if (0 == strcmp(p, "mvme88k"))
254 return(ARCH_mvme88k);
255 else if (0 == strcmp(p, "mvmeppc"))
256 return(ARCH_mvmeppc);
257 else if (0 == strcmp(p, "pmax"))
258 return(ARCH_pmax);
259 else if (0 == strcmp(p, "sgi"))
260 return(ARCH_sgi);
261 else if (0 == strcmp(p, "socppc"))
262 return(ARCH_socppc);
263 else if (0 == strcmp(p, "sparc"))
264 return(ARCH_sparc);
265 else if (0 == strcmp(p, "sparc64"))
266 return(ARCH_sparc64);
267 else if (0 == strcmp(p, "sun3"))
268 return(ARCH_sun3);
269 else if (0 == strcmp(p, "vax"))
270 return(ARCH_vax);
271 else if (0 == strcmp(p, "zaurus"))
272 return(ARCH_zaurus);
273
274 return(ARCH_DEFAULT);
275 }
276
277
278 enum mdoc_att
279 mdoc_atoatt(const char *p)
280 {
281
282 assert(p);
283 if (0 == strcmp(p, "v1"))
284 return(ATT_v1);
285 else if (0 == strcmp(p, "v2"))
286 return(ATT_v2);
287 else if (0 == strcmp(p, "v3"))
288 return(ATT_v3);
289 else if (0 == strcmp(p, "v4"))
290 return(ATT_v4);
291 else if (0 == strcmp(p, "v5"))
292 return(ATT_v5);
293 else if (0 == strcmp(p, "v6"))
294 return(ATT_v6);
295 else if (0 == strcmp(p, "v7"))
296 return(ATT_v7);
297 else if (0 == strcmp(p, "32v"))
298 return(ATT_32v);
299 else if (0 == strcmp(p, "V.1"))
300 return(ATT_V1);
301 else if (0 == strcmp(p, "V.2"))
302 return(ATT_V2);
303 else if (0 == strcmp(p, "V.3"))
304 return(ATT_V3);
305 else if (0 == strcmp(p, "V.4"))
306 return(ATT_V4);
307
308 return(ATT_DEFAULT);
309 }
310
311
312 char *
313 mdoc_type2a(enum mdoc_type type)
314 {
315 switch (type) {
316 case (MDOC_ROOT):
317 return("root");
318 case (MDOC_BLOCK):
319 return("block");
320 case (MDOC_HEAD):
321 return("block-head");
322 case (MDOC_BODY):
323 return("block-body");
324 case (MDOC_TAIL):
325 return("block-tail");
326 case (MDOC_ELEM):
327 return("elem");
328 case (MDOC_TEXT):
329 return("text");
330 default:
331 break;
332 }
333
334 abort();
335 /* NOTREACHED */
336 }