-/* $Id: mdoc_argv.c,v 1.30 2009/10/24 05:52:13 kristaps Exp $ */
+/* $Id: mdoc_argv.c,v 1.37 2010/04/03 14:02:10 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
* 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 <sys/types.h>
#include <assert.h>
#include <string.h>
#include "libmdoc.h"
+#include "libmandoc.h"
/*
* Routines to parse arguments of macros. Arguments follow the syntax
#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,
* 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;
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));
-
- if (NULL == arg->argv) {
- (void)mdoc_nerr(m, m->last, EMALLOC);
- return(ARGV_ERROR);
- }
+ arg->argv = mandoc_realloc
+ (arg->argv, arg->argc * sizeof(struct mdoc_argv));
(void)memcpy(&arg->argv[(int)arg->argc - 1],
&tmp, sizeof(struct mdoc_argv));
void
mdoc_argv_free(struct mdoc_arg *p)
{
- int i, j;
+ int i;
if (NULL == p)
return;
}
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);
+
+ free(p->argv);
+ free(p);
+}
- /* 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[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];
}
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));
* 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])
static int
-argv_a2arg(int tok, const char *p)
+argv_a2arg(enum mdoct tok, const char *p)
{
/*
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);
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);
}
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);
}