]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.80 2011/06/18 16:53:27 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
21 #include <sys/types.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) */
46 ARGV_OPT_SINGLE
/* optional arg (e.g., -offset [xxx]) */
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_opt_single(struct mdoc
*, int,
61 struct mdoc_argv
*, int *, char *);
62 static int argv_single(struct mdoc
*, int,
63 struct mdoc_argv
*, int *, char *);
65 static const enum argvflag argvflags
[MDOC_ARG_MAX
] = {
66 ARGV_NONE
, /* MDOC_Split */
67 ARGV_NONE
, /* MDOC_Nosplit */
68 ARGV_NONE
, /* MDOC_Ragged */
69 ARGV_NONE
, /* MDOC_Unfilled */
70 ARGV_NONE
, /* MDOC_Literal */
71 ARGV_SINGLE
, /* MDOC_File */
72 ARGV_OPT_SINGLE
, /* MDOC_Offset */
73 ARGV_NONE
, /* MDOC_Bullet */
74 ARGV_NONE
, /* MDOC_Dash */
75 ARGV_NONE
, /* MDOC_Hyphen */
76 ARGV_NONE
, /* MDOC_Item */
77 ARGV_NONE
, /* MDOC_Enum */
78 ARGV_NONE
, /* MDOC_Tag */
79 ARGV_NONE
, /* MDOC_Diag */
80 ARGV_NONE
, /* MDOC_Hang */
81 ARGV_NONE
, /* MDOC_Ohang */
82 ARGV_NONE
, /* MDOC_Inset */
83 ARGV_MULTI
, /* MDOC_Column */
84 ARGV_SINGLE
, /* MDOC_Width */
85 ARGV_NONE
, /* MDOC_Compact */
86 ARGV_NONE
, /* MDOC_Std */
87 ARGV_NONE
, /* MDOC_Filled */
88 ARGV_NONE
, /* MDOC_Words */
89 ARGV_NONE
, /* MDOC_Emphasis */
90 ARGV_NONE
, /* MDOC_Symbolic */
91 ARGV_NONE
/* MDOC_Symbolic */
94 static const enum mdocargt args_Ex
[] = {
99 static const enum mdocargt args_An
[] = {
105 static const enum mdocargt args_Bd
[] = {
117 static const enum mdocargt args_Bf
[] = {
124 static const enum mdocargt args_Bk
[] = {
129 static const enum mdocargt args_Bl
[] = {
148 static const struct mdocarg mdocargs
[MDOC_MAX
] = {
149 { ARGSFL_NONE
, NULL
}, /* Ap */
150 { ARGSFL_NONE
, NULL
}, /* Dd */
151 { ARGSFL_NONE
, NULL
}, /* Dt */
152 { ARGSFL_NONE
, NULL
}, /* Os */
153 { ARGSFL_NONE
, NULL
}, /* Sh */
154 { ARGSFL_NONE
, NULL
}, /* Ss */
155 { ARGSFL_NONE
, NULL
}, /* Pp */
156 { ARGSFL_DELIM
, NULL
}, /* D1 */
157 { ARGSFL_DELIM
, NULL
}, /* Dl */
158 { ARGSFL_NONE
, args_Bd
}, /* Bd */
159 { ARGSFL_NONE
, NULL
}, /* Ed */
160 { ARGSFL_NONE
, args_Bl
}, /* Bl */
161 { ARGSFL_NONE
, NULL
}, /* El */
162 { ARGSFL_NONE
, NULL
}, /* It */
163 { ARGSFL_DELIM
, NULL
}, /* Ad */
164 { ARGSFL_DELIM
, args_An
}, /* An */
165 { ARGSFL_DELIM
, NULL
}, /* Ar */
166 { ARGSFL_NONE
, NULL
}, /* Cd */
167 { ARGSFL_DELIM
, NULL
}, /* Cm */
168 { ARGSFL_DELIM
, NULL
}, /* Dv */
169 { ARGSFL_DELIM
, NULL
}, /* Er */
170 { ARGSFL_DELIM
, NULL
}, /* Ev */
171 { ARGSFL_NONE
, args_Ex
}, /* Ex */
172 { ARGSFL_DELIM
, NULL
}, /* Fa */
173 { ARGSFL_NONE
, NULL
}, /* Fd */
174 { ARGSFL_DELIM
, NULL
}, /* Fl */
175 { ARGSFL_DELIM
, NULL
}, /* Fn */
176 { ARGSFL_DELIM
, NULL
}, /* Ft */
177 { ARGSFL_DELIM
, NULL
}, /* Ic */
178 { ARGSFL_NONE
, NULL
}, /* In */
179 { ARGSFL_DELIM
, NULL
}, /* Li */
180 { ARGSFL_NONE
, NULL
}, /* Nd */
181 { ARGSFL_DELIM
, NULL
}, /* Nm */
182 { ARGSFL_DELIM
, NULL
}, /* Op */
183 { ARGSFL_NONE
, NULL
}, /* Ot */
184 { ARGSFL_DELIM
, NULL
}, /* Pa */
185 { ARGSFL_NONE
, args_Ex
}, /* Rv */
186 { ARGSFL_DELIM
, NULL
}, /* St */
187 { ARGSFL_DELIM
, NULL
}, /* Va */
188 { ARGSFL_DELIM
, NULL
}, /* Vt */
189 { ARGSFL_DELIM
, NULL
}, /* Xr */
190 { ARGSFL_NONE
, NULL
}, /* %A */
191 { ARGSFL_NONE
, NULL
}, /* %B */
192 { ARGSFL_NONE
, NULL
}, /* %D */
193 { ARGSFL_NONE
, NULL
}, /* %I */
194 { ARGSFL_NONE
, NULL
}, /* %J */
195 { ARGSFL_NONE
, NULL
}, /* %N */
196 { ARGSFL_NONE
, NULL
}, /* %O */
197 { ARGSFL_NONE
, NULL
}, /* %P */
198 { ARGSFL_NONE
, NULL
}, /* %R */
199 { ARGSFL_NONE
, NULL
}, /* %T */
200 { ARGSFL_NONE
, NULL
}, /* %V */
201 { ARGSFL_DELIM
, NULL
}, /* Ac */
202 { ARGSFL_NONE
, NULL
}, /* Ao */
203 { ARGSFL_DELIM
, NULL
}, /* Aq */
204 { ARGSFL_DELIM
, NULL
}, /* At */
205 { ARGSFL_DELIM
, NULL
}, /* Bc */
206 { ARGSFL_NONE
, args_Bf
}, /* Bf */
207 { ARGSFL_NONE
, NULL
}, /* Bo */
208 { ARGSFL_DELIM
, NULL
}, /* Bq */
209 { ARGSFL_DELIM
, NULL
}, /* Bsx */
210 { ARGSFL_DELIM
, NULL
}, /* Bx */
211 { ARGSFL_NONE
, NULL
}, /* Db */
212 { ARGSFL_DELIM
, NULL
}, /* Dc */
213 { ARGSFL_NONE
, NULL
}, /* Do */
214 { ARGSFL_DELIM
, NULL
}, /* Dq */
215 { ARGSFL_DELIM
, NULL
}, /* Ec */
216 { ARGSFL_NONE
, NULL
}, /* Ef */
217 { ARGSFL_DELIM
, NULL
}, /* Em */
218 { ARGSFL_NONE
, NULL
}, /* Eo */
219 { ARGSFL_DELIM
, NULL
}, /* Fx */
220 { ARGSFL_DELIM
, NULL
}, /* Ms */
221 { ARGSFL_DELIM
, NULL
}, /* No */
222 { ARGSFL_DELIM
, NULL
}, /* Ns */
223 { ARGSFL_DELIM
, NULL
}, /* Nx */
224 { ARGSFL_DELIM
, NULL
}, /* Ox */
225 { ARGSFL_DELIM
, NULL
}, /* Pc */
226 { ARGSFL_DELIM
, NULL
}, /* Pf */
227 { ARGSFL_NONE
, NULL
}, /* Po */
228 { ARGSFL_DELIM
, NULL
}, /* Pq */
229 { ARGSFL_DELIM
, NULL
}, /* Qc */
230 { ARGSFL_DELIM
, NULL
}, /* Ql */
231 { ARGSFL_NONE
, NULL
}, /* Qo */
232 { ARGSFL_DELIM
, NULL
}, /* Qq */
233 { ARGSFL_NONE
, NULL
}, /* Re */
234 { ARGSFL_NONE
, NULL
}, /* Rs */
235 { ARGSFL_DELIM
, NULL
}, /* Sc */
236 { ARGSFL_NONE
, NULL
}, /* So */
237 { ARGSFL_DELIM
, NULL
}, /* Sq */
238 { ARGSFL_NONE
, NULL
}, /* Sm */
239 { ARGSFL_DELIM
, NULL
}, /* Sx */
240 { ARGSFL_DELIM
, NULL
}, /* Sy */
241 { ARGSFL_DELIM
, NULL
}, /* Tn */
242 { ARGSFL_DELIM
, NULL
}, /* Ux */
243 { ARGSFL_DELIM
, NULL
}, /* Xc */
244 { ARGSFL_NONE
, NULL
}, /* Xo */
245 { ARGSFL_NONE
, NULL
}, /* Fo */
246 { ARGSFL_NONE
, NULL
}, /* Fc */
247 { ARGSFL_NONE
, NULL
}, /* Oo */
248 { ARGSFL_DELIM
, NULL
}, /* Oc */
249 { ARGSFL_NONE
, args_Bk
}, /* Bk */
250 { ARGSFL_NONE
, NULL
}, /* Ek */
251 { ARGSFL_NONE
, NULL
}, /* Bt */
252 { ARGSFL_NONE
, NULL
}, /* Hf */
253 { ARGSFL_NONE
, NULL
}, /* Fr */
254 { ARGSFL_NONE
, NULL
}, /* Ud */
255 { ARGSFL_NONE
, NULL
}, /* Lb */
256 { ARGSFL_NONE
, NULL
}, /* Lp */
257 { ARGSFL_DELIM
, NULL
}, /* Lk */
258 { ARGSFL_DELIM
, NULL
}, /* Mt */
259 { ARGSFL_DELIM
, NULL
}, /* Brq */
260 { ARGSFL_NONE
, NULL
}, /* Bro */
261 { ARGSFL_DELIM
, NULL
}, /* Brc */
262 { ARGSFL_NONE
, NULL
}, /* %C */
263 { ARGSFL_NONE
, NULL
}, /* Es */
264 { ARGSFL_NONE
, NULL
}, /* En */
265 { ARGSFL_NONE
, NULL
}, /* Dx */
266 { ARGSFL_NONE
, NULL
}, /* %Q */
267 { ARGSFL_NONE
, NULL
}, /* br */
268 { ARGSFL_NONE
, NULL
}, /* sp */
269 { ARGSFL_NONE
, NULL
}, /* %U */
270 { ARGSFL_NONE
, NULL
}, /* Ta */
275 * Parse an argument from line text. This comes in the form of -key
276 * [value0...], which may either have a single mandatory value, at least
277 * one mandatory value, an optional single value, or no value.
280 mdoc_argv(struct mdoc
*m
, int line
, enum mdoct tok
,
281 struct mdoc_arg
**v
, int *pos
, char *buf
)
284 struct mdoc_argv tmp
;
285 struct mdoc_arg
*arg
;
286 const enum mdocargt
*ap
;
288 if ('\0' == buf
[*pos
])
290 else if (NULL
== (ap
= mdocargs
[tok
].argvs
))
292 else if ('-' != buf
[*pos
])
295 /* Seek to the first unescaped space. */
301 for ( ; buf
[*pos
] ; (*pos
)++)
302 if (' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
306 * We want to nil-terminate the word to look it up (it's easier
307 * that way). But we may not have a flag, in which case we need
308 * to restore the line as-is. So keep around the stray byte,
309 * which we'll reset upon exiting (if necessary).
312 if ('\0' != (sv
= buf
[*pos
]))
313 buf
[(*pos
)++] = '\0';
316 * Now look up the word as a flag. Use temporary storage that
317 * we'll copy into the node's flags, if necessary.
320 memset(&tmp
, 0, sizeof(struct mdoc_argv
));
324 tmp
.arg
= MDOC_ARG_MAX
;
326 while (MDOC_ARG_MAX
!= (tmp
.arg
= *ap
++))
327 if (0 == strcmp(p
, mdoc_argnames
[tmp
.arg
]))
330 if (MDOC_ARG_MAX
== tmp
.arg
) {
332 * The flag was not found.
333 * Restore saved zeroed byte and return as a word.
340 /* Read to the next word (the argument). */
342 while (buf
[*pos
] && ' ' == buf
[*pos
])
345 switch (argvflags
[tmp
.arg
]) {
347 if ( ! argv_single(m
, line
, &tmp
, pos
, buf
))
351 if ( ! argv_multi(m
, line
, &tmp
, pos
, buf
))
354 case (ARGV_OPT_SINGLE
):
355 if ( ! argv_opt_single(m
, line
, &tmp
, pos
, buf
))
362 if (NULL
== (arg
= *v
))
363 arg
= *v
= mandoc_calloc(1, sizeof(struct mdoc_arg
));
366 arg
->argv
= mandoc_realloc
367 (arg
->argv
, arg
->argc
* sizeof(struct mdoc_argv
));
369 memcpy(&arg
->argv
[(int)arg
->argc
- 1],
370 &tmp
, sizeof(struct mdoc_argv
));
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_zargs(struct mdoc
*m
, int line
, int *pos
, char *buf
, char **v
)
419 return(args(m
, line
, pos
, buf
, ARGSFL_NONE
, v
));
423 mdoc_args(struct mdoc
*m
, int line
, int *pos
,
424 char *buf
, enum mdoct tok
, char **v
)
429 fl
= mdocargs
[tok
].flags
;
432 return(args(m
, line
, pos
, buf
, fl
, v
));
435 * We know that we're in an `It', so it's reasonable to expect
436 * us to be sitting in a `Bl'. Someday this may not be the case
437 * (if we allow random `It's sitting out there), so provide a
438 * safe fall-back into the default behaviour.
441 for (n
= m
->last
; n
; n
= n
->parent
)
442 if (MDOC_Bl
== n
->tok
)
443 if (LIST_column
== n
->norm
->Bl
.type
) {
448 return(args(m
, line
, pos
, buf
, fl
, v
));
452 args(struct mdoc
*m
, int line
, int *pos
,
453 char *buf
, enum argsflag fl
, char **v
)
458 if ('\0' == buf
[*pos
]) {
459 if (MDOC_PPHRASE
& m
->flags
)
462 * If we're not in a partial phrase and the flag for
463 * being a phrase literal is still set, the punctuation
466 if (MDOC_PHRASELIT
& m
->flags
)
467 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
);
469 m
->flags
&= ~MDOC_PHRASELIT
;
475 if (ARGSFL_DELIM
== fl
)
476 if (args_checkpunct(buf
, *pos
))
480 * First handle TABSEP items, restricted to `Bl -column'. This
481 * ignores conventional token parsing and instead uses tabs or
482 * `Ta' macros to separate phrases. Phrases are parsed again
483 * for arguments at a later phase.
486 if (ARGSFL_TABSEP
== fl
) {
487 /* Scan ahead to tab (can't be escaped). */
488 p
= strchr(*v
, '\t');
491 /* Scan ahead to unescaped `Ta'. */
492 if ( ! (MDOC_PHRASELIT
& m
->flags
))
493 for (pp
= *v
; ; pp
++) {
494 if (NULL
== (pp
= strstr(pp
, "Ta")))
496 if (pp
> *v
&& ' ' != *(pp
- 1))
498 if (' ' == *(pp
+ 2) || '\0' == *(pp
+ 2))
502 /* By default, assume a phrase. */
506 * Adjust new-buffer position to be beyond delimiter
507 * mark (e.g., Ta -> end + 2).
510 *pos
+= pp
< p
? 2 : 1;
511 rc
= pp
< p
? ARGS_PHRASE
: ARGS_PPHRASE
;
513 } else if (p
&& ! pp
) {
516 } else if (pp
&& ! p
) {
524 /* Whitespace check for eoln case... */
525 if ('\0' == *p
&& ' ' == *(p
- 1))
526 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
528 *pos
+= (int)(p
- *v
);
530 /* Strip delimiter's preceding whitespace. */
532 while (pp
> *v
&& ' ' == *pp
) {
533 if (pp
> *v
&& '\\' == *(pp
- 1))
539 /* Strip delimiter's proceeding whitespace. */
540 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
547 * Process a quoted literal. A quote begins with a double-quote
548 * and ends with a double-quote NOT preceded by a double-quote.
549 * Whitespace is NOT involved in literal termination.
552 if (MDOC_PHRASELIT
& m
->flags
|| '\"' == buf
[*pos
]) {
553 if ( ! (MDOC_PHRASELIT
& m
->flags
))
556 if (MDOC_PPHRASE
& m
->flags
)
557 m
->flags
|= MDOC_PHRASELIT
;
559 for ( ; buf
[*pos
]; (*pos
)++) {
560 if ('\"' != buf
[*pos
])
562 if ('\"' != buf
[*pos
+ 1])
567 if ('\0' == buf
[*pos
]) {
568 if (MDOC_PPHRASE
& m
->flags
)
570 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
);
574 m
->flags
&= ~MDOC_PHRASELIT
;
575 buf
[(*pos
)++] = '\0';
577 if ('\0' == buf
[*pos
])
580 while (' ' == buf
[*pos
])
583 if ('\0' == buf
[*pos
])
584 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
590 *v
= mandoc_getarg(m
->parse
, &p
, line
, pos
);
596 * Check if the string consists only of space-separated closing
597 * delimiters. This is a bit of a dance: the first must be a close
598 * delimiter, but it may be followed by middle delimiters. Arbitrary
599 * whitespace may separate these tokens.
602 args_checkpunct(const char *buf
, int i
)
608 /* First token must be a close-delimiter. */
610 for (j
= 0; buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
; j
++, i
++)
617 if (DELIM_CLOSE
!= mdoc_isdelim(dbuf
))
620 while (' ' == buf
[i
])
623 /* Remaining must NOT be open/none. */
627 while (buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
)
628 dbuf
[j
++] = buf
[i
++];
634 d
= mdoc_isdelim(dbuf
);
635 if (DELIM_NONE
== d
|| DELIM_OPEN
== d
)
638 while (' ' == buf
[i
])
642 return('\0' == buf
[i
]);
646 argv_multi(struct mdoc
*m
, int line
,
647 struct mdoc_argv
*v
, int *pos
, char *buf
)
652 for (v
->sz
= 0; ; v
->sz
++) {
653 if ('-' == buf
[*pos
])
655 ac
= args(m
, line
, pos
, buf
, ARGSFL_NONE
, &p
);
656 if (ARGS_ERROR
== ac
)
658 else if (ARGS_EOLN
== ac
)
661 if (0 == v
->sz
% MULTI_STEP
)
662 v
->value
= mandoc_realloc(v
->value
,
663 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
665 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
672 argv_opt_single(struct mdoc
*m
, int line
,
673 struct mdoc_argv
*v
, int *pos
, char *buf
)
678 if ('-' == buf
[*pos
])
681 ac
= args(m
, line
, pos
, buf
, ARGSFL_NONE
, &p
);
682 if (ARGS_ERROR
== ac
)
688 v
->value
= mandoc_malloc(sizeof(char *));
689 v
->value
[0] = mandoc_strdup(p
);
695 argv_single(struct mdoc
*m
, int line
,
696 struct mdoc_argv
*v
, int *pos
, char *buf
)
704 ac
= args(m
, line
, pos
, buf
, ARGSFL_NONE
, &p
);
705 if (ARGS_EOLN
== ac
) {
706 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_SYNTARGVCOUNT
);
708 } else if (ARGS_ERROR
== ac
)
712 v
->value
= mandoc_malloc(sizeof(char *));
713 v
->value
[0] = mandoc_strdup(p
);