]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.49 2010/05/17 22:11:42 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.
21 #include <sys/types.h>
31 #include "libmandoc.h"
34 * Routines to parse arguments of macros. Arguments follow the syntax
35 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
36 * arguments, multiple arguments per value, no-value arguments, etc.
38 * There's no limit to the number or arguments that may be allocated.
41 #define ARGV_NONE (1 << 0)
42 #define ARGV_SINGLE (1 << 1)
43 #define ARGV_MULTI (1 << 2)
44 #define ARGV_OPT_SINGLE (1 << 3)
48 static int argv_a2arg(enum mdoct
, const char *);
49 static enum margserr
args(struct mdoc
*, int, int *,
50 char *, int, char **);
51 static int argv(struct mdoc
*, int,
52 struct mdoc_argv
*, int *, char *);
53 static int argv_single(struct mdoc
*, int,
54 struct mdoc_argv
*, int *, char *);
55 static int argv_opt_single(struct mdoc
*, int,
56 struct mdoc_argv
*, int *, char *);
57 static int argv_multi(struct mdoc
*, int,
58 struct mdoc_argv
*, int *, char *);
60 /* Per-argument flags. */
62 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
63 ARGV_NONE
, /* MDOC_Split */
64 ARGV_NONE
, /* MDOC_Nosplit */
65 ARGV_NONE
, /* MDOC_Ragged */
66 ARGV_NONE
, /* MDOC_Unfilled */
67 ARGV_NONE
, /* MDOC_Literal */
68 ARGV_SINGLE
, /* MDOC_File */
69 ARGV_OPT_SINGLE
, /* MDOC_Offset */
70 ARGV_NONE
, /* MDOC_Bullet */
71 ARGV_NONE
, /* MDOC_Dash */
72 ARGV_NONE
, /* MDOC_Hyphen */
73 ARGV_NONE
, /* MDOC_Item */
74 ARGV_NONE
, /* MDOC_Enum */
75 ARGV_NONE
, /* MDOC_Tag */
76 ARGV_NONE
, /* MDOC_Diag */
77 ARGV_NONE
, /* MDOC_Hang */
78 ARGV_NONE
, /* MDOC_Ohang */
79 ARGV_NONE
, /* MDOC_Inset */
80 ARGV_MULTI
, /* MDOC_Column */
81 ARGV_SINGLE
, /* MDOC_Width */
82 ARGV_NONE
, /* MDOC_Compact */
83 ARGV_NONE
, /* MDOC_Std */
84 ARGV_NONE
, /* MDOC_Filled */
85 ARGV_NONE
, /* MDOC_Words */
86 ARGV_NONE
, /* MDOC_Emphasis */
87 ARGV_NONE
, /* MDOC_Symbolic */
88 ARGV_NONE
/* MDOC_Symbolic */
91 static int mdoc_argflags
[MDOC_MAX
] = {
152 ARGS_DELIM
, /* Bsx */
202 ARGS_DELIM
, /* Brq */
204 ARGS_DELIM
, /* Brc */
217 * Parse an argument from line text. This comes in the form of -key
218 * [value0...], which may either have a single mandatory value, at least
219 * one mandatory value, an optional single value, or no value.
222 mdoc_argv(struct mdoc
*m
, int line
, enum mdoct tok
,
223 struct mdoc_arg
**v
, int *pos
, char *buf
)
226 struct mdoc_argv tmp
;
227 struct mdoc_arg
*arg
;
232 assert(' ' != buf
[*pos
]);
234 /* Parse through to the first unescaped space. */
242 if (' ' == buf
[*pos
])
243 if ('\\' != buf
[*pos
- 1])
248 /* XXX - save zeroed byte, if not an argument. */
256 (void)memset(&tmp
, 0, sizeof(struct mdoc_argv
));
260 /* See if our token accepts the argument. */
262 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
263 /* XXX - restore saved zeroed byte. */
269 while (buf
[*pos
] && ' ' == buf
[*pos
])
272 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
275 if (NULL
== (arg
= *v
))
276 arg
= *v
= mandoc_calloc(1, sizeof(struct mdoc_arg
));
279 arg
->argv
= mandoc_realloc
280 (arg
->argv
, arg
->argc
* sizeof(struct mdoc_argv
));
282 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
283 &tmp
, sizeof(struct mdoc_argv
));
290 mdoc_argv_free(struct mdoc_arg
*p
)
304 for (i
= (int)p
->argc
- 1; i
>= 0; i
--)
305 mdoc_argn_free(p
, i
);
313 mdoc_argn_free(struct mdoc_arg
*p
, int iarg
)
315 struct mdoc_argv
*arg
= &p
->argv
[iarg
];
318 if (arg
->sz
&& arg
->value
) {
319 for (j
= (int)arg
->sz
- 1; j
>= 0; j
--)
324 for (--p
->argc
; iarg
< (int)p
->argc
; iarg
++)
325 p
->argv
[iarg
] = p
->argv
[iarg
+1];
330 mdoc_zargs(struct mdoc
*m
, int line
, int *pos
,
331 char *buf
, int flags
, char **v
)
334 return(args(m
, line
, pos
, buf
, flags
, v
));
339 mdoc_args(struct mdoc
*m
, int line
, int *pos
,
340 char *buf
, enum mdoct tok
, char **v
)
345 fl
= mdoc_argflags
[tok
];
348 return(args(m
, line
, pos
, buf
, fl
, v
));
351 * The `It' macro is a special case, as it acquires parameters from its
352 * parent `Bl' context, specifically, we're concerned with -column.
355 for (n
= m
->last
; n
; n
= n
->parent
)
356 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
360 c
= (int)(n
->args
? n
->args
->argc
: 0);
364 for (i
= 0; i
< c
; i
++) {
365 if (MDOC_Column
!= n
->args
->argv
[i
].arg
)
372 return(args(m
, line
, pos
, buf
, fl
, v
));
377 args(struct mdoc
*m
, int line
, int *pos
,
378 char *buf
, int fl
, char **v
)
385 * Parse out the terms (like `val' in `.Xx -arg val' or simply
386 * `.Xx val'), which can have all sorts of properties:
388 * ARGS_DELIM: use special handling if encountering trailing
389 * delimiters in the form of [[::delim::][ ]+]+.
391 * ARGS_NOWARN: don't post warnings. This is only used when
392 * re-parsing delimiters, as the warnings have already been
395 * ARGS_TABSEP: use special handling for tab/`Ta' separated
396 * phrases like in `Bl -column'.
400 assert(' ' != buf
[*pos
]);
402 if ('\0' == buf
[*pos
]) {
403 if (MDOC_PPHRASE
& m
->flags
)
406 * If we're not in a partial phrase and the flag for
407 * being a phrase literal is still set, the punctuation
410 if (MDOC_PHRASELIT
& m
->flags
)
411 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
))
414 m
->flags
&= ~MDOC_PHRASELIT
;
419 * If the first character is a closing delimiter and we're to
420 * look for delimited strings, then pass down the buffer seeing
421 * if it follows the pattern of [[::delim::][ ]+]+. Note that
422 * we ONLY care about closing delimiters.
425 if ((fl
& ARGS_DELIM
) && DELIM_CLOSE
== mdoc_iscdelim(buf
[*pos
])) {
426 for (i
= *pos
; buf
[i
]; ) {
427 enum mdelim d
= mdoc_iscdelim(buf
[i
]);
428 if (DELIM_NONE
== d
|| DELIM_OPEN
== d
)
431 if ('\0' == buf
[i
] || ' ' != buf
[i
])
434 while (buf
[i
] && ' ' == buf
[i
])
438 if ('\0' == buf
[i
]) {
440 if (' ' != buf
[i
- 1])
442 if (ARGS_NOWARN
& fl
)
444 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
))
453 * First handle TABSEP items, restricted to `Bl -column'. This
454 * ignores conventional token parsing and instead uses tabs or
455 * `Ta' macros to separate phrases. Phrases are parsed again
456 * for arguments at a later phase.
459 if (ARGS_TABSEP
& fl
) {
460 /* Scan ahead to tab (can't be escaped). */
461 p
= strchr(*v
, '\t');
464 /* Scan ahead to unescaped `Ta'. */
465 if ( ! (MDOC_PHRASELIT
& m
->flags
))
466 for (pp
= *v
; ; pp
++) {
467 if (NULL
== (pp
= strstr(pp
, "Ta")))
469 if (pp
> *v
&& ' ' != *(pp
- 1))
471 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
475 /* By default, assume a phrase. */
479 * Adjust new-buffer position to be beyond delimiter
480 * mark (e.g., Ta -> end + 2).
483 *pos
+= pp
< p
? 2 : 1;
484 rc
= pp
< p
? ARGS_PHRASE
: ARGS_PPHRASE
;
486 } else if (p
&& ! pp
) {
489 } else if (pp
&& ! p
) {
497 /* Whitespace check for eoln case... */
498 if (0 == *p
&& ' ' == *(p
- 1) && ! (ARGS_NOWARN
& fl
))
499 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
))
502 *pos
+= (int)(p
- *v
);
504 /* Strip delimiter's preceding whitespace. */
506 while (pp
> *v
&& ' ' == *pp
) {
507 if (pp
> *v
&& '\\' == *(pp
- 1))
513 /* Strip delimiter's proceeding whitespace. */
514 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
521 * Process a quoted literal. A quote begins with a double-quote
522 * and ends with a double-quote NOT preceded by a double-quote.
523 * Whitespace is NOT involved in literal termination.
526 if (MDOC_PHRASELIT
& m
->flags
|| '\"' == buf
[*pos
]) {
527 if ( ! (MDOC_PHRASELIT
& m
->flags
))
530 if (MDOC_PPHRASE
& m
->flags
)
531 m
->flags
|= MDOC_PHRASELIT
;
533 for ( ; buf
[*pos
]; (*pos
)++) {
534 if ('\"' != buf
[*pos
])
536 if ('\"' != buf
[*pos
+ 1])
541 if ('\0' == buf
[*pos
]) {
542 if (ARGS_NOWARN
& fl
|| MDOC_PPHRASE
& m
->flags
)
544 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
))
549 m
->flags
&= ~MDOC_PHRASELIT
;
550 buf
[(*pos
)++] = '\0';
552 if ('\0' == buf
[*pos
])
555 while (' ' == buf
[*pos
])
558 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
559 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
))
566 * A non-quoted term progresses until either the end of line or
567 * a non-escaped whitespace.
570 for ( ; buf
[*pos
]; (*pos
)++)
571 if (' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
574 if ('\0' == buf
[*pos
])
577 buf
[(*pos
)++] = '\0';
579 while (' ' == buf
[*pos
])
582 if ('\0' == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
583 if ( ! mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
))
591 argv_a2arg(enum mdoct tok
, const char *p
)
595 * Parse an argument identifier from its text. XXX - this
596 * should really be table-driven to clarify the code.
598 * If you add an argument to the list, make sure that you
599 * register it here with its one or more macros!
604 if (0 == strcmp(p
, "split"))
606 else if (0 == strcmp(p
, "nosplit"))
607 return(MDOC_Nosplit
);
611 if (0 == strcmp(p
, "ragged"))
613 else if (0 == strcmp(p
, "unfilled"))
614 return(MDOC_Unfilled
);
615 else if (0 == strcmp(p
, "filled"))
617 else if (0 == strcmp(p
, "literal"))
618 return(MDOC_Literal
);
619 else if (0 == strcmp(p
, "file"))
621 else if (0 == strcmp(p
, "offset"))
623 else if (0 == strcmp(p
, "compact"))
624 return(MDOC_Compact
);
625 else if (0 == strcmp(p
, "centered"))
626 return(MDOC_Centred
);
630 if (0 == strcmp(p
, "emphasis"))
631 return(MDOC_Emphasis
);
632 else if (0 == strcmp(p
, "literal"))
633 return(MDOC_Literal
);
634 else if (0 == strcmp(p
, "symbolic"))
635 return(MDOC_Symbolic
);
639 if (0 == strcmp(p
, "words"))
644 if (0 == strcmp(p
, "bullet"))
646 else if (0 == strcmp(p
, "dash"))
648 else if (0 == strcmp(p
, "hyphen"))
650 else if (0 == strcmp(p
, "item"))
652 else if (0 == strcmp(p
, "enum"))
654 else if (0 == strcmp(p
, "tag"))
656 else if (0 == strcmp(p
, "diag"))
658 else if (0 == strcmp(p
, "hang"))
660 else if (0 == strcmp(p
, "ohang"))
662 else if (0 == strcmp(p
, "inset"))
664 else if (0 == strcmp(p
, "column"))
666 else if (0 == strcmp(p
, "width"))
668 else if (0 == strcmp(p
, "offset"))
670 else if (0 == strcmp(p
, "compact"))
671 return(MDOC_Compact
);
672 else if (0 == strcmp(p
, "nested"))
679 if (0 == strcmp(p
, "std"))
686 return(MDOC_ARG_MAX
);
691 argv_multi(struct mdoc
*m
, int line
,
692 struct mdoc_argv
*v
, int *pos
, char *buf
)
697 for (v
->sz
= 0; ; v
->sz
++) {
698 if ('-' == buf
[*pos
])
700 ac
= args(m
, line
, pos
, buf
, 0, &p
);
701 if (ARGS_ERROR
== ac
)
703 else if (ARGS_EOLN
== ac
)
706 if (0 == v
->sz
% MULTI_STEP
)
707 v
->value
= mandoc_realloc(v
->value
,
708 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
710 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
718 argv_opt_single(struct mdoc
*m
, int line
,
719 struct mdoc_argv
*v
, int *pos
, char *buf
)
724 if ('-' == buf
[*pos
])
727 ac
= args(m
, line
, pos
, buf
, 0, &p
);
728 if (ARGS_ERROR
== ac
)
734 v
->value
= mandoc_malloc(sizeof(char *));
735 v
->value
[0] = mandoc_strdup(p
);
742 * Parse a single, mandatory value from the stream.
745 argv_single(struct mdoc
*m
, int line
,
746 struct mdoc_argv
*v
, int *pos
, char *buf
)
754 ac
= args(m
, line
, pos
, buf
, 0, &p
);
755 if (ARGS_EOLN
== ac
) {
756 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_SYNTARGVCOUNT
);
758 } else if (ARGS_ERROR
== ac
)
762 v
->value
= mandoc_malloc(sizeof(char *));
763 v
->value
[0] = mandoc_strdup(p
);
770 * Determine rules for parsing arguments. Arguments can either accept
771 * no parameters, an optional single parameter, one parameter, or
772 * multiple parameters.
775 argv(struct mdoc
*mdoc
, int line
,
776 struct mdoc_argv
*v
, int *pos
, char *buf
)
782 switch (mdoc_argvflags
[v
->arg
]) {
784 return(argv_single(mdoc
, line
, v
, pos
, buf
));
786 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
787 case (ARGV_OPT_SINGLE
):
788 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));