2 * argv_parse.c --- utility function for parsing a string into a
5 * This file defines a function argv_parse() which parsing a
6 * passed-in string, handling double quotes and backslashes, and
7 * creates an allocated argv vector which can be freed using the
8 * argv_free() function.
10 * See argv_parse.h for the formal definition of the functions.
12 * Copyright 1999 by Theodore Ts'o.
14 * Permission to use, copy, modify, and distribute this software for
15 * any purpose with or without fee is hereby granted, provided that
16 * the above copyright notice and this permission notice appear in all
17 * copies. THE SOFTWARE IS PROVIDED "AS IS" AND THEODORE TS'O (THE
18 * AUTHOR) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
21 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
22 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
23 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
24 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. (Isn't
25 * it sick that the U.S. culture of lawsuit-happy lawyers requires
26 * this kind of disclaimer?)
28 * Version 1.1, modified 2/27/1999
36 #include "argv_parse.h"
38 #define STATE_WHITESPACE 1
40 #define STATE_QUOTED 3
43 * Returns 0 on success, -1 on failure.
45 int argv_parse(char *in_buf, int *ret_argc, char ***ret_argv)
47 int argc = 0, max_argc = 0;
48 char **argv, **new_argv, *buf, ch;
49 char *cp = 0, *outcp = 0;
50 int state = STATE_WHITESPACE;
52 buf = malloc(strlen(in_buf)+1);
56 max_argc = 0; argc = 0; argv = 0;
58 for (cp = in_buf; (ch = *cp); cp++) {
59 if (state == STATE_WHITESPACE) {
60 if (isspace((int) ch))
62 /* Not whitespace, so start a new token */
64 if (argc >= max_argc) {
66 new_argv = realloc(argv,
67 (max_argc+1)*sizeof(char *));
77 if (state == STATE_QUOTED) {
84 /* Must be processing characters in a word */
85 if (isspace((int) ch)) {
87 * Terminate the current word and start
88 * looking for the beginning of the next word.
91 state = STATE_WHITESPACE;
102 ch = '\\'; cp--; break;
113 if (state != STATE_WHITESPACE)
116 argv = malloc(sizeof(char *));
127 void argv_free(char **argv)
140 int main(int argc, char **argv)
146 while (!feof(stdin)) {
147 if (fgets(buf, sizeof(buf), stdin) == NULL)
149 ret = argv_parse(buf, &ac, &av);
151 printf("Argv_parse returned %d!\n", ret);
154 printf("Argv_parse returned %d arguments...\n", ac);
155 for (cpp = av; *cpp; cpp++) {
158 printf("'%s'", *cpp);