fix crash when specifying --source on command line
[rofl0r-gnuboy.git] / split.c
blobaf53d7300ea7a2127d5ca88ca894d0f46c4f924f
3 /*
4 * splitline is a destructive argument parser, much like a very primitive
5 * form of a shell parser. it supports quotes for embedded spaces and
6 * literal quotes with the backslash escape.
7 */
9 char *splitnext(char **pos)
11 char *a, *d, *s;
13 d = s = *pos;
14 while (*s == ' ' || *s == '\t') s++;
15 a = s;
16 while (*s && *s != ' ' && *s != '\t')
18 if (*s == '"')
20 s++;
21 while (*s && *s != '"')
23 if (*s == '\\')
24 s++;
25 if (*s)
26 *(d++) = *(s++);
28 if (*s == '"') s++;
30 else
32 if (*s == '\\')
33 s++;
34 *(d++) = *(s++);
37 while (*s == ' ' || *s == '\t') s++;
38 *d = 0;
39 *pos = s;
40 return a;
43 int splitline(char **argv, int max, char *line)
45 char *s;
46 int i;
48 s = line;
49 for (i = 0; *s && i < max + 1; i++)
50 argv[i] = splitnext(&s);
51 argv[i] = 0;
52 return i;