]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_strings.c
0d63dac1d7d982b4c0eeb52a1b8d8a31c92f4547
[mandoc.git] / mdoc_strings.c
1 /* $Id: mdoc_strings.c,v 1.12 2009/10/27 08:26:12 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 above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #include <sys/types.h>
18
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <time.h>
24
25 #include "libmdoc.h"
26
27 /* FIXME: this file is poorly named. */
28
29 struct mdoc_secname {
30 const char *name; /* Name of section. */
31 enum mdoc_sec sec; /* Corresponding section. */
32 };
33
34 #define SECNAME_MAX (20)
35
36 static const struct mdoc_secname secnames[SECNAME_MAX] = {
37 { "NAME", SEC_NAME },
38 { "LIBRARY", SEC_LIBRARY },
39 { "SYNOPSIS", SEC_SYNOPSIS },
40 { "DESCRIPTION", SEC_DESCRIPTION },
41 { "IMPLEMENTATION NOTES", SEC_IMPLEMENTATION },
42 { "EXIT STATUS", SEC_EXIT_STATUS },
43 { "RETURN VALUES", SEC_RETURN_VALUES },
44 { "ENVIRONMENT", SEC_ENVIRONMENT },
45 { "FILES", SEC_FILES },
46 { "EXAMPLES", SEC_EXAMPLES },
47 { "DIAGNOSTICS", SEC_DIAGNOSTICS },
48 { "COMPATIBILITY", SEC_COMPATIBILITY },
49 { "ERRORS", SEC_ERRORS },
50 { "SEE ALSO", SEC_SEE_ALSO },
51 { "STANDARDS", SEC_STANDARDS },
52 { "HISTORY", SEC_HISTORY },
53 { "AUTHORS", SEC_AUTHORS },
54 { "CAVEATS", SEC_CAVEATS },
55 { "BUGS", SEC_BUGS },
56 { "SECURITY CONSIDERATIONS", SEC_SECURITY }
57 };
58
59 #ifdef __linux__
60 extern char *strptime(const char *, const char *, struct tm *);
61 #endif
62
63
64 int
65 mdoc_iscdelim(char p)
66 {
67
68 switch (p) {
69 case('|'):
70 /* FALLTHROUGH */
71 case('.'):
72 /* FALLTHROUGH */
73 case(','):
74 /* FALLTHROUGH */
75 case(';'):
76 /* FALLTHROUGH */
77 case(':'):
78 /* FALLTHROUGH */
79 case('?'):
80 /* FALLTHROUGH */
81 case('!'):
82 /* FALLTHROUGH */
83 case('('):
84 /* FALLTHROUGH */
85 case(')'):
86 /* FALLTHROUGH */
87 case('['):
88 /* FALLTHROUGH */
89 case(']'):
90 /* FALLTHROUGH */
91 case('{'):
92 /* FALLTHROUGH */
93 case('}'):
94 return(1);
95 default:
96 break;
97 }
98
99 return(0);
100 }
101
102
103 int
104 mdoc_isdelim(const char *p)
105 {
106
107 if (0 == *p)
108 return(0);
109 if (0 != *(p + 1))
110 return(0);
111 return(mdoc_iscdelim(*p));
112 }
113
114
115 enum mdoc_sec
116 mdoc_atosec(const char *p)
117 {
118 int i;
119
120 for (i = 0; i < SECNAME_MAX; i++)
121 if (0 == strcmp(p, secnames[i].name))
122 return(secnames[i].sec);
123
124 return(SEC_CUSTOM);
125 }
126
127
128 time_t
129 mdoc_atotime(const char *p)
130 {
131 struct tm tm;
132 char *pp;
133
134 memset(&tm, 0, sizeof(struct tm));
135
136 if (0 == strcmp(p, "$" "Mdocdate$"))
137 return(time(NULL));
138 if ((pp = strptime(p, "$" "Mdocdate: %b %d %Y $", &tm)) && 0 == *pp)
139 return(mktime(&tm));
140 /* XXX - this matches "June 1999", which is wrong. */
141 if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
142 return(mktime(&tm));
143 if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
144 return(mktime(&tm));
145
146 return(0);
147 }
148
149
150 /* FIXME: move this into an editable .in file. */
151 size_t
152 mdoc_macro2len(int macro)
153 {
154
155 switch (macro) {
156 case(MDOC_Ad):
157 return(12);
158 case(MDOC_Ao):
159 return(12);
160 case(MDOC_An):
161 return(12);
162 case(MDOC_Aq):
163 return(12);
164 case(MDOC_Ar):
165 return(12);
166 case(MDOC_Bo):
167 return(12);
168 case(MDOC_Bq):
169 return(12);
170 case(MDOC_Cd):
171 return(12);
172 case(MDOC_Cm):
173 return(10);
174 case(MDOC_Do):
175 return(10);
176 case(MDOC_Dq):
177 return(12);
178 case(MDOC_Dv):
179 return(12);
180 case(MDOC_Eo):
181 return(12);
182 case(MDOC_Em):
183 return(10);
184 case(MDOC_Er):
185 return(17);
186 case(MDOC_Ev):
187 return(15);
188 case(MDOC_Fa):
189 return(12);
190 case(MDOC_Fl):
191 return(10);
192 case(MDOC_Fo):
193 return(16);
194 case(MDOC_Fn):
195 return(16);
196 case(MDOC_Ic):
197 return(10);
198 case(MDOC_Li):
199 return(16);
200 case(MDOC_Ms):
201 return(6);
202 case(MDOC_Nm):
203 return(10);
204 case(MDOC_No):
205 return(12);
206 case(MDOC_Oo):
207 return(10);
208 case(MDOC_Op):
209 return(14);
210 case(MDOC_Pa):
211 return(32);
212 case(MDOC_Pf):
213 return(12);
214 case(MDOC_Po):
215 return(12);
216 case(MDOC_Pq):
217 return(12);
218 case(MDOC_Ql):
219 return(16);
220 case(MDOC_Qo):
221 return(12);
222 case(MDOC_So):
223 return(12);
224 case(MDOC_Sq):
225 return(12);
226 case(MDOC_Sy):
227 return(6);
228 case(MDOC_Sx):
229 return(16);
230 case(MDOC_Tn):
231 return(10);
232 case(MDOC_Va):
233 return(12);
234 case(MDOC_Vt):
235 return(12);
236 case(MDOC_Xr):
237 return(10);
238 default:
239 break;
240 };
241 return(0);
242 }