1 /* $Id: mdoc_argv.c,v 1.107 2015/10/17 00:21:07 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2014, 2015 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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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>
27 #include "mandoc_aux.h"
31 #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 roff_man
*, int, int *,
56 char *, enum argsflag
, char **);
57 static int args_checkpunct(const char *, int);
58 static void argv_multi(struct roff_man
*, int,
59 struct mdoc_argv
*, int *, char *);
60 static void argv_single(struct roff_man
*, 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_DELIM
, 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_DELIM
, 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_DELIM
, 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 */
269 { ARGSFL_NONE
, NULL
}, /* ll */
274 * Parse flags and their arguments from the input line.
275 * These come in the form -flag [argument ...].
276 * Some flags take no argument, some one, some multiple.
279 mdoc_argv(struct roff_man
*mdoc
, int line
, int tok
,
280 struct mdoc_arg
**reta
, int *pos
, char *buf
)
282 struct mdoc_argv tmpv
;
283 struct mdoc_argv
**retv
;
284 const enum mdocargt
*argtable
;
291 /* Which flags does this macro support? */
293 argtable
= mdocargs
[tok
].argvs
;
294 if (argtable
== NULL
)
297 /* Loop over the flags on the input line. */
300 while (buf
[ipos
] == '-') {
302 /* Seek to the first unescaped space. */
304 for (argname
= buf
+ ++ipos
; buf
[ipos
] != '\0'; ipos
++)
305 if (buf
[ipos
] == ' ' && buf
[ipos
- 1] != '\\')
309 * We want to nil-terminate the word to look it up.
310 * But we may not have a flag, in which case we need
311 * to restore the line as-is. So keep around the
312 * stray byte, which we'll reset upon exiting.
315 if ((savechar
= buf
[ipos
]) != '\0')
319 * Now look up the word as a flag. Use temporary
320 * storage that we'll copy into the node's flags.
323 while ((tmpv
.arg
= *argtable
++) != MDOC_ARG_MAX
)
324 if ( ! strcmp(argname
, mdoc_argnames
[tmpv
.arg
]))
327 /* If it isn't a flag, restore the saved byte. */
329 if (tmpv
.arg
== MDOC_ARG_MAX
) {
330 if (savechar
!= '\0')
331 buf
[ipos
- 1] = savechar
;
335 /* Read to the next word (the first argument). */
337 while (buf
[ipos
] == ' ')
340 /* Parse the arguments of the flag. */
347 switch (argvflags
[tmpv
.arg
]) {
349 argv_single(mdoc
, line
, &tmpv
, &ipos
, buf
);
352 argv_multi(mdoc
, line
, &tmpv
, &ipos
, buf
);
358 /* Append to the return values. */
361 *reta
= mandoc_calloc(1, sizeof(**reta
));
363 retc
= ++(*reta
)->argc
;
364 retv
= &(*reta
)->argv
;
365 *retv
= mandoc_reallocarray(*retv
, retc
, sizeof(**retv
));
366 memcpy(*retv
+ retc
- 1, &tmpv
, sizeof(**retv
));
368 /* Prepare for parsing the next flag. */
371 argtable
= mdocargs
[tok
].argvs
;
376 mdoc_argv_free(struct mdoc_arg
*p
)
390 for (i
= (int)p
->argc
- 1; i
>= 0; i
--)
398 argn_free(struct mdoc_arg
*p
, int iarg
)
400 struct mdoc_argv
*arg
;
403 arg
= &p
->argv
[iarg
];
405 if (arg
->sz
&& arg
->value
) {
406 for (j
= (int)arg
->sz
- 1; j
>= 0; j
--)
411 for (--p
->argc
; iarg
< (int)p
->argc
; iarg
++)
412 p
->argv
[iarg
] = p
->argv
[iarg
+1];
416 mdoc_args(struct roff_man
*mdoc
, int line
, int *pos
,
417 char *buf
, int tok
, char **v
)
425 fl
= tok
== TOKEN_NONE
? ARGSFL_NONE
: mdocargs
[tok
].flags
;
427 return args(mdoc
, line
, pos
, buf
, fl
, v
);
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.
436 for (n
= mdoc
->last
; n
; n
= n
->parent
)
437 if (MDOC_Bl
== n
->tok
)
438 if (LIST_column
== n
->norm
->Bl
.type
) {
443 return args(mdoc
, line
, pos
, buf
, fl
, v
);
447 args(struct roff_man
*mdoc
, int line
, int *pos
,
448 char *buf
, enum argsflag fl
, char **v
)
453 if (buf
[*pos
] == '\0') {
454 if (mdoc
->flags
& MDOC_PHRASELIT
&&
455 ! (mdoc
->flags
& MDOC_PHRASE
)) {
456 mandoc_msg(MANDOCERR_ARG_QUOTE
,
457 mdoc
->parse
, line
, *pos
, NULL
);
458 mdoc
->flags
&= ~MDOC_PHRASELIT
;
465 if (fl
== ARGSFL_DELIM
&& args_checkpunct(buf
, *pos
))
469 * Tabs in `It' lines in `Bl -column' can't be escaped.
470 * Phrases are reparsed for `Ta' and other macros later.
473 if (fl
== ARGSFL_TABSEP
) {
474 if ((p
= strchr(*v
, '\t')) != NULL
) {
477 * Words right before and right after
478 * tab characters are not parsed,
479 * unless there is a blank in between.
483 mdoc
->flags
|= MDOC_PHRASEQL
;
485 mdoc
->flags
|= MDOC_PHRASEQN
;
488 * One or more blanks after a tab cause
489 * one leading blank in the next column.
490 * So skip all but one of them.
493 *pos
+= (int)(p
- *v
) + 1;
494 while (buf
[*pos
] == ' ' && buf
[*pos
+ 1] == ' ')
498 * A tab at the end of an input line
499 * switches to the next column.
502 if (buf
[*pos
] == '\0' || buf
[*pos
+ 1] == '\0')
503 mdoc
->flags
|= MDOC_PHRASEQN
;
505 p
= strchr(*v
, '\0');
507 mandoc_msg(MANDOCERR_SPACE_EOL
,
508 mdoc
->parse
, line
, *pos
, NULL
);
509 *pos
+= (int)(p
- *v
);
512 /* Skip any trailing blank characters. */
513 while (p
> *v
&& p
[-1] == ' ' &&
514 (p
- 1 == *v
|| p
[-2] != '\\'))
522 * Process a quoted literal. A quote begins with a double-quote
523 * and ends with a double-quote NOT preceded by a double-quote.
524 * NUL-terminate the literal in place.
525 * Collapse pairs of quotes inside quoted literals.
526 * Whitespace is NOT involved in literal termination.
529 if (mdoc
->flags
& MDOC_PHRASELIT
|| buf
[*pos
] == '\"') {
530 if ( ! (mdoc
->flags
& MDOC_PHRASELIT
))
533 if (mdoc
->flags
& MDOC_PHRASE
)
534 mdoc
->flags
|= MDOC_PHRASELIT
;
537 for ( ; buf
[*pos
]; (*pos
)++) {
538 /* Move following text left after quoted quotes. */
540 buf
[*pos
- pairs
] = buf
[*pos
];
541 if ('\"' != buf
[*pos
])
543 /* Unquoted quotes end quoted args. */
544 if ('\"' != buf
[*pos
+ 1])
546 /* Quoted quotes collapse. */
551 buf
[*pos
- pairs
] = '\0';
553 if (buf
[*pos
] == '\0') {
554 if ( ! (mdoc
->flags
& MDOC_PHRASE
))
555 mandoc_msg(MANDOCERR_ARG_QUOTE
,
556 mdoc
->parse
, line
, *pos
, NULL
);
560 mdoc
->flags
&= ~MDOC_PHRASELIT
;
561 buf
[(*pos
)++] = '\0';
563 if ('\0' == buf
[*pos
])
566 while (' ' == buf
[*pos
])
569 if ('\0' == buf
[*pos
])
570 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
577 *v
= mandoc_getarg(mdoc
->parse
, &p
, line
, pos
);
580 * After parsing the last word in this phrase,
581 * tell lookup() whether or not to interpret it.
584 if (*p
== '\0' && mdoc
->flags
& MDOC_PHRASEQL
) {
585 mdoc
->flags
&= ~MDOC_PHRASEQL
;
586 mdoc
->flags
|= MDOC_PHRASEQF
;
592 * Check if the string consists only of space-separated closing
593 * delimiters. This is a bit of a dance: the first must be a close
594 * delimiter, but it may be followed by middle delimiters. Arbitrary
595 * whitespace may separate these tokens.
598 args_checkpunct(const char *buf
, int i
)
604 /* First token must be a close-delimiter. */
606 for (j
= 0; buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
; j
++, i
++)
613 if (DELIM_CLOSE
!= mdoc_isdelim(dbuf
))
616 while (' ' == buf
[i
])
619 /* Remaining must NOT be open/none. */
623 while (buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
)
624 dbuf
[j
++] = buf
[i
++];
630 d
= mdoc_isdelim(dbuf
);
631 if (DELIM_NONE
== d
|| DELIM_OPEN
== d
)
634 while (' ' == buf
[i
])
638 return '\0' == buf
[i
];
642 argv_multi(struct roff_man
*mdoc
, int line
,
643 struct mdoc_argv
*v
, int *pos
, char *buf
)
648 for (v
->sz
= 0; ; v
->sz
++) {
649 if (buf
[*pos
] == '-')
651 ac
= args(mdoc
, line
, pos
, buf
, ARGSFL_NONE
, &p
);
655 if (v
->sz
% MULTI_STEP
== 0)
656 v
->value
= mandoc_reallocarray(v
->value
,
657 v
->sz
+ MULTI_STEP
, sizeof(char *));
659 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
664 argv_single(struct roff_man
*mdoc
, int line
,
665 struct mdoc_argv
*v
, int *pos
, char *buf
)
670 ac
= args(mdoc
, line
, pos
, buf
, ARGSFL_NONE
, &p
);
675 v
->value
= mandoc_malloc(sizeof(char *));
676 v
->value
[0] = mandoc_strdup(p
);