]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.3 2009/04/12 19:45:26 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
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.
17 #include <sys/types.h>
28 * Routines to parse arguments of macros. Arguments follow the syntax
29 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
30 * arguments, multiple arguments per value, no-value arguments, etc.
32 * There's no limit to the number or arguments that may be allocated.
35 #define ARGS_QUOTED (1 << 0)
36 #define ARGS_DELIM (1 << 1)
37 #define ARGS_TABSEP (1 << 2)
38 #define ARGS_ARGVLIKE (1 << 3)
40 #define ARGV_NONE (1 << 0)
41 #define ARGV_SINGLE (1 << 1)
42 #define ARGV_MULTI (1 << 2)
43 #define ARGV_OPT_SINGLE (1 << 3)
60 static int argv_a2arg(int, const char *);
61 static int args(struct mdoc
*, int, int *,
62 char *, int, char **);
63 static int argv(struct mdoc
*, int,
64 struct mdoc_argv
*, int *, char *);
65 static int argv_single(struct mdoc
*, int,
66 struct mdoc_argv
*, int *, char *);
67 static int argv_opt_single(struct mdoc
*, int,
68 struct mdoc_argv
*, int *, char *);
69 static int argv_multi(struct mdoc
*, int,
70 struct mdoc_argv
*, int *, char *);
71 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
72 static int perr(struct mdoc
*, int, int, enum merr
);
74 #define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
76 /* Per-argument flags. */
78 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
79 ARGV_NONE
, /* MDOC_Split */
80 ARGV_NONE
, /* MDOC_Nosplit */
81 ARGV_NONE
, /* MDOC_Ragged */
82 ARGV_NONE
, /* MDOC_Unfilled */
83 ARGV_NONE
, /* MDOC_Literal */
84 ARGV_NONE
, /* MDOC_File */
85 ARGV_SINGLE
, /* MDOC_Offset */
86 ARGV_NONE
, /* MDOC_Bullet */
87 ARGV_NONE
, /* MDOC_Dash */
88 ARGV_NONE
, /* MDOC_Hyphen */
89 ARGV_NONE
, /* MDOC_Item */
90 ARGV_NONE
, /* MDOC_Enum */
91 ARGV_NONE
, /* MDOC_Tag */
92 ARGV_NONE
, /* MDOC_Diag */
93 ARGV_NONE
, /* MDOC_Hang */
94 ARGV_NONE
, /* MDOC_Ohang */
95 ARGV_NONE
, /* MDOC_Inset */
96 ARGV_MULTI
, /* MDOC_Column */
97 ARGV_SINGLE
, /* MDOC_Width */
98 ARGV_NONE
, /* MDOC_Compact */
99 ARGV_OPT_SINGLE
, /* MDOC_Std */
100 ARGV_NONE
, /* MDOC_Filled */
101 ARGV_NONE
, /* MDOC_Words */
102 ARGV_NONE
, /* MDOC_Emphasis */
103 ARGV_NONE
, /* MDOC_Symbolic */
104 ARGV_NONE
/* MDOC_Symbolic */
107 static int mdoc_argflags
[MDOC_MAX
] = {
112 ARGS_QUOTED
, /* Sh */
113 ARGS_QUOTED
, /* Ss */
116 ARGS_DELIM
| ARGS_QUOTED
, /* Dl */
124 ARGS_DELIM
| ARGS_QUOTED
, /* Ar */
125 ARGS_QUOTED
, /* Cd */
131 ARGS_DELIM
| ARGS_QUOTED
, /* Fa */
134 ARGS_DELIM
| ARGS_QUOTED
, /* Fn */
135 ARGS_DELIM
| ARGS_QUOTED
, /* Ft */
138 ARGS_DELIM
| ARGS_QUOTED
, /* Li */
139 ARGS_QUOTED
, /* Nd */
145 ARGS_DELIM
| ARGS_ARGVLIKE
, /* St */
149 ARGS_QUOTED
, /* %A */
150 ARGS_QUOTED
, /* %B */
151 ARGS_QUOTED
, /* %D */
152 ARGS_QUOTED
, /* %I */
153 ARGS_QUOTED
, /* %J */
154 ARGS_QUOTED
, /* %N */
155 ARGS_QUOTED
, /* %O */
156 ARGS_QUOTED
, /* %P */
157 ARGS_QUOTED
, /* %R */
158 ARGS_QUOTED
, /* %T */
159 ARGS_QUOTED
, /* %V */
168 ARGS_DELIM
, /* Bsx */
199 ARGS_DELIM
| ARGS_QUOTED
, /* Sy */
204 ARGS_QUOTED
, /* Fo */
217 ARGS_DELIM
| ARGS_QUOTED
, /* Lk */
218 ARGS_DELIM
| ARGS_QUOTED
, /* Mt */
219 ARGS_DELIM
, /* Brq */
221 ARGS_DELIM
, /* Brc */
222 ARGS_QUOTED
, /* %C */
226 ARGS_QUOTED
, /* %Q */
231 * Parse an argument from line text. This comes in the form of -key
232 * [value0...], which may either have a single mandatory value, at least
233 * one mandatory value, an optional single value, or no value.
236 mdoc_argv(struct mdoc
*mdoc
, int line
, int tok
,
237 struct mdoc_arg
**v
, int *pos
, char *buf
)
241 struct mdoc_argv tmp
;
242 struct mdoc_arg
*arg
;
247 assert(' ' != buf
[*pos
]);
249 if ('-' != buf
[*pos
] || ARGS_ARGVLIKE
& mdoc_argflags
[tok
])
252 /* Parse through to the first unescaped space. */
261 if (' ' == buf
[*pos
])
262 if ('\\' != buf
[*pos
- 1])
267 /* XXX - save zeroed byte, if not an argument. */
275 (void)memset(&tmp
, 0, sizeof(struct mdoc_argv
));
279 /* See if our token accepts the argument. */
281 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
282 /* XXX - restore saved zeroed byte. */
285 if ( ! pwarn(mdoc
, line
, i
, WARGVPARM
))
290 while (buf
[*pos
] && ' ' == buf
[*pos
])
293 if ( ! argv(mdoc
, line
, &tmp
, pos
, buf
))
296 if (NULL
== (arg
= *v
)) {
297 *v
= calloc(1, sizeof(struct mdoc_arg
));
299 (void)verr(mdoc
, EMALLOC
);
306 arg
->argv
= realloc(arg
->argv
, arg
->argc
*
307 sizeof(struct mdoc_argv
));
309 if (NULL
== arg
->argv
) {
310 (void)verr(mdoc
, EMALLOC
);
314 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
315 &tmp
, sizeof(struct mdoc_argv
));
322 mdoc_argv_free(struct mdoc_arg
*p
)
337 for (i
= 0; i
< (int)p
->argc
; i
++) {
338 if (0 == p
->argv
[i
].sz
)
341 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
342 free(p
->argv
[i
].value
[j
]);
344 free(p
->argv
[i
].value
);
354 perr(struct mdoc
*mdoc
, int line
, int pos
, enum merr code
)
361 p
= "memory exhausted";
364 p
= "unterminated quoted parameter";
367 p
= "argument requires a value";
371 return(mdoc_perr(mdoc
, line
, pos
, p
));
376 pwarn(struct mdoc
*mdoc
, int line
, int pos
, enum mwarn code
)
385 p
= "unexpected quoted parameter";
388 p
= "argument-like parameter";
391 p
= "last list column is empty";
395 p
= "trailing whitespace";
400 return(mdoc_pwarn(mdoc
, line
, pos
, c
, p
));
405 mdoc_args(struct mdoc
*mdoc
, int line
,
406 int *pos
, char *buf
, int tok
, char **v
)
411 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
414 * Override per-macro argument flags with context-specific ones.
415 * As of now, this is only valid for `It' depending on its list
421 for (n
= mdoc
->last
; n
; n
= n
->parent
)
422 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
426 c
= (int)(n
->args
? n
->args
->argc
: 0);
430 * Using `Bl -column' adds ARGS_TABSEP to the arguments
431 * and invalidates ARGS_DELIM. Using `Bl -diag' allows
432 * for quoted arguments.
436 for (i
= 0; i
< c
; i
++) {
437 switch (n
->args
->argv
[i
].arg
) {
456 return(args(mdoc
, line
, pos
, buf
, fl
, v
));
461 args(struct mdoc
*mdoc
, int line
,
462 int *pos
, char *buf
, int fl
, char **v
)
472 if ('\"' == buf
[*pos
] && ! (fl
& ARGS_QUOTED
))
473 if ( ! pwarn(mdoc
, line
, *pos
, WQUOTPARM
))
476 if ( ! (fl
& ARGS_ARGVLIKE
) && '-' == buf
[*pos
])
477 if ( ! pwarn(mdoc
, line
, *pos
, WARGVPARM
))
481 * If the first character is a delimiter and we're to look for
482 * delimited strings, then pass down the buffer seeing if it
483 * follows the pattern of [[::delim::][ ]+]+.
486 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
487 for (i
= *pos
; buf
[i
]; ) {
488 if ( ! mdoc_iscdelim(buf
[i
]))
491 /* There must be at least one space... */
492 if (0 == buf
[i
] || ' ' != buf
[i
])
495 while (buf
[i
] && ' ' == buf
[i
])
504 /* First parse non-quoted strings. */
506 if ('\"' != buf
[*pos
] || ! (ARGS_QUOTED
& fl
)) {
510 * Thar be dragons here! If we're tab-separated, search
511 * ahead for either a tab or the `Ta' macro.
512 * If a `Ta' is detected, it must be space-buffered before and
513 * after. If either of these hold true, then prune out the
514 * extra spaces and call it an argument.
517 if (ARGS_TABSEP
& fl
) {
518 /* Scan ahead to unescaped tab. */
520 p
= strchr(*v
, '\t');
522 /* Scan ahead to unescaped `Ta'. */
524 for (pp
= *v
; ; pp
++) {
525 if (NULL
== (pp
= strstr(pp
, "Ta")))
527 if (pp
> *v
&& ' ' != *(pp
- 1))
529 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
533 /* Choose delimiter tab/Ta. */
536 p
= (p
< pp
? p
: pp
);
540 /* Strip delimiter's preceding whitespace. */
544 while (pp
> *v
&& ' ' == *pp
)
546 if (pp
== *v
&& ' ' == *pp
)
552 /* ...in- and proceding whitespace. */
554 if (p
&& ('\t' != *p
)) {
565 *pos
+= (int)(p
- *v
);
569 if ( ! pwarn(mdoc
, line
, *pos
, WCOLEMPTY
))
571 if (p
&& 0 == *p
&& p
> *v
&& ' ' == *(p
- 1))
572 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
578 /* Configure the eoln case, too. */
583 if (p
> *v
&& ' ' == *(p
- 1))
584 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
586 *pos
+= (int)(p
- *v
);
591 /* Do non-tabsep look-ahead here. */
593 if ( ! (ARGS_TABSEP
& fl
))
595 if (' ' == buf
[*pos
])
596 if ('\\' != buf
[*pos
- 1])
609 if ( ! (ARGS_TABSEP
& fl
))
610 while (buf
[*pos
] && ' ' == buf
[*pos
])
616 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
623 * If we're a quoted string (and quoted strings are allowed),
624 * then parse ahead to the next quote. If none's found, it's an
625 * error. After, parse to the next word.
630 while (buf
[*pos
] && '\"' != buf
[*pos
])
633 if (0 == buf
[*pos
]) {
634 (void)perr(mdoc
, line
, *pos
, EQUOTTERM
);
642 while (buf
[*pos
] && ' ' == buf
[*pos
])
648 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
656 argv_a2arg(int tok
, const char *argv
)
660 * Parse an argument identifier from its text. XXX - this
661 * should really be table-driven to clarify the code.
663 * If you add an argument to the list, make sure that you
664 * register it here with its one or more macros!
669 if (0 == strcmp(argv
, "split"))
671 else if (0 == strcmp(argv
, "nosplit"))
672 return(MDOC_Nosplit
);
676 if (0 == strcmp(argv
, "ragged"))
678 else if (0 == strcmp(argv
, "unfilled"))
679 return(MDOC_Unfilled
);
680 else if (0 == strcmp(argv
, "filled"))
682 else if (0 == strcmp(argv
, "literal"))
683 return(MDOC_Literal
);
684 else if (0 == strcmp(argv
, "file"))
686 else if (0 == strcmp(argv
, "offset"))
688 else if (0 == strcmp(argv
, "compact"))
689 return(MDOC_Compact
);
693 if (0 == strcmp(argv
, "emphasis"))
694 return(MDOC_Emphasis
);
695 else if (0 == strcmp(argv
, "literal"))
696 return(MDOC_Literal
);
697 else if (0 == strcmp(argv
, "symbolic"))
698 return(MDOC_Symbolic
);
702 if (0 == strcmp(argv
, "words"))
707 if (0 == strcmp(argv
, "bullet"))
709 else if (0 == strcmp(argv
, "dash"))
711 else if (0 == strcmp(argv
, "hyphen"))
713 else if (0 == strcmp(argv
, "item"))
715 else if (0 == strcmp(argv
, "enum"))
717 else if (0 == strcmp(argv
, "tag"))
719 else if (0 == strcmp(argv
, "diag"))
721 else if (0 == strcmp(argv
, "hang"))
723 else if (0 == strcmp(argv
, "ohang"))
725 else if (0 == strcmp(argv
, "inset"))
727 else if (0 == strcmp(argv
, "column"))
729 else if (0 == strcmp(argv
, "width"))
731 else if (0 == strcmp(argv
, "offset"))
733 else if (0 == strcmp(argv
, "compact"))
734 return(MDOC_Compact
);
735 else if (0 == strcmp(argv
, "nested"))
742 if (0 == strcmp(argv
, "std"))
749 return(MDOC_ARG_MAX
);
754 argv_multi(struct mdoc
*mdoc
, int line
,
755 struct mdoc_argv
*v
, int *pos
, char *buf
)
762 for (v
->sz
= 0; ; v
->sz
++) {
763 if ('-' == buf
[*pos
])
765 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
768 else if (ARGS_EOLN
== c
)
771 if (0 == v
->sz
% MULTI_STEP
) {
772 v
->value
= realloc(v
->value
,
773 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
774 if (NULL
== v
->value
) {
775 (void)verr(mdoc
, EMALLOC
);
779 if (NULL
== (v
->value
[(int)v
->sz
] = strdup(p
)))
780 return(verr(mdoc
, EMALLOC
));
786 return(perr(mdoc
, line
, ppos
, EARGVAL
));
791 argv_opt_single(struct mdoc
*mdoc
, int line
,
792 struct mdoc_argv
*v
, int *pos
, char *buf
)
797 if ('-' == buf
[*pos
])
800 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
807 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
808 return(verr(mdoc
, EMALLOC
));
809 if (NULL
== (v
->value
[0] = strdup(p
)))
810 return(verr(mdoc
, EMALLOC
));
817 * Parse a single, mandatory value from the stream.
820 argv_single(struct mdoc
*mdoc
, int line
,
821 struct mdoc_argv
*v
, int *pos
, char *buf
)
828 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
832 return(perr(mdoc
, line
, ppos
, EARGVAL
));
835 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
836 return(verr(mdoc
, EMALLOC
));
837 if (NULL
== (v
->value
[0] = strdup(p
)))
838 return(verr(mdoc
, EMALLOC
));
845 * Determine rules for parsing arguments. Arguments can either accept
846 * no parameters, an optional single parameter, one parameter, or
847 * multiple parameters.
850 argv(struct mdoc
*mdoc
, int line
,
851 struct mdoc_argv
*v
, int *pos
, char *buf
)
857 switch (mdoc_argvflags
[v
->arg
]) {
859 return(argv_single(mdoc
, line
, v
, pos
, buf
));
861 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
862 case (ARGV_OPT_SINGLE
):
863 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));