]> git.cameronkatri.com Git - mandoc.git/blob - argv.c
8ad5d662ff223b4d3e4b395cbfdcd3be45bec86c
[mandoc.git] / argv.c
1 /* $Id: argv.c,v 1.34 2009/02/28 12:16:02 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 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
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <assert.h>
20 #include <ctype.h>
21 #include <err.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "private.h"
27
28 /*
29 * Routines to parse arguments of macros. Arguments follow the syntax
30 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
31 * arguments, multiple arguments per value, no-value arguments, etc.
32 */
33
34 #define ARGS_QUOTED (1 << 0)
35 #define ARGS_DELIM (1 << 1)
36 #define ARGS_TABSEP (1 << 2)
37
38 static int argv_a2arg(int, const char *);
39 static int args(struct mdoc *, int, int *,
40 char *, int, char **);
41 static int argv(struct mdoc *, int,
42 struct mdoc_arg *, int *, char *);
43 static int argv_single(struct mdoc *, int,
44 struct mdoc_arg *, int *, char *);
45 static int argv_multi(struct mdoc *, int,
46 struct mdoc_arg *, int *, char *);
47 static int pwarn(struct mdoc *, int, int, int);
48 static int perr(struct mdoc *, int, int, int);
49
50 /* Warning messages. */
51
52 #define WQUOTPARM (0)
53 #define WARGVPARM (1)
54 #define WCOLEMPTY (2)
55 #define WTAILWS (3)
56
57 /* Error messages. */
58
59 #define EQUOTTERM (0)
60 #define EARGVAL (1)
61 #define EARGMANY (2)
62
63 static int mdoc_argflags[MDOC_MAX] = {
64 0, /* \" */
65 0, /* Dd */
66 0, /* Dt */
67 0, /* Os */
68 0, /* Sh */
69 0, /* Ss */
70 ARGS_DELIM, /* Pp */
71 ARGS_DELIM, /* D1 */
72 ARGS_DELIM, /* Dl */
73 0, /* Bd */
74 0, /* Ed */
75 0, /* Bl */
76 0, /* El */
77 0, /* It */
78 ARGS_DELIM, /* Ad */
79 ARGS_DELIM, /* An */
80 ARGS_DELIM, /* Ar */
81 ARGS_QUOTED, /* Cd */
82 ARGS_DELIM, /* Cm */
83 ARGS_DELIM, /* Dv */
84 ARGS_DELIM, /* Er */
85 ARGS_DELIM, /* Ev */
86 0, /* Ex */
87 ARGS_DELIM | ARGS_QUOTED, /* Fa */
88 0, /* Fd */
89 ARGS_DELIM, /* Fl */
90 ARGS_DELIM | ARGS_QUOTED, /* Fn */
91 ARGS_DELIM | ARGS_QUOTED, /* Ft */
92 ARGS_DELIM, /* Ic */
93 0, /* In */
94 ARGS_DELIM, /* Li */
95 0, /* Nd */
96 ARGS_DELIM, /* Nm */
97 ARGS_DELIM, /* Op */
98 0, /* Ot */
99 ARGS_DELIM, /* Pa */
100 0, /* Rv */
101 ARGS_DELIM, /* St */
102 ARGS_DELIM, /* Va */
103 ARGS_DELIM, /* Vt */
104 ARGS_DELIM, /* Xr */
105 ARGS_QUOTED, /* %A */
106 ARGS_QUOTED, /* %B */
107 ARGS_QUOTED, /* %D */
108 ARGS_QUOTED, /* %I */
109 ARGS_QUOTED, /* %J */
110 ARGS_QUOTED, /* %N */
111 ARGS_QUOTED, /* %O */
112 ARGS_QUOTED, /* %P */
113 ARGS_QUOTED, /* %R */
114 ARGS_QUOTED, /* %T */
115 ARGS_QUOTED, /* %V */
116 ARGS_DELIM, /* Ac */
117 0, /* Ao */
118 ARGS_DELIM, /* Aq */
119 ARGS_DELIM, /* At */
120 ARGS_DELIM, /* Bc */
121 0, /* Bf */
122 0, /* Bo */
123 ARGS_DELIM, /* Bq */
124 ARGS_DELIM, /* Bsx */
125 ARGS_DELIM, /* Bx */
126 0, /* Db */
127 ARGS_DELIM, /* Dc */
128 0, /* Do */
129 ARGS_DELIM, /* Dq */
130 ARGS_DELIM, /* Ec */
131 0, /* Ef */
132 ARGS_DELIM, /* Em */
133 0, /* Eo */
134 ARGS_DELIM, /* Fx */
135 ARGS_DELIM, /* Ms */
136 ARGS_DELIM, /* No */
137 ARGS_DELIM, /* Ns */
138 ARGS_DELIM, /* Nx */
139 ARGS_DELIM, /* Ox */
140 ARGS_DELIM, /* Pc */
141 ARGS_DELIM, /* Pf */
142 0, /* Po */
143 ARGS_DELIM, /* Pq */
144 ARGS_DELIM, /* Qc */
145 ARGS_DELIM, /* Ql */
146 0, /* Qo */
147 ARGS_DELIM, /* Qq */
148 0, /* Re */
149 0, /* Rs */
150 ARGS_DELIM, /* Sc */
151 0, /* So */
152 ARGS_DELIM, /* Sq */
153 0, /* Sm */
154 ARGS_DELIM, /* Sx */
155 ARGS_DELIM, /* Sy */
156 ARGS_DELIM, /* Tn */
157 ARGS_DELIM, /* Ux */
158 ARGS_DELIM, /* Xc */
159 0, /* Xo */
160 0, /* Fo */
161 0, /* Fc */
162 0, /* Oo */
163 ARGS_DELIM, /* Oc */
164 0, /* Bk */
165 0, /* Ek */
166 0, /* Bt */
167 0, /* Hf */
168 0, /* Fr */
169 0, /* Ud */
170 };
171
172
173 static int
174 perr(struct mdoc *mdoc, int line, int pos, int code)
175 {
176 int c;
177
178 switch (code) {
179 case (EQUOTTERM):
180 c = mdoc_perr(mdoc, line, pos,
181 "unterminated quoted parameter");
182 break;
183 case (EARGVAL):
184 c = mdoc_perr(mdoc, line, pos,
185 "argument requires a value");
186 break;
187 case (EARGMANY):
188 c = mdoc_perr(mdoc, line, pos,
189 "too many values for argument");
190 break;
191 default:
192 abort();
193 /* NOTREACHED */
194 }
195 return(c);
196 }
197
198
199 static int
200 pwarn(struct mdoc *mdoc, int line, int pos, int code)
201 {
202 int c;
203
204 switch (code) {
205 case (WQUOTPARM):
206 c = mdoc_pwarn(mdoc, line, pos, WARN_SYNTAX,
207 "unexpected quoted parameter");
208 break;
209 case (WARGVPARM):
210 c = mdoc_pwarn(mdoc, line, pos, WARN_SYNTAX,
211 "argument-like parameter");
212 break;
213 case (WCOLEMPTY):
214 c = mdoc_pwarn(mdoc, line, pos, WARN_SYNTAX,
215 "last list column is empty");
216 break;
217 case (WTAILWS):
218 c = mdoc_pwarn(mdoc, line, pos, WARN_COMPAT,
219 "trailing whitespace");
220 break;
221 default:
222 abort();
223 /* NOTREACHED */
224 }
225 return(c);
226 }
227
228
229 int
230 mdoc_args(struct mdoc *mdoc, int line,
231 int *pos, char *buf, int tok, char **v)
232 {
233 int fl, c, i;
234 struct mdoc_node *n;
235
236 fl = (0 == tok) ? 0 : mdoc_argflags[tok];
237
238 /*
239 * First see if we should use TABSEP (Bl -column). This
240 * invalidates the use of ARGS_DELIM.
241 */
242
243 if (MDOC_It == tok) {
244 for (n = mdoc->last; n; n = n->parent)
245 if (MDOC_BLOCK == n->type)
246 if (MDOC_Bl == n->tok)
247 break;
248 assert(n);
249 c = (int)n->data.block.argc;
250 assert(c > 0);
251
252 /* LINTED */
253 for (i = 0; i < c; i++) {
254 if (MDOC_Column != n->data.block.argv[i].arg)
255 continue;
256 fl |= ARGS_TABSEP;
257 fl &= ~ARGS_DELIM;
258 break;
259 }
260 }
261
262 return(args(mdoc, line, pos, buf, fl, v));
263 }
264
265
266 static int
267 args(struct mdoc *mdoc, int line,
268 int *pos, char *buf, int fl, char **v)
269 {
270 int i;
271 char *p, *pp;
272
273 assert(*pos > 0);
274
275 if (0 == buf[*pos])
276 return(ARGS_EOLN);
277
278 if ('\"' == buf[*pos] && ! (fl & ARGS_QUOTED))
279 if ( ! pwarn(mdoc, line, *pos, WQUOTPARM))
280 return(ARGS_ERROR);
281
282 if ('-' == buf[*pos])
283 if ( ! pwarn(mdoc, line, *pos, WARGVPARM))
284 return(ARGS_ERROR);
285
286 /*
287 * If the first character is a delimiter and we're to look for
288 * delimited strings, then pass down the buffer seeing if it
289 * follows the pattern of [[::delim::][ ]+]+.
290 */
291
292 if ((fl & ARGS_DELIM) && mdoc_iscdelim(buf[*pos])) {
293 for (i = *pos; buf[i]; ) {
294 if ( ! mdoc_iscdelim(buf[i]))
295 break;
296 i++;
297 /* There must be at least one space... */
298 if (0 == buf[i] || ! isspace((int)buf[i]))
299 break;
300 i++;
301 while (buf[i] && isspace((int)buf[i]))
302 i++;
303 }
304 if (0 == buf[i]) {
305 *v = &buf[*pos];
306 return(ARGS_PUNCT);
307 }
308 }
309
310 /* First parse non-quoted strings. */
311
312 if ('\"' != buf[*pos] || ! (ARGS_QUOTED & fl)) {
313 *v = &buf[*pos];
314
315 /*
316 * Thar be dragons here! If we're tab-separated, search
317 * ahead for either a tab or the `Ta' macro. If a tab
318 * is detected, it mustn't be escaped; if a `Ta' is
319 * detected, it must be space-buffered before and after.
320 * If either of these hold true, then prune out the
321 * extra spaces and call it an argument.
322 */
323
324 if (ARGS_TABSEP & fl) {
325 /* Scan ahead to unescaped tab. */
326
327 for (p = *v; ; p++) {
328 if (NULL == (p = strchr(p, '\t')))
329 break;
330 if (p == *v)
331 break;
332 if ('\\' != *(p - 1))
333 break;
334 }
335
336 /* Scan ahead to unescaped `Ta'. */
337
338 for (pp = *v; ; pp++) {
339 if (NULL == (pp = strstr(pp, "Ta")))
340 break;
341 if (pp > *v && ' ' != *(pp - 1))
342 continue;
343 if (' ' == *(pp + 2) || 0 == *(pp + 2))
344 break;
345 }
346
347 /* Choose delimiter tab/Ta. */
348
349 if (p && pp)
350 p = (p < pp ? p : pp);
351 else if ( ! p && pp)
352 p = pp;
353
354 /* Strip delimiter's preceding whitespace. */
355
356 if (p && p > *v) {
357 pp = p - 1;
358 while (pp > *v && ' ' == *pp)
359 pp--;
360 if (pp == *v && ' ' == *pp)
361 *pp = 0;
362 else if (' ' == *pp)
363 *(pp + 1) = 0;
364 }
365
366 /* ...in- and proceding whitespace. */
367
368 if (p && ('\t' != *p)) {
369 *p++ = 0;
370 *p++ = 0;
371 } else if (p)
372 *p++ = 0;
373
374 if (p) {
375 while (' ' == *p)
376 p++;
377 if (0 != *p)
378 *(p - 1) = 0;
379 *pos += (int)(p - *v);
380 }
381
382 if (p && 0 == *p)
383 if ( ! pwarn(mdoc, line, *pos, WCOLEMPTY))
384 return(0);
385 if (p && 0 == *p && p > *v && ' ' == *(p - 1))
386 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
387 return(0);
388
389 if (p)
390 return(ARGS_WORD);
391
392 /* Configure the eoln case, too. */
393
394 p = strchr(*v, 0);
395 assert(p);
396
397 if (p > *v && ' ' == *(p - 1))
398 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
399 return(0);
400 *pos += (int)(p - *v);
401
402 return(ARGS_WORD);
403 }
404
405 /* Do non-tabsep look-ahead here. */
406
407 if ( ! (ARGS_TABSEP & fl))
408 while (buf[*pos]) {
409 if (isspace((int)buf[*pos]))
410 if ('\\' != buf[*pos - 1])
411 break;
412 (*pos)++;
413 }
414
415 if (0 == buf[*pos])
416 return(ARGS_WORD);
417
418 buf[(*pos)++] = 0;
419
420 if (0 == buf[*pos])
421 return(ARGS_WORD);
422
423 if ( ! (ARGS_TABSEP & fl))
424 while (buf[*pos] && isspace((int)buf[*pos]))
425 (*pos)++;
426
427 if (buf[*pos])
428 return(ARGS_WORD);
429
430 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
431 return(ARGS_ERROR);
432
433 return(ARGS_WORD);
434 }
435
436 /*
437 * If we're a quoted string (and quoted strings are allowed),
438 * then parse ahead to the next quote. If none's found, it's an
439 * error. After, parse to the next word.
440 */
441
442 *v = &buf[++(*pos)];
443
444 while (buf[*pos] && '\"' != buf[*pos])
445 (*pos)++;
446
447 if (0 == buf[*pos]) {
448 (void)perr(mdoc, line, *pos, EQUOTTERM);
449 return(ARGS_ERROR);
450 }
451
452 buf[(*pos)++] = 0;
453 if (0 == buf[*pos])
454 return(ARGS_QWORD);
455
456 while (buf[*pos] && isspace((int)buf[*pos]))
457 (*pos)++;
458
459 if (buf[*pos])
460 return(ARGS_QWORD);
461
462 if ( ! pwarn(mdoc, line, *pos, WTAILWS))
463 return(ARGS_ERROR);
464
465 return(ARGS_QWORD);
466 }
467
468
469 static int
470 argv_a2arg(int tok, const char *argv)
471 {
472
473 switch (tok) {
474 case (MDOC_An):
475 if (xstrcmp(argv, "split"))
476 return(MDOC_Split);
477 else if (xstrcmp(argv, "nosplit"))
478 return(MDOC_Nosplit);
479 break;
480
481 case (MDOC_Bd):
482 if (xstrcmp(argv, "ragged"))
483 return(MDOC_Ragged);
484 else if (xstrcmp(argv, "unfilled"))
485 return(MDOC_Unfilled);
486 else if (xstrcmp(argv, "filled"))
487 return(MDOC_Filled);
488 else if (xstrcmp(argv, "literal"))
489 return(MDOC_Literal);
490 else if (xstrcmp(argv, "file"))
491 return(MDOC_File);
492 else if (xstrcmp(argv, "offset"))
493 return(MDOC_Offset);
494 break;
495
496 case (MDOC_Bf):
497 if (xstrcmp(argv, "emphasis"))
498 return(MDOC_Emphasis);
499 else if (xstrcmp(argv, "literal"))
500 return(MDOC_Literal);
501 else if (xstrcmp(argv, "symbolic"))
502 return(MDOC_Symbolic);
503 break;
504
505 case (MDOC_Bk):
506 if (xstrcmp(argv, "words"))
507 return(MDOC_Words);
508 break;
509
510 case (MDOC_Bl):
511 if (xstrcmp(argv, "bullet"))
512 return(MDOC_Bullet);
513 else if (xstrcmp(argv, "dash"))
514 return(MDOC_Dash);
515 else if (xstrcmp(argv, "hyphen"))
516 return(MDOC_Hyphen);
517 else if (xstrcmp(argv, "item"))
518 return(MDOC_Item);
519 else if (xstrcmp(argv, "enum"))
520 return(MDOC_Enum);
521 else if (xstrcmp(argv, "tag"))
522 return(MDOC_Tag);
523 else if (xstrcmp(argv, "diag"))
524 return(MDOC_Diag);
525 else if (xstrcmp(argv, "hang"))
526 return(MDOC_Hang);
527 else if (xstrcmp(argv, "ohang"))
528 return(MDOC_Ohang);
529 else if (xstrcmp(argv, "inset"))
530 return(MDOC_Inset);
531 else if (xstrcmp(argv, "column"))
532 return(MDOC_Column);
533 else if (xstrcmp(argv, "width"))
534 return(MDOC_Width);
535 else if (xstrcmp(argv, "offset"))
536 return(MDOC_Offset);
537 else if (xstrcmp(argv, "compact"))
538 return(MDOC_Compact);
539 break;
540
541 case (MDOC_Rv):
542 /* FALLTHROUGH */
543 case (MDOC_Ex):
544 if (xstrcmp(argv, "std"))
545 return(MDOC_Std);
546 break;
547
548 case (MDOC_St):
549 if (xstrcmp(argv, "p1003.1-88"))
550 return(MDOC_p1003_1_88);
551 else if (xstrcmp(argv, "p1003.1-90"))
552 return(MDOC_p1003_1_90);
553 else if (xstrcmp(argv, "p1003.1-96"))
554 return(MDOC_p1003_1_96);
555 else if (xstrcmp(argv, "p1003.1-2001"))
556 return(MDOC_p1003_1_2001);
557 else if (xstrcmp(argv, "p1003.1-2004"))
558 return(MDOC_p1003_1_2004);
559 else if (xstrcmp(argv, "p1003.1"))
560 return(MDOC_p1003_1);
561 else if (xstrcmp(argv, "p1003.1b"))
562 return(MDOC_p1003_1b);
563 else if (xstrcmp(argv, "p1003.1b-93"))
564 return(MDOC_p1003_1b_93);
565 else if (xstrcmp(argv, "p1003.1c-95"))
566 return(MDOC_p1003_1c_95);
567 else if (xstrcmp(argv, "p1003.1g-2000"))
568 return(MDOC_p1003_1g_2000);
569 else if (xstrcmp(argv, "p1003.2-92"))
570 return(MDOC_p1003_2_92);
571 else if (xstrcmp(argv, "p1003.2-95"))
572 return(MDOC_p1387_2_95);
573 else if (xstrcmp(argv, "p1003.2"))
574 return(MDOC_p1003_2);
575 else if (xstrcmp(argv, "p1387.2-95"))
576 return(MDOC_p1387_2);
577 else if (xstrcmp(argv, "isoC-90"))
578 return(MDOC_isoC_90);
579 else if (xstrcmp(argv, "isoC-amd1"))
580 return(MDOC_isoC_amd1);
581 else if (xstrcmp(argv, "isoC-tcor1"))
582 return(MDOC_isoC_tcor1);
583 else if (xstrcmp(argv, "isoC-tcor2"))
584 return(MDOC_isoC_tcor2);
585 else if (xstrcmp(argv, "isoC-99"))
586 return(MDOC_isoC_99);
587 else if (xstrcmp(argv, "ansiC"))
588 return(MDOC_ansiC);
589 else if (xstrcmp(argv, "ansiC-89"))
590 return(MDOC_ansiC_89);
591 else if (xstrcmp(argv, "ansiC-99"))
592 return(MDOC_ansiC_99);
593 else if (xstrcmp(argv, "ieee754"))
594 return(MDOC_ieee754);
595 else if (xstrcmp(argv, "iso8802-3"))
596 return(MDOC_iso8802_3);
597 else if (xstrcmp(argv, "xpg3"))
598 return(MDOC_xpg3);
599 else if (xstrcmp(argv, "xpg4"))
600 return(MDOC_xpg4);
601 else if (xstrcmp(argv, "xpg4.2"))
602 return(MDOC_xpg4_2);
603 else if (xstrcmp(argv, "xpg4.3"))
604 return(MDOC_xpg4_3);
605 else if (xstrcmp(argv, "xbd5"))
606 return(MDOC_xbd5);
607 else if (xstrcmp(argv, "xcu5"))
608 return(MDOC_xcu5);
609 else if (xstrcmp(argv, "xsh5"))
610 return(MDOC_xsh5);
611 else if (xstrcmp(argv, "xns5"))
612 return(MDOC_xns5);
613 else if (xstrcmp(argv, "xns5.2d2.0"))
614 return(MDOC_xns5_2d2_0);
615 else if (xstrcmp(argv, "xcurses4.2"))
616 return(MDOC_xcurses4_2);
617 else if (xstrcmp(argv, "susv2"))
618 return(MDOC_susv2);
619 else if (xstrcmp(argv, "susv3"))
620 return(MDOC_susv3);
621 else if (xstrcmp(argv, "svid4"))
622 return(MDOC_svid4);
623 break;
624
625 default:
626 break;
627 }
628
629 return(MDOC_ARG_MAX);
630 }
631
632
633 static int
634 argv_multi(struct mdoc *mdoc, int line,
635 struct mdoc_arg *v, int *pos, char *buf)
636 {
637 int c, ppos;
638 char *p;
639
640 v->sz = 0;
641 v->value = xcalloc(MDOC_LINEARG_MAX, sizeof(char *));
642
643 ppos = *pos;
644
645 for (v->sz = 0; v->sz < MDOC_LINEARG_MAX; v->sz++) {
646 if ('-' == buf[*pos])
647 break;
648 c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p);
649 if (ARGS_ERROR == c) {
650 free(v->value);
651 return(0);
652 } else if (ARGS_EOLN == c)
653 break;
654 v->value[(int)v->sz] = p;
655 }
656
657 if (0 < v->sz && v->sz < MDOC_LINEARG_MAX)
658 return(1);
659
660 free(v->value);
661 if (0 == v->sz)
662 return(perr(mdoc, line, ppos, EARGVAL));
663
664 return(perr(mdoc, line, ppos, EARGMANY));
665 }
666
667
668 static int
669 argv_single(struct mdoc *mdoc, int line,
670 struct mdoc_arg *v, int *pos, char *buf)
671 {
672 int c, ppos;
673 char *p;
674
675 ppos = *pos;
676
677 c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p);
678 if (ARGS_ERROR == c)
679 return(0);
680 if (ARGS_EOLN == c)
681 return(perr(mdoc, line, ppos, EARGVAL));
682
683 v->sz = 1;
684 v->value = xcalloc(1, sizeof(char *));
685 v->value[0] = p;
686 return(1);
687 }
688
689
690 static int
691 argv(struct mdoc *mdoc, int line,
692 struct mdoc_arg *v, int *pos, char *buf)
693 {
694
695 v->sz = 0;
696 v->value = NULL;
697
698 switch (v->arg) {
699 case(MDOC_Std):
700 /* FALLTHROUGH */
701 case(MDOC_Width):
702 /* FALLTHROUGH */
703 case(MDOC_Offset):
704 return(argv_single(mdoc, line, v, pos, buf));
705 case(MDOC_Column):
706 return(argv_multi(mdoc, line, v, pos, buf));
707 default:
708 break;
709 }
710
711 return(1);
712 }
713
714
715 int
716 mdoc_argv(struct mdoc *mdoc, int line, int tok,
717 struct mdoc_arg *v, int *pos, char *buf)
718 {
719 int i;
720 char *p;
721
722 (void)memset(v, 0, sizeof(struct mdoc_arg));
723
724 if (0 == buf[*pos])
725 return(ARGV_EOLN);
726
727 assert( ! isspace((int)buf[*pos]));
728
729 if ('-' != buf[*pos])
730 return(ARGV_WORD);
731
732 i = *pos;
733 p = &buf[++(*pos)];
734
735 v->line = line;
736 v->pos = *pos;
737
738 assert(*pos > 0);
739
740 /* LINTED */
741 while (buf[*pos]) {
742 if (isspace((int)buf[*pos]))
743 if ('\\' != buf[*pos - 1])
744 break;
745 (*pos)++;
746 }
747
748 if (buf[*pos])
749 buf[(*pos)++] = 0;
750
751 if (MDOC_ARG_MAX == (v->arg = argv_a2arg(tok, p))) {
752 if ( ! pwarn(mdoc, line, i, WARGVPARM))
753 return(ARGV_ERROR);
754 return(ARGV_WORD);
755 }
756
757 while (buf[*pos] && isspace((int)buf[*pos]))
758 (*pos)++;
759
760 /* FIXME: whitespace if no value. */
761
762 if ( ! argv(mdoc, line, v, pos, buf))
763 return(ARGV_ERROR);
764
765 return(ARGV_ARG);
766 }
767
768
769 void
770 mdoc_argv_free(int sz, struct mdoc_arg *arg)
771 {
772 int i;
773
774 for (i = 0; i < sz; i++) {
775 if (0 == arg[i].sz) {
776 assert(NULL == arg[i].value);
777 continue;
778 }
779 assert(arg[i].value);
780 free(arg[i].value);
781 }
782 }
783