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