]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_strings.c
All special characters sync'd with groff, both -Thtml and -Tascii.
[mandoc.git] / mdoc_strings.c
1 /* $Id: mdoc_strings.c,v 1.10 2009/08/20 13:32:09 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
24 #include "libmdoc.h"
25
26 /* FIXME: this file is poorly named. */
27
28 struct mdoc_secname {
29 const char *name; /* Name of section. */
30 enum mdoc_sec sec; /* Corresponding section. */
31 };
32
33 #define SECNAME_MAX (20)
34
35 static const struct mdoc_secname secnames[SECNAME_MAX] = {
36 { "NAME", SEC_NAME },
37 { "LIBRARY", SEC_LIBRARY },
38 { "SYNOPSIS", SEC_SYNOPSIS },
39 { "DESCRIPTION", SEC_DESCRIPTION },
40 { "IMPLEMENTATION NOTES", SEC_IMPLEMENTATION },
41 { "EXIT STATUS", SEC_EXIT_STATUS },
42 { "RETURN VALUES", SEC_RETURN_VALUES },
43 { "ENVIRONMENT", SEC_ENVIRONMENT },
44 { "FILES", SEC_FILES },
45 { "EXAMPLES", SEC_EXAMPLES },
46 { "DIAGNOSTICS", SEC_DIAGNOSTICS },
47 { "COMPATIBILITY", SEC_COMPATIBILITY },
48 { "ERRORS", SEC_ERRORS },
49 { "SEE ALSO", SEC_SEE_ALSO },
50 { "STANDARDS", SEC_STANDARDS },
51 { "HISTORY", SEC_HISTORY },
52 { "AUTHORS", SEC_AUTHORS },
53 { "CAVEATS", SEC_CAVEATS },
54 { "BUGS", SEC_BUGS },
55 { "SECURITY CONSIDERATIONS", SEC_SECURITY }
56 };
57
58 #ifdef __linux__
59 extern char *strptime(const char *, const char *, struct tm *);
60 #endif
61
62
63 int
64 mdoc_iscdelim(char p)
65 {
66
67 switch (p) {
68 case('|'):
69 /* FALLTHROUGH */
70 case('.'):
71 /* FALLTHROUGH */
72 case(','):
73 /* FALLTHROUGH */
74 case(';'):
75 /* FALLTHROUGH */
76 case(':'):
77 /* FALLTHROUGH */
78 case('?'):
79 /* FALLTHROUGH */
80 case('!'):
81 /* FALLTHROUGH */
82 case('('):
83 /* FALLTHROUGH */
84 case(')'):
85 /* FALLTHROUGH */
86 case('['):
87 /* FALLTHROUGH */
88 case(']'):
89 /* FALLTHROUGH */
90 case('{'):
91 /* FALLTHROUGH */
92 case('}'):
93 return(1);
94 default:
95 break;
96 }
97
98 return(0);
99 }
100
101
102 int
103 mdoc_isdelim(const char *p)
104 {
105
106 if (0 == *p)
107 return(0);
108 if (0 != *(p + 1))
109 return(0);
110 return(mdoc_iscdelim(*p));
111 }
112
113
114 enum mdoc_sec
115 mdoc_atosec(const char *p)
116 {
117 int i;
118
119 for (i = 0; i < SECNAME_MAX; i++)
120 if (0 == strcmp(p, secnames[i].name))
121 return(secnames[i].sec);
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 bzero(&tm, sizeof(struct tm));
134
135 if (0 == strcmp(p, "$" "Mdocdate$"))
136 return(time(NULL));
137 if ((pp = strptime(p, "$" "Mdocdate: %b %d %Y $", &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 /* FIXME: move this into an editable .in file. */
150 size_t
151 mdoc_macro2len(int macro)
152 {
153
154 switch (macro) {
155 case(MDOC_Ad):
156 return(12);
157 case(MDOC_Ao):
158 return(12);
159 case(MDOC_An):
160 return(12);
161 case(MDOC_Aq):
162 return(12);
163 case(MDOC_Ar):
164 return(12);
165 case(MDOC_Bo):
166 return(12);
167 case(MDOC_Bq):
168 return(12);
169 case(MDOC_Cd):
170 return(12);
171 case(MDOC_Cm):
172 return(10);
173 case(MDOC_Do):
174 return(10);
175 case(MDOC_Dq):
176 return(12);
177 case(MDOC_Dv):
178 return(12);
179 case(MDOC_Eo):
180 return(12);
181 case(MDOC_Em):
182 return(10);
183 case(MDOC_Er):
184 return(17);
185 case(MDOC_Ev):
186 return(15);
187 case(MDOC_Fa):
188 return(12);
189 case(MDOC_Fl):
190 return(10);
191 case(MDOC_Fo):
192 return(16);
193 case(MDOC_Fn):
194 return(16);
195 case(MDOC_Ic):
196 return(10);
197 case(MDOC_Li):
198 return(16);
199 case(MDOC_Ms):
200 return(6);
201 case(MDOC_Nm):
202 return(10);
203 case(MDOC_No):
204 return(12);
205 case(MDOC_Oo):
206 return(10);
207 case(MDOC_Op):
208 return(14);
209 case(MDOC_Pa):
210 return(32);
211 case(MDOC_Pf):
212 return(12);
213 case(MDOC_Po):
214 return(12);
215 case(MDOC_Pq):
216 return(12);
217 case(MDOC_Ql):
218 return(16);
219 case(MDOC_Qo):
220 return(12);
221 case(MDOC_So):
222 return(12);
223 case(MDOC_Sq):
224 return(12);
225 case(MDOC_Sy):
226 return(6);
227 case(MDOC_Sx):
228 return(16);
229 case(MDOC_Tn):
230 return(10);
231 case(MDOC_Va):
232 return(12);
233 case(MDOC_Vt):
234 return(12);
235 case(MDOC_Xr):
236 return(10);
237 default:
238 break;
239 };
240 return(0);
241 }