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.c111
1 files changed, 57 insertions, 54 deletions
diff --git a/cmd-line-utils/libedit/tokenizer.c b/cmd-line-utils/libedit/tokenizer.c
index 5161cdd0a22..7267164e9b6 100644
--- a/cmd-line-utils/libedit/tokenizer.c
+++ b/cmd-line-utils/libedit/tokenizer.c
@@ -1,4 +1,4 @@
-/* $NetBSD: tokenizer.c,v 1.14 2003/12/05 13:37:48 lukem Exp $ */
+/* $NetBSD: tokenizer.c,v 1.19 2011/07/28 20:50:55 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -40,51 +40,53 @@ static char sccsid[] = "@(#)tokenizer.c 8.1 (Berkeley) 6/4/93";
#endif
#endif /* not lint && not SCCSID */
+/* We build this file twice, once as NARROW, once as WIDE. */
/*
* tokenize.c: Bourne shell like tokenizer
*/
#include <string.h>
#include <stdlib.h>
#include "histedit.h"
+#include "chartype.h"
typedef enum {
Q_none, Q_single, Q_double, Q_one, Q_doubleone
} quote_t;
-#define IFS "\t \n"
-
#define TOK_KEEP 1
#define TOK_EAT 2
#define WINCR 20
#define AINCR 10
-#define tok_strdup(a) strdup(a)
+#define IFS STR("\t \n")
+
#define tok_malloc(a) malloc(a)
#define tok_free(a) free(a)
#define tok_realloc(a, b) realloc(a, b)
+#define tok_strdup(a) Strdup(a)
-struct tokenizer {
- char *ifs; /* In field separator */
+struct TYPE(tokenizer) {
+ Char *ifs; /* In field separator */
int argc, amax; /* Current and maximum number of args */
- 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 */
+ 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 */
quote_t quote; /* Quoting state */
int flags; /* flags; */
};
-private void tok_finish(Tokenizer *);
+private void FUN(tok,finish)(TYPE(Tokenizer) *);
-/* tok_finish():
+/* FUN(tok,finish)():
* Finish a word in the tokenizer.
*/
private void
-tok_finish(Tokenizer *tok)
+FUN(tok,finish)(TYPE(Tokenizer) *tok)
{
*tok->wptr = '\0';
@@ -97,35 +99,35 @@ tok_finish(Tokenizer *tok)
}
-/* tok_init():
+/* FUN(tok,init)():
* Initialize the tokenizer
*/
-public Tokenizer *
-tok_init(const char *ifs)
+public TYPE(Tokenizer) *
+FUN(tok,init)(const Char *ifs)
{
- Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
+ TYPE(Tokenizer) *tok = tok_malloc(sizeof(*tok));
if (tok == NULL)
return NULL;
tok->ifs = tok_strdup(ifs ? ifs : IFS);
if (tok->ifs == NULL) {
- tok_free((ptr_t)tok);
+ tok_free(tok);
return NULL;
}
tok->argc = 0;
tok->amax = AINCR;
- tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
+ tok->argv = tok_malloc(sizeof(*tok->argv) * tok->amax);
if (tok->argv == NULL) {
- tok_free((ptr_t)tok->ifs);
- tok_free((ptr_t)tok);
+ tok_free(tok->ifs);
+ tok_free(tok);
return NULL;
}
tok->argv[0] = NULL;
- tok->wspace = (char *) tok_malloc(WINCR);
+ tok->wspace = tok_malloc(WINCR * sizeof(*tok->wspace));
if (tok->wspace == NULL) {
- tok_free((ptr_t)tok->argv);
- tok_free((ptr_t)tok->ifs);
- tok_free((ptr_t)tok);
+ tok_free(tok->argv);
+ tok_free(tok->ifs);
+ tok_free(tok);
return NULL;
}
tok->wmax = tok->wspace + WINCR;
@@ -138,11 +140,11 @@ tok_init(const char *ifs)
}
-/* tok_reset():
+/* FUN(tok,reset)():
* Reset the tokenizer
*/
public void
-tok_reset(Tokenizer *tok)
+FUN(tok,reset)(TYPE(Tokenizer) *tok)
{
tok->argc = 0;
@@ -153,25 +155,25 @@ tok_reset(Tokenizer *tok)
}
-/* tok_end():
+/* FUN(tok,end)():
* Clean up
*/
public void
-tok_end(Tokenizer *tok)
+FUN(tok,end)(TYPE(Tokenizer) *tok)
{
- tok_free((ptr_t) tok->ifs);
- tok_free((ptr_t) tok->wspace);
- tok_free((ptr_t) tok->argv);
- tok_free((ptr_t) tok);
+ tok_free(tok->ifs);
+ tok_free(tok->wspace);
+ tok_free(tok->argv);
+ tok_free(tok);
}
-/* tok_line():
+/* FUN(tok,line)():
* Bourne shell (sh(1)) like tokenizing
* Arguments:
- * tok current tokenizer state (setup with tok_init())
+ * tok current tokenizer state (setup with FUN(tok,init)())
* line line to parse
* Returns:
* -1 Internal error
@@ -186,20 +188,20 @@ tok_end(Tokenizer *tok)
* cursorv if !NULL, offset in argv[cursorc] of cursor
*/
public int
-tok_line(Tokenizer *tok, const LineInfo *line,
- int *argc, const char ***argv, int *cursorc, int *cursoro)
+FUN(tok,line)(TYPE(Tokenizer) *tok, const TYPE(LineInfo) *line,
+ int *argc, const Char ***argv, int *cursorc, int *cursoro)
{
- const char *ptr;
+ const Char *ptr;
int cc, co;
cc = co = -1;
ptr = line->buffer;
for (ptr = line->buffer; ;ptr++) {
if (ptr >= line->lastchar)
- ptr = "";
+ ptr = STR("");
if (ptr == line->cursor) {
cc = tok->argc;
- co = tok->wptr - tok->wstart;
+ co = (int)(tok->wptr - tok->wstart);
}
switch (*ptr) {
case '\'':
@@ -357,8 +359,8 @@ tok_line(Tokenizer *tok, const LineInfo *line,
tok->flags &= ~TOK_EAT;
switch (tok->quote) {
case Q_none:
- if (strchr(tok->ifs, *ptr) != NULL)
- tok_finish(tok);
+ if (Strchr(tok->ifs, *ptr) != NULL)
+ FUN(tok,finish)(tok);
else
*tok->wptr++ = *ptr;
break;
@@ -389,7 +391,8 @@ tok_line(Tokenizer *tok, const LineInfo *line,
if (tok->wptr >= tok->wmax - 4) {
size_t size = tok->wmax - tok->wspace + WINCR;
- char *s = (char *) tok_realloc(tok->wspace, size);
+ Char *s = tok_realloc(tok->wspace,
+ size * sizeof(*s));
if (s == NULL)
return (-1);
@@ -406,10 +409,9 @@ tok_line(Tokenizer *tok, const LineInfo *line,
tok->wmax = s + size;
}
if (tok->argc >= tok->amax - 4) {
- char **p;
+ Char **p;
tok->amax += AINCR;
- p = (char **) tok_realloc(tok->argv,
- tok->amax * sizeof(char *));
+ p = tok_realloc(tok->argv, tok->amax * sizeof(*p));
if (p == NULL)
return (-1);
tok->argv = p;
@@ -418,29 +420,30 @@ tok_line(Tokenizer *tok, const LineInfo *line,
tok_line_outok:
if (cc == -1 && co == -1) {
cc = tok->argc;
- co = tok->wptr - tok->wstart;
+ co = (int)(tok->wptr - tok->wstart);
}
if (cursorc != NULL)
*cursorc = cc;
if (cursoro != NULL)
*cursoro = co;
- tok_finish(tok);
- *argv = (const char **)tok->argv;
+ FUN(tok,finish)(tok);
+ *argv = (const Char **)tok->argv;
*argc = tok->argc;
return (0);
}
-/* tok_str():
+/* FUN(tok,str)():
* Simpler version of tok_line, taking a NUL terminated line
* and splitting into words, ignoring cursor state.
*/
public int
-tok_str(Tokenizer *tok, const char *line, int *argc, const char ***argv)
+FUN(tok,str)(TYPE(Tokenizer) *tok, const Char *line, int *argc,
+ const Char ***argv)
{
- LineInfo li;
+ TYPE(LineInfo) li;
memset(&li, 0, sizeof(li));
li.buffer = line;
- li.cursor = li.lastchar = strchr(line, '\0');
- return (tok_line(tok, &li, argc, argv, NULL, NULL));
+ li.cursor = li.lastchar = Strchr(line, '\0');
+ return (FUN(tok,line)(tok, &li, argc, argv, NULL, NULL));
}