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