]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.27 2009/10/07 14:52:35 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_OPT_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
)
312 if (NULL
== p
->argv
[i
].value
)
316 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
317 if (p
->argv
[i
].value
[j
])
318 free(p
->argv
[i
].value
[j
]);
320 free(p
->argv
[i
].value
);
329 mdoc_zargs(struct mdoc
*m
, int line
, int *pos
,
330 char *buf
, int flags
, char **v
)
333 return(args(m
, line
, pos
, buf
, flags
, 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 * Parse out the terms (like `val' in `.Xx -arg val' or simply
384 * `.Xx val'), which can have all sorts of properties:
386 * ARGS_DELIM: use special handling if encountering trailing
387 * delimiters in the form of [[::delim::][ ]+]+.
389 * ARGS_NOWARN: don't post warnings. This is only used when
390 * re-parsing delimiters, as the warnings have already been
393 * ARGS_TABSEP: use special handling for tab/`Ta' separated
394 * phrases like in `Bl -column'.
398 assert(' ' != buf
[*pos
]);
404 * If the first character is a delimiter and we're to look for
405 * delimited strings, then pass down the buffer seeing if it
406 * follows the pattern of [[::delim::][ ]+]+.
409 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
410 for (i
= *pos
; buf
[i
]; ) {
411 if ( ! mdoc_iscdelim(buf
[i
]))
414 if (0 == buf
[i
] || ' ' != buf
[i
])
417 while (buf
[i
] && ' ' == buf
[i
])
423 if (' ' != buf
[i
- 1])
425 if (ARGS_NOWARN
& fl
)
427 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
436 * First handle TABSEP items, restricted to `Bl -column'. This
437 * ignores conventional token parsing and instead uses tabs or
438 * `Ta' macros to separate phrases. Phrases are parsed again
439 * for arguments at a later phase.
442 if (ARGS_TABSEP
& fl
) {
443 /* Scan ahead to tab (can't be escaped). */
444 p
= strchr(*v
, '\t');
446 /* Scan ahead to unescaped `Ta'. */
447 for (pp
= *v
; ; pp
++) {
448 if (NULL
== (pp
= strstr(pp
, "Ta")))
450 if (pp
> *v
&& ' ' != *(pp
- 1))
452 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
457 * Adjust new-buffer position to be beyond delimiter
458 * mark (e.g., Ta -> end + 2).
461 *pos
+= pp
< p
? 2 : 1;
463 } else if (p
&& ! pp
) {
465 } else if (pp
&& ! p
) {
471 /* Whitespace check for eoln case... */
472 if (0 == *p
&& ' ' == *(p
- 1) && ! (ARGS_NOWARN
& fl
))
473 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
476 *pos
+= (int)(p
- *v
);
478 /* Strip delimiter's preceding whitespace. */
480 while (pp
> *v
&& ' ' == *pp
) {
481 if (pp
> *v
&& '\\' == *(pp
- 1))
487 /* Strip delimiter's proceeding whitespace. */
488 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
495 * Process a quoted literal. A quote begins with a double-quote
496 * and ends with a double-quote NOT preceded by a double-quote.
497 * Whitespace is NOT involved in literal termination.
500 if ('\"' == buf
[*pos
]) {
503 for ( ; buf
[*pos
]; (*pos
)++) {
504 if ('\"' != buf
[*pos
])
506 if ('\"' != buf
[*pos
+ 1])
511 if (0 == buf
[*pos
]) {
512 if (ARGS_NOWARN
& fl
)
514 if ( ! mdoc_pwarn(m
, line
, *pos
, EQUOTTERM
))
524 while (' ' == buf
[*pos
])
527 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
528 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
535 * A non-quoted term progresses until either the end of line or
536 * a non-escaped whitespace.
539 for ( ; buf
[*pos
]; (*pos
)++)
540 if (' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
548 while (' ' == buf
[*pos
])
551 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
552 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
560 argv_a2arg(int tok
, const char *p
)
564 * Parse an argument identifier from its text. XXX - this
565 * should really be table-driven to clarify the code.
567 * If you add an argument to the list, make sure that you
568 * register it here with its one or more macros!
573 if (0 == strcmp(p
, "split"))
575 else if (0 == strcmp(p
, "nosplit"))
576 return(MDOC_Nosplit
);
580 if (0 == strcmp(p
, "ragged"))
582 else if (0 == strcmp(p
, "unfilled"))
583 return(MDOC_Unfilled
);
584 else if (0 == strcmp(p
, "filled"))
586 else if (0 == strcmp(p
, "literal"))
587 return(MDOC_Literal
);
588 else if (0 == strcmp(p
, "file"))
590 else if (0 == strcmp(p
, "offset"))
592 else if (0 == strcmp(p
, "compact"))
593 return(MDOC_Compact
);
597 if (0 == strcmp(p
, "emphasis"))
598 return(MDOC_Emphasis
);
599 else if (0 == strcmp(p
, "literal"))
600 return(MDOC_Literal
);
601 else if (0 == strcmp(p
, "symbolic"))
602 return(MDOC_Symbolic
);
606 if (0 == strcmp(p
, "words"))
611 if (0 == strcmp(p
, "bullet"))
613 else if (0 == strcmp(p
, "dash"))
615 else if (0 == strcmp(p
, "hyphen"))
617 else if (0 == strcmp(p
, "item"))
619 else if (0 == strcmp(p
, "enum"))
621 else if (0 == strcmp(p
, "tag"))
623 else if (0 == strcmp(p
, "diag"))
625 else if (0 == strcmp(p
, "hang"))
627 else if (0 == strcmp(p
, "ohang"))
629 else if (0 == strcmp(p
, "inset"))
631 else if (0 == strcmp(p
, "column"))
633 else if (0 == strcmp(p
, "width"))
635 else if (0 == strcmp(p
, "offset"))
637 else if (0 == strcmp(p
, "compact"))
638 return(MDOC_Compact
);
639 else if (0 == strcmp(p
, "nested"))
646 if (0 == strcmp(p
, "std"))
653 return(MDOC_ARG_MAX
);
658 argv_multi(struct mdoc
*m
, int line
,
659 struct mdoc_argv
*v
, int *pos
, char *buf
)
664 for (v
->sz
= 0; ; v
->sz
++) {
665 if ('-' == buf
[*pos
])
667 c
= args(m
, line
, pos
, buf
, 0, &p
);
670 else if (ARGS_EOLN
== c
)
673 if (0 == v
->sz
% MULTI_STEP
) {
674 v
->value
= realloc(v
->value
,
675 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
676 if (NULL
== v
->value
) {
677 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
681 if (NULL
== (v
->value
[(int)v
->sz
] = strdup(p
)))
682 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
690 argv_opt_single(struct mdoc
*m
, int line
,
691 struct mdoc_argv
*v
, int *pos
, char *buf
)
696 if ('-' == buf
[*pos
])
699 c
= args(m
, line
, pos
, buf
, 0, &p
);
706 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
707 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
708 if (NULL
== (v
->value
[0] = strdup(p
)))
709 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
716 * Parse a single, mandatory value from the stream.
719 argv_single(struct mdoc
*m
, int line
,
720 struct mdoc_argv
*v
, int *pos
, char *buf
)
727 c
= args(m
, line
, pos
, buf
, 0, &p
);
731 return(mdoc_perr(m
, line
, ppos
, EARGVAL
));
734 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
735 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
736 if (NULL
== (v
->value
[0] = strdup(p
)))
737 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
744 * Determine rules for parsing arguments. Arguments can either accept
745 * no parameters, an optional single parameter, one parameter, or
746 * multiple parameters.
749 argv(struct mdoc
*mdoc
, int line
,
750 struct mdoc_argv
*v
, int *pos
, char *buf
)
756 switch (mdoc_argvflags
[v
->arg
]) {
758 return(argv_single(mdoc
, line
, v
, pos
, buf
));
760 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
761 case (ARGV_OPT_SINGLE
):
762 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));