… | |
… | |
25 | |
25 | |
26 | #include "../config.h" /* NECESSARY */ |
26 | #include "../config.h" /* NECESSARY */ |
27 | #include "rxvt.h" /* NECESSARY */ |
27 | #include "rxvt.h" /* NECESSARY */ |
28 | #include "misc.intpro" /* PROTOS for internal routines */ |
28 | #include "misc.intpro" /* PROTOS for internal routines */ |
29 | |
29 | |
30 | /* INTPROTO */ |
|
|
31 | char * |
30 | char * |
32 | rxvt_wcstombs (const wchar_t *str, int len) |
31 | rxvt_wcstombs (const wchar_t *str, int len) |
33 | { |
32 | { |
|
|
33 | if (len < 0) len = wcslen (str); |
|
|
34 | |
34 | mbstate mbs; |
35 | mbstate mbs; |
35 | char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); |
36 | char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); |
36 | |
37 | |
37 | char *dst = r; |
38 | char *dst = r; |
38 | while (len--) |
39 | while (len--) |
39 | { |
40 | { |
40 | int l = wcrtomb (dst, *str++, mbs); |
41 | ssize_t l = wcrtomb (dst, *str++, mbs); |
41 | if (l < 0) |
42 | if (l < 0) |
42 | *dst++ = '?'; |
43 | *dst++ = '?'; |
43 | else |
44 | else |
44 | dst += l; |
45 | dst += l; |
45 | } |
46 | } |
46 | |
47 | |
47 | *dst++ = 0; |
48 | *dst++ = 0; |
48 | |
49 | |
|
|
50 | return (char *)rxvt_realloc (r, dst - r); |
|
|
51 | } |
|
|
52 | |
|
|
53 | wchar_t * |
|
|
54 | rxvt_mbstowcs (const char *str, int len) |
|
|
55 | { |
|
|
56 | if (len < 0) len = strlen (str); |
|
|
57 | |
|
|
58 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); |
|
|
59 | |
|
|
60 | if ((ssize_t)mbstowcs (r, str, len + 1) < 0) |
|
|
61 | *r = 0; |
|
|
62 | |
49 | return r; |
63 | return r; |
50 | } |
64 | } |
51 | |
65 | |
52 | /* INTPROTO */ |
66 | char * |
|
|
67 | rxvt_wcstoutf8 (const wchar_t *str, int len) |
|
|
68 | { |
|
|
69 | if (len < 0) len = wcslen (str); |
|
|
70 | |
|
|
71 | char *r = (char *)rxvt_malloc (len * 4 + 1); |
|
|
72 | char *p = r; |
|
|
73 | |
|
|
74 | while (len--) |
|
|
75 | { |
|
|
76 | unicode_t w = *str++ & UNICODE_MASK; |
|
|
77 | |
|
|
78 | if (w < 0x000080) |
|
|
79 | *p++ = w; |
|
|
80 | else if (w < 0x000800) |
|
|
81 | *p++ = 0xc0 | ( w >> 6), |
|
|
82 | *p++ = 0x80 | ( w & 0x3f); |
|
|
83 | else if (w < 0x010000) |
|
|
84 | *p++ = 0xe0 | ( w >> 12 ), |
|
|
85 | *p++ = 0x80 | ((w >> 6) & 0x3f), |
|
|
86 | *p++ = 0x80 | ( w & 0x3f); |
|
|
87 | else if (w < 0x110000) |
|
|
88 | *p++ = 0xf0 | ( w >> 18), |
|
|
89 | *p++ = 0x80 | ((w >> 12) & 0x3f), |
|
|
90 | *p++ = 0x80 | ((w >> 6) & 0x3f), |
|
|
91 | *p++ = 0x80 | ( w & 0x3f); |
|
|
92 | else |
|
|
93 | *p++ = '?'; |
|
|
94 | } |
|
|
95 | |
|
|
96 | *p++ = 0; |
|
|
97 | |
|
|
98 | return (char *)rxvt_realloc (r, p - r); |
|
|
99 | } |
|
|
100 | |
|
|
101 | wchar_t * |
|
|
102 | rxvt_utf8towcs (const char *str, int len) |
|
|
103 | { |
|
|
104 | if (len < 0) len = strlen (str); |
|
|
105 | |
|
|
106 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)), |
|
|
107 | *p = r; |
|
|
108 | |
|
|
109 | unsigned char *s = (unsigned char *)str, |
|
|
110 | *e = s + len; |
|
|
111 | |
|
|
112 | for (;;) |
|
|
113 | { |
|
|
114 | len = e - s; |
|
|
115 | |
|
|
116 | if (len == 0) |
|
|
117 | break; |
|
|
118 | else if (s[0] < 0x80) |
|
|
119 | *p++ = *s++; |
|
|
120 | else if (len >= 2 |
|
|
121 | && s[0] >= 0xc2 && s[0] <= 0xdf |
|
|
122 | && (s[1] & 0xc0) == 0x80) |
|
|
123 | { |
|
|
124 | *p++ = ((s[0] & 0x1f) << 6) |
|
|
125 | | (s[1] & 0x3f); |
|
|
126 | s += 2; |
|
|
127 | } |
|
|
128 | else if (len >= 3 |
|
|
129 | && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) |
|
|
130 | || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
131 | || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) |
|
|
132 | || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
133 | ) |
|
|
134 | && (s[2] & 0xc0) == 0x80) |
|
|
135 | { |
|
|
136 | *p++ = ((s[0] & 0x0f) << 12) |
|
|
137 | | ((s[1] & 0x3f) << 6) |
|
|
138 | | (s[2] & 0x3f); |
|
|
139 | s += 3; |
|
|
140 | } |
|
|
141 | else if (len >= 4 |
|
|
142 | && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) |
|
|
143 | || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
144 | || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) |
|
|
145 | ) |
|
|
146 | && (s[2] & 0xc0) == 0x80 |
|
|
147 | && (s[3] & 0xc0) == 0x80) |
|
|
148 | { |
|
|
149 | *p++ = ((s[0] & 0x07) << 18) |
|
|
150 | | ((s[1] & 0x3f) << 12) |
|
|
151 | | ((s[2] & 0x3f) << 6) |
|
|
152 | | (s[3] & 0x3f); |
|
|
153 | s += 4; |
|
|
154 | } |
|
|
155 | else |
|
|
156 | { |
|
|
157 | *p++ = 0xfffd; |
|
|
158 | s++; |
|
|
159 | } |
|
|
160 | } |
|
|
161 | |
|
|
162 | *p = 0; |
|
|
163 | |
|
|
164 | return r; |
|
|
165 | } |
|
|
166 | |
53 | char * |
167 | char * |
54 | rxvt_strdup (const char *str) |
168 | rxvt_strdup (const char *str) |
55 | { |
169 | { |
56 | return str ? strdup (str) : 0; |
170 | return str ? strdup (str) : 0; |
57 | } |
171 | } |
… | |
… | |
168 | /* INTPROTO */ |
282 | /* INTPROTO */ |
169 | char * |
283 | char * |
170 | rxvt_Str_trim (char *str) |
284 | rxvt_Str_trim (char *str) |
171 | { |
285 | { |
172 | char *r, *s; |
286 | char *r, *s; |
173 | int n; |
|
|
174 | |
287 | |
175 | if (!str || !*str) /* shortcut */ |
288 | if (!str || !*str) /* shortcut */ |
176 | return str; |
289 | return str; |
177 | |
290 | |
178 | /* skip leading spaces */ |
291 | /* skip leading spaces */ |
179 | for (s = str; *s && isspace (*s); s++) ; |
292 | for (s = str; *s && isspace (*s); s++) ; |
|
|
293 | |
180 | /* goto end of string */ |
294 | /* goto end of string */ |
181 | for (n = 0, r = s; *r++; n++) ; |
295 | r = s + strlen (s) - 1; |
182 | r -= 2; |
296 | |
183 | /* dump return */ |
297 | /* dump return and other trailing whitespace */ |
184 | if (n > 0 && *r == '\n') |
298 | while (r > s && isspace (*r)) |
185 | n--, r--; |
299 | r--; |
186 | /* backtrack along trailing spaces */ |
300 | |
187 | for (; n > 0 && isspace (*r); r--, n--) ; |
301 | #if 0 |
188 | /* skip matching leading/trailing quotes */ |
302 | /* skip matching leading/trailing quotes */ |
189 | if (*s == '"' && *r == '"' && n > 1) |
303 | if (*s == '"' && *r == '"' && n > 1) |
190 | { |
304 | { |
191 | s++; |
305 | s++; |
192 | n -= 2; |
306 | n -= 2; |
193 | } |
307 | } |
|
|
308 | #endif |
194 | |
309 | |
195 | /* copy back over: forwards copy */ |
310 | memmove (str, s, r + 1 - s); |
196 | for (r = str; n; n--) |
311 | str[r + 1 - s] = 0; |
197 | *r++ = *s++; |
|
|
198 | *r = '\0'; |
|
|
199 | |
312 | |
200 | return str; |
313 | return str; |
201 | } |
314 | } |
202 | |
315 | |
203 | /* |
316 | /* |
… | |
… | |
286 | return (d - str); |
399 | return (d - str); |
287 | } |
400 | } |
288 | |
401 | |
289 | /* |
402 | /* |
290 | * Split a comma-separated string into an array, stripping leading and |
403 | * Split a comma-separated string into an array, stripping leading and |
291 | * trailing spaces (and paired quotes) from each entry. Empty strings |
404 | * trailing spaces from each entry. Empty strings are properly returned |
292 | * are properly returned |
|
|
293 | * Caller should free each entry and array when done |
405 | * Caller should free each entry and array when done |
294 | */ |
406 | */ |
295 | /* INTPROTO */ |
407 | /* INTPROTO */ |
296 | char ** |
408 | char ** |
297 | rxvt_splitcommastring (const char *cs) |
409 | rxvt_splitcommastring (const char *cs) |
… | |
… | |
304 | s = ""; |
416 | s = ""; |
305 | |
417 | |
306 | for (n = 1, t = s; *t; t++) |
418 | for (n = 1, t = s; *t; t++) |
307 | if (*t == ',') |
419 | if (*t == ',') |
308 | n++; |
420 | n++; |
|
|
421 | |
309 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
422 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
310 | ret[n] = NULL; |
423 | ret[n] = NULL; |
311 | |
424 | |
312 | for (l = 0, t = s; l < n; l++) |
425 | for (l = 0, t = s; l < n; l++) |
313 | { |
426 | { |
… | |
… | |
317 | strncpy (ret[l], s, p); |
430 | strncpy (ret[l], s, p); |
318 | ret[l][p] = '\0'; |
431 | ret[l][p] = '\0'; |
319 | rxvt_Str_trim (ret[l]); |
432 | rxvt_Str_trim (ret[l]); |
320 | s = ++t; |
433 | s = ++t; |
321 | } |
434 | } |
|
|
435 | |
322 | return ret; |
436 | return ret; |
|
|
437 | } |
|
|
438 | |
|
|
439 | void |
|
|
440 | rxvt_freecommastring (char **cs) |
|
|
441 | { |
|
|
442 | for (int i = 0; cs[i]; ++i) |
|
|
443 | free (cs[i]); |
|
|
444 | |
|
|
445 | free (cs); |
323 | } |
446 | } |
324 | |
447 | |
325 | /*----------------------------------------------------------------------* |
448 | /*----------------------------------------------------------------------* |
326 | * file searching |
449 | * file searching |
327 | */ |
450 | */ |