[BACK]Return to tokenizer.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libedit

Annotation of src/lib/libedit/tokenizer.c, Revision 1.14

1.14    ! lukem       1: /*     $NetBSD: tokenizer.c,v 1.13 2003/10/18 23:48:42 christos Exp $  */
1.2       lukem       2:
1.1       cgd         3: /*-
                      4:  * Copyright (c) 1992, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Christos Zoulas of Cornell University.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
1.12      agc        18:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        19:  *    may be used to endorse or promote products derived from this software
                     20:  *    without specific prior written permission.
                     21:  *
                     22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     32:  * SUCH DAMAGE.
                     33:  */
                     34:
1.10      christos   35: #include "config.h"
1.1       cgd        36: #if !defined(lint) && !defined(SCCSID)
1.2       lukem      37: #if 0
1.1       cgd        38: static char sccsid[] = "@(#)tokenizer.c        8.1 (Berkeley) 6/4/93";
1.2       lukem      39: #else
1.14    ! lukem      40: __RCSID("$NetBSD: tokenizer.c,v 1.13 2003/10/18 23:48:42 christos Exp $");
1.2       lukem      41: #endif
1.1       cgd        42: #endif /* not lint && not SCCSID */
                     43:
                     44: /*
                     45:  * tokenize.c: Bourne shell like tokenizer
                     46:  */
                     47: #include <string.h>
                     48: #include <stdlib.h>
1.14    ! lukem      49: #include "histedit.h"
1.1       cgd        50:
1.6       lukem      51: typedef enum {
                     52:        Q_none, Q_single, Q_double, Q_one, Q_doubleone
                     53: } quote_t;
1.1       cgd        54:
1.6       lukem      55: #define        IFS             "\t \n"
1.1       cgd        56:
1.6       lukem      57: #define        TOK_KEEP        1
                     58: #define        TOK_EAT         2
1.1       cgd        59:
1.6       lukem      60: #define        WINCR           20
                     61: #define        AINCR           10
1.1       cgd        62:
1.13      christos   63: #define        tok_strdup(a)           strdup(a)
1.6       lukem      64: #define        tok_malloc(a)           malloc(a)
                     65: #define        tok_free(a)             free(a)
                     66: #define        tok_realloc(a, b)       realloc(a, b)
1.1       cgd        67:
                     68:
                     69: struct tokenizer {
1.6       lukem      70:        char    *ifs;           /* In field separator                    */
                     71:        int      argc, amax;    /* Current and maximum number of args    */
                     72:        char   **argv;          /* Argument list                         */
                     73:        char    *wptr, *wmax;   /* Space and limit on the word buffer    */
                     74:        char    *wstart;        /* Beginning of next word                */
                     75:        char    *wspace;        /* Space of word buffer                  */
                     76:        quote_t  quote;         /* Quoting state                         */
                     77:        int      flags;         /* flags;                                */
1.1       cgd        78: };
                     79:
                     80:
1.6       lukem      81: private void tok_finish(Tokenizer *);
1.1       cgd        82:
                     83:
                     84: /* tok_finish():
                     85:  *     Finish a word in the tokenizer.
                     86:  */
                     87: private void
1.6       lukem      88: tok_finish(Tokenizer *tok)
1.1       cgd        89: {
1.6       lukem      90:
                     91:        *tok->wptr = '\0';
                     92:        if ((tok->flags & TOK_KEEP) || tok->wptr != tok->wstart) {
                     93:                tok->argv[tok->argc++] = tok->wstart;
                     94:                tok->argv[tok->argc] = NULL;
                     95:                tok->wstart = ++tok->wptr;
                     96:        }
                     97:        tok->flags &= ~TOK_KEEP;
1.1       cgd        98: }
                     99:
                    100:
                    101: /* tok_init():
                    102:  *     Initialize the tokenizer
                    103:  */
                    104: public Tokenizer *
