]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.12 2009/07/11 20:47:52 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 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 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 /* FIXME .Bf Li raises "macro-like parameter". */
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)
49 static int argv_a2arg(int, const char *);
50 static int args(struct mdoc
*, int, int *,
51 char *, int, char **);
52 static int argv(struct mdoc
*, int,
53 struct mdoc_argv
*, int *, char *);
54 static int argv_single(struct mdoc
*, int,
55 struct mdoc_argv
*, int *, char *);
56 static int argv_opt_single(struct mdoc
*, int,
57 struct mdoc_argv
*, int *, char *);
58 static int argv_multi(struct mdoc
*, int,
59 struct mdoc_argv
*, int *, char *);
61 /* Per-argument flags. */
63 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
64 ARGV_NONE
, /* MDOC_Split */
65 ARGV_NONE
, /* MDOC_Nosplit */
66 ARGV_NONE
, /* MDOC_Ragged */
67 ARGV_NONE
, /* MDOC_Unfilled */
68 ARGV_NONE
, /* MDOC_Literal */
69 ARGV_NONE
, /* MDOC_File */
70 ARGV_SINGLE
, /* MDOC_Offset */
71 ARGV_NONE
, /* MDOC_Bullet */
72 ARGV_NONE
, /* MDOC_Dash */
73 ARGV_NONE
, /* MDOC_Hyphen */
74 ARGV_NONE
, /* MDOC_Item */
75 ARGV_NONE
, /* MDOC_Enum */
76 ARGV_NONE
, /* MDOC_Tag */
77 ARGV_NONE
, /* MDOC_Diag */
78 ARGV_NONE
, /* MDOC_Hang */
79 ARGV_NONE
, /* MDOC_Ohang */
80 ARGV_NONE
, /* MDOC_Inset */
81 ARGV_MULTI
, /* MDOC_Column */
82 ARGV_SINGLE
, /* MDOC_Width */
83 ARGV_NONE
, /* MDOC_Compact */
84 ARGV_OPT_SINGLE
, /* MDOC_Std */
85 ARGV_NONE
, /* MDOC_Filled */
86 ARGV_NONE
, /* MDOC_Words */
87 ARGV_NONE
, /* MDOC_Emphasis */
88 ARGV_NONE
, /* MDOC_Symbolic */
89 ARGV_NONE
/* MDOC_Symbolic */
92 static int mdoc_argflags
[MDOC_MAX
] = {
101 ARGS_DELIM
| ARGS_QUOTED
, /* Dl */
104 ARGS_QUOTED
, /* Bl */
109 ARGS_DELIM
| ARGS_QUOTED
, /* Ar */
110 ARGS_QUOTED
, /* Cd */
116 ARGS_DELIM
| ARGS_QUOTED
, /* Fa */
119 ARGS_DELIM
| ARGS_QUOTED
, /* Fn */
120 ARGS_DELIM
| ARGS_QUOTED
, /* Ft */
123 ARGS_DELIM
| ARGS_QUOTED
, /* Li */
124 ARGS_QUOTED
, /* Nd */
130 ARGS_DELIM
| ARGS_ARGVLIKE
, /* St */
134 ARGS_QUOTED
, /* %A */
135 ARGS_QUOTED
, /* %B */
136 ARGS_QUOTED
, /* %D */
137 ARGS_QUOTED
, /* %I */
138 ARGS_QUOTED
, /* %J */
139 ARGS_QUOTED
, /* %N */
140 ARGS_QUOTED
, /* %O */
141 ARGS_QUOTED
, /* %P */
142 ARGS_QUOTED
, /* %R */
143 ARGS_QUOTED
, /* %T */
144 ARGS_QUOTED
, /* %V */
153 ARGS_DELIM
, /* Bsx */
184 ARGS_DELIM
| ARGS_QUOTED
, /* Sy */
189 ARGS_QUOTED
, /* Fo */
201 ARGS_DELIM
| ARGS_QUOTED
, /* Lk */
202 ARGS_DELIM
| ARGS_QUOTED
, /* Mt */
203 ARGS_DELIM
, /* Brq */
205 ARGS_DELIM
, /* Brc */
206 ARGS_QUOTED
, /* %C */
210 ARGS_QUOTED
, /* %Q */
215 * Parse an argument from line text. This comes in the form of -key
216 * [value0...], which may either have a single mandatory value, at least
217 * one mandatory value, an optional single value, or no value.
220 mdoc_argv(struct mdoc
*m
, int line
, int tok
,
221 struct mdoc_arg
**v
, int *pos
, char *buf
)
225 struct mdoc_argv tmp
;
226 struct mdoc_arg
*arg
;
231 assert(' ' != buf
[*pos
]);
233 if ('-' != buf
[*pos
] || ARGS_ARGVLIKE
& mdoc_argflags
[tok
])
236 /* Parse through to the first unescaped space. */
245 if (' ' == buf
[*pos
])
246 if ('\\' != buf
[*pos
- 1])
251 /* XXX - save zeroed byte, if not an argument. */
259 (void)memset(&tmp
, 0, sizeof(struct mdoc_argv
));
263 /* See if our token accepts the argument. */
265 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
266 /* XXX - restore saved zeroed byte. */
269 if ( ! mdoc_pwarn(m
, line
, i
, EARGVPARM
))
274 while (buf
[*pos
] && ' ' == buf
[*pos
])
277 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
280 if (NULL
== (arg
= *v
)) {
281 *v
= calloc(1, sizeof(struct mdoc_arg
));
283 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
290 arg
->argv
= realloc(arg
->argv
, arg
->argc
*
291 sizeof(struct mdoc_argv
));
293 if (NULL
== arg
->argv
) {
294 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
298 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
299 &tmp
, sizeof(struct mdoc_argv
));
306 mdoc_argv_free(struct mdoc_arg
*p
)
321 for (i
= 0; i
< (int)p
->argc
; i
++) {
322 if (0 == p
->argv
[i
].sz
)
325 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
326 free(p
->argv
[i
].value
[j
]);
328 free(p
->argv
[i
].value
);
337 mdoc_args(struct mdoc
*m
, int line
,
338 int *pos
, char *buf
, int tok
, char **v
)
343 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
346 * Override per-macro argument flags with context-specific ones.
347 * As of now, this is only valid for `It' depending on its list
353 for (n
= m
->last
; n
; n
= n
->parent
)
354 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
358 c
= (int)(n
->args
? n
->args
->argc
: 0);
362 * Using `Bl -column' adds ARGS_TABSEP to the arguments
363 * and invalidates ARGS_DELIM. Using `Bl -diag' allows
364 * for quoted arguments.
368 for (i
= 0; i
< c
; i
++) {
369 switch (n
->args
->argv
[i
].arg
) {
388 return(args(m
, line
, pos
, buf
, fl
, v
));
393 args(struct mdoc
*m
, int line
,
394 int *pos
, char *buf
, int fl
, char **v
)
404 if ('\"' == buf
[*pos
] && ! (fl
& ARGS_QUOTED
))
405 if ( ! mdoc_pwarn(m
, line
, *pos
, EQUOTPARM
))
408 if ( ! (fl
& ARGS_ARGVLIKE
) && '-' == buf
[*pos
])
409 if ( ! mdoc_pwarn(m
, line
, *pos
, EARGVPARM
))
413 * If the first character is a delimiter and we're to look for
414 * delimited strings, then pass down the buffer seeing if it
415 * follows the pattern of [[::delim::][ ]+]+.
418 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
419 for (i
= *pos
; buf
[i
]; ) {
420 if ( ! mdoc_iscdelim(buf
[i
]))
423 /* There must be at least one space... */
424 if (0 == buf
[i
] || ' ' != buf
[i
])
427 while (buf
[i
] && ' ' == buf
[i
])
436 /* First parse non-quoted strings. */
438 if ('\"' != buf
[*pos
] || ! (ARGS_QUOTED
& fl
)) {
442 * Thar be dragons here! If we're tab-separated, search
443 * ahead for either a tab or the `Ta' macro.
444 * If a `Ta' is detected, it must be space-buffered before and
445 * after. If either of these hold true, then prune out the
446 * extra spaces and call it an argument.
449 if (ARGS_TABSEP
& fl
) {
450 /* Scan ahead to unescaped tab. */
452 p
= strchr(*v
, '\t');
454 /* Scan ahead to unescaped `Ta'. */
456 for (pp
= *v
; ; pp
++) {
457 if (NULL
== (pp
= strstr(pp
, "Ta")))
459 if (pp
> *v
&& ' ' != *(pp
- 1))
461 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
465 /* Choose delimiter tab/Ta. */
468 p
= (p
< pp
? p
: pp
);
472 /* Strip delimiter's preceding whitespace. */
476 while (pp
> *v
&& ' ' == *pp
)
478 if (pp
== *v
&& ' ' == *pp
)
484 /* ...in- and proceding whitespace. */
486 if (p
&& ('\t' != *p
)) {
497 *pos
+= (int)(p
- *v
);
501 if ( ! mdoc_pwarn(m
, line
, *pos
, ECOLEMPTY
))
503 if (p
&& 0 == *p
&& p
> *v
&& ' ' == *(p
- 1))
504 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
510 /* Configure the eoln case, too. */
515 if (p
> *v
&& ' ' == *(p
- 1))
516 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
518 *pos
+= (int)(p
- *v
);
523 /* Do non-tabsep look-ahead here. */
525 if ( ! (ARGS_TABSEP
& fl
))
527 if (' ' == buf
[*pos
])
528 if ('\\' != buf
[*pos
- 1])
541 if ( ! (ARGS_TABSEP
& fl
))
542 while (buf
[*pos
] && ' ' == buf
[*pos
])
548 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
555 * If we're a quoted string (and quoted strings are allowed),
556 * then parse ahead to the next quote. If none's found, it's an
557 * error. After, parse to the next word.
562 while (buf
[*pos
] && '\"' != buf
[*pos
])
565 if (0 == buf
[*pos
]) {
566 (void)mdoc_perr(m
, line
, *pos
, EQUOTTERM
);
574 while (buf
[*pos
] && ' ' == buf
[*pos
])
580 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
588 argv_a2arg(int tok
, const char *argv
)
592 * Parse an argument identifier from its text. XXX - this
593 * should really be table-driven to clarify the code.
595 * If you add an argument to the list, make sure that you
596 * register it here with its one or more macros!
601 if (0 == strcmp(argv
, "split"))
603 else if (0 == strcmp(argv
, "nosplit"))
604 return(MDOC_Nosplit
);
608 if (0 == strcmp(argv
, "ragged"))
610 else if (0 == strcmp(argv
, "unfilled"))
611 return(MDOC_Unfilled
);
612 else if (0 == strcmp(argv
, "filled"))
614 else if (0 == strcmp(argv
, "literal"))
615 return(MDOC_Literal
);
616 else if (0 == strcmp(argv
, "file"))
618 else if (0 == strcmp(argv
, "offset"))
620 else if (0 == strcmp(argv
, "compact"))
621 return(MDOC_Compact
);
625 if (0 == strcmp(argv
, "emphasis"))
626 return(MDOC_Emphasis
);
627 else if (0 == strcmp(argv
, "literal"))
628 return(MDOC_Literal
);
629 else if (0 == strcmp(argv
, "symbolic"))
630 return(MDOC_Symbolic
);
634 if (0 == strcmp(argv
, "words"))
639 if (0 == strcmp(argv
, "bullet"))
641 else if (0 == strcmp(argv
, "dash"))
643 else if (0 == strcmp(argv
, "hyphen"))
645 else if (0 == strcmp(argv
, "item"))
647 else if (0 == strcmp(argv
, "enum"))
649 else if (0 == strcmp(argv
, "tag"))
651 else if (0 == strcmp(argv
, "diag"))
653 else if (0 == strcmp(argv
, "hang"))
655 else if (0 == strcmp(argv
, "ohang"))
657 else if (0 == strcmp(argv
, "inset"))
659 else if (0 == strcmp(argv
, "column"))
661 else if (0 == strcmp(argv
, "width"))
663 else if (0 == strcmp(argv
, "offset"))
665 else if (0 == strcmp(argv
, "compact"))
666 return(MDOC_Compact
);
667 else if (0 == strcmp(argv
, "nested"))
674 if (0 == strcmp(argv
, "std"))
681 return(MDOC_ARG_MAX
);
686 argv_multi(struct mdoc
*m
, int line
,
687 struct mdoc_argv
*v
, int *pos
, char *buf
)
692 for (v
->sz
= 0; ; v
->sz
++) {
693 if ('-' == buf
[*pos
])
695 c
= args(m
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
698 else if (ARGS_EOLN
== c
)
701 if (0 == v
->sz
% MULTI_STEP
) {
702 v
->value
= realloc(v
->value
,
703 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
704 if (NULL
== v
->value
) {
705 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
709 if (NULL
== (v
->value
[(int)v
->sz
] = strdup(p
)))
710 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
718 argv_opt_single(struct mdoc
*m
, int line
,
719 struct mdoc_argv
*v
, int *pos
, char *buf
)
724 if ('-' == buf
[*pos
])
727 c
= args(m
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
734 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
735 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
736 if (NULL
== (v
->value
[0] = strdup(p
)))
737 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
744 * Parse a single, mandatory value from the stream.
747 argv_single(struct mdoc
*m
, int line
,
748 struct mdoc_argv
*v
, int *pos
, char *buf
)
755 c
= args(m
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
759 return(mdoc_perr(m
, line
, ppos
, EARGVAL
));
762 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
763 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
764 if (NULL
== (v
->value
[0] = strdup(p
)))
765 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
772 * Determine rules for parsing arguments. Arguments can either accept
773 * no parameters, an optional single parameter, one parameter, or
774 * multiple parameters.
777 argv(struct mdoc
*mdoc
, int line
,
778 struct mdoc_argv
*v
, int *pos
, char *buf
)
784 switch (mdoc_argvflags
[v
->arg
]) {
786 return(argv_single(mdoc
, line
, v
, pos
, buf
));
788 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
789 case (ARGV_OPT_SINGLE
):
790 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));