]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.21 2009/07/20 14:12:27 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_zargs(struct mdoc
*m
, int line
, int *pos
, char *buf
, char **v
)
333 return(args(m
, line
, pos
, buf
, 0, v
));
338 mdoc_args(struct mdoc
*m
, int line
,
339 int *pos
, char *buf
, int tok
, char **v
)
344 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
347 return(args(m
, line
, pos
, buf
, fl
, v
));
350 * The `It' macro is a special case, as it acquires parameters from its
351 * parent `Bl' context, specifically, we're concerned with -column.
354 for (n
= m
->last
; n
; n
= n
->parent
)
355 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
359 c
= (int)(n
->args
? n
->args
->argc
: 0);
363 for (i
= 0; i
< c
; i
++) {
364 if (MDOC_Column
!= n
->args
->argv
[i
].arg
)
371 return(args(m
, line
, pos
, buf
, fl
, v
));
376 args(struct mdoc
*m
, int line
, int *pos
,
377 char *buf
, int fl
, char **v
)
383 assert(' ' != buf
[*pos
]);
389 * If the first character is a delimiter and we're to look for
390 * delimited strings, then pass down the buffer seeing if it
391 * follows the pattern of [[::delim::][ ]+]+.
394 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
395 for (i
= *pos
; buf
[i
]; ) {
396 if ( ! mdoc_iscdelim(buf
[i
]))
399 if (0 == buf
[i
] || ' ' != buf
[i
])
402 while (buf
[i
] && ' ' == buf
[i
])
406 /* FIXME: warn about trailing whitespace. */
417 * First handle TABSEP items, restricted to `Bl -column'. This
418 * ignores conventional token parsing and instead uses tabs or
419 * `Ta' macros to separate phrases. Phrases are parsed again
420 * for arguments at a later phase.
423 if (ARGS_TABSEP
& fl
) {
424 /* Scan ahead to tab (can't be escaped). */
425 p
= strchr(*v
, '\t');
427 /* Scan ahead to unescaped `Ta'. */
428 for (pp
= *v
; ; pp
++) {
429 if (NULL
== (pp
= strstr(pp
, "Ta")))
431 if (pp
> *v
&& ' ' != *(pp
- 1))
433 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
438 * Adjust new-buffer position to be beyond delimiter
439 * mark (e.g., Ta -> end + 2).
442 *pos
+= pp
< p
? 2 : 1;
444 } else if (p
&& ! pp
) {
446 } else if (pp
&& ! p
) {
452 /* Whitespace check for eoln case... */
453 if (0 == *p
&& ' ' == *(p
- 1))
454 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
457 *pos
+= (int)(p
- *v
);
459 /* Strip delimiter's preceding whitespace. */
461 while (pp
> *v
&& ' ' == *pp
) {
462 if (pp
> *v
&& '\\' == *(pp
- 1))
468 /* Strip delimiter's proceeding whitespace. */
469 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
476 * Process a quoted literal. A quote begins with a double-quote
477 * and ends with a double-quote NOT preceded by a double-quote.
478 * Whitespace is NOT involved in literal termination.
481 if ('\"' == buf
[*pos
]) {
484 for ( ; buf
[*pos
]; (*pos
)++) {
485 if ('\"' != buf
[*pos
])
487 if ('\"' != buf
[*pos
+ 1])
492 if (0 == buf
[*pos
]) {
493 if ( ! mdoc_pwarn(m
, line
, *pos
, EQUOTTERM
))
503 while (' ' == buf
[*pos
])
507 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
514 * A non-quoted term progresses until either the end of line or
515 * a non-escaped whitespace.
518 for ( ; buf
[*pos
]; (*pos
)++)
519 if (' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
527 while (' ' == buf
[*pos
])
531 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
539 argv_a2arg(int tok
, const char *argv
)
543 * Parse an argument identifier from its text. XXX - this
544 * should really be table-driven to clarify the code.
546 * If you add an argument to the list, make sure that you
547 * register it here with its one or more macros!
552 if (0 == strcmp(argv
, "split"))
554 else if (0 == strcmp(argv
, "nosplit"))
555 return(MDOC_Nosplit
);
559 if (0 == strcmp(argv
, "ragged"))
561 else if (0 == strcmp(argv
, "unfilled"))
562 return(MDOC_Unfilled
);
563 else if (0 == strcmp(argv
, "filled"))
565 else if (0 == strcmp(argv
, "literal"))
566 return(MDOC_Literal
);
567 else if (0 == strcmp(argv
, "file"))
569 else if (0 == strcmp(argv
, "offset"))
571 else if (0 == strcmp(argv
, "compact"))
572 return(MDOC_Compact
);
576 if (0 == strcmp(argv
, "emphasis"))
577 return(MDOC_Emphasis
);
578 else if (0 == strcmp(argv
, "literal"))
579 return(MDOC_Literal
);
580 else if (0 == strcmp(argv
, "symbolic"))
581 return(MDOC_Symbolic
);
585 if (0 == strcmp(argv
, "words"))
590 if (0 == strcmp(argv
, "bullet"))
592 else if (0 == strcmp(argv
, "dash"))
594 else if (0 == strcmp(argv
, "hyphen"))
596 else if (0 == strcmp(argv
, "item"))
598 else if (0 == strcmp(argv
, "enum"))
600 else if (0 == strcmp(argv
, "tag"))
602 else if (0 == strcmp(argv
, "diag"))
604 else if (0 == strcmp(argv
, "hang"))
606 else if (0 == strcmp(argv
, "ohang"))
608 else if (0 == strcmp(argv
, "inset"))
610 else if (0 == strcmp(argv
, "column"))
612 else if (0 == strcmp(argv
, "width"))
614 else if (0 == strcmp(argv
, "offset"))
616 else if (0 == strcmp(argv
, "compact"))
617 return(MDOC_Compact
);
618 else if (0 == strcmp(argv
, "nested"))
625 if (0 == strcmp(argv
, "std"))
632 return(MDOC_ARG_MAX
);
637 argv_multi(struct mdoc
*m
, int line
,
638 struct mdoc_argv
*v
, int *pos
, char *buf
)
643 for (v
->sz
= 0; ; v
->sz
++) {
644 if ('-' == buf
[*pos
])
646 c
= args(m
, line
, pos
, buf
, 0, &p
);
649 else if (ARGS_EOLN
== c
)
652 if (0 == v
->sz
% MULTI_STEP
) {
653 v
->value
= realloc(v
->value
,
654 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
655 if (NULL
== v
->value
) {
656 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
660 if (NULL
== (v
->value
[(int)v
->sz
] = strdup(p
)))
661 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
669 argv_opt_single(struct mdoc
*m
, int line
,
670 struct mdoc_argv
*v
, int *pos
, char *buf
)
675 if ('-' == buf
[*pos
])
678 c
= args(m
, line
, pos
, buf
, 0, &p
);
685 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
686 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
687 if (NULL
== (v
->value
[0] = strdup(p
)))
688 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
695 * Parse a single, mandatory value from the stream.
698 argv_single(struct mdoc
*m
, int line
,
699 struct mdoc_argv
*v
, int *pos
, char *buf
)
706 c
= args(m
, line
, pos
, buf
, 0, &p
);
710 return(mdoc_perr(m
, line
, ppos
, EARGVAL
));
713 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
714 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
715 if (NULL
== (v
->value
[0] = strdup(p
)))
716 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
723 * Determine rules for parsing arguments. Arguments can either accept
724 * no parameters, an optional single parameter, one parameter, or
725 * multiple parameters.
728 argv(struct mdoc
*mdoc
, int line
,
729 struct mdoc_argv
*v
, int *pos
, char *buf
)
735 switch (mdoc_argvflags
[v
->arg
]) {
737 return(argv_single(mdoc
, line
, v
, pos
, buf
));
739 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
740 case (ARGV_OPT_SINGLE
):
741 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));