]> git.cameronkatri.com Git - mandoc.git/blob - strings.c
termact.c -> term.c
[mandoc.git] / strings.c
1 /* $Id: strings.c,v 1.13 2009/02/21 21:00:06 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 #ifndef __OpenBSD__
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 char *pp;
128
129 (void)memset(&tm, 0, sizeof(struct tm));
130
131 if (xstrcmp(p, "$Mdocdate: February 21 2009 $"))
132 return(time(NULL));
133 if ((pp = strptime(p, "$Mdocdate: February 21 2009 $", &tm)) && 0 == *pp)
134 return(mktime(&tm));
135 /* XXX - this matches "June 1999", which is wrong. */
136 if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
137 return(mktime(&tm));
138 if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
139 return(mktime(&tm));
140
141 return(0);
142 }
143
144
145 enum mdoc_msec
146 mdoc_atomsec(const char *p)
147 {
148
149 if (0 == strcmp(p, "1"))
150 return(MSEC_1);
151 else if (0 == strcmp(p, "2"))
152 return(MSEC_2);
153 else if (0 == strcmp(p, "3"))
154 return(MSEC_3);
155 else if (0 == strcmp(p, "3f"))
156 return(MSEC_3f);
157 else if (0 == strcmp(p, "3p"))
158 return(MSEC_3p);
159 else if (0 == strcmp(p, "4"))
160 return(MSEC_4);
161 else if (0 == strcmp(p, "5"))
162 return(MSEC_5);
163 else if (0 == strcmp(p, "6"))
164 return(MSEC_6);
165 else if (0 == strcmp(p, "7"))
166 return(MSEC_7);
167 else if (0 == strcmp(p, "8"))
168 return(MSEC_8);
169 else if (0 == strcmp(p, "9"))
170 return(MSEC_9);
171 else if (0 == strcmp(p, "X11"))
172 return(MSEC_X11);
173 else if (0 == strcmp(p, "X11R6"))
174 return(MSEC_X11R6);
175 else if (0 == strcmp(p, "local"))
176 return(MSEC_local);
177 else if (0 == strcmp(p, "n"))
178 return(MSEC_n);
179 else if (0 == strcmp(p, "unass"))
180 return(MSEC_unass);
181 else if (0 == strcmp(p, "draft"))
182 return(MSEC_draft);
183 else if (0 == strcmp(p, "paper"))
184 return(MSEC_paper);
185
186 return(MSEC_DEFAULT);
187 }
188
189
190 enum mdoc_vol
191 mdoc_atovol(const char *p)
192 {
193
194 if (0 == strcmp(p, "AMD"))
195 return(VOL_AMD);
196 else if (0 == strcmp(p, "IND"))
197 return(VOL_IND);
198 else if (0 == strcmp(p, "KM"))
199 return(VOL_KM);
200 else if (0 == strcmp(p, "LOCAL"))
201 return(VOL_LOCAL);
202 else if (0 == strcmp(p, "PRM"))
203 return(VOL_PRM);
204 else if (0 == strcmp(p, "PS1"))
205 return(VOL_PS1);
206 else if (0 == strcmp(p, "SMM"))
207 return(VOL_SMM);
208 else if (0 == strcmp(p, "URM"))
209 return(VOL_URM);
210 else if (0 == strcmp(p, "USD"))
211 return(VOL_USD);
212
213 return(VOL_DEFAULT);
214 }
215
216
217 enum mdoc_arch
218 mdoc_atoarch(const char *p)
219 {
220
221 if (0 == strcmp(p, "alpha"))
222 return(ARCH_alpha);
223 else if (0 == strcmp(p, "amd64"))
224 return(ARCH_amd64);
225 else if (0 == strcmp(p, "amiga"))
226 return(ARCH_amiga);
227 else if (0 == strcmp(p, "arc"))
228 return(ARCH_arc);
229 else if (0 == strcmp(p, "arm"))
230 return(ARCH_arm);
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 }
337
338
339 const char *
340 mdoc_arch2a(enum mdoc_arch arch)
341 {
342
343 switch (arch) {
344 case (ARCH_alpha):
345 return("Alpha");
346 case (ARCH_amd64):
347 return("AMD64");
348 case (ARCH_amiga):
349 return("Amiga");
350 case (ARCH_arc):
351 return("ARC");
352 case (ARCH_arm):
353 return("ARM");
354 case (ARCH_armish):
355 return("ARMISH");
356 case (ARCH_aviion):
357 return("AViion");
358 case (ARCH_hp300):
359 return("HP300");
360 case (ARCH_hppa):
361 return("HPPA");
362 case (ARCH_hppa64):
363 return("HPPA64");
364 case (ARCH_i386):
365 return("i386");
366 case (ARCH_landisk):
367 return("LANDISK");
368 case (ARCH_luna88k):
369 return("Luna88k");
370 case (ARCH_mac68k):
371 return("Mac68k");
372 case (ARCH_macppc):
373 return("MacPPC");
374 case (ARCH_mvme68k):
375 return("MVME68k");
376 case (ARCH_mvme88k):
377 return("MVME88k");
378 case (ARCH_mvmeppc):
379 return("MVMEPPC");
380 case (ARCH_pmax):
381 return("PMAX");
382 case (ARCH_sgi):
383 return("SGI");
384 case (ARCH_socppc):
385 return("SOCPPC");
386 case (ARCH_sparc):
387 return("SPARC");
388 case (ARCH_sparc64):
389 return("SPARC64");
390 case (ARCH_sun3):
391 return("Sun3");
392 case (ARCH_vax):
393 return("VAX");
394 case (ARCH_zaurus):
395 return("Zaurus");
396 case (ARCH_DEFAULT):
397 return(NULL);
398 default:
399 break;
400 }
401
402 abort();
403 /* NOTREACHED */
404 }
405
406
407 const char *
408 mdoc_vol2a(enum mdoc_vol vol)
409 {
410
411 switch (vol) {
412 case (VOL_AMD):
413 return("OpenBSD Ancestral Manual Documents");
414 case (VOL_IND):
415 return("OpenBSD Manual Master Index");
416 case (VOL_KM):
417 return("OpenBSD Kernel Manual");
418 case (VOL_LOCAL):
419 return("OpenBSD Local Manual");
420 case (VOL_PRM):
421 return("OpenBSD Programmer's Manual");
422 case (VOL_PS1):
423 return("OpenBSD Programmer's Supplementary Documents");
424 case (VOL_SMM):
425 return("OpenBSD System Manager's Manual");
426 case (VOL_URM):
427 return("OpenBSD Reference Manual");
428 case (VOL_USD):
429 return("OpenBSD User's Supplementary Documents");
430 case (VOL_DEFAULT):
431 return(NULL);
432 default:
433 break;
434 }
435
436 abort();
437 /* NOTREACHED */
438 }
439
440
441 const char *
442 mdoc_msec2a(enum mdoc_msec msec)
443 {
444
445 switch (msec) {
446 case(MSEC_1):
447 return("1");
448 case(MSEC_2):
449 return("2");
450 case(MSEC_3):
451 return("3");
452 case(MSEC_3f):
453 return("3f");
454 case(MSEC_3p):
455 return("3p");
456 case(MSEC_4):
457 return("4");
458 case(MSEC_5):
459 return("5");
460 case(MSEC_6):
461 return("6");
462 case(MSEC_7):
463 return("7");
464 case(MSEC_8):
465 return("8");
466 case(MSEC_9):
467 return("9");
468 case(MSEC_X11):
469 return("X11");
470 case(MSEC_X11R6):
471 return("X11R6");
472 case(MSEC_local):
473 return("local");
474 case(MSEC_n):
475 return("n");
476 case(MSEC_unass):
477 /* FALLTHROUGH */
478 case(MSEC_draft):
479 return("draft");
480 case(MSEC_paper):
481 return("paper");
482 case(MSEC_DEFAULT):
483 return(NULL);
484 default:
485 break;
486 }
487
488 abort();
489 /* NOTREACHED */
490 }
491