7 #ifdef HAVE_BSD_STRING_H
8 #include <bsd/string.h>
25 static DFA mtable
[9][6] = {
26 {{3, 1}, {0, 0}, {4, 0}, {1, 0}, {8, 0}, {6, 0}},
27 {{1, 1}, {1, 1}, {2, 0}, {3, 0}, {5, 0}, {1, 1}},
28 {{1, 1}, {1, 1}, {1, 1}, {1, 1}, {5, 0}, {1, 1}},
29 {{3, 1}, {5, 0}, {4, 0}, {1, 0}, {5, 0}, {6, 0}},
30 {{3, 1}, {3, 1}, {3, 1}, {3, 1}, {5, 0}, {3, 1}},
31 {{-1, -1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, /* final state */
32 {{6, 1}, {6, 1}, {7, 0}, {6, 1}, {5, 0}, {3, 0}},
33 {{6, 1}, {6, 1}, {6, 1}, {6, 1}, {5, 0}, {6, 1}},
34 {{-1, -1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, /* final state */
37 char *wtokennext(char *word
, char **next
)
43 t
= ret
= wmalloc(strlen(word
) + 1);
50 else if (*ptr
== '\\')
54 else if (*ptr
== '\'')
56 else if (*ptr
== ' ' || *ptr
== '\t')
61 if (mtable
[state
][ctype
].output
) {
66 state
= mtable
[state
][ctype
].nstate
;
68 if (mtable
[state
][0].output
< 0) {
88 /* separate a string in tokens, taking " and ' into account */
89 void wtokensplit(char *command
, char ***argv
, int *argc
)
97 token
= wtokennext(line
, &line
);
100 *argv
= wmalloc(sizeof(char **));
102 *argv
= wrealloc(*argv
, (count
+ 1) * sizeof(char **));
103 (*argv
)[count
++] = token
;
105 } while (token
!= NULL
&& line
!= NULL
);
110 char *wtokenjoin(char **list
, int count
)
113 char *flat_string
, *wspace
;
116 for (i
= 0; i
< count
; i
++) {
117 if (list
[i
] != NULL
&& list
[i
][0] != 0) {
118 j
+= strlen(list
[i
]);
119 if (strpbrk(list
[i
], " \t"))
124 flat_string
= wmalloc(j
+ count
+ 1);
126 for (i
= 0; i
< count
; i
++) {
127 if (list
[i
] != NULL
&& list
[i
][0] != 0) {
129 strcat(flat_string
, " ");
130 wspace
= strpbrk(list
[i
], " \t");
132 strcat(flat_string
, "\"");
133 strcat(flat_string
, list
[i
]);
135 strcat(flat_string
, "\"");
142 void wtokenfree(char **tokens
, int count
)
145 wfree(tokens
[count
]);
149 char *wtrimspace(const char *s
)
156 while (isspace(*s
) && *s
)
158 t
= (char *)s
+ strlen(s
) - 1;
159 while (t
> s
&& isspace(*t
))
162 return wstrndup(s
, t
- s
+ 1);
165 char *wstrdup(const char *str
)
169 return strcpy(wmalloc(strlen(str
) + 1), str
);
172 char *wstrndup(const char *str
, size_t len
)
178 len
= WMIN(len
, strlen(str
));
179 copy
= strncpy(wmalloc(len
+ 1), str
, len
);
185 char *wstrconcat(char *str1
, char *str2
)
190 return wstrdup(str2
);
192 return wstrdup(str1
);
194 str
= wmalloc(strlen(str1
) + strlen(str2
) + 1);
201 char *wstrappend(char *dst
, char *src
)
205 else if (!src
|| *src
== 0)
208 dst
= wrealloc(dst
, strlen(dst
) + strlen(src
) + 1);
217 wstrlcat(char *dst
, const char *src
, size_t siz
)
219 return strlcat(dst
, src
, siz
);
222 /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
225 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
227 * Permission to use, copy, modify, and distribute this software for any
228 * purpose with or without fee is hereby granted, provided that the above
229 * copyright notice and this permission notice appear in all copies.
231 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
232 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
233 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
234 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
235 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
236 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
237 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
241 * Appends src to string dst of size siz (unlike strncat, siz is the
242 * full size of dst, not space left). At most siz-1 characters
243 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
244 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
245 * If retval >= siz, truncation occurred.
248 wstrlcat(char *dst
, const char *src
, size_t siz
)
255 /* Find the end of dst and adjust bytes left but don't go past end */
256 while (n
-- != 0 && *d
!= '\0')
262 return(dlen
+ strlen(s
));
272 return(dlen
+ (s
- src
)); /* count does not include NUL */
274 #endif /* HAVE_STRLCAT */
278 wstrlcpy(char *dst
, const char *src
, size_t siz
)
280 return strlcpy(dst
, src
, siz
);
284 /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
287 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
289 * Permission to use, copy, modify, and distribute this software for any
290 * purpose with or without fee is hereby granted, provided that the above
291 * copyright notice and this permission notice appear in all copies.
293 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
294 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
295 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
296 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
297 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
298 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
299 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
303 * Copy src to string dst of size siz. At most siz-1 characters
304 * will be copied. Always NUL terminates (unless siz == 0).
305 * Returns strlen(src); if retval >= siz, truncation occurred.
308 wstrlcpy(char *dst
, const char *src
, size_t siz
)
314 /* Copy as many bytes as will fit */
317 if ((*d
++ = *s
++) == '\0')
322 /* Not enough room in dst, add NUL and traverse rest of src */
325 *d
= '\0'; /* NUL-terminate dst */
330 return(s
- src
- 1); /* count does not include NUL */
332 #endif /* HAVE_STRLCPY */