]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.18 2009/07/18 18:59:33 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 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 above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
28 * Routines to parse arguments of macros. Arguments follow the syntax
29 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
30 * arguments, multiple arguments per value, no-value arguments, etc.
32 * There's no limit to the number or arguments that may be allocated.
35 /* FIXME .Bf Li raises "macro-like parameter". */
37 #define ARGS_DELIM (1 << 1)
38 #define ARGS_TABSEP (1 << 2)
40 #define ARGV_NONE (1 << 0)
41 #define ARGV_SINGLE (1 << 1)
42 #define ARGV_MULTI (1 << 2)
43 #define ARGV_OPT_SINGLE (1 << 3)
47 static int argv_a2arg(int, const char *);
48 static int args(struct mdoc
*, int, int *,
49 char *, int, char **);
50 static int argv(struct mdoc
*, int,
51 struct mdoc_argv
*, int *, char *);
52 static int argv_single(struct mdoc
*, int,
53 struct mdoc_argv
*, int *, char *);
54 static int argv_opt_single(struct mdoc
*, int,
55 struct mdoc_argv
*, int *, char *);
56 static int argv_multi(struct mdoc
*, int,
57 struct mdoc_argv
*, int *, char *);
59 /* Per-argument flags. */
61 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
62 ARGV_NONE
, /* MDOC_Split */
63 ARGV_NONE
, /* MDOC_Nosplit */
64 ARGV_NONE
, /* MDOC_Ragged */
65 ARGV_NONE
, /* MDOC_Unfilled */
66 ARGV_NONE
, /* MDOC_Literal */
67 ARGV_NONE
, /* MDOC_File */
68 ARGV_SINGLE
, /* MDOC_Offset */
69 ARGV_NONE
, /* MDOC_Bullet */
70 ARGV_NONE
, /* MDOC_Dash */
71 ARGV_NONE
, /* MDOC_Hyphen */
72 ARGV_NONE
, /* MDOC_Item */
73 ARGV_NONE
, /* MDOC_Enum */
74 ARGV_NONE
, /* MDOC_Tag */
75 ARGV_NONE
, /* MDOC_Diag */
76 ARGV_NONE
, /* MDOC_Hang */
77 ARGV_NONE
, /* MDOC_Ohang */
78 ARGV_NONE
, /* MDOC_Inset */
79 ARGV_MULTI
, /* MDOC_Column */
80 ARGV_SINGLE
, /* MDOC_Width */
81 ARGV_NONE
, /* MDOC_Compact */
82 ARGV_OPT_SINGLE
, /* MDOC_Std */
83 ARGV_NONE
, /* MDOC_Filled */
84 ARGV_NONE
, /* MDOC_Words */
85 ARGV_NONE
, /* MDOC_Emphasis */
86 ARGV_NONE
, /* MDOC_Symbolic */
87 ARGV_NONE
/* MDOC_Symbolic */
90 static int mdoc_argflags
[MDOC_MAX
] = {
151 ARGS_DELIM
, /* Bsx */
201 ARGS_DELIM
, /* Brq */
203 ARGS_DELIM
, /* Brc */
215 * Parse an argument from line text. This comes in the form of -key
216 * [value0...], which may either have a single mandatory value, at least
217 * one mandatory value, an optional single value, or no value.
220 mdoc_argv(struct mdoc
*m
, int line
, int tok
,
221 struct mdoc_arg
**v
, int *pos
, char *buf
)
224 struct mdoc_argv tmp
;
225 struct mdoc_arg
*arg
;
230 assert(' ' != buf
[*pos
]);
232 /* Parse through to the first unescaped space. */
240 if (' ' == buf
[*pos
])
241 if ('\\' != buf
[*pos
- 1])
246 /* XXX - save zeroed byte, if not an argument. */
254 (void)memset(&tmp
, 0, sizeof(struct mdoc_argv
));
258 /* See if our token accepts the argument. */
260 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
261 /* XXX - restore saved zeroed byte. */
267 while (buf
[*pos
] && ' ' == buf
[*pos
])
270 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
273 if (NULL
== (arg
= *v
)) {
274 *v
= calloc(1, sizeof(struct mdoc_arg
));
276 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
283 arg
->argv
= realloc(arg
->argv
, arg
->argc
*
284 sizeof(struct mdoc_argv
));
286 if (NULL
== arg
->argv
) {
287 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
291 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
292 &tmp
, sizeof(struct mdoc_argv
));
299 mdoc_argv_free(struct mdoc_arg
*p
)
314 for (i
= 0; i
< (int)p
->argc
; i
++) {
315 if (0 == p
->argv
[i
].sz
)
318 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
319 free(p
->argv
[i
].value
[j
]);
321 free(p
->argv
[i
].value
);
330 mdoc_args(struct mdoc
*m
, int line
,
331 int *pos
, char *buf
, int tok
, char **v
)
336 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
339 return(args(m
, line
, pos
, buf
, fl
, v
));
342 * The `It' macro is a special case, as it acquires parameters from its
343 * parent `Bl' context, specifically, we're concerned with -column.
346 for (n
= m
->last
; n
; n
= n
->parent
)
347 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
351 c
= (int)(n
->args
? n
->args
->argc
: 0);
355 for (i
= 0; i
< c
; i
++) {
356 if (MDOC_Column
!= n
->args
->argv
[i
].arg
)
363 return(args(m
, line
, pos
, buf
, fl
, v
));
368 args(struct mdoc
*m
, int line
, int *pos
,
369 char *buf
, int fl
, char **v
)
375 assert(' ' != buf
[*pos
]);
381 * If the first character is a delimiter and we're to look for
382 * delimited strings, then pass down the buffer seeing if it
383 * follows the pattern of [[::delim::][ ]+]+.
386 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
387 for (i
= *pos
; buf
[i
]; ) {
388 if ( ! mdoc_iscdelim(buf
[i
]))
391 if (0 == buf
[i
] || ' ' != buf
[i
])
394 while (buf
[i
] && ' ' == buf
[i
])
398 /* FIXME: warn about trailing whitespace. */
409 * First handle TABSEP items, restricted to `Bl -column'. This
410 * ignores conventional token parsing and instead uses tabs or
411 * `Ta' macros to separate phrases. Phrases are parsed again
412 * for arguments at a later phase.
415 if (ARGS_TABSEP
& fl
) {
416 /* Scan ahead to unescaped tab. */
417 p
= strchr(*v
, '\t');
419 /* Scan ahead to unescaped `Ta'. */
420 for (pp
= *v
; ; pp
++) {
421 if (NULL
== (pp
= strstr(pp
, "Ta")))
423 if (pp
> *v
&& ' ' != *(pp
- 1))
425 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
429 /* Choose delimiter tab/Ta. */
431 p
= (p
< pp
? p
: pp
);
435 /* Strip delimiter's preceding whitespace. */
436 /* FIXME: escaped whitespace? */
439 while (pp
> *v
&& ' ' == *pp
)
441 if (pp
== *v
&& ' ' == *pp
)
447 /* ...in- and proceding whitespace. */
448 if (p
&& ('\t' != *p
)) {
459 *pos
+= (int)(p
- *v
);
462 /* Some warnings, if applicable. */
464 if ( ! mdoc_pwarn(m
, line
, *pos
, ECOLEMPTY
))
466 if (p
&& 0 == *p
&& p
> *v
&& ' ' == *(p
- 1))
467 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
470 /* Non-eoln case returns now. */
474 /* Configure the eoln case, too. */
478 if (p
> *v
&& ' ' == *(p
- 1))
479 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
481 *pos
+= (int)(p
- *v
);
487 * Process a quoted literal. A quote begins with a double-quote
488 * and ends with a double-quote NOT preceded by a double-quote.
489 * Whitespace is NOT involved in literal termination.
492 if ('\"' == buf
[*pos
]) {
495 for ( ; buf
[*pos
]; (*pos
)++) {
496 if ('\"' != buf
[*pos
])
498 if ('\"' != buf
[*pos
+ 1])
503 if (0 == buf
[*pos
]) {
504 if ( ! mdoc_pwarn(m
, line
, *pos
, EQUOTTERM
))
514 while (' ' == buf
[*pos
])
518 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
525 * A non-quoted term progresses until either the end of line or
526 * a non-escaped whitespace.
529 for ( ; buf
[*pos
]; (*pos
)++)
530 if (' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
538 while (' ' == buf
[*pos
])
542 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
550 argv_a2arg(int tok
, const char *argv
)
554 * Parse an argument identifier from its text. XXX - this
555 * should really be table-driven to clarify the code.
557 * If you add an argument to the list, make sure that you
558 * register it here with its one or more macros!
563 if (0 == strcmp(argv
, "split"))
565 else if (0 == strcmp(argv
, "nosplit"))
566 return(MDOC_Nosplit
);
570 if (0 == strcmp(argv
, "ragged"))
572 else if (0 == strcmp(argv
, "unfilled"))
573 return(MDOC_Unfilled
);
574 else if (0 == strcmp(argv
, "filled"))
576 else if (0 == strcmp(argv
, "literal"))
577 return(MDOC_Literal
);
578 else if (0 == strcmp(argv
, "file"))
580 else if (0 == strcmp(argv
, "offset"))
582 else if (0 == strcmp(argv
, "compact"))
583 return(MDOC_Compact
);
587 if (0 == strcmp(argv
, "emphasis"))
588 return(MDOC_Emphasis
);
589 else if (0 == strcmp(argv
, "literal"))
590 return(MDOC_Literal
);
591 else if (0 == strcmp(argv
, "symbolic"))
592 return(MDOC_Symbolic
);
596 if (0 == strcmp(argv
, "words"))
601 if (0 == strcmp(argv
, "bullet"))
603 else if (0 == strcmp(argv
, "dash"))
605 else if (0 == strcmp(argv
, "hyphen"))
607 else if (0 == strcmp(argv
, "item"))
609 else if (0 == strcmp(argv
, "enum"))
611 else if (0 == strcmp(argv
, "tag"))
613 else if (0 == strcmp(argv
, "diag"))
615 else if (0 == strcmp(argv
, "hang"))
617 else if (0 == strcmp(argv
, "ohang"))
619 else if (0 == strcmp(argv
, "inset"))
621 else if (0 == strcmp(argv
, "column"))
623 else if (0 == strcmp(argv
, "width"))
625 else if (0 == strcmp(argv
, "offset"))
627 else if (0 == strcmp(argv
, "compact"))
628 return(MDOC_Compact
);
629 else if (0 == strcmp(argv
, "nested"))
636 if (0 == strcmp(argv
, "std"))
643 return(MDOC_ARG_MAX
);
648 argv_multi(struct mdoc
*m
, int line
,
649 struct mdoc_argv
*v
, int *pos
, char *buf
)
654 for (v
->sz
= 0; ; v
->sz
++) {
655 if ('-' == buf
[*pos
])
657 c
= args(m
, line
, pos
, buf
, 0, &p
);
660 else if (ARGS_EOLN
== c
)
663 if (0 == v
->sz
% MULTI_STEP
) {
664 v
->value
= realloc(v
->value
,
665 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
666 if (NULL
== v
->value
) {
667 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
671 if (NULL
== (v
->value
[(int)v
->sz
] = strdup(p
)))
672 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
680 argv_opt_single(struct mdoc
*m
, int line
,
681 struct mdoc_argv
*v
, int *pos
, char *buf
)
686 if ('-' == buf
[*pos
])
689 c
= args(m
, line
, pos
, buf
, 0, &p
);
696 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
697 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
698 if (NULL
== (v
->value
[0] = strdup(p
)))
699 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
706 * Parse a single, mandatory value from the stream.
709 argv_single(struct mdoc
*m
, int line
,
710 struct mdoc_argv
*v
, int *pos
, char *buf
)
717 c
= args(m
, line
, pos
, buf
, 0, &p
);
721 return(mdoc_perr(m
, line
, ppos
, EARGVAL
));
724 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
725 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
726 if (NULL
== (v
->value
[0] = strdup(p
)))
727 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
734 * Determine rules for parsing arguments. Arguments can either accept
735 * no parameters, an optional single parameter, one parameter, or
736 * multiple parameters.
739 argv(struct mdoc
*mdoc
, int line
,
740 struct mdoc_argv
*v
, int *pos
, char *buf
)
746 switch (mdoc_argvflags
[v
->arg
]) {
748 return(argv_single(mdoc
, line
, v
, pos
, buf
));
750 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
751 case (ARGV_OPT_SINGLE
):
752 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));