]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.72 2011/03/22 14:33:05 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>
32 #include "libmandoc.h"
34 #define MULTI_STEP 5 /* pre-allocate argument values */
36 static enum mdocargt
argv_a2arg(enum mdoct
, const char *);
37 static enum margserr
args(struct mdoc
*, int, int *,
38 char *, int, char **);
39 static int args_checkpunct(struct mdoc
*,
40 const char *, int, int, int);
41 static int argv(struct mdoc
*, int,
42 struct mdoc_argv
*, int *, char *);
43 static int argv_single(struct mdoc
*, int,
44 struct mdoc_argv
*, int *, char *);
45 static int argv_opt_single(struct mdoc
*, int,
46 struct mdoc_argv
*, int *, char *);
47 static int argv_multi(struct mdoc
*, int,
48 struct mdoc_argv
*, int *, char *);
49 static void argn_free(struct mdoc_arg
*, int);
52 ARGV_NONE
, /* no args to flag (e.g., -split) */
53 ARGV_SINGLE
, /* one arg to flag (e.g., -file xxx) */
54 ARGV_MULTI
, /* multiple args (e.g., -column xxx yyy) */
55 ARGV_OPT_SINGLE
/* optional arg (e.g., -offset [xxx]) */
58 static const enum argvflag argvflags
[MDOC_ARG_MAX
] = {
59 ARGV_NONE
, /* MDOC_Split */
60 ARGV_NONE
, /* MDOC_Nosplit */
61 ARGV_NONE
, /* MDOC_Ragged */
62 ARGV_NONE
, /* MDOC_Unfilled */
63 ARGV_NONE
, /* MDOC_Literal */
64 ARGV_SINGLE
, /* MDOC_File */
65 ARGV_OPT_SINGLE
, /* MDOC_Offset */
66 ARGV_NONE
, /* MDOC_Bullet */
67 ARGV_NONE
, /* MDOC_Dash */
68 ARGV_NONE
, /* MDOC_Hyphen */
69 ARGV_NONE
, /* MDOC_Item */
70 ARGV_NONE
, /* MDOC_Enum */
71 ARGV_NONE
, /* MDOC_Tag */
72 ARGV_NONE
, /* MDOC_Diag */
73 ARGV_NONE
, /* MDOC_Hang */
74 ARGV_NONE
, /* MDOC_Ohang */
75 ARGV_NONE
, /* MDOC_Inset */
76 ARGV_MULTI
, /* MDOC_Column */
77 ARGV_SINGLE
, /* MDOC_Width */
78 ARGV_NONE
, /* MDOC_Compact */
79 ARGV_NONE
, /* MDOC_Std */
80 ARGV_NONE
, /* MDOC_Filled */
81 ARGV_NONE
, /* MDOC_Words */
82 ARGV_NONE
, /* MDOC_Emphasis */
83 ARGV_NONE
, /* MDOC_Symbolic */
84 ARGV_NONE
/* MDOC_Symbolic */
87 static const int argflags
[MDOC_MAX
] = {
148 ARGS_DELIM
, /* Bsx */
198 ARGS_DELIM
, /* Brq */
200 ARGS_DELIM
, /* Brc */
213 * Parse an argument from line text. This comes in the form of -key
214 * [value0...], which may either have a single mandatory value, at least
215 * one mandatory value, an optional single value, or no value.
218 mdoc_argv(struct mdoc
*m
, int line
, enum mdoct tok
,
219 struct mdoc_arg
**v
, int *pos
, char *buf
)
222 struct mdoc_argv tmp
;
223 struct mdoc_arg
*arg
;
225 if ('\0' == buf
[*pos
])
228 assert(' ' != buf
[*pos
]);
230 /* Parse through to the first unescaped space. */
238 if (' ' == buf
[*pos
])
239 if ('\\' != buf
[*pos
- 1])
244 /* XXX - save zeroed byte, if not an argument. */
249 buf
[(*pos
)++] = '\0';
252 memset(&tmp
, 0, sizeof(struct mdoc_argv
));
256 /* See if our token accepts the argument. */
258 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
259 /* XXX - restore saved zeroed byte. */
265 while (buf
[*pos
] && ' ' == buf
[*pos
])
268 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
271 if (NULL
== (arg
= *v
))
272 arg
= *v
= mandoc_calloc(1, sizeof(struct mdoc_arg
));
275 arg
->argv
= mandoc_realloc
276 (arg
->argv
, arg
->argc
* sizeof(struct mdoc_argv
));
278 memcpy(&arg
->argv
[(int)arg
->argc
- 1],
279 &tmp
, sizeof(struct mdoc_argv
));
285 mdoc_argv_free(struct mdoc_arg
*p
)
299 for (i
= (int)p
->argc
- 1; i
>= 0; i
--)
307 argn_free(struct mdoc_arg
*p
, int iarg
)
309 struct mdoc_argv
*arg
;
312 arg
= &p
->argv
[iarg
];
314 if (arg
->sz
&& arg
->value
) {
315 for (j
= (int)arg
->sz
- 1; j
>= 0; j
--)
320 for (--p
->argc
; iarg
< (int)p
->argc
; iarg
++)
321 p
->argv
[iarg
] = p
->argv
[iarg
+1];
325 mdoc_zargs(struct mdoc
*m
, int line
, int *pos
,
326 char *buf
, int flags
, char **v
)
329 return(args(m
, line
, pos
, buf
, flags
, v
));
333 mdoc_args(struct mdoc
*m
, int line
, int *pos
,
334 char *buf
, enum mdoct tok
, char **v
)
342 return(args(m
, line
, pos
, buf
, fl
, v
));
345 * We know that we're in an `It', so it's reasonable to expect
346 * us to be sitting in a `Bl'. Someday this may not be the case
347 * (if we allow random `It's sitting out there), so provide a
348 * safe fall-back into the default behaviour.
351 for (n
= m
->last
; n
; n
= n
->parent
)
352 if (MDOC_Bl
== n
->tok
)
355 if (n
&& LIST_column
== n
->norm
->Bl
.type
) {
360 return(args(m
, line
, pos
, buf
, fl
, v
));
364 args(struct mdoc
*m
, int line
, int *pos
,
365 char *buf
, int fl
, char **v
)
371 * Parse out the terms (like `val' in `.Xx -arg val' or simply
372 * `.Xx val'), which can have all sorts of properties:
374 * ARGS_DELIM: use special handling if encountering trailing
375 * delimiters in the form of [[::delim::][ ]+]+.
377 * ARGS_NOWARN: don't post warnings. This is only used when
378 * re-parsing delimiters, as the warnings have already been
381 * ARGS_TABSEP: use special handling for tab/`Ta' separated
382 * phrases like in `Bl -column'.
385 assert(' ' != buf
[*pos
]);
387 if ('\0' == buf
[*pos
]) {
388 if (MDOC_PPHRASE
& m
->flags
)
391 * If we're not in a partial phrase and the flag for
392 * being a phrase literal is still set, the punctuation
395 if (MDOC_PHRASELIT
& m
->flags
)
396 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
);
398 m
->flags
&= ~MDOC_PHRASELIT
;
404 if (ARGS_DELIM
& fl
&& args_checkpunct(m
, buf
, *pos
, line
, fl
))
408 * First handle TABSEP items, restricted to `Bl -column'. This
409 * ignores conventional token parsing and instead uses tabs or
410 * `Ta' macros to separate phrases. Phrases are parsed again
411 * for arguments at a later phase.
414 if (ARGS_TABSEP
& fl
) {
415 /* Scan ahead to tab (can't be escaped). */
416 p
= strchr(*v
, '\t');
419 /* Scan ahead to unescaped `Ta'. */
420 if ( ! (MDOC_PHRASELIT
& m
->flags
))
421 for (pp
= *v
; ; pp
++) {
422 if (NULL
== (pp
= strstr(pp
, "Ta")))
424 if (pp
> *v
&& ' ' != *(pp
- 1))
426 if (' ' == *(pp
+ 2) || '\0' == *(pp
+ 2))
430 /* By default, assume a phrase. */
434 * Adjust new-buffer position to be beyond delimiter
435 * mark (e.g., Ta -> end + 2).
438 *pos
+= pp
< p
? 2 : 1;
439 rc
= pp
< p
? ARGS_PHRASE
: ARGS_PPHRASE
;
441 } else if (p
&& ! pp
) {
444 } else if (pp
&& ! p
) {
452 /* Whitespace check for eoln case... */
453 if ('\0' == *p
&& ' ' == *(p
- 1) && ! (ARGS_NOWARN
& fl
))
454 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
456 *pos
+= (int)(p
- *v
);
458 /* Strip delimiter's preceding whitespace. */
460 while (pp
> *v
&& ' ' == *pp
) {
461 if (pp
> *v
&& '\\' == *(pp
- 1))
467 /* Strip delimiter's proceeding whitespace. */
468 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
475 * Process a quoted literal. A quote begins with a double-quote
476 * and ends with a double-quote NOT preceded by a double-quote.
477 * Whitespace is NOT involved in literal termination.
480 if (MDOC_PHRASELIT
& m
->flags
|| '\"' == buf
[*pos
]) {
481 if ( ! (MDOC_PHRASELIT
& m
->flags
))
484 if (MDOC_PPHRASE
& m
->flags
)
485 m
->flags
|= MDOC_PHRASELIT
;
487 for ( ; buf
[*pos
]; (*pos
)++) {
488 if ('\"' != buf
[*pos
])
490 if ('\"' != buf
[*pos
+ 1])
495 if ('\0' == buf
[*pos
]) {
496 if (ARGS_NOWARN
& fl
|| MDOC_PPHRASE
& m
->flags
)
498 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_BADQUOTE
);
502 m
->flags
&= ~MDOC_PHRASELIT
;
503 buf
[(*pos
)++] = '\0';
505 if ('\0' == buf
[*pos
])
508 while (' ' == buf
[*pos
])
511 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
512 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
518 * A non-quoted term progresses until either the end of line or
519 * a non-escaped whitespace.
522 for ( ; buf
[*pos
]; (*pos
)++)
523 if (*pos
&& ' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
526 if ('\0' == buf
[*pos
])
529 buf
[(*pos
)++] = '\0';
531 while (' ' == buf
[*pos
])
534 if ('\0' == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
535 mdoc_pmsg(m
, line
, *pos
, MANDOCERR_EOLNSPACE
);
541 * Check if the string consists only of space-separated closing
542 * delimiters. This is a bit of a dance: the first must be a close
543 * delimiter, but it may be followed by middle delimiters. Arbitrary
544 * whitespace may separate these tokens.
547 args_checkpunct(struct mdoc
*m
, const char *buf
, int i
, int ln
, int fl
)
553 /* First token must be a close-delimiter. */
555 for (j
= 0; buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
; j
++, i
++)
562 if (DELIM_CLOSE
!= mdoc_isdelim(dbuf
))
565 while (' ' == buf
[i
])
568 /* Remaining must NOT be open/none. */
572 while (buf
[i
] && ' ' != buf
[i
] && j
< DELIMSZ
)
573 dbuf
[j
++] = buf
[i
++];
579 d
= mdoc_isdelim(dbuf
);
580 if (DELIM_NONE
== d
|| DELIM_OPEN
== d
)
583 while (' ' == buf
[i
])
587 if ( ! (ARGS_NOWARN
& fl
) && i
&& ' ' == buf
[i
- 1])
588 mdoc_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
);
590 return('\0' == buf
[i
]);
594 * Match up an argument string (e.g., `-foo bar' having "foo") with the
595 * correrct identifier. It must apply to the given macro. If none was
596 * found (including bad matches), return MDOC_ARG_MAX.
599 argv_a2arg(enum mdoct tok
, const char *p
)
601 enum mdocargt args
[MDOC_ARG_MAX
];
608 args
[len
++] = MDOC_Split
;
609 args
[len
++] = MDOC_Nosplit
;
612 args
[len
++] = MDOC_Ragged
;
613 args
[len
++] = MDOC_Unfilled
;
614 args
[len
++] = MDOC_Filled
;
615 args
[len
++] = MDOC_Literal
;
616 args
[len
++] = MDOC_File
;
617 args
[len
++] = MDOC_Offset
;
618 args
[len
++] = MDOC_Compact
;
619 args
[len
++] = MDOC_Centred
;
622 args
[len
++] = MDOC_Emphasis
;
623 args
[len
++] = MDOC_Literal
;
624 args
[len
++] = MDOC_Symbolic
;
627 args
[len
++] = MDOC_Words
;
630 args
[len
++] = MDOC_Bullet
;
631 args
[len
++] = MDOC_Dash
;
632 args
[len
++] = MDOC_Hyphen
;
633 args
[len
++] = MDOC_Item
;
634 args
[len
++] = MDOC_Enum
;
635 args
[len
++] = MDOC_Tag
;
636 args
[len
++] = MDOC_Diag
;
637 args
[len
++] = MDOC_Hang
;
638 args
[len
++] = MDOC_Ohang
;
639 args
[len
++] = MDOC_Inset
;
640 args
[len
++] = MDOC_Column
;
641 args
[len
++] = MDOC_Width
;
642 args
[len
++] = MDOC_Offset
;
643 args
[len
++] = MDOC_Compact
;
644 args
[len
++] = MDOC_Nested
;
649 args
[len
++] = MDOC_Std
;
655 for (i
= 0; i
< len
; i
++)
656 if (0 == strcmp(p
, mdoc_argnames
[args
[i
]]))
659 return(MDOC_ARG_MAX
);
663 argv_multi(struct mdoc
*m
, int line
,
664 struct mdoc_argv
*v
, int *pos
, char *buf
)
669 for (v
->sz
= 0; ; v
->sz
++) {
670 if ('-' == buf
[*pos
])
672 ac
= args(m
, line
, pos
, buf
, 0, &p
);
673 if (ARGS_ERROR
== ac
)
675 else if (ARGS_EOLN
== ac
)
678 if (0 == v
->sz
% MULTI_STEP
)
679 v
->value
= mandoc_realloc(v
->value
,
680 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
682 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
689 argv_opt_single(struct mdoc
*m
, int line
,
690 struct mdoc_argv
*v
, int *pos
, char *buf
)
695 if ('-' == buf
[*pos
])
698 ac
= args(m
, line
, pos
, buf
, 0, &p
);
699 if (ARGS_ERROR
== ac
)
705 v
->value
= mandoc_malloc(sizeof(char *));
706 v
->value
[0] = mandoc_strdup(p
);
712 * Parse a single, mandatory value from the stream.
715 argv_single(struct mdoc
*m
, int line
,
716 struct mdoc_argv
*v
, int *pos
, char *buf
)
724 ac
= args(m
, line
, pos
, buf
, 0, &p
);
725 if (ARGS_EOLN
== ac
) {
726 mdoc_pmsg(m
, line
, ppos
, MANDOCERR_SYNTARGVCOUNT
);
728 } else if (ARGS_ERROR
== ac
)
732 v
->value
= mandoc_malloc(sizeof(char *));
733 v
->value
[0] = mandoc_strdup(p
);
739 * Determine rules for parsing arguments. Arguments can either accept
740 * no parameters, an optional single parameter, one parameter, or
741 * multiple parameters.
744 argv(struct mdoc
*mdoc
, int line
,
745 struct mdoc_argv
*v
, int *pos
, char *buf
)
751 switch (argvflags
[v
->arg
]) {
753 return(argv_single(mdoc
, line
, v
, pos
, buf
));
755 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
756 case (ARGV_OPT_SINGLE
):
757 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));