]> git.cameronkatri.com Git - mandoc.git/blob - argv.c
Fixed mdoc_phrase escape handling.
[mandoc.git] / argv.c
1 /* $Id: argv.c,v 1.52 2009/03/16 22:19:19 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 <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 };
226
227
228 /*
229 * Parse an argument from line text. This comes in the form of -key
230 * [value0...], which may either have a single mandatory value, at least
231 * one mandatory value, an optional single value, or no value.
232 */
233 int
234 mdoc_argv(struct mdoc *mdoc, int line, int tok,
235 struct mdoc_arg **v, int *pos, char *buf)
236 {
237 int i;
238 char *p, sv;
239 struct mdoc_argv tmp;
240 struct mdoc_arg *arg;
241
242 if (0 == buf[*pos])
243 return(ARGV_EOLN);
244
245 assert(' ' != buf[*pos]);
246
247 if ('-' != buf[*pos] || ARGS_ARGVLIKE & mdoc_argflags[tok])
248 return(ARGV_WORD);
249
250 /* Parse through to the first unescaped space. */
251
252 i = *pos;
253 p = &buf[++(*pos)];
254
255 assert(*pos > 0);
256
257 /* LINTED */
258 while (buf[*pos]) {
259 if (' ' == buf[*pos])
260 if ('\\' != buf[*pos - 1])
261 break;
262 (*pos)++;
263 }
264
265 /* XXX - save zeroed byte, if not an argument. */
266
267 sv = 0;
268 if (buf[*pos]) {
269 sv = buf[*pos];
270 buf[(*pos)++] = 0;
271 }
272
273 (void)memset(&tmp, 0, sizeof(struct mdoc_argv));
274 tmp.line = line;
275 tmp.pos = *pos;
276
277 /* See if our token accepts the argument. */
278
279 if (MDOC_ARG_MAX == (tmp.arg = argv_a2arg(tok, p))) {
280 /* XXX - restore saved zeroed byte. */
281 if (sv)
282 buf[*pos - 1] = sv;
283 if ( ! pwarn(mdoc, line, i, WARGVPARM))
284 return(ARGV_ERROR);
285 return(ARGV_WORD);
286 }
287
288 while (buf[*pos] && ' ' == buf[*pos])
289 (*pos)++;
290
291 if ( ! argv(mdoc, line, &tmp, pos, buf))
292 return(ARGV_ERROR);
293
294 if (NULL == (arg = *v)) {
295 *v = xcalloc(1, sizeof(struct mdoc_arg));
296 arg = *v;
297 }
298
299 arg->argc++;
300 arg->argv = xrealloc(arg->argv, arg->argc *
301 sizeof(struct mdoc_argv));
302
303 (void)memcpy(&arg->argv[(int)arg->argc - 1],
304 &tmp, sizeof(struct mdoc_argv));
305
306 return(ARGV_ARG);
307 }
308
309
310 void
311 mdoc_argv_free(struct mdoc_arg *p)
312 {
313 int i, j;
314
315 if (NULL == p)
316 return;
317
318 if (p->refcnt) {
319 --(p->refcnt);
320 if (p->refcnt)
321 return;
322 }
323
324 assert(p->argc);
325
326 /* LINTED */
327 for (i = 0; i < (int)p->argc; i++) {
328 if (0 == p->argv[i].sz)
329 continue;
330 /* LINTED */
331 for (j = 0; j < (int)p->argv[i].sz; j++)
332 free(p->argv[i].value[j]);
333
334 free(p->argv[i].value);
335 }
336
337 free(p->argv);
338 free(p);
339 }
340
341
342
343 static int
344 perr(struct mdoc *mdoc, int line, int pos, enum merr code)
345 {
346 char *p;
347
348 p = NULL;
349
350 switch (code) {
351 case (EQUOTTERM):
352 p = "unterminated quoted parameter";
353 break;
354 case (EARGVAL):
355 p = "argument requires a value";
356 break;
357 }
358
359 assert(p);
360 return(mdoc_perr(mdoc, line, pos, p));
361 }
362
363
364 static int
365 pwarn(struct mdoc *mdoc, int line, int pos, enum mwarn code)
366 {
367 char *p;
368 int c;
369
370 p = NULL;
371 c = WARN_SYNTAX;
372
373 switch (code) {
374 case (WQUOTPARM):
375 p = "unexpected quoted parameter";
376 break;
377 case (WARGVPARM):
378 p = "argument-like parameter";
379 break;
380 case (WCOLEMPTY):
381 p = "last list column is empty";
382 c = WARN_COMPAT;
383 break;
384 case (WTAILWS):
385 p = "trailing whitespace";
386 c = WARN_COMPAT;
387 break;
388 }
389
390 assert(p);
391 return(mdoc_pwarn(mdoc, line, pos, c, p));
392 }
393
394
395 int
396 mdoc_args(struct mdoc *mdoc, int line,
397 int *pos, char *buf, int tok, char **v)
398 {
399 int fl, c, i;
400 struct mdoc_node *n;
401
402 fl = (0 == tok) ? 0 : mdoc_argflags[tok];
403
404 /*
405 * Override per-macro argument flags with context-specific ones.
406 * As of now, this is only valid for `It' depending on its list
407 * context.
408 */
409
410 switch (tok) {
411 case (MDOC_It):
412 for (n = mdoc->last; n; n = n->parent)
413 if (MDOC_BLOCK == n->type && MDOC_Bl == n->tok)
414 break;
415
416 assert(n);
417 c = (int)(n->args ? n->args->argc : 0);
418 assert(c > 0);
419
420 /*
421 * Using `Bl -column' adds ARGS_TABSEP to the arguments
422 * and invalidates ARGS_DELIM. Using `Bl -diag' allows
423 * for quoted arguments.
424 */
425
426 /* LINTED */
427 for (i = 0; i < c; i++) {
428 switch (n->args->argv[i].arg) {
429 case (MDOC_Column):
430 fl |= ARGS_TABSEP;
431 fl &= ~ARGS_DELIM;
432 i = c;
433 break;
434 case (MDOC_Diag):
435 fl |= ARGS_QUOTED;
436 i = c;
437 break;
438 default:
439 break;
440 }
441 }
442 break;
443 default:
444 break;
445 }
446
447 return(args(mdoc, line, pos, buf, fl, v));
448 }
449
450
451 static int
452 args(struct mdoc *mdoc, int line,
453 int *pos, char *buf, int fl, char **v)
454 {
455 int i;
456 char *p, *pp;
457
458 assert(*pos > 0);
459
460 if (0 == buf[*pos])
461 return(ARGS_EOLN);
462
463 if ('\"' == buf[*pos] && ! (fl & ARGS_QUOTED))
464 if ( ! pwarn(mdoc, line, *pos, WQUOTPARM))
465 return(ARGS_ERROR);
466
467 if ( ! (fl & ARGS_ARGVLIKE) && '-' == buf[*pos])
468 if ( ! pwarn(mdoc, line, *pos, WARGVPARM))
469 return(ARGS_ERROR);
470
471 /*
472 * If the first character is a delimiter and we're to look for
473 * delimited strings, then pass down the buffer seeing if it
474 * follows the pattern of [[::delim::][ ]+]+.
475 */
476
477 if ((fl & ARGS_DELIM) && mdoc_iscdelim(buf[*pos])) {
478 for (i = *pos; buf[i]; ) {
479 if ( ! mdoc_iscdelim(buf[i]))
480 break;
481 i++;
482 /* There must be at least one space... */
483 if (0 == buf[i] || ' ' != buf[i])
484 break;
485 i++;
486 while (buf[i] && ' ' == buf[i])
487 i++;
488 }
489 if (0 == buf[i]) {
490 *v = &buf[*pos];
491 return(ARGS_PUNCT);
492 }
493 }
494
495 /* First parse non-quoted strings. */
496
497 if ('\"' != buf[*pos] || ! (ARGS_QUOTED & fl)) {
498 *v = &buf[*pos];
499
500 /*
501 * Thar be dragons here! If we're tab-separated, search
502 * ahead for either a tab or the `Ta' macro.
503 * If a `Ta' is detected, it must be space-buffered before and
504 * after. If either of these hold true, then prune out the
505 * extra spaces and call it an argument.
506 */
507
508 if (ARGS_TABSEP & fl) {
509 /* Scan ahead to unescaped tab. */
510
511 p = strchr(*v, '\t');
512
513 /* Scan ahead to unescaped `Ta'. */
514
515 for (pp = *v; ; pp++) {
516 if (NULL == (pp = strstr(pp, "Ta")))
517 break;
518 if (pp > *v && ' ' != *(pp - 1))
519 continue;
520 if (' ' == *(pp + 2) || 0 == *(pp + 2))
521 break;
522 }
523
524 /* Choose delimiter tab/Ta. */
525
526 if (p && pp)
527 p = (p < pp ? p : pp);
528 else if ( ! p && pp)
529 p = pp;
530
531 /* Strip delimiter's preceding whitespace. */
532
533 if (p && p > *v) {
534 pp = p - 1;
535 while (pp > *v && ' ' == *pp)
536 pp--;
537 if (pp == *v && ' ' == *pp)
538 *pp = 0;
539 else if (' ' == *pp)
540 *(pp + 1) = 0;
541 }
542
543 /* ...in- and proceding whitespace. */
544
545 if (p && ('\t' != *p)) {
546 *p++ = 0;
547 *p++ = 0;
548 } else if (p)
549 *p++ = 0;
550
551 if (p) {
552 while (' ' == *p)
553 p++;
554 if (0 != *p)
555 *(p - 1) = 0;
556 *pos += (int)(p - *v);
557 }
558
559 if (p && 0 == *p)
560 if ( ! pwarn(mdoc, line, *pos, WCOLEMPTY))
561 return(0);
562 if (p && 0 == *p && p > *v && ' ' == *(p - 1))
563 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
564 return(0);
565
566 if (p)
567 return(ARGS_PHRASE);
568
569 /* Configure the eoln case, too. */
570
571 p = strchr(*v, 0);
572 assert(p);
573
574 if (p > *v && ' ' == *(p - 1))
575 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
576 return(0);
577 *pos += (int)(p - *v);
578
579 return(ARGS_PHRASE);
580 }
581
582 /* Do non-tabsep look-ahead here. */
583
584 if ( ! (ARGS_TABSEP & fl))
585 while (buf[*pos]) {
586 if (' ' == buf[*pos])
587 if ('\\' != buf[*pos - 1])
588 break;
589 (*pos)++;
590 }
591
592 if (0 == buf[*pos])
593 return(ARGS_WORD);
594
595 buf[(*pos)++] = 0;
596
597 if (0 == buf[*pos])
598 return(ARGS_WORD);
599
600 if ( ! (ARGS_TABSEP & fl))
601 while (buf[*pos] && ' ' == buf[*pos])
602 (*pos)++;
603
604 if (buf[*pos])
605 return(ARGS_WORD);
606
607 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
608 return(ARGS_ERROR);
609
610 return(ARGS_WORD);
611 }
612
613 /*
614 * If we're a quoted string (and quoted strings are allowed),
615 * then parse ahead to the next quote. If none's found, it's an
616 * error. After, parse to the next word.
617 */
618
619 *v = &buf[++(*pos)];
620
621 while (buf[*pos] && '\"' != buf[*pos])
622 (*pos)++;
623
624 if (0 == buf[*pos]) {
625 (void)perr(mdoc, line, *pos, EQUOTTERM);
626 return(ARGS_ERROR);
627 }
628
629 buf[(*pos)++] = 0;
630 if (0 == buf[*pos])
631 return(ARGS_QWORD);
632
633 while (buf[*pos] && ' ' == buf[*pos])
634 (*pos)++;
635
636 if (buf[*pos])
637 return(ARGS_QWORD);
638
639 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
640 return(ARGS_ERROR);
641
642 return(ARGS_QWORD);
643 }
644
645
646 static int
647 argv_a2arg(int tok, const char *argv)
648 {
649
650 /*
651 * Parse an argument identifier from its text. XXX - this
652 * should really be table-driven to clarify the code.
653 *
654 * If you add an argument to the list, make sure that you
655 * register it here with its one or more macros!
656 */
657
658 switch (tok) {
659 case (MDOC_An):
660 if (xstrcmp(argv, "split"))
661 return(MDOC_Split);
662 else if (xstrcmp(argv, "nosplit"))
663 return(MDOC_Nosplit);
664 break;
665
666 case (MDOC_Bd):
667 if (xstrcmp(argv, "ragged"))
668 return(MDOC_Ragged);
669 else if (xstrcmp(argv, "unfilled"))
670 return(MDOC_Unfilled);
671 else if (xstrcmp(argv, "filled"))
672 return(MDOC_Filled);
673 else if (xstrcmp(argv, "literal"))
674 return(MDOC_Literal);
675 else if (xstrcmp(argv, "file"))
676 return(MDOC_File);
677 else if (xstrcmp(argv, "offset"))
678 return(MDOC_Offset);
679 else if (xstrcmp(argv, "compact"))
680 return(MDOC_Compact);
681 break;
682
683 case (MDOC_Bf):
684 if (xstrcmp(argv, "emphasis"))
685 return(MDOC_Emphasis);
686 else if (xstrcmp(argv, "literal"))
687 return(MDOC_Literal);
688 else if (xstrcmp(argv, "symbolic"))
689 return(MDOC_Symbolic);
690 break;
691
692 case (MDOC_Bk):
693 if (xstrcmp(argv, "words"))
694 return(MDOC_Words);
695 break;
696
697 case (MDOC_Bl):
698 if (xstrcmp(argv, "bullet"))
699 return(MDOC_Bullet);
700 else if (xstrcmp(argv, "dash"))
701 return(MDOC_Dash);
702 else if (xstrcmp(argv, "hyphen"))
703 return(MDOC_Hyphen);
704 else if (xstrcmp(argv, "item"))
705 return(MDOC_Item);
706 else if (xstrcmp(argv, "enum"))
707 return(MDOC_Enum);
708 else if (xstrcmp(argv, "tag"))
709 return(MDOC_Tag);
710 else if (xstrcmp(argv, "diag"))
711 return(MDOC_Diag);
712 else if (xstrcmp(argv, "hang"))
713 return(MDOC_Hang);
714 else if (xstrcmp(argv, "ohang"))
715 return(MDOC_Ohang);
716 else if (xstrcmp(argv, "inset"))
717 return(MDOC_Inset);
718 else if (xstrcmp(argv, "column"))
719 return(MDOC_Column);
720 else if (xstrcmp(argv, "width"))
721 return(MDOC_Width);
722 else if (xstrcmp(argv, "offset"))
723 return(MDOC_Offset);
724 else if (xstrcmp(argv, "compact"))
725 return(MDOC_Compact);
726 else if (xstrcmp(argv, "nested"))
727 return(MDOC_Nested);
728 break;
729
730 case (MDOC_Rv):
731 /* FALLTHROUGH */
732 case (MDOC_Ex):
733 if (xstrcmp(argv, "std"))
734 return(MDOC_Std);
735 break;
736 default:
737 break;
738 }
739
740 return(MDOC_ARG_MAX);
741 }
742
743
744 static int
745 argv_multi(struct mdoc *mdoc, int line,
746 struct mdoc_argv *v, int *pos, char *buf)
747 {
748 int c, ppos;
749 char *p;
750
751 ppos = *pos;
752
753 for (v->sz = 0; ; v->sz++) {
754 if ('-' == buf[*pos])
755 break;
756 c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p);
757 if (ARGS_ERROR == c)
758 return(0);
759 else if (ARGS_EOLN == c)
760 break;
761
762 if (0 == v->sz % MULTI_STEP)
763 v->value = xrealloc(v->value,
764 (v->sz + MULTI_STEP) * sizeof(char *));
765
766 v->value[(int)v->sz] = xstrdup(p);
767 }
768
769 if (v->sz)
770 return(1);
771
772 return(perr(mdoc, line, ppos, EARGVAL));
773 }
774
775
776 static int
777 argv_opt_single(struct mdoc *mdoc, int line,
778 struct mdoc_argv *v, int *pos, char *buf)
779 {
780 int c;
781 char *p;
782
783 if ('-' == buf[*pos])
784 return(1);
785
786 c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p);
787 if (ARGS_ERROR == c)
788 return(0);
789 if (ARGS_EOLN == c)
790 return(1);
791
792 v->sz = 1;
793 v->value = xcalloc(1, sizeof(char *));
794 v->value[0] = xstrdup(p);
795 return(1);
796 }
797
798
799 /*
800 * Parse a single, mandatory value from the stream.
801 */
802 static int
803 argv_single(struct mdoc *mdoc, int line,
804 struct mdoc_argv *v, int *pos, char *buf)
805 {
806 int c, ppos;
807 char *p;
808
809 ppos = *pos;
810
811 c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p);
812 if (ARGS_ERROR == c)
813 return(0);
814 if (ARGS_EOLN == c)
815 return(perr(mdoc, line, ppos, EARGVAL));
816
817 v->sz = 1;
818 v->value = xcalloc(1, sizeof(char *));
819 v->value[0] = xstrdup(p);
820 return(1);
821 }
822
823
824 /*
825 * Determine rules for parsing arguments. Arguments can either accept
826 * no parameters, an optional single parameter, one parameter, or
827 * multiple parameters.
828 */
829 static int
830 argv(struct mdoc *mdoc, int line,
831 struct mdoc_argv *v, int *pos, char *buf)
832 {
833
834 v->sz = 0;
835 v->value = NULL;
836
837 switch (mdoc_argvflags[v->arg]) {
838 case (ARGV_SINGLE):
839 return(argv_single(mdoc, line, v, pos, buf));
840 case (ARGV_MULTI):
841 return(argv_multi(mdoc, line, v, pos, buf));
842 case (ARGV_OPT_SINGLE):
843 return(argv_opt_single(mdoc, line, v, pos, buf));
844 default:
845 /* ARGV_NONE */
846 break;
847 }
848
849 return(1);
850 }