… | |
… | |
36 | char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); |
36 | char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); |
37 | |
37 | |
38 | char *dst = r; |
38 | char *dst = r; |
39 | while (len--) |
39 | while (len--) |
40 | { |
40 | { |
41 | int l = wcrtomb (dst, *str++, mbs); |
41 | ssize_t l = wcrtomb (dst, *str++, mbs); |
42 | if (l < 0) |
42 | if (l < 0) |
43 | *dst++ = '?'; |
43 | *dst++ = '?'; |
44 | else |
44 | else |
45 | dst += l; |
45 | dst += l; |
46 | } |
46 | } |
47 | |
47 | |
48 | *dst++ = 0; |
48 | *dst++ = 0; |
49 | |
49 | |
50 | return r; |
50 | return (char *)rxvt_realloc (r, dst - r); |
51 | } |
51 | } |
52 | |
52 | |
53 | wchar_t * |
53 | wchar_t * |
54 | rxvt_mbstowcs (const char *str, int len) |
54 | rxvt_mbstowcs (const char *str, int len) |
55 | { |
55 | { |
56 | if (len < 0) len = strlen (str); |
56 | if (len < 0) len = strlen (str); |
57 | |
57 | |
58 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); |
58 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); |
59 | |
59 | |
60 | if (mbstowcs (r, str, len + 1) < 0) |
60 | if ((ssize_t)mbstowcs (r, str, len + 1) < 0) |
61 | *r = 0; |
61 | *r = 0; |
62 | |
62 | |
63 | return r; |
63 | return r; |
64 | } |
64 | } |
65 | |
65 | |
… | |
… | |
71 | char *r = (char *)rxvt_malloc (len * 4 + 1); |
71 | char *r = (char *)rxvt_malloc (len * 4 + 1); |
72 | char *p = r; |
72 | char *p = r; |
73 | |
73 | |
74 | while (len--) |
74 | while (len--) |
75 | { |
75 | { |
76 | unicode_t w = *str++; |
76 | unicode_t w = *str++ & UNICODE_MASK; |
77 | |
77 | |
78 | if (w < 0x000080) |
78 | if (w < 0x000080) |
79 | *p++ = w; |
79 | *p++ = w; |
80 | else if (w < 0x000800) |
80 | else if (w < 0x000800) |
81 | *p++ = 0xc0 | ( w >> 6), |
81 | *p++ = 0xc0 | ( w >> 6), |
… | |
… | |
91 | *p++ = 0x80 | ( w & 0x3f); |
91 | *p++ = 0x80 | ( w & 0x3f); |
92 | else |
92 | else |
93 | *p++ = '?'; |
93 | *p++ = '?'; |
94 | } |
94 | } |
95 | |
95 | |
96 | *p = 0; |
96 | *p++ = 0; |
97 | |
97 | |
98 | return r; |
98 | return (char *)rxvt_realloc (r, p - r); |
99 | } |
99 | } |
100 | |
100 | |
101 | wchar_t * |
101 | wchar_t * |
102 | rxvt_utf8towcs (const char *str, int len) |
102 | rxvt_utf8towcs (const char *str, int len) |
103 | { |
103 | { |
104 | if (len < 0) len = strlen (str); |
104 | if (len < 0) len = strlen (str); |
105 | |
105 | |
106 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); |
106 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)), |
107 | wchar_t *p = r; |
107 | *p = r; |
108 | |
108 | |
109 | unsigned char *s = (unsigned char *)str; |
109 | unsigned char *s = (unsigned char *)str, |
|
|
110 | *e = s + len; |
110 | |
111 | |
111 | while (len) |
112 | for (;;) |
112 | { |
113 | { |
|
|
114 | len = e - s; |
|
|
115 | |
|
|
116 | if (len == 0) |
|
|
117 | break; |
113 | if (s[0] < 0x80) |
118 | else if (s[0] < 0x80) |
114 | { |
|
|
115 | *p++ = *s++; len--; |
119 | *p++ = *s++; |
116 | } |
|
|
117 | else if (len > 0 |
120 | else if (len >= 2 |
118 | && s[0] >= 0xc2 && s[0] <= 0xdf |
121 | && s[0] >= 0xc2 && s[0] <= 0xdf |
119 | && (s[1] & 0xc0) == 0x80) |
122 | && (s[1] & 0xc0) == 0x80) |
120 | { |
123 | { |
121 | *p++ = ((s[0] & 0x1f) << 6) |
124 | *p++ = ((s[0] & 0x1f) << 6) |
122 | | (s[1] & 0x3f); |
125 | | (s[1] & 0x3f); |
123 | s += 2; len -= 2; |
126 | s += 2; |
124 | } |
127 | } |
125 | else if (len > 1 |
128 | else if (len >= 3 |
126 | && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) |
129 | && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) |
127 | || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf) |
130 | || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf) |
128 | || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) |
131 | || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) |
129 | || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf) |
132 | || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf) |
130 | ) |
133 | ) |
131 | && (s[2] & 0xc0) == 0x80) |
134 | && (s[2] & 0xc0) == 0x80) |
132 | { |
135 | { |
133 | *p++ = ((s[0] & 0x0f) << 12) |
136 | *p++ = ((s[0] & 0x0f) << 12) |
134 | | ((s[1] & 0x3f) << 6) |
137 | | ((s[1] & 0x3f) << 6) |
135 | | (s[2] & 0x3f); |
138 | | (s[2] & 0x3f); |
136 | s += 3; len -= 3; |
139 | s += 3; |
137 | } |
140 | } |
138 | else if (len > 2 |
141 | else if (len >= 4 |
139 | && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) |
142 | && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) |
140 | || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf) |
143 | || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf) |
141 | || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) |
144 | || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) |
142 | ) |
145 | ) |
143 | && (s[2] & 0xc0) == 0x80 |
146 | && (s[2] & 0xc0) == 0x80 |
… | |
… | |
145 | { |
148 | { |
146 | *p++ = ((s[0] & 0x07) << 18) |
149 | *p++ = ((s[0] & 0x07) << 18) |
147 | | ((s[1] & 0x3f) << 12) |
150 | | ((s[1] & 0x3f) << 12) |
148 | | ((s[2] & 0x3f) << 6) |
151 | | ((s[2] & 0x3f) << 6) |
149 | | (s[3] & 0x3f); |
152 | | (s[3] & 0x3f); |
150 | s += 4; len -= 4; |
153 | s += 4; |
151 | } |
154 | } |
152 | else |
155 | else |
153 | { |
156 | { |
154 | *p++ = 0xfffd; |
157 | *p++ = 0xfffd; |
155 | s++; len--; |
158 | s++; |
156 | } |
159 | } |
157 | } |
160 | } |
158 | |
161 | |
159 | *p = 0; |
162 | *p = 0; |
160 | |
163 | |
… | |
… | |
279 | /* INTPROTO */ |
282 | /* INTPROTO */ |
280 | char * |
283 | char * |
281 | rxvt_Str_trim (char *str) |
284 | rxvt_Str_trim (char *str) |
282 | { |
285 | { |
283 | char *r, *s; |
286 | char *r, *s; |
284 | int n; |
|
|
285 | |
287 | |
286 | if (!str || !*str) /* shortcut */ |
288 | if (!str || !*str) /* shortcut */ |
287 | return str; |
289 | return str; |
288 | |
290 | |
289 | /* skip leading spaces */ |
291 | /* skip leading spaces */ |
290 | for (s = str; *s && isspace (*s); s++) ; |
292 | for (s = str; *s && isspace (*s); s++) ; |
|
|
293 | |
291 | /* goto end of string */ |
294 | /* goto end of string */ |
292 | for (n = 0, r = s; *r++; n++) ; |
295 | r = s + strlen (s) - 1; |
293 | r -= 2; |
296 | |
294 | /* dump return */ |
297 | /* dump return and other trailing whitespace */ |
295 | if (n > 0 && *r == '\n') |
298 | while (r > s && isspace (*r)) |
296 | n--, r--; |
299 | r--; |
297 | /* backtrack along trailing spaces */ |
300 | |
298 | for (; n > 0 && isspace (*r); r--, n--) ; |
301 | #if 0 |
299 | /* skip matching leading/trailing quotes */ |
302 | /* skip matching leading/trailing quotes */ |
300 | if (*s == '"' && *r == '"' && n > 1) |
303 | if (*s == '"' && *r == '"' && n > 1) |
301 | { |
304 | { |
302 | s++; |
305 | s++; |
303 | n -= 2; |
306 | n -= 2; |
304 | } |
307 | } |
|
|
308 | #endif |
305 | |
309 | |
306 | /* copy back over: forwards copy */ |
310 | memmove (str, s, r + 1 - s); |
307 | for (r = str; n; n--) |
311 | str[r + 1 - s] = 0; |
308 | *r++ = *s++; |
|
|
309 | *r = '\0'; |
|
|
310 | |
312 | |
311 | return str; |
313 | return str; |
312 | } |
314 | } |
313 | |
315 | |
314 | /* |
316 | /* |
… | |
… | |
397 | return (d - str); |
399 | return (d - str); |
398 | } |
400 | } |
399 | |
401 | |
400 | /* |
402 | /* |
401 | * Split a comma-separated string into an array, stripping leading and |
403 | * Split a comma-separated string into an array, stripping leading and |
402 | * trailing spaces (and paired quotes) from each entry. Empty strings |
404 | * trailing spaces from each entry. Empty strings are properly returned |
403 | * are properly returned |
|
|
404 | * Caller should free each entry and array when done |
405 | * Caller should free each entry and array when done |
405 | */ |
406 | */ |
406 | /* INTPROTO */ |
407 | /* INTPROTO */ |
407 | char ** |
408 | char ** |
408 | rxvt_splitcommastring (const char *cs) |
409 | rxvt_splitcommastring (const char *cs) |
… | |
… | |
415 | s = ""; |
416 | s = ""; |
416 | |
417 | |
417 | for (n = 1, t = s; *t; t++) |
418 | for (n = 1, t = s; *t; t++) |
418 | if (*t == ',') |
419 | if (*t == ',') |
419 | n++; |
420 | n++; |
|
|
421 | |
420 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
422 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
421 | ret[n] = NULL; |
423 | ret[n] = NULL; |
422 | |
424 | |
423 | for (l = 0, t = s; l < n; l++) |
425 | for (l = 0, t = s; l < n; l++) |
424 | { |
426 | { |
… | |
… | |
428 | strncpy (ret[l], s, p); |
430 | strncpy (ret[l], s, p); |
429 | ret[l][p] = '\0'; |
431 | ret[l][p] = '\0'; |
430 | rxvt_Str_trim (ret[l]); |
432 | rxvt_Str_trim (ret[l]); |
431 | s = ++t; |
433 | s = ++t; |
432 | } |
434 | } |
|
|
435 | |
433 | return ret; |
436 | return ret; |
434 | } |
437 | } |
435 | |
438 | |
436 | void |
439 | void |
437 | rxvt_freecommastring (char **cs) |
440 | rxvt_freecommastring (char **cs) |