1.6       lukem     105: tok_init(const char *ifs)
1.1       cgd       106: {
1.6       lukem     107:        Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
1.1       cgd       108:
1.11      christos  109:        if (tok == NULL)
                    110:                return NULL;
1.13      christos  111:        tok->ifs = tok_strdup(ifs ? ifs : IFS);
1.11      christos  112:        if (tok->ifs == NULL) {
                    113:                tok_free((ptr_t)tok);
                    114:                return NULL;
                    115:        }
1.6       lukem     116:        tok->argc = 0;
                    117:        tok->amax = AINCR;
                    118:        tok->argv = (char **) tok_malloc(sizeof(char *) * tok->amax);
1.11      christos  119:        if (tok->argv == NULL) {
                    120:                tok_free((ptr_t)tok->ifs);
                    121:                tok_free((ptr_t)tok);
                    122:                return NULL;
                    123:        }
1.6       lukem     124:        tok->argv[0] = NULL;
                    125:        tok->wspace = (char *) tok_malloc(WINCR);
1.11      christos  126:        if (tok->wspace == NULL) {
                    127:                tok_free((ptr_t)tok->argv);
                    128:                tok_free((ptr_t)tok->ifs);
                    129:                tok_free((ptr_t)tok);
                    130:                return NULL;
                    131:        }
1.6       lukem     132:        tok->wmax = tok->wspace + WINCR;
                    133:        tok->wstart = tok->wspace;
                    134:        tok->wptr = tok->wspace;
                    135:        tok->flags = 0;
                    136:        tok->quote = Q_none;
1.1       cgd       137:
1.6       lukem     138:        return (tok);
1.1       cgd       139: }
                    140:
                    141:
                    142: /* tok_reset():
                    143:  *     Reset the tokenizer
                    144:  */
                    145: public void
1.6       lukem     146: tok_reset(Tokenizer *tok)
1.1       cgd       147: {
1.6       lukem     148:
                    149:        tok->argc = 0;
                    150:        tok->wstart = tok->wspace;
                    151:        tok->wptr = tok->wspace;
                    152:        tok->flags = 0;
                    153:        tok->quote = Q_none;
1.1       cgd       154: }
                    155:
                    156:
                    157: /* tok_end():
                    158:  *     Clean up
                    159:  */
                    160: public void
1.6       lukem     161: tok_end(Tokenizer *tok)
1.1       cgd       162: {
1.6       lukem     163:
                    164:        tok_free((ptr_t) tok->ifs);
                    165:        tok_free((ptr_t) tok->wspace);
                    166:        tok_free((ptr_t) tok->argv);
                    167:        tok_free((ptr_t) tok);
1.1       cgd       168: }
                    169:
                    170:
                    171:
                    172: /* tok_line():
1.14    ! lukem     173:  *     Bourne shell (sh(1)) like tokenizing
        !           174:  *     Arguments:
        !           175:  *             tok     current tokenizer state (setup with tok_init())
        !           176:  *             line    line to parse
        !           177:  *     Returns:
        !           178:  *             -1      Internal error
        !           179:  *              3      Quoted return
        !           180:  *              2      Unmatched double quote
        !           181:  *              1      Unmatched single quote
        !           182:  *              0      Ok
        !           183:  *     Modifies (if return value is 0):
        !           184:  *             argc    number of arguments
        !           185:  *             argv    argument array
        !           186:  *             cursorc if !NULL, argv element containing cursor
        !           187:  *             cursorv if !NULL, offset in argv[cursorc] of cursor
1.1       cgd       188:  */
                    189: public int
1.14    ! lukem     190: tok_line(Tokenizer *tok, const LineInfo *line,
        !           191:     int *argc, const char ***argv, int *cursorc, int *cursoro)
