… | |
… | |
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 */ |
28 | #include "misc.intpro" /* PROTOS for internal routines */ |
29 | |
29 | |
30 | /* EXTPROTO */ |
30 | /* INTPROTO */ |
31 | char * |
31 | char * |
32 | rxvt_wcstombs (const wchar_t *str, int len) |
32 | rxvt_wcstombs (const wchar_t *str, int len) |
33 | { |
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); |
… | |
… | |
47 | *dst++ = 0; |
47 | *dst++ = 0; |
48 | |
48 | |
49 | return r; |
49 | return r; |
50 | } |
50 | } |
51 | |
51 | |
52 | /* EXTPROTO */ |
52 | /* INTPROTO */ |
53 | char * |
53 | char * |
54 | rxvt_strdup (const char *str) |
54 | rxvt_strdup (const char *str) |
55 | { |
55 | { |
56 | return str ? strdup (str) : 0; |
56 | return str ? strdup (str) : 0; |
57 | } |
57 | } |
58 | |
58 | |
59 | /* EXTPROTO */ |
59 | /* INTPROTO */ |
60 | char * |
60 | char * |
61 | rxvt_r_basename (const char *str) |
61 | rxvt_r_basename (const char *str) |
62 | { |
62 | { |
63 | char *base = STRRCHR (str, '/'); |
63 | char *base = strrchr (str, '/'); |
64 | |
64 | |
65 | return (char *) (base ? base + 1 : str); |
65 | return (char *) (base ? base + 1 : str); |
66 | } |
66 | } |
67 | |
67 | |
68 | /* |
68 | /* |
69 | * Print an error message |
69 | * Print an error message |
70 | */ |
70 | */ |
71 | /* EXTPROTO */ |
71 | /* INTPROTO */ |
72 | void |
72 | void |
73 | rxvt_vlog (const char *fmt, va_list arg_ptr) |
73 | rxvt_vlog (const char *fmt, va_list arg_ptr) |
74 | { |
74 | { |
75 | char msg[1024]; |
75 | char msg[1024]; |
76 | |
76 | |
… | |
… | |
80 | (*GET_R->log_hook) (msg); |
80 | (*GET_R->log_hook) (msg); |
81 | else |
81 | else |
82 | write (STDOUT_FILENO, msg, strlen (msg)); |
82 | write (STDOUT_FILENO, msg, strlen (msg)); |
83 | } |
83 | } |
84 | |
84 | |
85 | /* EXTPROTO */ |
85 | /* INTPROTO */ |
86 | void |
86 | void |
87 | rxvt_log (const char *fmt,...) |
87 | rxvt_log (const char *fmt,...) |
88 | { |
88 | { |
89 | va_list arg_ptr; |
89 | va_list arg_ptr; |
90 | |
90 | |
… | |
… | |
94 | } |
94 | } |
95 | |
95 | |
96 | /* |
96 | /* |
97 | * Print an error message |
97 | * Print an error message |
98 | */ |
98 | */ |
99 | /* EXTPROTO */ |
99 | /* INTPROTO */ |
100 | void |
100 | void |
101 | rxvt_warn (const char *fmt,...) |
101 | rxvt_warn (const char *fmt,...) |
102 | { |
102 | { |
103 | va_list arg_ptr; |
103 | va_list arg_ptr; |
104 | |
104 | |
… | |
… | |
107 | va_start (arg_ptr, fmt); |
107 | va_start (arg_ptr, fmt); |
108 | rxvt_vlog (fmt, arg_ptr); |
108 | rxvt_vlog (fmt, arg_ptr); |
109 | va_end (arg_ptr); |
109 | va_end (arg_ptr); |
110 | } |
110 | } |
111 | |
111 | |
112 | /* EXTPROTO */ |
112 | /* INTPROTO */ |
113 | void |
113 | void |
114 | rxvt_fatal (const char *fmt,...) |
114 | rxvt_fatal (const char *fmt,...) |
115 | { |
115 | { |
116 | va_list arg_ptr; |
116 | va_list arg_ptr; |
117 | |
117 | |
… | |
… | |
124 | rxvt_exit_failure (); |
124 | rxvt_exit_failure (); |
125 | } |
125 | } |
126 | |
126 | |
127 | class rxvt_failure_exception rxvt_failure_exception; |
127 | class rxvt_failure_exception rxvt_failure_exception; |
128 | |
128 | |
129 | /* EXTPROTO */ |
129 | /* INTPROTO */ |
130 | void |
130 | void |
131 | rxvt_exit_failure () |
131 | rxvt_exit_failure () |
132 | { |
132 | { |
133 | throw (rxvt_failure_exception); |
133 | throw (rxvt_failure_exception); |
134 | } |
134 | } |
… | |
… | |
137 | * check that the first characters of S1 match S2 |
137 | * check that the first characters of S1 match S2 |
138 | * |
138 | * |
139 | * No Match |
139 | * No Match |
140 | * return: 0 |
140 | * return: 0 |
141 | * Match |
141 | * Match |
142 | * return: STRLEN (S2) |
142 | * return: strlen (S2) |
143 | */ |
143 | */ |
144 | /* EXTPROTO */ |
144 | /* INTPROTO */ |
145 | int |
145 | int |
146 | rxvt_Str_match (const char *s1, const char *s2) |
146 | rxvt_Str_match (const char *s1, const char *s2) |
147 | { |
147 | { |
148 | int n = STRLEN (s2); |
148 | int n = strlen (s2); |
149 | |
149 | |
150 | return ((STRNCMP (s1, s2, n) == 0) ? n : 0); |
150 | return ((strncmp (s1, s2, n) == 0) ? n : 0); |
151 | } |
151 | } |
152 | |
152 | |
153 | /* EXTPROTO */ |
153 | /* INTPROTO */ |
154 | const char * |
154 | const char * |
155 | rxvt_Str_skip_space (const char *str) |
155 | rxvt_Str_skip_space (const char *str) |
156 | { |
156 | { |
157 | if (str) |
157 | if (str) |
158 | while (*str && isspace (*str)) |
158 | while (*str && isspace (*str)) |
… | |
… | |
163 | |
163 | |
164 | /* |
164 | /* |
165 | * remove leading/trailing space and strip-off leading/trailing quotes. |
165 | * remove leading/trailing space and strip-off leading/trailing quotes. |
166 | * in place. |
166 | * in place. |
167 | */ |
167 | */ |
168 | /* EXTPROTO */ |
168 | /* INTPROTO */ |
169 | char * |
169 | char * |
170 | rxvt_Str_trim (char *str) |
170 | rxvt_Str_trim (char *str) |
171 | { |
171 | { |
172 | char *r, *s; |
172 | char *r, *s; |
173 | int n; |
173 | int n; |
… | |
… | |
212 | * "M-x" prefixed strings, append "\r" if needed |
212 | * "M-x" prefixed strings, append "\r" if needed |
213 | * "\E]" prefixed strings (XTerm escape sequence) append ST if needed |
213 | * "\E]" prefixed strings (XTerm escape sequence) append ST if needed |
214 | * |
214 | * |
215 | * returns the converted string length |
215 | * returns the converted string length |
216 | */ |
216 | */ |
217 | /* EXTPROTO */ |
217 | /* INTPROTO */ |
218 | int |
218 | int |
219 | rxvt_Str_escaped (char *str) |
219 | rxvt_Str_escaped (char *str) |
220 | { |
220 | { |
221 | char ch, *s, *d; |
221 | char ch, *s, *d; |
222 | int i, num, append = 0; |
222 | int i, num, append = 0; |
… | |
… | |
290 | * Split a comma-separated string into an array, stripping leading and |
290 | * Split a comma-separated string into an array, stripping leading and |
291 | * trailing spaces (and paired quotes) from each entry. Empty strings |
291 | * trailing spaces (and paired quotes) from each entry. Empty strings |
292 | * are properly returned |
292 | * are properly returned |
293 | * Caller should free each entry and array when done |
293 | * Caller should free each entry and array when done |
294 | */ |
294 | */ |
295 | /* EXTPROTO */ |
295 | /* INTPROTO */ |
296 | char ** |
296 | char ** |
297 | rxvt_splitcommastring (const char *cs) |
297 | rxvt_splitcommastring (const char *cs) |
298 | { |
298 | { |
299 | int l, n, p; |
299 | int l, n, p; |
300 | const char *s, *t; |
300 | const char *s, *t; |
… | |
… | |
320 | s = ++t; |
320 | s = ++t; |
321 | } |
321 | } |
322 | return ret; |
322 | return ret; |
323 | } |
323 | } |
324 | |
324 | |
|
|
325 | void |
|
|
326 | rxvt_freecommastring (char **cs) |
|
|
327 | { |
|
|
328 | for (int i = 0; cs[i]; ++i) |
|
|
329 | free (cs[i]); |
|
|
330 | |
|
|
331 | free (cs); |
|
|
332 | } |
|
|
333 | |
325 | /*----------------------------------------------------------------------* |
334 | /*----------------------------------------------------------------------* |
326 | * file searching |
335 | * file searching |
327 | */ |
336 | */ |
328 | |
337 | |
329 | /* #define DEBUG_SEARCH_PATH */ |
338 | /* #define DEBUG_SEARCH_PATH */ |
… | |
… | |
342 | int maxpath, len; |
351 | int maxpath, len; |
343 | const char *p, *path; |
352 | const char *p, *path; |
344 | char name[256]; |
353 | char name[256]; |
345 | |
354 | |
346 | if (!access (file, R_OK)) /* found (plain name) in current directory */ |
355 | if (!access (file, R_OK)) /* found (plain name) in current directory */ |
347 | return STRDUP (file); |
356 | return strdup (file); |
348 | |
357 | |
349 | /* semi-colon delimited */ |
358 | /* semi-colon delimited */ |
350 | if ((p = STRCHR (file, ';'))) |
359 | if ((p = strchr (file, ';'))) |
351 | len = (p - file); |
360 | len = (p - file); |
352 | else |
361 | else |
353 | len = STRLEN (file); |
362 | len = strlen (file); |
354 | |
363 | |
355 | #ifdef DEBUG_SEARCH_PATH |
364 | #ifdef DEBUG_SEARCH_PATH |
356 | getcwd (name, sizeof (name)); |
365 | getcwd (name, sizeof (name)); |
357 | fprintf (stderr, "pwd: \"%s\"\n", name); |
366 | fprintf (stderr, "pwd: \"%s\"\n", name); |
358 | fprintf (stderr, "find: \"%.*s\"\n", len, file); |
367 | fprintf (stderr, "find: \"%.*s\"\n", len, file); |
359 | #endif |
368 | #endif |
360 | |
369 | |
361 | /* leave room for an extra '/' and trailing '\0' */ |
370 | /* leave room for an extra '/' and trailing '\0' */ |
362 | maxpath = sizeof (name) - (len + (ext ? STRLEN (ext) : 0) + 2); |
371 | maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2); |
363 | if (maxpath <= 0) |
372 | if (maxpath <= 0) |
364 | return NULL; |
373 | return NULL; |
365 | |
374 | |
366 | /* check if we can find it now */ |
375 | /* check if we can find it now */ |
367 | STRNCPY (name, file, len); |
376 | strncpy (name, file, len); |
368 | name[len] = '\0'; |
377 | name[len] = '\0'; |
369 | |
378 | |
370 | if (!access (name, R_OK)) |
379 | if (!access (name, R_OK)) |
371 | return STRDUP (name); |
380 | return strdup (name); |
372 | if (ext) |
381 | if (ext) |
373 | { |
382 | { |
374 | STRCAT (name, ext); |
383 | strcat (name, ext); |
375 | if (!access (name, R_OK)) |
384 | if (!access (name, R_OK)) |
376 | return STRDUP (name); |
385 | return strdup (name); |
377 | } |
386 | } |
378 | for (path = pathlist; path != NULL && *path != '\0'; path = p) |
387 | for (path = pathlist; path != NULL && *path != '\0'; path = p) |
379 | { |
388 | { |
380 | int n; |
389 | int n; |
381 | |
390 | |
382 | /* colon delimited */ |
391 | /* colon delimited */ |
383 | if ((p = STRCHR (path, ':')) == NULL) |
392 | if ((p = strchr (path, ':')) == NULL) |
384 | p = STRCHR (path, '\0'); |
393 | p = strchr (path, '\0'); |
385 | |
394 | |
386 | n = (p - path); |
395 | n = (p - path); |
387 | if (*p != '\0') |
396 | if (*p != '\0') |
388 | p++; |
397 | p++; |
389 | |
398 | |
390 | if (n > 0 && n <= maxpath) |
399 | if (n > 0 && n <= maxpath) |
391 | { |
400 | { |
392 | STRNCPY (name, path, n); |
401 | strncpy (name, path, n); |
393 | if (name[n - 1] != '/') |
402 | if (name[n - 1] != '/') |
394 | name[n++] = '/'; |
403 | name[n++] = '/'; |
395 | name[n] = '\0'; |
404 | name[n] = '\0'; |
396 | STRNCAT (name, file, len); |
405 | strncat (name, file, len); |
397 | |
406 | |
398 | if (!access (name, R_OK)) |
407 | if (!access (name, R_OK)) |
399 | return STRDUP (name); |
408 | return strdup (name); |
400 | if (ext) |
409 | if (ext) |
401 | { |
410 | { |
402 | STRCAT (name, ext); |
411 | strcat (name, ext); |
403 | if (!access (name, R_OK)) |
412 | if (!access (name, R_OK)) |
404 | return STRDUP (name); |
413 | return strdup (name); |
405 | } |
414 | } |
406 | } |
415 | } |
407 | } |
416 | } |
408 | return NULL; |
417 | return NULL; |
409 | } |
418 | } |
410 | |
419 | |
411 | /* EXTPROTO */ |
420 | /* INTPROTO */ |
412 | char * |
421 | char * |
413 | rxvt_File_find (const char *file, const char *ext, const char *path) |
422 | rxvt_File_find (const char *file, const char *ext, const char *path) |
414 | { |
423 | { |
415 | char *f; |
424 | char *f; |
416 | |
425 | |
… | |
… | |
439 | |
448 | |
440 | /* |
449 | /* |
441 | * Draw top/left and bottom/right border shadows around windows |
450 | * Draw top/left and bottom/right border shadows around windows |
442 | */ |
451 | */ |
443 | #if defined(RXVT_SCROLLBAR) || defined(MENUBAR) |
452 | #if defined(RXVT_SCROLLBAR) || defined(MENUBAR) |
444 | /* EXTPROTO */ |
453 | /* INTPROTO */ |
445 | void |
454 | void |
446 | rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) |
455 | rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) |
447 | { |
456 | { |
448 | int shadow; |
457 | int shadow; |
449 | |
458 | |
… | |
… | |
460 | } |
469 | } |
461 | #endif |
470 | #endif |
462 | |
471 | |
463 | /* button shapes */ |
472 | /* button shapes */ |
464 | #ifdef MENUBAR |
473 | #ifdef MENUBAR |
465 | /* EXTPROTO */ |
474 | /* INTPROTO */ |
466 | void |
475 | void |
467 | rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) |
476 | rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) |
468 | { |
477 | { |
469 | switch (type) |
478 | switch (type) |
470 | { |
479 | { |