]>
git.cameronkatri.com Git - mandoc.git/blob - argv.c
1 /* $Id: argv.c,v 1.38 2009/03/06 14:13:47 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.
34 #define ARGS_QUOTED (1 << 0)
35 #define ARGS_DELIM (1 << 1)
36 #define ARGS_TABSEP (1 << 2)
37 #define ARGS_ARGVLIKE (1 << 3)
39 #define ARGV_NONE (1 << 0)
40 #define ARGV_SINGLE (1 << 1)
41 #define ARGV_MULTI (1 << 2)
42 #define ARGV_OPT_SINGLE (1 << 3)
44 static int argv_a2arg(int, const char *);
45 static int args(struct mdoc
*, int, int *,
46 char *, int, char **);
47 static int argv(struct mdoc
*, int, int,
48 struct mdoc_arg
*, int *, char *);
49 static int argv_single(struct mdoc
*, int,
50 struct mdoc_arg
*, int *, char *);
51 static int argv_opt_single(struct mdoc
*, int,
52 struct mdoc_arg
*, int *, char *);
53 static int argv_multi(struct mdoc
*, int,
54 struct mdoc_arg
*, int *, char *);
55 static int pwarn(struct mdoc
*, int, int, int);
56 static int perr(struct mdoc
*, int, int, int);
58 /* Warning messages. */
71 /* Per-argument flags. */
73 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
74 ARGV_NONE
, /* MDOC_Split */
75 ARGV_NONE
, /* MDOC_Nosplit */
76 ARGV_NONE
, /* MDOC_Ragged */
77 ARGV_NONE
, /* MDOC_Unfilled */
78 ARGV_NONE
, /* MDOC_Literal */
79 ARGV_NONE
, /* MDOC_File */
80 ARGV_SINGLE
, /* MDOC_Offset */
81 ARGV_NONE
, /* MDOC_Bullet */
82 ARGV_NONE
, /* MDOC_Dash */
83 ARGV_NONE
, /* MDOC_Hyphen */
84 ARGV_NONE
, /* MDOC_Item */
85 ARGV_NONE
, /* MDOC_Enum */
86 ARGV_NONE
, /* MDOC_Tag */
87 ARGV_NONE
, /* MDOC_Diag */
88 ARGV_NONE
, /* MDOC_Hang */
89 ARGV_NONE
, /* MDOC_Ohang */
90 ARGV_NONE
, /* MDOC_Inset */
91 ARGV_MULTI
, /* MDOC_Column */
92 ARGV_SINGLE
, /* MDOC_Width */
93 ARGV_NONE
, /* MDOC_Compact */
94 ARGV_SINGLE
, /* MDOC_Std */
95 ARGV_NONE
, /* MDOC_Filled */
96 ARGV_NONE
, /* MDOC_Words */
97 ARGV_NONE
, /* MDOC_Emphasis */
98 ARGV_NONE
/* MDOC_Symbolic */
101 static int mdoc_argflags
[MDOC_MAX
] = {
119 ARGS_QUOTED
, /* Cd */
125 ARGS_DELIM
| ARGS_QUOTED
, /* Fa */
128 ARGS_DELIM
| ARGS_QUOTED
, /* Fn */
129 ARGS_DELIM
| ARGS_QUOTED
, /* Ft */
139 ARGS_DELIM
| ARGS_ARGVLIKE
, /* St */
143 ARGS_QUOTED
, /* %A */
144 ARGS_QUOTED
, /* %B */
145 ARGS_QUOTED
, /* %D */
146 ARGS_QUOTED
, /* %I */
147 ARGS_QUOTED
, /* %J */
148 ARGS_QUOTED
, /* %N */
149 ARGS_QUOTED
, /* %O */
150 ARGS_QUOTED
, /* %P */
151 ARGS_QUOTED
, /* %R */
152 ARGS_QUOTED
, /* %T */
153 ARGS_QUOTED
, /* %V */
162 ARGS_DELIM
, /* Bsx */
213 perr(struct mdoc
*mdoc
, int line
, int pos
, int code
)
219 c
= mdoc_perr(mdoc
, line
, pos
,
220 "unterminated quoted parameter");
223 c
= mdoc_perr(mdoc
, line
, pos
,
224 "argument requires a value");
227 c
= mdoc_perr(mdoc
, line
, pos
,
228 "too many values for argument");
239 pwarn(struct mdoc
*mdoc
, int line
, int pos
, int code
)
245 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
246 "unexpected quoted parameter");
249 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
250 "argument-like parameter");
253 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
254 "last list column is empty");
257 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_COMPAT
,
258 "trailing whitespace");
269 mdoc_args(struct mdoc
*mdoc
, int line
,
270 int *pos
, char *buf
, int tok
, char **v
)
275 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
278 * Override per-macro argument flags with context-specific ones.
279 * As of now, this is only valid for `It' depending on its list
285 for (n
= mdoc
->last
; n
; n
= n
->parent
)
286 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
290 c
= (int)n
->data
.block
.argc
;
294 * Using `Bl -column' adds ARGS_TABSEP to the arguments
295 * and invalidates ARGS_DELIM. Using `Bl -diag' allows
296 * for quoted arguments.
300 for (i
= 0; i
< c
; i
++) {
301 switch (n
->data
.block
.argv
[i
].arg
) {
319 /* Continue parsing the arguments themselves... */
321 return(args(mdoc
, line
, pos
, buf
, fl
, v
));
326 args(struct mdoc
*mdoc
, int line
,
327 int *pos
, char *buf
, int fl
, char **v
)
337 if ('\"' == buf
[*pos
] && ! (fl
& ARGS_QUOTED
))
338 if ( ! pwarn(mdoc
, line
, *pos
, WQUOTPARM
))
341 if ( ! (fl
& ARGS_ARGVLIKE
) && '-' == buf
[*pos
])
342 if ( ! pwarn(mdoc
, line
, *pos
, WARGVPARM
))
346 * If the first character is a delimiter and we're to look for
347 * delimited strings, then pass down the buffer seeing if it
348 * follows the pattern of [[::delim::][ ]+]+.
351 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
352 for (i
= *pos
; buf
[i
]; ) {
353 if ( ! mdoc_iscdelim(buf
[i
]))
356 /* There must be at least one space... */
357 if (0 == buf
[i
] || ! isspace((u_char
)buf
[i
]))
360 while (buf
[i
] && isspace((u_char
)buf
[i
]))
369 /* First parse non-quoted strings. */
371 if ('\"' != buf
[*pos
] || ! (ARGS_QUOTED
& fl
)) {
375 * Thar be dragons here! If we're tab-separated, search
376 * ahead for either a tab or the `Ta' macro. If a tab
377 * is detected, it mustn't be escaped; if a `Ta' is
378 * detected, it must be space-buffered before and after.
379 * If either of these hold true, then prune out the
380 * extra spaces and call it an argument.
383 if (ARGS_TABSEP
& fl
) {
384 /* Scan ahead to unescaped tab. */
386 for (p
= *v
; ; p
++) {
387 if (NULL
== (p
= strchr(p
, '\t')))
391 if ('\\' != *(p
- 1))
395 /* Scan ahead to unescaped `Ta'. */
397 for (pp
= *v
; ; pp
++) {
398 if (NULL
== (pp
= strstr(pp
, "Ta")))
400 if (pp
> *v
&& ' ' != *(pp
- 1))
402 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
406 /* Choose delimiter tab/Ta. */
409 p
= (p
< pp
? p
: pp
);
413 /* Strip delimiter's preceding whitespace. */
417 while (pp
> *v
&& ' ' == *pp
)
419 if (pp
== *v
&& ' ' == *pp
)
425 /* ...in- and proceding whitespace. */
427 if (p
&& ('\t' != *p
)) {
438 *pos
+= (int)(p
- *v
);
442 if ( ! pwarn(mdoc
, line
, *pos
, WCOLEMPTY
))
444 if (p
&& 0 == *p
&& p
> *v
&& ' ' == *(p
- 1))
445 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
451 /* Configure the eoln case, too. */
456 if (p
> *v
&& ' ' == *(p
- 1))
457 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
459 *pos
+= (int)(p
- *v
);
464 /* Do non-tabsep look-ahead here. */
466 if ( ! (ARGS_TABSEP
& fl
))
468 if (isspace((u_char
)buf
[*pos
]))
469 if ('\\' != buf
[*pos
- 1])
482 if ( ! (ARGS_TABSEP
& fl
))
483 while (buf
[*pos
] && isspace((u_char
)buf
[*pos
]))
489 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
496 * If we're a quoted string (and quoted strings are allowed),
497 * then parse ahead to the next quote. If none's found, it's an
498 * error. After, parse to the next word.
503 while (buf
[*pos
] && '\"' != buf
[*pos
])
506 if (0 == buf
[*pos
]) {
507 (void)perr(mdoc
, line
, *pos
, EQUOTTERM
);
515 while (buf
[*pos
] && isspace((u_char
)buf
[*pos
]))
521 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
529 argv_a2arg(int tok
, const char *argv
)
533 * Parse an argument identifier from its text. XXX - this
534 * should really be table-driven to clarify the code.
536 * If you add an argument to the list, make sure that you
537 * register it here with its one or more macros!
542 if (xstrcmp(argv
, "split"))
544 else if (xstrcmp(argv
, "nosplit"))
545 return(MDOC_Nosplit
);
549 if (xstrcmp(argv
, "ragged"))
551 else if (xstrcmp(argv
, "unfilled"))
552 return(MDOC_Unfilled
);
553 else if (xstrcmp(argv
, "filled"))
555 else if (xstrcmp(argv
, "literal"))
556 return(MDOC_Literal
);
557 else if (xstrcmp(argv
, "file"))
559 else if (xstrcmp(argv
, "offset"))
564 if (xstrcmp(argv
, "emphasis"))
565 return(MDOC_Emphasis
);
566 else if (xstrcmp(argv
, "literal"))
567 return(MDOC_Literal
);
568 else if (xstrcmp(argv
, "symbolic"))
569 return(MDOC_Symbolic
);
573 if (xstrcmp(argv
, "words"))
578 if (xstrcmp(argv
, "bullet"))
580 else if (xstrcmp(argv
, "dash"))
582 else if (xstrcmp(argv
, "hyphen"))
584 else if (xstrcmp(argv
, "item"))
586 else if (xstrcmp(argv
, "enum"))
588 else if (xstrcmp(argv
, "tag"))
590 else if (xstrcmp(argv
, "diag"))
592 else if (xstrcmp(argv
, "hang"))
594 else if (xstrcmp(argv
, "ohang"))
596 else if (xstrcmp(argv
, "inset"))
598 else if (xstrcmp(argv
, "column"))
600 else if (xstrcmp(argv
, "width"))
602 else if (xstrcmp(argv
, "offset"))
604 else if (xstrcmp(argv
, "compact"))
605 return(MDOC_Compact
);
611 if (xstrcmp(argv
, "std"))
618 return(MDOC_ARG_MAX
);
623 argv_multi(struct mdoc
*mdoc
, int line
,
624 struct mdoc_arg
*v
, int *pos
, char *buf
)
630 v
->value
= xcalloc(MDOC_LINEARG_MAX
, sizeof(char *));
634 for (v
->sz
= 0; v
->sz
< MDOC_LINEARG_MAX
; v
->sz
++) {
635 if ('-' == buf
[*pos
])
637 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
638 if (ARGS_ERROR
== c
) {
641 } else if (ARGS_EOLN
== c
)
643 v
->value
[(int)v
->sz
] = p
;
646 if (0 < v
->sz
&& v
->sz
< MDOC_LINEARG_MAX
)
651 return(perr(mdoc
, line
, ppos
, EARGVAL
));
653 return(perr(mdoc
, line
, ppos
, EARGMANY
));
658 argv_opt_single(struct mdoc
*mdoc
, int line
,
659 struct mdoc_arg
*v
, int *pos
, char *buf
)
666 if ('-' == buf
[*pos
])
669 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
676 v
->value
= xcalloc(1, sizeof(char *));
683 * Parse a single, mandatory value from the stream.
686 argv_single(struct mdoc
*mdoc
, int line
,
687 struct mdoc_arg
*v
, int *pos
, char *buf
)
694 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
698 return(perr(mdoc
, line
, ppos
, EARGVAL
));
701 v
->value
= xcalloc(1, sizeof(char *));
708 * Determine rules for parsing arguments. Arguments can either accept
709 * no parameters, an optional single parameter, one parameter, or
710 * multiple parameters.
713 argv(struct mdoc
*mdoc
, int tok
, int line
,
714 struct mdoc_arg
*v
, int *pos
, char *buf
)
720 fl
= mdoc_argvflags
[v
->arg
];
723 * Override the default per-argument value.
728 fl
= ARGV_OPT_SINGLE
;
736 return(argv_single(mdoc
, line
, v
, pos
, buf
));
738 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
739 case (ARGV_OPT_SINGLE
):
740 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));
751 * Parse an argument from line text. This comes in the form of -key
752 * [value0...], which may either have a single mandatory value, at least
753 * one mandatory value, an optional single value, or no value.
756 mdoc_argv(struct mdoc
*mdoc
, int line
, int tok
,
757 struct mdoc_arg
*v
, int *pos
, char *buf
)
762 (void)memset(v
, 0, sizeof(struct mdoc_arg
));
767 assert( ! isspace((u_char
)buf
[*pos
]));
769 if ('-' != buf
[*pos
])
782 if (isspace((u_char
)buf
[*pos
]))
783 if ('\\' != buf
[*pos
- 1])
792 * We now parse out the per-macro arguments. XXX - this can be
793 * made much cleaner using per-argument tables. See argv_a2arg
797 if (MDOC_ARG_MAX
== (v
->arg
= argv_a2arg(tok
, p
))) {
798 if ( ! pwarn(mdoc
, line
, i
, WARGVPARM
))
803 while (buf
[*pos
] && isspace((u_char
)buf
[*pos
]))
806 /* FIXME: whitespace if no value. */
808 if ( ! argv(mdoc
, tok
, line
, v
, pos
, buf
))
816 mdoc_argv_free(int sz
, struct mdoc_arg
*arg
)
820 for (i
= 0; i
< sz
; i
++) {
821 if (0 == arg
[i
].sz
) {
822 assert(NULL
== arg
[i
].value
);
825 assert(arg
[i
].value
);