1 /* $Vendor-Id: mdoc_argv.c,v 1.32 2009/10/30 05:58:38 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>
26 #include "libmandoc.h"
29 * Routines to parse arguments of macros. Arguments follow the syntax
30 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
31 * arguments, multiple arguments per value, no-value arguments, etc.
33 * There's no limit to the number or arguments that may be allocated.
36 #define ARGV_NONE (1 << 0)
37 #define ARGV_SINGLE (1 << 1)
38 #define ARGV_MULTI (1 << 2)
39 #define ARGV_OPT_SINGLE (1 << 3)
43 static int argv_a2arg(int, const char *);
44 static int args(struct mdoc
*, int, int *,
45 char *, int, char **);
46 static int argv(struct mdoc
*, int,
47 struct mdoc_argv
*, int *, char *);
48 static int argv_single(struct mdoc
*, int,
49 struct mdoc_argv
*, int *, char *);
50 static int argv_opt_single(struct mdoc
*, int,
51 struct mdoc_argv
*, int *, char *);
52 static int argv_multi(struct mdoc
*, int,
53 struct mdoc_argv
*, int *, char *);
55 /* Per-argument flags. */
57 static int mdoc_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 int mdoc_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
, int tok
,
218 struct mdoc_arg
**v
, int *pos
, char *buf
)
221 struct mdoc_argv tmp
;
222 struct mdoc_arg
*arg
;
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. */
251 (void)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 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
278 &tmp
, sizeof(struct mdoc_argv
));
285 mdoc_argv_free(struct mdoc_arg
*p
)
300 for (i
= 0; i
< (int)p
->argc
; i
++) {
301 if (0 == p
->argv
[i
].sz
)
303 if (NULL
== p
->argv
[i
].value
)
307 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
308 if (p
->argv
[i
].value
[j
])
309 free(p
->argv
[i
].value
[j
]);
311 free(p
->argv
[i
].value
);
320 mdoc_zargs(struct mdoc
*m
, int line
, int *pos
,
321 char *buf
, int flags
, char **v
)
324 return(args(m
, line
, pos
, buf
, flags
, v
));
329 mdoc_args(struct mdoc
*m
, int line
,
330 int *pos
, char *buf
, int tok
, char **v
)
335 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
338 return(args(m
, line
, pos
, buf
, fl
, v
));
341 * The `It' macro is a special case, as it acquires parameters from its
342 * parent `Bl' context, specifically, we're concerned with -column.
345 for (n
= m
->last
; n
; n
= n
->parent
)
346 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
350 c
= (int)(n
->args
? n
->args
->argc
: 0);
354 for (i
= 0; i
< c
; i
++) {
355 if (MDOC_Column
!= n
->args
->argv
[i
].arg
)
362 return(args(m
, line
, pos
, buf
, fl
, v
));
367 args(struct mdoc
*m
, int line
, int *pos
,
368 char *buf
, int fl
, char **v
)
374 * Parse out the terms (like `val' in `.Xx -arg val' or simply
375 * `.Xx val'), which can have all sorts of properties:
377 * ARGS_DELIM: use special handling if encountering trailing
378 * delimiters in the form of [[::delim::][ ]+]+.
380 * ARGS_NOWARN: don't post warnings. This is only used when
381 * re-parsing delimiters, as the warnings have already been
384 * ARGS_TABSEP: use special handling for tab/`Ta' separated
385 * phrases like in `Bl -column'.
389 assert(' ' != buf
[*pos
]);
395 * If the first character is a delimiter and we're to look for
396 * delimited strings, then pass down the buffer seeing if it
397 * follows the pattern of [[::delim::][ ]+]+.
400 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
401 for (i
= *pos
; buf
[i
]; ) {
402 if ( ! mdoc_iscdelim(buf
[i
]))
405 if (0 == buf
[i
] || ' ' != buf
[i
])
408 while (buf
[i
] && ' ' == buf
[i
])
414 if (' ' != buf
[i
- 1])
416 if (ARGS_NOWARN
& fl
)
418 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
427 * First handle TABSEP items, restricted to `Bl -column'. This
428 * ignores conventional token parsing and instead uses tabs or
429 * `Ta' macros to separate phrases. Phrases are parsed again
430 * for arguments at a later phase.
433 if (ARGS_TABSEP
& fl
) {
434 /* Scan ahead to tab (can't be escaped). */
435 p
= strchr(*v
, '\t');
437 /* Scan ahead to unescaped `Ta'. */
438 for (pp
= *v
; ; pp
++) {
439 if (NULL
== (pp
= strstr(pp
, "Ta")))
441 if (pp
> *v
&& ' ' != *(pp
- 1))
443 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
448 * Adjust new-buffer position to be beyond delimiter
449 * mark (e.g., Ta -> end + 2).
452 *pos
+= pp
< p
? 2 : 1;
454 } else if (p
&& ! pp
) {
456 } else if (pp
&& ! p
) {
462 /* Whitespace check for eoln case... */
463 if (0 == *p
&& ' ' == *(p
- 1) && ! (ARGS_NOWARN
& fl
))
464 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
467 *pos
+= (int)(p
- *v
);
469 /* Strip delimiter's preceding whitespace. */
471 while (pp
> *v
&& ' ' == *pp
) {
472 if (pp
> *v
&& '\\' == *(pp
- 1))
478 /* Strip delimiter's proceeding whitespace. */
479 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
486 * Process a quoted literal. A quote begins with a double-quote
487 * and ends with a double-quote NOT preceded by a double-quote.
488 * Whitespace is NOT involved in literal termination.
491 if ('\"' == buf
[*pos
]) {
494 for ( ; buf
[*pos
]; (*pos
)++) {
495 if ('\"' != buf
[*pos
])
497 if ('\"' != buf
[*pos
+ 1])
502 if (0 == buf
[*pos
]) {
503 if (ARGS_NOWARN
& fl
)
505 if ( ! mdoc_pwarn(m
, line
, *pos
, EQUOTTERM
))
515 while (' ' == buf
[*pos
])
518 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
519 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
526 * A non-quoted term progresses until either the end of line or
527 * a non-escaped whitespace.
530 for ( ; buf
[*pos
]; (*pos
)++)
531 if (' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
539 while (' ' == buf
[*pos
])
542 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
543 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
551 argv_a2arg(int tok
, const char *p
)
555 * Parse an argument identifier from its text. XXX - this
556 * should really be table-driven to clarify the code.
558 * If you add an argument to the list, make sure that you
559 * register it here with its one or more macros!
564 if (0 == strcmp(p
, "split"))
566 else if (0 == strcmp(p
, "nosplit"))
567 return(MDOC_Nosplit
);
571 if (0 == strcmp(p
, "ragged"))
573 else if (0 == strcmp(p
, "unfilled"))
574 return(MDOC_Unfilled
);
575 else if (0 == strcmp(p
, "filled"))
577 else if (0 == strcmp(p
, "literal"))
578 return(MDOC_Literal
);
579 else if (0 == strcmp(p
, "file"))
581 else if (0 == strcmp(p
, "offset"))
583 else if (0 == strcmp(p
, "compact"))
584 return(MDOC_Compact
);
585 else if (0 == strcmp(p
, "centered"))
586 return(MDOC_Centred
);
590 if (0 == strcmp(p
, "emphasis"))
591 return(MDOC_Emphasis
);
592 else if (0 == strcmp(p
, "literal"))
593 return(MDOC_Literal
);
594 else if (0 == strcmp(p
, "symbolic"))
595 return(MDOC_Symbolic
);
599 if (0 == strcmp(p
, "words"))
604 if (0 == strcmp(p
, "bullet"))
606 else if (0 == strcmp(p
, "dash"))
608 else if (0 == strcmp(p
, "hyphen"))
610 else if (0 == strcmp(p
, "item"))
612 else if (0 == strcmp(p
, "enum"))
614 else if (0 == strcmp(p
, "tag"))
616 else if (0 == strcmp(p
, "diag"))
618 else if (0 == strcmp(p
, "hang"))
620 else if (0 == strcmp(p
, "ohang"))
622 else if (0 == strcmp(p
, "inset"))
624 else if (0 == strcmp(p
, "column"))
626 else if (0 == strcmp(p
, "width"))
628 else if (0 == strcmp(p
, "offset"))
630 else if (0 == strcmp(p
, "compact"))
631 return(MDOC_Compact
);
632 else if (0 == strcmp(p
, "nested"))
639 if (0 == strcmp(p
, "std"))
646 return(MDOC_ARG_MAX
);
651 argv_multi(struct mdoc
*m
, int line
,
652 struct mdoc_argv
*v
, int *pos
, char *buf
)
657 for (v
->sz
= 0; ; v
->sz
++) {
658 if ('-' == buf
[*pos
])
660 c
= args(m
, line
, pos
, buf
, 0, &p
);
663 else if (ARGS_EOLN
== c
)
666 if (0 == v
->sz
% MULTI_STEP
)
667 v
->value
= mandoc_realloc(v
->value
,
668 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
670 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
678 argv_opt_single(struct mdoc
*m
, int line
,
679 struct mdoc_argv
*v
, int *pos
, char *buf
)
684 if ('-' == buf
[*pos
])
687 c
= args(m
, line
, pos
, buf
, 0, &p
);
694 v
->value
= mandoc_malloc(sizeof(char *));
695 v
->value
[0] = mandoc_strdup(p
);
702 * Parse a single, mandatory value from the stream.
705 argv_single(struct mdoc
*m
, int line
,
706 struct mdoc_argv
*v
, int *pos
, char *buf
)
713 c
= args(m
, line
, pos
, buf
, 0, &p
);
717 return(mdoc_perr(m
, line
, ppos
, EARGVAL
));
720 v
->value
= mandoc_malloc(sizeof(char *));
721 v
->value
[0] = mandoc_strdup(p
);
728 * Determine rules for parsing arguments. Arguments can either accept
729 * no parameters, an optional single parameter, one parameter, or
730 * multiple parameters.
733 argv(struct mdoc
*mdoc
, int line
,
734 struct mdoc_argv
*v
, int *pos
, char *buf
)
740 switch (mdoc_argvflags
[v
->arg
]) {
742 return(argv_single(mdoc
, line
, v
, pos
, buf
));
744 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
745 case (ARGV_OPT_SINGLE
):
746 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));