WINGs: Fix memory leak in wtokenfree()
[wmaker-crm.git] / WINGs / string.c
blob70277c8a1c1d3a08200aa6c296749e48ea36ff8f
2 #include "wconfig.h"
4 #include <string.h>
5 #include <stdlib.h>
6 #include <ctype.h>
8 #include "WUtil.h"
10 #define PRC_ALPHA 0
11 #define PRC_BLANK 1
12 #define PRC_ESCAPE 2
13 #define PRC_DQUOTE 3
14 #define PRC_EOS 4
15 #define PRC_SQUOTE 5
17 typedef struct {
18 short nstate;
19 short output;
20 } DFA;
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)
36 char *ptr;
37 char *ret, *t;
38 int state, ctype;
40 t = ret = wmalloc(strlen(word) + 1);
41 ptr = word;
43 state = 0;
44 *t = 0;
45 while (1) {
46 if (*ptr == 0)
47 ctype = PRC_EOS;
48 else if (*ptr == '\\')
49 ctype = PRC_ESCAPE;
50 else if (*ptr == '"')
51 ctype = PRC_DQUOTE;
52 else if (*ptr == '\'')
53 ctype = PRC_SQUOTE;
54 else if (*ptr == ' ' || *ptr == '\t')
55 ctype = PRC_BLANK;
56 else
57 ctype = PRC_ALPHA;
59 if (mtable[state][ctype].output) {
60 *t = *ptr;
61 t++;
62 *t = 0;
64 state = mtable[state][ctype].nstate;
65 ptr++;
66 if (mtable[state][0].output < 0) {
67 break;
71 if (*ret == 0)
72 t = NULL;
73 else
74 t = wstrdup(ret);
76 wfree(ret);
78 if (ctype == PRC_EOS)
79 *next = NULL;
80 else
81 *next = ptr;
83 return t;
86 /* separate a string in tokens, taking " and ' into account */
87 void wtokensplit(char *command, char ***argv, int *argc)
89 char *token, *line;
90 int count;
92 count = 0;
93 line = command;
94 do {
95 token = wtokennext(line, &line);
96 if (token) {
97 if (count == 0)
98 *argv = wmalloc(sizeof(char **));
99 else
100 *argv = wrealloc(*argv, (count + 1) * sizeof(char **));
101 (*argv)[count++] = token;
103 } while (token != NULL && line != NULL);
105 *argc = count;
108 char *wtokenjoin(char **list, int count)
110 int i, j;
111 char *flat_string, *wspace;
113 j = 0;
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"))
118 j += 2;
122 flat_string = wmalloc(j + count + 1);
124 *flat_string = 0;
125 for (i = 0; i < count; i++) {
126 if (list[i] != NULL && list[i][0] != 0) {
127 if (i > 0)
128 strcat(flat_string, " ");
129 wspace = strpbrk(list[i], " \t");
130 if (wspace)
131 strcat(flat_string, "\"");
132 strcat(flat_string, list[i]);
133 if (wspace)
134 strcat(flat_string, "\"");
138 return flat_string;
141 void wtokenfree(char **tokens, int count)
143 while (count--)
144 wfree(tokens[count]);
145 wfree(tokens);
148 char *wtrimspace(char *s)
150 char *t;
151 char *c;
153 while (isspace(*s) && *s)
154 s++;
155 t = s + strlen(s) - 1;
156 while (t > s && isspace(*t))
157 t--;
159 c = wmalloc(t - s + 2);
160 memcpy(c, s, t - s + 1);
161 c[t - s + 1] = 0;
163 return c;
166 char *wstrdup(char *str)
168 assert(str != NULL);
170 return strcpy(wmalloc(strlen(str) + 1), str);
173 char *wstrndup(char *str, size_t len)
175 char *copy;
177 assert(str != NULL);
179 len = WMIN(len, strlen(str));
180 copy = strncpy(wmalloc(len + 1), str, len);
181 copy[len] = 0;
183 return copy;
186 char *wstrconcat(char *str1, char *str2)
188 char *str;
190 if (!str1)
191 return wstrdup(str2);
192 else if (!str2)
193 return wstrdup(str1);
195 str = wmalloc(strlen(str1) + strlen(str2) + 1);
196 strcpy(str, str1);
197 strcat(str, str2);
199 return str;
202 char *wstrappend(char *dst, char *src)
204 if (!dst)
205 return wstrdup(src);
206 else if (!src || *src == 0)
207 return dst;
209 dst = wrealloc(dst, strlen(dst) + strlen(src) + 1);
210 strcat(dst, src);
212 return dst;
215 #ifndef HAVE_STRCASECMP
216 int strcasecmp(const char *s1, const char *s2)
218 while (*s1 && *s2 && (tolower(*s1) == tolower(*s2))) {
219 s1++;
220 s2++;
223 return (tolower(*s1) - tolower(*s2));
225 #endif