]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
Clarified EXIT STATUS sections. Discussed among schwarze@, Thomas, and
[mandoc.git] / mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.58 2010/07/21 09:36:41 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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 enum mdocargt 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;
317 int j;
318
319 arg = &p->argv[iarg];
320
321 if (arg->sz && arg->value) {
322 for (j = (int)arg->sz - 1; j >= 0; j--)
323 free(arg->value[j]);
324 free(arg->value);
325 }
326
327 for (--p->argc; iarg < (int)p->argc; iarg++)
328 p->argv[iarg] = p->argv[iarg+1];
329 }
330
331
332 enum margserr
333 mdoc_zargs(struct mdoc *m, int line, int *pos,
334 char *buf, int flags, char **v)
335 {
336
337 return(args(m, line, pos, buf, flags, v));
338 }
339
340
341 enum margserr
342 mdoc_args(struct mdoc *m, int line, int *pos,
343 char *buf, enum mdoct tok, char **v)
344 {
345 int fl;
346 struct mdoc_node *n;
347
348 fl = mdoc_argflags[tok];
349
350 if (MDOC_It != tok)
351 return(args(m, line, pos, buf, fl, v));
352
353 /*
354 * We know that we're in an `It', so it's reasonable to expect
355 * us to be sitting in a `Bl'. Someday this may not be the case
356 * (if we allow random `It's sitting out there), so provide a
357 * safe fall-back into the default behaviour.
358 */
359
360 for (n = m->last; n; n = n->parent)
361 if (MDOC_Bl == n->tok)
362 break;
363
364 assert(n->data.Bl);
365 if (n && LIST_column == n->data.Bl->type) {
366 fl |= ARGS_TABSEP;
367 fl &= ~ARGS_DELIM;
368 }
369
370 return(args(m, line, pos, buf, fl, v));
371 }
372
373
374 static enum margserr
375 args(struct mdoc *m, int line, int *pos,
376 char *buf, int fl, char **v)
377 {
378 int i;
379 char *p, *pp;
380 enum margserr rc;
381 enum mdelim d;
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(' ' != buf[*pos]);
399
400 if ('\0' == buf[*pos]) {
401 if (MDOC_PPHRASE & m->flags)
402 return(ARGS_EOLN);
403 /*
404 * If we're not in a partial phrase and the flag for
405 * being a phrase literal is still set, the punctuation
406 * is unterminated.
407 */
408 if (MDOC_PHRASELIT & m->flags)
409 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_BADQUOTE))
410 return(ARGS_ERROR);
411
412 m->flags &= ~MDOC_PHRASELIT;
413 return(ARGS_EOLN);
414 }
415
416 /*
417 * If the first character is a closing delimiter and we're to
418 * look for delimited strings, then pass down the buffer seeing
419 * if it follows the pattern of [[::delim::][ ]+]+. Note that
420 * we ONLY care about closing delimiters.
421 */
422
423 if ((fl & ARGS_DELIM) && DELIM_CLOSE == mdoc_iscdelim(buf[*pos])) {
424 for (i = *pos; buf[i]; ) {
425 d = mdoc_iscdelim(buf[i]);
426 if (DELIM_NONE == d || DELIM_OPEN == d)
427 break;
428 i++;
429 if ('\0' == buf[i] || ' ' != buf[i])
430 break;
431 i++;
432 while (buf[i] && ' ' == buf[i])
433 i++;
434 }
435
436 if ('\0' == buf[i]) {
437 *v = &buf[*pos];
438 if (i && ' ' != buf[i - 1])
439 return(ARGS_PUNCT);
440 if (ARGS_NOWARN & fl)
441 return(ARGS_PUNCT);
442 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE))
443 return(ARGS_ERROR);
444 return(ARGS_PUNCT);
445 }
446 }
447
448 *v = &buf[*pos];
449
450 /*
451 * First handle TABSEP items, restricted to `Bl -column'. This
452 * ignores conventional token parsing and instead uses tabs or
453 * `Ta' macros to separate phrases. Phrases are parsed again
454 * for arguments at a later phase.
455 */
456
457 if (ARGS_TABSEP & fl) {
458 /* Scan ahead to tab (can't be escaped). */
459 p = strchr(*v, '\t');
460 pp = NULL;
461
462 /* Scan ahead to unescaped `Ta'. */
463 if ( ! (MDOC_PHRASELIT & m->flags))
464 for (pp = *v; ; pp++) {
465 if (NULL == (pp = strstr(pp, "Ta")))
466 break;
467 if (pp > *v && ' ' != *(pp - 1))
468 continue;
469 if (' ' == *(pp + 2) || '\0' == *(pp + 2))
470 break;
471 }
472
473 /* By default, assume a phrase. */
474 rc = ARGS_PHRASE;
475
476 /*
477 * Adjust new-buffer position to be beyond delimiter
478 * mark (e.g., Ta -> end + 2).
479 */
480 if (p && pp) {
481 *pos += pp < p ? 2 : 1;
482 rc = pp < p ? ARGS_PHRASE : ARGS_PPHRASE;
483 p = pp < p ? pp : p;
484 } else if (p && ! pp) {
485 rc = ARGS_PPHRASE;
486 *pos += 1;
487 } else if (pp && ! p) {
488 p = pp;
489 *pos += 2;
490 } else {
491 rc = ARGS_PEND;
492 p = strchr(*v, 0);
493 }
494
495 /* Whitespace check for eoln case... */
496 if ('\0' == *p && ' ' == *(p - 1) && ! (ARGS_NOWARN & fl))
497 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE))
498 return(ARGS_ERROR);
499
500 *pos += (int)(p - *v);
501
502 /* Strip delimiter's preceding whitespace. */
503 pp = p - 1;
504 while (pp > *v && ' ' == *pp) {
505 if (pp > *v && '\\' == *(pp - 1))
506 break;
507 pp--;
508 }
509 *(pp + 1) = 0;
510
511 /* Strip delimiter's proceeding whitespace. */
512 for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)
513 /* Skip ahead. */ ;
514
515 return(rc);
516 }
517
518 /*
519 * Process a quoted literal. A quote begins with a double-quote
520 * and ends with a double-quote NOT preceded by a double-quote.
521 * Whitespace is NOT involved in literal termination.
522 */
523
524 if (MDOC_PHRASELIT & m->flags || '\"' == buf[*pos]) {
525 if ( ! (MDOC_PHRASELIT & m->flags))
526 *v = &buf[++(*pos)];
527
528 if (MDOC_PPHRASE & m->flags)
529 m->flags |= MDOC_PHRASELIT;
530
531 for ( ; buf[*pos]; (*pos)++) {
532 if ('\"' != buf[*pos])
533 continue;
534 if ('\"' != buf[*pos + 1])
535 break;
536 (*pos)++;
537 }
538
539 if ('\0' == buf[*pos]) {
540 if (ARGS_NOWARN & fl || MDOC_PPHRASE & m->flags)
541 return(ARGS_QWORD);
542 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_BADQUOTE))
543 return(ARGS_ERROR);
544 return(ARGS_QWORD);
545 }
546
547 m->flags &= ~MDOC_PHRASELIT;
548 buf[(*pos)++] = '\0';
549
550 if ('\0' == buf[*pos])
551 return(ARGS_QWORD);
552
553 while (' ' == buf[*pos])
554 (*pos)++;
555
556 if (0 == buf[*pos] && ! (ARGS_NOWARN & fl))
557 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE))
558 return(ARGS_ERROR);
559
560 return(ARGS_QWORD);
561 }
562
563 /*
564 * A non-quoted term progresses until either the end of line or
565 * a non-escaped whitespace.
566 */
567
568 for ( ; buf[*pos]; (*pos)++)
569 if (*pos && ' ' == buf[*pos] && '\\' != buf[*pos - 1])
570 break;
571
572 if ('\0' == buf[*pos])
573 return(ARGS_WORD);
574
575 buf[(*pos)++] = '\0';
576
577 while (' ' == buf[*pos])
578 (*pos)++;
579
580 if ('\0' == buf[*pos] && ! (ARGS_NOWARN & fl))
581 if ( ! mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE))
582 return(ARGS_ERROR);
583
584 return(ARGS_WORD);
585 }
586
587
588 static enum mdocargt
589 argv_a2arg(enum mdoct tok, const char *p)
590 {
591
592 /*
593 * Parse an argument identifier from its text. XXX - this
594 * should really be table-driven to clarify the code.
595 *
596 * If you add an argument to the list, make sure that you
597 * register it here with its one or more macros!
598 */
599
600 switch (tok) {
601 case (MDOC_An):
602 if (0 == strcmp(p, "split"))
603 return(MDOC_Split);
604 else if (0 == strcmp(p, "nosplit"))
605 return(MDOC_Nosplit);
606 break;
607
608 case (MDOC_Bd):
609 if (0 == strcmp(p, "ragged"))
610 return(MDOC_Ragged);
611 else if (0 == strcmp(p, "unfilled"))
612 return(MDOC_Unfilled);
613 else if (0 == strcmp(p, "filled"))
614 return(MDOC_Filled);
615 else if (0 == strcmp(p, "literal"))
616 return(MDOC_Literal);
617 else if (0 == strcmp(p, "file"))
618 return(MDOC_File);
619 else if (0 == strcmp(p, "offset"))
620 return(MDOC_Offset);
621 else if (0 == strcmp(p, "compact"))
622 return(MDOC_Compact);
623 else if (0 == strcmp(p, "centered"))
624 return(MDOC_Centred);
625 break;
626
627 case (MDOC_Bf):
628 if (0 == strcmp(p, "emphasis"))
629 return(MDOC_Emphasis);
630 else if (0 == strcmp(p, "literal"))
631 return(MDOC_Literal);
632 else if (0 == strcmp(p, "symbolic"))
633 return(MDOC_Symbolic);
634 break;
635
636 case (MDOC_Bk):
637 if (0 == strcmp(p, "words"))
638 return(MDOC_Words);
639 break;
640
641 case (MDOC_Bl):
642 if (0 == strcmp(p, "bullet"))
643 return(MDOC_Bullet);
644 else if (0 == strcmp(p, "dash"))
645 return(MDOC_Dash);
646 else if (0 == strcmp(p, "hyphen"))
647 return(MDOC_Hyphen);
648 else if (0 == strcmp(p, "item"))
649 return(MDOC_Item);
650 else if (0 == strcmp(p, "enum"))
651 return(MDOC_Enum);
652 else if (0 == strcmp(p, "tag"))
653 return(MDOC_Tag);
654 else if (0 == strcmp(p, "diag"))
655 return(MDOC_Diag);
656 else if (0 == strcmp(p, "hang"))
657 return(MDOC_Hang);
658 else if (0 == strcmp(p, "ohang"))
659 return(MDOC_Ohang);
660 else if (0 == strcmp(p, "inset"))
661 return(MDOC_Inset);
662 else if (0 == strcmp(p, "column"))
663 return(MDOC_Column);
664 else if (0 == strcmp(p, "width"))
665 return(MDOC_Width);
666 else if (0 == strcmp(p, "offset"))
667 return(MDOC_Offset);
668 else if (0 == strcmp(p, "compact"))
669 return(MDOC_Compact);
670 else if (0 == strcmp(p, "nested"))
671 return(MDOC_Nested);
672 break;
673
674 case (MDOC_Rv):
675 /* FALLTHROUGH */
676 case (MDOC_Ex):
677 if (0 == strcmp(p, "std"))
678 return(MDOC_Std);
679 break;
680 default:
681 break;
682 }
683
684 return(MDOC_ARG_MAX);
685 }
686
687
688 static int
689 argv_multi(struct mdoc *m, int line,
690 struct mdoc_argv *v, int *pos, char *buf)
691 {
692 enum margserr ac;
693 char *p;
694
695 for (v->sz = 0; ; v->sz++) {
696 if ('-' == buf[*pos])
697 break;
698 ac = args(m, line, pos, buf, 0, &p);
699 if (ARGS_ERROR == ac)
700 return(0);
701 else if (ARGS_EOLN == ac)
702 break;
703
704 if (0 == v->sz % MULTI_STEP)
705 v->value = mandoc_realloc(v->value,
706 (v->sz + MULTI_STEP) * sizeof(char *));
707
708 v->value[(int)v->sz] = mandoc_strdup(p);
709 }
710
711 return(1);
712 }
713
714
715 static int
716 argv_opt_single(struct mdoc *m, int line,
717 struct mdoc_argv *v, int *pos, char *buf)
718 {
719 enum margserr ac;
720 char *p;
721
722 if ('-' == buf[*pos])
723 return(1);
724
725 ac = args(m, line, pos, buf, 0, &p);
726 if (ARGS_ERROR == ac)
727 return(0);
728 if (ARGS_EOLN == ac)
729 return(1);
730
731 v->sz = 1;
732 v->value = mandoc_malloc(sizeof(char *));
733 v->value[0] = mandoc_strdup(p);
734
735 return(1);
736 }
737
738
739 /*
740 * Parse a single, mandatory value from the stream.
741 */
742 static int
743 argv_single(struct mdoc *m, int line,
744 struct mdoc_argv *v, int *pos, char *buf)
745 {
746 int ppos;
747 enum margserr ac;
748 char *p;
749
750 ppos = *pos;
751
752 ac = args(m, line, pos, buf, 0, &p);
753 if (ARGS_EOLN == ac) {
754 mdoc_pmsg(m, line, ppos, MANDOCERR_SYNTARGVCOUNT);
755 return(0);
756 } else if (ARGS_ERROR == ac)
757 return(0);
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 }