diff options
author | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-03-05 12:08:52 +0000 |
---|---|---|
committer | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-03-05 12:08:52 +0000 |
commit | 6936c4e5a0bd4bddcfd7c9d08d801aba0415dbad (patch) | |
tree | e85d890b346e499d7bd9aab58c9c512adfbdae36 | |
parent | 9e132a57b087be87cd731dd96397d9cc75b7bfa3 (diff) | |
download | mandoc-6936c4e5a0bd4bddcfd7c9d08d801aba0415dbad.tar.gz |
Added argv regression tests.
Table-ised argument flags in argv.c.
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | action.c | 39 | ||||
-rw-r--r-- | argv.c | 165 | ||||
-rw-r--r-- | macro.c | 5 | ||||
-rw-r--r-- | regress/test.argv.00 | 8 | ||||
-rw-r--r-- | regress/test.argv.01 | 7 |
6 files changed, 210 insertions, 20 deletions
@@ -101,7 +101,8 @@ FAIL = regress/test.empty \ regress/test.escape.09 \ regress/test.escape.11 \ regress/test.escape.12 \ - regress/test.escape.14 + regress/test.escape.14 \ + regress/test.argv.01 SUCCEED = regress/test.prologue.05 \ regress/test.prologue.07 \ @@ -131,7 +132,8 @@ SUCCEED = regress/test.prologue.05 \ regress/test.escape.00 \ regress/test.escape.05 \ regress/test.escape.10 \ - regress/test.escape.13 + regress/test.escape.13 \ + regress/test.argv.00 REGRESS = $(FAIL) $(SUCCEED) @@ -39,11 +39,12 @@ struct actions { static int post_bl(struct mdoc *); static int post_bl_width(struct mdoc *); static int post_bl_tagwidth(struct mdoc *); -static int post_sh(struct mdoc *); -static int post_os(struct mdoc *); -static int post_dt(struct mdoc *); static int post_dd(struct mdoc *); +static int post_dt(struct mdoc *); +static int post_ex(struct mdoc *); static int post_nm(struct mdoc *); +static int post_os(struct mdoc *); +static int post_sh(struct mdoc *); static int post_prologue(struct mdoc *); @@ -72,7 +73,7 @@ const struct actions mdoc_actions[MDOC_MAX] = { { NULL }, /* Dv */ { NULL }, /* Er */ { NULL }, /* Ev */ - { NULL }, /* Ex */ + { post_ex }, /* Ex */ { NULL }, /* Fa */ { NULL }, /* Fd */ { NULL }, /* Fl */ @@ -160,6 +161,36 @@ const struct actions mdoc_actions[MDOC_MAX] = { static int +post_ex(struct mdoc *mdoc) +{ + + /* + * If `.Ex -std' is invoked without an argument, fill it in with + * our name (if it's been set). + */ + + if (0 == mdoc->last->data.elem.argc) + return(1); + + assert(1 == mdoc->last->data.elem.argc); + if (1 == mdoc->last->data.elem.argv[0].sz) + return(1); + assert(0 == mdoc->last->data.elem.argv[0].sz); + + if (NULL == mdoc->meta.name) + return(mdoc_err(mdoc, "default name not yet set")); + + mdoc_msg(mdoc, "writing %s argument: %s", + mdoc_argnames[MDOC_Std], mdoc->meta.name); + + mdoc->last->data.elem.argv[0].sz = 1; + mdoc->last->data.elem.argv[0].value = xcalloc(1, sizeof(char *)); + mdoc->last->data.elem.argv[0].value[0] = xstrdup(mdoc->meta.name); + return(1); +} + + +static int post_nm(struct mdoc *mdoc) { char buf[64]; @@ -35,6 +35,11 @@ #define ARGS_DELIM (1 << 1) #define ARGS_TABSEP (1 << 2) +#define ARGV_NONE (1 << 0) +#define ARGV_SINGLE (1 << 1) +#define ARGV_MULTI (1 << 2) +#define ARGV_OPT_SINGLE (1 << 3) + static int argv_a2arg(int, const char *); static int args(struct mdoc *, int, int *, char *, int, char **); @@ -42,6 +47,8 @@ static int argv(struct mdoc *, int, struct mdoc_arg *, int *, char *); static int argv_single(struct mdoc *, int, struct mdoc_arg *, int *, char *); +static int argv_opt_single(struct mdoc *, int, + struct mdoc_arg *, int *, char *); static int argv_multi(struct mdoc *, int, struct mdoc_arg *, int *, char *); static int pwarn(struct mdoc *, int, int, int); @@ -60,6 +67,73 @@ static int perr(struct mdoc *, int, int, int); #define EARGVAL (1) #define EARGMANY (2) +/* Per-argument flags. */ + +static int mdoc_argvflags[MDOC_ARG_MAX] = { + ARGV_NONE, /* MDOC_Split */ + ARGV_NONE, /* MDOC_Nosplit */ + ARGV_NONE, /* MDOC_Ragged */ + ARGV_NONE, /* MDOC_Unfilled */ + ARGV_NONE, /* MDOC_Literal */ + ARGV_NONE, /* MDOC_File */ + ARGV_SINGLE, /* MDOC_Offset */ + ARGV_NONE, /* MDOC_Bullet */ + ARGV_NONE, /* MDOC_Dash */ + ARGV_NONE, /* MDOC_Hyphen */ + ARGV_NONE, /* MDOC_Item */ + ARGV_NONE, /* MDOC_Enum */ + ARGV_NONE, /* MDOC_Tag */ + ARGV_NONE, /* MDOC_Diag */ + ARGV_NONE, /* MDOC_Hang */ + ARGV_NONE, /* MDOC_Ohang */ + ARGV_NONE, /* MDOC_Inset */ + ARGV_MULTI, /* MDOC_Column */ + ARGV_SINGLE, /* MDOC_Width */ + ARGV_NONE, /* MDOC_Compact */ + ARGV_OPT_SINGLE, /* MDOC_Std */ + ARGV_NONE, /* MDOC_p1003_1_88 */ + ARGV_NONE, /* MDOC_p1003_1_90 */ + ARGV_NONE, /* MDOC_p1003_1_96 */ + ARGV_NONE, /* MDOC_p1003_1_2001 */ + ARGV_NONE, /* MDOC_p1003_1_2004 */ + ARGV_NONE, /* MDOC_p1003_1 */ + ARGV_NONE, /* MDOC_p1003_1b */ + ARGV_NONE, /* MDOC_p1003_1b_93 */ + ARGV_NONE, /* MDOC_p1003_1c_95 */ + ARGV_NONE, /* MDOC_p1003_1g_2000 */ + ARGV_NONE, /* MDOC_p1003_2_92 */ + ARGV_NONE, /* MDOC_p1387_2_95 */ + ARGV_NONE, /* MDOC_p1003_2 */ + ARGV_NONE, /* MDOC_p1387_2 */ + ARGV_NONE, /* MDOC_isoC_90 */ + ARGV_NONE, /* MDOC_isoC_amd1 */ + ARGV_NONE, /* MDOC_isoC_tcor1 */ + ARGV_NONE, /* MDOC_isoC_tcor2 */ + ARGV_NONE, /* MDOC_isoC_99 */ + ARGV_NONE, /* MDOC_ansiC */ + ARGV_NONE, /* MDOC_ansiC_89 */ + ARGV_NONE, /* MDOC_ansiC_99 */ + ARGV_NONE, /* MDOC_ieee754 */ + ARGV_NONE, /* MDOC_iso8802_3 */ + ARGV_NONE, /* MDOC_xpg3 */ + ARGV_NONE, /* MDOC_xpg4 */ + ARGV_NONE, /* MDOC_xpg4_2 */ + ARGV_NONE, /* MDOC_xpg4_3 */ + ARGV_NONE, /* MDOC_xbd5 */ + ARGV_NONE, /* MDOC_xcu5 */ + ARGV_NONE, /* MDOC_xsh5 */ + ARGV_NONE, /* MDOC_xns5 */ + ARGV_NONE, /* MDOC_xns5_2d2_0 */ + ARGV_NONE, /* MDOC_xcurses4_2 */ + ARGV_NONE, /* MDOC_susv2 */ + ARGV_NONE, /* MDOC_susv3 */ + ARGV_NONE, /* MDOC_svid4 */ + ARGV_NONE, /* MDOC_Filled */ + ARGV_NONE, /* MDOC_Words */ + ARGV_NONE, /* MDOC_Emphasis */ + ARGV_NONE /* MDOC_Symbolic */ +}; + static int mdoc_argflags[MDOC_MAX] = { 0, /* \" */ 0, /* Dd */ @@ -236,19 +310,27 @@ mdoc_args(struct mdoc *mdoc, int line, fl = (0 == tok) ? 0 : mdoc_argflags[tok]; /* - * First see if we should use TABSEP (Bl -column). This - * invalidates the use of ARGS_DELIM. + * Override per-macro argument flags with context-specific ones. + * As of now, this is only valid for `It' depending on its list + * context. */ - if (MDOC_It == tok) { + switch (tok) { + case (MDOC_It): for (n = mdoc->last; n; n = n->parent) - if (MDOC_BLOCK == n->type) - if (MDOC_Bl == n->tok) - break; + if (MDOC_BLOCK == n->type && MDOC_Bl == n->tok) + break; + assert(n); c = (int)n->data.block.argc; assert(c > 0); + /* + * Using `Bl -column' adds ARGS_TABSEP to the arguments + * and invalidates ARGS_DELIM. Using `Bl -diag' allows + * for quoted arguments. + */ + /* LINTED */ for (i = 0; i < c; i++) { switch (n->data.block.argv[i].arg) { @@ -265,8 +347,12 @@ mdoc_args(struct mdoc *mdoc, int line, break; } } + default: + break; } + /* Continue parsing the arguments themselves... */ + return(args(mdoc, line, pos, buf, fl, v)); } @@ -478,6 +564,14 @@ static int argv_a2arg(int tok, const char *argv) { + /* + * Parse an argument identifier from its text. XXX - this + * should really be table-driven to clarify the code. + * + * If you add an argument to the list, make sure that you + * register it here with its one or more macros! + */ + switch (tok) { case (MDOC_An): if (xstrcmp(argv, "split")) @@ -674,6 +768,34 @@ argv_multi(struct mdoc *mdoc, int line, static int +argv_opt_single(struct mdoc *mdoc, int line, + struct mdoc_arg *v, int *pos, char *buf) +{ + int c, ppos; + char *p; + + ppos = *pos; + + if ('-' == buf[*pos]) + return(1); + + c = args(mdoc, line, pos, buf, ARGS_QUOTED, &p); + if (ARGS_ERROR == c) + return(0); + if (ARGS_EOLN == c) + return(1); + + v->sz = 1; + v->value = xcalloc(1, sizeof(char *)); + v->value[0] = p; + return(1); +} + + +/* + * Parse a single, mandatory value from the stream. + */ +static int argv_single(struct mdoc *mdoc, int line, struct mdoc_arg *v, int *pos, char *buf) { @@ -686,7 +808,7 @@ argv_single(struct mdoc *mdoc, int line, if (ARGS_ERROR == c) return(0); if (ARGS_EOLN == c) - return(perr(mdoc, line, ppos, EARGVAL)); + return(perr(mdoc, line, ppos, EARGVAL)); v->sz = 1; v->value = xcalloc(1, sizeof(char *)); @@ -695,6 +817,11 @@ argv_single(struct mdoc *mdoc, int line, } +/* + * Determine rules for parsing arguments. Arguments can either accept + * no parameters, an optional single parameter, one parameter, or + * multiple parameters. + */ static int argv(struct mdoc *mdoc, int line, struct mdoc_arg *v, int *pos, char *buf) @@ -703,16 +830,15 @@ argv(struct mdoc *mdoc, int line, v->sz = 0; v->value = NULL; - switch (v->arg) { - case(MDOC_Std): - /* FALLTHROUGH */ - case(MDOC_Width): - /* FALLTHROUGH */ - case(MDOC_Offset): + switch (mdoc_argvflags[v->arg]) { + case (ARGV_SINGLE): return(argv_single(mdoc, line, v, pos, buf)); - case(MDOC_Column): + case (ARGV_MULTI): return(argv_multi(mdoc, line, v, pos, buf)); + case (ARGV_OPT_SINGLE): + return(argv_opt_single(mdoc, line, v, pos, buf)); default: + /* ARGV_NONE */ break; } @@ -720,6 +846,11 @@ argv(struct mdoc *mdoc, int line, } +/* + * Parse an argument from line text. This comes in the form of -key + * [value0...], which may either have a single mandatory value, at least + * one mandatory value, an optional single value, or no value. + */ int mdoc_argv(struct mdoc *mdoc, int line, int tok, struct mdoc_arg *v, int *pos, char *buf) @@ -756,6 +887,12 @@ mdoc_argv(struct mdoc *mdoc, int line, int tok, if (buf[*pos]) buf[(*pos)++] = 0; + /* + * We now parse out the per-macro arguments. XXX - this can be + * made much cleaner using per-argument tables. See argv_a2arg + * for details. + */ + if (MDOC_ARG_MAX == (v->arg = argv_a2arg(tok, p))) { if ( ! pwarn(mdoc, line, i, WARGVPARM)) return(ARGV_ERROR); @@ -31,6 +31,11 @@ * macro. */ +/* + * FIXME: don't use static mdoc_argv values, as they require us to do a + * complicated copy-over when actually assigning them to dynamic memory. + */ + #include "private.h" static int macro_obsolete(MACRO_PROT_ARGS); diff --git a/regress/test.argv.00 b/regress/test.argv.00 new file mode 100644 index 00000000..fb637c40 --- /dev/null +++ b/regress/test.argv.00 @@ -0,0 +1,8 @@ +.Dd $Mdocdate$ +.Dt test 1 +.Os +.Sh NAME +.Nm foo +.Nd bar +.Sh DESCRIPTION +.Ex -std diff --git a/regress/test.argv.01 b/regress/test.argv.01 new file mode 100644 index 00000000..60568fe2 --- /dev/null +++ b/regress/test.argv.01 @@ -0,0 +1,7 @@ +.Dd $Mdocdate$ +.Dt test 1 +.Os +.Sh NAME +.Nd bar +.Sh DESCRIPTION +.Ex -std |