1.1       cgd       192: {
1.6       lukem     193:        const char *ptr;
1.14    ! lukem     194:        int cc, co;
1.1       cgd       195:
1.14    ! lukem     196:        cc = co = -1;
        !           197:        ptr = line->buffer;
        !           198:        for (ptr = line->buffer; ;ptr++) {
        !           199:                if (ptr >= line->lastchar)
        !           200:                        ptr = "";
        !           201:                if (ptr == line->cursor) {
        !           202:                        cc = tok->argc;
        !           203:                        co = tok->wptr - tok->wstart;
        !           204:                }
        !           205:                switch (*ptr) {
1.6       lukem     206:                case '\'':
                    207:                        tok->flags |= TOK_KEEP;
                    208:                        tok->flags &= ~TOK_EAT;
                    209:                        switch (tok->quote) {
                    210:                        case Q_none:
                    211:                                tok->quote = Q_single;  /* Enter single quote
                    212:                                                         * mode */
                    213:                                break;
                    214:
                    215:                        case Q_single:  /* Exit single quote mode */
                    216:                                tok->quote = Q_none;
                    217:                                break;
                    218:
                    219:                        case Q_one:     /* Quote this ' */
                    220:                                tok->quote = Q_none;
                    221:                                *tok->wptr++ = *ptr;
                    222:                                break;
                    223:
                    224:                        case Q_double:  /* Stay in double quote mode */
                    225:                                *tok->wptr++ = *ptr;
                    226:                                break;
                    227:
                    228:                        case Q_doubleone:       /* Quote this ' */
                    229:                                tok->quote = Q_double;
                    230:                                *tok->wptr++ = *ptr;
                    231:                                break;
                    232:
                    233:                        default:
                    234:                                return (-1);
                    235:                        }
                    236:                        break;
                    237:
                    238:                case '"':
                    239:                        tok->flags &= ~TOK_EAT;
                    240:                        tok->flags |= TOK_KEEP;
                    241:                        switch (tok->quote) {
                    242:                        case Q_none:    /* Enter double quote mode */
                    243:                                tok->quote = Q_double;
                    244:                                break;
                    245:
                    246:                        case Q_double:  /* Exit double quote mode */
                    247:                                tok->quote = Q_none;
                    248:                                break;
                    249:
                    250:                        case Q_one:     /* Quote this " */
                    251:                                tok->quote = Q_none;
                    252:                                *tok->wptr++ = *ptr;
                    253:                                break;
                    254:
                    255:                        case Q_single:  /* Stay in single quote mode */
                    256:                                *tok->wptr++ = *ptr;
                    257:                                break;
                    258:
                    259:                        case Q_doubleone:       /* Quote this " */
                    260:                                tok->quote = Q_double;
                    261:                                *tok->wptr++ = *ptr;
                    262:                                break;
                    263:
                    264:                        default:
                    265:                                return (-1);
                    266:                        }
                    267:                        break;
                    268:
                    269:                case '\\':
                    270:                        tok->flags |= TOK_KEEP;
                    271:                        tok->flags &= ~TOK_EAT;
                    272:                        switch (tok->quote) {
                    273:                        case Q_none:    /* Quote next character */
                    274:                                tok->quote = Q_one;
                    275:                                break;
                    276:
                    277:                        case Q_double:  /* Quote next character */
                    278:                                tok->quote = Q_doubleone;
                    279:                                break;
                    280:
                    281:                        case Q_one:     /* Quote this, restore state */
                    282:                                *tok->wptr++ = *ptr;
                    283:                                tok->quote = Q_none;
                    284:                                break;
                    285:
                    286:                        case Q_single:  /* Stay in single quote mode */
                    287:                                *tok->wptr++ = *ptr;
                    288:                                break;
                    289:
                    290:                        case Q_doubleone:       /* Quote this \ */
                    291:                                tok->quote = Q_double;
                    292:                                *tok->wptr++ = *ptr;
                    293:                                break;
                    294:
                    295:                        default:
                    296:                                return (-1);
                    297:                        }
                    298:                        break;
                    299:
                    300:                case '\n':
                    301:                        tok->flags &= ~TOK_EAT;
                    302:                        switch (tok->quote) {
                    303:                        case Q_none:
1.14    ! lukem     304:                                goto tok_line_outok;
1.6       lukem     305:
                    306:                        case Q_single:
                    307:                        case Q_double:
                    308:                                *tok->wptr++ = *ptr;    /* Add the return */
                    309:                                break;
                    310:
                    311:                        case Q_doubleone:   /* Back to double, eat the '\n' */
                    312:                                tok->flags |= TOK_EAT;
                    313:                                tok->quote = Q_double;
                    314:                                break;
                    315:
                    316:                        case Q_one:     /* No quote, more eat the '\n' */
                    317:                                tok->flags |= TOK_EAT;
                    318:                                tok->quote = Q_none;
                    319:                                break;
                    320:
                    321:                        default:
                    322:                                return (0);
                    323:                        }
                    324:                        break;
                    325:
                    326:                case '\0':
                    327:                        switch (tok->quote) {
                    328:                        case Q_none:
                    329:                                /* Finish word and return */
                    330:                                if (tok->flags & TOK_EAT) {
                    331:                                        tok->flags &= ~TOK_EAT;
                    332:                                        return (3);
                    333:                                }
1.14    ! lukem     334:                                goto tok_line_outok;
1.6       lukem     335:
                    336:                        case Q_single:
                    337:                                return (1);
                    338:
                    339:                        case Q_double:
                    340:                                return (2);
                    341:
                    342:                        case Q_doubleone:
                    343:                                tok->quote = Q_double;
                    344:                                *tok->wptr++ = *ptr;
                    345:                                break;
                    346:
                    347:                        case Q_one:
                    348:                                tok->quote = Q_none;
                    349:                                *tok->wptr++ = *ptr;
                    350:                                break;
                    351:
                    352:                        default:
                    353:                                return (-1);
                    354:                        }
                    355:                        break;
                    356:
                    357:                default:
                    358:                        tok->flags &= ~TOK_EAT;
                    359:                        switch (tok->quote) {
                    360:                        case Q_none:
                    361:                                if (strchr(tok->ifs, *ptr) != NULL)
                    362:                                        tok_finish(tok);
                    363:                                else
                    364:                                        *tok->wptr++ = *ptr;
                    365:                                break;
                    366:
                    367:                        case Q_single:
                    368:                        case Q_double:
                    369:                                *tok->wptr++ = *ptr;
                    370:                                break;
                    371:
                    372:
                    373:                        case Q_doubleone:
                    374:                                *tok->wptr++ = '\\';
                    375:                                tok->quote = Q_double;
                    376:                                *tok->wptr++ = *ptr;
                    377:                                break;
                    378:
                    379:                        case Q_one:
                    380:                                tok->quote = Q_none;
                    381:                                *tok->wptr++ = *ptr;
                    382:                                break;
1.1       cgd       383:
1.6       lukem     384:                        default:
                    385:                                return (-1);
1.1       cgd       386:
1.6       lukem     387:                        }
                    388:                        break;
                    389:                }
1.1       cgd       390:
1.6       lukem     391:                if (tok->wptr >= tok->wmax - 4) {
                    392:                        size_t size = tok->wmax - tok->wspace + WINCR;
                    393:                        char *s = (char *) tok_realloc(tok->wspace, size);
1.7       christos  394:                        if (s == NULL)
                    395:                                return (-1);
1.6       lukem     396:
1.8       christos  397:                        if (s != tok->wspace) {
1.6       lukem     398:                                int i;
1.8       christos  399:                                for (i = 0; i < tok->argc; i++) {
                    400:                                    tok->argv[i] =
                    401:                                        (tok->argv[i] - tok->wspace) + s;
                    402:                                }
                    403:                                tok->wptr = (tok->wptr - tok->wspace) + s;
                    404:                                tok->wstart = (tok->wstart - tok->wspace) + s;
1.6       lukem     405:                                tok->wspace = s;
                    406:                        }
1.9       christos  407:                        tok->wmax = s + size;
1.6       lukem     408:                }
                    409:                if (tok->argc >= tok->amax - 4) {
1.7       christos  410:                        char **p;
1.6       lukem     411:                        tok->amax += AINCR;
1.7       christos  412:                        p = (char **) tok_realloc(tok->argv,
1.6       lukem     413:                            tok->amax * sizeof(char *));
1.7       christos  414:                        if (p == NULL)
                    415:                                return (-1);
                    416:                        tok->argv = p;
1.6       lukem     417:                }
1.1       cgd       418:        }
1.14    ! lukem     419:  tok_line_outok:
        !           420:        if (cc == -1 && co == -1) {
        !           421:                cc = tok->argc;
        !           422:                co = tok->wptr - tok->wstart;
        !           423:        }
        !           424:        if (cursorc != NULL)
        !           425:                *cursorc = cc;
        !           426:        if (cursoro != NULL)
        !           427:                *cursoro = co;
        !           428:        tok_finish(tok);
        !           429:        *argv = (const char **)tok->argv;
        !           430:        *argc = tok->argc;
        !           431:        return (0);
        !           432: }
        !           433:
        !           434: /* tok_str():
        !           435:  *     Simpler version of tok_line, taking a NUL terminated line
        !           436:  *     and splitting into words, ignoring cursor state.
        !           437:  */
        !           438: public int
        !           439: tok_str(Tokenizer *tok, const char *line, int *argc, const char ***argv)
        !           440: {
        !           441:        LineInfo li;
        !           442:
        !           443:        memset(&li, 0, sizeof(li));
        !           444:        li.buffer = line;
        !           445:        li.cursor = li.lastchar = strchr(line, '\0');
        !           446:        return (tok_line(tok, &li, argc, argv, NULL, NULL));
1.1       cgd       447: }

CVSweb <webmaster@jp.NetBSD.org>