]>
git.cameronkatri.com Git - mandoc.git/blob - argv.c
1 /* $Id: argv.c,v 1.27 2009/02/23 12:45:19 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 postargv(struct mdoc
*, int,
48 const struct mdoc_arg
*, int);
49 static int pwarn(struct mdoc
*, int, int, int);
50 static int perr(struct mdoc
*, int, int, int);
52 /* Warning messages. */
66 static int mdoc_argflags
[MDOC_MAX
] = {
90 ARGS_DELIM
| ARGS_QUOTED
, /* Fa */
93 ARGS_DELIM
| ARGS_QUOTED
, /* Fn */
94 ARGS_DELIM
| ARGS_QUOTED
, /* Ft */
108 ARGS_QUOTED
, /* %A */
109 ARGS_QUOTED
, /* %B */
110 ARGS_QUOTED
, /* %D */
111 ARGS_QUOTED
, /* %I */
112 ARGS_QUOTED
, /* %J */
113 ARGS_QUOTED
, /* %N */
114 ARGS_QUOTED
, /* %O */
115 ARGS_QUOTED
, /* %P */
116 ARGS_QUOTED
, /* %R */
117 ARGS_QUOTED
, /* %T */
118 ARGS_QUOTED
, /* %V */
127 ARGS_DELIM
, /* Bsx */
177 perr(struct mdoc
*mdoc
, int line
, int pos
, int code
)
183 c
= mdoc_perr(mdoc
, line
, pos
,
184 "unterminated quoted parameter");
187 c
= mdoc_perr(mdoc
, line
, pos
,
188 "invalid value for offset argument");
191 c
= mdoc_perr(mdoc
, line
, pos
,
192 "argument requires a value");
195 c
= mdoc_perr(mdoc
, line
, pos
,
196 "too many values for argument");
207 pwarn(struct mdoc
*mdoc
, int line
, int pos
, int code
)
213 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
214 "unexpected quoted parameter");
217 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
218 "argument-like parameter");
221 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_SYNTAX
,
222 "last list column is empty");
225 c
= mdoc_pwarn(mdoc
, line
, pos
, WARN_COMPAT
,
226 "trailing whitespace");
237 mdoc_args(struct mdoc
*mdoc
, int line
,
238 int *pos
, char *buf
, int tok
, char **v
)
243 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
246 * First see if we should use TABSEP (Bl -column). This
247 * invalidates the use of ARGS_DELIM.
250 if (MDOC_It
== tok
) {
251 for (n
= mdoc
->last
; n
; n
= n
->parent
)
252 if (MDOC_BLOCK
== n
->type
)
253 if (MDOC_Bl
== n
->tok
)
256 c
= (int)n
->data
.block
.argc
;
260 for (i
= 0; i
< c
; i
++) {
261 if (MDOC_Column
!= n
->data
.block
.argv
[i
].arg
)
269 return(args(mdoc
, line
, pos
, buf
, fl
, v
));
274 args(struct mdoc
*mdoc
, int line
,
275 int *pos
, char *buf
, int fl
, char **v
)
285 if ('\"' == buf
[*pos
] && ! (fl
& ARGS_QUOTED
))
286 if ( ! pwarn(mdoc
, line
, *pos
, WQUOTPARM
))
289 if ('-' == buf
[*pos
])
290 if ( ! pwarn(mdoc
, line
, *pos
, WARGVPARM
))
294 * If the first character is a delimiter and we're to look for
295 * delimited strings, then pass down the buffer seeing if it
296 * follows the pattern of [[::delim::][ ]+]+.
299 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
300 for (i
= *pos
; (c
= buf
[i
]); ) {
301 if ( ! mdoc_iscdelim(c
))
304 if (0 == buf
[i
] || ! isspace(c
))
307 while (buf
[i
] && isspace(c
))
316 /* First parse non-quoted strings. */
318 if ('\"' != buf
[*pos
] || ! (ARGS_QUOTED
& fl
)) {
322 * Thar be dragons here! If we're tab-separated, search
323 * ahead for either a tab or the `Ta' macro. If a tab
324 * is detected, it mustn't be escaped; if a `Ta' is
325 * detected, it must be space-buffered before and after.
326 * If either of these hold true, then prune out the
327 * extra spaces and call it an argument.
330 if (ARGS_TABSEP
& fl
) {
331 /* Scan ahead to unescaped tab. */
333 for (p
= *v
; ; p
++) {
334 if (NULL
== (p
= strchr(p
, '\t')))
338 if ('\\' != *(p
- 1))
342 /* Scan ahead to unescaped `Ta'. */
344 for (pp
= *v
; ; pp
++) {
345 if (NULL
== (pp
= strstr(pp
, "Ta")))
347 if (pp
> *v
&& ' ' != *(pp
- 1))
349 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
353 /* Choose delimiter tab/Ta. */
356 p
= (p
< pp
? p
: pp
);
360 /* Strip delimiter's preceding whitespace. */
364 while (pp
> *v
&& ' ' == *pp
)
366 if (pp
== *v
&& ' ' == *pp
)
372 /* ...in- and proceding whitespace. */
374 if (p
&& ('\t' != *p
)) {
389 if ( ! pwarn(mdoc
, line
, *pos
, WCOLEMPTY
))
391 if (p
&& 0 == *p
&& p
> *v
&& ' ' == *(p
- 1))
392 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
398 /* Configure the eoln case, too. */
403 if (p
> *v
&& ' ' == *(p
- 1))
404 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
411 /* Do non-tabsep look-ahead here. */
413 if ( ! (ARGS_TABSEP
& fl
))
414 while ((c
= buf
[*pos
])) {
416 if ('\\' != buf
[*pos
- 1])
429 if ( ! (ARGS_TABSEP
& fl
))
430 while (buf
[*pos
] && isspace((int)buf
[*pos
]))
436 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
443 * If we're a quoted string (and quoted strings are allowed),
444 * then parse ahead to the next quote. If none's found, it's an
445 * error. After, parse to the next word.
450 while (buf
[*pos
] && '\"' != buf
[*pos
])
453 if (0 == buf
[*pos
]) {
454 (void)perr(mdoc
, line
, *pos
, EQUOTTERM
);
462 while (buf
[*pos
] && isspace((int)buf
[*pos
]))
468 if ( ! pwarn(mdoc
, line
, *pos
, WTAILWS
))
476 lookup(int tok
, const char *argv
)
481 if (xstrcmp(argv
, "split"))
483 else if (xstrcmp(argv
, "nosplit"))
484 return(MDOC_Nosplit
);
488 if (xstrcmp(argv
, "ragged"))
490 else if (xstrcmp(argv
, "unfilled"))
491 return(MDOC_Unfilled
);
492 else if (xstrcmp(argv
, "filled"))
494 else if (xstrcmp(argv
, "literal"))
495 return(MDOC_Literal
);
496 else if (xstrcmp(argv
, "file"))
498 else if (xstrcmp(argv
, "offset"))
503 if (xstrcmp(argv
, "emphasis"))
504 return(MDOC_Emphasis
);
505 else if (xstrcmp(argv
, "literal"))
506 return(MDOC_Literal
);
507 else if (xstrcmp(argv
, "symbolic"))
508 return(MDOC_Symbolic
);
512 if (xstrcmp(argv
, "words"))
517 if (xstrcmp(argv
, "bullet"))
519 else if (xstrcmp(argv
, "dash"))
521 else if (xstrcmp(argv
, "hyphen"))
523 else if (xstrcmp(argv
, "item"))
525 else if (xstrcmp(argv
, "enum"))
527 else if (xstrcmp(argv
, "tag"))
529 else if (xstrcmp(argv
, "diag"))
531 else if (xstrcmp(argv
, "hang"))
533 else if (xstrcmp(argv
, "ohang"))
535 else if (xstrcmp(argv
, "inset"))
537 else if (xstrcmp(argv
, "column"))
539 else if (xstrcmp(argv
, "width"))
541 else if (xstrcmp(argv
, "offset"))
543 else if (xstrcmp(argv
, "compact"))
544 return(MDOC_Compact
);
550 if (xstrcmp(argv
, "std"))
555 if (xstrcmp(argv
, "p1003.1-88"))
556 return(MDOC_p1003_1_88
);
557 else if (xstrcmp(argv
, "p1003.1-90"))
558 return(MDOC_p1003_1_90
);
559 else if (xstrcmp(argv
, "p1003.1-96"))
560 return(MDOC_p1003_1_96
);
561 else if (xstrcmp(argv
, "p1003.1-2001"))
562 return(MDOC_p1003_1_2001
);
563 else if (xstrcmp(argv
, "p1003.1-2004"))
564 return(MDOC_p1003_1_2004
);
565 else if (xstrcmp(argv
, "p1003.1"))
566 return(MDOC_p1003_1
);
567 else if (xstrcmp(argv
, "p1003.1b"))
568 return(MDOC_p1003_1b
);
569 else if (xstrcmp(argv
, "p1003.1b-93"))
570 return(MDOC_p1003_1b_93
);
571 else if (xstrcmp(argv
, "p1003.1c-95"))
572 return(MDOC_p1003_1c_95
);
573 else if (xstrcmp(argv
, "p1003.1g-2000"))
574 return(MDOC_p1003_1g_2000
);
575 else if (xstrcmp(argv
, "p1003.2-92"))
576 return(MDOC_p1003_2_92
);
577 else if (xstrcmp(argv
, "p1003.2-95"))
578 return(MDOC_p1387_2_95
);
579 else if (xstrcmp(argv
, "p1003.2"))
580 return(MDOC_p1003_2
);
581 else if (xstrcmp(argv
, "p1387.2-95"))
582 return(MDOC_p1387_2
);
583 else if (xstrcmp(argv
, "isoC-90"))
584 return(MDOC_isoC_90
);
585 else if (xstrcmp(argv
, "isoC-amd1"))
586 return(MDOC_isoC_amd1
);
587 else if (xstrcmp(argv
, "isoC-tcor1"))
588 return(MDOC_isoC_tcor1
);
589 else if (xstrcmp(argv
, "isoC-tcor2"))
590 return(MDOC_isoC_tcor2
);
591 else if (xstrcmp(argv
, "isoC-99"))
592 return(MDOC_isoC_99
);
593 else if (xstrcmp(argv
, "ansiC"))
595 else if (xstrcmp(argv
, "ansiC-89"))
596 return(MDOC_ansiC_89
);
597 else if (xstrcmp(argv
, "ansiC-99"))
598 return(MDOC_ansiC_99
);
599 else if (xstrcmp(argv
, "ieee754"))
600 return(MDOC_ieee754
);
601 else if (xstrcmp(argv
, "iso8802-3"))
602 return(MDOC_iso8802_3
);
603 else if (xstrcmp(argv
, "xpg3"))
605 else if (xstrcmp(argv
, "xpg4"))
607 else if (xstrcmp(argv
, "xpg4.2"))
609 else if (xstrcmp(argv
, "xpg4.3"))
611 else if (xstrcmp(argv
, "xbd5"))
613 else if (xstrcmp(argv
, "xcu5"))
615 else if (xstrcmp(argv
, "xsh5"))
617 else if (xstrcmp(argv
, "xns5"))
619 else if (xstrcmp(argv
, "xns5.2d2.0"))
620 return(MDOC_xns5_2d2_0
);
621 else if (xstrcmp(argv
, "xcurses4.2"))
622 return(MDOC_xcurses4_2
);
623 else if (xstrcmp(argv
, "susv2"))
625 else if (xstrcmp(argv
, "susv3"))
627 else if (xstrcmp(argv
, "svid4"))
635 return(MDOC_ARG_MAX
);
640 postargv(struct mdoc
*mdoc
, int line
, const struct mdoc_arg
*v
, int pos
)
647 if (xstrcmp(v
->value
[0], "left"))
649 if (xstrcmp(v
->value
[0], "right"))
651 if (xstrcmp(v
->value
[0], "center"))
653 if (xstrcmp(v
->value
[0], "indent"))
655 if (xstrcmp(v
->value
[0], "indent-two"))
657 return(perr(mdoc
, line
, pos
, EOFFSET
));
667 argv_multi(struct mdoc
*mdoc
, int line
,
668 struct mdoc_arg
*v
, int *pos
, char *buf
)
674 v
->value
= xcalloc(MDOC_LINEARG_MAX
, sizeof(char *));
678 for (v
->sz
= 0; v
->sz
< MDOC_LINEARG_MAX
; v
->sz
++) {
679 if ('-' == buf
[*pos
])
681 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
682 if (ARGS_ERROR
== c
) {
685 } else if (ARGS_EOLN
== c
)
690 if (0 < v
->sz
&& v
->sz
< MDOC_LINEARG_MAX
)
695 return(perr(mdoc
, line
, ppos
, EARGVAL
));
697 return(perr(mdoc
, line
, ppos
, EARGMANY
));
702 argv_single(struct mdoc
*mdoc
, int line
,
703 struct mdoc_arg
*v
, int *pos
, char *buf
)
710 c
= args(mdoc
, line
, pos
, buf
, ARGS_QUOTED
, &p
);
714 return(perr(mdoc
, line
, ppos
, EARGVAL
));
717 v
->value
= xcalloc(1, sizeof(char *));
724 argv(struct mdoc
*mdoc
, int line
,
725 struct mdoc_arg
*v
, int *pos
, char *buf
)
737 return(argv_single(mdoc
, line
, v
, pos
, buf
));
739 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
749 mdoc_argv(struct mdoc
*mdoc
, int line
, int tok
,
750 struct mdoc_arg
*v
, int *pos
, char *buf
)
755 (void)memset(v
, 0, sizeof(struct mdoc_arg
));
760 assert( ! isspace((int)buf
[*pos
]));
762 if ('-' != buf
[*pos
])
775 if (isspace((int)buf
[*pos
]))
776 if ('\\' != buf
[*pos
- 1])
784 if (MDOC_ARG_MAX
== (v
->arg
= lookup(tok
, p
))) {
785 if ( ! pwarn(mdoc
, line
, i
, WARGVPARM
))
790 while (buf
[*pos
] && isspace((int)buf
[*pos
]))
793 /* FIXME: whitespace if no value. */
796 if ( ! argv(mdoc
, line
, v
, pos
, buf
))
798 if ( ! postargv(mdoc
, line
, v
, ppos
))
806 mdoc_argv_free(int sz
, struct mdoc_arg
*arg
)
810 for (i
= 0; i
< sz
; i
++) {
811 if (0 == arg
[i
].sz
) {
812 assert(NULL
== arg
[i
].value
);
815 assert(arg
[i
].value
);