]> git.cameronkatri.com Git - mandoc.git/blob - argv.c
9cd5350307cb90026731725c37f4550acde1bb42
[mandoc.git] / argv.c
1 /* $Id: argv.c,v 1.40 2009/03/08 12:40:27 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 * Routines to parse arguments of macros. Arguments follow the syntax
30 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
31 * arguments, multiple arguments per value, no-value arguments, etc.
32 *
33 * There's no limit to the number or arguments that may be allocated.
34 */
35
36 #define ARGS_QUOTED (1 << 0)
37 #define ARGS_DELIM (1 << 1)
38 #define ARGS_TABSEP (1 << 2)
39 #define ARGS_ARGVLIKE (1 << 3)
40
41 #define ARGV_NONE (1 << 0)
42 #define ARGV_SINGLE (1 << 1)
43 #define ARGV_MULTI (1 << 2)
44 #define ARGV_OPT_SINGLE (1 << 3)
45
46 enum mwarn {
47 WQUOTPARM,
48 WARGVPARM,
49 WCOLEMPTY,
50 WTAILWS
51 };
52
53 enum merr {
54 EQUOTTERM,
55 EARGVAL
56 };
57
58 static int argv_a2arg(int, const char *);
59 static int args(struct mdoc *, int, int *,
60 char *, int, char **);
61 static int argv(struct mdoc *, int, int,
62 struct mdoc_argv *, int *, char *);
63 static int argv_single(struct mdoc *, int,
64 struct mdoc_argv *, int *, char *);
65 static int argv_opt_single(struct mdoc *, int,
66 struct mdoc_argv *, int *, char *);
67 static int argv_multi(struct mdoc *, int,
68 struct mdoc_argv *, int *, char *);
69 static int pwarn(struct mdoc *, int, int, enum mwarn);
70 static int perr(struct mdoc *, int, int, enum merr);
71
72 /* Per-argument flags. */
73
74 static int mdoc_argvflags[MDOC_ARG_MAX] = {
75 ARGV_NONE, /* MDOC_Split */
76 ARGV_NONE, /* MDOC_Nosplit */
77 ARGV_NONE, /* MDOC_Ragged */
78 ARGV_NONE, /* MDOC_Unfilled */
79 ARGV_NONE, /* MDOC_Literal */
80 ARGV_NONE, /* MDOC_File */
81 ARGV_SINGLE, /* MDOC_Offset */
82 ARGV_NONE, /* MDOC_Bullet */
83 ARGV_NONE, /* MDOC_Dash */
84 ARGV_NONE, /* MDOC_Hyphen */
85 ARGV_NONE, /* MDOC_Item */
86 ARGV_NONE, /* MDOC_Enum */
87 ARGV_NONE, /* MDOC_Tag */
88 ARGV_NONE, /* MDOC_Diag */
89 ARGV_NONE, /* MDOC_Hang */
90 ARGV_NONE, /* MDOC_Ohang */
91 ARGV_NONE, /* MDOC_Inset */
92 ARGV_MULTI, /* MDOC_Column */
93 ARGV_SINGLE, /* MDOC_Width */
94 ARGV_NONE, /* MDOC_Compact */
95 ARGV_SINGLE, /* MDOC_Std */
96 ARGV_NONE, /* MDOC_Filled */
97 ARGV_NONE, /* MDOC_Words */
98 ARGV_NONE, /* MDOC_Emphasis */
99 ARGV_NONE /* MDOC_Symbolic */
100 };
101
102 static int mdoc_argflags[MDOC_MAX] = {
103 0, /* \" */
104 0, /* Dd */
105 0, /* Dt */
106 0, /* Os */
107 0, /* Sh */
108 0, /* Ss */
109 ARGS_DELIM, /* Pp */
110 ARGS_DELIM, /* D1 */
111 ARGS_DELIM, /* Dl */
112 0, /* Bd */
113 0, /* Ed */
114 0, /* Bl */
115 0, /* El */
116 0, /* It */
117 ARGS_DELIM, /* Ad */
118 ARGS_DELIM, /* An */
119 ARGS_DELIM, /* Ar */
120 ARGS_QUOTED, /* Cd */
121 ARGS_DELIM, /* Cm */
122 ARGS_DELIM, /* Dv */
123 ARGS_DELIM, /* Er */
124 ARGS_DELIM, /* Ev */
125 0, /* Ex */
126 ARGS_DELIM | ARGS_QUOTED, /* Fa */
127 0, /* Fd */
128 ARGS_DELIM, /* Fl */
129 ARGS_DELIM | ARGS_QUOTED, /* Fn */
130 ARGS_DELIM | ARGS_QUOTED, /* Ft */
131 ARGS_DELIM, /* Ic */
132 0, /* In */
133 ARGS_DELIM, /* Li */
134 0, /* Nd */
135 ARGS_DELIM, /* Nm */
136 ARGS_DELIM, /* Op */
137 0, /* Ot */
138 ARGS_DELIM, /* Pa */
139 0, /* Rv */
140 ARGS_DELIM | ARGS_ARGVLIKE, /* St */
141 ARGS_DELIM, /* Va */
142 ARGS_DELIM, /* Vt */
143 ARGS_DELIM, /* Xr */
144 ARGS_QUOTED, /* %A */
145 ARGS_QUOTED, /* %B */
146 ARGS_QUOTED, /* %D */
147 ARGS_QUOTED, /* %I */
148 ARGS_QUOTED, /* %J */
149 ARGS_QUOTED, /* %N */
150 ARGS_QUOTED, /* %O */
151 ARGS_QUOTED, /* %P */
152 ARGS_QUOTED, /* %R */
153 ARGS_QUOTED, /* %T */
154 ARGS_QUOTED, /* %V */
155 ARGS_DELIM, /* Ac */
156 0, /* Ao */
157 ARGS_DELIM, /* Aq */
158 ARGS_DELIM, /* At */
159 ARGS_DELIM, /* Bc */
160 0, /* Bf */
161 0, /* Bo */
162 ARGS_DELIM, /* Bq */
163 ARGS_DELIM, /* Bsx */
164 ARGS_DELIM, /* Bx */
165 0, /* Db */
166 ARGS_DELIM, /* Dc */
167 0, /* Do */
168 ARGS_DELIM, /* Dq */
169 ARGS_DELIM, /* Ec */
170 0, /* Ef */
171 ARGS_DELIM, /* Em */
172 0, /* Eo */
173 ARGS_DELIM, /* Fx */
174 ARGS_DELIM, /* Ms */
175 ARGS_DELIM, /* No */
176 ARGS_DELIM, /* Ns */
177 ARGS_DELIM, /* Nx */
178 ARGS_DELIM, /* Ox */
179 ARGS_DELIM, /* Pc */
180 ARGS_DELIM, /* Pf */
181 0, /* Po */
182 ARGS_DELIM, /* Pq */
183 ARGS_DELIM, /* Qc */
184 ARGS_DELIM, /* Ql */
185 0, /* Qo */
186 ARGS_DELIM, /* Qq */
187 0, /* Re */
188 0, /* Rs */
189 ARGS_DELIM, /* Sc */
190 0, /* So */
191 ARGS_DELIM, /* Sq */
192 0, /* Sm */
193 ARGS_DELIM, /* Sx */
194 ARGS_DELIM, /* Sy */
195 ARGS_DELIM, /* Tn */
196 ARGS_DELIM, /* Ux */
197 ARGS_DELIM, /* Xc */
198 0, /* Xo */
199 0, /* Fo */
200 0, /* Fc */
201 0, /* Oo */
202 ARGS_DELIM, /* Oc */
203 0, /* Bk */
204 0, /* Ek */
205 0, /* Bt */
206 0, /* Hf */
207 0, /* Fr */
208 0, /* Ud */
209 0, /* Lb */
210 };
211
212
213 /*
214 * Parse an argument from line text. This comes in the form of -key
215 * [value0...], which may either have a single mandatory value, at least
216 * one mandatory value, an optional single value, or no value.
217 */
218 int
219 mdoc_argv(struct mdoc *mdoc, int line, int tok,
220 struct mdoc_arg **v, int *pos, char *buf)
221 {
222 int i;
223 char *p;
224 struct mdoc_argv tmp;
225 struct mdoc_arg *arg;
226
227 if (0 == buf[*pos])
228 return(ARGV_EOLN);
229
230 assert( ! isspace((u_char)buf[*pos]));
231
232 if ('-' != buf[*pos])
233 return(ARGV_WORD);
234
235 i = *pos;
236 p = &buf[++(*pos)];
237
238 assert(*pos > 0);
239
240 /* LINTED */
241 while (buf[*pos]) {
242 if (isspace((u_char)buf[*pos]))
243 if ('\\' != buf[*pos - 1])
244 break;
245 (*pos)++;
246 }
247
248 if (buf[*pos])
249 buf[(*pos)++] = 0;
250
251 (void)memset(&tmp, 0, sizeof(struct mdoc_argv));
252 tmp.line = line;
253 tmp.pos = *pos;
254
255 /*
256 * We now parse out the per-macro arguments. XXX - this can be
257 * made much cleaner using per-argument tables. See argv_a2arg
258 * for details.
259 */
260
261 if (MDOC_ARG_MAX == (tmp.arg = argv_a2arg(tok, p))) {
262 if ( ! pwarn(mdoc, line, i, WARGVPARM))
263 return(ARGV_ERROR);
264 return(ARGV_WORD);
265 }
266
267 while (buf[*pos] && isspace((u_char)buf[*pos]))
268 (*pos)++;
269
270 /* FIXME: whitespace if no value. */
271
272 if ( ! argv(mdoc, tok, line, &tmp, pos, buf))
273 return(ARGV_ERROR);
274
275 if (NULL == (arg = *v)) {
276 *v = xcalloc(1, sizeof(struct mdoc_arg));
277 arg = *v;
278 }
279
280 arg->argc++;
281 arg->argv = xrealloc(arg->argv, arg->argc *
282 sizeof(struct mdoc_argv));
283
284 (void)memcpy(&arg->argv[(int)arg->argc - 1],
285 &tmp, sizeof(struct mdoc_argv));
286
287 return(ARGV_ARG);
288 }
289
290
291 void
292 mdoc_argv_free(struct mdoc_arg *p)
293 {
294 int i, j;
295
296 if (p->refcnt) {
297 --(p->refcnt);
298 if (p->refcnt)
299 return;
300 }
301
302 assert(p->argc);
303
304 /* LINTED */
305 for (i = 0; i < (int)p->argc; i++) {
306 if (0 == p->argv[i].sz)
307 continue;
308 /* LINTED */
309 for (j = 0; j < (int)p->argv[i].sz; j++)
310 free(p->argv[i].value[j]);
311
312 free(p->argv[i].value);
313 }
314
315 free(p->argv);
316 free(p);
317 }
318
319
320
321 static int
322 perr(struct mdoc *mdoc, int line, int pos, enum merr code)
323 {
324 char *p;
325
326 p = NULL;
327
328 switch (code) {
329 case (EQUOTTERM):
330 p = "unterminated quoted parameter";
331 break;
332 case (EARGVAL):
333 p = "argument requires a value";
334 break;
335 }
336
337 assert(p);
338 return(mdoc_perr(mdoc, line, pos, p));
339 }
340
341
342 static int
343 pwarn(struct mdoc *mdoc, int line, int pos, enum mwarn code)
344 {
345 char *p;
346 int c;
347
348 p = NULL;
349 c = WARN_SYNTAX;
350
351 switch (code) {
352 case (WQUOTPARM):
353 p = "unexpected quoted parameter";
354 break;
355 case (WARGVPARM):
356 p = "argument-like parameter";
357 break;
358 case (WCOLEMPTY):
359 p = "last list column is empty";
360 c = WARN_COMPAT;
361 break;
362 case (WTAILWS):
363 p = "trailing whitespace";
364 c = WARN_COMPAT;
365 break;
366 }
367
368 assert(p);
369 return(mdoc_pwarn(mdoc, line, pos, c, p));
370 }
371
372
373 int
374 mdoc_args(struct mdoc *mdoc, int line,
375 int *pos, char *buf, int tok, char **v)
376 {
377 int fl, c, i;
378 struct mdoc_node *n;
379
380 fl = (0 == tok) ? 0 : mdoc_argflags[tok];
381
382 /*
383 * Override per-macro argument flags with context-specific ones.
384 * As of now, this is only valid for `It' depending on its list
385 * context.
386 */
387
388 switch (tok) {
389 case (MDOC_It):
390 for (n = mdoc->last; n; n = n->parent)
391 if (MDOC_BLOCK == n->type && MDOC_Bl == n->tok)
392 break;
393
394 assert(n);
395 c = (int)(n->args ? n->args->argc : 0);
396 assert(c > 0);
397
398 /*
399 * Using `Bl -column' adds ARGS_TABSEP to the arguments
400 * and invalidates ARGS_DELIM. Using `Bl -diag' allows
401 * for quoted arguments.
402 */
403
404 /* LINTED */
405 for (i = 0; i < c; i++) {
406 switch (n->args->argv[i].arg) {
407 case (MDOC_Column):
408 fl |= ARGS_TABSEP;
409 fl &= ~ARGS_DELIM;
410 i = c;
411 break;
412 case (MDOC_Diag):
413 fl |= ARGS_QUOTED;
414 i = c;
415 break;
416 default:
417 break;
418 }
419 }
420 break;
421 default:
422 break;
423 }
424
425 /* Continue parsing the arguments themselves... */
426
427 return(args(mdoc, line, pos, buf, fl, v));
428 }
429
430
431 static int
432 args(struct mdoc *mdoc, int line,
433 int *pos, char *buf, int fl, char **v)
434 {
435 int i;
436 char *p, *pp;
437
438 assert(*pos > 0);
439
440 if (0 == buf[*pos])
441 return(ARGS_EOLN);
442
443 if ('\"' == buf[*pos] && ! (fl & ARGS_QUOTED))
444 if ( ! pwarn(mdoc, line, *pos, WQUOTPARM))
445 return(ARGS_ERROR);
446
447 if ( ! (fl & ARGS_ARGVLIKE) && '-' == buf[*pos])
448 if ( ! pwarn(mdoc, line, *pos, WARGVPARM))
449 return(ARGS_ERROR);
450
451 /*
452 * If the first character is a delimiter and we're to look for
453 * delimited strings, then pass down the buffer seeing if it
454 * follows the pattern of [[::delim::][ ]+]+.
455 */
456
457 if ((fl & ARGS_DELIM) && mdoc_iscdelim(buf[*pos])) {
458 for (i = *pos; buf[i]; ) {
459 if ( ! mdoc_iscdelim(buf[i]))
460 break;
461 i++;
462 /* There must be at least one space... */
463 if (0 == buf[i] || ! isspace((u_char)buf[i]))
464 break;
465 i++;
466 while (buf[i] && isspace((u_char)buf[i]))
467 i++;
468 }
469 if (0 == buf[i]) {
470 *v = &buf[*pos];
471 return(ARGS_PUNCT);
472 }
473 }
474
475 /* First parse non-quoted strings. */
476
477 if ('\"' != buf[*pos] || ! (ARGS_QUOTED & fl)) {
478 *v = &buf[*pos];
479
480 /*
481 * Thar be dragons here! If we're tab-separated, search
482 * ahead for either a tab or the `Ta' macro. If a tab
483 * is detected, it mustn't be escaped; if a `Ta' is
484 * detected, it must be space-buffered before and after.
485 * If either of these hold true, then prune out the
486 * extra spaces and call it an argument.
487 */
488
489 if (ARGS_TABSEP & fl) {
490 /* Scan ahead to unescaped tab. */
491
492 for (p = *v; ; p++) {
493 if (NULL == (p = strchr(p, '\t')))
494 break;
495 if (p == *v)
496 break;
497 if ('\\' != *(p - 1))
498 break;
499 }
500
501 /* Scan ahead to unescaped `Ta'. */
502
503 for (pp = *v; ; pp++) {
504 if (NULL == (pp = strstr(pp, "Ta")))
505 break;
506 if (pp > *v && ' ' != *(pp - 1))
507 continue;
508 if (' ' == *(pp + 2) || 0 == *(pp + 2))
509 break;
510 }
511
512 /* Choose delimiter tab/Ta. */
513
514 if (p && pp)
515 p = (p < pp ? p : pp);
516 else if ( ! p && pp)
517 p = pp;
518
519 /* Strip delimiter's preceding whitespace. */
520
521 if (p && p > *v) {
522 pp = p - 1;
523 while (pp > *v && ' ' == *pp)
524 pp--;
525 if (pp == *v && ' ' == *pp)
526 *pp = 0;
527 else if (' ' == *pp)
528 *(pp + 1) = 0;
529 }
530
531 /* ...in- and proceding whitespace. */
532
533 if (p && ('\t' != *p)) {
534 *p++ = 0;
535 *p++ = 0;
536 } else if (p)
537 *p++ = 0;
538
539 if (p) {
540 while (' ' == *p)
541 p++;
542 if (0 != *p)
543 *(p - 1) = 0;
544 *pos += (int)(p - *v);
545 }
546
547 if (p && 0 == *p)
548 if ( ! pwarn(mdoc, line, *pos, WCOLEMPTY))
549 return(0);
550 if (p && 0 == *p && p > *v && ' ' == *(p - 1))
551 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
552 return(0);
553
554 if (p)
555 return(ARGS_WORD);
556
557 /* Configure the eoln case, too. */
558
559 p = strchr(*v, 0);
560 assert(p);
561
562 if (p > *v && ' ' == *(p - 1))
563 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
564 return(0);
565 *pos += (int)(p - *v);
566
567 return(ARGS_WORD);
568 }
569
570 /* Do non-tabsep look-ahead here. */
571
572 if ( ! (ARGS_TABSEP & fl))
573 while (buf[*pos]) {
574 if (isspace((u_char)buf[*pos]))
575 if ('\\' != buf[*pos - 1])
576 break;
577 (*pos)++;
578 }
579
580 if (0 == buf[*pos])
581 return(ARGS_WORD);
582
583 buf[(*pos)++] = 0;
584
585 if (0 == buf[*pos])
586 return(ARGS_WORD);
587
588 if ( ! (ARGS_TABSEP & fl))
589 while (buf[*pos] && isspace((u_char)buf[*pos]))
590 (*pos)++;
591
592 if (buf[*pos])
593 return(ARGS_WORD);
594
595 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
596 return(ARGS_ERROR);
597
598 return(ARGS_WORD);
599 }
600
601 /*
602 * If we're a quoted string (and quoted strings are allowed),
603 * then parse ahead to the next quote. If none's found, it's an
604 * error. After, parse to the next word.
605 */
606
607 *v = &buf[++(*pos)];
608
609 while (buf[*pos] && '\"' != buf[*pos])
610 (*pos)++;
611
612 if (0 == buf[*pos]) {
613 (void)perr(mdoc, line, *pos, EQUOTTERM);
614 return(ARGS_ERROR);
615 }
616
617 buf[(*pos)++] = 0;
618 if (0 == buf[*pos])
619 return(ARGS_QWORD);
620
621 while (buf[*pos] && isspace((u_char)buf[*pos]))
622 (*pos)++;
623
624 if (buf[*pos])
625 return(ARGS_QWORD);
626
627 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
628 return(ARGS_ERROR);
629
630 return(ARGS_QWORD);
631 }
632
633
634 static int
635 argv_a2arg(int tok, const char *argv)
636 {
637
638 /*
639 * Parse an argument identifier from its text. XXX - this
640 * should really be table-driven to clarify the code.
641 *
642 * If you add an argument to the list, make sure that you
643 * register it here with its one or more macros!
644 */
645
646 switch (tok) {
647 case (MDOC_An):
648 if (xstrcmp(argv, "split"))
649 return(MDOC_Split);
650 else if (xstrcmp(argv, "nosplit"))
651 return(MDOC_Nosplit);
652 break;
653
654 case (MDOC_Bd):
655 if (xstrcmp(argv, "ragged"))
656 return(MDOC_Ragged);
657 else if (xstrcmp(argv, "unfilled"))
658 return(MDOC_Unfilled);
659 else if (xstrcmp(argv, "filled"))
660 return(MDOC_Filled);
661 else if (xstrcmp(argv, "literal"))
662 return(MDOC_Literal);
663 else if (xstrcmp(argv, "file"))
664 return(MDOC_File);
665 else if (xstrcmp(argv, "offset"))
666 return(MDOC_Offset);
667 break;
668
669 case (MDOC_Bf):
670 if (xstrcmp(argv, "emphasis"))
671 return(MDOC_Emphasis);
672 else if (xstrcmp(argv, "literal"))
673 return(MDOC_Literal);
674 else if (xstrcmp(argv, "symbolic"))
675 return(MDOC_Symbolic);
676 break;
677
678 case (MDOC_Bk):
679 if (xstrcmp(argv, "words"))
680 return(MDOC_Words);
681 break;
682
683 case (MDOC_Bl):
684 if (xstrcmp(argv, "bullet"))
685 return(MDOC_Bullet);
686 else if (xstrcmp(argv, "dash"))
687 return(MDOC_Dash);
688 else if (xstrcmp(argv, "hyphen"))
689 return(MDOC_Hyphen);
690 else if (xstrcmp(argv, "item"))
691 return(MDOC_Item);
692 else if (xstrcmp(argv, "enum"))
693 return(MDOC_Enum);
694 else if (xstrcmp(argv, "tag"))
695 return(MDOC_Tag);
696 else if (xstrcmp(argv, "diag"))
697 return(MDOC_Diag);
698 else if (xstrcmp(argv, "hang"))
699 return(MDOC_Hang);
700 else if (xstrcmp(argv, "ohang"))
701 return(MDOC_Ohang);
702 else if (xstrcmp(argv, "inset"))
703 return(MDOC_Inset);
704 else if (xstrcmp(argv, "column"))
705 return(MDOC_Column);
706 else if (xstrcmp(argv, "width"))
707 return(MDOC_Width);
708 else if (xstrcmp(argv, "offset"))
709 return(MDOC_Offset);
710 else if (xstrcmp(argv, "compact"))
711 return(MDOC_Compact);
712 break;
713
714 case (MDOC_Rv):
715 /* FALLTHROUGH */
716 case (MDOC_Ex):
717 if (xstrcmp(argv, "std"))
718 return(MDOC_Std);
719 break;
720 default:
721 break;
722 }
723
724 return(MDOC_ARG_MAX);
725 }
726
727
728 static int
729 argv_multi(struct mdoc *mdoc, int line,
730 struct mdoc_argv *v, int *pos, char *buf)
731 {
732 int c, ppos;
733 char *p;
734
735 ppos = *pos;
736
737 for (v->sz = 0; ; v->sz++) {
738 if ('-' == buf[*pos])
739 break;
740 c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p);
741 if (ARGS_ERROR == c)
742 return(0);
743 else if (ARGS_EOLN == c)
744 break;
745
746 if (0 == v->sz % 5)
747 v->value = xrealloc(v->value,
748 (v->sz + 5) * sizeof(char *));
749
750 v->value[(int)v->sz] = xstrdup(p);
751 }
752
753 if (v->sz)
754 return(1);
755
756 return(perr(mdoc, line, ppos, EARGVAL));
757 }
758
759
760 static int
761 argv_opt_single(struct mdoc *mdoc, int line,
762 struct mdoc_argv *v, int *pos, char *buf)
763 {
764 int c;
765 char *p;
766
767 if ('-' == buf[*pos])
768 return(1);
769
770 c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p);
771 if (ARGS_ERROR == c)
772 return(0);
773 if (ARGS_EOLN == c)
774 return(1);
775
776 v->sz = 1;
777 v->value = xcalloc(1, sizeof(char *));
778 v->value[0] = xstrdup(p);
779 return(1);
780 }
781
782
783 /*
784 * Parse a single, mandatory value from the stream.
785 */
786 static int
787 argv_single(struct mdoc *mdoc, int line,
788 struct mdoc_argv *v, int *pos, char *buf)
789 {
790 int c, ppos;
791 char *p;
792
793 ppos = *pos;
794
795 c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p);
796 if (ARGS_ERROR == c)
797 return(0);
798 if (ARGS_EOLN == c)
799 return(perr(mdoc, line, ppos, EARGVAL));
800
801 v->sz = 1;
802 v->value = xcalloc(1, sizeof(char *));
803 v->value[0] = xstrdup(p);
804 return(1);
805 }
806
807
808 /*
809 * Determine rules for parsing arguments. Arguments can either accept
810 * no parameters, an optional single parameter, one parameter, or
811 * multiple parameters.
812 */
813 static int
814 argv(struct mdoc *mdoc, int tok, int line,
815 struct mdoc_argv *v, int *pos, char *buf)
816 {
817 int fl;
818
819 v->sz = 0;
820 v->value = NULL;
821 fl = mdoc_argvflags[v->arg];
822
823 /*
824 * Override the default per-argument value.
825 */
826
827 switch (tok) {
828 case (MDOC_Ex):
829 fl = ARGV_OPT_SINGLE;
830 break;
831 default:
832 break;
833 }
834
835 switch (fl) {
836 case (ARGV_SINGLE):
837 return(argv_single(mdoc, line, v, pos, buf));
838 case (ARGV_MULTI):
839 return(argv_multi(mdoc, line, v, pos, buf));
840 case (ARGV_OPT_SINGLE):
841 return(argv_opt_single(mdoc, line, v, pos, buf));
842 default:
843 /* ARGV_NONE */
844 break;
845 }
846
847 return(1);
848 }