1
/* $NetBSD: tokenizer.c,v 1.14 2003/12/05 13:37:48 lukem Exp $ */
1
/* $NetBSD: tokenizer.c,v 1.19 2011/07/28 20:50:55 christos Exp $ */
4
4
* Copyright (c) 1992, 1993
41
41
#endif /* not lint && not SCCSID */
43
/* We build this file twice, once as NARROW, once as WIDE. */
44
45
* tokenize.c: Bourne shell like tokenizer
46
47
#include <string.h>
47
48
#include <stdlib.h>
48
49
#include "histedit.h"
51
53
Q_none, Q_single, Q_double, Q_one, Q_doubleone
62
#define tok_strdup(a) strdup(a)
62
#define IFS STR("\t \n")
63
64
#define tok_malloc(a) malloc(a)
64
65
#define tok_free(a) free(a)
65
66
#define tok_realloc(a, b) realloc(a, b)
69
char *ifs; /* In field separator */
67
#define tok_strdup(a) Strdup(a)
70
struct TYPE(tokenizer) {
71
Char *ifs; /* In field separator */
70
72
int argc, amax; /* Current and maximum number of args */
71
char **argv; /* Argument list */
72
char *wptr, *wmax; /* Space and limit on the word buffer */
73
char *wstart; /* Beginning of next word */
74
char *wspace; /* Space of word buffer */
73
Char **argv; /* Argument list */
74
Char *wptr, *wmax; /* Space and limit on the word buffer */
75
Char *wstart; /* Beginning of next word */
76
Char *wspace; /* Space of word buffer */
75
77
quote_t quote; /* Quoting state */
76
78
int flags; /* flags; */
80
private void tok_finish(Tokenizer *);
82
private void FUN(tok,finish)(TYPE(Tokenizer) *);
84
86
* Finish a word in the tokenizer.
87
tok_finish(Tokenizer *tok)
89
FUN(tok,finish)(TYPE(Tokenizer) *tok)
101
103
* Initialize the tokenizer
104
tok_init(const char *ifs)
105
public TYPE(Tokenizer) *
106
FUN(tok,init)(const Char *ifs)
106
Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
108
TYPE(Tokenizer) *tok = tok_malloc(sizeof(*tok));
110
112
tok->ifs = tok_strdup(ifs ? ifs : IFS);
111
113
if (tok->ifs == NULL) {
112
tok_free((ptr_t)tok);
116
118
tok->amax = AINCR;
117
tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
119
tok->argv = tok_malloc(sizeof(*tok->argv) * tok->amax);
118
120
if (tok->argv == NULL) {
119
tok_free((ptr_t)tok->ifs);
120
tok_free((ptr_t)tok);
123
125
tok->argv[0] = NULL;
124
tok->wspace = (char *) tok_malloc(WINCR);
126
tok->wspace = tok_malloc(WINCR * sizeof(*tok->wspace));
125
127
if (tok->wspace == NULL) {
126
tok_free((ptr_t)tok->argv);
127
tok_free((ptr_t)tok->ifs);
128
tok_free((ptr_t)tok);
131
133
tok->wmax = tok->wspace + WINCR;
160
tok_end(Tokenizer *tok)
162
FUN(tok,end)(TYPE(Tokenizer) *tok)
163
tok_free((ptr_t) tok->ifs);
164
tok_free((ptr_t) tok->wspace);
165
tok_free((ptr_t) tok->argv);
166
tok_free((ptr_t) tok);
166
tok_free(tok->wspace);
172
174
* Bourne shell (sh(1)) like tokenizing
174
* tok current tokenizer state (setup with tok_init())
176
* tok current tokenizer state (setup with FUN(tok,init)())
175
177
* line line to parse
177
179
* -1 Internal error
186
188
* cursorv if !NULL, offset in argv[cursorc] of cursor
189
tok_line(Tokenizer *tok, const LineInfo *line,
190
int *argc, const char ***argv, int *cursorc, int *cursoro)
191
FUN(tok,line)(TYPE(Tokenizer) *tok, const TYPE(LineInfo) *line,
192
int *argc, const Char ***argv, int *cursorc, int *cursoro)
196
198
ptr = line->buffer;
197
199
for (ptr = line->buffer; ;ptr++) {
198
200
if (ptr >= line->lastchar)
200
202
if (ptr == line->cursor) {
202
co = tok->wptr - tok->wstart;
204
co = (int)(tok->wptr - tok->wstart);
390
392
if (tok->wptr >= tok->wmax - 4) {
391
393
size_t size = tok->wmax - tok->wspace + WINCR;
392
char *s = (char *) tok_realloc(tok->wspace, size);
394
Char *s = tok_realloc(tok->wspace,
406
409
tok->wmax = s + size;
408
411
if (tok->argc >= tok->amax - 4) {
410
413
tok->amax += AINCR;
411
p = (char **) tok_realloc(tok->argv,
412
tok->amax * sizeof(char *));
414
p = tok_realloc(tok->argv, tok->amax * sizeof(*p));
419
421
if (cc == -1 && co == -1) {
421
co = tok->wptr - tok->wstart;
423
co = (int)(tok->wptr - tok->wstart);
423
425
if (cursorc != NULL)
425
427
if (cursoro != NULL)
428
*argv = (const char **)tok->argv;
429
FUN(tok,finish)(tok);
430
*argv = (const Char **)tok->argv;
429
431
*argc = tok->argc;
434
436
* Simpler version of tok_line, taking a NUL terminated line
435
437
* and splitting into words, ignoring cursor state.
438
tok_str(Tokenizer *tok, const char *line, int *argc, const char ***argv)
440
FUN(tok,str)(TYPE(Tokenizer) *tok, const Char *line, int *argc,
442
445
memset(&li, 0, sizeof(li));
443
446
li.buffer = line;
444
li.cursor = li.lastchar = strchr(line, '\0');
445
return (tok_line(tok, &li, argc, argv, NULL, NULL));
447
li.cursor = li.lastchar = Strchr(line, '\0');
448
return (FUN(tok,line)(tok, &li, argc, argv, NULL, NULL));