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