Annotation of mandoc/strings.c, Revision 1.29
1.29 ! kristaps 1: /* $Id: strings.c,v 1.28 2009/03/13 07:46:10 kristaps Exp $ */
1.1 kristaps 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: */
1.27 kristaps 19: #include <sys/types.h>
20:
1.1 kristaps 21: #include <assert.h>
22: #include <ctype.h>
23: #include <stdlib.h>
24: #include <stdio.h>
1.2 kristaps 25: #include <string.h>
1.1 kristaps 26:
1.26 kristaps 27: #include "private.h"
28:
1.14 kristaps 29: /*
1.26 kristaps 30: * Various string-literal operations: converting scalars to and from
31: * strings, etc.
1.14 kristaps 32: */
33:
1.26 kristaps 34: struct mdoc_secname {
35: const char *name;
36: int flag;
37: #define MSECNAME_META (1 << 0)
38: };
39:
40: /* Section names corresponding to mdoc_sec. */
41:
42: static const struct mdoc_secname secnames[] = {
43: { "PROLOGUE", MSECNAME_META },
44: { "BODY", MSECNAME_META },
45: { "NAME", 0 },
46: { "LIBRARY", 0 },
47: { "SYNOPSIS", 0 },
48: { "DESCRIPTION", 0 },
49: { "IMPLEMENTATION NOTES", 0 },
50: { "RETURN VALUES", 0 },
51: { "ENVIRONMENT", 0 },
52: { "FILES", 0 },
53: { "EXAMPLES", 0 },
54: { "DIAGNOSTICS", 0 },
55: { "COMPATIBILITY", 0 },
56: { "ERRORS", 0 },
57: { "SEE ALSO", 0 },
58: { "STANDARDS", 0 },
59: { "HISTORY", 0 },
60: { "AUTHORS", 0 },
61: { "CAVEATS", 0 },
62: { "BUGS", 0 },
63: { NULL, 0 }
64: };
1.1 kristaps 65:
1.4 kristaps 66: #ifdef __linux__
67: extern char *strptime(const char *, const char *, struct tm *);
68: #endif
69:
1.16 kristaps 70:
71: size_t
72: mdoc_isescape(const char *p)
73: {
74: size_t c;
75:
76: if ('\\' != *p++)
77: return(0);
78:
79: switch (*p) {
80: case ('\\'):
81: /* FALLTHROUGH */
82: case ('\''):
83: /* FALLTHROUGH */
84: case ('`'):
85: /* FALLTHROUGH */
1.28 kristaps 86: case ('q'):
87: /* FALLTHROUGH */
1.16 kristaps 88: case ('-'):
89: /* FALLTHROUGH */
90: case (' '):
91: /* FALLTHROUGH */
1.29 ! kristaps 92: case ('|'):
! 93: /* FALLTHROUGH */
1.17 kristaps 94: case ('&'):
95: /* FALLTHROUGH */
1.16 kristaps 96: case ('.'):
97: /* FALLTHROUGH */
98: case ('e'):
99: return(2);
1.21 kristaps 100: case ('*'):
1.25 kristaps 101: if (0 == *++p || ! isgraph((u_char)*p))
1.21 kristaps 102: return(0);
103: switch (*p) {
104: case ('('):
1.25 kristaps 105: if (0 == *++p || ! isgraph((u_char)*p))
1.21 kristaps 106: return(0);
107: return(4);
1.24 kristaps 108: case ('['):
109: for (c = 3, p++; *p && ']' != *p; p++, c++)
1.25 kristaps 110: if ( ! isgraph((u_char)*p))
1.24 kristaps 111: break;
112: return(*p == ']' ? c : 0);
1.21 kristaps 113: default:
114: break;
115: }
116: return(3);
1.16 kristaps 117: case ('('):
1.25 kristaps 118: if (0 == *++p || ! isgraph((u_char)*p))
1.16 kristaps 119: return(0);
1.25 kristaps 120: if (0 == *++p || ! isgraph((u_char)*p))
1.16 kristaps 121: return(0);
122: return(4);
123: case ('['):
124: break;
125: default:
126: return(0);
127: }
128:
129: for (c = 3, p++; *p && ']' != *p; p++, c++)
1.25 kristaps 130: if ( ! isgraph((u_char)*p))
1.16 kristaps 131: break;
132:
133: return(*p == ']' ? c : 0);
134: }
135:
136:
1.1 kristaps 137: int
1.3 kristaps 138: mdoc_iscdelim(char p)
1.1 kristaps 139: {
140:
1.3 kristaps 141: switch (p) {
1.1 kristaps 142: case('.'):
143: /* FALLTHROUGH */
144: case(','):
145: /* FALLTHROUGH */
146: case(';'):
147: /* FALLTHROUGH */
148: case(':'):
149: /* FALLTHROUGH */
150: case('?'):
151: /* FALLTHROUGH */
152: case('!'):
153: /* FALLTHROUGH */
154: case('('):
155: /* FALLTHROUGH */
156: case(')'):
157: /* FALLTHROUGH */
158: case('['):
159: /* FALLTHROUGH */
160: case(']'):
161: /* FALLTHROUGH */
1.7 kristaps 162: case('{'):
163: /* FALLTHROUGH */
1.1 kristaps 164: case('}'):
165: return(1);
166: default:
167: break;
168: }
169:
170: return(0);
171: }
172:
1.2 kristaps 173:
1.3 kristaps 174: int
175: mdoc_isdelim(const char *p)
176: {
177:
178: if (0 == *p)
179: return(0);
180: if (0 != *(p + 1))
181: return(0);
182: return(mdoc_iscdelim(*p));
183: }
184:
185:
1.2 kristaps 186: enum mdoc_sec
1.9 kristaps 187: mdoc_atosec(const char *p)
1.2 kristaps 188: {
1.26 kristaps 189: const struct mdoc_secname *n;
190: int i;
1.2 kristaps 191:
1.26 kristaps 192: for (i = 0, n = secnames; n->name; n++, i++)
193: if ( ! (n->flag & MSECNAME_META))
194: if (xstrcmp(p, n->name))
195: return((enum mdoc_sec)i);
1.2 kristaps 196:
197: return(SEC_CUSTOM);
198: }
199:
200:
201: time_t
202: mdoc_atotime(const char *p)
203: {
204: struct tm tm;
1.11 kristaps 205: char *pp;
1.2 kristaps 206:
1.5 kristaps 207: (void)memset(&tm, 0, sizeof(struct tm));
208:
1.11 kristaps 209: if (xstrcmp(p, "$Mdocdate$"))
210: return(time(NULL));
211: if ((pp = strptime(p, "$Mdocdate: %b %d %Y $", &tm)) && 0 == *pp)
1.2 kristaps 212: return(mktime(&tm));
1.11 kristaps 213: /* XXX - this matches "June 1999", which is wrong. */
214: if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
215: return(mktime(&tm));
216: if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
1.2 kristaps 217: return(mktime(&tm));
218:
219: return(0);
220: }
221:
222:
1.20 kristaps 223: size_t
224: mdoc_macro2len(int macro)
225: {
226:
227: switch (macro) {
228: case(MDOC_Ad):
229: return(12);
230: case(MDOC_Ao):
231: return(12);
232: case(MDOC_An):
233: return(12);
234: case(MDOC_Aq):
235: return(12);
236: case(MDOC_Ar):
237: return(12);
238: case(MDOC_Bo):
239: return(12);
240: case(MDOC_Bq):
241: return(12);
242: case(MDOC_Cd):
243: return(12);
244: case(MDOC_Cm):
245: return(10);
246: case(MDOC_Do):
247: return(10);
248: case(MDOC_Dq):
249: return(12);
250: case(MDOC_Dv):
251: return(12);
252: case(MDOC_Eo):
253: return(12);
254: case(MDOC_Em):
255: return(10);
256: case(MDOC_Er):
257: return(12);
258: case(MDOC_Ev):
259: return(15);
260: case(MDOC_Fa):
261: return(12);
262: case(MDOC_Fl):
263: return(10);
264: case(MDOC_Fo):
265: return(16);
266: case(MDOC_Fn):
267: return(16);
268: case(MDOC_Ic):
269: return(10);
270: case(MDOC_Li):
271: return(16);
272: case(MDOC_Ms):
273: return(6);
274: case(MDOC_Nm):
275: return(10);
276: case(MDOC_No):
277: return(12);
278: case(MDOC_Oo):
279: return(10);
280: case(MDOC_Op):
281: return(14);
282: case(MDOC_Pa):
283: return(32);
284: case(MDOC_Pf):
285: return(12);
286: case(MDOC_Po):
287: return(12);
288: case(MDOC_Pq):
289: return(12);
290: case(MDOC_Ql):
291: return(16);
292: case(MDOC_Qo):
293: return(12);
294: case(MDOC_So):
295: return(12);
296: case(MDOC_Sq):
297: return(12);
298: case(MDOC_Sy):
299: return(6);
300: case(MDOC_Sx):
301: return(16);
302: case(MDOC_Tn):
303: return(10);
304: case(MDOC_Va):
305: return(12);
306: case(MDOC_Vt):
307: return(12);
308: case(MDOC_Xr):
309: return(10);
310: default:
311: break;
312: };
313: return(0);
314: }
CVSweb