36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] =
"@(#)tokenizer.c 8.1 (Berkeley) 6/4/93";
53 Q_none, Q_single, Q_double, Q_one, Q_doubleone
62 #define IFS STR("\t \n")
64 #define tok_malloc(a) malloc(a)
65 #define tok_free(a) free(a)
66 #define tok_realloc(a, b) realloc(a, b)
67 #define tok_strdup(a) Strdup(a)
70 struct TYPE(tokenizer) {
82 private void FUN(tok,finish)(TYPE(Tokenizer) *);
89 FUN(tok,finish)(TYPE(Tokenizer) *tok)
93 if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
94 tok->argv[tok->argc++] = tok->wstart;
95 tok->argv[tok->argc] = NULL;
96 tok->wstart = ++tok->wptr;
98 tok->flags &= ~TOK_KEEP;
105 public TYPE(Tokenizer) *
106 FUN(tok,init)(
const Char *ifs)
108 TYPE(Tokenizer) *tok = tok_malloc(
sizeof(*tok));
112 tok->ifs = tok_strdup(ifs ? ifs : IFS);
113 if (tok->ifs == NULL) {
119 tok->argv = tok_malloc(
sizeof(*tok->argv) * tok->amax);
120 if (tok->argv == NULL) {
126 tok->wspace = tok_malloc(WINCR *
sizeof(*tok->wspace));
127 if (tok->wspace == NULL) {
133 tok->wmax = tok->wspace + WINCR;
134 tok->wstart = tok->wspace;
135 tok->wptr = tok->wspace;
147 FUN(tok,reset)(TYPE(Tokenizer) *tok)
151 tok->wstart = tok->wspace;
152 tok->wptr = tok->wspace;
162 FUN(tok,end)(TYPE(Tokenizer) *tok)
166 tok_free(tok->wspace);
191 FUN(tok,line)(TYPE(Tokenizer) *tok,
const TYPE(
LineInfo) *line,
192 int *argc,
const Char ***argv,
int *cursorc,
int *cursoro)
199 for (ptr = line->buffer; ;ptr++) {
200 if (ptr >= line->lastchar)
202 if (ptr == line->cursor) {
204 co = (int)(tok->wptr - tok->wstart);
208 tok->flags |= TOK_KEEP;
209 tok->flags &= ~TOK_EAT;
210 switch (tok->quote) {
212 tok->quote = Q_single;
230 tok->quote = Q_double;
240 tok->flags &= ~TOK_EAT;
241 tok->flags |= TOK_KEEP;
242 switch (tok->quote) {
244 tok->quote = Q_double;
261 tok->quote = Q_double;
271 tok->flags |= TOK_KEEP;
272 tok->flags &= ~TOK_EAT;
273 switch (tok->quote) {
279 tok->quote = Q_doubleone;
292 tok->quote = Q_double;
302 tok->flags &= ~TOK_EAT;
303 switch (tok->quote) {
313 tok->flags |= TOK_EAT;
314 tok->quote = Q_double;
318 tok->flags |= TOK_EAT;
328 switch (tok->quote) {
331 if (tok->flags & TOK_EAT) {
332 tok->flags &= ~TOK_EAT;
344 tok->quote = Q_double;
359 tok->flags &= ~TOK_EAT;
360 switch (tok->quote) {
362 if (Strchr(tok->ifs, *ptr) != NULL)
363 FUN(tok,finish)(tok);
376 tok->quote = Q_double;
392 if (tok->wptr >= tok->wmax - 4) {
393 size_t size = tok->wmax - tok->wspace + WINCR;
394 Char *s = tok_realloc(tok->wspace,
399 if (s != tok->wspace) {
401 for (i = 0; i < tok->argc; i++) {
403 (tok->argv[
i] - tok->wspace) + s;
405 tok->wptr = (tok->wptr - tok->wspace) + s;
406 tok->wstart = (tok->wstart - tok->wspace) + s;
409 tok->wmax = s +
size;
411 if (tok->argc >= tok->amax - 4) {
414 p = tok_realloc(tok->argv, tok->amax *
sizeof(*p));
421 if (cc == -1 && co == -1) {
423 co = (int)(tok->wptr - tok->wstart);
429 FUN(tok,finish)(tok);
430 *argv = (
const Char **)tok->argv;
440 FUN(tok,str)(TYPE(Tokenizer) *tok,
const Char *line,
int *argc,
445 memset(&li, 0, sizeof(li));
447 li.
cursor = li.lastchar = Strchr(line, '\0');
448 return (FUN(tok,line)(tok, &li, argc, argv, NULL, NULL));