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