]> git.cameronkatri.com Git - mandoc.git/blob - argv.c
Finished initial parse sequence.
[mandoc.git] / argv.c
1 /* $Id: argv.c,v 1.7 2009/01/05 16:11:14 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 <err.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "private.h"
27
28
29 static int lookup(int, const char *);
30 static int parse(struct mdoc *, int,
31 struct mdoc_arg *, int *, char *);
32 static int postparse(struct mdoc *, int,
33 const struct mdoc_arg *, int);
34
35
36 int
37 mdoc_args(struct mdoc *mdoc, int tok, int *pos, char *buf, int fl, char **v)
38 {
39 int i;
40
41 if (0 == buf[*pos])
42 return(ARGS_EOLN);
43
44 if ('\"' == buf[*pos] && ! (fl & ARGS_QUOTED))
45 if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_QUOTED))
46 return(ARGS_ERROR);
47
48 if ('-' == buf[*pos])
49 if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_ARGLIKE))
50 return(ARGS_ERROR);
51
52 if ((fl & ARGS_DELIM) && mdoc_iscdelim(buf[*pos])) {
53 for (i = *pos; buf[i]; ) {
54 if ( ! mdoc_iscdelim(buf[i]))
55 break;
56 i++;
57 if (0 == buf[i] || ! isspace(buf[i]))
58 break;
59 i++;
60 while (buf[i] && isspace(buf[i]))
61 i++;
62 }
63 if (0 == buf[i]) {
64 *v = &buf[*pos];
65 return(ARGS_PUNCT);
66 }
67 }
68
69 /*
70 * Parse routine for non-quoted string.
71 */
72
73 if ('\"' != buf[*pos]) {
74 *v = &buf[*pos];
75
76 while (buf[*pos] && ! isspace(buf[*pos]))
77 (*pos)++;
78
79 if (0 == buf[*pos])
80 return(ARGS_WORD);
81
82 buf[(*pos)++] = 0;
83 if (0 == buf[*pos])
84 return(ARGS_WORD);
85
86 while (buf[*pos] && isspace(buf[*pos]))
87 (*pos)++;
88
89 if (buf[*pos])
90 return(ARGS_WORD);
91
92 if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_WS_EOLN))
93 return(ARGS_ERROR);
94
95 return(ARGS_WORD);
96 }
97
98 /*
99 * If we're a quoted string (and quoted strings are allowed),
100 * then parse ahead to the next quote. If none's found, it's an
101 * error. After, parse to the next word.
102 */
103
104 *v = &buf[++(*pos)];
105
106 while (buf[*pos] && '\"' != buf[*pos])
107 (*pos)++;
108
109 if (0 == buf[*pos]) {
110 (void)mdoc_err(mdoc, tok, *pos, ERR_SYNTAX_UNQUOTE);
111 return(ARGS_ERROR);
112 }
113
114 buf[(*pos)++] = 0;
115 if (0 == buf[*pos])
116 return(ARGS_WORD);
117
118 while (buf[*pos] && isspace(buf[*pos]))
119 (*pos)++;
120
121 if (buf[*pos])
122 return(ARGS_WORD);
123
124 if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_WS_EOLN))
125 return(ARGS_ERROR);
126
127 return(ARGS_WORD);
128 }
129
130
131 static int
132 lookup(int tok, const char *argv)
133 {
134
135 switch (tok) {
136 case (MDOC_Bf):
137 if (xstrcmp(argv, "emphasis"))
138 return(MDOC_Emphasis);
139 else if (xstrcmp(argv, "literal"))
140 return(MDOC_Literal);
141 else if (xstrcmp(argv, "symbolic"))
142 return(MDOC_Symbolic);
143 break;
144
145 case (MDOC_An):
146 if (xstrcmp(argv, "split"))
147 return(MDOC_Split);
148 else if (xstrcmp(argv, "nosplit"))
149 return(MDOC_Nosplit);
150 break;
151
152 case (MDOC_Bd):
153 if (xstrcmp(argv, "ragged"))
154 return(MDOC_Ragged);
155 else if (xstrcmp(argv, "unfilled"))
156 return(MDOC_Unfilled);
157 else if (xstrcmp(argv, "literal"))
158 return(MDOC_Literal);
159 else if (xstrcmp(argv, "file"))
160 return(MDOC_File);
161 else if (xstrcmp(argv, "offset"))
162 return(MDOC_Offset);
163 break;
164
165 case (MDOC_Bk):
166 if (xstrcmp(argv, "words"))
167 return(MDOC_Words);
168 break;
169
170 case (MDOC_Bl):
171 if (xstrcmp(argv, "bullet"))
172 return(MDOC_Bullet);
173 else if (xstrcmp(argv, "dash"))
174 return(MDOC_Dash);
175 else if (xstrcmp(argv, "hyphen"))
176 return(MDOC_Hyphen);
177 else if (xstrcmp(argv, "item"))
178 return(MDOC_Item);
179 else if (xstrcmp(argv, "enum"))
180 return(MDOC_Enum);
181 else if (xstrcmp(argv, "tag"))
182 return(MDOC_Tag);
183 else if (xstrcmp(argv, "diag"))
184 return(MDOC_Diag);
185 else if (xstrcmp(argv, "hang"))
186 return(MDOC_Hang);
187 else if (xstrcmp(argv, "ohang"))
188 return(MDOC_Ohang);
189 else if (xstrcmp(argv, "inset"))
190 return(MDOC_Inset);
191 else if (xstrcmp(argv, "column"))
192 return(MDOC_Column);
193 else if (xstrcmp(argv, "width"))
194 return(MDOC_Width);
195 else if (xstrcmp(argv, "offset"))
196 return(MDOC_Offset);
197 else if (xstrcmp(argv, "compact"))
198 return(MDOC_Compact);
199 break;
200
201 case (MDOC_Rv):
202 /* FALLTHROUGH */
203 case (MDOC_Ex):
204 if (xstrcmp(argv, "std"))
205 return(MDOC_Std);
206 break;
207
208 case (MDOC_St):
209 if (xstrcmp(argv, "p1003.1-88"))
210 return(MDOC_p1003_1_88);
211 else if (xstrcmp(argv, "p1003.1-90"))
212 return(MDOC_p1003_1_90);
213 else if (xstrcmp(argv, "p1003.1-96"))
214 return(MDOC_p1003_1_96);
215 else if (xstrcmp(argv, "p1003.1-2001"))
216 return(MDOC_p1003_1_2001);
217 else if (xstrcmp(argv, "p1003.1-2004"))
218 return(MDOC_p1003_1_2004);
219 else if (xstrcmp(argv, "p1003.1"))
220 return(MDOC_p1003_1);
221 else if (xstrcmp(argv, "p1003.1b"))
222 return(MDOC_p1003_1b);
223 else if (xstrcmp(argv, "p1003.1b-93"))
224 return(MDOC_p1003_1b_93);
225 else if (xstrcmp(argv, "p1003.1c-95"))
226 return(MDOC_p1003_1c_95);
227 else if (xstrcmp(argv, "p1003.1g-2000"))
228 return(MDOC_p1003_1g_2000);
229 else if (xstrcmp(argv, "p1003.2-92"))
230 return(MDOC_p1003_2_92);
231 else if (xstrcmp(argv, "p1003.2-95"))
232 return(MDOC_p1387_2_95);
233 else if (xstrcmp(argv, "p1003.2"))
234 return(MDOC_p1003_2);
235 else if (xstrcmp(argv, "p1387.2-95"))
236 return(MDOC_p1387_2);
237 else if (xstrcmp(argv, "isoC-90"))
238 return(MDOC_isoC_90);
239 else if (xstrcmp(argv, "isoC-amd1"))
240 return(MDOC_isoC_amd1);
241 else if (xstrcmp(argv, "isoC-tcor1"))
242 return(MDOC_isoC_tcor1);
243 else if (xstrcmp(argv, "isoC-tcor2"))
244 return(MDOC_isoC_tcor2);
245 else if (xstrcmp(argv, "isoC-99"))
246 return(MDOC_isoC_99);
247 else if (xstrcmp(argv, "ansiC"))
248 return(MDOC_ansiC);
249 else if (xstrcmp(argv, "ansiC-89"))
250 return(MDOC_ansiC_89);
251 else if (xstrcmp(argv, "ansiC-99"))
252 return(MDOC_ansiC_99);
253 else if (xstrcmp(argv, "ieee754"))
254 return(MDOC_ieee754);
255 else if (xstrcmp(argv, "iso8802-3"))
256 return(MDOC_iso8802_3);
257 else if (xstrcmp(argv, "xpg3"))
258 return(MDOC_xpg3);
259 else if (xstrcmp(argv, "xpg4"))
260 return(MDOC_xpg4);
261 else if (xstrcmp(argv, "xpg4.2"))
262 return(MDOC_xpg4_2);
263 else if (xstrcmp(argv, "xpg4.3"))
264 return(MDOC_xpg4_3);
265 else if (xstrcmp(argv, "xbd5"))
266 return(MDOC_xbd5);
267 else if (xstrcmp(argv, "xcu5"))
268 return(MDOC_xcu5);
269 else if (xstrcmp(argv, "xsh5"))
270 return(MDOC_xsh5);
271 else if (xstrcmp(argv, "xns5"))
272 return(MDOC_xns5);
273 else if (xstrcmp(argv, "xns5.2d2.0"))
274 return(MDOC_xns5_2d2_0);
275 else if (xstrcmp(argv, "xcurses4.2"))
276 return(MDOC_xcurses4_2);
277 else if (xstrcmp(argv, "susv2"))
278 return(MDOC_susv2);
279 else if (xstrcmp(argv, "susv3"))
280 return(MDOC_susv3);
281 else if (xstrcmp(argv, "svid4"))
282 return(MDOC_svid4);
283 break;
284
285 default:
286 abort();
287 /* NOTREACHED */
288 }
289
290 return(MDOC_ARG_MAX);
291 }
292
293
294 static int
295 postparse(struct mdoc *mdoc, int tok, const struct mdoc_arg *v, int pos)
296 {
297
298 switch (v->arg) {
299 case (MDOC_Offset):
300 assert(v->value);
301 assert(v->value[0]);
302 if (xstrcmp(v->value[0], "left"))
303 break;
304 if (xstrcmp(v->value[0], "right"))
305 break;
306 if (xstrcmp(v->value[0], "center"))
307 break;
308 if (xstrcmp(v->value[0], "indent"))
309 break;
310 if (xstrcmp(v->value[0], "indent-two"))
311 break;
312 return(mdoc_err(mdoc, tok, pos, ERR_SYNTAX_ARGBAD));
313 default:
314 break;
315 }
316
317 return(1);
318 }
319
320
321 static int
322 parse(struct mdoc *mdoc, int tok,
323 struct mdoc_arg *v, int *pos, char *buf)
324 {
325 char *p;
326 int c, ppos, i;
327
328 ppos = *pos;
329
330 switch (v->arg) {
331 case(MDOC_Std):
332 /* FALLTHROUGH */
333 case(MDOC_Width):
334 /* FALLTHROUGH */
335 case(MDOC_Offset):
336 /*
337 * This has a single value for an argument.
338 */
339 c = mdoc_args(mdoc, tok, pos, buf, ARGS_QUOTED, &p);
340 if (ARGS_ERROR == c)
341 return(0);
342 else if (ARGS_EOLN == c)
343 return(mdoc_err(mdoc, tok, ppos, ERR_SYNTAX_ARGVAL));
344
345 v->sz = 1;
346 v->value = xcalloc(1, sizeof(char *));
347 v->value[0] = p;
348 break;
349
350 case(MDOC_Column):
351 /*
352 * This has several value for a single argument. We
353 * pre-allocate a pointer array and don't let it exceed
354 * this size.
355 */
356 v->sz = 0;
357 v->value = xcalloc(MDOC_LINEARG_MAX, sizeof(char *));
358 for (i = 0; i < MDOC_LINEARG_MAX; i++) {
359 c = mdoc_args(mdoc, tok, pos, buf, ARGS_QUOTED, &p);
360 if (ARGS_ERROR == c) {
361 free(v->value);
362 return(0);
363 } else if (ARGS_EOLN == c)
364 break;
365 v->value[i] = p;
366 }
367 if (0 == i) {
368 free(v->value);
369 return(mdoc_err(mdoc, tok, ppos, ERR_SYNTAX_ARGVAL));
370 } else if (MDOC_LINEARG_MAX == i)
371 return(mdoc_err(mdoc, tok, ppos, ERR_SYNTAX_ARGMANY));
372
373 v->sz = i;
374 break;
375
376 default:
377 v->sz = 0;
378 v->value = NULL;
379 break;
380 }
381
382 return(1);
383 }
384
385
386 int
387 mdoc_argv(struct mdoc *mdoc, int tok,
388 struct mdoc_arg *v, int *pos, char *buf)
389 {
390 int i, ppos;
391 char *argv;
392
393 (void)memset(v, 0, sizeof(struct mdoc_arg));
394
395 if (0 == buf[*pos])
396 return(ARGV_EOLN);
397
398 assert( ! isspace(buf[*pos]));
399
400 if ('-' != buf[*pos])
401 return(ARGV_WORD);
402
403 i = *pos;
404 argv = &buf[++(*pos)];
405
406 while (buf[*pos] && ! isspace(buf[*pos]))
407 (*pos)++;
408
409 if (buf[*pos])
410 buf[(*pos)++] = 0;
411
412 if (MDOC_ARG_MAX == (v->arg = lookup(tok, argv))) {
413 (void)mdoc_err(mdoc, tok, i, ERR_SYNTAX_ARG);
414 return(ARGV_ERROR);
415 }
416
417 while (buf[*pos] && isspace(buf[*pos]))
418 (*pos)++;
419
420 /* FIXME: whitespace if no value. */
421
422 ppos = *pos;
423 if ( ! parse(mdoc, tok, v, pos, buf))
424 return(ARGV_ERROR);
425 if ( ! postparse(mdoc, tok, v, ppos))
426 return(ARGV_ERROR);
427
428 return(ARGV_ARG);
429 }
430
431
432 void
433 mdoc_argv_free(int sz, struct mdoc_arg *arg)
434 {
435 int i;
436
437 for (i = 0; i < sz; i++) {
438 if (0 == arg[i].sz) {
439 assert(NULL == arg[i].value);
440 continue;
441 }
442 assert(arg[i].value);
443 free(arg[i].value);
444 }
445 }
446