]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
Simplify the code by making various mdoc parser helper functions void.
[mandoc.git] / mdoc_argv.c
1 /* $OpenBSD$ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "mdoc.h"
28 #include "mandoc.h"
29 #include "mandoc_aux.h"
30 #include "libmdoc.h"
31 #include "libmandoc.h"
32
33 #define MULTI_STEP 5 /* pre-allocate argument values */
34 #define DELIMSZ 6 /* max possible size of a delimiter */
35
36 enum argsflag {
37 ARGSFL_NONE = 0,
38 ARGSFL_DELIM, /* handle delimiters of [[::delim::][ ]+]+ */
39 ARGSFL_TABSEP /* handle tab/`Ta' separated phrases */
40 };
41
42 enum argvflag {
43 ARGV_NONE, /* no args to flag (e.g., -split) */
44 ARGV_SINGLE, /* one arg to flag (e.g., -file xxx) */
45 ARGV_MULTI /* multiple args (e.g., -column xxx yyy) */
46 };
47
48 struct mdocarg {
49 enum argsflag flags;
50 const enum mdocargt *argvs;
51 };
52
53 static void argn_free(struct mdoc_arg *, int);
54 static enum margserr args(struct mdoc *, int, int *,
55 char *, enum argsflag, char **);
56 static int args_checkpunct(const char *, int);
57 static void argv_multi(struct mdoc *, int,
58 struct mdoc_argv *, int *, char *);
59 static void argv_single(struct mdoc *, int,
60 struct mdoc_argv *, int *, char *);
61
62 static const enum argvflag 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_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 const enum mdocargt args_Ex[] = {
92 MDOC_Std,
93 MDOC_ARG_MAX
94 };
95
96 static const enum mdocargt args_An[] = {
97 MDOC_Split,
98 MDOC_Nosplit,
99 MDOC_ARG_MAX
100 };
101
102 static const enum mdocargt args_Bd[] = {
103 MDOC_Ragged,
104 MDOC_Unfilled,
105 MDOC_Filled,
106 MDOC_Literal,
107 MDOC_File,
108 MDOC_Offset,
109 MDOC_Compact,
110 MDOC_Centred,
111 MDOC_ARG_MAX
112 };
113
114 static const enum mdocargt args_Bf[] = {
115 MDOC_Emphasis,
116 MDOC_Literal,
117 MDOC_Symbolic,
118 MDOC_ARG_MAX
119 };
120
121 static const enum mdocargt args_Bk[] = {
122 MDOC_Words,
123 MDOC_ARG_MAX
124 };
125
126 static const enum mdocargt args_Bl[] = {
127 MDOC_Bullet,
128 MDOC_Dash,
129 MDOC_Hyphen,
130 MDOC_Item,
131 MDOC_Enum,
132 MDOC_Tag,
133 MDOC_Diag,
134 MDOC_Hang,
135 MDOC_Ohang,
136 MDOC_Inset,
137 MDOC_Column,
138 MDOC_Width,
139 MDOC_Offset,
140 MDOC_Compact,
141 MDOC_Nested,
142 MDOC_ARG_MAX
143 };
144
145 static const struct mdocarg mdocargs[MDOC_MAX] = {
146 { ARGSFL_DELIM, NULL }, /* Ap */
147 { ARGSFL_NONE, NULL }, /* Dd */
148 { ARGSFL_NONE, NULL }, /* Dt */
149 { ARGSFL_NONE, NULL }, /* Os */
150 { ARGSFL_NONE, NULL }, /* Sh */
151 { ARGSFL_NONE, NULL }, /* Ss */
152 { ARGSFL_NONE, NULL }, /* Pp */
153 { ARGSFL_DELIM, NULL }, /* D1 */
154 { ARGSFL_DELIM, NULL }, /* Dl */
155 { ARGSFL_NONE, args_Bd }, /* Bd */
156 { ARGSFL_NONE, NULL }, /* Ed */
157 { ARGSFL_NONE, args_Bl }, /* Bl */
158 { ARGSFL_NONE, NULL }, /* El */
159 { ARGSFL_NONE, NULL }, /* It */
160 { ARGSFL_DELIM, NULL }, /* Ad */
161 { ARGSFL_DELIM, args_An }, /* An */
162 { ARGSFL_DELIM, NULL }, /* Ar */
163 { ARGSFL_DELIM, NULL }, /* Cd */
164 { ARGSFL_DELIM, NULL }, /* Cm */
165 { ARGSFL_DELIM, NULL }, /* Dv */
166 { ARGSFL_DELIM, NULL }, /* Er */
167 { ARGSFL_DELIM, NULL }, /* Ev */
168 { ARGSFL_NONE, args_Ex }, /* Ex */
169 { ARGSFL_DELIM, NULL }, /* Fa */
170 { ARGSFL_NONE, NULL }, /* Fd */
171 { ARGSFL_DELIM, NULL }, /* Fl */
172 { ARGSFL_DELIM, NULL }, /* Fn */
173 { ARGSFL_DELIM, NULL }, /* Ft */
174 { ARGSFL_DELIM, NULL }, /* Ic */
175 { ARGSFL_DELIM, NULL }, /* In */
176 { ARGSFL_DELIM, NULL }, /* Li */
177 { ARGSFL_NONE, NULL }, /* Nd */
178 { ARGSFL_DELIM, NULL }, /* Nm */
179 { ARGSFL_DELIM, NULL }, /* Op */
180 { ARGSFL_DELIM, NULL }, /* Ot */
181 { ARGSFL_DELIM, NULL }, /* Pa */
182 { ARGSFL_NONE, args_Ex }, /* Rv */
183 { ARGSFL_DELIM, NULL }, /* St */
184 { ARGSFL_DELIM, NULL }, /* Va */
185 { ARGSFL_DELIM, NULL }, /* Vt */
186 { ARGSFL_DELIM, NULL }, /* Xr */
187 { ARGSFL_NONE, NULL }, /* %A */
188 { ARGSFL_NONE, NULL }, /* %B */
189 { ARGSFL_NONE, NULL }, /* %D */
190 { ARGSFL_NONE, NULL }, /* %I */
191 { ARGSFL_NONE, NULL }, /* %J */
192 { ARGSFL_NONE, NULL }, /* %N */
193 { ARGSFL_NONE, NULL }, /* %O */
194 { ARGSFL_NONE, NULL }, /* %P */
195 { ARGSFL_NONE, NULL }, /* %R */
196 { ARGSFL_NONE, NULL }, /* %T */
197 { ARGSFL_NONE, NULL }, /* %V */
198 { ARGSFL_DELIM, NULL }, /* Ac */
199 { ARGSFL_NONE, NULL }, /* Ao */
200 { ARGSFL_DELIM, NULL }, /* Aq */
201 { ARGSFL_DELIM, NULL }, /* At */
202 { ARGSFL_DELIM, NULL }, /* Bc */
203 { ARGSFL_NONE, args_Bf }, /* Bf */
204 { ARGSFL_NONE, NULL }, /* Bo */
205 { ARGSFL_DELIM, NULL }, /* Bq */
206 { ARGSFL_DELIM, NULL }, /* Bsx */
207 { ARGSFL_DELIM, NULL }, /* Bx */
208 { ARGSFL_NONE, NULL }, /* Db */
209 { ARGSFL_DELIM, NULL }, /* Dc */
210 { ARGSFL_NONE, NULL }, /* Do */
211 { ARGSFL_DELIM, NULL }, /* Dq */
212 { ARGSFL_DELIM, NULL }, /* Ec */
213 { ARGSFL_NONE, NULL }, /* Ef */
214 { ARGSFL_DELIM, NULL }, /* Em */
215 { ARGSFL_NONE, NULL }, /* Eo */
216 { ARGSFL_DELIM, NULL }, /* Fx */
217 { ARGSFL_DELIM, NULL }, /* Ms */
218 { ARGSFL_DELIM, NULL }, /* No */
219 { ARGSFL_DELIM, NULL }, /* Ns */
220 { ARGSFL_DELIM, NULL }, /* Nx */
221 { ARGSFL_DELIM, NULL }, /* Ox */
222 { ARGSFL_DELIM, NULL }, /* Pc */
223 { ARGSFL_DELIM, NULL }, /* Pf */
224 { ARGSFL_NONE, NULL }, /* Po */
225 { ARGSFL_DELIM, NULL }, /* Pq */
226 { ARGSFL_DELIM, NULL }, /* Qc */
227 { ARGSFL_DELIM, NULL }, /* Ql */
228 { ARGSFL_NONE, NULL }, /* Qo */
229 { ARGSFL_DELIM, NULL }, /* Qq */
230 { ARGSFL_NONE, NULL }, /* Re */
231 { ARGSFL_NONE, NULL }, /* Rs */
232 { ARGSFL_DELIM, NULL }, /* Sc */
233 { ARGSFL_NONE, NULL }, /* So */
234 { ARGSFL_DELIM, NULL }, /* Sq */
235 { ARGSFL_NONE, NULL }, /* Sm */
236 { ARGSFL_DELIM, NULL }, /* Sx */
237 { ARGSFL_DELIM, NULL }, /* Sy */
238 { ARGSFL_DELIM, NULL }, /* Tn */
239 { ARGSFL_DELIM, NULL }, /* Ux */
240 { ARGSFL_DELIM, NULL }, /* Xc */
241 { ARGSFL_NONE, NULL }, /* Xo */
242 { ARGSFL_NONE, NULL }, /* Fo */
243 { ARGSFL_DELIM, NULL }, /* Fc */
244 { ARGSFL_NONE, NULL }, /* Oo */
245 { ARGSFL_DELIM, NULL }, /* Oc */
246 { ARGSFL_NONE, args_Bk }, /* Bk */
247 { ARGSFL_NONE, NULL }, /* Ek */
248 { ARGSFL_NONE, NULL }, /* Bt */
249 { ARGSFL_NONE, NULL }, /* Hf */
250 { ARGSFL_DELIM, NULL }, /* Fr */
251 { ARGSFL_NONE, NULL }, /* Ud */
252 { ARGSFL_DELIM, NULL }, /* Lb */
253 { ARGSFL_NONE, NULL }, /* Lp */
254 { ARGSFL_DELIM, NULL }, /* Lk */
255 { ARGSFL_DELIM, NULL }, /* Mt */
256 { ARGSFL_DELIM, NULL }, /* Brq */
257 { ARGSFL_NONE, NULL }, /* Bro */
258 { ARGSFL_DELIM, NULL }, /* Brc */
259 { ARGSFL_NONE, NULL }, /* %C */
260 { ARGSFL_NONE, NULL }, /* Es */
261 { ARGSFL_DELIM, NULL }, /* En */
262 { ARGSFL_DELIM, NULL }, /* Dx */
263 { ARGSFL_NONE, NULL }, /* %Q */
264 { ARGSFL_NONE, NULL }, /* br */
265 { ARGSFL_NONE, NULL }, /* sp */
266 { ARGSFL_NONE, NULL }, /* %U */
267 { ARGSFL_NONE, NULL }, /* Ta */
268 { ARGSFL_NONE, NULL }, /* ll */
269 };
270
271
272 /*
273 * Parse an argument from line text. This comes in the form of -key
274 * [value0...], which may either have a single mandatory value, at least
275 * one mandatory value, an optional single value, or no value.
276 */
277 enum margverr
278 mdoc_argv(struct mdoc *mdoc, int line, enum mdoct tok,
279 struct mdoc_arg **v, int *pos, char *buf)
280 {
281 char *p, sv;
282 struct mdoc_argv tmp;
283 struct mdoc_arg *arg;
284 const enum mdocargt *ap;
285
286 if ('\0' == buf[*pos])
287 return(ARGV_EOLN);
288 else if (NULL == (ap = mdocargs[tok].argvs))
289 return(ARGV_WORD);
290 else if ('-' != buf[*pos])
291 return(ARGV_WORD);
292
293 /* Seek to the first unescaped space. */
294
295 p = &buf[++(*pos)];
296
297 assert(*pos > 0);
298
299 for ( ; buf[*pos] ; (*pos)++)
300 if (' ' == buf[*pos] && '\\' != buf[*pos - 1])
301 break;
302
303 /*
304 * We want to nil-terminate the word to look it up (it's easier
305 * that way). But we may not have a flag, in which case we need
306 * to restore the line as-is. So keep around the stray byte,
307 * which we'll reset upon exiting (if necessary).
308 */
309
310 if ('\0' != (sv = buf[*pos]))
311 buf[(*pos)++] = '\0';
312
313 /*
314 * Now look up the word as a flag. Use temporary storage that
315 * we'll copy into the node's flags, if necessary.
316 */
317
318 memset(&tmp, 0, sizeof(struct mdoc_argv));
319
320 tmp.line = line;
321 tmp.pos = *pos;
322 tmp.arg = MDOC_ARG_MAX;
323
324 while (MDOC_ARG_MAX != (tmp.arg = *ap++))
325 if (0 == strcmp(p, mdoc_argnames[tmp.arg]))
326 break;
327
328 if (MDOC_ARG_MAX == tmp.arg) {
329 /*
330 * The flag was not found.
331 * Restore saved zeroed byte and return as a word.
332 */
333 if (sv)
334 buf[*pos - 1] = sv;
335 return(ARGV_WORD);
336 }
337
338 /* Read to the next word (the argument). */
339
340 while (buf[*pos] && ' ' == buf[*pos])
341 (*pos)++;
342
343 switch (argvflags[tmp.arg]) {
344 case ARGV_SINGLE:
345 argv_single(mdoc, line, &tmp, pos, buf);
346 break;
347 case ARGV_MULTI:
348 argv_multi(mdoc, line, &tmp, pos, buf);
349 break;
350 case ARGV_NONE:
351 break;
352 }
353
354 if (NULL == (arg = *v))
355 arg = *v = mandoc_calloc(1, sizeof(struct mdoc_arg));
356
357 arg->argc++;
358 arg->argv = mandoc_reallocarray(arg->argv,
359 arg->argc, sizeof(struct mdoc_argv));
360
361 memcpy(&arg->argv[(int)arg->argc - 1], &tmp,
362 sizeof(struct mdoc_argv));
363
364 return(ARGV_ARG);
365 }
366
367 void
368 mdoc_argv_free(struct mdoc_arg *p)
369 {
370 int i;
371
372 if (NULL == p)
373 return;
374
375 if (p->refcnt) {
376 --(p->refcnt);
377 if (p->refcnt)
378 return;
379 }
380 assert(p->argc);
381
382 for (i = (int)p->argc - 1; i >= 0; i--)
383 argn_free(p, i);
384
385 free(p->argv);
386 free(p);
387 }
388
389 static void
390 argn_free(struct mdoc_arg *p, int iarg)
391 {
392 struct mdoc_argv *arg;
393 int j;
394
395 arg = &p->argv[iarg];
396
397 if (arg->sz && arg->value) {
398 for (j = (int)arg->sz - 1; j >= 0; j--)
399 free(arg->value[j]);
400 free(arg->value);
401 }
402
403 for (--p->argc; iarg < (int)p->argc; iarg++)
404 p->argv[iarg] = p->argv[iarg+1];
405 }
406
407 enum margserr
408 mdoc_zargs(struct mdoc *mdoc, int line, int *pos, char *buf, char **v)
409 {
410
411 return(args(mdoc, line, pos, buf, ARGSFL_NONE, v));
412 }
413
414 enum margserr
415 mdoc_args(struct mdoc *mdoc, int line, int *pos,
416 char *buf, enum mdoct tok, char **v)
417 {
418 enum argsflag fl;
419 struct mdoc_node *n;
420
421 fl = mdocargs[tok].flags;
422
423 if (MDOC_It != tok)
424 return(args(mdoc, line, pos, buf, fl, v));
425
426 /*
427 * We know that we're in an `It', so it's reasonable to expect
428 * us to be sitting in a `Bl'. Someday this may not be the case
429 * (if we allow random `It's sitting out there), so provide a
430 * safe fall-back into the default behaviour.
431 */
432
433 for (n = mdoc->last; n; n = n->parent)
434 if (MDOC_Bl == n->tok)
435 if (LIST_column == n->norm->Bl.type) {
436 fl = ARGSFL_TABSEP;
437 break;
438 }
439
440 return(args(mdoc, line, pos, buf, fl, v));
441 }
442
443 static enum margserr
444 args(struct mdoc *mdoc, int line, int *pos,
445 char *buf, enum argsflag fl, char **v)
446 {
447 char *p, *pp;
448 int pairs;
449 enum margserr rc;
450
451 if ('\0' == buf[*pos]) {
452 if (MDOC_PPHRASE & mdoc->flags)
453 return(ARGS_EOLN);
454 /*
455 * If we're not in a partial phrase and the flag for
456 * being a phrase literal is still set, the punctuation
457 * is unterminated.
458 */
459 if (MDOC_PHRASELIT & mdoc->flags)
460 mandoc_msg(MANDOCERR_ARG_QUOTE,
461 mdoc->parse, line, *pos, NULL);
462
463 mdoc->flags &= ~MDOC_PHRASELIT;
464 return(ARGS_EOLN);
465 }
466
467 *v = &buf[*pos];
468
469 if (ARGSFL_DELIM == fl)
470 if (args_checkpunct(buf, *pos))
471 return(ARGS_PUNCT);
472
473 /*
474 * First handle TABSEP items, restricted to `Bl -column'. This
475 * ignores conventional token parsing and instead uses tabs or
476 * `Ta' macros to separate phrases. Phrases are parsed again
477 * for arguments at a later phase.
478 */
479
480 if (ARGSFL_TABSEP == fl) {
481 /* Scan ahead to tab (can't be escaped). */
482 p = strchr(*v, '\t');
483 pp = NULL;
484
485 /* Scan ahead to unescaped `Ta'. */
486 if ( ! (MDOC_PHRASELIT & mdoc->flags))
487 for (pp = *v; ; pp++) {
488 if (NULL == (pp = strstr(pp, "Ta")))
489 break;
490 if (pp > *v && ' ' != *(pp - 1))
491 continue;
492 if (' ' == *(pp + 2) || '\0' == *(pp + 2))
493 break;
494 }
495
496 /* By default, assume a phrase. */
497 rc = ARGS_PHRASE;
498
499 /*
500 * Adjust new-buffer position to be beyond delimiter
501 * mark (e.g., Ta -> end + 2).
502 */
503 if (p && pp) {
504 *pos += pp < p ? 2 : 1;
505 rc = pp < p ? ARGS_PHRASE : ARGS_PPHRASE;
506 p = pp < p ? pp : p;
507 } else if (p && ! pp) {
508 rc = ARGS_PPHRASE;
509 *pos += 1;
510 } else if (pp && ! p) {
511 p = pp;
512 *pos += 2;
513 } else {
514 rc = ARGS_PEND;
515 p = strchr(*v, 0);
516 }
517
518 /* Whitespace check for eoln case... */
519 if ('\0' == *p && ' ' == *(p - 1))
520 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
521 line, *pos, NULL);
522
523 *pos += (int)(p - *v);
524
525 /* Strip delimiter's preceding whitespace. */
526 pp = p - 1;
527 while (pp > *v && ' ' == *pp) {
528 if (pp > *v && '\\' == *(pp - 1))
529 break;
530 pp--;
531 }
532 *(pp + 1) = 0;
533
534 /* Strip delimiter's proceeding whitespace. */
535 for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)
536 /* Skip ahead. */ ;
537
538 return(rc);
539 }
540
541 /*
542 * Process a quoted literal. A quote begins with a double-quote
543 * and ends with a double-quote NOT preceded by a double-quote.
544 * NUL-terminate the literal in place.
545 * Collapse pairs of quotes inside quoted literals.
546 * Whitespace is NOT involved in literal termination.
547 */
548
549 if (MDOC_PHRASELIT & mdoc->flags || '\"' == buf[*pos]) {
550 if ( ! (MDOC_PHRASELIT & mdoc->flags))
551 *v = &buf[++(*pos)];
552
553 if (MDOC_PPHRASE & mdoc->flags)
554 mdoc->flags |= MDOC_PHRASELIT;
555
556 pairs = 0;
557 for ( ; buf[*pos]; (*pos)++) {
558 /* Move following text left after quoted quotes. */
559 if (pairs)
560 buf[*pos - pairs] = buf[*pos];
561 if ('\"' != buf[*pos])
562 continue;
563 /* Unquoted quotes end quoted args. */
564 if ('\"' != buf[*pos + 1])
565 break;
566 /* Quoted quotes collapse. */
567 pairs++;
568 (*pos)++;
569 }
570 if (pairs)
571 buf[*pos - pairs] = '\0';
572
573 if ('\0' == buf[*pos]) {
574 if (MDOC_PPHRASE & mdoc->flags)
575 return(ARGS_QWORD);
576 mandoc_msg(MANDOCERR_ARG_QUOTE,
577 mdoc->parse, line, *pos, NULL);
578 return(ARGS_QWORD);
579 }
580
581 mdoc->flags &= ~MDOC_PHRASELIT;
582 buf[(*pos)++] = '\0';
583
584 if ('\0' == buf[*pos])
585 return(ARGS_QWORD);
586
587 while (' ' == buf[*pos])
588 (*pos)++;
589
590 if ('\0' == buf[*pos])
591 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
592 line, *pos, NULL);
593
594 return(ARGS_QWORD);
595 }
596
597 p = &buf[*pos];
598 *v = mandoc_getarg(mdoc->parse, &p, line, pos);
599
600 return(ARGS_WORD);
601 }
602
603 /*
604 * Check if the string consists only of space-separated closing
605 * delimiters. This is a bit of a dance: the first must be a close
606 * delimiter, but it may be followed by middle delimiters. Arbitrary
607 * whitespace may separate these tokens.
608 */
609 static int
610 args_checkpunct(const char *buf, int i)
611 {
612 int j;
613 char dbuf[DELIMSZ];
614 enum mdelim d;
615
616 /* First token must be a close-delimiter. */
617
618 for (j = 0; buf[i] && ' ' != buf[i] && j < DELIMSZ; j++, i++)
619 dbuf[j] = buf[i];
620
621 if (DELIMSZ == j)
622 return(0);
623
624 dbuf[j] = '\0';
625 if (DELIM_CLOSE != mdoc_isdelim(dbuf))
626 return(0);
627
628 while (' ' == buf[i])
629 i++;
630
631 /* Remaining must NOT be open/none. */
632
633 while (buf[i]) {
634 j = 0;
635 while (buf[i] && ' ' != buf[i] && j < DELIMSZ)
636 dbuf[j++] = buf[i++];
637
638 if (DELIMSZ == j)
639 return(0);
640
641 dbuf[j] = '\0';
642 d = mdoc_isdelim(dbuf);
643 if (DELIM_NONE == d || DELIM_OPEN == d)
644 return(0);
645
646 while (' ' == buf[i])
647 i++;
648 }
649
650 return('\0' == buf[i]);
651 }
652
653 static void
654 argv_multi(struct mdoc *mdoc, int line,
655 struct mdoc_argv *v, int *pos, char *buf)
656 {
657 enum margserr ac;
658 char *p;
659
660 for (v->sz = 0; ; v->sz++) {
661 if (buf[*pos] == '-')
662 break;
663 ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
664 if (ac == ARGS_EOLN)
665 break;
666
667 if (v->sz % MULTI_STEP == 0)
668 v->value = mandoc_reallocarray(v->value,
669 v->sz + MULTI_STEP, sizeof(char *));
670
671 v->value[(int)v->sz] = mandoc_strdup(p);
672 }
673 }
674
675 static void
676 argv_single(struct mdoc *mdoc, int line,
677 struct mdoc_argv *v, int *pos, char *buf)
678 {
679 enum margserr ac;
680 char *p;
681
682 ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
683 if (ac == ARGS_EOLN)
684 return;
685
686 v->sz = 1;
687 v->value = mandoc_malloc(sizeof(char *));
688 v->value[0] = mandoc_strdup(p);
689 }