]>
git.cameronkatri.com Git - mandoc.git/blob - argv.c
1 /* $Id: argv.c,v 1.28 2009/02/23 15:19: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)
38 static int lookup(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
; (c
= buf
[i
]); ) {
294 if ( ! mdoc_iscdelim(c
))
297 if (0 == buf
[i
] || ! isspace(c
))
300 while (buf
[i
] && isspace(c
))
309 /* First parse non-quoted strings. */
311 if ('\"' != buf
[*pos
] || ! (ARGS_QUOTED
& fl
)) {
315 * Thar be dragons here! If we're tab-separated, search
316 * ahead for either a tab or the `Ta' macro. If a tab
317 * is detected, it mustn't be escaped; if a `Ta' is
318 * detected, it must be space-buffered before and after.
319 * If either of these hold true, then prune out the
320 * extra spaces and call it an argument.
323 if (ARGS_TABSEP
& fl
) {
324 /* Scan ahead to unescaped tab. */
326 for (p
= *v
; ; p
++) {
327 if (NULL
== (p
= strchr(p
, '\t')))
331 if ('\\' != *(p
- 1))
335 /* Scan ahead to unescaped `Ta'. */
337 for (pp
= *v
; ; pp
++) {
338 if (NULL
== (pp
= strstr(pp
, "Ta")))
340 if (pp
> *v
&& ' ' != *(pp
- 1))
342 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
346 /* Choose delimiter tab/Ta. */
349 p
= (p
< pp
? p
: pp
);
353 /* Strip delimiter's preceding whitespace. */
357 while (pp
> *v
&& ' ' == *pp
)
359 if (pp
== *v
&& ' ' == *pp
)
365 /* ...in- and proceding whitespace. */
367 if (p
&& ('\t' != *p
)) {
382 if ( ! pwarn(mdoc
, line
, *pos
, WCOLEMPTY
))
384 if (p
&& 0 == *p
&& p
> *v
&& ' ' == *(p
- 1))
385 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
391 /* Configure the eoln case, too. */
396 if (p
> *v
&& ' ' == *(p
- 1))
397 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
404 /* Do non-tabsep look-ahead here. */
406 if ( ! (ARGS_TABSEP
& fl
))
407 while ((c
= buf
[*pos
])) {
409 if ('\\' != buf
[*pos
- 1])
422 if ( ! (ARGS_TABSEP
& fl
))
423 while (buf
[*pos
] && isspace((int)buf
[*pos
]))
429 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
436 * If we're a quoted string (and quoted strings are allowed),
437 * then parse ahead to the next quote. If none's found, it's an
438 * error. After, parse to the next word.
443 while (buf
[*pos
] && '\"' != buf
[*pos
])
446 if (0 == buf
[*pos
]) {
447 (void)perr(mdoc
, line
, *pos
, EQUOTTERM
);
455 while (buf
[*pos
] && isspace((int)buf
[*pos
]))
461 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
469 lookup(int tok
, const char *argv
)
474 if (xstrcmp(argv
, "split"))
476 else if (xstrcmp(argv
, "nosplit"))
477 return(MDOC_Nosplit
);
481 if (xstrcmp(argv
, "ragged"))
483 else if (xstrcmp(argv
, "unfilled"))
484 return(MDOC_Unfilled
);
485 else if (xstrcmp(argv
, "filled"))
487 else if (xstrcmp(argv
, "literal"))
488 return(MDOC_Literal
);
489 else if (xstrcmp(argv
, "file"))
491 else if (xstrcmp(argv
, "offset"))
496 if (xstrcmp(argv
, "emphasis"))
497 return(MDOC_Emphasis
);
498 else if (xstrcmp(argv
, "literal"))
499 return(MDOC_Literal
);
500 else if (xstrcmp(argv
, "symbolic"))
501 return(MDOC_Symbolic
);
505 if (xstrcmp(argv
, "words"))
510 if (xstrcmp(argv
, "bullet"))
512 else if (xstrcmp(argv
, "dash"))
514 else if (xstrcmp(argv
, "hyphen"))
516 else if (xstrcmp(argv
, "item"))
518 else if (xstrcmp(argv
, "enum"))
520 else if (xstrcmp(argv
, "tag"))
522 else if (xstrcmp(argv
, "diag"))
524 else if (xstrcmp(argv
, "hang"))
526 else if (xstrcmp(argv
, "ohang"))
528 else if (xstrcmp(argv
, "inset"))
530 else if (xstrcmp(argv
, "column"))
532 else if (xstrcmp(argv
, "width"))
534 else if (xstrcmp(argv
, "offset"))
536 else if (xstrcmp(argv
, "compact"))
537 return(MDOC_Compact
);
543 if (xstrcmp(argv
, "std"))
548 if (xstrcmp(argv
, "p1003.1-88"))
549 return(MDOC_p1003_1_88
);
550 else if (xstrcmp(argv
, "p1003.1-90"))
551 return(MDOC_p1003_1_90
);
552 else if (xstrcmp(argv
, "p1003.1-96"))
553 return(MDOC_p1003_1_96
);
554 else if (xstrcmp(argv
, "p1003.1-2001"))
555 return(MDOC_p1003_1_2001
);
556 else if (xstrcmp(argv
, "p1003.1-2004"))
557 return(MDOC_p1003_1_2004
);
558 else if (xstrcmp(argv
, "p1003.1"))
559 return(MDOC_p1003_1
);
560 else if (xstrcmp(argv
, "p1003.1b"))
561 return(MDOC_p1003_1b
);
562 else if (xstrcmp(argv
, "p1003.1b-93"))
563 return(MDOC_p1003_1b_93
);
564 else if (xstrcmp(argv
, "p1003.1c-95"))
565 return(MDOC_p1003_1c_95
);
566 else if (xstrcmp(argv
, "p1003.1g-2000"))
567 return(MDOC_p1003_1g_2000
);
568 else if (xstrcmp(argv
, "p1003.2-92"))
569 return(MDOC_p1003_2_92
);
570 else if (xstrcmp(argv
, "p1003.2-95"))
571 return(MDOC_p1387_2_95
);
572 else if (xstrcmp(argv
, "p1003.2"))
573 return(MDOC_p1003_2
);
574 else if (xstrcmp(argv
, "p1387.2-95"))
575 return(MDOC_p1387_2
);
576 else if (xstrcmp(argv
, "isoC-90"))
577 return(MDOC_isoC_90
);
578 else if (xstrcmp(argv
, "isoC-amd1"))
579 return(MDOC_isoC_amd1
);
580 else if (xstrcmp(argv
, "isoC-tcor1"))
581 return(MDOC_isoC_tcor1
);
582 else if (xstrcmp(argv
, "isoC-tcor2"))
583 return(MDOC_isoC_tcor2
);
584 else if (xstrcmp(argv
, "isoC-99"))
585 return(MDOC_isoC_99
);
586 else if (xstrcmp(argv
, "ansiC"))
588 else if (xstrcmp(argv
, "ansiC-89"))
589 return(MDOC_ansiC_89
);
590 else if (xstrcmp(argv
, "ansiC-99"))
591 return(MDOC_ansiC_99
);
592 else if (xstrcmp(argv
, "ieee754"))
593 return(MDOC_ieee754
);
594 else if (xstrcmp(argv
, "iso8802-3"))
595 return(MDOC_iso8802_3
);
596 else if (xstrcmp(argv
, "xpg3"))
598 else if (xstrcmp(argv
, "xpg4"))
600 else if (xstrcmp(argv
, "xpg4.2"))
602 else if (xstrcmp(argv
, "xpg4.3"))
604 else if (xstrcmp(argv
, "xbd5"))
606 else if (xstrcmp(argv
, "xcu5"))
608 else if (xstrcmp(argv
, "xsh5"))
610 else if (xstrcmp(argv
, "xns5"))
612 else if (xstrcmp(argv
, "xns5.2d2.0"))
613 return(MDOC_xns5_2d2_0
);
614 else if (xstrcmp(argv
, "xcurses4.2"))
615 return(MDOC_xcurses4_2
);
616 else if (xstrcmp(argv
, "susv2"))
618 else if (xstrcmp(argv
, "susv3"))
620 else if (xstrcmp(argv
, "svid4"))
628 return(MDOC_ARG_MAX
);
633 argv_multi(struct mdoc
*mdoc
, int line
,
634 struct mdoc_arg
*v
, int *pos
, char *buf
)
640 v
->value
= xcalloc(MDOC_LINEARG_MAX
, sizeof(char *));
644 for (v
->sz
= 0; v
->sz
< MDOC_LINEARG_MAX
; v
->sz
++) {
645 if ('-' == buf
[*pos
])
647 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
648 if (ARGS_ERROR
== c
) {
651 } else if (ARGS_EOLN
== c
)
656 if (0 < v
->sz
&& v
->sz
< MDOC_LINEARG_MAX
)
661 return(perr(mdoc
, line
, ppos
, EARGVAL
));
663 return(perr(mdoc
, line
, ppos
, EARGMANY
));
668 argv_single(struct mdoc
*mdoc
, int line
,
669 struct mdoc_arg
*v
, int *pos
, char *buf
)
676 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
680 return(perr(mdoc
, line
, ppos
, EARGVAL
));
683 v
->value
= xcalloc(1, sizeof(char *));
690 argv(struct mdoc
*mdoc
, int line
,
691 struct mdoc_arg
*v
, int *pos
, char *buf
)
703 return(argv_single(mdoc
, line
, v
, pos
, buf
));
705 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
715 mdoc_argv(struct mdoc
*mdoc
, int line
, int tok
,
716 struct mdoc_arg
*v
, int *pos
, char *buf
)
721 (void)memset(v
, 0, sizeof(struct mdoc_arg
));
726 assert( ! isspace((int)buf
[*pos
]));
728 if ('-' != buf
[*pos
])
741 if (isspace((int)buf
[*pos
]))
742 if ('\\' != buf
[*pos
- 1])
750 if (MDOC_ARG_MAX
== (v
->arg
= lookup(tok
, p
))) {
751 if ( ! pwarn(mdoc
, line
, i
, WARGVPARM
))
756 while (buf
[*pos
] && isspace((int)buf
[*pos
]))
759 /* FIXME: whitespace if no value. */
762 if ( ! argv(mdoc
, line
, v
, pos
, buf
))
770 mdoc_argv_free(int sz
, struct mdoc_arg
*arg
)
774 for (i
= 0; i
< sz
; i
++) {
775 if (0 == arg
[i
].sz
) {
776 assert(NULL
== arg
[i
].value
);
779 assert(arg
[i
].value
);