]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.2 2009/03/31 13:50:19 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
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.
19 #include <sys/types.h>
30 * Routines to parse arguments of macros. Arguments follow the syntax
31 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
32 * arguments, multiple arguments per value, no-value arguments, etc.
34 * There's no limit to the number or arguments that may be allocated.
37 #define ARGS_QUOTED (1 << 0)
38 #define ARGS_DELIM (1 << 1)
39 #define ARGS_TABSEP (1 << 2)
40 #define ARGS_ARGVLIKE (1 << 3)
42 #define ARGV_NONE (1 << 0)
43 #define ARGV_SINGLE (1 << 1)
44 #define ARGV_MULTI (1 << 2)
45 #define ARGV_OPT_SINGLE (1 << 3)
62 static int argv_a2arg(int, const char *);
63 static int args(struct mdoc
*, int, int *,
64 char *, int, char **);
65 static int argv(struct mdoc
*, int,
66 struct mdoc_argv
*, int *, char *);
67 static int argv_single(struct mdoc
*, int,
68 struct mdoc_argv
*, int *, char *);
69 static int argv_opt_single(struct mdoc
*, int,
70 struct mdoc_argv
*, int *, char *);
71 static int argv_multi(struct mdoc
*, int,
72 struct mdoc_argv
*, int *, char *);
73 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
74 static int perr(struct mdoc
*, int, int, enum merr
);
76 #define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
78 /* Per-argument flags. */
80 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
81 ARGV_NONE
, /* MDOC_Split */
82 ARGV_NONE
, /* MDOC_Nosplit */
83 ARGV_NONE
, /* MDOC_Ragged */
84 ARGV_NONE
, /* MDOC_Unfilled */
85 ARGV_NONE
, /* MDOC_Literal */
86 ARGV_NONE
, /* MDOC_File */
87 ARGV_SINGLE
, /* MDOC_Offset */
88 ARGV_NONE
, /* MDOC_Bullet */
89 ARGV_NONE
, /* MDOC_Dash */
90 ARGV_NONE
, /* MDOC_Hyphen */
91 ARGV_NONE
, /* MDOC_Item */
92 ARGV_NONE
, /* MDOC_Enum */
93 ARGV_NONE
, /* MDOC_Tag */
94 ARGV_NONE
, /* MDOC_Diag */
95 ARGV_NONE
, /* MDOC_Hang */
96 ARGV_NONE
, /* MDOC_Ohang */
97 ARGV_NONE
, /* MDOC_Inset */
98 ARGV_MULTI
, /* MDOC_Column */
99 ARGV_SINGLE
, /* MDOC_Width */
100 ARGV_NONE
, /* MDOC_Compact */
101 ARGV_OPT_SINGLE
, /* MDOC_Std */
102 ARGV_NONE
, /* MDOC_Filled */
103 ARGV_NONE
, /* MDOC_Words */
104 ARGV_NONE
, /* MDOC_Emphasis */
105 ARGV_NONE
, /* MDOC_Symbolic */
106 ARGV_NONE
/* MDOC_Symbolic */
109 static int mdoc_argflags
[MDOC_MAX
] = {
114 ARGS_QUOTED
, /* Sh */
115 ARGS_QUOTED
, /* Ss */
118 ARGS_DELIM
| ARGS_QUOTED
, /* Dl */
126 ARGS_DELIM
| ARGS_QUOTED
, /* Ar */
127 ARGS_QUOTED
, /* Cd */
133 ARGS_DELIM
| ARGS_QUOTED
, /* Fa */
136 ARGS_DELIM
| ARGS_QUOTED
, /* Fn */
137 ARGS_DELIM
| ARGS_QUOTED
, /* Ft */
140 ARGS_DELIM
| ARGS_QUOTED
, /* Li */
141 ARGS_QUOTED
, /* Nd */
147 ARGS_DELIM
| ARGS_ARGVLIKE
, /* St */
151 ARGS_QUOTED
, /* %A */
152 ARGS_QUOTED
, /* %B */
153 ARGS_QUOTED
, /* %D */
154 ARGS_QUOTED
, /* %I */
155 ARGS_QUOTED
, /* %J */
156 ARGS_QUOTED
, /* %N */
157 ARGS_QUOTED
, /* %O */
158 ARGS_QUOTED
, /* %P */
159 ARGS_QUOTED
, /* %R */
160 ARGS_QUOTED
, /* %T */
161 ARGS_QUOTED
, /* %V */
170 ARGS_DELIM
, /* Bsx */
201 ARGS_DELIM
| ARGS_QUOTED
, /* Sy */
206 ARGS_QUOTED
, /* Fo */
219 ARGS_DELIM
| ARGS_QUOTED
, /* Lk */
220 ARGS_DELIM
| ARGS_QUOTED
, /* Mt */
221 ARGS_DELIM
, /* Brq */
223 ARGS_DELIM
, /* Brc */
224 ARGS_QUOTED
, /* %C */
228 ARGS_QUOTED
, /* %Q */
233 * Parse an argument from line text. This comes in the form of -key
234 * [value0...], which may either have a single mandatory value, at least
235 * one mandatory value, an optional single value, or no value.
238 mdoc_argv(struct mdoc
*mdoc
, int line
, int tok
,
239 struct mdoc_arg
**v
, int *pos
, char *buf
)
243 struct mdoc_argv tmp
;
244 struct mdoc_arg
*arg
;
249 assert(' ' != buf
[*pos
]);
251 if ('-' != buf
[*pos
] || ARGS_ARGVLIKE
& mdoc_argflags
[tok
])
254 /* Parse through to the first unescaped space. */
263 if (' ' == buf
[*pos
])
264 if ('\\' != buf
[*pos
- 1])
269 /* XXX - save zeroed byte, if not an argument. */
277 (void)memset(&tmp
, 0, sizeof(struct mdoc_argv
));
281 /* See if our token accepts the argument. */
283 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
284 /* XXX - restore saved zeroed byte. */
287 if ( ! pwarn(mdoc
, line
, i
, WARGVPARM
))
292 while (buf
[*pos
] && ' ' == buf
[*pos
])
295 if ( ! argv(mdoc
, line
, &tmp
, pos
, buf
))
298 if (NULL
== (arg
= *v
)) {
299 *v
= calloc(1, sizeof(struct mdoc_arg
));
301 (void)verr(mdoc
, EMALLOC
);
308 arg
->argv
= realloc(arg
->argv
, arg
->argc
*
309 sizeof(struct mdoc_argv
));
311 if (NULL
== arg
->argv
) {
312 (void)verr(mdoc
, EMALLOC
);
316 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
317 &tmp
, sizeof(struct mdoc_argv
));
324 mdoc_argv_free(struct mdoc_arg
*p
)
339 for (i
= 0; i
< (int)p
->argc
; i
++) {
340 if (0 == p
->argv
[i
].sz
)
343 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
344 free(p
->argv
[i
].value
[j
]);
346 free(p
->argv
[i
].value
);
356 perr(struct mdoc
*mdoc
, int line
, int pos
, enum merr code
)
363 p
= "memory exhausted";
366 p
= "unterminated quoted parameter";
369 p
= "argument requires a value";
373 return(mdoc_perr(mdoc
, line
, pos
, p
));
378 pwarn(struct mdoc
*mdoc
, int line
, int pos
, enum mwarn code
)
387 p
= "unexpected quoted parameter";
390 p
= "argument-like parameter";
393 p
= "last list column is empty";
397 p
= "trailing whitespace";
402 return(mdoc_pwarn(mdoc
, line
, pos
, c
, p
));
407 mdoc_args(struct mdoc
*mdoc
, int line
,
408 int *pos
, char *buf
, int tok
, char **v
)
413 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
416 * Override per-macro argument flags with context-specific ones.
417 * As of now, this is only valid for `It' depending on its list
423 for (n
= mdoc
->last
; n
; n
= n
->parent
)
424 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
428 c
= (int)(n
->args
? n
->args
->argc
: 0);
432 * Using `Bl -column' adds ARGS_TABSEP to the arguments
433 * and invalidates ARGS_DELIM. Using `Bl -diag' allows
434 * for quoted arguments.
438 for (i
= 0; i
< c
; i
++) {
439 switch (n
->args
->argv
[i
].arg
) {
458 return(args(mdoc
, line
, pos
, buf
, fl
, v
));
463 args(struct mdoc
*mdoc
, int line
,
464 int *pos
, char *buf
, int fl
, char **v
)
474 if ('\"' == buf
[*pos
] && ! (fl
& ARGS_QUOTED
))
475 if ( ! pwarn(mdoc
, line
, *pos
, WQUOTPARM
))
478 if ( ! (fl
& ARGS_ARGVLIKE
) && '-' == buf
[*pos
])
479 if ( ! pwarn(mdoc
, line
, *pos
, WARGVPARM
))
483 * If the first character is a delimiter and we're to look for
484 * delimited strings, then pass down the buffer seeing if it
485 * follows the pattern of [[::delim::][ ]+]+.
488 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
489 for (i
= *pos
; buf
[i
]; ) {
490 if ( ! mdoc_iscdelim(buf
[i
]))
493 /* There must be at least one space... */
494 if (0 == buf
[i
] || ' ' != buf
[i
])
497 while (buf
[i
] && ' ' == buf
[i
])
506 /* First parse non-quoted strings. */
508 if ('\"' != buf
[*pos
] || ! (ARGS_QUOTED
& fl
)) {
512 * Thar be dragons here! If we're tab-separated, search
513 * ahead for either a tab or the `Ta' macro.
514 * If a `Ta' is detected, it must be space-buffered before and
515 * after. If either of these hold true, then prune out the
516 * extra spaces and call it an argument.
519 if (ARGS_TABSEP
& fl
) {
520 /* Scan ahead to unescaped tab. */
522 p
= strchr(*v
, '\t');
524 /* Scan ahead to unescaped `Ta'. */
526 for (pp
= *v
; ; pp
++) {
527 if (NULL
== (pp
= strstr(pp
, "Ta")))
529 if (pp
> *v
&& ' ' != *(pp
- 1))
531 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
535 /* Choose delimiter tab/Ta. */
538 p
= (p
< pp
? p
: pp
);
542 /* Strip delimiter's preceding whitespace. */
546 while (pp
> *v
&& ' ' == *pp
)
548 if (pp
== *v
&& ' ' == *pp
)
554 /* ...in- and proceding whitespace. */
556 if (p
&& ('\t' != *p
)) {
567 *pos
+= (int)(p
- *v
);
571 if ( ! pwarn(mdoc
, line
, *pos
, WCOLEMPTY
))
573 if (p
&& 0 == *p
&& p
> *v
&& ' ' == *(p
- 1))
574 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
580 /* Configure the eoln case, too. */
585 if (p
> *v
&& ' ' == *(p
- 1))
586 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
588 *pos
+= (int)(p
- *v
);
593 /* Do non-tabsep look-ahead here. */
595 if ( ! (ARGS_TABSEP
& fl
))
597 if (' ' == buf
[*pos
])
598 if ('\\' != buf
[*pos
- 1])
611 if ( ! (ARGS_TABSEP
& fl
))
612 while (buf
[*pos
] && ' ' == buf
[*pos
])
618 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
625 * If we're a quoted string (and quoted strings are allowed),
626 * then parse ahead to the next quote. If none's found, it's an
627 * error. After, parse to the next word.
632 while (buf
[*pos
] && '\"' != buf
[*pos
])
635 if (0 == buf
[*pos
]) {
636 (void)perr(mdoc
, line
, *pos
, EQUOTTERM
);
644 while (buf
[*pos
] && ' ' == buf
[*pos
])
650 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
658 argv_a2arg(int tok
, const char *argv
)
662 * Parse an argument identifier from its text. XXX - this
663 * should really be table-driven to clarify the code.
665 * If you add an argument to the list, make sure that you
666 * register it here with its one or more macros!
671 if (0 == strcmp(argv
, "split"))
673 else if (0 == strcmp(argv
, "nosplit"))
674 return(MDOC_Nosplit
);
678 if (0 == strcmp(argv
, "ragged"))
680 else if (0 == strcmp(argv
, "unfilled"))
681 return(MDOC_Unfilled
);
682 else if (0 == strcmp(argv
, "filled"))
684 else if (0 == strcmp(argv
, "literal"))
685 return(MDOC_Literal
);
686 else if (0 == strcmp(argv
, "file"))
688 else if (0 == strcmp(argv
, "offset"))
690 else if (0 == strcmp(argv
, "compact"))
691 return(MDOC_Compact
);
695 if (0 == strcmp(argv
, "emphasis"))
696 return(MDOC_Emphasis
);
697 else if (0 == strcmp(argv
, "literal"))
698 return(MDOC_Literal
);
699 else if (0 == strcmp(argv
, "symbolic"))
700 return(MDOC_Symbolic
);
704 if (0 == strcmp(argv
, "words"))
709 if (0 == strcmp(argv
, "bullet"))
711 else if (0 == strcmp(argv
, "dash"))
713 else if (0 == strcmp(argv
, "hyphen"))
715 else if (0 == strcmp(argv
, "item"))
717 else if (0 == strcmp(argv
, "enum"))
719 else if (0 == strcmp(argv
, "tag"))
721 else if (0 == strcmp(argv
, "diag"))
723 else if (0 == strcmp(argv
, "hang"))
725 else if (0 == strcmp(argv
, "ohang"))
727 else if (0 == strcmp(argv
, "inset"))
729 else if (0 == strcmp(argv
, "column"))
731 else if (0 == strcmp(argv
, "width"))
733 else if (0 == strcmp(argv
, "offset"))
735 else if (0 == strcmp(argv
, "compact"))
736 return(MDOC_Compact
);
737 else if (0 == strcmp(argv
, "nested"))
744 if (0 == strcmp(argv
, "std"))
751 return(MDOC_ARG_MAX
);
756 argv_multi(struct mdoc
*mdoc
, int line
,
757 struct mdoc_argv
*v
, int *pos
, char *buf
)
764 for (v
->sz
= 0; ; v
->sz
++) {
765 if ('-' == buf
[*pos
])
767 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
770 else if (ARGS_EOLN
== c
)
773 if (0 == v
->sz
% MULTI_STEP
) {
774 v
->value
= realloc(v
->value
,
775 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
776 if (NULL
== v
->value
) {
777 (void)verr(mdoc
, EMALLOC
);
781 if (NULL
== (v
->value
[(int)v
->sz
] = strdup(p
)))
782 return(verr(mdoc
, EMALLOC
));
788 return(perr(mdoc
, line
, ppos
, EARGVAL
));
793 argv_opt_single(struct mdoc
*mdoc
, int line
,
794 struct mdoc_argv
*v
, int *pos
, char *buf
)
799 if ('-' == buf
[*pos
])
802 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
809 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
810 return(verr(mdoc
, EMALLOC
));
811 if (NULL
== (v
->value
[0] = strdup(p
)))
812 return(verr(mdoc
, EMALLOC
));
819 * Parse a single, mandatory value from the stream.
822 argv_single(struct mdoc
*mdoc
, int line
,
823 struct mdoc_argv
*v
, int *pos
, char *buf
)
830 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
834 return(perr(mdoc
, line
, ppos
, EARGVAL
));
837 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
838 return(verr(mdoc
, EMALLOC
));
839 if (NULL
== (v
->value
[0] = strdup(p
)))
840 return(verr(mdoc
, EMALLOC
));
847 * Determine rules for parsing arguments. Arguments can either accept
848 * no parameters, an optional single parameter, one parameter, or
849 * multiple parameters.
852 argv(struct mdoc
*mdoc
, int line
,
853 struct mdoc_argv
*v
, int *pos
, char *buf
)
859 switch (mdoc_argvflags
[v
->arg
]) {
861 return(argv_single(mdoc
, line
, v
, pos
, buf
));
863 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
864 case (ARGV_OPT_SINGLE
):
865 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));