]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
The -width and -offset macro arguments consume the next argument
[mandoc.git] / mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.83 2012/04/15 15:26:49 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "mdoc.h"
29 #include "mandoc.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 int argv_multi(struct mdoc *, int,
58 struct mdoc_argv *, int *, char *);
59 static int 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_NONE, 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_NONE, 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_NONE, NULL }, /* In */
176 { ARGSFL_DELIM, NULL }, /* Li */
177 { ARGSFL_NONE, NULL }, /* Nd */
178 { ARGSFL_DELIM, NULL }, /* Nm */
179 { ARGSFL_DELIM, NULL }, /* Op */
180 { ARGSFL_NONE, 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_NONE, 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_NONE, NULL }, /* Fr */
251 { ARGSFL_NONE, NULL }, /* Ud */
252 { ARGSFL_NONE, 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_NONE, NULL }, /* En */
262 { ARGSFL_NONE, 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 };
269
270
271 /*
272 * Parse an argument from line text. This comes in the form of -key
273 * [value0...], which may either have a single mandatory value, at least
274 * one mandatory value, an optional single value, or no value.
275 */
276 enum margverr
277 mdoc_argv(struct mdoc *m, int line, enum mdoct tok,
278 struct mdoc_arg **v, int *pos, char *buf)
279 {
280 char *p, sv;
281 struct mdoc_argv tmp;
282 struct mdoc_arg *arg;
283 const enum mdocargt *ap;
284
285 if ('\0' == buf[*pos])
286 return(ARGV_EOLN);
287 else if (NULL == (ap = mdocargs[tok].argvs))
288 return(ARGV_WORD);
289 else if ('-' != buf[*pos])
290 return(ARGV_WORD);
291
292 /* Seek to the first unescaped space. */
293
294 p = &buf[++(*pos)];
295
296 assert(*pos > 0);
297
298 for ( ; buf[*pos] ; (*pos)++)
299 if (' ' == buf[*pos] && '\\' != buf[*pos - 1])
300 break;
301
302 /*
303 * We want to nil-terminate the word to look it up (it's easier
304 * that way). But we may not have a flag, in which case we need
305 * to restore the line as-is. So keep around the stray byte,
306 * which we'll reset upon exiting (if necessary).
307 */
308
309 if ('\0' != (sv = buf[*pos]))
310 buf[(*pos)++] = '\0';
311
312 /*
313 * Now look up the word as a flag. Use temporary storage that
314 * we'll copy into the node's flags, if necessary.
315 */
316
317 memset(&tmp, 0, sizeof(struct mdoc_argv));
318
319 tmp.line = line;
320 tmp.pos = *pos;
321 tmp.arg = MDOC_ARG_MAX;
322
323 while (MDOC_ARG_MAX != (tmp.arg = *ap++))
324 if (0 == strcmp(p, mdoc_argnames[tmp.arg]))
325 break;
326
327 if (MDOC_ARG_MAX == tmp.arg) {
328 /*
329 * The flag was not found.
330 * Restore saved zeroed byte and return as a word.
331 */
332 if (sv)
333 buf[*pos - 1] = sv;
334 return(ARGV_WORD);
335 }
336
337 /* Read to the next word (the argument). */
338
339 while (buf[*pos] && ' ' == buf[*pos])
340 (*pos)++;
341
342 switch (argvflags[tmp.arg]) {
343 case (ARGV_SINGLE):
344 if ( ! argv_single(m, line, &tmp, pos, buf))
345 return(ARGV_ERROR);
346 break;
347 case (ARGV_MULTI):
348 if ( ! argv_multi(m, line, &tmp, pos, buf))
349 return(ARGV_ERROR);
350 break;
351 case (ARGV_NONE):
352 break;
353 }
354
355 if (NULL == (arg = *v))
356 arg = *v = mandoc_calloc(1, sizeof(struct mdoc_arg));
357
358 arg->argc++;
359 arg->argv = mandoc_realloc
360 (arg->argv, arg->argc * sizeof(struct mdoc_argv));
361
362 memcpy(&arg->argv[(int)arg->argc - 1],
363 &tmp, sizeof(struct mdoc_argv));
364
365 return(ARGV_ARG);
366 }
367
368 void
369 mdoc_argv_free(struct mdoc_arg *p)
370 {
371 int i;
372
373 if (NULL == p)
374 return;
375
376 if (p->refcnt) {
377 --(p->refcnt);
378 if (p->refcnt)
379 return;
380 }
381 assert(p->argc);
382
383 for (i = (int)p->argc - 1; i >= 0; i--)
384 argn_free(p, i);
385
386 free(p->argv);
387 free(p);
388 }
389
390 static void
391 argn_free(struct mdoc_arg *p, int iarg)
392 {
393 struct mdoc_argv *arg;
394 int j;
395
396 arg = &p->argv[iarg];
397
398 if (arg->sz && arg->value) {
399 for (j = (int)arg->sz - 1; j >= 0; j--)
400 free(arg->value[j]);
401 free(arg->value);
402 }
403
404 for (--p->argc; iarg < (int)p->argc; iarg++)
405 p->argv[iarg] = p->argv[iarg+1];
406 }
407
408 enum margserr
409 mdoc_zargs(struct mdoc *m, int line, int *pos, char *buf, char **v)
410 {
411
412 return(args(m, line, pos, buf, ARGSFL_NONE, v));
413 }
414
415 enum margserr
416 mdoc_args(struct mdoc *m, int line, int *pos,
417 char *buf, enum mdoct tok, char **v)
418 {
419 enum argsflag fl;
420 struct mdoc_node *n;
421
422 fl = mdocargs[tok].flags;
423
424 if (MDOC_It != tok)
425 return(args(m, line, pos, buf, fl, v));
426
427 /*
428 * We know that we're in an `It', so it's reasonable to expect
429 * us to be sitting in a `Bl'. Someday this may not be the case
430 * (if we allow random `It's sitting out there), so provide a
431 * safe fall-back into the default behaviour.
432 */
433
434 for (n = m->last; n; n = n->parent)
435 if (MDOC_Bl == n->tok)
436 if (LIST_column == n->norm->Bl.type) {
437 fl = ARGSFL_TABSEP;
438 break;
439 }
440
441 return(args(m, line, pos, buf, fl, v));
442 }
443
444 static enum margserr
445 args(struct mdoc *m, int line, int *pos,
446 char *buf, enum argsflag fl, char **v)
447 {
448 char *p, *pp;
449 enum margserr rc;
450
451 if ('\0' == buf[*pos]) {
452 if (MDOC_PPHRASE & m->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 & m->flags)
460 mdoc_pmsg(m, line, *pos, MANDOCERR_BADQUOTE);
461
462 m->flags &= ~MDOC_PHRASELIT;
463 return(ARGS_EOLN);
464 }
465
466 *v = &buf[*pos];
467
468 if (ARGSFL_DELIM == fl)
469 if (args_checkpunct(buf, *pos))
470 return(ARGS_PUNCT);
471
472 /*
473 * First handle TABSEP items, restricted to `Bl -column'. This
474 * ignores conventional token parsing and instead uses tabs or
475 * `Ta' macros to separate phrases. Phrases are parsed again
476 * for arguments at a later phase.
477 */
478
479 if (ARGSFL_TABSEP == fl) {
480 /* Scan ahead to tab (can't be escaped). */
481 p = strchr(*v, '\t');
482 pp = NULL;
483
484 /* Scan ahead to unescaped `Ta'. */
485 if ( ! (MDOC_PHRASELIT & m->flags))
486 for (pp = *v; ; pp++) {
487 if (NULL == (pp = strstr(pp, "Ta")))
488 break;
489 if (pp > *v && ' ' != *(pp - 1))
490 continue;
491 if (' ' == *(pp + 2) || '\0' == *(pp + 2))
492 break;
493 }
494
495 /* By default, assume a phrase. */
496 rc = ARGS_PHRASE;
497
498 /*
499 * Adjust new-buffer position to be beyond delimiter
500 * mark (e.g., Ta -> end + 2).
501 */
502 if (p && pp) {
503 *pos += pp < p ? 2 : 1;
504 rc = pp < p ? ARGS_PHRASE : ARGS_PPHRASE;
505 p = pp < p ? pp : p;
506 } else if (p && ! pp) {
507 rc = ARGS_PPHRASE;
508 *pos += 1;
509 } else if (pp && ! p) {
510 p = pp;
511 *pos += 2;
512 } else {
513 rc = ARGS_PEND;
514 p = strchr(*v, 0);
515 }
516
517 /* Whitespace check for eoln case... */
518 if ('\0' == *p && ' ' == *(p - 1))
519 mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE);
520
521 *pos += (int)(p - *v);
522
523 /* Strip delimiter's preceding whitespace. */
524 pp = p - 1;
525 while (pp > *v && ' ' == *pp) {
526 if (pp > *v && '\\' == *(pp - 1))
527 break;
528 pp--;
529 }
530 *(pp + 1) = 0;
531
532 /* Strip delimiter's proceeding whitespace. */
533 for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)
534 /* Skip ahead. */ ;
535
536 return(rc);
537 }
538
539 /*
540 * Process a quoted literal. A quote begins with a double-quote
541 * and ends with a double-quote NOT preceded by a double-quote.
542 * Whitespace is NOT involved in literal termination.
543 */
544
545 if (MDOC_PHRASELIT & m->flags || '\"' == buf[*pos]) {
546 if ( ! (MDOC_PHRASELIT & m->flags))
547 *v = &buf[++(*pos)];
548
549 if (MDOC_PPHRASE & m->flags)
550 m->flags |= MDOC_PHRASELIT;
551
552 for ( ; buf[*pos]; (*pos)++) {
553 if ('\"' != buf[*pos])
554 continue;
555 if ('\"' != buf[*pos + 1])
556 break;
557 (*pos)++;
558 }
559
560 if ('\0' == buf[*pos]) {
561 if (MDOC_PPHRASE & m->flags)
562 return(ARGS_QWORD);
563 mdoc_pmsg(m, line, *pos, MANDOCERR_BADQUOTE);
564 return(ARGS_QWORD);
565 }
566
567 m->flags &= ~MDOC_PHRASELIT;
568 buf[(*pos)++] = '\0';
569
570 if ('\0' == buf[*pos])
571 return(ARGS_QWORD);
572
573 while (' ' == buf[*pos])
574 (*pos)++;
575
576 if ('\0' == buf[*pos])
577 mdoc_pmsg(m, line, *pos, MANDOCERR_EOLNSPACE);
578
579 return(ARGS_QWORD);
580 }
581
582 p = &buf[*pos];
583 *v = mandoc_getarg(m->parse, &p, line, pos);
584
585 return(ARGS_WORD);
586 }
587
588 /*
589 * Check if the string consists only of space-separated closing
590 * delimiters. This is a bit of a dance: the first must be a close
591 * delimiter, but it may be followed by middle delimiters. Arbitrary
592 * whitespace may separate these tokens.
593 */
594 static int
595 args_checkpunct(const char *buf, int i)
596 {
597 int j;
598 char dbuf[DELIMSZ];
599 enum mdelim d;
600
601 /* First token must be a close-delimiter. */
602
603 for (j = 0; buf[i] && ' ' != buf[i] && j < DELIMSZ; j++, i++)
604 dbuf[j] = buf[i];
605
606 if (DELIMSZ == j)
607 return(0);
608
609 dbuf[j] = '\0';
610 if (DELIM_CLOSE != mdoc_isdelim(dbuf))
611 return(0);
612
613 while (' ' == buf[i])
614 i++;
615
616 /* Remaining must NOT be open/none. */
617
618 while (buf[i]) {
619 j = 0;
620 while (buf[i] && ' ' != buf[i] && j < DELIMSZ)
621 dbuf[j++] = buf[i++];
622
623 if (DELIMSZ == j)
624 return(0);
625
626 dbuf[j] = '\0';
627 d = mdoc_isdelim(dbuf);
628 if (DELIM_NONE == d || DELIM_OPEN == d)
629 return(0);
630
631 while (' ' == buf[i])
632 i++;
633 }
634
635 return('\0' == buf[i]);
636 }
637
638 static int
639 argv_multi(struct mdoc *m, int line,
640 struct mdoc_argv *v, int *pos, char *buf)
641 {
642 enum margserr ac;
643 char *p;
644
645 for (v->sz = 0; ; v->sz++) {
646 if ('-' == buf[*pos])
647 break;
648 ac = args(m, line, pos, buf, ARGSFL_NONE, &p);
649 if (ARGS_ERROR == ac)
650 return(0);
651 else if (ARGS_EOLN == ac)
652 break;
653
654 if (0 == v->sz % MULTI_STEP)
655 v->value = mandoc_realloc(v->value,
656 (v->sz + MULTI_STEP) * sizeof(char *));
657
658 v->value[(int)v->sz] = mandoc_strdup(p);
659 }
660
661 return(1);
662 }
663
664 static int
665 argv_single(struct mdoc *m, int line,
666 struct mdoc_argv *v, int *pos, char *buf)
667 {
668 enum margserr ac;
669 char *p;
670
671 ac = args(m, line, pos, buf, ARGSFL_NONE, &p);
672 if (ARGS_ERROR == ac)
673 return(0);
674 if (ARGS_EOLN == ac)
675 return(1);
676
677 v->sz = 1;
678 v->value = mandoc_malloc(sizeof(char *));
679 v->value[0] = mandoc_strdup(p);
680
681 return(1);
682 }