]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.69 2011/03/17 11:09:36 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 */
35 static enum mdocargt
argv_a2arg(enum mdoct
, const char *);
36 static enum margserr
args(struct mdoc
*, int, int *,
37 char *, int, char **);
38 static int args_checkpunct(struct mdoc
*,
39 const char *, int, int, int);
40 static int argv(struct mdoc
*, int,
41 struct mdoc_argv
*, int *, char *);
42 static int argv_single(struct mdoc
*, int,
43 struct mdoc_argv
*, int *, char *);
44 static int argv_opt_single(struct mdoc
*, int,
45 struct mdoc_argv
*, int *, char *);
46 static int argv_multi(struct mdoc
*, int,
47 struct mdoc_argv
*, int *, char *);
50 ARGV_NONE
, /* no args to flag (e.g., -split) */
51 ARGV_SINGLE
, /* one arg to flag (e.g., -file xxx) */
52 ARGV_MULTI
, /* multiple args (e.g., -column xxx yyy) */
53 ARGV_OPT_SINGLE
/* optional arg (e.g., -offset [xxx]) */
56 static const enum argvflag argvflags
[MDOC_ARG_MAX
] = {
57 ARGV_NONE
, /* MDOC_Split */
58 ARGV_NONE
, /* MDOC_Nosplit */
59 ARGV_NONE
, /* MDOC_Ragged */
60 ARGV_NONE
, /* MDOC_Unfilled */
61 ARGV_NONE
, /* MDOC_Literal */
62 ARGV_SINGLE
, /* MDOC_File */
63 ARGV_OPT_SINGLE
, /* MDOC_Offset */
64 ARGV_NONE
, /* MDOC_Bullet */
65 ARGV_NONE
, /* MDOC_Dash */
66 ARGV_NONE
, /* MDOC_Hyphen */
67 ARGV_NONE
, /* MDOC_Item */
68 ARGV_NONE
, /* MDOC_Enum */
69 ARGV_NONE
, /* MDOC_Tag */
70 ARGV_NONE
, /* MDOC_Diag */
71 ARGV_NONE
, /* MDOC_Hang */
72 ARGV_NONE
, /* MDOC_Ohang */
73 ARGV_NONE
, /* MDOC_Inset */
74 ARGV_MULTI
, /* MDOC_Column */
75 ARGV_SINGLE
, /* MDOC_Width */
76 ARGV_NONE
, /* MDOC_Compact */
77 ARGV_NONE
, /* MDOC_Std */
78 ARGV_NONE
, /* MDOC_Filled */
79 ARGV_NONE
, /* MDOC_Words */
80 ARGV_NONE
, /* MDOC_Emphasis */
81 ARGV_NONE
, /* MDOC_Symbolic */
82 ARGV_NONE
/* MDOC_Symbolic */
85 static const int argflags
[MDOC_MAX
] = {
146 ARGS_DELIM
, /* Bsx */
196 ARGS_DELIM
, /* Brq */
198 ARGS_DELIM
, /* Brc */
211 * Parse an argument from line text. This comes in the form of -key
212 * [value0...], which may either have a single mandatory value, at least
213 * one mandatory value, an optional single value, or no value.
216 mdoc_argv(struct mdoc
*m
, int line
, enum mdoct tok
,
217 struct mdoc_arg
**v
, int *pos
, char *buf
)
220 struct mdoc_argv tmp
;
221 struct mdoc_arg
*arg
;
223 if ('\0' == buf
[*pos
])
226 assert(' ' != buf
[*pos
]);
228 /* Parse through to the first unescaped space. */
236 if (' ' == buf
[*pos
])
237 if ('\\' != buf
[*pos
- 1])
242 /* XXX - save zeroed byte, if not an argument. */
247 buf
[(*pos
)++] = '\0';
250 memset(&tmp
, 0, sizeof(struct mdoc_argv
));
254 /* See if our token accepts the argument. */
256 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
257 /* XXX - restore saved zeroed byte. */
263 while (buf
[*pos
] && ' ' == buf
[*pos
])
266 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
269 if (NULL
== (arg
= *v
))
270 arg
= *v
= mandoc_calloc(1, sizeof(struct mdoc_arg
));
273 arg
->argv
= mandoc_realloc
274 (arg
->argv
, arg
->argc
* sizeof(struct mdoc_argv
));
276 memcpy(&arg
->argv
[(int)arg
->argc
- 1],
277 &tmp
, sizeof(struct mdoc_argv
));
283 mdoc_argv_free(struct mdoc_arg
*p
)
297 for (i
= (int)p
->argc
- 1; i
>= 0; i
--)
298 mdoc_argn_free(p
, i
);
305 mdoc_argn_free(struct mdoc_arg
*p
, int iarg
)
307 struct mdoc_argv
*arg
;
310 arg
= &p
->argv
[iarg
];
312 if (arg
->sz
&& arg
->value
) {
313 for (j
= (int)arg
->sz
- 1; j
>= 0; j
--)
318 for (--p
->argc
; iarg
< (int)p
->argc
; iarg
++)
319 p
->argv
[iarg
] = p
->argv
[iarg
+1];
323 mdoc_zargs(struct mdoc
*m
, int line
, int *pos
,
324 char *buf
, int flags
, char **v
)
327 return(args(m
, line
, pos
, buf
, flags
, v
));
331 mdoc_args(struct mdoc
*m
, int line
, int *pos
,
332 char *buf
, enum mdoct tok
, char **v
)
340 return(args(m
, line
, pos
, buf
, fl
, v
));
343 * We know that we're in an `It', so it's reasonable to expect
344 * us to be sitting in a `Bl'. Someday this may not be the case
345 * (if we allow random `It's sitting out there), so provide a
346 * safe fall-back into the default behaviour.
349 for (n
= m
->last
; n
; n
= n
->parent
)
350 if (MDOC_Bl
== n
->tok
)
353 if (n
&& LIST_column
== n
->norm
->Bl
.type
) {
358 return(args(m
, line
, pos
, buf
, fl
, v
));
362 args(struct mdoc
*m
, int line
, int *pos
,
363 char *buf
, int fl
, char **v
)
369 * Parse out the terms (like `val' in `.Xx -arg val' or simply
370 * `.Xx val'), which can have all sorts of properties:
372 * ARGS_DELIM: use special handling if encountering trailing
373 * delimiters in the form of [[::delim::][ ]+]+.
375 * ARGS_NOWARN: don't post warnings. This is only used when
376 * re-parsing delimiters, as the warnings have already been
379 * ARGS_TABSEP: use special handling for tab/`Ta' separated
380 * phrases like in `Bl -column'.
383 assert(' ' != buf
[*pos
]);
385 if ('\0' == buf
[*pos
]) {
386 if (MDOC_PPHRASE
& m
->flags
)
389 * If we're not in a partial phrase and the flag for
390 * being a phrase literal is still set, the punctuation
393 if (MDOC_PHRASELIT
& m
->flags
)
394 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
);
396 m
->flags
&= ~MDOC_PHRASELIT
;
402 if (ARGS_DELIM
& fl
&& args_checkpunct(m
, buf
, *pos
, line
, fl
))
406 * First handle TABSEP items, restricted to `Bl -column'. This
407 * ignores conventional token parsing and instead uses tabs or
408 * `Ta' macros to separate phrases. Phrases are parsed again
409 * for arguments at a later phase.
412 if (ARGS_TABSEP
& fl
) {
413 /* Scan ahead to tab (can't be escaped). */
414 p
= strchr(*v
, '\t');
417 /* Scan ahead to unescaped `Ta'. */
418 if ( ! (MDOC_PHRASELIT
& m
->flags
))
419 for (pp
= *v
; ; pp
++) {
420 if (NULL
== (pp
= strstr(pp
, "Ta")))
422 if (pp
> *v
&& ' ' != *(pp
- 1))
424 if (' ' == *(pp
+ 2) || '\0' == *(pp
+ 2))
428 /* By default, assume a phrase. */
432 * Adjust new-buffer position to be beyond delimiter
433 * mark (e.g., Ta -> end + 2).
436 *pos
+= pp
< p
? 2 : 1;
437 rc
= pp
< p
? ARGS_PHRASE
: ARGS_PPHRASE
;
439 } else if (p
&& ! pp
) {
442 } else if (pp
&& ! p
) {
450 /* Whitespace check for eoln case... */
451 if ('\0' == *p
&& ' ' == *(p
- 1) && ! (ARGS_NOWARN
& fl
))
452 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
454 *pos
+= (int)(p
- *v
);
456 /* Strip delimiter's preceding whitespace. */
458 while (pp
> *v
&& ' ' == *pp
) {
459 if (pp
> *v
&& '\\' == *(pp
- 1))
465 /* Strip delimiter's proceeding whitespace. */
466 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
473 * Process a quoted literal. A quote begins with a double-quote
474 * and ends with a double-quote NOT preceded by a double-quote.
475 * Whitespace is NOT involved in literal termination.
478 if (MDOC_PHRASELIT
& m
->flags
|| '\"' == buf
[*pos
]) {
479 if ( ! (MDOC_PHRASELIT
& m
->flags
))
482 if (MDOC_PPHRASE
& m
->flags
)
483 m
->flags
|= MDOC_PHRASELIT
;
485 for ( ; buf
[*pos
]; (*pos
)++) {
486 if ('\"' != buf
[*pos
])
488 if ('\"' != buf
[*pos
+ 1])
493 if ('\0' == buf
[*pos
]) {
494 if (ARGS_NOWARN
& fl
|| MDOC_PPHRASE
& m
->flags
)
496 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
);
500 m
->flags
&= ~MDOC_PHRASELIT
;
501 buf
[(*pos
)++] = '\0';
503 if ('\0' == buf
[*pos
])
506 while (' ' == buf
[*pos
])
509 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
510 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
516 * A non-quoted term progresses until either the end of line or
517 * a non-escaped whitespace.
520 for ( ; buf
[*pos
]; (*pos
)++)
521 if (*pos
&& ' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
524 if ('\0' == buf
[*pos
])
527 buf
[(*pos
)++] = '\0';
529 while (' ' == buf
[*pos
])
532 if ('\0' == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
533 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
539 * Check if the string consists only of space-separated closing
540 * delimiters. This is a bit of a dance: the first must be a close
541 * delimiter, but it may be followed by middle delimiters. Arbitrary
542 * whitespace may separate these tokens.
545 args_checkpunct(struct mdoc
*m
, const char *buf
, int i
, int ln
, int fl
)
551 /* First token must be a close-delimiter. */
553 for (j
= 0; buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
; j
++, i
++)
560 if (DELIM_CLOSE
!= mandoc_isdelim(dbuf
))
563 while (' ' == buf
[i
])
566 /* Remaining must NOT be open/none. */
570 while (buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
)
571 dbuf
[j
++] = buf
[i
++];
577 d
= mandoc_isdelim(dbuf
);
578 if (DELIM_NONE
== d
|| DELIM_OPEN
== d
)
581 while (' ' == buf
[i
])
585 if ( ! (ARGS_NOWARN
& fl
) && i
&& ' ' == buf
[i
- 1])
586 mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
);
588 return('\0' == buf
[i
]);
592 * Match up an argument string (e.g., `-foo bar' having "foo") with the
593 * correrct identifier. It must apply to the given macro. If none was
594 * found (including bad matches), return MDOC_ARG_MAX.
597 argv_a2arg(enum mdoct tok
, const char *p
)
599 enum mdocargt args
[MDOC_ARG_MAX
];
606 args
[len
++] = MDOC_Split
;
607 args
[len
++] = MDOC_Nosplit
;
610 args
[len
++] = MDOC_Ragged
;
611 args
[len
++] = MDOC_Unfilled
;
612 args
[len
++] = MDOC_Filled
;
613 args
[len
++] = MDOC_Literal
;
614 args
[len
++] = MDOC_File
;
615 args
[len
++] = MDOC_Offset
;
616 args
[len
++] = MDOC_Compact
;
617 args
[len
++] = MDOC_Centred
;
620 args
[len
++] = MDOC_Emphasis
;
621 args
[len
++] = MDOC_Literal
;
622 args
[len
++] = MDOC_Symbolic
;
625 args
[len
++] = MDOC_Words
;
628 args
[len
++] = MDOC_Bullet
;
629 args
[len
++] = MDOC_Dash
;
630 args
[len
++] = MDOC_Hyphen
;
631 args
[len
++] = MDOC_Item
;
632 args
[len
++] = MDOC_Enum
;
633 args
[len
++] = MDOC_Tag
;
634 args
[len
++] = MDOC_Diag
;
635 args
[len
++] = MDOC_Hang
;
636 args
[len
++] = MDOC_Ohang
;
637 args
[len
++] = MDOC_Inset
;
638 args
[len
++] = MDOC_Column
;
639 args
[len
++] = MDOC_Width
;
640 args
[len
++] = MDOC_Offset
;
641 args
[len
++] = MDOC_Compact
;
642 args
[len
++] = MDOC_Nested
;
647 args
[len
++] = MDOC_Std
;
653 for (i
= 0; i
< len
; i
++)
654 if (0 == strcmp(p
, mdoc_argnames
[args
[i
]]))
657 return(MDOC_ARG_MAX
);
661 argv_multi(struct mdoc
*m
, int line
,
662 struct mdoc_argv
*v
, int *pos
, char *buf
)
667 for (v
->sz
= 0; ; v
->sz
++) {
668 if ('-' == buf
[*pos
])
670 ac
= args(m
, line
, pos
, buf
, 0, &p
);
671 if (ARGS_ERROR
== ac
)
673 else if (ARGS_EOLN
== ac
)
676 if (0 == v
->sz
% MULTI_STEP
)
677 v
->value
= mandoc_realloc(v
->value
,
678 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
680 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
687 argv_opt_single(struct mdoc
*m
, int line
,
688 struct mdoc_argv
*v
, int *pos
, char *buf
)
693 if ('-' == buf
[*pos
])
696 ac
= args(m
, line
, pos
, buf
, 0, &p
);
697 if (ARGS_ERROR
== ac
)
703 v
->value
= mandoc_malloc(sizeof(char *));
704 v
->value
[0] = mandoc_strdup(p
);
710 * Parse a single, mandatory value from the stream.
713 argv_single(struct mdoc
*m
, int line
,
714 struct mdoc_argv
*v
, int *pos
, char *buf
)
722 ac
= args(m
, line
, pos
, buf
, 0, &p
);
723 if (ARGS_EOLN
== ac
) {
724 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_SYNTARGVCOUNT
);
726 } else if (ARGS_ERROR
== ac
)
730 v
->value
= mandoc_malloc(sizeof(char *));
731 v
->value
[0] = mandoc_strdup(p
);
737 * Determine rules for parsing arguments. Arguments can either accept
738 * no parameters, an optional single parameter, one parameter, or
739 * multiple parameters.
742 argv(struct mdoc
*mdoc
, int line
,
743 struct mdoc_argv
*v
, int *pos
, char *buf
)
749 switch (argvflags
[v
->arg
]) {
751 return(argv_single(mdoc
, line
, v
, pos
, buf
));
753 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
754 case (ARGV_OPT_SINGLE
):
755 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));