]>
git.cameronkatri.com Git - mandoc.git/blob - argv.c
9cd5350307cb90026731725c37f4550acde1bb42
1 /* $Id: argv.c,v 1.40 2009/03/08 12:40:27 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
29 * Routines to parse arguments of macros. Arguments follow the syntax
30 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
31 * arguments, multiple arguments per value, no-value arguments, etc.
33 * There's no limit to the number or arguments that may be allocated.
36 #define ARGS_QUOTED (1 << 0)
37 #define ARGS_DELIM (1 << 1)
38 #define ARGS_TABSEP (1 << 2)
39 #define ARGS_ARGVLIKE (1 << 3)
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)
58 static int argv_a2arg(int, const char *);
59 static int args(struct mdoc
*, int, int *,
60 char *, int, char **);
61 static int argv(struct mdoc
*, int, int,
62 struct mdoc_argv
*, int *, char *);
63 static int argv_single(struct mdoc
*, int,
64 struct mdoc_argv
*, int *, char *);
65 static int argv_opt_single(struct mdoc
*, int,
66 struct mdoc_argv
*, int *, char *);
67 static int argv_multi(struct mdoc
*, int,
68 struct mdoc_argv
*, int *, char *);
69 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
70 static int perr(struct mdoc
*, int, int, enum merr
);
72 /* Per-argument flags. */
74 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
75 ARGV_NONE
, /* MDOC_Split */
76 ARGV_NONE
, /* MDOC_Nosplit */
77 ARGV_NONE
, /* MDOC_Ragged */
78 ARGV_NONE
, /* MDOC_Unfilled */
79 ARGV_NONE
, /* MDOC_Literal */
80 ARGV_NONE
, /* MDOC_File */
81 ARGV_SINGLE
, /* MDOC_Offset */
82 ARGV_NONE
, /* MDOC_Bullet */
83 ARGV_NONE
, /* MDOC_Dash */
84 ARGV_NONE
, /* MDOC_Hyphen */
85 ARGV_NONE
, /* MDOC_Item */
86 ARGV_NONE
, /* MDOC_Enum */
87 ARGV_NONE
, /* MDOC_Tag */
88 ARGV_NONE
, /* MDOC_Diag */
89 ARGV_NONE
, /* MDOC_Hang */
90 ARGV_NONE
, /* MDOC_Ohang */
91 ARGV_NONE
, /* MDOC_Inset */
92 ARGV_MULTI
, /* MDOC_Column */
93 ARGV_SINGLE
, /* MDOC_Width */
94 ARGV_NONE
, /* MDOC_Compact */
95 ARGV_SINGLE
, /* MDOC_Std */
96 ARGV_NONE
, /* MDOC_Filled */
97 ARGV_NONE
, /* MDOC_Words */
98 ARGV_NONE
, /* MDOC_Emphasis */
99 ARGV_NONE
/* MDOC_Symbolic */
102 static int mdoc_argflags
[MDOC_MAX
] = {
120 ARGS_QUOTED
, /* Cd */
126 ARGS_DELIM
| ARGS_QUOTED
, /* Fa */
129 ARGS_DELIM
| ARGS_QUOTED
, /* Fn */
130 ARGS_DELIM
| ARGS_QUOTED
, /* Ft */
140 ARGS_DELIM
| ARGS_ARGVLIKE
, /* St */
144 ARGS_QUOTED
, /* %A */
145 ARGS_QUOTED
, /* %B */
146 ARGS_QUOTED
, /* %D */
147 ARGS_QUOTED
, /* %I */
148 ARGS_QUOTED
, /* %J */
149 ARGS_QUOTED
, /* %N */
150 ARGS_QUOTED
, /* %O */
151 ARGS_QUOTED
, /* %P */
152 ARGS_QUOTED
, /* %R */
153 ARGS_QUOTED
, /* %T */
154 ARGS_QUOTED
, /* %V */
163 ARGS_DELIM
, /* Bsx */
214 * Parse an argument from line text. This comes in the form of -key
215 * [value0...], which may either have a single mandatory value, at least
216 * one mandatory value, an optional single value, or no value.
219 mdoc_argv(struct mdoc
*mdoc
, int line
, int tok
,
220 struct mdoc_arg
**v
, int *pos
, char *buf
)
224 struct mdoc_argv tmp
;
225 struct mdoc_arg
*arg
;
230 assert( ! isspace((u_char
)buf
[*pos
]));
232 if ('-' != buf
[*pos
])
242 if (isspace((u_char
)buf
[*pos
]))
243 if ('\\' != buf
[*pos
- 1])
251 (void)memset(&tmp
, 0, sizeof(struct mdoc_argv
));
256 * We now parse out the per-macro arguments. XXX - this can be
257 * made much cleaner using per-argument tables. See argv_a2arg
261 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
262 if ( ! pwarn(mdoc
, line
, i
, WARGVPARM
))
267 while (buf
[*pos
] && isspace((u_char
)buf
[*pos
]))
270 /* FIXME: whitespace if no value. */
272 if ( ! argv(mdoc
, tok
, line
, &tmp
, pos
, buf
))
275 if (NULL
== (arg
= *v
)) {
276 *v
= xcalloc(1, sizeof(struct mdoc_arg
));
281 arg
->argv
= xrealloc(arg
->argv
, arg
->argc
*
282 sizeof(struct mdoc_argv
));
284 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
285 &tmp
, sizeof(struct mdoc_argv
));
292 mdoc_argv_free(struct mdoc_arg
*p
)
305 for (i
= 0; i
< (int)p
->argc
; i
++) {
306 if (0 == p
->argv
[i
].sz
)
309 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
310 free(p
->argv
[i
].value
[j
]);
312 free(p
->argv
[i
].value
);
322 perr(struct mdoc
*mdoc
, int line
, int pos
, enum merr code
)
330 p
= "unterminated quoted parameter";
333 p
= "argument requires a value";
338 return(mdoc_perr(mdoc
, line
, pos
, p
));
343 pwarn(struct mdoc
*mdoc
, int line
, int pos
, enum mwarn code
)
353 p
= "unexpected quoted parameter";
356 p
= "argument-like parameter";
359 p
= "last list column is empty";
363 p
= "trailing whitespace";
369 return(mdoc_pwarn(mdoc
, line
, pos
, c
, p
));
374 mdoc_args(struct mdoc
*mdoc
, int line
,
375 int *pos
, char *buf
, int tok
, char **v
)
380 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
383 * Override per-macro argument flags with context-specific ones.
384 * As of now, this is only valid for `It' depending on its list
390 for (n
= mdoc
->last
; n
; n
= n
->parent
)
391 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
395 c
= (int)(n
->args
? n
->args
->argc
: 0);
399 * Using `Bl -column' adds ARGS_TABSEP to the arguments
400 * and invalidates ARGS_DELIM. Using `Bl -diag' allows
401 * for quoted arguments.
405 for (i
= 0; i
< c
; i
++) {
406 switch (n
->args
->argv
[i
].arg
) {
425 /* Continue parsing the arguments themselves... */
427 return(args(mdoc
, line
, pos
, buf
, fl
, v
));
432 args(struct mdoc
*mdoc
, int line
,
433 int *pos
, char *buf
, int fl
, char **v
)
443 if ('\"' == buf
[*pos
] && ! (fl
& ARGS_QUOTED
))
444 if ( ! pwarn(mdoc
, line
, *pos
, WQUOTPARM
))
447 if ( ! (fl
& ARGS_ARGVLIKE
) && '-' == buf
[*pos
])
448 if ( ! pwarn(mdoc
, line
, *pos
, WARGVPARM
))
452 * If the first character is a delimiter and we're to look for
453 * delimited strings, then pass down the buffer seeing if it
454 * follows the pattern of [[::delim::][ ]+]+.
457 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
458 for (i
= *pos
; buf
[i
]; ) {
459 if ( ! mdoc_iscdelim(buf
[i
]))
462 /* There must be at least one space... */
463 if (0 == buf
[i
] || ! isspace((u_char
)buf
[i
]))
466 while (buf
[i
] && isspace((u_char
)buf
[i
]))
475 /* First parse non-quoted strings. */
477 if ('\"' != buf
[*pos
] || ! (ARGS_QUOTED
& fl
)) {
481 * Thar be dragons here! If we're tab-separated, search
482 * ahead for either a tab or the `Ta' macro. If a tab
483 * is detected, it mustn't be escaped; if a `Ta' is
484 * detected, it must be space-buffered before and after.
485 * If either of these hold true, then prune out the
486 * extra spaces and call it an argument.
489 if (ARGS_TABSEP
& fl
) {
490 /* Scan ahead to unescaped tab. */
492 for (p
= *v
; ; p
++) {
493 if (NULL
== (p
= strchr(p
, '\t')))
497 if ('\\' != *(p
- 1))
501 /* Scan ahead to unescaped `Ta'. */
503 for (pp
= *v
; ; pp
++) {
504 if (NULL
== (pp
= strstr(pp
, "Ta")))
506 if (pp
> *v
&& ' ' != *(pp
- 1))
508 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
512 /* Choose delimiter tab/Ta. */
515 p
= (p
< pp
? p
: pp
);
519 /* Strip delimiter's preceding whitespace. */
523 while (pp
> *v
&& ' ' == *pp
)
525 if (pp
== *v
&& ' ' == *pp
)
531 /* ...in- and proceding whitespace. */
533 if (p
&& ('\t' != *p
)) {
544 *pos
+= (int)(p
- *v
);
548 if ( ! pwarn(mdoc
, line
, *pos
, WCOLEMPTY
))
550 if (p
&& 0 == *p
&& p
> *v
&& ' ' == *(p
- 1))
551 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
557 /* Configure the eoln case, too. */
562 if (p
> *v
&& ' ' == *(p
- 1))
563 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
565 *pos
+= (int)(p
- *v
);
570 /* Do non-tabsep look-ahead here. */
572 if ( ! (ARGS_TABSEP
& fl
))
574 if (isspace((u_char
)buf
[*pos
]))
575 if ('\\' != buf
[*pos
- 1])
588 if ( ! (ARGS_TABSEP
& fl
))
589 while (buf
[*pos
] && isspace((u_char
)buf
[*pos
]))
595 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
602 * If we're a quoted string (and quoted strings are allowed),
603 * then parse ahead to the next quote. If none's found, it's an
604 * error. After, parse to the next word.
609 while (buf
[*pos
] && '\"' != buf
[*pos
])
612 if (0 == buf
[*pos
]) {
613 (void)perr(mdoc
, line
, *pos
, EQUOTTERM
);
621 while (buf
[*pos
] && isspace((u_char
)buf
[*pos
]))
627 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
635 argv_a2arg(int tok
, const char *argv
)
639 * Parse an argument identifier from its text. XXX - this
640 * should really be table-driven to clarify the code.
642 * If you add an argument to the list, make sure that you
643 * register it here with its one or more macros!
648 if (xstrcmp(argv
, "split"))
650 else if (xstrcmp(argv
, "nosplit"))
651 return(MDOC_Nosplit
);
655 if (xstrcmp(argv
, "ragged"))
657 else if (xstrcmp(argv
, "unfilled"))
658 return(MDOC_Unfilled
);
659 else if (xstrcmp(argv
, "filled"))
661 else if (xstrcmp(argv
, "literal"))
662 return(MDOC_Literal
);
663 else if (xstrcmp(argv
, "file"))
665 else if (xstrcmp(argv
, "offset"))
670 if (xstrcmp(argv
, "emphasis"))
671 return(MDOC_Emphasis
);
672 else if (xstrcmp(argv
, "literal"))
673 return(MDOC_Literal
);
674 else if (xstrcmp(argv
, "symbolic"))
675 return(MDOC_Symbolic
);
679 if (xstrcmp(argv
, "words"))
684 if (xstrcmp(argv
, "bullet"))
686 else if (xstrcmp(argv
, "dash"))
688 else if (xstrcmp(argv
, "hyphen"))
690 else if (xstrcmp(argv
, "item"))
692 else if (xstrcmp(argv
, "enum"))
694 else if (xstrcmp(argv
, "tag"))
696 else if (xstrcmp(argv
, "diag"))
698 else if (xstrcmp(argv
, "hang"))
700 else if (xstrcmp(argv
, "ohang"))
702 else if (xstrcmp(argv
, "inset"))
704 else if (xstrcmp(argv
, "column"))
706 else if (xstrcmp(argv
, "width"))
708 else if (xstrcmp(argv
, "offset"))
710 else if (xstrcmp(argv
, "compact"))
711 return(MDOC_Compact
);
717 if (xstrcmp(argv
, "std"))
724 return(MDOC_ARG_MAX
);
729 argv_multi(struct mdoc
*mdoc
, int line
,
730 struct mdoc_argv
*v
, int *pos
, char *buf
)
737 for (v
->sz
= 0; ; v
->sz
++) {
738 if ('-' == buf
[*pos
])
740 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
743 else if (ARGS_EOLN
== c
)
747 v
->value
= xrealloc(v
->value
,
748 (v
->sz
+ 5) * sizeof(char *));
750 v
->value
[(int)v
->sz
] = xstrdup(p
);
756 return(perr(mdoc
, line
, ppos
, EARGVAL
));
761 argv_opt_single(struct mdoc
*mdoc
, int line
,
762 struct mdoc_argv
*v
, int *pos
, char *buf
)
767 if ('-' == buf
[*pos
])
770 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
777 v
->value
= xcalloc(1, sizeof(char *));
778 v
->value
[0] = xstrdup(p
);
784 * Parse a single, mandatory value from the stream.
787 argv_single(struct mdoc
*mdoc
, int line
,
788 struct mdoc_argv
*v
, int *pos
, char *buf
)
795 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
799 return(perr(mdoc
, line
, ppos
, EARGVAL
));
802 v
->value
= xcalloc(1, sizeof(char *));
803 v
->value
[0] = xstrdup(p
);
809 * Determine rules for parsing arguments. Arguments can either accept
810 * no parameters, an optional single parameter, one parameter, or
811 * multiple parameters.
814 argv(struct mdoc
*mdoc
, int tok
, int line
,
815 struct mdoc_argv
*v
, int *pos
, char *buf
)
821 fl
= mdoc_argvflags
[v
->arg
];
824 * Override the default per-argument value.
829 fl
= ARGV_OPT_SINGLE
;
837 return(argv_single(mdoc
, line
, v
, pos
, buf
));
839 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
840 case (ARGV_OPT_SINGLE
):
841 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));