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