]> git.cameronkatri.com Git - mandoc.git/blob - strings.c
Adding mdoclint.1 manual.
[mandoc.git] / strings.c
1 /* $Id: strings.c,v 1.14 2009/02/23 12:45:19 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 /*
29 * Convert scalars to and from string format.
30 */
31
32 #include "private.h"
33
34 #ifdef __linux__
35 extern char *strptime(const char *, const char *, struct tm *);
36 #endif
37
38 int
39 mdoc_iscdelim(char p)
40 {
41
42 switch (p) {
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 /* FALLTHROUGH */
63 case('{'):
64 /* FALLTHROUGH */
65 case('}'):
66 return(1);
67 default:
68 break;
69 }
70
71 return(0);
72 }
73
74
75 int
76 mdoc_isdelim(const char *p)
77 {
78
79 if (0 == *p)
80 return(0);
81 if (0 != *(p + 1))
82 return(0);
83 return(mdoc_iscdelim(*p));
84 }
85
86
87 enum mdoc_sec
88 mdoc_atosec(const char *p)
89 {
90
91 assert(p);
92 if (0 == strcmp(p, "NAME"))
93 return(SEC_NAME);
94 else if (0 == strcmp(p, "RETURN VALUES"))
95 return(SEC_RETURN_VALUES);
96 else if (0 == strcmp(p, "SEE ALSO"))
97 return(SEC_SEE_ALSO);
98 else if (0 == strcmp(p, "SYNOPSIS"))
99 return(SEC_SYNOPSIS);
100 else if (0 == strcmp(p, "DESCRIPTION"))
101 return(SEC_DESCRIPTION);
102 else if (0 == strcmp(p, "ENVIRONMENT"))
103 return(SEC_ENVIRONMENT);
104 else if (0 == strcmp(p, "FILES"))
105 return(SEC_FILES);
106 else if (0 == strcmp(p, "EXAMPLES"))
107 return(SEC_EXAMPLES);
108 else if (0 == strcmp(p, "DIAGNOSTICS"))
109 return(SEC_DIAGNOSTICS);
110 else if (0 == strcmp(p, "ERRORS"))
111 return(SEC_ERRORS);
112 else if (0 == strcmp(p, "STANDARDS"))
113 return(SEC_STANDARDS);
114 else if (0 == strcmp(p, "HISTORY"))
115 return(SEC_HISTORY);
116 else if (0 == strcmp(p, "AUTHORS"))
117 return(SEC_AUTHORS);
118 else if (0 == strcmp(p, "CAVEATS"))
119 return(SEC_CAVEATS);
120 else if (0 == strcmp(p, "BUGS"))
121 return(SEC_BUGS);
122
123 return(SEC_CUSTOM);
124 }
125
126
127 time_t
128 mdoc_atotime(const char *p)
129 {
130 struct tm tm;
131 char *pp;
132
133 (void)memset(&tm, 0, sizeof(struct tm));
134
135 if (xstrcmp(p, "$Mdocdate: February 23 2009 $"))
136 return(time(NULL));
137 if ((pp = strptime(p, "$Mdocdate: February 23 2009 $", &tm)) && 0 == *pp)
138 return(mktime(&tm));
139 /* XXX - this matches "June 1999", which is wrong. */
140 if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
141 return(mktime(&tm));
142 if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
143 return(mktime(&tm));
144
145 return(0);
146 }
147
148
149 enum mdoc_msec
150 mdoc_atomsec(const char *p)
151 {
152
153 if (0 == strcmp(p, "1"))
154 return(MSEC_1);
155 else if (0 == strcmp(p, "2"))
156 return(MSEC_2);
157 else if (0 == strcmp(p, "3"))
158 return(MSEC_3);
159 else if (0 == strcmp(p, "3f"))
160 return(MSEC_3f);
161 else if (0 == strcmp(p, "3p"))
162 return(MSEC_3p);
163 else if (0 == strcmp(p, "4"))
164 return(MSEC_4);
165 else if (0 == strcmp(p, "5"))
166 return(MSEC_5);
167 else if (0 == strcmp(p, "6"))
168 return(MSEC_6);
169 else if (0 == strcmp(p, "7"))
170 return(MSEC_7);
171 else if (0 == strcmp(p, "8"))
172 return(MSEC_8);
173 else if (0 == strcmp(p, "9"))
174 return(MSEC_9);
175 else if (0 == strcmp(p, "X11"))
176 return(MSEC_X11);
177 else if (0 == strcmp(p, "X11R6"))
178 return(MSEC_X11R6);
179 else if (0 == strcmp(p, "local"))
180 return(MSEC_local);
181 else if (0 == strcmp(p, "n"))
182 return(MSEC_n);
183 else if (0 == strcmp(p, "unass"))
184 return(MSEC_unass);
185 else if (0 == strcmp(p, "draft"))
186 return(MSEC_draft);
187 else if (0 == strcmp(p, "paper"))
188 return(MSEC_paper);
189
190 return(MSEC_DEFAULT);
191 }
192
193
194 enum mdoc_vol
195 mdoc_atovol(const char *p)
196 {
197
198 if (0 == strcmp(p, "AMD"))
199 return(VOL_AMD);
200 else if (0 == strcmp(p, "IND"))
201 return(VOL_IND);
202 else if (0 == strcmp(p, "KM"))
203 return(VOL_KM);
204 else if (0 == strcmp(p, "LOCAL"))
205 return(VOL_LOCAL);
206 else if (0 == strcmp(p, "PRM"))
207 return(VOL_PRM);
208 else if (0 == strcmp(p, "PS1"))
209 return(VOL_PS1);
210 else if (0 == strcmp(p, "SMM"))
211 return(VOL_SMM);
212 else if (0 == strcmp(p, "URM"))
213 return(VOL_URM);
214 else if (0 == strcmp(p, "USD"))
215 return(VOL_USD);
216
217 return(VOL_DEFAULT);
218 }
219
220
221 enum mdoc_arch
222 mdoc_atoarch(const char *p)
223 {
224
225 if (0 == strcmp(p, "alpha"))
226 return(ARCH_alpha);
227 else if (0 == strcmp(p, "amd64"))
228 return(ARCH_amd64);
229 else if (0 == strcmp(p, "amiga"))
230 return(ARCH_amiga);
231 else if (0 == strcmp(p, "arc"))
232 return(ARCH_arc);
233 else if (0 == strcmp(p, "arm"))
234 return(ARCH_arm);
235 else if (0 == strcmp(p, "armish"))
236 return(ARCH_armish);
237 else if (0 == strcmp(p, "aviion"))
238 return(ARCH_aviion);
239 else if (0 == strcmp(p, "hp300"))
240 return(ARCH_hp300);
241 else if (0 == strcmp(p, "hppa"))
242 return(ARCH_hppa);
243 else if (0 == strcmp(p, "hppa64"))
244 return(ARCH_hppa64);
245 else if (0 == strcmp(p, "i386"))
246 return(ARCH_i386);
247 else if (0 == strcmp(p, "landisk"))
248 return(ARCH_landisk);
249 else if (0 == strcmp(p, "luna88k"))
250 return(ARCH_luna88k);
251 else if (0 == strcmp(p, "mac68k"))
252 return(ARCH_mac68k);
253 else if (0 == strcmp(p, "macppc"))
254 return(ARCH_macppc);
255 else if (0 == strcmp(p, "mvme68k"))
256 return(ARCH_mvme68k);
257 else if (0 == strcmp(p, "mvme88k"))
258 return(ARCH_mvme88k);
259 else if (0 == strcmp(p, "mvmeppc"))
260 return(ARCH_mvmeppc);
261 else if (0 == strcmp(p, "pmax"))
262 return(ARCH_pmax);
263 else if (0 == strcmp(p, "sgi"))
264 return(ARCH_sgi);
265 else if (0 == strcmp(p, "socppc"))
266 return(ARCH_socppc);
267 else if (0 == strcmp(p, "sparc"))
268 return(ARCH_sparc);
269 else if (0 == strcmp(p, "sparc64"))
270 return(ARCH_sparc64);
271 else if (0 == strcmp(p, "sun3"))
272 return(ARCH_sun3);
273 else if (0 == strcmp(p, "vax"))
274 return(ARCH_vax);
275 else if (0 == strcmp(p, "zaurus"))
276 return(ARCH_zaurus);
277
278 return(ARCH_DEFAULT);
279 }
280
281
282 enum mdoc_att
283 mdoc_atoatt(const char *p)
284 {
285
286 assert(p);
287 if (0 == strcmp(p, "v1"))
288 return(ATT_v1);
289 else if (0 == strcmp(p, "v2"))
290 return(ATT_v2);
291 else if (0 == strcmp(p, "v3"))
292 return(ATT_v3);
293 else if (0 == strcmp(p, "v4"))
294 return(ATT_v4);
295 else if (0 == strcmp(p, "v5"))
296 return(ATT_v5);
297 else if (0 == strcmp(p, "v6"))
298 return(ATT_v6);
299 else if (0 == strcmp(p, "v7"))
300 return(ATT_v7);
301 else if (0 == strcmp(p, "32v"))
302 return(ATT_32v);
303 else if (0 == strcmp(p, "V.1"))
304 return(ATT_V1);
305 else if (0 == strcmp(p, "V.2"))
306 return(ATT_V2);
307 else if (0 == strcmp(p, "V.3"))
308 return(ATT_V3);
309 else if (0 == strcmp(p, "V.4"))
310 return(ATT_V4);
311
312 return(ATT_DEFAULT);
313 }
314
315
316 char *
317 mdoc_type2a(enum mdoc_type type)
318 {
319 switch (type) {
320 case (MDOC_ROOT):
321 return("root");
322 case (MDOC_BLOCK):
323 return("block");
324 case (MDOC_HEAD):
325 return("block-head");
326 case (MDOC_BODY):
327 return("block-body");
328 case (MDOC_TAIL):
329 return("block-tail");
330 case (MDOC_ELEM):
331 return("elem");
332 case (MDOC_TEXT):
333 return("text");
334 default:
335 break;
336 }
337
338 abort();
339 /* NOTREACHED */
340 }
341
342
343 const char *
344 mdoc_arch2a(enum mdoc_arch arch)
345 {
346
347 switch (arch) {
348 case (ARCH_alpha):
349 return("Alpha");
350 case (ARCH_amd64):
351 return("AMD64");
352 case (ARCH_amiga):
353 return("Amiga");
354 case (ARCH_arc):
355 return("ARC");
356 case (ARCH_arm):
357 return("ARM");
358 case (ARCH_armish):
359 return("ARMISH");
360 case (ARCH_aviion):
361 return("AViion");
362 case (ARCH_hp300):
363 return("HP300");
364 case (ARCH_hppa):
365 return("HPPA");
366 case (ARCH_hppa64):
367 return("HPPA64");
368 case (ARCH_i386):
369 return("i386");
370 case (ARCH_landisk):
371 return("LANDISK");
372 case (ARCH_luna88k):
373 return("Luna88k");
374 case (ARCH_mac68k):
375 return("Mac68k");
376 case (ARCH_macppc):
377 return("MacPPC");
378 case (ARCH_mvme68k):
379 return("MVME68k");
380 case (ARCH_mvme88k):
381 return("MVME88k");
382 case (ARCH_mvmeppc):
383 return("MVMEPPC");
384 case (ARCH_pmax):
385 return("PMAX");
386 case (ARCH_sgi):
387 return("SGI");
388 case (ARCH_socppc):
389 return("SOCPPC");
390 case (ARCH_sparc):
391 return("SPARC");
392 case (ARCH_sparc64):
393 return("SPARC64");
394 case (ARCH_sun3):
395 return("Sun3");
396 case (ARCH_vax):
397 return("VAX");
398 case (ARCH_zaurus):
399 return("Zaurus");
400 case (ARCH_DEFAULT):
401 return(NULL);
402 default:
403 break;
404 }
405
406 abort();
407 /* NOTREACHED */
408 }
409
410
411 const char *
412 mdoc_vol2a(enum mdoc_vol vol)
413 {
414
415 switch (vol) {
416 case (VOL_AMD):
417 return("OpenBSD Ancestral Manual Documents");
418 case (VOL_IND):
419 return("OpenBSD Manual Master Index");
420 case (VOL_KM):
421 return("OpenBSD Kernel Manual");
422 case (VOL_LOCAL):
423 return("OpenBSD Local Manual");
424 case (VOL_PRM):
425 return("OpenBSD Programmer's Manual");
426 case (VOL_PS1):
427 return("OpenBSD Programmer's Supplementary Documents");
428 case (VOL_SMM):
429 return("OpenBSD System Manager's Manual");
430 case (VOL_URM):
431 return("OpenBSD Reference Manual");
432 case (VOL_USD):
433 return("OpenBSD User's Supplementary Documents");
434 case (VOL_DEFAULT):
435 return(NULL);
436 default:
437 break;
438 }
439
440 abort();
441 /* NOTREACHED */
442 }
443
444
445 const char *
446 mdoc_msec2a(enum mdoc_msec msec)
447 {
448
449 switch (msec) {
450 case(MSEC_1):
451 return("1");
452 case(MSEC_2):
453 return("2");
454 case(MSEC_3):
455 return("3");
456 case(MSEC_3f):
457 return("3f");
458 case(MSEC_3p):
459 return("3p");
460 case(MSEC_4):
461 return("4");
462 case(MSEC_5):
463 return("5");
464 case(MSEC_6):
465 return("6");
466 case(MSEC_7):
467 return("7");
468 case(MSEC_8):
469 return("8");
470 case(MSEC_9):
471 return("9");
472 case(MSEC_X11):
473 return("X11");
474 case(MSEC_X11R6):
475 return("X11R6");
476 case(MSEC_local):
477 return("local");
478 case(MSEC_n):
479 return("n");
480 case(MSEC_unass):
481 /* FALLTHROUGH */
482 case(MSEC_draft):
483 return("draft");
484 case(MSEC_paper):
485 return("paper");
486 case(MSEC_DEFAULT):
487 return(NULL);
488 default:
489 break;
490 }
491
492 abort();
493 /* NOTREACHED */
494 }
495