22 static DFA mtable[9][6] = {
23 {{3, 1}, {0, 0}, {4, 0}, {1, 0}, {8, 0}, {6, 0}},
24 {{1, 1}, {1, 1}, {2, 0}, {3, 0}, {5, 0}, {1, 1}},
25 {{1, 1}, {1, 1}, {1, 1}, {1, 1}, {5, 0}, {1, 1}},
26 {{3, 1}, {5, 0}, {4, 0}, {1, 0}, {5, 0}, {6, 0}},
27 {{3, 1}, {3, 1}, {3, 1}, {3, 1}, {5, 0}, {3, 1}},
28 {{-1, -1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, /* final state */
29 {{6, 1}, {6, 1}, {7, 0}, {6, 1}, {5, 0}, {3, 0}},
30 {{6, 1}, {6, 1}, {6, 1}, {6, 1}, {5, 0}, {6, 1}},
31 {{-1, -1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, /* final state */
34 char *wtokennext(char *word, char **next)
40 t = ret = wmalloc(strlen(word) + 1);
48 else if (*ptr == '\\')
52 else if (*ptr == '\'')
54 else if (*ptr == ' ' || *ptr == '\t')
59 if (mtable[state][ctype].output) {
64 state = mtable[state][ctype].nstate;
66 if (mtable[state][0].output < 0) {
86 /* separate a string in tokens, taking " and ' into account */
87 void wtokensplit(char *command, char ***argv, int *argc)
95 token = wtokennext(line, &line);
98 *argv = wmalloc(sizeof(char **));
100 *argv = wrealloc(*argv, (count + 1) * sizeof(char **));
101 (*argv)[count++] = token;
103 } while (token != NULL && line != NULL);
108 char *wtokenjoin(char **list, int count)
111 char *flat_string, *wspace;
114 for (i = 0; i < count; i++) {
115 if (list[i] != NULL && list[i][0] != 0) {
116 j += strlen(list[i]);
117 if (strpbrk(list[i], " \t"))
122 flat_string = wmalloc(j + count + 1);
125 for (i = 0; i < count; i++) {
126 if (list[i] != NULL && list[i][0] != 0) {
128 strcat(flat_string, " ");
129 wspace = strpbrk(list[i], " \t");
131 strcat(flat_string, "\"");
132 strcat(flat_string, list[i]);
134 strcat(flat_string, "\"");
141 void wtokenfree(char **tokens, int count)
144 wfree(tokens[count]);
148 char *wtrimspace(char *s)
153 while (isspace(*s) && *s)
155 t = s + strlen(s) - 1;
156 while (t > s && isspace(*t))
159 c = wmalloc(t - s + 2);
160 memcpy(c, s, t - s + 1);
166 char *wstrdup(char *str)
170 return strcpy(wmalloc(strlen(str) + 1), str);
173 char *wstrndup(char *str, size_t len)
179 len = WMIN(len, strlen(str));
180 copy = strncpy(wmalloc(len + 1), str, len);
186 char *wstrconcat(char *str1, char *str2)
191 return wstrdup(str2);
193 return wstrdup(str1);
195 str = wmalloc(strlen(str1) + strlen(str2) + 1);
202 char *wstrappend(char *dst, char *src)
206 else if (!src || *src == 0)
209 dst = wrealloc(dst, strlen(dst) + strlen(src) + 1);
215 #ifndef HAVE_STRCASECMP
216 int strcasecmp(const char *s1, const char *s2)
218 while (*s1 && *s2 && (tolower(*s1) == tolower(*s2))) {
223 return (tolower(*s1) - tolower(*s2));