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(const char *s
)
155 while (isspace(*s
) && *s
)
157 t
= (char *)s
+ strlen(s
) - 1;
158 while (t
> s
&& isspace(*t
))
161 return wstrndup(s
, t
- s
+ 1);
164 char *wstrdup(const char *str
)
168 return strcpy(wmalloc(strlen(str
) + 1), str
);
171 char *wstrndup(const char *str
, size_t len
)
177 len
= WMIN(len
, strlen(str
));
178 copy
= strncpy(wmalloc(len
+ 1), str
, len
);
184 char *wstrconcat(char *str1
, char *str2
)
189 return wstrdup(str2
);
191 return wstrdup(str1
);
193 str
= wmalloc(strlen(str1
) + strlen(str2
) + 1);
200 char *wstrappend(char *dst
, char *src
)
204 else if (!src
|| *src
== 0)
207 dst
= wrealloc(dst
, strlen(dst
) + strlen(src
) + 1);
213 #ifndef HAVE_STRCASECMP
214 int strcasecmp(const char *s1
, const char *s2
)
216 while (*s1
&& *s2
&& (tolower(*s1
) == tolower(*s2
))) {
221 return (tolower(*s1
) - tolower(*s2
));