summaryrefslogtreecommitdiff
path: root/cmd-line-utils/libedit/tokenizer.c
diff options
context:
space:
mode:
Diffstat (limited to 'cmd-line-utils/libedit/tokenizer.c')
-rw-r--r--cmd-line-utils/libedit/tokenizer.c61
1 files changed, 40 insertions, 21 deletions
diff --git a/cmd-line-utils/libedit/tokenizer.c b/cmd-line-utils/libedit/tokenizer.c
index 7a7e5b5ed75..f6892d9954c 100644
--- a/cmd-line-utils/libedit/tokenizer.c
+++ b/cmd-line-utils/libedit/tokenizer.c
@@ -1,4 +1,4 @@
-/* $NetBSD: tokenizer.c,v 1.7 2001/01/04 15:56:32 christos Exp $ */
+/* $NetBSD: tokenizer.c,v 1.11 2002/10/27 20:24:29 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -36,12 +36,18 @@
* SUCH DAMAGE.
*/
-#include "compat.h"
+#include "config.h"
+#if !defined(lint) && !defined(SCCSID)
+#if 0
+static char sccsid[] = "@(#)tokenizer.c 8.1 (Berkeley) 6/4/93";
+#else
+__RCSID("$NetBSD: tokenizer.c,v 1.11 2002/10/27 20:24:29 christos Exp $");
+#endif
+#endif /* not lint && not SCCSID */
/*
* tokenize.c: Bourne shell like tokenizer
*/
-#include "sys.h"
#include <string.h>
#include <stdlib.h>
#include "tokenizer.h"
@@ -66,7 +72,7 @@ typedef enum {
struct tokenizer {
char *ifs; /* In field separator */
int argc, amax; /* Current and maximum number of args */
- const char **argv; /* Argument list */
+ char **argv; /* Argument list */
char *wptr, *wmax; /* Space and limit on the word buffer */
char *wstart; /* Beginning of next word */
char *wspace; /* Space of word buffer */
@@ -103,16 +109,29 @@ tok_init(const char *ifs)
{
Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
+ if (tok == NULL)
+ return NULL;
tok->ifs = strdup(ifs ? ifs : IFS);
+ if (tok->ifs == NULL) {
+ tok_free((ptr_t)tok);
+ return NULL;
+ }
tok->argc = 0;
tok->amax = AINCR;
- tok->argv = (const char **) tok_malloc(sizeof(char *) * tok->amax);
- if (tok->argv == NULL)
- return (NULL);
+ tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
+ if (tok->argv == NULL) {
+ tok_free((ptr_t)tok->ifs);
+ tok_free((ptr_t)tok);
+ return NULL;
+ }
tok->argv[0] = NULL;
tok->wspace = (char *) tok_malloc(WINCR);
- if (tok->wspace == NULL)
- return (NULL);
+ if (tok->wspace == NULL) {
+ tok_free((ptr_t)tok->argv);
+ tok_free((ptr_t)tok->ifs);
+ tok_free((ptr_t)tok);
+ return NULL;
+ }
tok->wmax = tok->wspace + WINCR;
tok->wstart = tok->wspace;
tok->wptr = tok->wspace;
@@ -268,7 +287,7 @@ tok_line(Tokenizer *tok, const char *line, int *argc, const char ***argv)
switch (tok->quote) {
case Q_none:
tok_finish(tok);
- *argv = tok->argv;
+ *argv = (const char **)tok->argv;
*argc = tok->argc;
return (0);
@@ -301,7 +320,7 @@ tok_line(Tokenizer *tok, const char *line, int *argc, const char ***argv)
return (3);
}
tok_finish(tok);
- *argv = tok->argv;
+ *argv = (const char **)tok->argv;
*argc = tok->argc;
return (0);
@@ -363,25 +382,25 @@ tok_line(Tokenizer *tok, const char *line, int *argc, const char ***argv)
if (tok->wptr >= tok->wmax - 4) {
size_t size = tok->wmax - tok->wspace + WINCR;
char *s = (char *) tok_realloc(tok->wspace, size);
- /* SUPPRESS 22 */
- int offs = s - tok->wspace;
if (s == NULL)
return (-1);
- if (offs != 0) {
+ if (s != tok->wspace) {
int i;
- for (i = 0; i < tok->argc; i++)
- tok->argv[i] = tok->argv[i] + offs;
- tok->wptr = tok->wptr + offs;
- tok->wstart = tok->wstart + offs;
- tok->wmax = s + size;
+ for (i = 0; i < tok->argc; i++) {
+ tok->argv[i] =
+ (tok->argv[i] - tok->wspace) + s;
+ }
+ tok->wptr = (tok->wptr - tok->wspace) + s;
+ tok->wstart = (tok->wstart - tok->wspace) + s;
tok->wspace = s;
}
+ tok->wmax = s + size;
}
if (tok->argc >= tok->amax - 4) {
- const char **p;
+ char **p;
tok->amax += AINCR;
- p = (const char **) tok_realloc(tok->argv,
+ p = (char **) tok_realloc(tok->argv,
tok->amax * sizeof(char *));
if (p == NULL)
return (-1);