… | |
… | |
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 | { |
… | |
… | |
168 | rxvt_strdup (const char *str) |
167 | rxvt_strdup (const char *str) |
169 | { |
168 | { |
170 | return str ? strdup (str) : 0; |
169 | return str ? strdup (str) : 0; |
171 | } |
170 | } |
172 | |
171 | |
173 | /* INTPROTO */ |
|
|
174 | char * |
172 | char * |
175 | rxvt_r_basename (const char *str) |
173 | rxvt_r_basename (const char *str) |
176 | { |
174 | { |
177 | char *base = strrchr (str, '/'); |
175 | char *base = strrchr (str, '/'); |
178 | |
176 | |
… | |
… | |
180 | } |
178 | } |
181 | |
179 | |
182 | /* |
180 | /* |
183 | * Print an error message |
181 | * Print an error message |
184 | */ |
182 | */ |
185 | /* INTPROTO */ |
|
|
186 | void |
183 | void |
187 | rxvt_vlog (const char *fmt, va_list arg_ptr) |
184 | rxvt_vlog (const char *fmt, va_list arg_ptr) |
188 | { |
185 | { |
189 | char msg[1024]; |
186 | char msg[1024]; |
190 | |
187 | |
… | |
… | |
194 | (*GET_R->log_hook) (msg); |
191 | (*GET_R->log_hook) (msg); |
195 | else |
192 | else |
196 | write (STDOUT_FILENO, msg, strlen (msg)); |
193 | write (STDOUT_FILENO, msg, strlen (msg)); |
197 | } |
194 | } |
198 | |
195 | |
199 | /* INTPROTO */ |
|
|
200 | void |
196 | void |
201 | rxvt_log (const char *fmt,...) |
197 | rxvt_log (const char *fmt,...) |
202 | { |
198 | { |
203 | va_list arg_ptr; |
199 | va_list arg_ptr; |
204 | |
200 | |
… | |
… | |
208 | } |
204 | } |
209 | |
205 | |
210 | /* |
206 | /* |
211 | * Print an error message |
207 | * Print an error message |
212 | */ |
208 | */ |
213 | /* INTPROTO */ |
|
|
214 | void |
209 | void |
215 | rxvt_warn (const char *fmt,...) |
210 | rxvt_warn (const char *fmt,...) |
216 | { |
211 | { |
217 | va_list arg_ptr; |
212 | va_list arg_ptr; |
218 | |
213 | |
… | |
… | |
221 | va_start (arg_ptr, fmt); |
216 | va_start (arg_ptr, fmt); |
222 | rxvt_vlog (fmt, arg_ptr); |
217 | rxvt_vlog (fmt, arg_ptr); |
223 | va_end (arg_ptr); |
218 | va_end (arg_ptr); |
224 | } |
219 | } |
225 | |
220 | |
226 | /* INTPROTO */ |
|
|
227 | void |
221 | void |
228 | rxvt_fatal (const char *fmt,...) |
222 | rxvt_fatal (const char *fmt,...) |
229 | { |
223 | { |
230 | va_list arg_ptr; |
224 | va_list arg_ptr; |
231 | |
225 | |
… | |
… | |
238 | rxvt_exit_failure (); |
232 | rxvt_exit_failure (); |
239 | } |
233 | } |
240 | |
234 | |
241 | class rxvt_failure_exception rxvt_failure_exception; |
235 | class rxvt_failure_exception rxvt_failure_exception; |
242 | |
236 | |
243 | /* INTPROTO */ |
|
|
244 | void |
237 | void |
245 | rxvt_exit_failure () |
238 | rxvt_exit_failure () |
246 | { |
239 | { |
247 | throw (rxvt_failure_exception); |
240 | throw (rxvt_failure_exception); |
248 | } |
241 | } |
… | |
… | |
253 | * No Match |
246 | * No Match |
254 | * return: 0 |
247 | * return: 0 |
255 | * Match |
248 | * Match |
256 | * return: strlen (S2) |
249 | * return: strlen (S2) |
257 | */ |
250 | */ |
258 | /* INTPROTO */ |
|
|
259 | int |
251 | int |
260 | rxvt_Str_match (const char *s1, const char *s2) |
252 | rxvt_Str_match (const char *s1, const char *s2) |
261 | { |
253 | { |
262 | int n = strlen (s2); |
254 | int n = strlen (s2); |
263 | |
255 | |
264 | return ((strncmp (s1, s2, n) == 0) ? n : 0); |
256 | return ((strncmp (s1, s2, n) == 0) ? n : 0); |
265 | } |
257 | } |
266 | |
258 | |
267 | /* INTPROTO */ |
|
|
268 | const char * |
259 | const char * |
269 | rxvt_Str_skip_space (const char *str) |
260 | rxvt_Str_skip_space (const char *str) |
270 | { |
261 | { |
271 | if (str) |
262 | if (str) |
272 | while (*str && isspace (*str)) |
263 | while (*str && isspace (*str)) |
… | |
… | |
277 | |
268 | |
278 | /* |
269 | /* |
279 | * remove leading/trailing space and strip-off leading/trailing quotes. |
270 | * remove leading/trailing space and strip-off leading/trailing quotes. |
280 | * in place. |
271 | * in place. |
281 | */ |
272 | */ |
282 | /* INTPROTO */ |
|
|
283 | char * |
273 | char * |
284 | rxvt_Str_trim (char *str) |
274 | rxvt_Str_trim (char *str) |
285 | { |
275 | { |
286 | char *r, *s; |
276 | char *r, *s; |
287 | int n; |
|
|
288 | |
277 | |
289 | if (!str || !*str) /* shortcut */ |
278 | if (!str || !*str) /* shortcut */ |
290 | return str; |
279 | return str; |
291 | |
280 | |
292 | /* skip leading spaces */ |
281 | /* skip leading spaces */ |
293 | for (s = str; *s && isspace (*s); s++) ; |
282 | for (s = str; *s && isspace (*s); s++) ; |
|
|
283 | |
294 | /* goto end of string */ |
284 | /* goto end of string */ |
295 | for (n = 0, r = s; *r++; n++) ; |
285 | r = s + strlen (s) - 1; |
296 | r -= 2; |
286 | |
297 | /* dump return */ |
287 | /* dump return and other trailing whitespace */ |
298 | if (n > 0 && *r == '\n') |
288 | while (r > s && isspace (*r)) |
299 | n--, r--; |
289 | r--; |
300 | /* backtrack along trailing spaces */ |
290 | |
301 | for (; n > 0 && isspace (*r); r--, n--) ; |
291 | #if 0 |
302 | /* skip matching leading/trailing quotes */ |
292 | /* skip matching leading/trailing quotes */ |
303 | if (*s == '"' && *r == '"' && n > 1) |
293 | if (*s == '"' && *r == '"' && n > 1) |
304 | { |
294 | { |
305 | s++; |
295 | s++; |
306 | n -= 2; |
296 | n -= 2; |
307 | } |
297 | } |
|
|
298 | #endif |
308 | |
299 | |
309 | /* copy back over: forwards copy */ |
300 | memmove (str, s, r + 1 - s); |
310 | for (r = str; n; n--) |
301 | str[r + 1 - s] = 0; |
311 | *r++ = *s++; |
|
|
312 | *r = '\0'; |
|
|
313 | |
302 | |
314 | return str; |
303 | return str; |
315 | } |
304 | } |
316 | |
305 | |
317 | /* |
306 | /* |
… | |
… | |
326 | * "M-x" prefixed strings, append "\r" if needed |
315 | * "M-x" prefixed strings, append "\r" if needed |
327 | * "\E]" prefixed strings (XTerm escape sequence) append ST if needed |
316 | * "\E]" prefixed strings (XTerm escape sequence) append ST if needed |
328 | * |
317 | * |
329 | * returns the converted string length |
318 | * returns the converted string length |
330 | */ |
319 | */ |
331 | /* INTPROTO */ |
|
|
332 | int |
320 | int |
333 | rxvt_Str_escaped (char *str) |
321 | rxvt_Str_escaped (char *str) |
334 | { |
322 | { |
335 | char ch, *s, *d; |
323 | char ch, *s, *d; |
336 | int i, num, append = 0; |
324 | int i, num, append = 0; |
… | |
… | |
400 | return (d - str); |
388 | return (d - str); |
401 | } |
389 | } |
402 | |
390 | |
403 | /* |
391 | /* |
404 | * Split a comma-separated string into an array, stripping leading and |
392 | * Split a comma-separated string into an array, stripping leading and |
405 | * trailing spaces (and paired quotes) from each entry. Empty strings |
393 | * trailing spaces from each entry. Empty strings are properly returned |
406 | * are properly returned |
|
|
407 | * Caller should free each entry and array when done |
394 | * Caller should free each entry and array when done |
408 | */ |
395 | */ |
409 | /* INTPROTO */ |
|
|
410 | char ** |
396 | char ** |
411 | rxvt_splitcommastring (const char *cs) |
397 | rxvt_splitcommastring (const char *cs) |
412 | { |
398 | { |
413 | int l, n, p; |
399 | int l, n, p; |
414 | const char *s, *t; |
400 | const char *s, *t; |
… | |
… | |
418 | s = ""; |
404 | s = ""; |
419 | |
405 | |
420 | for (n = 1, t = s; *t; t++) |
406 | for (n = 1, t = s; *t; t++) |
421 | if (*t == ',') |
407 | if (*t == ',') |
422 | n++; |
408 | n++; |
|
|
409 | |
423 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
410 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
424 | ret[n] = NULL; |
411 | ret[n] = NULL; |
425 | |
412 | |
426 | for (l = 0, t = s; l < n; l++) |
413 | for (l = 0, t = s; l < n; l++) |
427 | { |
414 | { |
… | |
… | |
431 | strncpy (ret[l], s, p); |
418 | strncpy (ret[l], s, p); |
432 | ret[l][p] = '\0'; |
419 | ret[l][p] = '\0'; |
433 | rxvt_Str_trim (ret[l]); |
420 | rxvt_Str_trim (ret[l]); |
434 | s = ++t; |
421 | s = ++t; |
435 | } |
422 | } |
|
|
423 | |
436 | return ret; |
424 | return ret; |
437 | } |
425 | } |
438 | |
426 | |
439 | void |
427 | void |
440 | rxvt_freecommastring (char **cs) |
428 | rxvt_freecommastring (char **cs) |
… | |
… | |
456 | * search for FILE in the current working directory, and within the |
444 | * search for FILE in the current working directory, and within the |
457 | * colon-delimited PATHLIST, adding the file extension EXT if required. |
445 | * colon-delimited PATHLIST, adding the file extension EXT if required. |
458 | * |
446 | * |
459 | * FILE is either semi-colon or zero terminated |
447 | * FILE is either semi-colon or zero terminated |
460 | */ |
448 | */ |
461 | /* INTPROTO */ |
|
|
462 | char * |
449 | char * |
463 | 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) |
464 | { |
451 | { |
465 | int maxpath, len; |
452 | int maxpath, len; |
466 | const char *p, *path; |
453 | const char *p, *path; |
… | |
… | |
529 | } |
516 | } |
530 | } |
517 | } |
531 | return NULL; |
518 | return NULL; |
532 | } |
519 | } |
533 | |
520 | |
534 | /* INTPROTO */ |
|
|
535 | char * |
521 | char * |
536 | 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) |
537 | { |
523 | { |
538 | char *f; |
524 | char *f; |
539 | |
525 | |
… | |
… | |
562 | |
548 | |
563 | /* |
549 | /* |
564 | * Draw top/left and bottom/right border shadows around windows |
550 | * Draw top/left and bottom/right border shadows around windows |
565 | */ |
551 | */ |
566 | #if defined(RXVT_SCROLLBAR) || defined(MENUBAR) |
552 | #if defined(RXVT_SCROLLBAR) || defined(MENUBAR) |
567 | /* INTPROTO */ |
|
|
568 | void |
553 | void |
569 | 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) |
570 | { |
555 | { |
571 | int shadow; |
556 | int shadow; |
572 | |
557 | |
… | |
… | |
583 | } |
568 | } |
584 | #endif |
569 | #endif |
585 | |
570 | |
586 | /* button shapes */ |
571 | /* button shapes */ |
587 | #ifdef MENUBAR |
572 | #ifdef MENUBAR |
588 | /* INTPROTO */ |
|
|
589 | void |
573 | void |
590 | 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) |
591 | { |
575 | { |
592 | switch (type) |
576 | switch (type) |
593 | { |
577 | { |
… | |
… | |
624 | #endif |
608 | #endif |
625 | |
609 | |
626 | } |
610 | } |
627 | } |
611 | } |
628 | #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 | |
629 | /*----------------------- end-of-file (C source) -----------------------*/ |
633 | /*----------------------- end-of-file (C source) -----------------------*/ |
|
|
634 | |