]>
git.cameronkatri.com Git - mandoc.git/blob - argv.c
db5b7ea47997d8091691053c247d6330df662109
1 /* $Id: argv.c,v 1.56 2009/03/23 14:22:11 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>
31 * Routines to parse arguments of macros. Arguments follow the syntax
32 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
33 * arguments, multiple arguments per value, no-value arguments, etc.
35 * There's no limit to the number or arguments that may be allocated.
38 #define ARGS_QUOTED (1 << 0)
39 #define ARGS_DELIM (1 << 1)
40 #define ARGS_TABSEP (1 << 2)
41 #define ARGS_ARGVLIKE (1 << 3)
43 #define ARGV_NONE (1 << 0)
44 #define ARGV_SINGLE (1 << 1)
45 #define ARGV_MULTI (1 << 2)
46 #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 /* 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
] = {
125 ARGS_QUOTED
, /* Cd */
131 ARGS_DELIM
| ARGS_QUOTED
, /* Fa */
134 ARGS_DELIM
| ARGS_QUOTED
, /* Fn */
135 ARGS_DELIM
| ARGS_QUOTED
, /* Ft */
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 */
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 if (NULL
== (*v
= calloc(1, sizeof(struct mdoc_arg
))))
303 arg
->argv
= xrealloc(arg
->argv
, arg
->argc
*
304 sizeof(struct mdoc_argv
));
306 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
307 &tmp
, sizeof(struct mdoc_argv
));
314 mdoc_argv_free(struct mdoc_arg
*p
)
330 for (i
= 0; i
< (int)p
->argc
; i
++) {
331 if (0 == p
->argv
[i
].sz
)
334 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
335 free(p
->argv
[i
].value
[j
]);
337 free(p
->argv
[i
].value
);
347 perr(struct mdoc
*mdoc
, int line
, int pos
, enum merr code
)
355 p
= "unterminated quoted parameter";
358 p
= "argument requires a value";
363 return(mdoc_perr(mdoc
, line
, pos
, p
));
368 pwarn(struct mdoc
*mdoc
, int line
, int pos
, enum mwarn code
)
378 p
= "unexpected quoted parameter";
381 p
= "argument-like parameter";
384 p
= "last list column is empty";
388 p
= "trailing whitespace";
394 return(mdoc_pwarn(mdoc
, line
, pos
, c
, p
));
399 mdoc_args(struct mdoc
*mdoc
, int line
,
400 int *pos
, char *buf
, int tok
, char **v
)
405 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
408 * Override per-macro argument flags with context-specific ones.
409 * As of now, this is only valid for `It' depending on its list
415 for (n
= mdoc
->last
; n
; n
= n
->parent
)
416 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
420 c
= (int)(n
->args
? n
->args
->argc
: 0);
424 * Using `Bl -column' adds ARGS_TABSEP to the arguments
425 * and invalidates ARGS_DELIM. Using `Bl -diag' allows
426 * for quoted arguments.
430 for (i
= 0; i
< c
; i
++) {
431 switch (n
->args
->argv
[i
].arg
) {
450 return(args(mdoc
, line
, pos
, buf
, fl
, v
));
455 args(struct mdoc
*mdoc
, int line
,
456 int *pos
, char *buf
, int fl
, char **v
)
466 if ('\"' == buf
[*pos
] && ! (fl
& ARGS_QUOTED
))
467 if ( ! pwarn(mdoc
, line
, *pos
, WQUOTPARM
))
470 if ( ! (fl
& ARGS_ARGVLIKE
) && '-' == buf
[*pos
])
471 if ( ! pwarn(mdoc
, line
, *pos
, WARGVPARM
))
475 * If the first character is a delimiter and we're to look for
476 * delimited strings, then pass down the buffer seeing if it
477 * follows the pattern of [[::delim::][ ]+]+.
480 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
481 for (i
= *pos
; buf
[i
]; ) {
482 if ( ! mdoc_iscdelim(buf
[i
]))
485 /* There must be at least one space... */
486 if (0 == buf
[i
] || ' ' != buf
[i
])
489 while (buf
[i
] && ' ' == buf
[i
])
498 /* First parse non-quoted strings. */
500 if ('\"' != buf
[*pos
] || ! (ARGS_QUOTED
& fl
)) {
504 * Thar be dragons here! If we're tab-separated, search
505 * ahead for either a tab or the `Ta' macro.
506 * If a `Ta' is detected, it must be space-buffered before and
507 * after. If either of these hold true, then prune out the
508 * extra spaces and call it an argument.
511 if (ARGS_TABSEP
& fl
) {
512 /* Scan ahead to unescaped tab. */
514 p
= strchr(*v
, '\t');
516 /* Scan ahead to unescaped `Ta'. */
518 for (pp
= *v
; ; pp
++) {
519 if (NULL
== (pp
= strstr(pp
, "Ta")))
521 if (pp
> *v
&& ' ' != *(pp
- 1))
523 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
527 /* Choose delimiter tab/Ta. */
530 p
= (p
< pp
? p
: pp
);
534 /* Strip delimiter's preceding whitespace. */
538 while (pp
> *v
&& ' ' == *pp
)
540 if (pp
== *v
&& ' ' == *pp
)
546 /* ...in- and proceding whitespace. */
548 if (p
&& ('\t' != *p
)) {
559 *pos
+= (int)(p
- *v
);
563 if ( ! pwarn(mdoc
, line
, *pos
, WCOLEMPTY
))
565 if (p
&& 0 == *p
&& p
> *v
&& ' ' == *(p
- 1))
566 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
572 /* Configure the eoln case, too. */
577 if (p
> *v
&& ' ' == *(p
- 1))
578 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
580 *pos
+= (int)(p
- *v
);
585 /* Do non-tabsep look-ahead here. */
587 if ( ! (ARGS_TABSEP
& fl
))
589 if (' ' == buf
[*pos
])
590 if ('\\' != buf
[*pos
- 1])
603 if ( ! (ARGS_TABSEP
& fl
))
604 while (buf
[*pos
] && ' ' == buf
[*pos
])
610 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
617 * If we're a quoted string (and quoted strings are allowed),
618 * then parse ahead to the next quote. If none's found, it's an
619 * error. After, parse to the next word.
624 while (buf
[*pos
] && '\"' != buf
[*pos
])
627 if (0 == buf
[*pos
]) {
628 (void)perr(mdoc
, line
, *pos
, EQUOTTERM
);
636 while (buf
[*pos
] && ' ' == buf
[*pos
])
642 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
650 argv_a2arg(int tok
, const char *argv
)
654 * Parse an argument identifier from its text. XXX - this
655 * should really be table-driven to clarify the code.
657 * If you add an argument to the list, make sure that you
658 * register it here with its one or more macros!
663 if (xstrcmp(argv
, "split"))
665 else if (xstrcmp(argv
, "nosplit"))
666 return(MDOC_Nosplit
);
670 if (xstrcmp(argv
, "ragged"))
672 else if (xstrcmp(argv
, "unfilled"))
673 return(MDOC_Unfilled
);
674 else if (xstrcmp(argv
, "filled"))
676 else if (xstrcmp(argv
, "literal"))
677 return(MDOC_Literal
);
678 else if (xstrcmp(argv
, "file"))
680 else if (xstrcmp(argv
, "offset"))
682 else if (xstrcmp(argv
, "compact"))
683 return(MDOC_Compact
);
687 if (xstrcmp(argv
, "emphasis"))
688 return(MDOC_Emphasis
);
689 else if (xstrcmp(argv
, "literal"))
690 return(MDOC_Literal
);
691 else if (xstrcmp(argv
, "symbolic"))
692 return(MDOC_Symbolic
);
696 if (xstrcmp(argv
, "words"))
701 if (xstrcmp(argv
, "bullet"))
703 else if (xstrcmp(argv
, "dash"))
705 else if (xstrcmp(argv
, "hyphen"))
707 else if (xstrcmp(argv
, "item"))
709 else if (xstrcmp(argv
, "enum"))
711 else if (xstrcmp(argv
, "tag"))
713 else if (xstrcmp(argv
, "diag"))
715 else if (xstrcmp(argv
, "hang"))
717 else if (xstrcmp(argv
, "ohang"))
719 else if (xstrcmp(argv
, "inset"))
721 else if (xstrcmp(argv
, "column"))
723 else if (xstrcmp(argv
, "width"))
725 else if (xstrcmp(argv
, "offset"))
727 else if (xstrcmp(argv
, "compact"))
728 return(MDOC_Compact
);
729 else if (xstrcmp(argv
, "nested"))
736 if (xstrcmp(argv
, "std"))
743 return(MDOC_ARG_MAX
);
748 argv_multi(struct mdoc
*mdoc
, int line
,
749 struct mdoc_argv
*v
, int *pos
, char *buf
)
756 for (v
->sz
= 0; ; v
->sz
++) {
757 if ('-' == buf
[*pos
])
759 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
762 else if (ARGS_EOLN
== c
)
765 if (0 == v
->sz
% MULTI_STEP
)
766 v
->value
= xrealloc(v
->value
,
767 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
769 v
->value
[(int)v
->sz
] = xstrdup(p
);
775 return(perr(mdoc
, line
, ppos
, EARGVAL
));
780 argv_opt_single(struct mdoc
*mdoc
, int line
,
781 struct mdoc_argv
*v
, int *pos
, char *buf
)
786 if ('-' == buf
[*pos
])
789 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
796 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
798 if (NULL
== (v
->value
[0] = strdup(p
)))
805 * Parse a single, mandatory value from the stream.
808 argv_single(struct mdoc
*mdoc
, int line
,
809 struct mdoc_argv
*v
, int *pos
, char *buf
)
816 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
820 return(perr(mdoc
, line
, ppos
, EARGVAL
));
823 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
825 if (NULL
== (v
->value
[0] = strdup(p
)))
832 * Determine rules for parsing arguments. Arguments can either accept
833 * no parameters, an optional single parameter, one parameter, or
834 * multiple parameters.
837 argv(struct mdoc
*mdoc
, int line
,
838 struct mdoc_argv
*v
, int *pos
, char *buf
)
844 switch (mdoc_argvflags
[v
->arg
]) {
846 return(argv_single(mdoc
, line
, v
, pos
, buf
));
848 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
849 case (ARGV_OPT_SINGLE
):
850 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));