]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.60 2010/09/13 20:15:43 schwarze 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"
34 * Routines to parse arguments of macros. Arguments follow the syntax
35 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
36 * arguments, multiple arguments per value, no-value arguments, etc.
38 * There's no limit to the number or arguments that may be allocated.
41 #define ARGV_NONE (1 << 0)
42 #define ARGV_SINGLE (1 << 1)
43 #define ARGV_MULTI (1 << 2)
44 #define ARGV_OPT_SINGLE (1 << 3)
48 static enum mdocargt
argv_a2arg(enum mdoct
, const char *);
49 static enum margserr
args(struct mdoc
*, int, int *,
50 char *, int, char **);
51 static int argv(struct mdoc
*, int,
52 struct mdoc_argv
*, int *, char *);
53 static int argv_single(struct mdoc
*, int,
54 struct mdoc_argv
*, int *, char *);
55 static int argv_opt_single(struct mdoc
*, int,
56 struct mdoc_argv
*, int *, char *);
57 static int argv_multi(struct mdoc
*, int,
58 struct mdoc_argv
*, int *, char *);
60 /* Per-argument flags. */
62 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
63 ARGV_NONE
, /* MDOC_Split */
64 ARGV_NONE
, /* MDOC_Nosplit */
65 ARGV_NONE
, /* MDOC_Ragged */
66 ARGV_NONE
, /* MDOC_Unfilled */
67 ARGV_NONE
, /* MDOC_Literal */
68 ARGV_SINGLE
, /* MDOC_File */
69 ARGV_OPT_SINGLE
, /* MDOC_Offset */
70 ARGV_NONE
, /* MDOC_Bullet */
71 ARGV_NONE
, /* MDOC_Dash */
72 ARGV_NONE
, /* MDOC_Hyphen */
73 ARGV_NONE
, /* MDOC_Item */
74 ARGV_NONE
, /* MDOC_Enum */
75 ARGV_NONE
, /* MDOC_Tag */
76 ARGV_NONE
, /* MDOC_Diag */
77 ARGV_NONE
, /* MDOC_Hang */
78 ARGV_NONE
, /* MDOC_Ohang */
79 ARGV_NONE
, /* MDOC_Inset */
80 ARGV_MULTI
, /* MDOC_Column */
81 ARGV_SINGLE
, /* MDOC_Width */
82 ARGV_NONE
, /* MDOC_Compact */
83 ARGV_NONE
, /* MDOC_Std */
84 ARGV_NONE
, /* MDOC_Filled */
85 ARGV_NONE
, /* MDOC_Words */
86 ARGV_NONE
, /* MDOC_Emphasis */
87 ARGV_NONE
, /* MDOC_Symbolic */
88 ARGV_NONE
/* MDOC_Symbolic */
91 static int mdoc_argflags
[MDOC_MAX
] = {
152 ARGS_DELIM
, /* Bsx */
202 ARGS_DELIM
, /* Brq */
204 ARGS_DELIM
, /* Brc */
218 * Parse an argument from line text. This comes in the form of -key
219 * [value0...], which may either have a single mandatory value, at least
220 * one mandatory value, an optional single value, or no value.
223 mdoc_argv(struct mdoc
*m
, int line
, enum mdoct tok
,
224 struct mdoc_arg
**v
, int *pos
, char *buf
)
227 struct mdoc_argv tmp
;
228 struct mdoc_arg
*arg
;
230 if ('\0' == buf
[*pos
])
233 assert(' ' != buf
[*pos
]);
235 /* Parse through to the first unescaped space. */
243 if (' ' == buf
[*pos
])
244 if ('\\' != buf
[*pos
- 1])
249 /* XXX - save zeroed byte, if not an argument. */
254 buf
[(*pos
)++] = '\0';
257 (void)memset(&tmp
, 0, sizeof(struct mdoc_argv
));
261 /* See if our token accepts the argument. */
263 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
264 /* XXX - restore saved zeroed byte. */
270 while (buf
[*pos
] && ' ' == buf
[*pos
])
273 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
276 if (NULL
== (arg
= *v
))
277 arg
= *v
= mandoc_calloc(1, sizeof(struct mdoc_arg
));
280 arg
->argv
= mandoc_realloc
281 (arg
->argv
, arg
->argc
* sizeof(struct mdoc_argv
));
283 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
284 &tmp
, sizeof(struct mdoc_argv
));
291 mdoc_argv_free(struct mdoc_arg
*p
)
305 for (i
= (int)p
->argc
- 1; i
>= 0; i
--)
306 mdoc_argn_free(p
, i
);
314 mdoc_argn_free(struct mdoc_arg
*p
, int iarg
)
316 struct mdoc_argv
*arg
;
319 arg
= &p
->argv
[iarg
];
321 if (arg
->sz
&& arg
->value
) {
322 for (j
= (int)arg
->sz
- 1; j
>= 0; j
--)
327 for (--p
->argc
; iarg
< (int)p
->argc
; iarg
++)
328 p
->argv
[iarg
] = p
->argv
[iarg
+1];
333 mdoc_zargs(struct mdoc
*m
, int line
, int *pos
,
334 char *buf
, int flags
, char **v
)
337 return(args(m
, line
, pos
, buf
, flags
, v
));
342 mdoc_args(struct mdoc
*m
, int line
, int *pos
,
343 char *buf
, enum mdoct tok
, char **v
)
348 fl
= mdoc_argflags
[tok
];
351 return(args(m
, line
, pos
, buf
, fl
, v
));
354 * We know that we're in an `It', so it's reasonable to expect
355 * us to be sitting in a `Bl'. Someday this may not be the case
356 * (if we allow random `It's sitting out there), so provide a
357 * safe fall-back into the default behaviour.
360 for (n
= m
->last
; n
; n
= n
->parent
)
361 if (MDOC_Bl
== n
->tok
)
365 if (n
&& LIST_column
== n
->data
.Bl
->type
) {
370 return(args(m
, line
, pos
, buf
, fl
, v
));
375 args(struct mdoc
*m
, int line
, int *pos
,
376 char *buf
, int fl
, char **v
)
384 * Parse out the terms (like `val' in `.Xx -arg val' or simply
385 * `.Xx val'), which can have all sorts of properties:
387 * ARGS_DELIM: use special handling if encountering trailing
388 * delimiters in the form of [[::delim::][ ]+]+.
390 * ARGS_NOWARN: don't post warnings. This is only used when
391 * re-parsing delimiters, as the warnings have already been
394 * ARGS_TABSEP: use special handling for tab/`Ta' separated
395 * phrases like in `Bl -column'.
398 assert(' ' != buf
[*pos
]);
400 if ('\0' == buf
[*pos
]) {
401 if (MDOC_PPHRASE
& m
->flags
)
404 * If we're not in a partial phrase and the flag for
405 * being a phrase literal is still set, the punctuation
408 if (MDOC_PHRASELIT
& m
->flags
)
409 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
))
412 m
->flags
&= ~MDOC_PHRASELIT
;
417 * If the first character is a closing delimiter and we're to
418 * look for delimited strings, then pass down the buffer seeing
419 * if it follows the pattern of [[::delim::][ ]+]+. Note that
420 * we ONLY care about closing delimiters.
423 if ((fl
& ARGS_DELIM
) && DELIM_CLOSE
== mdoc_iscdelim(buf
[*pos
])) {
424 for (i
= *pos
; buf
[i
]; ) {
425 d
= mdoc_iscdelim(buf
[i
]);
426 if (DELIM_NONE
== d
|| DELIM_OPEN
== d
)
429 if ('\0' == buf
[i
] || ' ' != buf
[i
])
432 while (buf
[i
] && ' ' == buf
[i
])
436 if ('\0' == buf
[i
]) {
438 if (i
&& ' ' != buf
[i
- 1])
440 if (ARGS_NOWARN
& fl
)
442 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
))
451 * First handle TABSEP items, restricted to `Bl -column'. This
452 * ignores conventional token parsing and instead uses tabs or
453 * `Ta' macros to separate phrases. Phrases are parsed again
454 * for arguments at a later phase.
457 if (ARGS_TABSEP
& fl
) {
458 /* Scan ahead to tab (can't be escaped). */
459 p
= strchr(*v
, '\t');
462 /* Scan ahead to unescaped `Ta'. */
463 if ( ! (MDOC_PHRASELIT
& m
->flags
))
464 for (pp
= *v
; ; pp
++) {
465 if (NULL
== (pp
= strstr(pp
, "Ta")))
467 if (pp
> *v
&& ' ' != *(pp
- 1))
469 if (' ' == *(pp
+ 2) || '\0' == *(pp
+ 2))
473 /* By default, assume a phrase. */
477 * Adjust new-buffer position to be beyond delimiter
478 * mark (e.g., Ta -> end + 2).
481 *pos
+= pp
< p
? 2 : 1;
482 rc
= pp
< p
? ARGS_PHRASE
: ARGS_PPHRASE
;
484 } else if (p
&& ! pp
) {
487 } else if (pp
&& ! p
) {
495 /* Whitespace check for eoln case... */
496 if ('\0' == *p
&& ' ' == *(p
- 1) && ! (ARGS_NOWARN
& fl
))
497 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
))
500 *pos
+= (int)(p
- *v
);
502 /* Strip delimiter's preceding whitespace. */
504 while (pp
> *v
&& ' ' == *pp
) {
505 if (pp
> *v
&& '\\' == *(pp
- 1))
511 /* Strip delimiter's proceeding whitespace. */
512 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
519 * Process a quoted literal. A quote begins with a double-quote
520 * and ends with a double-quote NOT preceded by a double-quote.
521 * Whitespace is NOT involved in literal termination.
524 if (MDOC_PHRASELIT
& m
->flags
|| '\"' == buf
[*pos
]) {
525 if ( ! (MDOC_PHRASELIT
& m
->flags
))
528 if (MDOC_PPHRASE
& m
->flags
)
529 m
->flags
|= MDOC_PHRASELIT
;
531 for ( ; buf
[*pos
]; (*pos
)++) {
532 if ('\"' != buf
[*pos
])
534 if ('\"' != buf
[*pos
+ 1])
539 if ('\0' == buf
[*pos
]) {
540 if (ARGS_NOWARN
& fl
|| MDOC_PPHRASE
& m
->flags
)
542 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
))
547 m
->flags
&= ~MDOC_PHRASELIT
;
548 buf
[(*pos
)++] = '\0';
550 if ('\0' == buf
[*pos
])
553 while (' ' == buf
[*pos
])
556 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
557 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
))
564 * A non-quoted term progresses until either the end of line or
565 * a non-escaped whitespace.
568 for ( ; buf
[*pos
]; (*pos
)++)
569 if (*pos
&& ' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
572 if ('\0' == buf
[*pos
])
575 buf
[(*pos
)++] = '\0';
577 while (' ' == buf
[*pos
])
580 if ('\0' == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
581 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
))
589 argv_a2arg(enum mdoct tok
, const char *p
)
593 * Parse an argument identifier from its text. XXX - this
594 * should really be table-driven to clarify the code.
596 * If you add an argument to the list, make sure that you
597 * register it here with its one or more macros!
602 if (0 == strcmp(p
, "split"))
604 else if (0 == strcmp(p
, "nosplit"))
605 return(MDOC_Nosplit
);
609 if (0 == strcmp(p
, "ragged"))
611 else if (0 == strcmp(p
, "unfilled"))
612 return(MDOC_Unfilled
);
613 else if (0 == strcmp(p
, "filled"))
615 else if (0 == strcmp(p
, "literal"))
616 return(MDOC_Literal
);
617 else if (0 == strcmp(p
, "file"))
619 else if (0 == strcmp(p
, "offset"))
621 else if (0 == strcmp(p
, "compact"))
622 return(MDOC_Compact
);
623 else if (0 == strcmp(p
, "centered"))
624 return(MDOC_Centred
);
628 if (0 == strcmp(p
, "emphasis"))
629 return(MDOC_Emphasis
);
630 else if (0 == strcmp(p
, "literal"))
631 return(MDOC_Literal
);
632 else if (0 == strcmp(p
, "symbolic"))
633 return(MDOC_Symbolic
);
637 if (0 == strcmp(p
, "words"))
642 if (0 == strcmp(p
, "bullet"))
644 else if (0 == strcmp(p
, "dash"))
646 else if (0 == strcmp(p
, "hyphen"))
648 else if (0 == strcmp(p
, "item"))
650 else if (0 == strcmp(p
, "enum"))
652 else if (0 == strcmp(p
, "tag"))
654 else if (0 == strcmp(p
, "diag"))
656 else if (0 == strcmp(p
, "hang"))
658 else if (0 == strcmp(p
, "ohang"))
660 else if (0 == strcmp(p
, "inset"))
662 else if (0 == strcmp(p
, "column"))
664 else if (0 == strcmp(p
, "width"))
666 else if (0 == strcmp(p
, "offset"))
668 else if (0 == strcmp(p
, "compact"))
669 return(MDOC_Compact
);
670 else if (0 == strcmp(p
, "nested"))
677 if (0 == strcmp(p
, "std"))
684 return(MDOC_ARG_MAX
);
689 argv_multi(struct mdoc
*m
, int line
,
690 struct mdoc_argv
*v
, int *pos
, char *buf
)
695 for (v
->sz
= 0; ; v
->sz
++) {
696 if ('-' == buf
[*pos
])
698 ac
= args(m
, line
, pos
, buf
, 0, &p
);
699 if (ARGS_ERROR
== ac
)
701 else if (ARGS_EOLN
== ac
)
704 if (0 == v
->sz
% MULTI_STEP
)
705 v
->value
= mandoc_realloc(v
->value
,
706 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
708 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
716 argv_opt_single(struct mdoc
*m
, int line
,
717 struct mdoc_argv
*v
, int *pos
, char *buf
)
722 if ('-' == buf
[*pos
])
725 ac
= args(m
, line
, pos
, buf
, 0, &p
);
726 if (ARGS_ERROR
== ac
)
732 v
->value
= mandoc_malloc(sizeof(char *));
733 v
->value
[0] = mandoc_strdup(p
);
740 * Parse a single, mandatory value from the stream.
743 argv_single(struct mdoc
*m
, int line
,
744 struct mdoc_argv
*v
, int *pos
, char *buf
)
752 ac
= args(m
, line
, pos
, buf
, 0, &p
);
753 if (ARGS_EOLN
== ac
) {
754 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_SYNTARGVCOUNT
);
756 } else if (ARGS_ERROR
== ac
)
760 v
->value
= mandoc_malloc(sizeof(char *));
761 v
->value
[0] = mandoc_strdup(p
);
768 * Determine rules for parsing arguments. Arguments can either accept
769 * no parameters, an optional single parameter, one parameter, or
770 * multiple parameters.
773 argv(struct mdoc
*mdoc
, int line
,
774 struct mdoc_argv
*v
, int *pos
, char *buf
)
780 switch (mdoc_argvflags
[v
->arg
]) {
782 return(argv_single(mdoc
, line
, v
, pos
, buf
));
784 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
785 case (ARGV_OPT_SINGLE
):
786 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));