]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.25 2009/08/20 13:22:48 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 #define ARGV_NONE (1 << 0)
36 #define ARGV_SINGLE (1 << 1)
37 #define ARGV_MULTI (1 << 2)
38 #define ARGV_OPT_SINGLE (1 << 3)
42 static int argv_a2arg(int, const char *);
43 static int args(struct mdoc
*, int, int *,
44 char *, int, char **);
45 static int argv(struct mdoc
*, int,
46 struct mdoc_argv
*, int *, char *);
47 static int argv_single(struct mdoc
*, int,
48 struct mdoc_argv
*, int *, char *);
49 static int argv_opt_single(struct mdoc
*, int,
50 struct mdoc_argv
*, int *, char *);
51 static int argv_multi(struct mdoc
*, int,
52 struct mdoc_argv
*, int *, char *);
54 /* Per-argument flags. */
56 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
57 ARGV_NONE
, /* MDOC_Split */
58 ARGV_NONE
, /* MDOC_Nosplit */
59 ARGV_NONE
, /* MDOC_Ragged */
60 ARGV_NONE
, /* MDOC_Unfilled */
61 ARGV_NONE
, /* MDOC_Literal */
62 ARGV_NONE
, /* MDOC_File */
63 ARGV_SINGLE
, /* MDOC_Offset */
64 ARGV_NONE
, /* MDOC_Bullet */
65 ARGV_NONE
, /* MDOC_Dash */
66 ARGV_NONE
, /* MDOC_Hyphen */
67 ARGV_NONE
, /* MDOC_Item */
68 ARGV_NONE
, /* MDOC_Enum */
69 ARGV_NONE
, /* MDOC_Tag */
70 ARGV_NONE
, /* MDOC_Diag */
71 ARGV_NONE
, /* MDOC_Hang */
72 ARGV_NONE
, /* MDOC_Ohang */
73 ARGV_NONE
, /* MDOC_Inset */
74 ARGV_MULTI
, /* MDOC_Column */
75 ARGV_SINGLE
, /* MDOC_Width */
76 ARGV_NONE
, /* MDOC_Compact */
77 ARGV_NONE
, /* MDOC_Std */
78 ARGV_NONE
, /* MDOC_Filled */
79 ARGV_NONE
, /* MDOC_Words */
80 ARGV_NONE
, /* MDOC_Emphasis */
81 ARGV_NONE
, /* MDOC_Symbolic */
82 ARGV_NONE
/* MDOC_Symbolic */
85 static int mdoc_argflags
[MDOC_MAX
] = {
146 ARGS_DELIM
, /* Bsx */
196 ARGS_DELIM
, /* Brq */
198 ARGS_DELIM
, /* Brc */
210 * Parse an argument from line text. This comes in the form of -key
211 * [value0...], which may either have a single mandatory value, at least
212 * one mandatory value, an optional single value, or no value.
215 mdoc_argv(struct mdoc
*m
, int line
, int tok
,
216 struct mdoc_arg
**v
, int *pos
, char *buf
)
219 struct mdoc_argv tmp
;
220 struct mdoc_arg
*arg
;
225 assert(' ' != buf
[*pos
]);
227 /* Parse through to the first unescaped space. */
235 if (' ' == buf
[*pos
])
236 if ('\\' != buf
[*pos
- 1])
241 /* XXX - save zeroed byte, if not an argument. */
249 (void)memset(&tmp
, 0, sizeof(struct mdoc_argv
));
253 /* See if our token accepts the argument. */
255 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
256 /* XXX - restore saved zeroed byte. */
262 while (buf
[*pos
] && ' ' == buf
[*pos
])
265 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
268 if (NULL
== (arg
= *v
)) {
269 *v
= calloc(1, sizeof(struct mdoc_arg
));
271 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
278 arg
->argv
= realloc(arg
->argv
, arg
->argc
*
279 sizeof(struct mdoc_argv
));
281 if (NULL
== arg
->argv
) {
282 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
286 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
287 &tmp
, sizeof(struct mdoc_argv
));
294 mdoc_argv_free(struct mdoc_arg
*p
)
309 for (i
= 0; i
< (int)p
->argc
; i
++) {
310 if (0 == p
->argv
[i
].sz
)
313 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
314 free(p
->argv
[i
].value
[j
]);
316 free(p
->argv
[i
].value
);
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
));
334 mdoc_args(struct mdoc
*m
, int line
,
335 int *pos
, char *buf
, int tok
, char **v
)
340 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
343 return(args(m
, line
, pos
, buf
, fl
, v
));
346 * The `It' macro is a special case, as it acquires parameters from its
347 * parent `Bl' context, specifically, we're concerned with -column.
350 for (n
= m
->last
; n
; n
= n
->parent
)
351 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
355 c
= (int)(n
->args
? n
->args
->argc
: 0);
359 for (i
= 0; i
< c
; i
++) {
360 if (MDOC_Column
!= n
->args
->argv
[i
].arg
)
367 return(args(m
, line
, pos
, buf
, fl
, v
));
372 args(struct mdoc
*m
, int line
, int *pos
,
373 char *buf
, int fl
, char **v
)
379 * Parse out the terms (like `val' in `.Xx -arg val' or simply
380 * `.Xx val'), which can have all sorts of properties:
382 * ARGS_DELIM: use special handling if encountering trailing
383 * delimiters in the form of [[::delim::][ ]+]+.
385 * ARGS_NOWARN: don't post warnings. This is only used when
386 * re-parsing delimiters, as the warnings have already been
389 * ARGS_TABSEP: use special handling for tab/`Ta' separated
390 * phrases like in `Bl -column'.
394 assert(' ' != buf
[*pos
]);
400 * If the first character is a delimiter and we're to look for
401 * delimited strings, then pass down the buffer seeing if it
402 * follows the pattern of [[::delim::][ ]+]+.
405 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
406 for (i
= *pos
; buf
[i
]; ) {
407 if ( ! mdoc_iscdelim(buf
[i
]))
410 if (0 == buf
[i
] || ' ' != buf
[i
])
413 while (buf
[i
] && ' ' == buf
[i
])
419 if (' ' != buf
[i
- 1])
421 if (ARGS_NOWARN
& fl
)
423 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
432 * First handle TABSEP items, restricted to `Bl -column'. This
433 * ignores conventional token parsing and instead uses tabs or
434 * `Ta' macros to separate phrases. Phrases are parsed again
435 * for arguments at a later phase.
438 if (ARGS_TABSEP
& fl
) {
439 /* Scan ahead to tab (can't be escaped). */
440 p
= strchr(*v
, '\t');
442 /* Scan ahead to unescaped `Ta'. */
443 for (pp
= *v
; ; pp
++) {
444 if (NULL
== (pp
= strstr(pp
, "Ta")))
446 if (pp
> *v
&& ' ' != *(pp
- 1))
448 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
453 * Adjust new-buffer position to be beyond delimiter
454 * mark (e.g., Ta -> end + 2).
457 *pos
+= pp
< p
? 2 : 1;
459 } else if (p
&& ! pp
) {
461 } else if (pp
&& ! p
) {
467 /* Whitespace check for eoln case... */
468 if (0 == *p
&& ' ' == *(p
- 1) && ! (ARGS_NOWARN
& fl
))
469 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
472 *pos
+= (int)(p
- *v
);
474 /* Strip delimiter's preceding whitespace. */
476 while (pp
> *v
&& ' ' == *pp
) {
477 if (pp
> *v
&& '\\' == *(pp
- 1))
483 /* Strip delimiter's proceeding whitespace. */
484 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
491 * Process a quoted literal. A quote begins with a double-quote
492 * and ends with a double-quote NOT preceded by a double-quote.
493 * Whitespace is NOT involved in literal termination.
496 if ('\"' == buf
[*pos
]) {
499 for ( ; buf
[*pos
]; (*pos
)++) {
500 if ('\"' != buf
[*pos
])
502 if ('\"' != buf
[*pos
+ 1])
507 if (0 == buf
[*pos
]) {
508 if (ARGS_NOWARN
& fl
)
510 if ( ! mdoc_pwarn(m
, line
, *pos
, EQUOTTERM
))
520 while (' ' == buf
[*pos
])
523 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
524 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
531 * A non-quoted term progresses until either the end of line or
532 * a non-escaped whitespace.
535 for ( ; buf
[*pos
]; (*pos
)++)
536 if (' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
544 while (' ' == buf
[*pos
])
547 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
548 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
556 argv_a2arg(int tok
, const char *argv
)
560 * Parse an argument identifier from its text. XXX - this
561 * should really be table-driven to clarify the code.
563 * If you add an argument to the list, make sure that you
564 * register it here with its one or more macros!
569 if (0 == strcmp(argv
, "split"))
571 else if (0 == strcmp(argv
, "nosplit"))
572 return(MDOC_Nosplit
);
576 if (0 == strcmp(argv
, "ragged"))
578 else if (0 == strcmp(argv
, "unfilled"))
579 return(MDOC_Unfilled
);
580 else if (0 == strcmp(argv
, "filled"))
582 else if (0 == strcmp(argv
, "literal"))
583 return(MDOC_Literal
);
584 else if (0 == strcmp(argv
, "file"))
586 else if (0 == strcmp(argv
, "offset"))
588 else if (0 == strcmp(argv
, "compact"))
589 return(MDOC_Compact
);
593 if (0 == strcmp(argv
, "emphasis"))
594 return(MDOC_Emphasis
);
595 else if (0 == strcmp(argv
, "literal"))
596 return(MDOC_Literal
);
597 else if (0 == strcmp(argv
, "symbolic"))
598 return(MDOC_Symbolic
);
602 if (0 == strcmp(argv
, "words"))
607 if (0 == strcmp(argv
, "bullet"))
609 else if (0 == strcmp(argv
, "dash"))
611 else if (0 == strcmp(argv
, "hyphen"))
613 else if (0 == strcmp(argv
, "item"))
615 else if (0 == strcmp(argv
, "enum"))
617 else if (0 == strcmp(argv
, "tag"))
619 else if (0 == strcmp(argv
, "diag"))
621 else if (0 == strcmp(argv
, "hang"))
623 else if (0 == strcmp(argv
, "ohang"))
625 else if (0 == strcmp(argv
, "inset"))
627 else if (0 == strcmp(argv
, "column"))
629 else if (0 == strcmp(argv
, "width"))
631 else if (0 == strcmp(argv
, "offset"))
633 else if (0 == strcmp(argv
, "compact"))
634 return(MDOC_Compact
);
635 else if (0 == strcmp(argv
, "nested"))
642 if (0 == strcmp(argv
, "std"))
649 return(MDOC_ARG_MAX
);
654 argv_multi(struct mdoc
*m
, int line
,
655 struct mdoc_argv
*v
, int *pos
, char *buf
)
660 for (v
->sz
= 0; ; v
->sz
++) {
661 if ('-' == buf
[*pos
])
663 c
= args(m
, line
, pos
, buf
, 0, &p
);
666 else if (ARGS_EOLN
== c
)
669 if (0 == v
->sz
% MULTI_STEP
) {
670 v
->value
= realloc(v
->value
,
671 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
672 if (NULL
== v
->value
) {
673 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
677 if (NULL
== (v
->value
[(int)v
->sz
] = strdup(p
)))
678 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
686 argv_opt_single(struct mdoc
*m
, int line
,
687 struct mdoc_argv
*v
, int *pos
, char *buf
)
692 if ('-' == buf
[*pos
])
695 c
= args(m
, line
, pos
, buf
, 0, &p
);
702 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
703 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
704 if (NULL
== (v
->value
[0] = strdup(p
)))
705 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
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
)
723 c
= args(m
, line
, pos
, buf
, 0, &p
);
727 return(mdoc_perr(m
, line
, ppos
, EARGVAL
));
730 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
731 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
732 if (NULL
== (v
->value
[0] = strdup(p
)))
733 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
740 * Determine rules for parsing arguments. Arguments can either accept
741 * no parameters, an optional single parameter, one parameter, or
742 * multiple parameters.
745 argv(struct mdoc
*mdoc
, int line
,
746 struct mdoc_argv
*v
, int *pos
, char *buf
)
752 switch (mdoc_argvflags
[v
->arg
]) {
754 return(argv_single(mdoc
, line
, v
, pos
, buf
));
756 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
757 case (ARGV_OPT_SINGLE
):
758 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));