]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
Fixed my breaking of Ingo's eoln-whitespace detection code, where a
[mandoc.git] / mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.52 2010/05/31 13:39:13 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 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 above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "mandoc.h"
30 #include "libmdoc.h"
31 #include "libmandoc.h"
32
33 /*
34 * Routines to parse arguments of macros. Arguments follow the syntax
35 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
36 * arguments, multiple arguments per value, no-value arguments, etc.
37 *
38 * There's no limit to the number or arguments that may be allocated.
39 */
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 #define MULTI_STEP 5
47
48 static int argv_a2arg(enum mdoct, const char *);
49 static enum margserr args(struct mdoc *, int, int *,
50 char *, int, char **);
51 static int argv(struct mdoc *, int,
52 struct mdoc_argv *, int *, char *);
53 static int argv_single(struct mdoc *, int,
54 struct mdoc_argv *, int *, char *);
55 static int argv_opt_single(struct mdoc *, int,
56 struct mdoc_argv *, int *, char *);
57 static int argv_multi(struct mdoc *, int,
58 struct mdoc_argv *, int *, char *);
59
60 /* Per-argument flags. */
61
62 static int mdoc_argvflags[MDOC_ARG_MAX] = {
63 ARGV_NONE, /* MDOC_Split */
64 ARGV_NONE, /* MDOC_Nosplit */
65 ARGV_NONE, /* MDOC_Ragged */
66 ARGV_NONE, /* MDOC_Unfilled */
67 ARGV_NONE, /* MDOC_Literal */
68 ARGV_SINGLE, /* MDOC_File */
69 ARGV_OPT_SINGLE, /* MDOC_Offset */
70 ARGV_NONE, /* MDOC_Bullet */
71 ARGV_NONE, /* MDOC_Dash */
72 ARGV_NONE, /* MDOC_Hyphen */
73 ARGV_NONE, /* MDOC_Item */
74 ARGV_NONE, /* MDOC_Enum */
75 ARGV_NONE, /* MDOC_Tag */
76 ARGV_NONE, /* MDOC_Diag */
77 ARGV_NONE, /* MDOC_Hang */
78 ARGV_NONE, /* MDOC_Ohang */
79 ARGV_NONE, /* MDOC_Inset */
80 ARGV_MULTI, /* MDOC_Column */
81 ARGV_SINGLE, /* MDOC_Width */
82 ARGV_NONE, /* MDOC_Compact */
83 ARGV_NONE, /* MDOC_Std */
84 ARGV_NONE, /* MDOC_Filled */
85 ARGV_NONE, /* MDOC_Words */
86 ARGV_NONE, /* MDOC_Emphasis */
87 ARGV_NONE, /* MDOC_Symbolic */
88 ARGV_NONE /* MDOC_Symbolic */
89 };
90
91 static int mdoc_argflags[MDOC_MAX] = {
92 0, /* Ap */
93 0, /* Dd */
94 0, /* Dt */
95 0, /* Os */
96 0, /* Sh */
97 0, /* Ss */
98 ARGS_DELIM, /* Pp */
99 ARGS_DELIM, /* D1 */
100 ARGS_DELIM, /* Dl */
101 0, /* Bd */
102 0, /* Ed */
103 0, /* Bl */
104 0, /* El */
105 0, /* It */
106 ARGS_DELIM, /* Ad */
107 ARGS_DELIM, /* An */
108 ARGS_DELIM, /* Ar */
109 0, /* Cd */
110 ARGS_DELIM, /* Cm */
111 ARGS_DELIM, /* Dv */
112 ARGS_DELIM, /* Er */
113 ARGS_DELIM, /* Ev */
114 0, /* Ex */
115 ARGS_DELIM, /* Fa */
116 0, /* Fd */
117 ARGS_DELIM, /* Fl */
118 ARGS_DELIM, /* Fn */
119 ARGS_DELIM, /* Ft */
120 ARGS_DELIM, /* Ic */
121 0, /* In */
122 ARGS_DELIM, /* Li */
123 0, /* Nd */
124 ARGS_DELIM, /* Nm */
125 ARGS_DELIM, /* Op */
126 0, /* Ot */
127 ARGS_DELIM, /* Pa */
128 0, /* Rv */
129 ARGS_DELIM, /* St */
130 ARGS_DELIM, /* Va */
131 ARGS_DELIM, /* Vt */
132 ARGS_DELIM, /* Xr */
133 0, /* %A */
134 0, /* %B */
135 0, /* %D */
136 0, /* %I */
137 0, /* %J */
138 0, /* %N */
139 0, /* %O */
140 0, /* %P */
141 0, /* %R */
142 0, /* %T */
143 0, /* %V */
144 ARGS_DELIM, /* Ac */
145 0, /* Ao */
146 ARGS_DELIM, /* Aq */
147 ARGS_DELIM, /* At */
148 ARGS_DELIM, /* Bc */
149 0, /* Bf */
150 0, /* Bo */
151 ARGS_DELIM, /* Bq */
152 ARGS_DELIM, /* Bsx */
153 ARGS_DELIM, /* Bx */
154 0, /* Db */
155 ARGS_DELIM, /* Dc */
156 0, /* Do */
157 ARGS_DELIM, /* Dq */
158 ARGS_DELIM, /* Ec */
159 0, /* Ef */
160 ARGS_DELIM, /* Em */
161 0, /* Eo */
162 ARGS_DELIM, /* Fx */
163 ARGS_DELIM, /* Ms */
164 ARGS_DELIM, /* No */
165 ARGS_DELIM, /* Ns */
166 ARGS_DELIM, /* Nx */
167 ARGS_DELIM, /* Ox */
168 ARGS_DELIM, /* Pc */
169 ARGS_DELIM, /* Pf */
170 0, /* Po */
171 ARGS_DELIM, /* Pq */
172 ARGS_DELIM, /* Qc */
173 ARGS_DELIM, /* Ql */
174 0, /* Qo */
175 ARGS_DELIM, /* Qq */
176 0, /* Re */
177 0, /* Rs */
178 ARGS_DELIM, /* Sc */
179 0, /* So */
180 ARGS_DELIM, /* Sq */
181 0, /* Sm */
182 ARGS_DELIM, /* Sx */
183 ARGS_DELIM, /* Sy */
184 ARGS_DELIM, /* Tn */
185 ARGS_DELIM, /* Ux */
186 ARGS_DELIM, /* Xc */
187 0, /* Xo */
188 0, /* Fo */
189 0, /* Fc */
190 0, /* Oo */
191 ARGS_DELIM, /* Oc */
192 0, /* Bk */
193 0, /* Ek */
194 0, /* Bt */
195 0, /* Hf */
196 0, /* Fr */
197 0, /* Ud */
198 0, /* Lb */
199 ARGS_DELIM, /* Lp */
200 ARGS_DELIM, /* Lk */
201 ARGS_DELIM, /* Mt */
202 ARGS_DELIM, /* Brq */
203 0, /* Bro */
204 ARGS_DELIM, /* Brc */
205 0, /* %C */
206 0, /* Es */
207 0, /* En */
208 0, /* Dx */
209 0, /* %Q */
210 0, /* br */
211 0, /* sp */
212 0, /* %U */
213 0, /* Ta */
214 };
215
216
217 /*
218 * Parse an argument from line text. This comes in the form of -key
219 * [value0...], which may either have a single mandatory value, at least
220 * one mandatory value, an optional single value, or no value.
221 */
222 enum margverr
223 mdoc_argv(struct mdoc *m, int line, enum mdoct tok,
224 struct mdoc_arg **v, int *pos, char *buf)
225 {
226 char *p, sv;
227 struct mdoc_argv tmp;
228 struct mdoc_arg *arg;
229
230 if ('\0' == buf[*pos])
231 return(ARGV_EOLN);
232
233 assert(' ' != buf[*pos]);
234
235 /* Parse through to the first unescaped space. */
236
237 p = &buf[++(*pos)];
238
239 assert(*pos > 0);
240
241 /* LINTED */
242 while (buf[*pos]) {
243 if (' ' == buf[*pos])
244 if ('\\' != buf[*pos - 1])
245 break;
246 (*pos)++;
247 }
248
249 /* XXX - save zeroed byte, if not an argument. */
250
251 sv = '\0';
252 if (buf[*pos]) {
253 sv = buf[*pos];
254 buf[(*pos)++] = '\0';
255 }
256
257 (void)memset(&tmp, 0, sizeof(struct mdoc_argv));
258 tmp.line = line;
259 tmp.pos = *pos;
260
261 /* See if our token accepts the argument. */
262
263 if (MDOC_ARG_MAX == (tmp.arg = argv_a2arg(tok, p))) {
264 /* XXX - restore saved zeroed byte. */
265 if (sv)
266 buf[*pos - 1] = sv;
267 return(ARGV_WORD);
268 }
269
270 while (buf[*pos] && ' ' == buf[*pos])
271 (*pos)++;
272
273 if ( ! argv(m, line, &tmp, pos, buf))
274 return(ARGV_ERROR);
275
276 if (NULL == (arg = *v))
277 arg = *v = mandoc_calloc(1, sizeof(struct mdoc_arg));
278
279 arg->argc++;
280 arg->argv = mandoc_realloc
281 (arg->argv, arg->argc * sizeof(struct mdoc_argv));
282
283 (void)memcpy(&arg->argv[(int)arg->argc - 1],
284 &tmp, sizeof(struct mdoc_argv));
285
286 return(ARGV_ARG);
287 }
288
289
290 void
291 mdoc_argv_free(struct mdoc_arg *p)
292 {
293 int i;
294
295 if (NULL == p)
296 return;
297
298 if (p->refcnt) {
299 --(p->refcnt);
300 if (p->refcnt)
301 return;
302 }
303 assert(p->argc);
304
305 for (i = (int)p->argc - 1; i >= 0; i--)
306 mdoc_argn_free(p, i);
307
308 free(p->argv);
309 free(p);
310 }
311
312
313 void
314 mdoc_argn_free(struct mdoc_arg *p, int iarg)
315 {
316 struct mdoc_argv *arg = &p->argv[iarg];
317 int j;
318
319 if (arg->sz && arg->value) {
320 for (j = (int)arg->sz - 1; j >= 0; j--)
321 free(arg->value[j]);
322 free(arg->value);
323 }
324
325 for (--p->argc; iarg < (int)p->argc; iarg++)
326 p->argv[iarg] = p->argv[iarg+1];
327 }
328
329
330 enum margserr
331 mdoc_zargs(struct mdoc *m, int line, int *pos,
332 char *buf, int flags, char **v)
333 {
334
335 return(args(m, line, pos, buf, flags, v));
336 }
337
338
339 enum margserr
340 mdoc_args(struct mdoc *m, int line, int *pos,
341 char *buf, enum mdoct tok, char **v)
342 {
343 int fl;
344 struct mdoc_node *n;
345
346 fl = mdoc_argflags[tok];
347
348 if (MDOC_It != tok)
349 return(args(m, line, pos, buf, fl, v));
350
351 /*
352 * We know that we're in an `It', so it's reasonable to expect
353 * us to be sitting in a `Bl'. Someday this may not be the case
354 * (if we allow random `It's sitting out there), so provide a
355 * safe fall-back into the default behaviour.
356 */
357
358 for (n = m->last; n; n = n->parent)
359 if (MDOC_Bl == n->tok)
360 break;
361
362 if (n && LIST_column == n->data.list) {
363 fl |= ARGS_TABSEP;
364 fl &= ~ARGS_DELIM;
365 }
366
367 return(args(m, line, pos, buf, fl, v));
368 }
369
370
371 static enum margserr
372 args(struct mdoc *m, int line, int *pos,
373 char *buf, int fl, char **v)
374 {
375 int i;
376 char *p, *pp;
377 enum margserr rc;
378 enum mdelim d;
379
380 /*
381 * Parse out the terms (like `val' in `.Xx -arg val' or simply
382 * `.Xx val'), which can have all sorts of properties:
383 *
384 * ARGS_DELIM: use special handling if encountering trailing
385 * delimiters in the form of [[::delim::][ ]+]+.
386 *
387 * ARGS_NOWARN: don't post warnings. This is only used when
388 * re-parsing delimiters, as the warnings have already been
389 * posted.
390 *
391 * ARGS_TABSEP: use special handling for tab/`Ta' separated
392 * phrases like in `Bl -column'.
393 */
394
395 assert(' ' != buf[*pos]);
396
397 if ('\0' == buf[*pos]) {
398 if (MDOC_PPHRASE & m->flags)
399 return(ARGS_EOLN);
400 /*
401 * If we're not in a partial phrase and the flag for
402 * being a phrase literal is still set, the punctuation
403 * is unterminated.
404 */
405 if (MDOC_PHRASELIT & m->flags)
406 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_BADQUOTE))
407 return(ARGS_ERROR);
408
409 m->flags &= ~MDOC_PHRASELIT;
410 return(ARGS_EOLN);
411 }
412
413 /*
414 * If the first character is a closing delimiter and we're to
415 * look for delimited strings, then pass down the buffer seeing
416 * if it follows the pattern of [[::delim::][ ]+]+. Note that
417 * we ONLY care about closing delimiters.
418 */
419
420 if ((fl & ARGS_DELIM) && DELIM_CLOSE == mdoc_iscdelim(buf[*pos])) {
421 for (i = *pos; buf[i]; ) {
422 d = mdoc_iscdelim(buf[i]);
423 if (DELIM_NONE == d || DELIM_OPEN == d)
424 break;
425 i++;
426 if ('\0' == buf[i] || ' ' != buf[i])
427 break;
428 i++;
429 while (buf[i] && ' ' == buf[i])
430 i++;
431 }
432
433 if ('\0' == buf[i]) {
434 *v = &buf[*pos];
435 if (i && ' ' != buf[i - 1])
436 return(ARGS_PUNCT);
437 if (ARGS_NOWARN & fl)
438 return(ARGS_PUNCT);
439 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE))
440 return(ARGS_ERROR);
441 return(ARGS_PUNCT);
442 }
443 }
444
445 *v = &buf[*pos];
446
447 /*
448 * First handle TABSEP items, restricted to `Bl -column'. This
449 * ignores conventional token parsing and instead uses tabs or
450 * `Ta' macros to separate phrases. Phrases are parsed again
451 * for arguments at a later phase.
452 */
453
454 if (ARGS_TABSEP & fl) {
455 /* Scan ahead to tab (can't be escaped). */
456 p = strchr(*v, '\t');
457 pp = NULL;
458
459 /* Scan ahead to unescaped `Ta'. */
460 if ( ! (MDOC_PHRASELIT & m->flags))
461 for (pp = *v; ; pp++) {
462 if (NULL == (pp = strstr(pp, "Ta")))
463 break;
464 if (pp > *v && ' ' != *(pp - 1))
465 continue;
466 if (' ' == *(pp + 2) || '\0' == *(pp + 2))
467 break;
468 }
469
470 /* By default, assume a phrase. */
471 rc = ARGS_PHRASE;
472
473 /*
474 * Adjust new-buffer position to be beyond delimiter
475 * mark (e.g., Ta -> end + 2).
476 */
477 if (p && pp) {
478 *pos += pp < p ? 2 : 1;
479 rc = pp < p ? ARGS_PHRASE : ARGS_PPHRASE;
480 p = pp < p ? pp : p;
481 } else if (p && ! pp) {
482 rc = ARGS_PPHRASE;
483 *pos += 1;
484 } else if (pp && ! p) {
485 p = pp;
486 *pos += 2;
487 } else {
488 rc = ARGS_PEND;
489 p = strchr(*v, 0);
490 }
491
492 /* Whitespace check for eoln case... */
493 if ('\0' == *p && ' ' == *(p - 1) && ! (ARGS_NOWARN & fl))
494 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE))
495 return(ARGS_ERROR);
496
497 *pos += (int)(p - *v);
498
499 /* Strip delimiter's preceding whitespace. */
500 pp = p - 1;
501 while (pp > *v && ' ' == *pp) {
502 if (pp > *v && '\\' == *(pp - 1))
503 break;
504 pp--;
505 }
506 *(pp + 1) = 0;
507
508 /* Strip delimiter's proceeding whitespace. */
509 for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)
510 /* Skip ahead. */ ;
511
512 return(rc);
513 }
514
515 /*
516 * Process a quoted literal. A quote begins with a double-quote
517 * and ends with a double-quote NOT preceded by a double-quote.
518 * Whitespace is NOT involved in literal termination.
519 */
520
521 if (MDOC_PHRASELIT & m->flags || '\"' == buf[*pos]) {
522 if ( ! (MDOC_PHRASELIT & m->flags))
523 *v = &buf[++(*pos)];
524
525 if (MDOC_PPHRASE & m->flags)
526 m->flags |= MDOC_PHRASELIT;
527
528 for ( ; buf[*pos]; (*pos)++) {
529 if ('\"' != buf[*pos])
530 continue;
531 if ('\"' != buf[*pos + 1])
532 break;
533 (*pos)++;
534 }
535
536 if ('\0' == buf[*pos]) {
537 if (ARGS_NOWARN & fl || MDOC_PPHRASE & m->flags)
538 return(ARGS_QWORD);
539 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_BADQUOTE))
540 return(ARGS_ERROR);
541 return(ARGS_QWORD);
542 }
543
544 m->flags &= ~MDOC_PHRASELIT;
545 buf[(*pos)++] = '\0';
546
547 if ('\0' == buf[*pos])
548 return(ARGS_QWORD);
549
550 while (' ' == buf[*pos])
551 (*pos)++;
552
553 if (0 == buf[*pos] && ! (ARGS_NOWARN & fl))
554 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE))
555 return(ARGS_ERROR);
556
557 return(ARGS_QWORD);
558 }
559
560 /*
561 * A non-quoted term progresses until either the end of line or
562 * a non-escaped whitespace.
563 */
564
565 for ( ; buf[*pos]; (*pos)++)
566 if (*pos && ' ' == buf[*pos] && '\\' != buf[*pos - 1])
567 break;
568
569 if ('\0' == buf[*pos])
570 return(ARGS_WORD);
571
572 buf[(*pos)++] = '\0';
573
574 while (' ' == buf[*pos])
575 (*pos)++;
576
577 if ('\0' == buf[*pos] && ! (ARGS_NOWARN & fl))
578 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE))
579 return(ARGS_ERROR);
580
581 return(ARGS_WORD);
582 }
583
584
585 static int
586 argv_a2arg(enum mdoct tok, const char *p)
587 {
588
589 /*
590 * Parse an argument identifier from its text. XXX - this
591 * should really be table-driven to clarify the code.
592 *
593 * If you add an argument to the list, make sure that you
594 * register it here with its one or more macros!
595 */
596
597 switch (tok) {
598 case (MDOC_An):
599 if (0 == strcmp(p, "split"))
600 return(MDOC_Split);
601 else if (0 == strcmp(p, "nosplit"))
602 return(MDOC_Nosplit);
603 break;
604
605 case (MDOC_Bd):
606 if (0 == strcmp(p, "ragged"))
607 return(MDOC_Ragged);
608 else if (0 == strcmp(p, "unfilled"))
609 return(MDOC_Unfilled);
610 else if (0 == strcmp(p, "filled"))
611 return(MDOC_Filled);
612 else if (0 == strcmp(p, "literal"))
613 return(MDOC_Literal);
614 else if (0 == strcmp(p, "file"))
615 return(MDOC_File);
616 else if (0 == strcmp(p, "offset"))
617 return(MDOC_Offset);
618 else if (0 == strcmp(p, "compact"))
619 return(MDOC_Compact);
620 else if (0 == strcmp(p, "centered"))
621 return(MDOC_Centred);
622 break;
623
624 case (MDOC_Bf):
625 if (0 == strcmp(p, "emphasis"))
626 return(MDOC_Emphasis);
627 else if (0 == strcmp(p, "literal"))
628 return(MDOC_Literal);
629 else if (0 == strcmp(p, "symbolic"))
630 return(MDOC_Symbolic);
631 break;
632
633 case (MDOC_Bk):
634 if (0 == strcmp(p, "words"))
635 return(MDOC_Words);
636 break;
637
638 case (MDOC_Bl):
639 if (0 == strcmp(p, "bullet"))
640 return(MDOC_Bullet);
641 else if (0 == strcmp(p, "dash"))
642 return(MDOC_Dash);
643 else if (0 == strcmp(p, "hyphen"))
644 return(MDOC_Hyphen);
645 else if (0 == strcmp(p, "item"))
646 return(MDOC_Item);
647 else if (0 == strcmp(p, "enum"))
648 return(MDOC_Enum);
649 else if (0 == strcmp(p, "tag"))
650 return(MDOC_Tag);
651 else if (0 == strcmp(p, "diag"))
652 return(MDOC_Diag);
653 else if (0 == strcmp(p, "hang"))
654 return(MDOC_Hang);
655 else if (0 == strcmp(p, "ohang"))
656 return(MDOC_Ohang);
657 else if (0 == strcmp(p, "inset"))
658 return(MDOC_Inset);
659 else if (0 == strcmp(p, "column"))
660 return(MDOC_Column);
661 else if (0 == strcmp(p, "width"))
662 return(MDOC_Width);
663 else if (0 == strcmp(p, "offset"))
664 return(MDOC_Offset);
665 else if (0 == strcmp(p, "compact"))
666 return(MDOC_Compact);
667 else if (0 == strcmp(p, "nested"))
668 return(MDOC_Nested);
669 break;
670
671 case (MDOC_Rv):
672 /* FALLTHROUGH */
673 case (MDOC_Ex):
674 if (0 == strcmp(p, "std"))
675 return(MDOC_Std);
676 break;
677 default:
678 break;
679 }
680
681 return(MDOC_ARG_MAX);
682 }
683
684
685 static int
686 argv_multi(struct mdoc *m, int line,
687 struct mdoc_argv *v, int *pos, char *buf)
688 {
689 enum margserr ac;
690 char *p;
691
692 for (v->sz = 0; ; v->sz++) {
693 if ('-' == buf[*pos])
694 break;
695 ac = args(m, line, pos, buf, 0, &p);
696 if (ARGS_ERROR == ac)
697 return(0);
698 else if (ARGS_EOLN == ac)
699 break;
700
701 if (0 == v->sz % MULTI_STEP)
702 v->value = mandoc_realloc(v->value,
703 (v->sz + MULTI_STEP) * sizeof(char *));
704
705 v->value[(int)v->sz] = mandoc_strdup(p);
706 }
707
708 return(1);
709 }
710
711
712 static int
713 argv_opt_single(struct mdoc *m, int line,
714 struct mdoc_argv *v, int *pos, char *buf)
715 {
716 enum margserr ac;
717 char *p;
718
719 if ('-' == buf[*pos])
720 return(1);
721
722 ac = args(m, line, pos, buf, 0, &p);
723 if (ARGS_ERROR == ac)
724 return(0);
725 if (ARGS_EOLN == ac)
726 return(1);
727
728 v->sz = 1;
729 v->value = mandoc_malloc(sizeof(char *));
730 v->value[0] = mandoc_strdup(p);
731
732 return(1);
733 }
734
735
736 /*
737 * Parse a single, mandatory value from the stream.
738 */
739 static int
740 argv_single(struct mdoc *m, int line,
741 struct mdoc_argv *v, int *pos, char *buf)
742 {
743 int ppos;
744 enum margserr ac;
745 char *p;
746
747 ppos = *pos;
748
749 ac = args(m, line, pos, buf, 0, &p);
750 if (ARGS_EOLN == ac) {
751 mdoc_pmsg(m, line, ppos, MANDOCERR_SYNTARGVCOUNT);
752 return(0);
753 } else if (ARGS_ERROR == ac)
754 return(0);
755
756 v->sz = 1;
757 v->value = mandoc_malloc(sizeof(char *));
758 v->value[0] = mandoc_strdup(p);
759
760 return(1);
761 }
762
763
764 /*
765 * Determine rules for parsing arguments. Arguments can either accept
766 * no parameters, an optional single parameter, one parameter, or
767 * multiple parameters.
768 */
769 static int
770 argv(struct mdoc *mdoc, int line,
771 struct mdoc_argv *v, int *pos, char *buf)
772 {
773
774 v->sz = 0;
775 v->value = NULL;
776
777 switch (mdoc_argvflags[v->arg]) {
778 case (ARGV_SINGLE):
779 return(argv_single(mdoc, line, v, pos, buf));
780 case (ARGV_MULTI):
781 return(argv_multi(mdoc, line, v, pos, buf));
782 case (ARGV_OPT_SINGLE):
783 return(argv_opt_single(mdoc, line, v, pos, buf));
784 default:
785 /* ARGV_NONE */
786 break;
787 }
788
789 return(1);
790 }