1 /* Id: mdoc_argv.c,v 1.89 2013/12/25 00:50:05 schwarze Exp */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012 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.
22 #include <sys/types.h>
32 #include "libmandoc.h"
34 #define MULTI_STEP 5 /* pre-allocate argument values */
35 #define DELIMSZ 6 /* max possible size of a delimiter */
39 ARGSFL_DELIM
, /* handle delimiters of [[::delim::][ ]+]+ */
40 ARGSFL_TABSEP
/* handle tab/`Ta' separated phrases */
44 ARGV_NONE
, /* no args to flag (e.g., -split) */
45 ARGV_SINGLE
, /* one arg to flag (e.g., -file xxx) */
46 ARGV_MULTI
/* multiple args (e.g., -column xxx yyy) */
51 const enum mdocargt
*argvs
;
54 static void argn_free(struct mdoc_arg
*, int);
55 static enum margserr
args(struct mdoc
*, int, int *,
56 char *, enum argsflag
, char **);
57 static int args_checkpunct(const char *, int);
58 static int argv_multi(struct mdoc
*, int,
59 struct mdoc_argv
*, int *, char *);
60 static int argv_single(struct mdoc
*, int,
61 struct mdoc_argv
*, int *, char *);
63 static const enum argvflag argvflags
[MDOC_ARG_MAX
] = {
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 */
70 ARGV_SINGLE
, /* MDOC_Offset */
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 */
82 ARGV_SINGLE
, /* MDOC_Width */
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 */
92 static const enum mdocargt args_Ex
[] = {
97 static const enum mdocargt args_An
[] = {
103 static const enum mdocargt args_Bd
[] = {
115 static const enum mdocargt args_Bf
[] = {
122 static const enum mdocargt args_Bk
[] = {
127 static const enum mdocargt args_Bl
[] = {
146 static const struct mdocarg mdocargs
[MDOC_MAX
] = {
147 { ARGSFL_DELIM
, NULL
}, /* Ap */
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 */
164 { ARGSFL_DELIM
, NULL
}, /* Cd */
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 */
176 { ARGSFL_DELIM
, NULL
}, /* In */
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 */
244 { ARGSFL_DELIM
, NULL
}, /* Fc */
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 */
253 { ARGSFL_DELIM
, NULL
}, /* Lb */
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 */
263 { ARGSFL_DELIM
, NULL
}, /* Dx */
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 */
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.
278 mdoc_argv(struct mdoc
*mdoc
, int line
, enum mdoct tok
,
279 struct mdoc_arg
**v
, int *pos
, char *buf
)
282 struct mdoc_argv tmp
;
283 struct mdoc_arg
*arg
;
284 const enum mdocargt
*ap
;
286 if ('\0' == buf
[*pos
])
288 else if (NULL
== (ap
= mdocargs
[tok
].argvs
))
290 else if ('-' != buf
[*pos
])
293 /* Seek to the first unescaped space. */
299 for ( ; buf
[*pos
] ; (*pos
)++)
300 if (' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
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).
310 if ('\0' != (sv
= buf
[*pos
]))
311 buf
[(*pos
)++] = '\0';
314 * Now look up the word as a flag. Use temporary storage that
315 * we'll copy into the node's flags, if necessary.
318 memset(&tmp
, 0, sizeof(struct mdoc_argv
));
322 tmp
.arg
= MDOC_ARG_MAX
;
324 while (MDOC_ARG_MAX
!= (tmp
.arg
= *ap
++))
325 if (0 == strcmp(p
, mdoc_argnames
[tmp
.arg
]))
328 if (MDOC_ARG_MAX
== tmp
.arg
) {
330 * The flag was not found.
331 * Restore saved zeroed byte and return as a word.
338 /* Read to the next word (the argument). */
340 while (buf
[*pos
] && ' ' == buf
[*pos
])
343 switch (argvflags
[tmp
.arg
]) {
345 if ( ! argv_single(mdoc
, line
, &tmp
, pos
, buf
))
349 if ( ! argv_multi(mdoc
, line
, &tmp
, pos
, buf
))
356 if (NULL
== (arg
= *v
))
357 arg
= *v
= mandoc_calloc(1, sizeof(struct mdoc_arg
));
360 arg
->argv
= mandoc_realloc
361 (arg
->argv
, arg
->argc
* sizeof(struct mdoc_argv
));
363 memcpy(&arg
->argv
[(int)arg
->argc
- 1],
364 &tmp
, sizeof(struct mdoc_argv
));
370 mdoc_argv_free(struct mdoc_arg
*p
)
384 for (i
= (int)p
->argc
- 1; i
>= 0; i
--)
392 argn_free(struct mdoc_arg
*p
, int iarg
)
394 struct mdoc_argv
*arg
;
397 arg
= &p
->argv
[iarg
];
399 if (arg
->sz
&& arg
->value
) {
400 for (j
= (int)arg
->sz
- 1; j
>= 0; j
--)
405 for (--p
->argc
; iarg
< (int)p
->argc
; iarg
++)
406 p
->argv
[iarg
] = p
->argv
[iarg
+1];
410 mdoc_zargs(struct mdoc
*mdoc
, int line
, int *pos
, char *buf
, char **v
)
413 return(args(mdoc
, line
, pos
, buf
, ARGSFL_NONE
, v
));
417 mdoc_args(struct mdoc
*mdoc
, int line
, int *pos
,
418 char *buf
, enum mdoct tok
, char **v
)
423 fl
= 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 mdoc_pmsg(mdoc
, line
, *pos
, MANDOCERR_BADQUOTE
);
464 mdoc
->flags
&= ~MDOC_PHRASELIT
;
470 if (ARGSFL_DELIM
== fl
)
471 if (args_checkpunct(buf
, *pos
))
475 * First handle TABSEP items, restricted to `Bl -column'. This
476 * ignores conventional token parsing and instead uses tabs or
477 * `Ta' macros to separate phrases. Phrases are parsed again
478 * for arguments at a later phase.
481 if (ARGSFL_TABSEP
== fl
) {
482 /* Scan ahead to tab (can't be escaped). */
483 p
= strchr(*v
, '\t');
486 /* Scan ahead to unescaped `Ta'. */
487 if ( ! (MDOC_PHRASELIT
& mdoc
->flags
))
488 for (pp
= *v
; ; pp
++) {
489 if (NULL
== (pp
= strstr(pp
, "Ta")))
491 if (pp
> *v
&& ' ' != *(pp
- 1))
493 if (' ' == *(pp
+ 2) || '\0' == *(pp
+ 2))
497 /* By default, assume a phrase. */
501 * Adjust new-buffer position to be beyond delimiter
502 * mark (e.g., Ta -> end + 2).
505 *pos
+= pp
< p
? 2 : 1;
506 rc
= pp
< p
? ARGS_PHRASE
: ARGS_PPHRASE
;
508 } else if (p
&& ! pp
) {
511 } else if (pp
&& ! p
) {
519 /* Whitespace check for eoln case... */
520 if ('\0' == *p
&& ' ' == *(p
- 1))
521 mdoc_pmsg(mdoc
, line
, *pos
, MANDOCERR_EOLNSPACE
);
523 *pos
+= (int)(p
- *v
);
525 /* Strip delimiter's preceding whitespace. */
527 while (pp
> *v
&& ' ' == *pp
) {
528 if (pp
> *v
&& '\\' == *(pp
- 1))
534 /* Strip delimiter's proceeding whitespace. */
535 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
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.
549 if (MDOC_PHRASELIT
& mdoc
->flags
|| '\"' == buf
[*pos
]) {
550 if ( ! (MDOC_PHRASELIT
& mdoc
->flags
))
553 if (MDOC_PPHRASE
& mdoc
->flags
)
554 mdoc
->flags
|= MDOC_PHRASELIT
;
557 for ( ; buf
[*pos
]; (*pos
)++) {
558 /* Move following text left after quoted quotes. */
560 buf
[*pos
- pairs
] = buf
[*pos
];
561 if ('\"' != buf
[*pos
])
563 /* Unquoted quotes end quoted args. */
564 if ('\"' != buf
[*pos
+ 1])
566 /* Quoted quotes collapse. */
571 buf
[*pos
- pairs
] = '\0';
573 if ('\0' == buf
[*pos
]) {
574 if (MDOC_PPHRASE
& mdoc
->flags
)
576 mdoc_pmsg(mdoc
, line
, *pos
, MANDOCERR_BADQUOTE
);
580 mdoc
->flags
&= ~MDOC_PHRASELIT
;
581 buf
[(*pos
)++] = '\0';
583 if ('\0' == buf
[*pos
])
586 while (' ' == buf
[*pos
])
589 if ('\0' == buf
[*pos
])
590 mdoc_pmsg(mdoc
, line
, *pos
, MANDOCERR_EOLNSPACE
);
596 *v
= mandoc_getarg(mdoc
->parse
, &p
, line
, pos
);
602 * Check if the string consists only of space-separated closing
603 * delimiters. This is a bit of a dance: the first must be a close
604 * delimiter, but it may be followed by middle delimiters. Arbitrary
605 * whitespace may separate these tokens.
608 args_checkpunct(const char *buf
, int i
)
614 /* First token must be a close-delimiter. */
616 for (j
= 0; buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
; j
++, i
++)
623 if (DELIM_CLOSE
!= mdoc_isdelim(dbuf
))
626 while (' ' == buf
[i
])
629 /* Remaining must NOT be open/none. */
633 while (buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
)
634 dbuf
[j
++] = buf
[i
++];
640 d
= mdoc_isdelim(dbuf
);
641 if (DELIM_NONE
== d
|| DELIM_OPEN
== d
)
644 while (' ' == buf
[i
])
648 return('\0' == buf
[i
]);
652 argv_multi(struct mdoc
*mdoc
, int line
,
653 struct mdoc_argv
*v
, int *pos
, char *buf
)
658 for (v
->sz
= 0; ; v
->sz
++) {
659 if ('-' == buf
[*pos
])
661 ac
= args(mdoc
, line
, pos
, buf
, ARGSFL_NONE
, &p
);
662 if (ARGS_ERROR
== ac
)
664 else if (ARGS_EOLN
== ac
)
667 if (0 == v
->sz
% MULTI_STEP
)
668 v
->value
= mandoc_realloc(v
->value
,
669 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
671 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
);
685 if (ARGS_ERROR
== ac
)
691 v
->value
= mandoc_malloc(sizeof(char *));
692 v
->value
[0] = mandoc_strdup(p
);