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);
47 else if (*ptr
== '\\')
51 else if (*ptr
== '\'')
53 else if (*ptr
== ' ' || *ptr
== '\t')
58 if (mtable
[state
][ctype
].output
) {
63 state
= mtable
[state
][ctype
].nstate
;
65 if (mtable
[state
][0].output
< 0) {
85 /* separate a string in tokens, taking " and ' into account */
86 void wtokensplit(char *command
, char ***argv
, int *argc
)
94 token
= wtokennext(line
, &line
);
97 *argv
= wmalloc(sizeof(char **));
99 *argv
= wrealloc(*argv
, (count
+ 1) * sizeof(char **));
100 (*argv
)[count
++] = token
;
102 } while (token
!= NULL
&& line
!= NULL
);
107 char *wtokenjoin(char **list
, int count
)
110 char *flat_string
, *wspace
;
113 for (i
= 0; i
< count
; i
++) {
114 if (list
[i
] != NULL
&& list
[i
][0] != 0) {
115 j
+= strlen(list
[i
]);
116 if (strpbrk(list
[i
], " \t"))
121 flat_string
= wmalloc(j
+ count
+ 1);
123 for (i
= 0; i
< count
; i
++) {
124 if (list
[i
] != NULL
&& list
[i
][0] != 0) {
126 strcat(flat_string
, " ");
127 wspace
= strpbrk(list
[i
], " \t");
129 strcat(flat_string
, "\"");
130 strcat(flat_string
, list
[i
]);
132 strcat(flat_string
, "\"");
139 void wtokenfree(char **tokens
, int count
)
142 wfree(tokens
[count
]);
146 char *wtrimspace(const char *s
)
153 while (isspace(*s
) && *s
)
155 t
= (char *)s
+ strlen(s
) - 1;
156 while (t
> s
&& isspace(*t
))
159 return wstrndup(s
, t
- s
+ 1);
162 char *wstrdup(const char *str
)
166 return strcpy(wmalloc(strlen(str
) + 1), str
);
169 char *wstrndup(const char *str
, size_t len
)
175 len
= WMIN(len
, strlen(str
));
176 copy
= strncpy(wmalloc(len
+ 1), str
, len
);
182 char *wstrconcat(char *str1
, char *str2
)
187 return wstrdup(str2
);
189 return wstrdup(str1
);
191 str
= wmalloc(strlen(str1
) + strlen(str2
) + 1);
198 char *wstrappend(char *dst
, char *src
)
202 else if (!src
|| *src
== 0)
205 dst
= wrealloc(dst
, strlen(dst
) + strlen(src
) + 1);