… | |
… | |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 1996 mj olesen <olesen@me.QueensU.CA> Queen's Univ at Kingston |
6 | * Copyright (c) 1996 mj olesen <olesen@me.QueensU.CA> Queen's Univ at Kingston |
7 | * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> |
7 | * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> |
8 | * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com> |
8 | * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com> |
9 | * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com> |
9 | * Copyright (c) 2003-2006 Marc Lehmann <schmorp@schmorp.de> |
10 | * |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License as published by |
12 | * it under the terms of the GNU General Public License as published by |
13 | * the Free Software Foundation; either version 2 of the License, or |
13 | * the Free Software Foundation; either version 3 of the License, or |
14 | * (at your option) any later version. |
14 | * (at your option) any later version. |
15 | * |
15 | * |
16 | * This program is distributed in the hope that it will be useful, |
16 | * This program is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
… | |
… | |
38 | while (len--) |
38 | while (len--) |
39 | { |
39 | { |
40 | ssize_t l = wcrtomb (dst, *str++, mbs); |
40 | ssize_t l = wcrtomb (dst, *str++, mbs); |
41 | |
41 | |
42 | if (l < 0) |
42 | if (l < 0) |
|
|
43 | { |
43 | *dst++ = '?'; |
44 | *dst++ = '?'; |
|
|
45 | wcrtomb (0, 0, mbs); // reset undefined state |
|
|
46 | } |
44 | else |
47 | else |
45 | dst += l; |
48 | dst += l; |
46 | } |
49 | } |
47 | |
50 | |
48 | *dst++ = 0; |
51 | *dst++ = 0; |
… | |
… | |
116 | if (len == 0) |
119 | if (len == 0) |
117 | break; |
120 | break; |
118 | else if (s[0] < 0x80) |
121 | else if (s[0] < 0x80) |
119 | *p++ = *s++; |
122 | *p++ = *s++; |
120 | else if (len >= 2 |
123 | else if (len >= 2 |
121 | && s[0] >= 0xc2 && s[0] <= 0xdf |
124 | && s[0] >= 0xc2 && s[0] <= 0xdf |
122 | && (s[1] & 0xc0) == 0x80) |
125 | && (s[1] & 0xc0) == 0x80) |
123 | { |
126 | { |
124 | *p++ = ((s[0] & 0x1f) << 6) |
127 | *p++ = ((s[0] & 0x1f) << 6) |
125 | | (s[1] & 0x3f); |
128 | | (s[1] & 0x3f); |
126 | s += 2; |
129 | s += 2; |
… | |
… | |
144 | || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) |
147 | || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) |
145 | ) |
148 | ) |
146 | && (s[2] & 0xc0) == 0x80 |
149 | && (s[2] & 0xc0) == 0x80 |
147 | && (s[3] & 0xc0) == 0x80) |
150 | && (s[3] & 0xc0) == 0x80) |
148 | { |
151 | { |
149 | *p++ = ((s[0] & 0x07) << 18) |
152 | *p++ = ((s[0] & 0x07) << 18) |
150 | | ((s[1] & 0x3f) << 12) |
153 | | ((s[1] & 0x3f) << 12) |
151 | | ((s[2] & 0x3f) << 6) |
154 | | ((s[2] & 0x3f) << 6) |
152 | | (s[3] & 0x3f); |
155 | | (s[3] & 0x3f); |
153 | s += 4; |
156 | s += 4; |
154 | } |
157 | } |
155 | else |
158 | else |
156 | { |
159 | { |
… | |
… | |
162 | *p = 0; |
165 | *p = 0; |
163 | |
166 | |
164 | return r; |
167 | return r; |
165 | } |
168 | } |
166 | |
169 | |
167 | char * |
170 | const char * |
168 | rxvt_basename (const char *str) NOTHROW |
171 | rxvt_basename (const char *str) NOTHROW |
169 | { |
172 | { |
170 | char *base = strrchr (str, '/'); |
173 | const char *base = strrchr (str, '/'); |
171 | |
174 | |
172 | return (char *) (base ? base + 1 : str); |
175 | return base ? base + 1 : str; |
173 | } |
176 | } |
174 | |
177 | |
175 | /* |
178 | /* |
176 | * Print an error message |
179 | * Print an error message |
177 | */ |
180 | */ |
… | |
… | |
260 | |
263 | |
261 | return str; |
264 | return str; |
262 | } |
265 | } |
263 | |
266 | |
264 | /* |
267 | /* |
265 | * Split a comma-separated string into an array, stripping leading and |
268 | * Split a string into an array based on the given delimiter, stripping leading and |
266 | * trailing spaces from each entry. Empty strings are properly returned |
269 | * trailing spaces from each entry. Empty strings are properly returned |
267 | */ |
270 | */ |
268 | char ** |
271 | char ** |
269 | rxvt_splitcommastring (const char *cs) NOTHROW |
272 | rxvt_strsplit (char delim, const char *str) NOTHROW |
270 | { |
273 | { |
271 | int l, n, p; |
274 | int l, n; |
272 | const char *s, *t; |
275 | char *s, *t; |
273 | char **ret; |
276 | char **ret; |
274 | |
277 | |
275 | if ((s = cs) == NULL) |
278 | s = strdup (str ? str : ""); |
276 | s = ""; |
|
|
277 | |
279 | |
278 | for (n = 1, t = s; *t; t++) |
280 | for (n = 1, t = s; *t; t++) |
279 | if (*t == ',') |
281 | if (*t == delim) |
280 | n++; |
282 | n++; |
281 | |
283 | |
282 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
284 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
283 | ret[n] = NULL; |
285 | ret[n] = NULL; |
284 | |
286 | |
285 | for (l = 0, t = s; l < n; l++) |
287 | for (l = 0, t = s; l < n; l++) |
286 | { |
288 | { |
287 | for ( ; *t && *t != ','; t++) ; |
289 | for (; *t && *t != delim; t++) |
288 | p = t - s; |
290 | ; |
289 | ret[l] = (char *)malloc (p + 1); |
291 | *t = '\0'; |
290 | strncpy (ret[l], s, p); |
|
|
291 | ret[l][p] = '\0'; |
292 | ret[l] = s; |
292 | rxvt_strtrim (ret[l]); |
293 | rxvt_strtrim (ret[l]); |
293 | s = ++t; |
294 | s = ++t; |
294 | } |
295 | } |
295 | |
296 | |
296 | return ret; |
297 | return ret; |
297 | } |
298 | } |
298 | |
299 | |
299 | void |
300 | void * |
300 | rxvt_freecommastring (char **cs) NOTHROW |
301 | rxvt_malloc (size_t size) |
301 | { |
302 | { |
302 | for (int i = 0; cs[i]; ++i) |
303 | void *p = malloc (size); |
303 | free (cs[i]); |
|
|
304 | |
304 | |
305 | free (cs); |
305 | if (!p) |
306 | } |
306 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
307 | |
307 | |
|
|
308 | return p; |
|
|
309 | } |
308 | |
310 | |
|
|
311 | void * |
|
|
312 | rxvt_calloc (size_t number, size_t size) |
|
|
313 | { |
|
|
314 | void *p = calloc (number, size); |
309 | |
315 | |
|
|
316 | if (!p) |
|
|
317 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
|
|
318 | |
|
|
319 | return p; |
|
|
320 | } |
|
|
321 | |
|
|
322 | void * |
|
|
323 | rxvt_realloc (void *ptr, size_t size) |
|
|
324 | { |
|
|
325 | void *p = realloc (ptr, size); |
|
|
326 | |
|
|
327 | if (!p) |
|
|
328 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
|
|
329 | |
|
|
330 | return p; |
|
|
331 | } |
|
|
332 | |
|
|
333 | KeySym |
|
|
334 | rxvt_XKeycodeToKeysym (Display *dpy, KeyCode code, int index) |
|
|
335 | { |
|
|
336 | int size; |
|
|
337 | KeySym *mapping = XGetKeyboardMapping (dpy, code, 1, &size); |
|
|
338 | KeySym keysym = IN_RANGE_EXC (index, 0, size) ? mapping[index] : NoSymbol; |
|
|
339 | XFree (mapping); |
|
|
340 | return keysym; |
|
|
341 | } |