]> git.cameronkatri.com Git - mandoc.git/blob - strings.c
Boolean validation.
[mandoc.git] / strings.c
1 /* $Id: strings.c,v 1.8 2009/01/16 12:23:25 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 (0 == strptime(p, "%b %d %Y", &tm))
139 return(mktime(&tm));
140 if (0 == 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, "arm"))
232 return(ARCH_arm);
233 else if (0 == strcmp(p, "armish"))
234 return(ARCH_armish);
235 else if (0 == strcmp(p, "aviion"))
236 return(ARCH_aviion);
237 else if (0 == strcmp(p, "hp300"))
238 return(ARCH_hp300);
239 else if (0 == strcmp(p, "hppa"))
240 return(ARCH_hppa);
241 else if (0 == strcmp(p, "hppa64"))
242 return(ARCH_hppa64);
243 else if (0 == strcmp(p, "i386"))
244 return(ARCH_i386);
245 else if (0 == strcmp(p, "landisk"))
246 return(ARCH_landisk);
247 else if (0 == strcmp(p, "luna88k"))
248 return(ARCH_luna88k);
249 else if (0 == strcmp(p, "mac68k"))
250 return(ARCH_mac68k);
251 else if (0 == strcmp(p, "macppc"))
252 return(ARCH_macppc);
253 else if (0 == strcmp(p, "mvme68k"))
254 return(ARCH_mvme68k);
255 else if (0 == strcmp(p, "mvme88k"))
256 return(ARCH_mvme88k);
257 else if (0 == strcmp(p, "mvmeppc"))
258 return(ARCH_mvmeppc);
259 else if (0 == strcmp(p, "pmax"))
260 return(ARCH_pmax);
261 else if (0 == strcmp(p, "sgi"))
262 return(ARCH_sgi);
263 else if (0 == strcmp(p, "socppc"))
264 return(ARCH_socppc);
265 else if (0 == strcmp(p, "sparc"))
266 return(ARCH_sparc);
267 else if (0 == strcmp(p, "sparc64"))
268 return(ARCH_sparc64);
269 else if (0 == strcmp(p, "sun3"))
270 return(ARCH_sun3);
271 else if (0 == strcmp(p, "vax"))
272 return(ARCH_vax);
273 else if (0 == strcmp(p, "zaurus"))
274 return(ARCH_zaurus);
275
276 return(ARCH_DEFAULT);
277 }
278
279
280 enum mdoc_att
281 mdoc_atoatt(const char *p)
282 {
283
284 assert(p);
285 if (0 == strcmp(p, "v1"))
286 return(ATT_v1);
287 else if (0 == strcmp(p, "v2"))
288 return(ATT_v2);
289 else if (0 == strcmp(p, "v3"))
290 return(ATT_v3);
291 else if (0 == strcmp(p, "v4"))
292 return(ATT_v4);
293 else if (0 == strcmp(p, "v5"))
294 return(ATT_v5);
295 else if (0 == strcmp(p, "v6"))
296 return(ATT_v6);
297 else if (0 == strcmp(p, "v7"))
298 return(ATT_v7);
299 else if (0 == strcmp(p, "32v"))
300 return(ATT_32v);
301 else if (0 == strcmp(p, "V.1"))
302 return(ATT_V1);
303 else if (0 == strcmp(p, "V.2"))
304 return(ATT_V2);
305 else if (0 == strcmp(p, "V.3"))
306 return(ATT_V3);
307 else if (0 == strcmp(p, "V.4"))
308 return(ATT_V4);
309
310 return(ATT_DEFAULT);
311 }
312
313
314 char *
315 mdoc_type2a(enum mdoc_type type)
316 {
317 switch (type) {
318 case (MDOC_ROOT):
319 return("root");
320 case (MDOC_BLOCK):
321 return("block");
322 case (MDOC_HEAD):
323 return("block-head");
324 case (MDOC_BODY):
325 return("block-body");
326 case (MDOC_TAIL):
327 return("block-tail");
328 case (MDOC_ELEM):
329 return("elem");
330 case (MDOC_TEXT):
331 return("text");
332 default:
333 break;
334 }
335
336 abort();
337 /* NOTREACHED */
338 }