… | |
… | |
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 | } |
58 | |
171 | |
59 | /* INTPROTO */ |
|
|
60 | char * |
172 | char * |
61 | rxvt_r_basename (const char *str) |
173 | rxvt_r_basename (const char *str) |
62 | { |
174 | { |
63 | char *base = strrchr (str, '/'); |
175 | char *base = strrchr (str, '/'); |
64 | |
176 | |
… | |
… | |
66 | } |
178 | } |
67 | |
179 | |
68 | /* |
180 | /* |
69 | * Print an error message |
181 | * Print an error message |
70 | */ |
182 | */ |
71 | /* INTPROTO */ |
|
|
72 | void |
183 | void |
73 | rxvt_vlog (const char *fmt, va_list arg_ptr) |
184 | rxvt_vlog (const char *fmt, va_list arg_ptr) |
74 | { |
185 | { |
75 | char msg[1024]; |
186 | char msg[1024]; |
76 | |
187 | |
… | |
… | |
80 | (*GET_R->log_hook) (msg); |
191 | (*GET_R->log_hook) (msg); |
81 | else |
192 | else |
82 | write (STDOUT_FILENO, msg, strlen (msg)); |
193 | write (STDOUT_FILENO, msg, strlen (msg)); |
83 | } |
194 | } |
84 | |
195 | |
85 | /* INTPROTO */ |
|
|
86 | void |
196 | void |
87 | rxvt_log (const char *fmt,...) |
197 | rxvt_log (const char *fmt,...) |
88 | { |
198 | { |
89 | va_list arg_ptr; |
199 | va_list arg_ptr; |
90 | |
200 | |
… | |
… | |
94 | } |
204 | } |
95 | |
205 | |
96 | /* |
206 | /* |
97 | * Print an error message |
207 | * Print an error message |
98 | */ |
208 | */ |
99 | /* INTPROTO */ |
|
|
100 | void |
209 | void |
101 | rxvt_warn (const char *fmt,...) |
210 | rxvt_warn (const char *fmt,...) |
102 | { |
211 | { |
103 | va_list arg_ptr; |
212 | va_list arg_ptr; |
104 | |
213 | |
… | |
… | |
107 | va_start (arg_ptr, fmt); |
216 | va_start (arg_ptr, fmt); |
108 | rxvt_vlog (fmt, arg_ptr); |
217 | rxvt_vlog (fmt, arg_ptr); |
109 | va_end (arg_ptr); |
218 | va_end (arg_ptr); |
110 | } |
219 | } |
111 | |
220 | |
112 | /* INTPROTO */ |
|
|
113 | void |
221 | void |
114 | rxvt_fatal (const char *fmt,...) |
222 | rxvt_fatal (const char *fmt,...) |
115 | { |
223 | { |
116 | va_list arg_ptr; |
224 | va_list arg_ptr; |
117 | |
225 | |
… | |
… | |
124 | rxvt_exit_failure (); |
232 | rxvt_exit_failure (); |
125 | } |
233 | } |
126 | |
234 | |
127 | class rxvt_failure_exception rxvt_failure_exception; |
235 | class rxvt_failure_exception rxvt_failure_exception; |
128 | |
236 | |
129 | /* INTPROTO */ |
|
|
130 | void |
237 | void |
131 | rxvt_exit_failure () |
238 | rxvt_exit_failure () |
132 | { |
239 | { |
133 | throw (rxvt_failure_exception); |
240 | throw (rxvt_failure_exception); |
134 | } |
241 | } |
… | |
… | |
139 | * No Match |
246 | * No Match |
140 | * return: 0 |
247 | * return: 0 |
141 | * Match |
248 | * Match |
142 | * return: strlen (S2) |
249 | * return: strlen (S2) |
143 | */ |
250 | */ |
144 | /* INTPROTO */ |
|
|
145 | int |
251 | int |
146 | rxvt_Str_match (const char *s1, const char *s2) |
252 | rxvt_Str_match (const char *s1, const char *s2) |
147 | { |
253 | { |
148 | int n = strlen (s2); |
254 | int n = strlen (s2); |
149 | |
255 | |
150 | return ((strncmp (s1, s2, n) == 0) ? n : 0); |
256 | return ((strncmp (s1, s2, n) == 0) ? n : 0); |
151 | } |
257 | } |
152 | |
258 | |
153 | /* INTPROTO */ |
|
|
154 | const char * |
259 | const char * |
155 | rxvt_Str_skip_space (const char *str) |
260 | rxvt_Str_skip_space (const char *str) |
156 | { |
261 | { |
157 | if (str) |
262 | if (str) |
158 | while (*str && isspace (*str)) |
263 | while (*str && isspace (*str)) |
… | |
… | |
163 | |
268 | |
164 | /* |
269 | /* |
165 | * remove leading/trailing space and strip-off leading/trailing quotes. |
270 | * remove leading/trailing space and strip-off leading/trailing quotes. |
166 | * in place. |
271 | * in place. |
167 | */ |
272 | */ |
168 | /* INTPROTO */ |
|
|
169 | char * |
273 | char * |
170 | rxvt_Str_trim (char *str) |
274 | rxvt_Str_trim (char *str) |
171 | { |
275 | { |
172 | char *r, *s; |
276 | char *r, *s; |
173 | int n; |
|
|
174 | |
277 | |
175 | if (!str || !*str) /* shortcut */ |
278 | if (!str || !*str) /* shortcut */ |
176 | return str; |
279 | return str; |
177 | |
280 | |
178 | /* skip leading spaces */ |
281 | /* skip leading spaces */ |
179 | for (s = str; *s && isspace (*s); s++) ; |
282 | for (s = str; *s && isspace (*s); s++) ; |
|
|
283 | |
180 | /* goto end of string */ |
284 | /* goto end of string */ |
181 | for (n = 0, r = s; *r++; n++) ; |
285 | r = s + strlen (s) - 1; |
182 | r -= 2; |
286 | |
183 | /* dump return */ |
287 | /* dump return and other trailing whitespace */ |
184 | if (n > 0 && *r == '\n') |
288 | while (r > s && isspace (*r)) |
185 | n--, r--; |
289 | r--; |
186 | /* backtrack along trailing spaces */ |
290 | |
187 | for (; n > 0 && isspace (*r); r--, n--) ; |
291 | #if 0 |
188 | /* skip matching leading/trailing quotes */ |
292 | /* skip matching leading/trailing quotes */ |
189 | if (*s == '"' && *r == '"' && n > 1) |
293 | if (*s == '"' && *r == '"' && n > 1) |
190 | { |
294 | { |
191 | s++; |
295 | s++; |
192 | n -= 2; |
296 | n -= 2; |
193 | } |
297 | } |
|
|
298 | #endif |
194 | |
299 | |
195 | /* copy back over: forwards copy */ |
300 | memmove (str, s, r + 1 - s); |
196 | for (r = str; n; n--) |
301 | str[r + 1 - s] = 0; |
197 | *r++ = *s++; |
|
|
198 | *r = '\0'; |
|
|
199 | |
302 | |
200 | return str; |
303 | return str; |
201 | } |
304 | } |
202 | |
305 | |
203 | /* |
306 | /* |
… | |
… | |
212 | * "M-x" prefixed strings, append "\r" if needed |
315 | * "M-x" prefixed strings, append "\r" if needed |
213 | * "\E]" prefixed strings (XTerm escape sequence) append ST if needed |
316 | * "\E]" prefixed strings (XTerm escape sequence) append ST if needed |
214 | * |
317 | * |
215 | * returns the converted string length |
318 | * returns the converted string length |
216 | */ |
319 | */ |
217 | /* INTPROTO */ |
|
|
218 | int |
320 | int |
219 | rxvt_Str_escaped (char *str) |
321 | rxvt_Str_escaped (char *str) |
220 | { |
322 | { |
221 | char ch, *s, *d; |
323 | char ch, *s, *d; |
222 | int i, num, append = 0; |
324 | int i, num, append = 0; |
… | |
… | |
286 | return (d - str); |
388 | return (d - str); |
287 | } |
389 | } |
288 | |
390 | |
289 | /* |
391 | /* |
290 | * Split a comma-separated string into an array, stripping leading and |
392 | * Split a comma-separated string into an array, stripping leading and |
291 | * trailing spaces (and paired quotes) from each entry. Empty strings |
393 | * trailing spaces from each entry. Empty strings are properly returned |
292 | * are properly returned |
|
|
293 | * Caller should free each entry and array when done |
394 | * Caller should free each entry and array when done |
294 | */ |
395 | */ |
295 | /* INTPROTO */ |
|
|
296 | char ** |
396 | char ** |
297 | rxvt_splitcommastring (const char *cs) |
397 | rxvt_splitcommastring (const char *cs) |
298 | { |
398 | { |
299 | int l, n, p; |
399 | int l, n, p; |
300 | const char *s, *t; |
400 | const char *s, *t; |
… | |
… | |
304 | s = ""; |
404 | s = ""; |
305 | |
405 | |
306 | for (n = 1, t = s; *t; t++) |
406 | for (n = 1, t = s; *t; t++) |
307 | if (*t == ',') |
407 | if (*t == ',') |
308 | n++; |
408 | n++; |
|
|
409 | |
309 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
410 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
310 | ret[n] = NULL; |
411 | ret[n] = NULL; |
311 | |
412 | |
312 | for (l = 0, t = s; l < n; l++) |
413 | for (l = 0, t = s; l < n; l++) |
313 | { |
414 | { |
… | |
… | |
317 | strncpy (ret[l], s, p); |
418 | strncpy (ret[l], s, p); |
318 | ret[l][p] = '\0'; |
419 | ret[l][p] = '\0'; |
319 | rxvt_Str_trim (ret[l]); |
420 | rxvt_Str_trim (ret[l]); |
320 | s = ++t; |
421 | s = ++t; |
321 | } |
422 | } |
|
|
423 | |
322 | return ret; |
424 | return ret; |
|
|
425 | } |
|
|
426 | |
|
|
427 | void |
|
|
428 | rxvt_freecommastring (char **cs) |
|
|
429 | { |
|
|
430 | for (int i = 0; cs[i]; ++i) |
|
|
431 | free (cs[i]); |
|
|
432 | |
|
|
433 | free (cs); |
323 | } |
434 | } |
324 | |
435 | |
325 | /*----------------------------------------------------------------------* |
436 | /*----------------------------------------------------------------------* |
326 | * file searching |
437 | * file searching |
327 | */ |
438 | */ |
… | |
… | |
333 | * search for FILE in the current working directory, and within the |
444 | * search for FILE in the current working directory, and within the |
334 | * colon-delimited PATHLIST, adding the file extension EXT if required. |
445 | * colon-delimited PATHLIST, adding the file extension EXT if required. |
335 | * |
446 | * |
336 | * FILE is either semi-colon or zero terminated |
447 | * FILE is either semi-colon or zero terminated |
337 | */ |
448 | */ |
338 | /* INTPROTO */ |
|
|
339 | char * |
449 | char * |
340 | rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) |
450 | rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) |
341 | { |
451 | { |
342 | int maxpath, len; |
452 | int maxpath, len; |
343 | const char *p, *path; |
453 | const char *p, *path; |
… | |
… | |
406 | } |
516 | } |
407 | } |
517 | } |
408 | return NULL; |
518 | return NULL; |
409 | } |
519 | } |
410 | |
520 | |
411 | /* INTPROTO */ |
|
|
412 | char * |
521 | char * |
413 | rxvt_File_find (const char *file, const char *ext, const char *path) |
522 | rxvt_File_find (const char *file, const char *ext, const char *path) |
414 | { |
523 | { |
415 | char *f; |
524 | char *f; |
416 | |
525 | |
… | |
… | |
439 | |
548 | |
440 | /* |
549 | /* |
441 | * Draw top/left and bottom/right border shadows around windows |
550 | * Draw top/left and bottom/right border shadows around windows |
442 | */ |
551 | */ |
443 | #if defined(RXVT_SCROLLBAR) || defined(MENUBAR) |
552 | #if defined(RXVT_SCROLLBAR) || defined(MENUBAR) |
444 | /* INTPROTO */ |
|
|
445 | void |
553 | void |
446 | rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) |
554 | rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) |
447 | { |
555 | { |
448 | int shadow; |
556 | int shadow; |
449 | |
557 | |
450 | shadow = (w == 0 || h == 0) ? 1 : SHADOW; |
558 | shadow = (w == 0 || h == 0) ? 1 : MENU_SHADOW; |
451 | w += x - 1; |
559 | w += x - 1; |
452 | h += y - 1; |
560 | h += y - 1; |
453 | for (; shadow-- > 0; x++, y++, w--, h--) |
561 | for (; shadow-- > 0; x++, y++, w--, h--) |
454 | { |
562 | { |
455 | XDrawLine (display, win, topShadow, x, y, w, y); |
563 | XDrawLine (display, win, topShadow, x, y, w, y); |
… | |
… | |
460 | } |
568 | } |
461 | #endif |
569 | #endif |
462 | |
570 | |
463 | /* button shapes */ |
571 | /* button shapes */ |
464 | #ifdef MENUBAR |
572 | #ifdef MENUBAR |
465 | /* INTPROTO */ |
|
|
466 | void |
573 | void |
467 | rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) |
574 | rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) |
468 | { |
575 | { |
469 | switch (type) |
576 | switch (type) |
470 | { |
577 | { |
… | |
… | |
501 | #endif |
608 | #endif |
502 | |
609 | |
503 | } |
610 | } |
504 | } |
611 | } |
505 | #endif |
612 | #endif |
|
|
613 | |
|
|
614 | // should not be used in interactive programs, for obvious reasons |
|
|
615 | void rxvt_usleep (int usecs) |
|
|
616 | { |
|
|
617 | #if HAVE_NANOSLEEP |
|
|
618 | struct timespec ts; |
|
|
619 | |
|
|
620 | ts.tv_sec = 0; |
|
|
621 | ts.tv_nsec = usecs * 1000; |
|
|
622 | nanosleep (&ts, NULL); |
|
|
623 | #else |
|
|
624 | /* use select for timing */ |
|
|
625 | struct timeval tv; |
|
|
626 | |
|
|
627 | tv.tv_sec = 0; |
|
|
628 | tv.tv_usec = usecs; |
|
|
629 | select (0, NULL, NULL, NULL, &tv); |
|
|
630 | #endif |
|
|
631 | } |
|
|
632 | |
506 | /*----------------------- end-of-file (C source) -----------------------*/ |
633 | /*----------------------- end-of-file (C source) -----------------------*/ |
|
|
634 | |