]> git.cameronkatri.com Git - mandoc.git/blob - strings.c
Documentation, more validations, cleaned up actions, added Nm action.
[mandoc.git] / strings.c
1 /* $Id: strings.c,v 1.9 2009/01/17 16:15:27 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(const char *p)
85 {
86
87 assert(p);
88 if (0 == strcmp(p, "NAME"))
89 return(SEC_NAME);
90 else if (0 == strcmp(p, "RETURN VALUES"))
91 return(SEC_RETURN_VALUES);
92 else if (0 == strcmp(p, "SEE ALSO"))
93 return(SEC_SEE_ALSO);
94 else if (0 == strcmp(p, "SYNOPSIS"))
95 return(SEC_SYNOPSIS);
96 else if (0 == strcmp(p, "DESCRIPTION"))
97 return(SEC_DESCRIPTION);
98 else if (0 == strcmp(p, "ENVIRONMENT"))
99 return(SEC_ENVIRONMENT);
100 else if (0 == strcmp(p, "FILES"))
101 return(SEC_FILES);
102 else if (0 == strcmp(p, "EXAMPLES"))
103 return(SEC_EXAMPLES);
104 else if (0 == strcmp(p, "DIAGNOSTICS"))
105 return(SEC_DIAGNOSTICS);
106 else if (0 == strcmp(p, "ERRORS"))
107 return(SEC_ERRORS);
108 else if (0 == strcmp(p, "STANDARDS"))
109 return(SEC_STANDARDS);
110 else if (0 == strcmp(p, "HISTORY"))
111 return(SEC_HISTORY);
112 else if (0 == strcmp(p, "AUTHORS"))
113 return(SEC_AUTHORS);
114 else if (0 == strcmp(p, "CAVEATS"))
115 return(SEC_CAVEATS);
116 else if (0 == strcmp(p, "BUGS"))
117 return(SEC_BUGS);
118
119 return(SEC_CUSTOM);
120 }
121
122
123 time_t
124 mdoc_atotime(const char *p)
125 {
126 struct tm tm;
127
128 (void)memset(&tm, 0, sizeof(struct tm));
129
130 if (0 == strptime(p, "%b %d %Y", &tm))
131 return(mktime(&tm));
132 if (0 == strptime(p, "%b %d, %Y", &tm))
133 return(mktime(&tm));
134
135 return(0);
136 }
137
138
139 enum mdoc_msec
140 mdoc_atomsec(const char *p)
141 {
142
143 if (0 == strcmp(p, "1"))
144 return(MSEC_1);
145 else if (0 == strcmp(p, "2"))
146 return(MSEC_2);
147 else if (0 == strcmp(p, "3"))
148 return(MSEC_3);
149 else if (0 == strcmp(p, "3f"))
150 return(MSEC_3f);
151 else if (0 == strcmp(p, "3p"))
152 return(MSEC_3p);
153 else if (0 == strcmp(p, "4"))
154 return(MSEC_4);
155 else if (0 == strcmp(p, "5"))
156 return(MSEC_5);
157 else if (0 == strcmp(p, "6"))
158 return(MSEC_6);
159 else if (0 == strcmp(p, "7"))
160 return(MSEC_7);
161 else if (0 == strcmp(p, "8"))
162 return(MSEC_8);
163 else if (0 == strcmp(p, "9"))
164 return(MSEC_9);
165 else if (0 == strcmp(p, "X11"))
166 return(MSEC_X11);
167 else if (0 == strcmp(p, "X11R6"))
168 return(MSEC_X11R6);
169 else if (0 == strcmp(p, "local"))
170 return(MSEC_local);
171 else if (0 == strcmp(p, "n"))
172 return(MSEC_n);
173 else if (0 == strcmp(p, "unass"))
174 return(MSEC_unass);
175 else if (0 == strcmp(p, "draft"))
176 return(MSEC_draft);
177 else if (0 == strcmp(p, "paper"))
178 return(MSEC_paper);
179
180 return(MSEC_DEFAULT);
181 }
182
183
184 enum mdoc_vol
185 mdoc_atovol(const char *p)
186 {
187
188 if (0 == strcmp(p, "AMD"))
189 return(VOL_AMD);
190 else if (0 == strcmp(p, "IND"))
191 return(VOL_IND);
192 else if (0 == strcmp(p, "KM"))
193 return(VOL_KM);
194 else if (0 == strcmp(p, "LOCAL"))
195 return(VOL_LOCAL);
196 else if (0 == strcmp(p, "PRM"))
197 return(VOL_PRM);
198 else if (0 == strcmp(p, "PS1"))
199 return(VOL_PS1);
200 else if (0 == strcmp(p, "SMM"))
201 return(VOL_SMM);
202 else if (0 == strcmp(p, "URM"))
203 return(VOL_URM);
204 else if (0 == strcmp(p, "USD"))
205 return(VOL_USD);
206
207 return(VOL_DEFAULT);
208 }
209
210
211 enum mdoc_arch
212 mdoc_atoarch(const char *p)
213 {
214
215 if (0 == strcmp(p, "alpha"))
216 return(ARCH_alpha);
217 else if (0 == strcmp(p, "amd64"))
218 return(ARCH_amd64);
219 else if (0 == strcmp(p, "amiga"))
220 return(ARCH_amiga);
221 else if (0 == strcmp(p, "arc"))
222 return(ARCH_arc);
223 else if (0 == strcmp(p, "arm"))
224 return(ARCH_arm);
225 else if (0 == strcmp(p, "armish"))
226 return(ARCH_armish);
227 else if (0 == strcmp(p, "aviion"))
228 return(ARCH_aviion);
229 else if (0 == strcmp(p, "hp300"))
230 return(ARCH_hp300);
231 else if (0 == strcmp(p, "hppa"))
232 return(ARCH_hppa);
233 else if (0 == strcmp(p, "hppa64"))
234 return(ARCH_hppa64);
235 else if (0 == strcmp(p, "i386"))
236 return(ARCH_i386);
237 else if (0 == strcmp(p, "landisk"))
238 return(ARCH_landisk);
239 else if (0 == strcmp(p, "luna88k"))
240 return(ARCH_luna88k);
241 else if (0 == strcmp(p, "mac68k"))
242 return(ARCH_mac68k);
243 else if (0 == strcmp(p, "macppc"))
244 return(ARCH_macppc);
245 else if (0 == strcmp(p, "mvme68k"))
246 return(ARCH_mvme68k);
247 else if (0 == strcmp(p, "mvme88k"))
248 return(ARCH_mvme88k);
249 else if (0 == strcmp(p, "mvmeppc"))
250 return(ARCH_mvmeppc);
251 else if (0 == strcmp(p, "pmax"))
252 return(ARCH_pmax);
253 else if (0 == strcmp(p, "sgi"))
254 return(ARCH_sgi);
255 else if (0 == strcmp(p, "socppc"))
256 return(ARCH_socppc);
257 else if (0 == strcmp(p, "sparc"))
258 return(ARCH_sparc);
259 else if (0 == strcmp(p, "sparc64"))
260 return(ARCH_sparc64);
261 else if (0 == strcmp(p, "sun3"))
262 return(ARCH_sun3);
263 else if (0 == strcmp(p, "vax"))
264 return(ARCH_vax);
265 else if (0 == strcmp(p, "zaurus"))
266 return(ARCH_zaurus);
267
268 return(ARCH_DEFAULT);
269 }
270
271
272 enum mdoc_att
273 mdoc_atoatt(const char *p)
274 {
275
276 assert(p);
277 if (0 == strcmp(p, "v1"))
278 return(ATT_v1);
279 else if (0 == strcmp(p, "v2"))
280 return(ATT_v2);
281 else if (0 == strcmp(p, "v3"))
282 return(ATT_v3);
283 else if (0 == strcmp(p, "v4"))
284 return(ATT_v4);
285 else if (0 == strcmp(p, "v5"))
286 return(ATT_v5);
287 else if (0 == strcmp(p, "v6"))
288 return(ATT_v6);
289 else if (0 == strcmp(p, "v7"))
290 return(ATT_v7);
291 else if (0 == strcmp(p, "32v"))
292 return(ATT_32v);
293 else if (0 == strcmp(p, "V.1"))
294 return(ATT_V1);
295 else if (0 == strcmp(p, "V.2"))
296 return(ATT_V2);
297 else if (0 == strcmp(p, "V.3"))
298 return(ATT_V3);
299 else if (0 == strcmp(p, "V.4"))
300 return(ATT_V4);
301
302 return(ATT_DEFAULT);
303 }
304
305
306 char *
307 mdoc_type2a(enum mdoc_type type)
308 {
309 switch (type) {
310 case (MDOC_ROOT):
311 return("root");
312 case (MDOC_BLOCK):
313 return("block");
314 case (MDOC_HEAD):
315 return("block-head");
316 case (MDOC_BODY):
317 return("block-body");
318 case (MDOC_TAIL):
319 return("block-tail");
320 case (MDOC_ELEM):
321 return("elem");
322 case (MDOC_TEXT):
323 return("text");
324 default:
325 break;
326 }
327
328 abort();
329 /* NOTREACHED */
330 }