~ubuntu-branches/ubuntu/natty/mysql-5.1/natty-proposed

« back to all changes in this revision

Viewing changes to cmd-line-utils/libedit/tokenizer.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 08:30:45 UTC
  • mfrom: (1.4.1)
  • Revision ID: package-import@ubuntu.com-20120222083045-2rd53r4bnyx7qus4
Tags: 5.1.61-0ubuntu0.11.04.1
* SECURITY UPDATE: Update to 5.1.61 to fix multiple security issues
  (LP: #937869)
  - http://www.oracle.com/technetwork/topics/security/cpujan2012-366304.html
  - CVE-2011-2262
  - CVE-2012-0075
  - CVE-2012-0112
  - CVE-2012-0113
  - CVE-2012-0114
  - CVE-2012-0115
  - CVE-2012-0116
  - CVE-2012-0117
  - CVE-2012-0118
  - CVE-2012-0119
  - CVE-2012-0120
  - CVE-2012-0484
  - CVE-2012-0485
  - CVE-2012-0486
  - CVE-2012-0487
  - CVE-2012-0488
  - CVE-2012-0489
  - CVE-2012-0490
  - CVE-2012-0491
  - CVE-2012-0492
  - CVE-2012-0493
  - CVE-2012-0494
  - CVE-2012-0495
  - CVE-2012-0496

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 $  */
2
2
 
3
3
/*-
4
4
 * Copyright (c) 1992, 1993
40
40
#endif
41
41
#endif /* not lint && not SCCSID */
42
42
 
 
43
/* We build this file twice, once as NARROW, once as WIDE. */
43
44
/*
44
45
 * tokenize.c: Bourne shell like tokenizer
45
46
 */
46
47
#include <string.h>
47
48
#include <stdlib.h>
48
49
#include "histedit.h"
 
50
#include "chartype.h"
49
51
 
50
52
typedef enum {
51
53
        Q_none, Q_single, Q_double, Q_one, Q_doubleone
52
54
} quote_t;
53
55
 
54
 
#define IFS             "\t \n"
55
 
 
56
56
#define TOK_KEEP        1
57
57
#define TOK_EAT         2
58
58
 
59
59
#define WINCR           20
60
60
#define AINCR           10
61
61
 
62
 
#define tok_strdup(a)           strdup(a)
 
62
#define IFS             STR("\t \n")
 
63
 
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)
66
 
 
67
 
 
68
 
struct tokenizer {
69
 
        char    *ifs;           /* In field separator                    */
 
67
#define tok_strdup(a)           Strdup(a)
 
68
 
 
69
 
 
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;                                */
77
79
};
78
80
 
79
81
 
80
 
private void tok_finish(Tokenizer *);
81
 
 
82
 
 
83
 
/* tok_finish():
 
82
private void FUN(tok,finish)(TYPE(Tokenizer) *);
 
83
 
 
84
 
 
85
/* FUN(tok,finish)():
84
86
 *      Finish a word in the tokenizer.
85
87
 */
86
88
private void
87
 
tok_finish(Tokenizer *tok)
 
89
FUN(tok,finish)(TYPE(Tokenizer) *tok)
88
90
{
89
91
 
90
92
        *tok->wptr = '\0';
97
99
}
98
100
 
99
101
 
100
 
/* tok_init():
 
102
/* FUN(tok,init)():
101
103
 *      Initialize the tokenizer
102
104
 */
103
 
public Tokenizer *
104
 
tok_init(const char *ifs)
 
105
public TYPE(Tokenizer) *
 
106
FUN(tok,init)(const Char *ifs)
105
107
{
106
 
        Tokenizer *tok = (Tokenizer *) tok_malloc(sizeof(Tokenizer));
 
108
        TYPE(Tokenizer) *tok = tok_malloc(sizeof(*tok));
107
109
 
108
110
        if (tok == NULL)
109
111
                return NULL;
110
112
        tok->ifs = tok_strdup(ifs ? ifs : IFS);
111
113
        if (tok->ifs == NULL) {
112
 
                tok_free((ptr_t)tok);
 
114
                tok_free(tok);
113
115
                return NULL;
114
116
        }
115
117
        tok->argc = 0;
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);
 
121
                tok_free(tok->ifs);
 
122
                tok_free(tok);
121
123
                return NULL;
122
124
        }
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);
 
