1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/*
* argv_parse.c --- utility function for parsing a string into a
* argc, argv array.
*
* This file defines a function argv_parse() which parsing a
* passed-in string, handling double quotes and backslashes, and
* creates an allocated argv vector which can be freed using the
* argv_free() function.
*
* See argv_parse.h for the formal definition of the functions.
*
* Copyright 1999 by Theodore Ts'o.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose with or without fee is hereby granted, provided that
* the above copyright notice and this permission notice appear in all
* copies. THE SOFTWARE IS PROVIDED "AS IS" AND THEODORE TS'O (THE
* AUTHOR) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. (Isn't
* it sick that the U.S. culture of lawsuit-happy lawyers requires
* this kind of disclaimer?)
*
* Version 1.1, modified 2/27/1999
*/
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "argv_parse.h"
#define STATE_WHITESPACE 1
#define STATE_TOKEN 2
#define STATE_QUOTED 3
/*
* Returns 0 on success, -1 on failure.
*/
int argv_parse(const char *in_buf, int *ret_argc, char ***ret_argv)
{
int argc = 0, max_argc = 0;
char **argv, **new_argv, *buf, ch;
const char *cp = NULL;
char *outcp = NULL;
int state = STATE_WHITESPACE;
buf = malloc(strlen(in_buf)+1);
if (!buf)
return -1;
argv = NULL;
outcp = buf;
for (cp = in_buf; (ch = *cp); cp++) {
if (state == STATE_WHITESPACE) {
if (isspace((int) ch))
continue;
/* Not whitespace, so start a new token */
state = STATE_TOKEN;
if (argc >= max_argc) {
max_argc += 3;
new_argv = realloc(argv,
(max_argc+1)*sizeof(char *));
if (!new_argv) {
if (argv) free(argv);
free(buf);
return -1;
}
argv = new_argv;
}
argv[argc++] = outcp;
}
if (state == STATE_QUOTED) {
if (ch == '"')
state = STATE_TOKEN;
else
*outcp++ = ch;
continue;
}
/* Must be processing characters in a word */
if (isspace((int) ch)) {
/*
* Terminate the current word and start
* looking for the beginning of the next word.
*/
*outcp++ = 0;
state = STATE_WHITESPACE;
continue;
}
if (ch == '"') {
state = STATE_QUOTED;
continue;
}
if (ch == '\\') {
ch = *++cp;
switch (ch) {
case '\0':
ch = '\\'; cp--; break;
case 'n':
ch = '\n'; break;
case 't':
ch = '\t'; break;
case 'b':
ch = '\b'; break;
}
}
*outcp++ = ch;
}
if (state != STATE_WHITESPACE)
*outcp++ = '\0';
if (ret_argv) {
if (argv == NULL) {
free(buf);
if ((argv=malloc(sizeof(char *))) == NULL)
return -1;
}
argv[argc] = NULL;
*ret_argv = argv;
} else {
free(buf);
free(argv);
}
if (ret_argc)
*ret_argc = argc;
return 0;
}
void argv_free(char **argv)
{
if (argv) {
if (*argv)
free(*argv);
free(argv);
}
}
#ifdef DEBUG_ARGV_PARSE
/*
* For debugging
*/
#include <stdio.h>
int main(int argc, char **argv)
{
int ac, ret;
char **av, **cpp;
char buf[256];
while (!feof(stdin)) {
if (fgets(buf, sizeof(buf), stdin) == NULL)
break;
ret = argv_parse(buf, &ac, &av);
if (ret != 0) {
printf("Argv_parse returned %d!\n", ret);
continue;
}
printf("Argv_parse returned %d arguments...\n", ac);
for (cpp = av; *cpp; cpp++) {
if (cpp != av)
printf(", ");
printf("'%s'", *cpp);
}
printf("\n");
argv_free(av);
}
exit(0);
}
#endif
|