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