1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*----------------------------------------------------------------------* |
2 | * File: misc.C |
2 | * File: misc.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
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-2004 Marc Lehmann <pcg@goof.com> |
9 | * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com> |
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 2 of the License, or |
14 | * (at your option) any later version. |
14 | * (at your option) any later version. |
… | |
… | |
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 | |
41 | if (l < 0) |
42 | if (l < 0) |
|
|
43 | { |
42 | *dst++ = '?'; |
44 | *dst++ = '?'; |
|
|
45 | wcrtomb (0, 0, mbs); // reset undefined state |
|
|
46 | } |
43 | else |
47 | else |
44 | dst += l; |
48 | dst += l; |
45 | } |
49 | } |
46 | |
50 | |
47 | *dst++ = 0; |
51 | *dst++ = 0; |
48 | |
52 | |
|
|
53 | return (char *)rxvt_realloc (r, dst - r); |
|
|
54 | } |
|
|
55 | |
|
|
56 | wchar_t * |
|
|
57 | rxvt_mbstowcs (const char *str, int len) |
|
|
58 | { |
|
|
59 | if (len < 0) len = strlen (str); |
|
|
60 | |
|
|
61 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); |
|
|
62 | |
|
|
63 | if ((ssize_t)mbstowcs (r, str, len + 1) < 0) |
|
|
64 | *r = 0; |
|
|
65 | |
49 | return r; |
66 | return r; |
50 | } |
67 | } |
51 | |
68 | |
52 | /* INTPROTO */ |
|
|
53 | char * |
69 | char * |
54 | rxvt_strdup (const char *str) |
70 | rxvt_wcstoutf8 (const wchar_t *str, int len) |
55 | { |
71 | { |
56 | return str ? strdup (str) : 0; |
72 | if (len < 0) len = wcslen (str); |
57 | } |
|
|
58 | |
73 | |
59 | /* INTPROTO */ |
74 | char *r = (char *)rxvt_malloc (len * 4 + 1); |
|
|
75 | char *p = r; |
|
|
76 | |
|
|
77 | while (len--) |
|
|
78 | { |
|
|
79 | unicode_t w = *str++ & UNICODE_MASK; |
|
|
80 | |
|
|
81 | if (w < 0x000080) |
|
|
82 | *p++ = w; |
|
|
83 | else if (w < 0x000800) |
|
|
84 | *p++ = 0xc0 | ( w >> 6), |
|
|
85 | *p++ = 0x80 | ( w & 0x3f); |
|
|
86 | else if (w < 0x010000) |
|
|
87 | *p++ = 0xe0 | ( w >> 12), |
|
|
88 | *p++ = 0x80 | ((w >> 6) & 0x3f), |
|
|
89 | *p++ = 0x80 | ( w & 0x3f); |
|
|
90 | else if (w < 0x110000) |
|
|
91 | *p++ = 0xf0 | ( w >> 18), |
|
|
92 | *p++ = 0x80 | ((w >> 12) & 0x3f), |
|
|
93 | *p++ = 0x80 | ((w >> 6) & 0x3f), |
|
|
94 | *p++ = 0x80 | ( w & 0x3f); |
|
|
95 | else |
|
|
96 | *p++ = '?'; |
|
|
97 | } |
|
|
98 | |
|
|
99 | *p++ = 0; |
|
|
100 | |
|
|
101 | return (char *)rxvt_realloc (r, p - r); |
|
|
102 | } |
|
|
103 | |
60 | char * |
104 | wchar_t * |
|
|
105 | rxvt_utf8towcs (const char *str, int len) |
|
|
106 | { |
|
|
107 | if (len < 0) len = strlen (str); |
|
|
108 | |
|
|
109 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)), |
|
|
110 | *p = r; |
|
|
111 | |
|
|
112 | unsigned char *s = (unsigned char *)str, |
|
|
113 | *e = s + len; |
|
|
114 | |
|
|
115 | for (;;) |
|
|
116 | { |
|
|
117 | len = e - s; |
|
|
118 | |
|
|
119 | if (len == 0) |
|
|
120 | break; |
|
|
121 | else if (s[0] < 0x80) |
|
|
122 | *p++ = *s++; |
|
|
123 | else if (len >= 2 |
|
|
124 | && s[0] >= 0xc2 && s[0] <= 0xdf |
|
|
125 | && (s[1] & 0xc0) == 0x80) |
|
|
126 | { |
|
|
127 | *p++ = ((s[0] & 0x1f) << 6) |
|
|
128 | | (s[1] & 0x3f); |
|
|
129 | s += 2; |
|
|
130 | } |
|
|
131 | else if (len >= 3 |
|
|
132 | && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) |
|
|
133 | || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
134 | || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) |
|
|
135 | || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
136 | ) |
|
|
137 | && (s[2] & 0xc0) == 0x80) |
|
|
138 | { |
|
|
139 | *p++ = ((s[0] & 0x0f) << 12) |
|
|
140 | | ((s[1] & 0x3f) << 6) |
|
|
141 | | (s[2] & 0x3f); |
|
|
142 | s += 3; |
|
|
143 | } |
|
|
144 | else if (len >= 4 |
|
|
145 | && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) |
|
|
146 | || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
147 | || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) |
|
|
148 | ) |
|
|
149 | && (s[2] & 0xc0) == 0x80 |
|
|
150 | && (s[3] & 0xc0) == 0x80) |
|
|
151 | { |
|
|
152 | *p++ = ((s[0] & 0x07) << 18) |
|
|
153 | | ((s[1] & 0x3f) << 12) |
|
|
154 | | ((s[2] & 0x3f) << 6) |
|
|
155 | | (s[3] & 0x3f); |
|
|
156 | s += 4; |
|
|
157 | } |
|
|
158 | else |
|
|
159 | { |
|
|
160 | *p++ = 0xfffd; |
|
|
161 | s++; |
|
|
162 | } |
|
|
163 | } |
|
|
164 | |
|
|
165 | *p = 0; |
|
|
166 | |
|
|
167 | return r; |
|
|
168 | } |
|
|
169 | |
|
|
170 | const char * |
61 | rxvt_r_basename (const char *str) |
171 | rxvt_basename (const char *str) NOTHROW |
62 | { |
172 | { |
63 | char *base = strrchr (str, '/'); |
173 | const char *base = strrchr (str, '/'); |
64 | |
174 | |
65 | return (char *) (base ? base + 1 : str); |
175 | return base ? base + 1 : str; |
66 | } |
176 | } |
67 | |
177 | |
68 | /* |
178 | /* |
69 | * Print an error message |
179 | * Print an error message |
70 | */ |
180 | */ |
71 | /* INTPROTO */ |
|
|
72 | void |
181 | void |
73 | rxvt_vlog (const char *fmt, va_list arg_ptr) |
182 | rxvt_vlog (const char *fmt, va_list arg_ptr) NOTHROW |
74 | { |
183 | { |
75 | char msg[1024]; |
184 | char msg[1024]; |
76 | |
185 | |
77 | vsnprintf (msg, sizeof msg, fmt, arg_ptr); |
186 | vsnprintf (msg, sizeof msg, fmt, arg_ptr); |
78 | |
187 | |
… | |
… | |
80 | (*GET_R->log_hook) (msg); |
189 | (*GET_R->log_hook) (msg); |
81 | else |
190 | else |
82 | write (STDOUT_FILENO, msg, strlen (msg)); |
191 | write (STDOUT_FILENO, msg, strlen (msg)); |
83 | } |
192 | } |
84 | |
193 | |
85 | /* INTPROTO */ |
|
|
86 | void |
194 | void |
87 | rxvt_log (const char *fmt,...) |
195 | rxvt_log (const char *fmt,...) NOTHROW |
88 | { |
196 | { |
89 | va_list arg_ptr; |
197 | va_list arg_ptr; |
90 | |
198 | |
91 | va_start (arg_ptr, fmt); |
199 | va_start (arg_ptr, fmt); |
92 | rxvt_vlog (fmt, arg_ptr); |
200 | rxvt_vlog (fmt, arg_ptr); |
… | |
… | |
94 | } |
202 | } |
95 | |
203 | |
96 | /* |
204 | /* |
97 | * Print an error message |
205 | * Print an error message |
98 | */ |
206 | */ |
99 | /* INTPROTO */ |
|
|
100 | void |
207 | void |
101 | rxvt_warn (const char *fmt,...) |
208 | rxvt_warn (const char *fmt,...) NOTHROW |
102 | { |
209 | { |
103 | va_list arg_ptr; |
210 | va_list arg_ptr; |
104 | |
211 | |
105 | rxvt_log ("%s: ", RESNAME); |
212 | rxvt_log ("%s: ", RESNAME); |
106 | |
213 | |
107 | va_start (arg_ptr, fmt); |
214 | va_start (arg_ptr, fmt); |
108 | rxvt_vlog (fmt, arg_ptr); |
215 | rxvt_vlog (fmt, arg_ptr); |
109 | va_end (arg_ptr); |
216 | va_end (arg_ptr); |
110 | } |
217 | } |
111 | |
218 | |
112 | /* INTPROTO */ |
|
|
113 | void |
219 | void |
114 | rxvt_fatal (const char *fmt,...) |
220 | rxvt_fatal (const char *fmt,...) THROW ((class rxvt_failure_exception)) |
115 | { |
221 | { |
116 | va_list arg_ptr; |
222 | va_list arg_ptr; |
117 | |
223 | |
118 | rxvt_log ("%s: ", RESNAME); |
224 | rxvt_log ("%s: ", RESNAME); |
119 | |
225 | |
… | |
… | |
122 | va_end (arg_ptr); |
228 | va_end (arg_ptr); |
123 | |
229 | |
124 | rxvt_exit_failure (); |
230 | rxvt_exit_failure (); |
125 | } |
231 | } |
126 | |
232 | |
|
|
233 | void |
|
|
234 | rxvt_exit_failure () THROW ((class rxvt_failure_exception)) |
|
|
235 | { |
127 | class rxvt_failure_exception rxvt_failure_exception; |
236 | static class rxvt_failure_exception rxvt_failure_exception; |
128 | |
|
|
129 | /* INTPROTO */ |
|
|
130 | void |
|
|
131 | rxvt_exit_failure () |
|
|
132 | { |
|
|
133 | throw (rxvt_failure_exception); |
237 | throw (rxvt_failure_exception); |
134 | } |
238 | } |
135 | |
239 | |
136 | /* |
240 | /* |
137 | * check that the first characters of S1 match S2 |
241 | * remove leading/trailing space in place. |
138 | * |
|
|
139 | * No Match |
|
|
140 | * return: 0 |
|
|
141 | * Match |
|
|
142 | * return: strlen (S2) |
|
|
143 | */ |
242 | */ |
144 | /* INTPROTO */ |
243 | char * |
145 | int |
|
|
146 | rxvt_Str_match (const char *s1, const char *s2) |
|
|
147 | { |
|
|
148 | int n = strlen (s2); |
|
|
149 | |
|
|
150 | return ((strncmp (s1, s2, n) == 0) ? n : 0); |
|
|
151 | } |
|
|
152 | |
|
|
153 | /* INTPROTO */ |
|
|
154 | const char * |
|
|
155 | rxvt_Str_skip_space (const char *str) |
|
|
156 | { |
|
|
157 | if (str) |
|
|
158 | while (*str && isspace (*str)) |
|
|
159 | str++; |
|
|
160 | |
|
|
161 | return str; |
|
|
162 | } |
|
|
163 | |
|
|
164 | /* |
|
|
165 | * remove leading/trailing space and strip-off leading/trailing quotes. |
|
|
166 | * in place. |
|
|
167 | */ |
|
|
168 | /* INTPROTO */ |
|
|
169 | char * |
|
|
170 | rxvt_Str_trim (char *str) |
244 | rxvt_strtrim (char *str) NOTHROW |
171 | { |
245 | { |
172 | char *r, *s; |
246 | char *r, *s; |
173 | int n; |
|
|
174 | |
247 | |
175 | if (!str || !*str) /* shortcut */ |
248 | if (!str || !*str) /* shortcut */ |
176 | return str; |
249 | return str; |
177 | |
250 | |
178 | /* skip leading spaces */ |
251 | /* skip leading spaces */ |
179 | for (s = str; *s && isspace (*s); s++) ; |
252 | for (s = str; *s && isspace (*s); s++) ; |
|
|
253 | |
180 | /* goto end of string */ |
254 | /* goto end of string */ |
181 | for (n = 0, r = s; *r++; n++) ; |
255 | r = s + strlen (s) - 1; |
182 | r -= 2; |
256 | |
183 | /* dump return */ |
257 | /* dump return and other trailing whitespace */ |
184 | if (n > 0 && *r == '\n') |
258 | while (r > s && isspace (*r)) |
185 | n--, r--; |
259 | r--; |
186 | /* backtrack along trailing spaces */ |
|
|
187 | for (; n > 0 && isspace (*r); r--, n--) ; |
|
|
188 | /* skip matching leading/trailing quotes */ |
|
|
189 | if (*s == '"' && *r == '"' && n > 1) |
|
|
190 | { |
|
|
191 | s++; |
|
|
192 | n -= 2; |
|
|
193 | } |
|
|
194 | |
260 | |
195 | /* copy back over: forwards copy */ |
261 | memmove (str, s, r + 1 - s); |
196 | for (r = str; n; n--) |
262 | str[r + 1 - s] = 0; |
197 | *r++ = *s++; |
|
|
198 | *r = '\0'; |
|
|
199 | |
263 | |
200 | return str; |
264 | return str; |
201 | } |
|
|
202 | |
|
|
203 | /* |
|
|
204 | * in-place interpretation of string: |
|
|
205 | * |
|
|
206 | * backslash-escaped: "\a\b\E\e\n\r\t", "\octal" |
|
|
207 | * Ctrl chars: ^@ .. ^_, ^? |
|
|
208 | * |
|
|
209 | * Emacs-style: "M-" prefix |
|
|
210 | * |
|
|
211 | * Also, |
|
|
212 | * "M-x" prefixed strings, append "\r" if needed |
|
|
213 | * "\E]" prefixed strings (XTerm escape sequence) append ST if needed |
|
|
214 | * |
|
|
215 | * returns the converted string length |
|
|
216 | */ |
|
|
217 | /* INTPROTO */ |
|
|
218 | int |
|
|
219 | rxvt_Str_escaped (char *str) |
|
|
220 | { |
|
|
221 | char ch, *s, *d; |
|
|
222 | int i, num, append = 0; |
|
|
223 | |
|
|
224 | if (!str || !*str) |
|
|
225 | return 0; |
|
|
226 | |
|
|
227 | d = s = str; |
|
|
228 | |
|
|
229 | if (*s == 'M' && s[1] == '-') |
|
|
230 | { |
|
|
231 | /* Emacs convenience, replace leading `M-..' with `\E..' */ |
|
|
232 | *d++ = C0_ESC; |
|
|
233 | s += 2; |
|
|
234 | if (toupper (*s) == 'X') |
|
|
235 | /* append carriage-return for `M-xcommand' */ |
|
|
236 | for (*d++ = 'x', append = '\r', s++; isspace (*s); s++) ; |
|
|
237 | } |
|
|
238 | for (; (ch = *s++);) |
|
|
239 | { |
|
|
240 | if (ch == '\\') |
|
|
241 | { |
|
|
242 | ch = *s++; |
|
|
243 | if (ch >= '0' && ch <= '7') |
|
|
244 | { /* octal */ |
|
|
245 | num = ch - '0'; |
|
|
246 | for (i = 0; i < 2; i++, s++) |
|
|
247 | { |
|
|
248 | ch = *s; |
|
|
249 | if (ch < '0' || ch > '7') |
|
|
250 | break; |
|
|
251 | num = num * 8 + ch - '0'; |
|
|
252 | } |
|
|
253 | ch = (char)num; |
|
|
254 | } |
|
|
255 | else if (ch == 'a') |
|
|
256 | ch = C0_BEL; /* bell */ |
|
|
257 | else if (ch == 'b') |
|
|
258 | ch = C0_BS; /* backspace */ |
|
|
259 | else if (ch == 'E' || ch == 'e') |
|
|
260 | ch = C0_ESC; /* escape */ |
|
|
261 | else if (ch == 'n') |
|
|
262 | ch = '\n'; /* newline */ |
|
|
263 | else if (ch == 'r') |
|
|
264 | ch = '\r'; /* carriage-return */ |
|
|
265 | else if (ch == 't') |
|
|
266 | ch = C0_HT; /* tab */ |
|
|
267 | } |
|
|
268 | else if (ch == '^') |
|
|
269 | { |
|
|
270 | ch = *s++; |
|
|
271 | ch = toupper (ch); |
|
|
272 | ch = (ch == '?' ? 127 : (ch - '@')); |
|
|
273 | } |
|
|
274 | *d++ = ch; |
|
|
275 | } |
|
|
276 | |
|
|
277 | /* ESC] is an XTerm escape sequence, must be terminated */ |
|
|
278 | if (*str == '\0' && str[1] == C0_ESC && str[2] == ']') |
|
|
279 | append = CHAR_ST; |
|
|
280 | |
|
|
281 | /* add trailing character as required */ |
|
|
282 | if (append && d[-1] != append) |
|
|
283 | *d++ = append; |
|
|
284 | *d = '\0'; |
|
|
285 | |
|
|
286 | return (d - str); |
|
|
287 | } |
265 | } |
288 | |
266 | |
289 | /* |
267 | /* |
290 | * Split a comma-separated string into an array, stripping leading and |
268 | * Split a comma-separated string into an array, stripping leading and |
291 | * trailing spaces (and paired quotes) from each entry. Empty strings |
269 | * trailing spaces from each entry. Empty strings are properly returned |
292 | * are properly returned |
|
|
293 | * Caller should free each entry and array when done |
|
|
294 | */ |
270 | */ |
295 | /* INTPROTO */ |
271 | char ** |
296 | char ** |
|
|
297 | rxvt_splitcommastring (const char *cs) |
272 | rxvt_splitcommastring (const char *cs) NOTHROW |
298 | { |
273 | { |
299 | int l, n, p; |
274 | int l, n, p; |
300 | const char *s, *t; |
275 | const char *s, *t; |
301 | char **ret; |
276 | char **ret; |
302 | |
277 | |
… | |
… | |
304 | s = ""; |
279 | s = ""; |
305 | |
280 | |
306 | for (n = 1, t = s; *t; t++) |
281 | for (n = 1, t = s; *t; t++) |
307 | if (*t == ',') |
282 | if (*t == ',') |
308 | n++; |
283 | n++; |
|
|
284 | |
309 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
285 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
310 | ret[n] = NULL; |
286 | ret[n] = NULL; |
311 | |
287 | |
312 | for (l = 0, t = s; l < n; l++) |
288 | for (l = 0, t = s; l < n; l++) |
313 | { |
289 | { |
314 | for ( ; *t && *t != ','; t++) ; |
290 | for ( ; *t && *t != ','; t++) ; |
315 | p = t - s; |
291 | p = t - s; |
316 | ret[l] = (char *)malloc (p + 1); |
292 | ret[l] = (char *)malloc (p + 1); |
317 | strncpy (ret[l], s, p); |
293 | memcpy (ret[l], s, p); |
318 | ret[l][p] = '\0'; |
294 | ret[l][p] = '\0'; |
319 | rxvt_Str_trim (ret[l]); |
295 | rxvt_strtrim (ret[l]); |
320 | s = ++t; |
296 | s = ++t; |
321 | } |
297 | } |
|
|
298 | |
322 | return ret; |
299 | return ret; |
323 | } |
300 | } |
324 | |
301 | |
325 | /*----------------------------------------------------------------------* |
302 | void |
326 | * file searching |
303 | rxvt_freecommastring (char **cs) NOTHROW |
327 | */ |
|
|
328 | |
|
|
329 | /* #define DEBUG_SEARCH_PATH */ |
|
|
330 | |
|
|
331 | #if defined (XPM_BACKGROUND) || (MENUBAR_MAX) |
|
|
332 | /* |
|
|
333 | * search for FILE in the current working directory, and within the |
|
|
334 | * colon-delimited PATHLIST, adding the file extension EXT if required. |
|
|
335 | * |
|
|
336 | * FILE is either semi-colon or zero terminated |
|
|
337 | */ |
|
|
338 | /* INTPROTO */ |
|
|
339 | char * |
|
|
340 | rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) |
|
|
341 | { |
304 | { |
342 | int maxpath, len; |
305 | for (int i = 0; cs[i]; ++i) |
343 | const char *p, *path; |
306 | free (cs[i]); |
344 | char name[256]; |
|
|
345 | |
307 | |
346 | if (!access (file, R_OK)) /* found (plain name) in current directory */ |
308 | free (cs); |
347 | return strdup (file); |
|
|
348 | |
|
|
349 | /* semi-colon delimited */ |
|
|
350 | if ((p = strchr (file, ';'))) |
|
|
351 | len = (p - file); |
|
|
352 | else |
|
|
353 | len = strlen (file); |
|
|
354 | |
|
|
355 | #ifdef DEBUG_SEARCH_PATH |
|
|
356 | getcwd (name, sizeof (name)); |
|
|
357 | fprintf (stderr, "pwd: \"%s\"\n", name); |
|
|
358 | fprintf (stderr, "find: \"%.*s\"\n", len, file); |
|
|
359 | #endif |
|
|
360 | |
|
|
361 | /* leave room for an extra '/' and trailing '\0' */ |
|
|
362 | maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2); |
|
|
363 | if (maxpath <= 0) |
|
|
364 | return NULL; |
|
|
365 | |
|
|
366 | /* check if we can find it now */ |
|
|
367 | strncpy (name, file, len); |
|
|
368 | name[len] = '\0'; |
|
|
369 | |
|
|
370 | if (!access (name, R_OK)) |
|
|
371 | return strdup (name); |
|
|
372 | if (ext) |
|
|
373 | { |
|
|
374 | strcat (name, ext); |
|
|
375 | if (!access (name, R_OK)) |
|
|
376 | return strdup (name); |
|
|
377 | } |
|
|
378 | for (path = pathlist; path != NULL && *path != '\0'; path = p) |
|
|
379 | { |
|
|
380 | int n; |
|
|
381 | |
|
|
382 | /* colon delimited */ |
|
|
383 | if ((p = strchr (path, ':')) == NULL) |
|
|
384 | p = strchr (path, '\0'); |
|
|
385 | |
|
|
386 | n = (p - path); |
|
|
387 | if (*p != '\0') |
|
|
388 | p++; |
|
|
389 | |
|
|
390 | if (n > 0 && n <= maxpath) |
|
|
391 | { |
|
|
392 | strncpy (name, path, n); |
|
|
393 | if (name[n - 1] != '/') |
|
|
394 | name[n++] = '/'; |
|
|
395 | name[n] = '\0'; |
|
|
396 | strncat (name, file, len); |
|
|
397 | |
|
|
398 | if (!access (name, R_OK)) |
|
|
399 | return strdup (name); |
|
|
400 | if (ext) |
|
|
401 | { |
|
|
402 | strcat (name, ext); |
|
|
403 | if (!access (name, R_OK)) |
|
|
404 | return strdup (name); |
|
|
405 | } |
|
|
406 | } |
|
|
407 | } |
|
|
408 | return NULL; |
|
|
409 | } |
309 | } |
410 | |
310 | |
411 | /* INTPROTO */ |
311 | void * |
412 | char * |
312 | rxvt_malloc (size_t size) |
413 | rxvt_File_find (const char *file, const char *ext, const char *path) |
|
|
414 | { |
313 | { |
415 | char *f; |
314 | void *p = malloc (size); |
416 | |
315 | |
417 | if (file == NULL || *file == '\0') |
|
|
418 | return NULL; |
|
|
419 | |
|
|
420 | /* search environment variables here too */ |
|
|
421 | if ((f = rxvt_File_search_path (path, file, ext)) == NULL) |
|
|
422 | #ifdef PATH_ENV |
|
|
423 | if ((f = rxvt_File_search_path (getenv (PATH_ENV), file, ext)) == NULL) |
|
|
424 | #endif |
|
|
425 | f = rxvt_File_search_path (getenv ("PATH"), file, ext); |
|
|
426 | |
|
|
427 | #ifdef DEBUG_SEARCH_PATH |
|
|
428 | if (f) |
316 | if (!p) |
429 | fprintf (stderr, "found: \"%s\"\n", f); |
317 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
430 | #endif |
|
|
431 | |
318 | |
432 | return f; |
319 | return p; |
433 | } |
320 | } |
434 | #endif /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */ |
|
|
435 | |
321 | |
436 | /*----------------------------------------------------------------------* |
|
|
437 | * miscellaneous drawing routines |
|
|
438 | */ |
|
|
439 | |
|
|
440 | /* |
|
|
441 | * Draw top/left and bottom/right border shadows around windows |
|
|
442 | */ |
|
|
443 | #if defined(RXVT_SCROLLBAR) || defined(MENUBAR) |
|
|
444 | /* INTPROTO */ |
|
|
445 | void |
322 | void * |
446 | rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) |
323 | rxvt_calloc (size_t number, size_t size) |
447 | { |
324 | { |
448 | int shadow; |
325 | void *p = calloc (number, size); |
449 | |
326 | |
450 | shadow = (w == 0 || h == 0) ? 1 : SHADOW; |
327 | if (!p) |
451 | w += x - 1; |
328 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
452 | h += y - 1; |
|
|
453 | for (; shadow-- > 0; x++, y++, w--, h--) |
|
|
454 | { |
|
|
455 | XDrawLine (display, win, topShadow, x, y, w, y); |
|
|
456 | XDrawLine (display, win, topShadow, x, y, x, h); |
|
|
457 | XDrawLine (display, win, botShadow, w, h, w, y + 1); |
|
|
458 | XDrawLine (display, win, botShadow, w, h, x + 1, h); |
|
|
459 | } |
|
|
460 | } |
|
|
461 | #endif |
|
|
462 | |
329 | |
463 | /* button shapes */ |
330 | return p; |
464 | #ifdef MENUBAR |
331 | } |
465 | /* INTPROTO */ |
332 | |
466 | void |
333 | void * |
467 | rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) |
334 | rxvt_realloc (void *ptr, size_t size) |
468 | { |
335 | { |
469 | switch (type) |
336 | void *p = realloc (ptr, size); |
470 | { |
|
|
471 | case 'r': /* right triangle */ |
|
|
472 | XDrawLine (display, win, topShadow, x, y, x, y + w); |
|
|
473 | XDrawLine (display, win, topShadow, x, y, x + w, y + w / 2); |
|
|
474 | XDrawLine (display, win, botShadow, x, y + w, x + w, y + w / 2); |
|
|
475 | break; |
|
|
476 | |
337 | |
477 | case 'l': /* left triangle */ |
338 | if (!p) |
478 | XDrawLine (display, win, botShadow, x + w, y + w, x + w, y); |
339 | rxvt_fatal ("memory allocation failure. aborting.\n"); |
479 | XDrawLine (display, win, botShadow, x + w, y + w, x, y + w / 2); |
|
|
480 | XDrawLine (display, win, topShadow, x, y + w / 2, x + w, y); |
|
|
481 | break; |
|
|
482 | |
340 | |
483 | case 'd': /* down triangle */ |
341 | return p; |
484 | XDrawLine (display, win, topShadow, x, y, x + w / 2, y + w); |
|
|
485 | XDrawLine (display, win, topShadow, x, y, x + w, y); |
|
|
486 | XDrawLine (display, win, botShadow, x + w, y, x + w / 2, y + w); |
|
|
487 | break; |
|
|
488 | |
|
|
489 | case 'u': /* up triangle */ |
|
|
490 | XDrawLine (display, win, botShadow, x + w, y + w, x + w / 2, y); |
|
|
491 | XDrawLine (display, win, botShadow, x + w, y + w, x, y + w); |
|
|
492 | XDrawLine (display, win, topShadow, x, y + w, x + w / 2, y); |
|
|
493 | break; |
|
|
494 | #if 0 |
|
|
495 | case 's': /* square */ |
|
|
496 | XDrawLine (display, win, topShadow, x + w, y, x, y); |
|
|
497 | XDrawLine (display, win, topShadow, x, y, x, y + w); |
|
|
498 | XDrawLine (display, win, botShadow, x, y + w, x + w, y + w); |
|
|
499 | XDrawLine (display, win, botShadow, x + w, y + w, x + w, y); |
|
|
500 | break; |
|
|
501 | #endif |
|
|
502 | |
|
|
503 | } |
|
|
504 | } |
342 | } |
505 | #endif |
|
|
506 | /*----------------------- end-of-file (C source) -----------------------*/ |
|
|