1 /* $Id: mdoc_argv.c,v 1.100 2015/02/04 18:59:45 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2014 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
20 #include <sys/types.h>
29 #include "mandoc_aux.h"
31 #include "libmandoc.h"
33 #define MULTI_STEP 5 /* pre-allocate argument values */
34 #define DELIMSZ 6 /* max possible size of a delimiter */
38 ARGSFL_DELIM, /* handle delimiters of [[::delim::][ ]+]+ */
39 ARGSFL_TABSEP /* handle tab/`Ta' separated phrases */
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) */
50 const enum mdocargt *argvs;
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 *);
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 */
91 static const enum mdocargt args_Ex[] = {
96 static const enum mdocargt args_An[] = {
102 static const enum mdocargt args_Bd[] = {
114 static const enum mdocargt args_Bf[] = {
121 static const enum mdocargt args_Bk[] = {
126 static const enum mdocargt args_Bl[] = {
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 */
273 * Parse flags and their arguments from the input line.
274 * These come in the form -flag [argument ...].
275 * Some flags take no argument, some one, some multiple.
278 mdoc_argv(struct mdoc *mdoc, int line, enum mdoct tok,
279 struct mdoc_arg **reta, int *pos, char *buf)
281 struct mdoc_argv tmpv;
282 struct mdoc_argv **retv;
283 const enum mdocargt *argtable;
290 /* Which flags does this macro support? */
292 argtable = mdocargs[tok].argvs;
293 if (argtable == NULL)
296 /* Loop over the flags on the input line. */
299 while (buf[ipos] == '-') {
301 /* Seek to the first unescaped space. */
303 for (argname = buf + ++ipos; buf[ipos] != '\0'; ipos++)
304 if (buf[ipos] == ' ' && buf[ipos - 1] != '\\')
308 * We want to nil-terminate the word to look it up.
309 * But we may not have a flag, in which case we need
310 * to restore the line as-is. So keep around the
311 * stray byte, which we'll reset upon exiting.
314 if ((savechar = buf[ipos]) != '\0')
318 * Now look up the word as a flag. Use temporary
319 * storage that we'll copy into the node's flags.
322 while ((tmpv.arg = *argtable++) != MDOC_ARG_MAX)
323 if ( ! strcmp(argname, mdoc_argnames[tmpv.arg]))
326 /* If it isn't a flag, restore the saved byte. */
328 if (tmpv.arg == MDOC_ARG_MAX) {
329 if (savechar != '\0')
330 buf[ipos - 1] = savechar;
334 /* Read to the next word (the first argument). */
336 while (buf[ipos] == ' ')
339 /* Parse the arguments of the flag. */
346 switch (argvflags[tmpv.arg]) {
348 argv_single(mdoc, line, &tmpv, &ipos, buf);
351 argv_multi(mdoc, line, &tmpv, &ipos, buf);
357 /* Append to the return values. */
360 *reta = mandoc_calloc(1, sizeof(**reta));
362 retc = ++(*reta)->argc;
363 retv = &(*reta)->argv;
364 *retv = mandoc_reallocarray(*retv, retc, sizeof(**retv));
365 memcpy(*retv + retc - 1, &tmpv, sizeof(**retv));
367 /* Prepare for parsing the next flag. */
370 argtable = mdocargs[tok].argvs;
375 mdoc_argv_free(struct mdoc_arg *p)
389 for (i = (int)p->argc - 1; i >= 0; i--)
397 argn_free(struct mdoc_arg *p, int iarg)
399 struct mdoc_argv *arg;
402 arg = &p->argv[iarg];
404 if (arg->sz && arg->value) {
405 for (j = (int)arg->sz - 1; j >= 0; j--)
410 for (--p->argc; iarg < (int)p->argc; iarg++)
411 p->argv[iarg] = p->argv[iarg+1];
415 mdoc_args(struct mdoc *mdoc, int line, int *pos,
416 char *buf, enum mdoct tok, char **v)
424 fl = tok == MDOC_MAX ? ARGSFL_NONE : mdocargs[tok].flags;
426 return(args(mdoc, line, pos, buf, fl, v));
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.
435 for (n = mdoc->last; n; n = n->parent)
436 if (MDOC_Bl == n->tok)
437 if (LIST_column == n->norm->Bl.type) {
442 return(args(mdoc, line, pos, buf, fl, v));
446 args(struct mdoc *mdoc, int line, int *pos,
447 char *buf, enum argsflag fl, char **v)
453 if ('\0' == buf[*pos]) {
454 if (MDOC_PPHRASE & mdoc->flags)
457 * If we're not in a partial phrase and the flag for
458 * being a phrase literal is still set, the punctuation
461 if (MDOC_PHRASELIT & mdoc->flags)
462 mandoc_msg(MANDOCERR_ARG_QUOTE,
463 mdoc->parse, line, *pos, NULL);
465 mdoc->flags &= ~MDOC_PHRASELIT;
471 if (ARGSFL_DELIM == fl)
472 if (args_checkpunct(buf, *pos))
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.
482 if (ARGSFL_TABSEP == fl) {
483 /* Scan ahead to tab (can't be escaped). */
484 p = strchr(*v, '\t');
487 /* Scan ahead to unescaped `Ta'. */
488 if ( ! (MDOC_PHRASELIT & mdoc->flags))
489 for (pp = *v; ; pp++) {
490 if (NULL == (pp = strstr(pp, "Ta")))
492 if (pp > *v && ' ' != *(pp - 1))
494 if (' ' == *(pp + 2) || '\0' == *(pp + 2))
498 /* By default, assume a phrase. */
502 * Adjust new-buffer position to be beyond delimiter
503 * mark (e.g., Ta -> end + 2).
506 *pos += pp < p ? 2 : 1;
507 rc = pp < p ? ARGS_PHRASE : ARGS_PPHRASE;
509 } else if (p && ! pp) {
512 } else if (pp && ! p) {
520 /* Whitespace check for eoln case... */
521 if ('\0' == *p && ' ' == *(p - 1))
522 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
525 *pos += (int)(p - *v);
527 /* Strip delimiter's preceding whitespace. */
529 while (pp > *v && ' ' == *pp) {
530 if (pp > *v && '\\' == *(pp - 1))
536 /* Strip delimiter's proceeding whitespace. */
537 for (pp = &buf[*pos]; ' ' == *pp; pp++, (*pos)++)
544 * Process a quoted literal. A quote begins with a double-quote
545 * and ends with a double-quote NOT preceded by a double-quote.
546 * NUL-terminate the literal in place.
547 * Collapse pairs of quotes inside quoted literals.
548 * Whitespace is NOT involved in literal termination.
551 if (MDOC_PHRASELIT & mdoc->flags || '\"' == buf[*pos]) {
552 if ( ! (MDOC_PHRASELIT & mdoc->flags))
555 if (MDOC_PPHRASE & mdoc->flags)
556 mdoc->flags |= MDOC_PHRASELIT;
559 for ( ; buf[*pos]; (*pos)++) {
560 /* Move following text left after quoted quotes. */
562 buf[*pos - pairs] = buf[*pos];
563 if ('\"' != buf[*pos])
565 /* Unquoted quotes end quoted args. */
566 if ('\"' != buf[*pos + 1])
568 /* Quoted quotes collapse. */
573 buf[*pos - pairs] = '\0';
575 if ('\0' == buf[*pos]) {
576 if (MDOC_PPHRASE & mdoc->flags)
578 mandoc_msg(MANDOCERR_ARG_QUOTE,
579 mdoc->parse, line, *pos, NULL);
583 mdoc->flags &= ~MDOC_PHRASELIT;
584 buf[(*pos)++] = '\0';
586 if ('\0' == buf[*pos])
589 while (' ' == buf[*pos])
592 if ('\0' == buf[*pos])
593 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
600 *v = mandoc_getarg(mdoc->parse, &p, line, pos);
606 * Check if the string consists only of space-separated closing
607 * delimiters. This is a bit of a dance: the first must be a close
608 * delimiter, but it may be followed by middle delimiters. Arbitrary
609 * whitespace may separate these tokens.
612 args_checkpunct(const char *buf, int i)
618 /* First token must be a close-delimiter. */
620 for (j = 0; buf[i] && ' ' != buf[i] && j < DELIMSZ; j++, i++)
627 if (DELIM_CLOSE != mdoc_isdelim(dbuf))
630 while (' ' == buf[i])
633 /* Remaining must NOT be open/none. */
637 while (buf[i] && ' ' != buf[i] && j < DELIMSZ)
638 dbuf[j++] = buf[i++];
644 d = mdoc_isdelim(dbuf);
645 if (DELIM_NONE == d || DELIM_OPEN == d)
648 while (' ' == buf[i])
652 return('\0' == buf[i]);
656 argv_multi(struct mdoc *mdoc, int line,
657 struct mdoc_argv *v, int *pos, char *buf)
662 for (v->sz = 0; ; v->sz++) {
663 if (buf[*pos] == '-')
665 ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
669 if (v->sz % MULTI_STEP == 0)
670 v->value = mandoc_reallocarray(v->value,
671 v->sz + MULTI_STEP, sizeof(char *));
673 v->value[(int)v->sz] = mandoc_strdup(p);
678 argv_single(struct mdoc *mdoc, int line,
679 struct mdoc_argv *v, int *pos, char *buf)
684 ac = args(mdoc, line, pos, buf, ARGSFL_NONE, &p);
689 v->value = mandoc_malloc(sizeof(char *));
690 v->value[0] = mandoc_strdup(p);