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