]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_argv.c
1 /* $Id: mdoc_argv.c,v 1.22 2009/08/19 14:44: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 ARGS_DELIM (1 << 1)
36 #define ARGS_TABSEP (1 << 2)
38 #define ARGV_NONE (1 << 0)
39 #define ARGV_SINGLE (1 << 1)
40 #define ARGV_MULTI (1 << 2)
41 #define ARGV_OPT_SINGLE (1 << 3)
45 static int argv_a2arg(int, const char *);
46 static int args(struct mdoc
*, int, int *,
47 char *, int, char **);
48 static int argv(struct mdoc
*, int,
49 struct mdoc_argv
*, int *, char *);
50 static int argv_single(struct mdoc
*, int,
51 struct mdoc_argv
*, int *, char *);
52 static int argv_opt_single(struct mdoc
*, int,
53 struct mdoc_argv
*, int *, char *);
54 static int argv_multi(struct mdoc
*, int,
55 struct mdoc_argv
*, int *, char *);
57 /* Per-argument flags. */
59 static int mdoc_argvflags
[MDOC_ARG_MAX
] = {
60 ARGV_NONE
, /* MDOC_Split */
61 ARGV_NONE
, /* MDOC_Nosplit */
62 ARGV_NONE
, /* MDOC_Ragged */
63 ARGV_NONE
, /* MDOC_Unfilled */
64 ARGV_NONE
, /* MDOC_Literal */
65 ARGV_NONE
, /* MDOC_File */
66 ARGV_SINGLE
, /* MDOC_Offset */
67 ARGV_NONE
, /* MDOC_Bullet */
68 ARGV_NONE
, /* MDOC_Dash */
69 ARGV_NONE
, /* MDOC_Hyphen */
70 ARGV_NONE
, /* MDOC_Item */
71 ARGV_NONE
, /* MDOC_Enum */
72 ARGV_NONE
, /* MDOC_Tag */
73 ARGV_NONE
, /* MDOC_Diag */
74 ARGV_NONE
, /* MDOC_Hang */
75 ARGV_NONE
, /* MDOC_Ohang */
76 ARGV_NONE
, /* MDOC_Inset */
77 ARGV_MULTI
, /* MDOC_Column */
78 ARGV_SINGLE
, /* MDOC_Width */
79 ARGV_NONE
, /* MDOC_Compact */
80 ARGV_OPT_SINGLE
, /* MDOC_Std */
81 ARGV_NONE
, /* MDOC_Filled */
82 ARGV_NONE
, /* MDOC_Words */
83 ARGV_NONE
, /* MDOC_Emphasis */
84 ARGV_NONE
, /* MDOC_Symbolic */
85 ARGV_NONE
/* MDOC_Symbolic */
88 static int mdoc_argflags
[MDOC_MAX
] = {
149 ARGS_DELIM
, /* Bsx */
199 ARGS_DELIM
, /* Brq */
201 ARGS_DELIM
, /* Brc */
213 * Parse an argument from line text. This comes in the form of -key
214 * [value0...], which may either have a single mandatory value, at least
215 * one mandatory value, an optional single value, or no value.
218 mdoc_argv(struct mdoc
*m
, int line
, int tok
,
219 struct mdoc_arg
**v
, int *pos
, char *buf
)
222 struct mdoc_argv tmp
;
223 struct mdoc_arg
*arg
;
228 assert(' ' != buf
[*pos
]);
230 /* Parse through to the first unescaped space. */
238 if (' ' == buf
[*pos
])
239 if ('\\' != buf
[*pos
- 1])
244 /* XXX - save zeroed byte, if not an argument. */
252 (void)memset(&tmp
, 0, sizeof(struct mdoc_argv
));
256 /* See if our token accepts the argument. */
258 if (MDOC_ARG_MAX
== (tmp
.arg
= argv_a2arg(tok
, p
))) {
259 /* XXX - restore saved zeroed byte. */
265 while (buf
[*pos
] && ' ' == buf
[*pos
])
268 if ( ! argv(m
, line
, &tmp
, pos
, buf
))
271 if (NULL
== (arg
= *v
)) {
272 *v
= calloc(1, sizeof(struct mdoc_arg
));
274 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
281 arg
->argv
= realloc(arg
->argv
, arg
->argc
*
282 sizeof(struct mdoc_argv
));
284 if (NULL
== arg
->argv
) {
285 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
289 (void)memcpy(&arg
->argv
[(int)arg
->argc
- 1],
290 &tmp
, sizeof(struct mdoc_argv
));
297 mdoc_argv_free(struct mdoc_arg
*p
)
312 for (i
= 0; i
< (int)p
->argc
; i
++) {
313 if (0 == p
->argv
[i
].sz
)
316 for (j
= 0; j
< (int)p
->argv
[i
].sz
; j
++)
317 free(p
->argv
[i
].value
[j
]);
319 free(p
->argv
[i
].value
);
328 mdoc_zargs(struct mdoc
*m
, int line
, int *pos
, char *buf
, char **v
)
331 return(args(m
, line
, pos
, buf
, 0, v
));
336 mdoc_args(struct mdoc
*m
, int line
,
337 int *pos
, char *buf
, int tok
, char **v
)
342 fl
= (0 == tok
) ? 0 : mdoc_argflags
[tok
];
345 return(args(m
, line
, pos
, buf
, fl
, v
));
348 * The `It' macro is a special case, as it acquires parameters from its
349 * parent `Bl' context, specifically, we're concerned with -column.
352 for (n
= m
->last
; n
; n
= n
->parent
)
353 if (MDOC_BLOCK
== n
->type
&& MDOC_Bl
== n
->tok
)
357 c
= (int)(n
->args
? n
->args
->argc
: 0);
361 for (i
= 0; i
< c
; i
++) {
362 if (MDOC_Column
!= n
->args
->argv
[i
].arg
)
369 return(args(m
, line
, pos
, buf
, fl
, v
));
374 args(struct mdoc
*m
, int line
, int *pos
,
375 char *buf
, int fl
, char **v
)
381 assert(' ' != buf
[*pos
]);
387 * If the first character is a delimiter and we're to look for
388 * delimited strings, then pass down the buffer seeing if it
389 * follows the pattern of [[::delim::][ ]+]+.
392 if ((fl
& ARGS_DELIM
) && mdoc_iscdelim(buf
[*pos
])) {
393 for (i
= *pos
; buf
[i
]; ) {
394 if ( ! mdoc_iscdelim(buf
[i
]))
397 if (0 == buf
[i
] || ' ' != buf
[i
])
400 while (buf
[i
] && ' ' == buf
[i
])
404 /* FIXME: warn about trailing whitespace. */
415 * First handle TABSEP items, restricted to `Bl -column'. This
416 * ignores conventional token parsing and instead uses tabs or
417 * `Ta' macros to separate phrases. Phrases are parsed again
418 * for arguments at a later phase.
421 if (ARGS_TABSEP
& fl
) {
422 /* Scan ahead to tab (can't be escaped). */
423 p
= strchr(*v
, '\t');
425 /* Scan ahead to unescaped `Ta'. */
426 for (pp
= *v
; ; pp
++) {
427 if (NULL
== (pp
= strstr(pp
, "Ta")))
429 if (pp
> *v
&& ' ' != *(pp
- 1))
431 if (' ' == *(pp
+ 2) || 0 == *(pp
+ 2))
436 * Adjust new-buffer position to be beyond delimiter
437 * mark (e.g., Ta -> end + 2).
440 *pos
+= pp
< p
? 2 : 1;
442 } else if (p
&& ! pp
) {
444 } else if (pp
&& ! p
) {
450 /* Whitespace check for eoln case... */
451 if (0 == *p
&& ' ' == *(p
- 1))
452 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
455 *pos
+= (int)(p
- *v
);
457 /* Strip delimiter's preceding whitespace. */
459 while (pp
> *v
&& ' ' == *pp
) {
460 if (pp
> *v
&& '\\' == *(pp
- 1))
466 /* Strip delimiter's proceeding whitespace. */
467 for (pp
= &buf
[*pos
]; ' ' == *pp
; pp
++, (*pos
)++)
474 * Process a quoted literal. A quote begins with a double-quote
475 * and ends with a double-quote NOT preceded by a double-quote.
476 * Whitespace is NOT involved in literal termination.
479 if ('\"' == buf
[*pos
]) {
482 for ( ; buf
[*pos
]; (*pos
)++) {
483 if ('\"' != buf
[*pos
])
485 if ('\"' != buf
[*pos
+ 1])
490 if (0 == buf
[*pos
]) {
491 if ( ! mdoc_pwarn(m
, line
, *pos
, EQUOTTERM
))
501 while (' ' == buf
[*pos
])
505 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
512 * A non-quoted term progresses until either the end of line or
513 * a non-escaped whitespace.
516 for ( ; buf
[*pos
]; (*pos
)++)
517 if (' ' == buf
[*pos
] && '\\' != buf
[*pos
- 1])
525 while (' ' == buf
[*pos
])
529 if ( ! mdoc_pwarn(m
, line
, *pos
, ETAILWS
))
537 argv_a2arg(int tok
, const char *argv
)
541 * Parse an argument identifier from its text. XXX - this
542 * should really be table-driven to clarify the code.
544 * If you add an argument to the list, make sure that you
545 * register it here with its one or more macros!
550 if (0 == strcmp(argv
, "split"))
552 else if (0 == strcmp(argv
, "nosplit"))
553 return(MDOC_Nosplit
);
557 if (0 == strcmp(argv
, "ragged"))
559 else if (0 == strcmp(argv
, "unfilled"))
560 return(MDOC_Unfilled
);
561 else if (0 == strcmp(argv
, "filled"))
563 else if (0 == strcmp(argv
, "literal"))
564 return(MDOC_Literal
);
565 else if (0 == strcmp(argv
, "file"))
567 else if (0 == strcmp(argv
, "offset"))
569 else if (0 == strcmp(argv
, "compact"))
570 return(MDOC_Compact
);
574 if (0 == strcmp(argv
, "emphasis"))
575 return(MDOC_Emphasis
);
576 else if (0 == strcmp(argv
, "literal"))
577 return(MDOC_Literal
);
578 else if (0 == strcmp(argv
, "symbolic"))
579 return(MDOC_Symbolic
);
583 if (0 == strcmp(argv
, "words"))
588 if (0 == strcmp(argv
, "bullet"))
590 else if (0 == strcmp(argv
, "dash"))
592 else if (0 == strcmp(argv
, "hyphen"))
594 else if (0 == strcmp(argv
, "item"))
596 else if (0 == strcmp(argv
, "enum"))
598 else if (0 == strcmp(argv
, "tag"))
600 else if (0 == strcmp(argv
, "diag"))
602 else if (0 == strcmp(argv
, "hang"))
604 else if (0 == strcmp(argv
, "ohang"))
606 else if (0 == strcmp(argv
, "inset"))
608 else if (0 == strcmp(argv
, "column"))
610 else if (0 == strcmp(argv
, "width"))
612 else if (0 == strcmp(argv
, "offset"))
614 else if (0 == strcmp(argv
, "compact"))
615 return(MDOC_Compact
);
616 else if (0 == strcmp(argv
, "nested"))
623 if (0 == strcmp(argv
, "std"))
630 return(MDOC_ARG_MAX
);
635 argv_multi(struct mdoc
*m
, int line
,
636 struct mdoc_argv
*v
, int *pos
, char *buf
)
641 for (v
->sz
= 0; ; v
->sz
++) {
642 if ('-' == buf
[*pos
])
644 c
= args(m
, line
, pos
, buf
, 0, &p
);
647 else if (ARGS_EOLN
== c
)
650 if (0 == v
->sz
% MULTI_STEP
) {
651 v
->value
= realloc(v
->value
,
652 (v
->sz
+ MULTI_STEP
) * sizeof(char *));
653 if (NULL
== v
->value
) {
654 (void)mdoc_nerr(m
, m
->last
, EMALLOC
);
658 if (NULL
== (v
->value
[(int)v
->sz
] = strdup(p
)))
659 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
667 argv_opt_single(struct mdoc
*m
, int line
,
668 struct mdoc_argv
*v
, int *pos
, char *buf
)
673 if ('-' == buf
[*pos
])
676 c
= args(m
, line
, pos
, buf
, 0, &p
);
683 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
684 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
685 if (NULL
== (v
->value
[0] = strdup(p
)))
686 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
693 * Parse a single, mandatory value from the stream.
696 argv_single(struct mdoc
*m
, int line
,
697 struct mdoc_argv
*v
, int *pos
, char *buf
)
704 c
= args(m
, line
, pos
, buf
, 0, &p
);
708 return(mdoc_perr(m
, line
, ppos
, EARGVAL
));
711 if (NULL
== (v
->value
= calloc(1, sizeof(char *))))
712 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
713 if (NULL
== (v
->value
[0] = strdup(p
)))
714 return(mdoc_nerr(m
, m
->last
, EMALLOC
));
721 * Determine rules for parsing arguments. Arguments can either accept
722 * no parameters, an optional single parameter, one parameter, or
723 * multiple parameters.
726 argv(struct mdoc
*mdoc
, int line
,
727 struct mdoc_argv
*v
, int *pos
, char *buf
)
733 switch (mdoc_argvflags
[v
->arg
]) {
735 return(argv_single(mdoc
, line
, v
, pos
, buf
));
737 return(argv_multi(mdoc
, line
, v
, pos
, buf
));
738 case (ARGV_OPT_SINGLE
):
739 return(argv_opt_single(mdoc
, line
, v
, pos
, buf
));