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