=================================================================== RCS file: /cvs/mandoc/mdoc_argv.c,v retrieving revision 1.28 retrieving revision 1.37 diff -u -p -r1.28 -r1.37 --- mandoc/mdoc_argv.c 2009/10/15 01:33:12 1.28 +++ mandoc/mdoc_argv.c 2010/04/03 14:02:10 1.37 @@ -1,4 +1,4 @@ -/* $Id: mdoc_argv.c,v 1.28 2009/10/15 01:33:12 kristaps Exp $ */ +/* $Id: mdoc_argv.c,v 1.37 2010/04/03 14:02:10 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -14,6 +14,10 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include @@ -23,6 +27,7 @@ #include #include "libmdoc.h" +#include "libmandoc.h" /* * Routines to parse arguments of macros. Arguments follow the syntax @@ -39,7 +44,7 @@ #define MULTI_STEP 5 -static int argv_a2arg(int, const char *); +static int argv_a2arg(enum mdoct, const char *); static int args(struct mdoc *, int, int *, char *, int, char **); static int argv(struct mdoc *, int, @@ -59,7 +64,7 @@ static int mdoc_argvflags[MDOC_ARG_MAX] = { ARGV_NONE, /* MDOC_Ragged */ ARGV_NONE, /* MDOC_Unfilled */ ARGV_NONE, /* MDOC_Literal */ - ARGV_NONE, /* MDOC_File */ + ARGV_SINGLE, /* MDOC_File */ ARGV_OPT_SINGLE, /* MDOC_Offset */ ARGV_NONE, /* MDOC_Bullet */ ARGV_NONE, /* MDOC_Dash */ @@ -203,6 +208,7 @@ static int mdoc_argflags[MDOC_MAX] = { 0, /* %Q */ 0, /* br */ 0, /* sp */ + 0, /* %U */ }; @@ -212,7 +218,7 @@ static int mdoc_argflags[MDOC_MAX] = { * one mandatory value, an optional single value, or no value. */ int -mdoc_argv(struct mdoc *m, int line, int tok, +mdoc_argv(struct mdoc *m, int line, enum mdoct tok, struct mdoc_arg **v, int *pos, char *buf) { char *p, sv; @@ -265,24 +271,13 @@ mdoc_argv(struct mdoc *m, int line, int tok, if ( ! argv(m, line, &tmp, pos, buf)) return(ARGV_ERROR); - if (NULL == (arg = *v)) { - *v = calloc(1, sizeof(struct mdoc_arg)); - if (NULL == *v) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(ARGV_ERROR); - } - arg = *v; - } + if (NULL == (arg = *v)) + arg = *v = mandoc_calloc(1, sizeof(struct mdoc_arg)); arg->argc++; - arg->argv = realloc(arg->argv, arg->argc * - sizeof(struct mdoc_argv)); + arg->argv = mandoc_realloc + (arg->argv, arg->argc * sizeof(struct mdoc_argv)); - if (NULL == arg->argv) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(ARGV_ERROR); - } - (void)memcpy(&arg->argv[(int)arg->argc - 1], &tmp, sizeof(struct mdoc_argv)); @@ -293,7 +288,7 @@ mdoc_argv(struct mdoc *m, int line, int tok, void mdoc_argv_free(struct mdoc_arg *p) { - int i, j; + int i; if (NULL == p) return; @@ -305,23 +300,28 @@ mdoc_argv_free(struct mdoc_arg *p) } assert(p->argc); - /* LINTED */ - for (i = 0; i < (int)p->argc; i++) { - if (0 == p->argv[i].sz) - continue; - if (NULL == p->argv[i].value) - continue; + for (i = (int)p->argc - 1; i >= 0; i--) + mdoc_argn_free(p, i); - /* LINTED */ - for (j = 0; j < (int)p->argv[i].sz; j++) - if (p->argv[i].value[j]) - free(p->argv[i].value[j]); + free(p->argv); + free(p); +} - free(p->argv[i].value); + +void +mdoc_argn_free(struct mdoc_arg *p, int iarg) +{ + struct mdoc_argv *arg = &p->argv[iarg]; + int j; + + if (arg->sz && arg->value) { + for (j = (int)arg->sz - 1; j >= 0; j--) + free(arg->value[j]); + free(arg->value); } - free(p->argv); - free(p); + for (--p->argc; iarg < (int)p->argc; iarg++) + p->argv[iarg] = p->argv[iarg+1]; } @@ -335,13 +335,13 @@ mdoc_zargs(struct mdoc *m, int line, int *pos, int -mdoc_args(struct mdoc *m, int line, - int *pos, char *buf, int tok, char **v) +mdoc_args(struct mdoc *m, int line, int *pos, + char *buf, enum mdoct tok, char **v) { int fl, c, i; struct mdoc_node *n; - fl = (0 == tok) ? 0 : mdoc_argflags[tok]; + fl = mdoc_argflags[tok]; if (MDOC_It != tok) return(args(m, line, pos, buf, fl, v)); @@ -406,9 +406,9 @@ args(struct mdoc *m, int line, int *pos, * follows the pattern of [[::delim::][ ]+]+. */ - if ((fl & ARGS_DELIM) && mdoc_iscdelim(buf[*pos])) { + if ((fl & ARGS_DELIM) && mdoc_iscdelim(buf[*pos]) > 1) { for (i = *pos; buf[i]; ) { - if ( ! mdoc_iscdelim(buf[i])) + if ( mdoc_iscdelim(buf[i]) < 2) break; i++; if (0 == buf[i] || ' ' != buf[i]) @@ -557,7 +557,7 @@ args(struct mdoc *m, int line, int *pos, static int -argv_a2arg(int tok, const char *p) +argv_a2arg(enum mdoct tok, const char *p) { /* @@ -672,16 +672,11 @@ argv_multi(struct mdoc *m, int line, else if (ARGS_EOLN == c) break; - if (0 == v->sz % MULTI_STEP) { - v->value = realloc(v->value, + if (0 == v->sz % MULTI_STEP) + v->value = mandoc_realloc(v->value, (v->sz + MULTI_STEP) * sizeof(char *)); - if (NULL == v->value) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(ARGV_ERROR); - } - } - if (NULL == (v->value[(int)v->sz] = strdup(p))) - return(mdoc_nerr(m, m->last, EMALLOC)); + + v->value[(int)v->sz] = mandoc_strdup(p); } return(1); @@ -705,10 +700,8 @@ argv_opt_single(struct mdoc *m, int line, return(1); v->sz = 1; - if (NULL == (v->value = calloc(1, sizeof(char *)))) - return(mdoc_nerr(m, m->last, EMALLOC)); - if (NULL == (v->value[0] = strdup(p))) - return(mdoc_nerr(m, m->last, EMALLOC)); + v->value = mandoc_malloc(sizeof(char *)); + v->value[0] = mandoc_strdup(p); return(1); } @@ -733,10 +726,8 @@ argv_single(struct mdoc *m, int line, return(mdoc_perr(m, line, ppos, EARGVAL)); v->sz = 1; - if (NULL == (v->value = calloc(1, sizeof(char *)))) - return(mdoc_nerr(m, m->last, EMALLOC)); - if (NULL == (v->value[0] = strdup(p))) - return(mdoc_nerr(m, m->last, EMALLOC)); + v->value = mandoc_malloc(sizeof(char *)); + v->value[0] = mandoc_strdup(p); return(1); }