]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.38 2010/05/07 05:34:56 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(enum mdoct
, const char *);
48 static enum margerr
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
, enum mdoct 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
)
303 for (i
= (int)p
->argc
- 1; i
>= 0; i
--)
304 mdoc_argn_free(p
, i
);
312 mdoc_argn_free(struct mdoc_arg
*p
, int iarg
)
314 struct mdoc_argv
*arg
= &p
->argv
[iarg
];
317 if (arg
->sz
&& arg
->value
) {
318 for (j
= (int)arg
->sz
- 1; j
>= 0; j
--)
323 for (--p
->argc
; iarg
< (int)p
->argc
; iarg
++)
324 p
->argv
[iarg
] = p
->argv
[iarg
+1];
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
, int *pos
,
339 char *buf
, enum mdoct tok
, char **v
)
344 fl
= 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
]) > 1) {
410 for (i
= *pos
; buf
[i
]; ) {
411 if ( mdoc_iscdelim(buf
[i
]) < 2)
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(enum mdoct 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
);
594 else if (0 == strcmp(p
, "centered"))
595 return(MDOC_Centred
);
599 if (0 == strcmp(p
, "emphasis"))
600 return(MDOC_Emphasis
);
601 else if (0 == strcmp(p
, "literal"))
602 return(MDOC_Literal
);
603 else if (0 == strcmp(p
, "symbolic"))
604 return(MDOC_Symbolic
);
608 if (0 == strcmp(p
, "words"))
613 if (0 == strcmp(p
, "bullet"))
615 else if (0 == strcmp(p
, "dash"))
617 else if (0 == strcmp(p
, "hyphen"))
619 else if (0 == strcmp(p
, "item"))
621 else if (0 == strcmp(p
, "enum"))
623 else if (0 == strcmp(p
, "tag"))
625 else if (0 == strcmp(p
, "diag"))
627 else if (0 == strcmp(p
, "hang"))
629 else if (0 == strcmp(p
, "ohang"))
631 else if (0 == strcmp(p
, "inset"))
633 else if (0 == strcmp(p
, "column"))
635 else if (0 == strcmp(p
, "width"))
637 else if (0 == strcmp(p
, "offset"))
639 else if (0 == strcmp(p
, "compact"))
640 return(MDOC_Compact
);
641 else if (0 == strcmp(p
, "nested"))
648 if (0 == strcmp(p
, "std"))
655 return(MDOC_ARG_MAX
);
660 argv_multi(struct mdoc
*m
, int line
,
661 struct mdoc_argv
*v
, int *pos
, char *buf
)
666 for (v
->sz
= 0; ; v
->sz
++) {
667 if ('-' == buf
[*pos
])
669 c
= args(m
, line
, pos
, buf
, 0, &p
);
672 else if (ARGS_EOLN
== c
)
675 if (0 == v
->sz
% MULTI_STEP
)
676 v
->value
= mandoc_realloc(v
->value
,
677 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
679 v
->value
[(int)v
->sz
] = mandoc_strdup(p
);
687 argv_opt_single(struct mdoc
*m
, int line
,
688 struct mdoc_argv
*v
, int *pos
, char *buf
)
693 if ('-' == buf
[*pos
])
696 c
= args(m
, line
, pos
, buf
, 0, &p
);
703 v
->value
= mandoc_malloc(sizeof(char *));
704 v
->value
[0] = mandoc_strdup(p
);
711 * Parse a single, mandatory value from the stream.
714 argv_single(struct mdoc
*m
, int line
,
715 struct mdoc_argv
*v
, int *pos
, char *buf
)
722 c
= args(m
, line
, pos
, buf
, 0, &p
);
726 return(mdoc_perr(m
, line
, ppos
, EARGVAL
));
729 v
->value
= mandoc_malloc(sizeof(char *));
730 v
->value
[0] = mandoc_strdup(p
);
737 * Determine rules for parsing arguments. Arguments can either accept
738 * no parameters, an optional single parameter, one parameter, or
739 * multiple parameters.
742 argv(struct mdoc
*mdoc
, int line
,
743 struct mdoc_argv
*v
, int *pos
, char *buf
)
749 switch (mdoc_argvflags
[v
->arg
]) {
751 return(argv_single(mdoc
, line
, v
, pos
, buf
));
753 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
754 case (ARGV_OPT_SINGLE
):
755 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));