]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.71 2011/03/22 14:28:48 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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"
33 #define MULTI_STEP 5 /* pre-allocate argument values */
35 static enum mdocargt
argv_a2arg(enum mdoct
, const char *);
36 static enum margserr
args(struct mdoc
*, int, int *,
37 char *, int, char **);
38 static int args_checkpunct(struct mdoc
*,
39 const char *, int, int, int);
40 static int argv(struct mdoc
*, int,
41 struct mdoc_argv
*, int *, char *);
42 static int argv_single(struct mdoc
*, int,
43 struct mdoc_argv
*, int *, char *);
44 static int argv_opt_single(struct mdoc
*, int,
45 struct mdoc_argv
*, int *, char *);
46 static int argv_multi(struct mdoc
*, int,
47 struct mdoc_argv
*, int *, char *);
48 static void argn_free(struct mdoc_arg
*, int);
51 ARGV_NONE
, /* no args to flag (e.g., -split) */
52 ARGV_SINGLE
, /* one arg to flag (e.g., -file xxx) */
53 ARGV_MULTI
, /* multiple args (e.g., -column xxx yyy) */
54 ARGV_OPT_SINGLE
/* optional arg (e.g., -offset [xxx]) */
57 static const enum argvflag argvflags
[MDOC_ARG_MAX
] = {
58 ARGV_NONE
, /* MDOC_Split */
59 ARGV_NONE
, /* MDOC_Nosplit */
60 ARGV_NONE
, /* MDOC_Ragged */
61 ARGV_NONE
, /* MDOC_Unfilled */
62 ARGV_NONE
, /* MDOC_Literal */
63 ARGV_SINGLE
, /* MDOC_File */
64 ARGV_OPT_SINGLE
, /* MDOC_Offset */
65 ARGV_NONE
, /* MDOC_Bullet */
66 ARGV_NONE
, /* MDOC_Dash */
67 ARGV_NONE
, /* MDOC_Hyphen */
68 ARGV_NONE
, /* MDOC_Item */
69 ARGV_NONE
, /* MDOC_Enum */
70 ARGV_NONE
, /* MDOC_Tag */
71 ARGV_NONE
, /* MDOC_Diag */
72 ARGV_NONE
, /* MDOC_Hang */
73 ARGV_NONE
, /* MDOC_Ohang */
74 ARGV_NONE
, /* MDOC_Inset */
75 ARGV_MULTI
, /* MDOC_Column */
76 ARGV_SINGLE
, /* MDOC_Width */
77 ARGV_NONE
, /* MDOC_Compact */
78 ARGV_NONE
, /* MDOC_Std */
79 ARGV_NONE
, /* MDOC_Filled */
80 ARGV_NONE
, /* MDOC_Words */
81 ARGV_NONE
, /* MDOC_Emphasis */
82 ARGV_NONE
, /* MDOC_Symbolic */
83 ARGV_NONE
/* MDOC_Symbolic */
86 static const int argflags
[MDOC_MAX
] = {
147 ARGS_DELIM
, /* Bsx */
197 ARGS_DELIM
, /* Brq */
199 ARGS_DELIM
, /* Brc */
212 * Parse an argument from line text. This comes in the form of -key
213 * [value0...], which may either have a single mandatory value, at least
214 * one mandatory value, an optional single value, or no value.
217 mdoc_argv(struct mdoc
*m
, int line
, enum mdoct tok
,
218 struct mdoc_arg
**v
, int *pos
, char *buf
)
221 struct mdoc_argv tmp
;
222 struct mdoc_arg
*arg
;
224 if ('\0' == buf
[*pos
])
227 assert(' ' != buf
[*pos
]);
229 /* Parse through to the first unescaped space. */
237 if (' ' == buf
[*pos
])
238 if ('\\' != buf
[*pos
- 1])
243 /* XXX - save zeroed byte, if not an argument. */
248 buf
[(*pos
)++] = '\0';
251 memset(&tmp
, 0, sizeof(struct mdoc_argv
));
255 /* See if our token accepts the argument. */
257 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
258 /* XXX - restore saved zeroed byte. */
264 while (buf
[*pos
] && ' ' == buf
[*pos
])
267 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
270 if (NULL
== (arg
= *v
))
271 arg
= *v
= mandoc_calloc(1, sizeof(struct mdoc_arg
));
274 arg
->argv
= mandoc_realloc
275 (arg
->argv
, arg
->argc
* sizeof(struct mdoc_argv
));
277 memcpy(&arg
->argv
[(int)arg
->argc
- 1],
278 &tmp
, sizeof(struct mdoc_argv
));
284 mdoc_argv_free(struct mdoc_arg
*p
)
298 for (i
= (int)p
->argc
- 1; i
>= 0; i
--)
306 argn_free(struct mdoc_arg
*p
, int iarg
)
308 struct mdoc_argv
*arg
;
311 arg
= &p
->argv
[iarg
];
313 if (arg
->sz
&& arg
->value
) {
314 for (j
= (int)arg
->sz
- 1; j
>= 0; j
--)
319 for (--p
->argc
; iarg
< (int)p
->argc
; iarg
++)
320 p
->argv
[iarg
] = p
->argv
[iarg
+1];
324 mdoc_zargs(struct mdoc
*m
, int line
, int *pos
,
325 char *buf
, int flags
, char **v
)
328 return(args(m
, line
, pos
, buf
, flags
, v
));
332 mdoc_args(struct mdoc
*m
, int line
, int *pos
,
333 char *buf
, enum mdoct tok
, char **v
)
341 return(args(m
, line
, pos
, buf
, fl
, v
));
344 * We know that we're in an `It', so it's reasonable to expect
345 * us to be sitting in a `Bl'. Someday this may not be the case
346 * (if we allow random `It's sitting out there), so provide a
347 * safe fall-back into the default behaviour.
350 for (n
= m
->last
; n
; n
= n
->parent
)
351 if (MDOC_Bl
== n
->tok
)
354 if (n
&& LIST_column
== n
->norm
->Bl
.type
) {
359 return(args(m
, line
, pos
, buf
, fl
, v
));
363 args(struct mdoc
*m
, int line
, int *pos
,
364 char *buf
, int fl
, char **v
)
370 * Parse out the terms (like `val' in `.Xx -arg val' or simply
371 * `.Xx val'), which can have all sorts of properties:
373 * ARGS_DELIM: use special handling if encountering trailing
374 * delimiters in the form of [[::delim::][ ]+]+.
376 * ARGS_NOWARN: don't post warnings. This is only used when
377 * re-parsing delimiters, as the warnings have already been
380 * ARGS_TABSEP: use special handling for tab/`Ta' separated
381 * phrases like in `Bl -column'.
384 assert(' ' != buf
[*pos
]);
386 if ('\0' == buf
[*pos
]) {
387 if (MDOC_PPHRASE
& m
->flags
)
390 * If we're not in a partial phrase and the flag for
391 * being a phrase literal is still set, the punctuation
394 if (MDOC_PHRASELIT
& m
->flags
)
395 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
);
397 m
->flags
&= ~MDOC_PHRASELIT
;
403 if (ARGS_DELIM
& fl
&& args_checkpunct(m
, buf
, *pos
, line
, fl
))
407 * First handle TABSEP items, restricted to `Bl -column'. This
408 * ignores conventional token parsing and instead uses tabs or
409 * `Ta' macros to separate phrases. Phrases are parsed again
410 * for arguments at a later phase.
413 if (ARGS_TABSEP
& fl
) {
414 /* Scan ahead to tab (can't be escaped). */
415 p
= strchr(*v
, '\t');
418 /* Scan ahead to unescaped `Ta'. */
419 if ( ! (MDOC_PHRASELIT
& m
->flags
))
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 /* By default, assume a phrase. */
433 * Adjust new-buffer position to be beyond delimiter
434 * mark (e.g., Ta -> end + 2).
437 *pos
+= pp
< p
? 2 : 1;
438 rc
= pp
< p
? ARGS_PHRASE
: ARGS_PPHRASE
;
440 } else if (p
&& ! pp
) {
443 } else if (pp
&& ! p
) {
451 /* Whitespace check for eoln case... */
452 if ('\0' == *p
&& ' ' == *(p
- 1) && ! (ARGS_NOWARN
& fl
))
453 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
455 *pos
+= (int)(p
- *v
);
457 /* Strip delimiter's preceding whitespace. */
459 while (pp
> *v
&& ' ' == *pp
) {
460 if (pp
> *v
&& '\\' == *(pp
- 1))
466 /* Strip delimiter's proceeding whitespace. */
467 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
474 * Process a quoted literal. A quote begins with a double-quote
475 * and ends with a double-quote NOT preceded by a double-quote.
476 * Whitespace is NOT involved in literal termination.
479 if (MDOC_PHRASELIT
& m
->flags
|| '\"' == buf
[*pos
]) {
480 if ( ! (MDOC_PHRASELIT
& m
->flags
))
483 if (MDOC_PPHRASE
& m
->flags
)
484 m
->flags
|= MDOC_PHRASELIT
;
486 for ( ; buf
[*pos
]; (*pos
)++) {
487 if ('\"' != buf
[*pos
])
489 if ('\"' != buf
[*pos
+ 1])
494 if ('\0' == buf
[*pos
]) {
495 if (ARGS_NOWARN
& fl
|| MDOC_PPHRASE
& m
->flags
)
497 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
);
501 m
->flags
&= ~MDOC_PHRASELIT
;
502 buf
[(*pos
)++] = '\0';
504 if ('\0' == buf
[*pos
])
507 while (' ' == buf
[*pos
])
510 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
511 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
517 * A non-quoted term progresses until either the end of line or
518 * a non-escaped whitespace.
521 for ( ; buf
[*pos
]; (*pos
)++)
522 if (*pos
&& ' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
525 if ('\0' == buf
[*pos
])
528 buf
[(*pos
)++] = '\0';
530 while (' ' == buf
[*pos
])
533 if ('\0' == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
534 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
540 * Check if the string consists only of space-separated closing
541 * delimiters. This is a bit of a dance: the first must be a close
542 * delimiter, but it may be followed by middle delimiters. Arbitrary
543 * whitespace may separate these tokens.
546 args_checkpunct(struct mdoc
*m
, const char *buf
, int i
, int ln
, int fl
)
552 /* First token must be a close-delimiter. */
554 for (j
= 0; buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
; j
++, i
++)
561 if (DELIM_CLOSE
!= mdoc_isdelim(dbuf
))
564 while (' ' == buf
[i
])
567 /* Remaining must NOT be open/none. */
571 while (buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
)
572 dbuf
[j
++] = buf
[i
++];
578 d
= mdoc_isdelim(dbuf
);
579 if (DELIM_NONE
== d
|| DELIM_OPEN
== d
)
582 while (' ' == buf
[i
])
586 if ( ! (ARGS_NOWARN
& fl
) && i
&& ' ' == buf
[i
- 1])
587 mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
);
589 return('\0' == buf
[i
]);
593 * Match up an argument string (e.g., `-foo bar' having "foo") with the
594 * correrct identifier. It must apply to the given macro. If none was
595 * found (including bad matches), return MDOC_ARG_MAX.
598 argv_a2arg(enum mdoct tok
, const char *p
)
600 enum mdocargt args
[MDOC_ARG_MAX
];
607 args
[len
++] = MDOC_Split
;
608 args
[len
++] = MDOC_Nosplit
;
611 args
[len
++] = MDOC_Ragged
;
612 args
[len
++] = MDOC_Unfilled
;
613 args
[len
++] = MDOC_Filled
;
614 args
[len
++] = MDOC_Literal
;
615 args
[len
++] = MDOC_File
;
616 args
[len
++] = MDOC_Offset
;
617 args
[len
++] = MDOC_Compact
;
618 args
[len
++] = MDOC_Centred
;
621 args
[len
++] = MDOC_Emphasis
;
622 args
[len
++] = MDOC_Literal
;
623 args
[len
++] = MDOC_Symbolic
;
626 args
[len
++] = MDOC_Words
;
629 args
[len
++] = MDOC_Bullet
;
630 args
[len
++] = MDOC_Dash
;
631 args
[len
++] = MDOC_Hyphen
;
632 args
[len
++] = MDOC_Item
;
633 args
[len
++] = MDOC_Enum
;
634 args
[len
++] = MDOC_Tag
;
635 args
[len
++] = MDOC_Diag
;
636 args
[len
++] = MDOC_Hang
;
637 args
[len
++] = MDOC_Ohang
;
638 args
[len
++] = MDOC_Inset
;
639 args
[len
++] = MDOC_Column
;
640 args
[len
++] = MDOC_Width
;
641 args
[len
++] = MDOC_Offset
;
642 args
[len
++] = MDOC_Compact
;
643 args
[len
++] = MDOC_Nested
;
648 args
[len
++] = MDOC_Std
;
654 for (i
= 0; i
< len
; i
++)
655 if (0 == strcmp(p
, mdoc_argnames
[args
[i
]]))
658 return(MDOC_ARG_MAX
);
662 argv_multi(struct mdoc
*m
, int line
,
663 struct mdoc_argv
*v
, int *pos
, char *buf
)
668 for (v
->sz
= 0; ; v
->sz
++) {
669 if ('-' == buf
[*pos
])
671 ac
= args(m
, line
, pos
, buf
, 0, &p
);
672 if (ARGS_ERROR
== ac
)
674 else if (ARGS_EOLN
== ac
)
677 if (0 == v
->sz
% MULTI_STEP
)
678 v
->value
= mandoc_realloc(v
->value
,
679 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
681 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
688 argv_opt_single(struct mdoc
*m
, int line
,
689 struct mdoc_argv
*v
, int *pos
, char *buf
)
694 if ('-' == buf
[*pos
])
697 ac
= args(m
, line
, pos
, buf
, 0, &p
);
698 if (ARGS_ERROR
== ac
)
704 v
->value
= mandoc_malloc(sizeof(char *));
705 v
->value
[0] = mandoc_strdup(p
);
711 * Parse a single, mandatory value from the stream.
714 argv_single(struct mdoc
*m
, int line
,
715 struct mdoc_argv
*v
, int *pos
, char *buf
)
723 ac
= args(m
, line
, pos
, buf
, 0, &p
);
724 if (ARGS_EOLN
== ac
) {
725 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_SYNTARGVCOUNT
);
727 } else if (ARGS_ERROR
== ac
)
731 v
->value
= mandoc_malloc(sizeof(char *));
732 v
->value
[0] = mandoc_strdup(p
);
738 * Determine rules for parsing arguments. Arguments can either accept
739 * no parameters, an optional single parameter, one parameter, or
740 * multiple parameters.
743 argv(struct mdoc
*mdoc
, int line
,
744 struct mdoc_argv
*v
, int *pos
, char *buf
)
750 switch (argvflags
[v
->arg
]) {
752 return(argv_single(mdoc
, line
, v
, pos
, buf
));
754 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
755 case (ARGV_OPT_SINGLE
):
756 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));