]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.33 2010/01/01 17:14:29 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.
21 #include <sys/types.h>
30 #include "libmandoc.h"
33 * Routines to parse arguments of macros. Arguments follow the syntax
34 * of `-arg [val [valN...]]'. Arguments come in all types: quoted
35 * arguments, multiple arguments per value, no-value arguments, etc.
37 * There's no limit to the number or arguments that may be allocated.
40 #define ARGV_NONE (1 << 0)
41 #define ARGV_SINGLE (1 << 1)
42 #define ARGV_MULTI (1 << 2)
43 #define ARGV_OPT_SINGLE (1 << 3)
47 static int argv_a2arg(int, const char *);
48 static int args(struct mdoc
*, int, int *,
49 char *, int, char **);
50 static int argv(struct mdoc
*, int,
51 struct mdoc_argv
*, int *, char *);
52 static int argv_single(struct mdoc
*, int,
53 struct mdoc_argv
*, int *, char *);
54 static int argv_opt_single(struct mdoc
*, int,
55 struct mdoc_argv
*, int *, char *);
56 static int argv_multi(struct mdoc
*, int,
57 struct mdoc_argv
*, int *, char *);
59 /* Per-argument flags. */
61 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
62 ARGV_NONE
, /* MDOC_Split */
63 ARGV_NONE
, /* MDOC_Nosplit */
64 ARGV_NONE
, /* MDOC_Ragged */
65 ARGV_NONE
, /* MDOC_Unfilled */
66 ARGV_NONE
, /* MDOC_Literal */
67 ARGV_SINGLE
, /* MDOC_File */
68 ARGV_OPT_SINGLE
, /* MDOC_Offset */
69 ARGV_NONE
, /* MDOC_Bullet */
70 ARGV_NONE
, /* MDOC_Dash */
71 ARGV_NONE
, /* MDOC_Hyphen */
72 ARGV_NONE
, /* MDOC_Item */
73 ARGV_NONE
, /* MDOC_Enum */
74 ARGV_NONE
, /* MDOC_Tag */
75 ARGV_NONE
, /* MDOC_Diag */
76 ARGV_NONE
, /* MDOC_Hang */
77 ARGV_NONE
, /* MDOC_Ohang */
78 ARGV_NONE
, /* MDOC_Inset */
79 ARGV_MULTI
, /* MDOC_Column */
80 ARGV_SINGLE
, /* MDOC_Width */
81 ARGV_NONE
, /* MDOC_Compact */
82 ARGV_NONE
, /* MDOC_Std */
83 ARGV_NONE
, /* MDOC_Filled */
84 ARGV_NONE
, /* MDOC_Words */
85 ARGV_NONE
, /* MDOC_Emphasis */
86 ARGV_NONE
, /* MDOC_Symbolic */
87 ARGV_NONE
/* MDOC_Symbolic */
90 static int mdoc_argflags
[MDOC_MAX
] = {
151 ARGS_DELIM
, /* Bsx */
201 ARGS_DELIM
, /* Brq */
203 ARGS_DELIM
, /* Brc */
216 * Parse an argument from line text. This comes in the form of -key
217 * [value0...], which may either have a single mandatory value, at least
218 * one mandatory value, an optional single value, or no value.
221 mdoc_argv(struct mdoc
*m
, int line
, int tok
,
222 struct mdoc_arg
**v
, int *pos
, char *buf
)
225 struct mdoc_argv tmp
;
226 struct mdoc_arg
*arg
;
231 assert(' ' != buf
[*pos
]);
233 /* Parse through to the first unescaped space. */
241 if (' ' == buf
[*pos
])
242 if ('\\' != buf
[*pos
- 1])
247 /* XXX - save zeroed byte, if not an argument. */
255 (void)memset(&tmp
, 0, sizeof(struct mdoc_argv
));
259 /* See if our token accepts the argument. */
261 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
262 /* XXX - restore saved zeroed byte. */
268 while (buf
[*pos
] && ' ' == buf
[*pos
])
271 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
274 if (NULL
== (arg
= *v
))
275 arg
= *v
= mandoc_calloc(1, sizeof(struct mdoc_arg
));
278 arg
->argv
= mandoc_realloc
279 (arg
->argv
, arg
->argc
* sizeof(struct mdoc_argv
));
281 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
282 &tmp
, sizeof(struct mdoc_argv
));
289 mdoc_argv_free(struct mdoc_arg
*p
)
304 for (i
= 0; i
< (int)p
->argc
; i
++) {
305 if (0 == p
->argv
[i
].sz
)
307 if (NULL
== p
->argv
[i
].value
)
311 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
312 if (p
->argv
[i
].value
[j
])
313 free(p
->argv
[i
].value
[j
]);
315 free(p
->argv
[i
].value
);
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
));
333 mdoc_args(struct mdoc
*m
, int line
,
334 int *pos
, char *buf
, int tok
, char **v
)
339 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
342 return(args(m
, line
, pos
, buf
, fl
, v
));
345 * The `It' macro is a special case, as it acquires parameters from its
346 * parent `Bl' context, specifically, we're concerned with -column.
349 for (n
= m
->last
; n
; n
= n
->parent
)
350 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
354 c
= (int)(n
->args
? n
->args
->argc
: 0);
358 for (i
= 0; i
< c
; i
++) {
359 if (MDOC_Column
!= n
->args
->argv
[i
].arg
)
366 return(args(m
, line
, pos
, buf
, fl
, v
));
371 args(struct mdoc
*m
, int line
, int *pos
,
372 char *buf
, int fl
, char **v
)
378 * Parse out the terms (like `val' in `.Xx -arg val' or simply
379 * `.Xx val'), which can have all sorts of properties:
381 * ARGS_DELIM: use special handling if encountering trailing
382 * delimiters in the form of [[::delim::][ ]+]+.
384 * ARGS_NOWARN: don't post warnings. This is only used when
385 * re-parsing delimiters, as the warnings have already been
388 * ARGS_TABSEP: use special handling for tab/`Ta' separated
389 * phrases like in `Bl -column'.
393 assert(' ' != buf
[*pos
]);
399 * If the first character is a delimiter and we're to look for
400 * delimited strings, then pass down the buffer seeing if it
401 * follows the pattern of [[::delim::][ ]+]+.
404 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
405 for (i
= *pos
; buf
[i
]; ) {
406 if ( ! mdoc_iscdelim(buf
[i
]))
409 if (0 == buf
[i
] || ' ' != buf
[i
])
412 while (buf
[i
] && ' ' == buf
[i
])
418 if (' ' != buf
[i
- 1])
420 if (ARGS_NOWARN
& fl
)
422 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
431 * First handle TABSEP items, restricted to `Bl -column'. This
432 * ignores conventional token parsing and instead uses tabs or
433 * `Ta' macros to separate phrases. Phrases are parsed again
434 * for arguments at a later phase.
437 if (ARGS_TABSEP
& fl
) {
438 /* Scan ahead to tab (can't be escaped). */
439 p
= strchr(*v
, '\t');
441 /* Scan ahead to unescaped `Ta'. */
442 for (pp
= *v
; ; pp
++) {
443 if (NULL
== (pp
= strstr(pp
, "Ta")))
445 if (pp
> *v
&& ' ' != *(pp
- 1))
447 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
452 * Adjust new-buffer position to be beyond delimiter
453 * mark (e.g., Ta -> end + 2).
456 *pos
+= pp
< p
? 2 : 1;
458 } else if (p
&& ! pp
) {
460 } else if (pp
&& ! p
) {
466 /* Whitespace check for eoln case... */
467 if (0 == *p
&& ' ' == *(p
- 1) && ! (ARGS_NOWARN
& fl
))
468 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
471 *pos
+= (int)(p
- *v
);
473 /* Strip delimiter's preceding whitespace. */
475 while (pp
> *v
&& ' ' == *pp
) {
476 if (pp
> *v
&& '\\' == *(pp
- 1))
482 /* Strip delimiter's proceeding whitespace. */
483 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
490 * Process a quoted literal. A quote begins with a double-quote
491 * and ends with a double-quote NOT preceded by a double-quote.
492 * Whitespace is NOT involved in literal termination.
495 if ('\"' == buf
[*pos
]) {
498 for ( ; buf
[*pos
]; (*pos
)++) {
499 if ('\"' != buf
[*pos
])
501 if ('\"' != buf
[*pos
+ 1])
506 if (0 == buf
[*pos
]) {
507 if (ARGS_NOWARN
& fl
)
509 if ( ! mdoc_pwarn(m
, line
, *pos
, EQUOTTERM
))
519 while (' ' == buf
[*pos
])
522 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
523 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
530 * A non-quoted term progresses until either the end of line or
531 * a non-escaped whitespace.
534 for ( ; buf
[*pos
]; (*pos
)++)
535 if (' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
543 while (' ' == buf
[*pos
])
546 if (0 == buf
[*pos
] && ! (ARGS_NOWARN
& fl
))
547 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
555 argv_a2arg(int tok
, const char *p
)
559 * Parse an argument identifier from its text. XXX - this
560 * should really be table-driven to clarify the code.
562 * If you add an argument to the list, make sure that you
563 * register it here with its one or more macros!
568 if (0 == strcmp(p
, "split"))
570 else if (0 == strcmp(p
, "nosplit"))
571 return(MDOC_Nosplit
);
575 if (0 == strcmp(p
, "ragged"))
577 else if (0 == strcmp(p
, "unfilled"))
578 return(MDOC_Unfilled
);
579 else if (0 == strcmp(p
, "filled"))
581 else if (0 == strcmp(p
, "literal"))
582 return(MDOC_Literal
);
583 else if (0 == strcmp(p
, "file"))
585 else if (0 == strcmp(p
, "offset"))
587 else if (0 == strcmp(p
, "compact"))
588 return(MDOC_Compact
);
589 else if (0 == strcmp(p
, "centered"))
590 return(MDOC_Centred
);
594 if (0 == strcmp(p
, "emphasis"))
595 return(MDOC_Emphasis
);
596 else if (0 == strcmp(p
, "literal"))
597 return(MDOC_Literal
);
598 else if (0 == strcmp(p
, "symbolic"))
599 return(MDOC_Symbolic
);
603 if (0 == strcmp(p
, "words"))
608 if (0 == strcmp(p
, "bullet"))
610 else if (0 == strcmp(p
, "dash"))
612 else if (0 == strcmp(p
, "hyphen"))
614 else if (0 == strcmp(p
, "item"))
616 else if (0 == strcmp(p
, "enum"))
618 else if (0 == strcmp(p
, "tag"))
620 else if (0 == strcmp(p
, "diag"))
622 else if (0 == strcmp(p
, "hang"))
624 else if (0 == strcmp(p
, "ohang"))
626 else if (0 == strcmp(p
, "inset"))
628 else if (0 == strcmp(p
, "column"))
630 else if (0 == strcmp(p
, "width"))
632 else if (0 == strcmp(p
, "offset"))
634 else if (0 == strcmp(p
, "compact"))
635 return(MDOC_Compact
);
636 else if (0 == strcmp(p
, "nested"))
643 if (0 == strcmp(p
, "std"))
650 return(MDOC_ARG_MAX
);
655 argv_multi(struct mdoc
*m
, int line
,
656 struct mdoc_argv
*v
, int *pos
, char *buf
)
661 for (v
->sz
= 0; ; v
->sz
++) {
662 if ('-' == buf
[*pos
])
664 c
= args(m
, line
, pos
, buf
, 0, &p
);
667 else if (ARGS_EOLN
== c
)
670 if (0 == v
->sz
% MULTI_STEP
)
671 v
->value
= mandoc_realloc(v
->value
,
672 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
674 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
682 argv_opt_single(struct mdoc
*m
, int line
,
683 struct mdoc_argv
*v
, int *pos
, char *buf
)
688 if ('-' == buf
[*pos
])
691 c
= args(m
, line
, pos
, buf
, 0, &p
);
698 v
->value
= mandoc_malloc(sizeof(char *));
699 v
->value
[0] = mandoc_strdup(p
);
706 * Parse a single, mandatory value from the stream.
709 argv_single(struct mdoc
*m
, int line
,
710 struct mdoc_argv
*v
, int *pos
, char *buf
)
717 c
= args(m
, line
, pos
, buf
, 0, &p
);
721 return(mdoc_perr(m
, line
, ppos
, EARGVAL
));
724 v
->value
= mandoc_malloc(sizeof(char *));
725 v
->value
[0] = mandoc_strdup(p
);
732 * Determine rules for parsing arguments. Arguments can either accept
733 * no parameters, an optional single parameter, one parameter, or
734 * multiple parameters.
737 argv(struct mdoc
*mdoc
, int line
,
738 struct mdoc_argv
*v
, int *pos
, char *buf
)
744 switch (mdoc_argvflags
[v
->arg
]) {
746 return(argv_single(mdoc
, line
, v
, pos
, buf
));
748 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
749 case (ARGV_OPT_SINGLE
):
750 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));