]>
git.cameronkatri.com Git - mandoc.git/blob - argv.c
a49c5146afbe07ecccdeae4c4ea569ffb9cdd761
1 /* $Id: argv.c,v 1.33 2009/02/27 09:39:40 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)
38 static int argv_a2arg(int, const char *);
39 static int args(struct mdoc
*, int, int *,
40 char *, int, char **);
41 static int argv(struct mdoc
*, int,
42 struct mdoc_arg
*, int *, char *);
43 static int argv_single(struct mdoc
*, int,
44 struct mdoc_arg
*, int *, char *);
45 static int argv_multi(struct mdoc
*, int,
46 struct mdoc_arg
*, int *, char *);
47 static int pwarn(struct mdoc
*, int, int, int);
48 static int perr(struct mdoc
*, int, int, int);
50 /* Warning messages. */
63 static int mdoc_argflags
[MDOC_MAX
] = {
87 ARGS_DELIM
| ARGS_QUOTED
, /* Fa */
90 ARGS_DELIM
| ARGS_QUOTED
, /* Fn */
91 ARGS_DELIM
| ARGS_QUOTED
, /* Ft */
105 ARGS_QUOTED
, /* %A */
106 ARGS_QUOTED
, /* %B */
107 ARGS_QUOTED
, /* %D */
108 ARGS_QUOTED
, /* %I */
109 ARGS_QUOTED
, /* %J */
110 ARGS_QUOTED
, /* %N */
111 ARGS_QUOTED
, /* %O */
112 ARGS_QUOTED
, /* %P */
113 ARGS_QUOTED
, /* %R */
114 ARGS_QUOTED
, /* %T */
115 ARGS_QUOTED
, /* %V */
124 ARGS_DELIM
, /* Bsx */
174 perr(struct mdoc
*mdoc
, int line
, int pos
, int code
)
180 c
= mdoc_perr(mdoc
, line
, pos
,
181 "unterminated quoted parameter");
184 c
= mdoc_perr(mdoc
, line
, pos
,
185 "argument requires a value");
188 c
= mdoc_perr(mdoc
, line
, pos
,
189 "too many values for argument");
200 pwarn(struct mdoc
*mdoc
, int line
, int pos
, int code
)
206 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
207 "unexpected quoted parameter");
210 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
211 "argument-like parameter");
214 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
215 "last list column is empty");
218 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_COMPAT
,
219 "trailing whitespace");
230 mdoc_args(struct mdoc
*mdoc
, int line
,
231 int *pos
, char *buf
, int tok
, char **v
)
236 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
239 * First see if we should use TABSEP (Bl -column). This
240 * invalidates the use of ARGS_DELIM.
243 if (MDOC_It
== tok
) {
244 for (n
= mdoc
->last
; n
; n
= n
->parent
)
245 if (MDOC_BLOCK
== n
->type
)
246 if (MDOC_Bl
== n
->tok
)
249 c
= (int)n
->data
.block
.argc
;
253 for (i
= 0; i
< c
; i
++) {
254 if (MDOC_Column
!= n
->data
.block
.argv
[i
].arg
)
262 return(args(mdoc
, line
, pos
, buf
, fl
, v
));
267 args(struct mdoc
*mdoc
, int line
,
268 int *pos
, char *buf
, int fl
, char **v
)
278 if ('\"' == buf
[*pos
] && ! (fl
& ARGS_QUOTED
))
279 if ( ! pwarn(mdoc
, line
, *pos
, WQUOTPARM
))
282 if ('-' == buf
[*pos
])
283 if ( ! pwarn(mdoc
, line
, *pos
, WARGVPARM
))
287 * If the first character is a delimiter and we're to look for
288 * delimited strings, then pass down the buffer seeing if it
289 * follows the pattern of [[::delim::][ ]+]+.
292 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
293 for (i
= *pos
; buf
[i
]; ) {
294 if ( ! mdoc_iscdelim(buf
[i
]))
297 while (buf
[i
] && isspace((int)buf
[i
]))
306 /* First parse non-quoted strings. */
308 if ('\"' != buf
[*pos
] || ! (ARGS_QUOTED
& fl
)) {
312 * Thar be dragons here! If we're tab-separated, search
313 * ahead for either a tab or the `Ta' macro. If a tab
314 * is detected, it mustn't be escaped; if a `Ta' is
315 * detected, it must be space-buffered before and after.
316 * If either of these hold true, then prune out the
317 * extra spaces and call it an argument.
320 if (ARGS_TABSEP
& fl
) {
321 /* Scan ahead to unescaped tab. */
323 for (p
= *v
; ; p
++) {
324 if (NULL
== (p
= strchr(p
, '\t')))
328 if ('\\' != *(p
- 1))
332 /* Scan ahead to unescaped `Ta'. */
334 for (pp
= *v
; ; pp
++) {
335 if (NULL
== (pp
= strstr(pp
, "Ta")))
337 if (pp
> *v
&& ' ' != *(pp
- 1))
339 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
343 /* Choose delimiter tab/Ta. */
346 p
= (p
< pp
? p
: pp
);
350 /* Strip delimiter's preceding whitespace. */
354 while (pp
> *v
&& ' ' == *pp
)
356 if (pp
== *v
&& ' ' == *pp
)
362 /* ...in- and proceding whitespace. */
364 if (p
&& ('\t' != *p
)) {
375 *pos
+= (int)(p
- *v
);
379 if ( ! pwarn(mdoc
, line
, *pos
, WCOLEMPTY
))
381 if (p
&& 0 == *p
&& p
> *v
&& ' ' == *(p
- 1))
382 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
388 /* Configure the eoln case, too. */
393 if (p
> *v
&& ' ' == *(p
- 1))
394 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
396 *pos
+= (int)(p
- *v
);
401 /* Do non-tabsep look-ahead here. */
403 if ( ! (ARGS_TABSEP
& fl
))
405 if (isspace((int)buf
[*pos
]))
406 if ('\\' != buf
[*pos
- 1])
419 if ( ! (ARGS_TABSEP
& fl
))
420 while (buf
[*pos
] && isspace((int)buf
[*pos
]))
426 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
433 * If we're a quoted string (and quoted strings are allowed),
434 * then parse ahead to the next quote. If none's found, it's an
435 * error. After, parse to the next word.
440 while (buf
[*pos
] && '\"' != buf
[*pos
])
443 if (0 == buf
[*pos
]) {
444 (void)perr(mdoc
, line
, *pos
, EQUOTTERM
);
452 while (buf
[*pos
] && isspace((int)buf
[*pos
]))
458 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
466 argv_a2arg(int tok
, const char *argv
)
471 if (xstrcmp(argv
, "split"))
473 else if (xstrcmp(argv
, "nosplit"))
474 return(MDOC_Nosplit
);
478 if (xstrcmp(argv
, "ragged"))
480 else if (xstrcmp(argv
, "unfilled"))
481 return(MDOC_Unfilled
);
482 else if (xstrcmp(argv
, "filled"))
484 else if (xstrcmp(argv
, "literal"))
485 return(MDOC_Literal
);
486 else if (xstrcmp(argv
, "file"))
488 else if (xstrcmp(argv
, "offset"))
493 if (xstrcmp(argv
, "emphasis"))
494 return(MDOC_Emphasis
);
495 else if (xstrcmp(argv
, "literal"))
496 return(MDOC_Literal
);
497 else if (xstrcmp(argv
, "symbolic"))
498 return(MDOC_Symbolic
);
502 if (xstrcmp(argv
, "words"))
507 if (xstrcmp(argv
, "bullet"))
509 else if (xstrcmp(argv
, "dash"))
511 else if (xstrcmp(argv
, "hyphen"))
513 else if (xstrcmp(argv
, "item"))
515 else if (xstrcmp(argv
, "enum"))
517 else if (xstrcmp(argv
, "tag"))
519 else if (xstrcmp(argv
, "diag"))
521 else if (xstrcmp(argv
, "hang"))
523 else if (xstrcmp(argv
, "ohang"))
525 else if (xstrcmp(argv
, "inset"))
527 else if (xstrcmp(argv
, "column"))
529 else if (xstrcmp(argv
, "width"))
531 else if (xstrcmp(argv
, "offset"))
533 else if (xstrcmp(argv
, "compact"))
534 return(MDOC_Compact
);
540 if (xstrcmp(argv
, "std"))
545 if (xstrcmp(argv
, "p1003.1-88"))
546 return(MDOC_p1003_1_88
);
547 else if (xstrcmp(argv
, "p1003.1-90"))
548 return(MDOC_p1003_1_90
);
549 else if (xstrcmp(argv
, "p1003.1-96"))
550 return(MDOC_p1003_1_96
);
551 else if (xstrcmp(argv
, "p1003.1-2001"))
552 return(MDOC_p1003_1_2001
);
553 else if (xstrcmp(argv
, "p1003.1-2004"))
554 return(MDOC_p1003_1_2004
);
555 else if (xstrcmp(argv
, "p1003.1"))
556 return(MDOC_p1003_1
);
557 else if (xstrcmp(argv
, "p1003.1b"))
558 return(MDOC_p1003_1b
);
559 else if (xstrcmp(argv
, "p1003.1b-93"))
560 return(MDOC_p1003_1b_93
);
561 else if (xstrcmp(argv
, "p1003.1c-95"))
562 return(MDOC_p1003_1c_95
);
563 else if (xstrcmp(argv
, "p1003.1g-2000"))
564 return(MDOC_p1003_1g_2000
);
565 else if (xstrcmp(argv
, "p1003.2-92"))
566 return(MDOC_p1003_2_92
);
567 else if (xstrcmp(argv
, "p1003.2-95"))
568 return(MDOC_p1387_2_95
);
569 else if (xstrcmp(argv
, "p1003.2"))
570 return(MDOC_p1003_2
);
571 else if (xstrcmp(argv
, "p1387.2-95"))
572 return(MDOC_p1387_2
);
573 else if (xstrcmp(argv
, "isoC-90"))
574 return(MDOC_isoC_90
);
575 else if (xstrcmp(argv
, "isoC-amd1"))
576 return(MDOC_isoC_amd1
);
577 else if (xstrcmp(argv
, "isoC-tcor1"))
578 return(MDOC_isoC_tcor1
);
579 else if (xstrcmp(argv
, "isoC-tcor2"))
580 return(MDOC_isoC_tcor2
);
581 else if (xstrcmp(argv
, "isoC-99"))
582 return(MDOC_isoC_99
);
583 else if (xstrcmp(argv
, "ansiC"))
585 else if (xstrcmp(argv
, "ansiC-89"))
586 return(MDOC_ansiC_89
);
587 else if (xstrcmp(argv
, "ansiC-99"))
588 return(MDOC_ansiC_99
);
589 else if (xstrcmp(argv
, "ieee754"))
590 return(MDOC_ieee754
);
591 else if (xstrcmp(argv
, "iso8802-3"))
592 return(MDOC_iso8802_3
);
593 else if (xstrcmp(argv
, "xpg3"))
595 else if (xstrcmp(argv
, "xpg4"))
597 else if (xstrcmp(argv
, "xpg4.2"))
599 else if (xstrcmp(argv
, "xpg4.3"))
601 else if (xstrcmp(argv
, "xbd5"))
603 else if (xstrcmp(argv
, "xcu5"))
605 else if (xstrcmp(argv
, "xsh5"))
607 else if (xstrcmp(argv
, "xns5"))
609 else if (xstrcmp(argv
, "xns5.2d2.0"))
610 return(MDOC_xns5_2d2_0
);
611 else if (xstrcmp(argv
, "xcurses4.2"))
612 return(MDOC_xcurses4_2
);
613 else if (xstrcmp(argv
, "susv2"))
615 else if (xstrcmp(argv
, "susv3"))
617 else if (xstrcmp(argv
, "svid4"))
625 return(MDOC_ARG_MAX
);
630 argv_multi(struct mdoc
*mdoc
, int line
,
631 struct mdoc_arg
*v
, int *pos
, char *buf
)
637 v
->value
= xcalloc(MDOC_LINEARG_MAX
, sizeof(char *));
641 for (v
->sz
= 0; v
->sz
< MDOC_LINEARG_MAX
; v
->sz
++) {
642 if ('-' == buf
[*pos
])
644 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
645 if (ARGS_ERROR
== c
) {
648 } else if (ARGS_EOLN
== c
)
650 v
->value
[(int)v
->sz
] = p
;
653 if (0 < v
->sz
&& v
->sz
< MDOC_LINEARG_MAX
)
658 return(perr(mdoc
, line
, ppos
, EARGVAL
));
660 return(perr(mdoc
, line
, ppos
, EARGMANY
));
665 argv_single(struct mdoc
*mdoc
, int line
,
666 struct mdoc_arg
*v
, int *pos
, char *buf
)
673 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
677 return(perr(mdoc
, line
, ppos
, EARGVAL
));
680 v
->value
= xcalloc(1, sizeof(char *));
687 argv(struct mdoc
*mdoc
, int line
,
688 struct mdoc_arg
*v
, int *pos
, char *buf
)
700 return(argv_single(mdoc
, line
, v
, pos
, buf
));
702 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
712 mdoc_argv(struct mdoc
*mdoc
, int line
, int tok
,
713 struct mdoc_arg
*v
, int *pos
, char *buf
)
718 (void)memset(v
, 0, sizeof(struct mdoc_arg
));
723 assert( ! isspace((int)buf
[*pos
]));
725 if ('-' != buf
[*pos
])
738 if (isspace((int)buf
[*pos
]))
739 if ('\\' != buf
[*pos
- 1])
747 if (MDOC_ARG_MAX
== (v
->arg
= argv_a2arg(tok
, p
))) {
748 if ( ! pwarn(mdoc
, line
, i
, WARGVPARM
))
753 while (buf
[*pos
] && isspace((int)buf
[*pos
]))
756 /* FIXME: whitespace if no value. */
758 if ( ! argv(mdoc
, line
, v
, pos
, buf
))
766 mdoc_argv_free(int sz
, struct mdoc_arg
*arg
)
770 for (i
= 0; i
< sz
; i
++) {
771 if (0 == arg
[i
].sz
) {
772 assert(NULL
== arg
[i
].value
);
775 assert(arg
[i
].value
);