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