=================================================================== RCS file: /cvs/mandoc/Attic/argv.c,v retrieving revision 1.50 retrieving revision 1.56 diff -u -p -r1.50 -r1.56 --- mandoc/Attic/argv.c 2009/03/12 16:30:50 1.50 +++ mandoc/Attic/argv.c 2009/03/23 14:22:11 1.56 @@ -1,6 +1,6 @@ -/* $Id: argv.c,v 1.50 2009/03/12 16:30:50 kristaps Exp $ */ +/* $Id: argv.c,v 1.56 2009/03/23 14:22:11 kristaps Exp $ */ /* - * Copyright (c) 2008 Kristaps Dzonsons + * Copyright (c) 2008, 2009 Kristaps Dzonsons * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the @@ -25,7 +25,7 @@ #include #include -#include "private.h" +#include "libmdoc.h" /* * Routines to parse arguments of macros. Arguments follow the syntax @@ -45,6 +45,8 @@ #define ARGV_MULTI (1 << 2) #define ARGV_OPT_SINGLE (1 << 3) +#define MULTI_STEP 5 + enum mwarn { WQUOTPARM, WARGVPARM, @@ -60,7 +62,7 @@ enum merr { static int argv_a2arg(int, const char *); static int args(struct mdoc *, int, int *, char *, int, char **); -static int argv(struct mdoc *, int, int, +static int argv(struct mdoc *, int, struct mdoc_argv *, int *, char *); static int argv_single(struct mdoc *, int, struct mdoc_argv *, int *, char *); @@ -218,6 +220,10 @@ static int mdoc_argflags[MDOC_MAX] = { 0, /* Bro */ ARGS_DELIM, /* Brc */ ARGS_QUOTED, /* %C */ + 0, /* Es */ + 0, /* En */ + 0, /* Dx */ + ARGS_QUOTED, /* %Q */ }; @@ -238,11 +244,13 @@ mdoc_argv(struct mdoc *mdoc, int line, int tok, if (0 == buf[*pos]) return(ARGV_EOLN); - assert( ! isspace((u_char)buf[*pos])); + assert(' ' != buf[*pos]); - if ('-' != buf[*pos]) + if ('-' != buf[*pos] || ARGS_ARGVLIKE & mdoc_argflags[tok]) return(ARGV_WORD); + /* Parse through to the first unescaped space. */ + i = *pos; p = &buf[++(*pos)]; @@ -250,17 +258,13 @@ mdoc_argv(struct mdoc *mdoc, int line, int tok, /* LINTED */ while (buf[*pos]) { - if (isspace((u_char)buf[*pos])) + if (' ' == buf[*pos]) if ('\\' != buf[*pos - 1]) break; (*pos)++; } - /* - * XXX: save the nullified byte as we'll restore it if this - * doesn't end up being a command after all. This is a little - * bit hacky. I don't like it, but it works for now. - */ + /* XXX - save zeroed byte, if not an argument. */ sv = 0; if (buf[*pos]) { @@ -272,14 +276,10 @@ mdoc_argv(struct mdoc *mdoc, int line, int tok, tmp.line = line; tmp.pos = *pos; - /* - * We now parse out the per-macro arguments. XXX - this can be - * made much cleaner using per-argument tables. See argv_a2arg - * for details. - */ + /* See if our token accepts the argument. */ if (MDOC_ARG_MAX == (tmp.arg = argv_a2arg(tok, p))) { - /* XXX - restore saved byte. */ + /* XXX - restore saved zeroed byte. */ if (sv) buf[*pos - 1] = sv; if ( ! pwarn(mdoc, line, i, WARGVPARM)) @@ -287,16 +287,15 @@ mdoc_argv(struct mdoc *mdoc, int line, int tok, return(ARGV_WORD); } - while (buf[*pos] && isspace((u_char)buf[*pos])) + while (buf[*pos] && ' ' == buf[*pos]) (*pos)++; - /* FIXME: whitespace if no value. */ - - if ( ! argv(mdoc, tok, line, &tmp, pos, buf)) + if ( ! argv(mdoc, line, &tmp, pos, buf)) return(ARGV_ERROR); if (NULL == (arg = *v)) { - *v = xcalloc(1, sizeof(struct mdoc_arg)); + if (NULL == (*v = calloc(1, sizeof(struct mdoc_arg)))) + err(1, "calloc"); arg = *v; } @@ -332,7 +331,7 @@ mdoc_argv_free(struct mdoc_arg *p) if (0 == p->argv[i].sz) continue; /* LINTED */ - for (j = 0; j < (int)p->argv[i].sz; j++) + for (j = 0; j < (int)p->argv[i].sz; j++) free(p->argv[i].value[j]); free(p->argv[i].value); @@ -448,8 +447,6 @@ mdoc_args(struct mdoc *mdoc, int line, break; } - /* Continue parsing the arguments themselves... */ - return(args(mdoc, line, pos, buf, fl, v)); } @@ -486,10 +483,10 @@ args(struct mdoc *mdoc, int line, break; i++; /* There must be at least one space... */ - if (0 == buf[i] || ! isspace((u_char)buf[i])) + if (0 == buf[i] || ' ' != buf[i]) break; i++; - while (buf[i] && isspace((u_char)buf[i])) + while (buf[i] && ' ' == buf[i]) i++; } if (0 == buf[i]) { @@ -505,24 +502,16 @@ args(struct mdoc *mdoc, int line, /* * Thar be dragons here! If we're tab-separated, search - * ahead for either a tab or the `Ta' macro. If a tab - * is detected, it mustn't be escaped; if a `Ta' is - * detected, it must be space-buffered before and after. - * If either of these hold true, then prune out the + * ahead for either a tab or the `Ta' macro. + * If a `Ta' is detected, it must be space-buffered before and + * after. If either of these hold true, then prune out the * extra spaces and call it an argument. */ if (ARGS_TABSEP & fl) { /* Scan ahead to unescaped tab. */ - for (p = *v; ; p++) { - if (NULL == (p = strchr(p, '\t'))) - break; - if (p == *v) - break; - if ('\\' != *(p - 1)) - break; - } + p = strchr(*v, '\t'); /* Scan ahead to unescaped `Ta'. */ @@ -597,7 +586,7 @@ args(struct mdoc *mdoc, int line, if ( ! (ARGS_TABSEP & fl)) while (buf[*pos]) { - if (isspace((u_char)buf[*pos])) + if (' ' == buf[*pos]) if ('\\' != buf[*pos - 1]) break; (*pos)++; @@ -612,7 +601,7 @@ args(struct mdoc *mdoc, int line, return(ARGS_WORD); if ( ! (ARGS_TABSEP & fl)) - while (buf[*pos] && isspace((u_char)buf[*pos])) + while (buf[*pos] && ' ' == buf[*pos]) (*pos)++; if (buf[*pos]) @@ -644,7 +633,7 @@ args(struct mdoc *mdoc, int line, if (0 == buf[*pos]) return(ARGS_QWORD); - while (buf[*pos] && isspace((u_char)buf[*pos])) + while (buf[*pos] && ' ' == buf[*pos]) (*pos)++; if (buf[*pos]) @@ -773,9 +762,9 @@ argv_multi(struct mdoc *mdoc, int line, else if (ARGS_EOLN == c) break; - if (0 == v->sz % 5) + if (0 == v->sz % MULTI_STEP) v->value = xrealloc(v->value, - (v->sz + 5) * sizeof(char *)); + (v->sz + MULTI_STEP) * sizeof(char *)); v->value[(int)v->sz] = xstrdup(p); } @@ -804,8 +793,10 @@ argv_opt_single(struct mdoc *mdoc, int line, return(1); v->sz = 1; - v->value = xcalloc(1, sizeof(char *)); - v->value[0] = xstrdup(p); + if (NULL == (v->value = calloc(1, sizeof(char *)))) + err(1, "calloc"); + if (NULL == (v->value[0] = strdup(p))) + err(1, "strdup"); return(1); } @@ -829,8 +820,10 @@ argv_single(struct mdoc *mdoc, int line, return(perr(mdoc, line, ppos, EARGVAL)); v->sz = 1; - v->value = xcalloc(1, sizeof(char *)); - v->value[0] = xstrdup(p); + if (NULL == (v->value = calloc(1, sizeof(char *)))) + err(1, "calloc"); + if (NULL == (v->value[0] = strdup(p))) + err(1, "strdup"); return(1); } @@ -841,7 +834,7 @@ argv_single(struct mdoc *mdoc, int line, * multiple parameters. */ static int -argv(struct mdoc *mdoc, int tok, int line, +argv(struct mdoc *mdoc, int line, struct mdoc_argv *v, int *pos, char *buf) {