]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.74 2011/04/07 01:08:42 joerg 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>
32 #include "libmandoc.h"
34 #define MULTI_STEP 5 /* pre-allocate argument values */
36 static enum mdocargt
argv_a2arg(enum mdoct
, const char *);
37 static enum margserr
args(struct mdoc
*, int, int *,
38 char *, int, char **);
39 static int args_checkpunct(struct mdoc
*,
40 const char *, int, int, int);
41 static int argv(struct mdoc
*, int,
42 struct mdoc_argv
*, int *, char *);
43 static int argv_single(struct mdoc
*, int,
44 struct mdoc_argv
*, int *, char *);
45 static int argv_opt_single(struct mdoc
*, int,
46 struct mdoc_argv
*, int *, char *);
47 static int argv_multi(struct mdoc
*, int,
48 struct mdoc_argv
*, int *, char *);
49 static void argn_free(struct mdoc_arg
*, int);
52 ARGV_NONE
, /* no args to flag (e.g., -split) */
53 ARGV_SINGLE
, /* one arg to flag (e.g., -file xxx) */
54 ARGV_MULTI
, /* multiple args (e.g., -column xxx yyy) */
55 ARGV_OPT_SINGLE
/* optional arg (e.g., -offset [xxx]) */
58 static const enum argvflag argvflags
[MDOC_ARG_MAX
] = {
59 ARGV_NONE
, /* MDOC_Split */
60 ARGV_NONE
, /* MDOC_Nosplit */
61 ARGV_NONE
, /* MDOC_Ragged */
62 ARGV_NONE
, /* MDOC_Unfilled */
63 ARGV_NONE
, /* MDOC_Literal */
64 ARGV_SINGLE
, /* MDOC_File */
65 ARGV_OPT_SINGLE
, /* MDOC_Offset */
66 ARGV_NONE
, /* MDOC_Bullet */
67 ARGV_NONE
, /* MDOC_Dash */
68 ARGV_NONE
, /* MDOC_Hyphen */
69 ARGV_NONE
, /* MDOC_Item */
70 ARGV_NONE
, /* MDOC_Enum */
71 ARGV_NONE
, /* MDOC_Tag */
72 ARGV_NONE
, /* MDOC_Diag */
73 ARGV_NONE
, /* MDOC_Hang */
74 ARGV_NONE
, /* MDOC_Ohang */
75 ARGV_NONE
, /* MDOC_Inset */
76 ARGV_MULTI
, /* MDOC_Column */
77 ARGV_SINGLE
, /* MDOC_Width */
78 ARGV_NONE
, /* MDOC_Compact */
79 ARGV_NONE
, /* MDOC_Std */
80 ARGV_NONE
, /* MDOC_Filled */
81 ARGV_NONE
, /* MDOC_Words */
82 ARGV_NONE
, /* MDOC_Emphasis */
83 ARGV_NONE
, /* MDOC_Symbolic */
84 ARGV_NONE
/* MDOC_Symbolic */
87 static const int argflags
[MDOC_MAX
] = {
148 ARGS_DELIM
, /* Bsx */
198 ARGS_DELIM
, /* Brq */
200 ARGS_DELIM
, /* Brc */
212 static const enum mdocargt args_Ex
[] = {
217 static const enum mdocargt args_An
[] = {
223 static const enum mdocargt args_Bd
[] = {
235 static const enum mdocargt args_Bf
[] = {
242 static const enum mdocargt args_Bk
[] = {
247 static const enum mdocargt args_Bl
[] = {
267 * Parse an argument from line text. This comes in the form of -key
268 * [value0...], which may either have a single mandatory value, at least
269 * one mandatory value, an optional single value, or no value.
272 mdoc_argv(struct mdoc
*m
, int line
, enum mdoct tok
,
273 struct mdoc_arg
**v
, int *pos
, char *buf
)
276 struct mdoc_argv tmp
;
277 struct mdoc_arg
*arg
;
279 if ('\0' == buf
[*pos
])
282 assert(' ' != buf
[*pos
]);
284 /* Parse through to the first unescaped space. */
292 if (' ' == buf
[*pos
])
293 if ('\\' != buf
[*pos
- 1])
298 /* XXX - save zeroed byte, if not an argument. */
303 buf
[(*pos
)++] = '\0';
306 memset(&tmp
, 0, sizeof(struct mdoc_argv
));
310 /* See if our token accepts the argument. */
312 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
313 /* XXX - restore saved zeroed byte. */
319 while (buf
[*pos
] && ' ' == buf
[*pos
])
322 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
325 if (NULL
== (arg
= *v
))
326 arg
= *v
= mandoc_calloc(1, sizeof(struct mdoc_arg
));
329 arg
->argv
= mandoc_realloc
330 (arg
->argv
, arg
->argc
* sizeof(struct mdoc_argv
));
332 memcpy(&arg
->argv
[(int)arg
->argc
- 1],
333 &tmp
, sizeof(struct mdoc_argv
));
339 mdoc_argv_free(struct mdoc_arg
*p
)
353 for (i
= (int)p
->argc
- 1; i
>= 0; i
--)
361 argn_free(struct mdoc_arg
*p
, int iarg
)
363 struct mdoc_argv
*arg
;
366 arg
= &p
->argv
[iarg
];
368 if (arg
->sz
&& arg
->value
) {
369 for (j
= (int)arg
->sz
- 1; j
>= 0; j
--)
374 for (--p
->argc
; iarg
< (int)p
->argc
; iarg
++)
375 p
->argv
[iarg
] = p
->argv
[iarg
+1];
379 mdoc_zargs(struct mdoc
*m
, int line
, int *pos
,
380 char *buf
, int flags
, char **v
)
383 return(args(m
, line
, pos
, buf
, flags
, v
));
387 mdoc_args(struct mdoc
*m
, int line
, int *pos
,
388 char *buf
, enum mdoct tok
, char **v
)
396 return(args(m
, line
, pos
, buf
, fl
, v
));
399 * We know that we're in an `It', so it's reasonable to expect
400 * us to be sitting in a `Bl'. Someday this may not be the case
401 * (if we allow random `It's sitting out there), so provide a
402 * safe fall-back into the default behaviour.
405 for (n
= m
->last
; n
; n
= n
->parent
)
406 if (MDOC_Bl
== n
->tok
)
409 if (n
&& LIST_column
== n
->norm
->Bl
.type
) {
414 return(args(m
, line
, pos
, buf
, fl
, v
));
418 args(struct mdoc
*m
, int line
, int *pos
,
419 char *buf
, int fl
, char **v
)
425 * Parse out the terms (like `val' in `.Xx -arg val' or simply
426 * `.Xx val'), which can have all sorts of properties:
428 * ARGS_DELIM: use special handling if encountering trailing
429 * delimiters in the form of [[::delim::][ ]+]+.
431 * ARGS_NOWARN: don't post warnings. This is only used when
432 * re-parsing delimiters, as the warnings have already been
435 * ARGS_TABSEP: use special handling for tab/`Ta' separated
436 * phrases like in `Bl -column'.
439 assert(' ' != buf
[*pos
]);
441 if ('\0' == buf
[*pos
]) {
442 if (MDOC_PPHRASE
& m
->flags
)
445 * If we're not in a partial phrase and the flag for
446 * being a phrase literal is still set, the punctuation
449 if (MDOC_PHRASELIT
& m
->flags
)
450 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
);
452 m
->flags
&= ~MDOC_PHRASELIT
;
458 if (ARGS_DELIM
& fl
&& args_checkpunct(m
, buf
, *pos
, line
, fl
))
462 * First handle TABSEP items, restricted to `Bl -column'. This
463 * ignores conventional token parsing and instead uses tabs or
464 * `Ta' macros to separate phrases. Phrases are parsed again
465 * for arguments at a later phase.
468 if (ARGS_TABSEP
& fl
) {
469 /* Scan ahead to tab (can't be escaped). */
470 p
= strchr(*v
, '\t');
473 /* Scan ahead to unescaped `Ta'. */
474 if ( ! (MDOC_PHRASELIT
& m
->flags
))
475 for (pp
= *v
; ; pp
++) {
476 if (NULL
== (pp
= strstr(pp
, "Ta")))
478 if (pp
> *v
&& ' ' != *(pp
- 1))
480 if (' ' == *(pp
+ 2) || '\0' == *(pp
+ 2))
484 /* By default, assume a phrase. */
488 * Adjust new-buffer position to be beyond delimiter
489 * mark (e.g., Ta -> end + 2).
492 *pos
+= pp
< p
? 2 : 1;
493 rc
= pp
< p
? ARGS_PHRASE
: ARGS_PPHRASE
;
495 } else if (p
&& ! pp
) {
498 } else if (pp
&& ! p
) {
506 /* Whitespace check for eoln case... */
507 if ('\0' == *p
&& ' ' == *(p
- 1) && ! (ARGS_NOWARN
& fl
))
508 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
510 *pos
+= (int)(p
- *v
);
512 /* Strip delimiter's preceding whitespace. */
514 while (pp
> *v
&& ' ' == *pp
) {
515 if (pp
> *v
&& '\\' == *(pp
- 1))
521 /* Strip delimiter's proceeding whitespace. */
522 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
529 * Process a quoted literal. A quote begins with a double-quote
530 * and ends with a double-quote NOT preceded by a double-quote.
531 * Whitespace is NOT involved in literal termination.
534 if (MDOC_PHRASELIT
& m
->flags
|| '\"' == buf
[*pos
]) {
535 if ( ! (MDOC_PHRASELIT
& m
->flags
))
538 if (MDOC_PPHRASE
& m
->flags
)
539 m
->flags
|= MDOC_PHRASELIT
;
541 for ( ; buf
[*pos
]; (*pos
)++) {
542 if ('\"' != buf
[*pos
])
544 if ('\"' != buf
[*pos
+ 1])
549 if ('\0' == buf
[*pos
]) {
550 if (ARGS_NOWARN
& fl
|| MDOC_PPHRASE
& m
->flags
)
552 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
);
556 m
->flags
&= ~MDOC_PHRASELIT
;
557 buf
[(*pos
)++] = '\0';
559 if ('\0' == buf
[*pos
])
562 while (' ' == buf
[*pos
])
565 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
566 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
572 * A non-quoted term progresses until either the end of line or
573 * a non-escaped whitespace.
576 for ( ; buf
[*pos
]; (*pos
)++)
577 if (*pos
&& ' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
580 if ('\0' == buf
[*pos
])
583 buf
[(*pos
)++] = '\0';
585 while (' ' == buf
[*pos
])
588 if ('\0' == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
589 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
595 * Check if the string consists only of space-separated closing
596 * delimiters. This is a bit of a dance: the first must be a close
597 * delimiter, but it may be followed by middle delimiters. Arbitrary
598 * whitespace may separate these tokens.
601 args_checkpunct(struct mdoc
*m
, const char *buf
, int i
, int ln
, int fl
)
607 /* First token must be a close-delimiter. */
609 for (j
= 0; buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
; j
++, i
++)
616 if (DELIM_CLOSE
!= mdoc_isdelim(dbuf
))
619 while (' ' == buf
[i
])
622 /* Remaining must NOT be open/none. */
626 while (buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
)
627 dbuf
[j
++] = buf
[i
++];
633 d
= mdoc_isdelim(dbuf
);
634 if (DELIM_NONE
== d
|| DELIM_OPEN
== d
)
637 while (' ' == buf
[i
])
641 if ( ! (ARGS_NOWARN
& fl
) && i
&& ' ' == buf
[i
- 1])
642 mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
);
644 return('\0' == buf
[i
]);
648 * Match up an argument string (e.g., `-foo bar' having "foo") with the
649 * correrct identifier. It must apply to the given macro. If none was
650 * found (including bad matches), return MDOC_ARG_MAX.
653 argv_a2arg(enum mdoct tok
, const char *p
)
655 const enum mdocargt
*argsp
;
681 return(MDOC_ARG_MAX
);
686 for ( ; MDOC_ARG_MAX
!= *argsp
; argsp
++)
687 if (0 == strcmp(p
, mdoc_argnames
[*argsp
]))
690 return(MDOC_ARG_MAX
);
694 argv_multi(struct mdoc
*m
, int line
,
695 struct mdoc_argv
*v
, int *pos
, char *buf
)
700 for (v
->sz
= 0; ; v
->sz
++) {
701 if ('-' == buf
[*pos
])
703 ac
= args(m
, line
, pos
, buf
, 0, &p
);
704 if (ARGS_ERROR
== ac
)
706 else if (ARGS_EOLN
== ac
)
709 if (0 == v
->sz
% MULTI_STEP
)
710 v
->value
= mandoc_realloc(v
->value
,
711 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
713 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
720 argv_opt_single(struct mdoc
*m
, int line
,
721 struct mdoc_argv
*v
, int *pos
, char *buf
)
726 if ('-' == buf
[*pos
])
729 ac
= args(m
, line
, pos
, buf
, 0, &p
);
730 if (ARGS_ERROR
== ac
)
736 v
->value
= mandoc_malloc(sizeof(char *));
737 v
->value
[0] = mandoc_strdup(p
);
743 * Parse a single, mandatory value from the stream.
746 argv_single(struct mdoc
*m
, int line
,
747 struct mdoc_argv
*v
, int *pos
, char *buf
)
755 ac
= args(m
, line
, pos
, buf
, 0, &p
);
756 if (ARGS_EOLN
== ac
) {
757 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_SYNTARGVCOUNT
);
759 } else if (ARGS_ERROR
== ac
)
763 v
->value
= mandoc_malloc(sizeof(char *));
764 v
->value
[0] = mandoc_strdup(p
);
770 * Determine rules for parsing arguments. Arguments can either accept
771 * no parameters, an optional single parameter, one parameter, or
772 * multiple parameters.
775 argv(struct mdoc
*mdoc
, int line
,
776 struct mdoc_argv
*v
, int *pos
, char *buf
)
782 switch (argvflags
[v
->arg
]) {
784 return(argv_single(mdoc
, line
, v
, pos
, buf
));
786 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
787 case (ARGV_OPT_SINGLE
):
788 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));