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