128
                tok_free(tok->argv);
 
129
                tok_free(tok->ifs);
 
130
                tok_free(tok);
129
131
                return NULL;
130
132
        }
131
133
        tok->wmax = tok->wspace + WINCR;
138
140
}
139
141
 
140
142
 
141
 
/* tok_reset():
 
143
/* FUN(tok,reset)():
142
144
 *      Reset the tokenizer
143
145
 */
144
146
public void
145
 
tok_reset(Tokenizer *tok)
 
147
FUN(tok,reset)(TYPE(Tokenizer) *tok)
146
148
{
147
149
 
148
150
        tok->argc = 0;
153
155
}
154
156
 
155
157
 
156
 
/* tok_end():
 
158
/* FUN(tok,end)():
157
159
 *      Clean up
158
160
 */
159
161
public void
160
 
tok_end(Tokenizer *tok)
 
162
FUN(tok,end)(TYPE(Tokenizer) *tok)
161
163
{
162
164
 
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);
 
165
        tok_free(tok->ifs);
 
166
        tok_free(tok->wspace);
 
167
        tok_free(tok->argv);
 
168
        tok_free(tok);
167
169
}
168
170
 
169
171
 
170
172
 
171
 
/* tok_line():
 
173
/* FUN(tok,line)():
172
174
 *      Bourne shell (sh(1)) like tokenizing
173
175
 *      Arguments:
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
176
178
 *      Returns:
177
179
 *              -1      Internal error
186
188
 *              cursorv if !NULL, offset in argv[cursorc] of cursor
187
189
 */
188
190
public int
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)
191
193
{
192
 
        const char *ptr;
 
194
        const Char *ptr;
193
195
        int cc, co;
194
196
 
195
197
        cc = co = -1;
196
198
        ptr = line->buffer;
197
199
        for (ptr = line->buffer; ;ptr++) {
198
200
                if (ptr >= line->lastchar)
199
 
                        ptr = "";
 
201
                        ptr = STR("");
200
202
                if (ptr == line->cursor) {
201
203
                        cc = tok->argc;
202
 
                        co = tok->wptr - tok->wstart;
 
204
                        co = (int)(tok->wptr - tok->wstart);
203
205
                }
204
206
                switch (*ptr) {
205
207
                case '\'':
357
359
                        tok->flags &= ~TOK_EAT;
358
360
                        switch (tok->quote) {
359
361
                        case Q_none:
360
 
                                if (strchr(tok->ifs, *ptr) != NULL)
361
 
                                        tok_finish(tok);
 
362
                                if (Strchr(tok->ifs, *ptr) != NULL)
 
363
                                        FUN(tok,finish)(tok);
362
364
                                else
363
365
                                        *tok->wptr++ = *ptr;
364
366
                                break;
389
391
 
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,
 
395
                            size * sizeof(*s));
393
396
                        if (s == NULL)
394
397
                                return (-1);
395
398
 
406
409
                        tok->wmax = s + size;
407
410
                }
408
411
                if (tok->argc >= tok->amax - 4) {
409
 
                        char **p;
 
412
                        Char **p;
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));
413
415
                        if (p == NULL)
414
416
                                return (-1);
415
417
                        tok->argv = p;
418
420
 tok_line_outok:
419
421
        if (cc == -1 && co == -1) {
420
422
                cc = tok->argc;
421
 
                co = tok->wptr - tok->wstart;
 
423
                co = (int)(tok->wptr - tok->wstart);
422
424
        }
423
425
        if (cursorc != NULL)
424
426
                *cursorc = cc;
425
427
        if (cursoro != NULL)
426
428
                *cursoro = co;
427
 
        tok_finish(tok);
428
 
        *argv = (const char **)tok->argv;
 
429
        FUN(tok,finish)(tok);
 
430
        *argv = (const Char **)tok->argv;
429
431
        *argc = tok->argc;
430
432
        return (0);
431
433
}
432
434
 
433
 
/* tok_str():
 
435
/* FUN(tok,str)():
434
436
 *      Simpler version of tok_line, taking a NUL terminated line
435
437
 *      and splitting into words, ignoring cursor state.
436
438
 */
437
439
public int
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,
 
441
    const Char ***argv)
439
442
{
440
 
        LineInfo li;
 
443
        TYPE(LineInfo) li;
441
444
 
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));
446
449
}