ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/misc.C
(Generate patch)

Comparing rxvt-unicode/src/misc.C (file contents):
Revision 1.2 by pcg, Mon Nov 24 17:31:27 2003 UTC vs.
Revision 1.6 by pcg, Mon Feb 9 07:11:49 2004 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: misc.c 2 * File: misc.c
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * $Id: misc.C,v 1.2 2003/11/24 17:31:27 pcg Exp $
5 * 4 *
6 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
7 * 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
8 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> 7 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
9 * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com> 8 * Copyright (c) 1998-2000 Geoff Wing <gcw@pobox.com>
26#include "../config.h" /* NECESSARY */ 25#include "../config.h" /* NECESSARY */
27#include "rxvt.h" /* NECESSARY */ 26#include "rxvt.h" /* NECESSARY */
28#include "misc.intpro" /* PROTOS for internal routines */ 27#include "misc.intpro" /* PROTOS for internal routines */
29 28
30/* EXTPROTO */ 29/* EXTPROTO */
30char *
31rxvt_strdup (const char *str)
32{
33 return str ? strdup (str) : 0;
34}
35
36/* EXTPROTO */
31char * 37char *
32rxvt_r_basename(const char *str) 38rxvt_r_basename(const char *str)
33{ 39{
34 char *base = STRRCHR(str, '/'); 40 char *base = STRRCHR(str, '/');
35 41
36 return (char *)(base ? base + 1 : str); 42 return (char *)(base ? base + 1 : str);
37} 43}
38 44
39/* 45/*
40 * Print an error message 46 * Print an error message
41 */ 47 */
42/* EXTPROTO */ 48/* EXTPROTO */
43void 49void
44rxvt_print_error(const char *fmt,...) 50rxvt_print_error(const char *fmt,...)
45{ 51{
46 va_list arg_ptr; 52 va_list arg_ptr;
47 53
48 va_start(arg_ptr, fmt); 54 va_start(arg_ptr, fmt);
49 fprintf(stderr, APL_NAME ": "); 55 fprintf(stderr, APL_NAME ": ");
50 vfprintf(stderr, fmt, arg_ptr); 56 vfprintf(stderr, fmt, arg_ptr);
51 fprintf(stderr, "\n"); 57 fprintf(stderr, "\n");
52 va_end(arg_ptr); 58 va_end(arg_ptr);
53} 59}
54 60
55/* 61/*
56 * check that the first characters of S1 match S2 62 * check that the first characters of S1 match S2
57 * 63 *
62 */ 68 */
63/* EXTPROTO */ 69/* EXTPROTO */
64int 70int
65rxvt_Str_match(const char *s1, const char *s2) 71rxvt_Str_match(const char *s1, const char *s2)
66{ 72{
67 int n = STRLEN(s2); 73 int n = STRLEN(s2);
68 74
69 return ((STRNCMP(s1, s2, n) == 0) ? n : 0); 75 return ((STRNCMP(s1, s2, n) == 0) ? n : 0);
70} 76}
71 77
72/* EXTPROTO */ 78/* EXTPROTO */
73const char * 79const char *
74rxvt_Str_skip_space(const char *str) 80rxvt_Str_skip_space(const char *str)
75{ 81{
76 if (str) 82 if (str)
77 while (*str && isspace(*str)) 83 while (*str && isspace(*str))
78 str++; 84 str++;
79 return str; 85 return str;
80} 86}
81 87
82/* 88/*
83 * remove leading/trailing space and strip-off leading/trailing quotes. 89 * remove leading/trailing space and strip-off leading/trailing quotes.
84 * in place. 90 * in place.
85 */ 91 */
86/* EXTPROTO */ 92/* EXTPROTO */
87char * 93char *
88rxvt_Str_trim(char *str) 94rxvt_Str_trim(char *str)
89{ 95{
90 char *r, *s; 96 char *r, *s;
91 int n; 97 int n;
92 98
93 if (!str || !*str) /* shortcut */ 99 if (!str || !*str) /* shortcut */
94 return str;
95
96/* skip leading spaces */
97 for (s = str; *s && isspace(*s); s++) ;
98/* goto end of string */
99 for (n = 0, r = s; *r++; n++) ;
100 r -= 2;
101/* dump return */
102 if (n > 0 && *r == '\n')
103 n--, r--;
104/* backtrack along trailing spaces */
105 for (; n > 0 && isspace(*r); r--, n--) ;
106/* skip matching leading/trailing quotes */
107 if (*s == '"' && *r == '"' && n > 1) {
108 s++;
109 n -= 2;
110 }
111/* copy back over: forwards copy */
112 for (r = str; n; n--)
113 *r++ = *s++;
114 *r = '\0';
115
116 return str; 100 return str;
101
102 /* skip leading spaces */
103 for (s = str; *s && isspace(*s); s++) ;
104 /* goto end of string */
105 for (n = 0, r = s; *r++; n++) ;
106 r -= 2;
107 /* dump return */
108 if (n > 0 && *r == '\n')
109 n--, r--;
110 /* backtrack along trailing spaces */
111 for (; n > 0 && isspace(*r); r--, n--) ;
112 /* skip matching leading/trailing quotes */
113 if (*s == '"' && *r == '"' && n > 1)
114 {
115 s++;
116 n -= 2;
117 }
118 /* copy back over: forwards copy */
119 for (r = str; n; n--)
120 *r++ = *s++;
121 *r = '\0';
122
123 return str;
117} 124}
118 125
119/* 126/*
120 * in-place interpretation of string: 127 * in-place interpretation of string:
121 * 128 *
132 */ 139 */
133/* EXTPROTO */ 140/* EXTPROTO */
134int 141int
135rxvt_Str_escaped(char *str) 142rxvt_Str_escaped(char *str)
136{ 143{
137 char ch, *s, *d; 144 char ch, *s, *d;
138 int i, num, append = 0; 145 int i, num, append = 0;
139 146
140 if (!str || !*str) 147 if (!str || !*str)
141 return 0; 148 return 0;
142 149
143 d = s = str; 150 d = s = str;
144 151
145 if (*s == 'M' && s[1] == '-') { 152 if (*s == 'M' && s[1] == '-')
153 {
146 /* Emacs convenience, replace leading `M-..' with `\E..' */ 154 /* Emacs convenience, replace leading `M-..' with `\E..' */
147 *d++ = C0_ESC; 155 *d++ = C0_ESC;
148 s += 2; 156 s += 2;
149 if (toupper(*s) == 'X') 157 if (toupper(*s) == 'X')
150 /* append carriage-return for `M-xcommand' */ 158 /* append carriage-return for `M-xcommand' */
151 for (*d++ = 'x', append = '\r', s++; isspace(*s); s++) ; 159 for (*d++ = 'x', append = '\r', s++; isspace(*s); s++) ;
152 } 160 }
153 for (; (ch = *s++);) { 161 for (; (ch = *s++);)
162 {
154 if (ch == '\\') { 163 if (ch == '\\')
164 {
155 ch = *s++; 165 ch = *s++;
156 if (ch >= '0' && ch <= '7') { /* octal */ 166 if (ch >= '0' && ch <= '7')
157 num = ch - '0'; 167 { /* octal */
168 num = ch - '0';
158 for (i = 0; i < 2; i++, s++) { 169 for (i = 0; i < 2; i++, s++)
159 ch = *s; 170 {
171 ch = *s;
160 if (ch < '0' || ch > '7') 172 if (ch < '0' || ch > '7')
161 break; 173 break;
162 num = num * 8 + ch - '0'; 174 num = num * 8 + ch - '0';
163 } 175 }
164 ch = (char)num; 176 ch = (char)num;
165 } else if (ch == 'a') 177 }
178 else if (ch == 'a')
166 ch = C0_BEL; /* bell */ 179 ch = C0_BEL; /* bell */
167 else if (ch == 'b') 180 else if (ch == 'b')
168 ch = C0_BS; /* backspace */ 181 ch = C0_BS; /* backspace */
169 else if (ch == 'E' || ch == 'e') 182 else if (ch == 'E' || ch == 'e')
170 ch = C0_ESC; /* escape */ 183 ch = C0_ESC; /* escape */
184 else if (ch == 'n')
185 ch = '\n'; /* newline */
186 else if (ch == 'r')
187 ch = '\r'; /* carriage-return */
188 else if (ch == 't')
189 ch = C0_HT; /* tab */
190 }
171 else if (ch == 'n') 191 else if (ch == '^')
172 ch = '\n'; /* newline */ 192 {
173 else if (ch == 'r')
174 ch = '\r'; /* carriage-return */
175 else if (ch == 't')
176 ch = C0_HT; /* tab */
177 } else if (ch == '^') {
178 ch = *s++; 193 ch = *s++;
179 ch = toupper(ch); 194 ch = toupper(ch);
180 ch = (ch == '?' ? 127 : (ch - '@')); 195 ch = (ch == '?' ? 127 : (ch - '@'));
181 } 196 }
182 *d++ = ch; 197 *d++ = ch;
183 } 198 }
184 199
185/* ESC] is an XTerm escape sequence, must be terminated */ 200 /* ESC] is an XTerm escape sequence, must be terminated */
186 if (*str == '\0' && str[1] == C0_ESC && str[2] == ']') 201 if (*str == '\0' && str[1] == C0_ESC && str[2] == ']')
187 append = CHAR_ST; 202 append = CHAR_ST;
188 203
189/* add trailing character as required */ 204 /* add trailing character as required */
190 if (append && d[-1] != append) 205 if (append && d[-1] != append)
191 *d++ = append; 206 *d++ = append;
192 *d = '\0'; 207 *d = '\0';
193 208
194 return (d - str); 209 return (d - str);
195} 210}
196 211
197/* 212/*
198 * Split a comma-separated string into an array, stripping leading and 213 * Split a comma-separated string into an array, stripping leading and
199 * trailing spaces (and paired quotes) from each entry. Empty strings 214 * trailing spaces (and paired quotes) from each entry. Empty strings
202 */ 217 */
203/* EXTPROTO */ 218/* EXTPROTO */
204char ** 219char **
205rxvt_splitcommastring(const char *cs) 220rxvt_splitcommastring(const char *cs)
206{ 221{
207 int l, n, p; 222 int l, n, p;
208 const char *s, *t; 223 const char *s, *t;
209 char **ret; 224 char **ret;
210 225
211 if ((s = cs) == NULL) 226 if ((s = cs) == NULL)
212 s = ""; 227 s = "";
213 228
214 for (n = 1, t = s; *t; t++) 229 for (n = 1, t = s; *t; t++)
215 if (*t == ',') 230 if (*t == ',')
216 n++; 231 n++;
217 ret = (char **)malloc((n + 1) * sizeof(char *)); 232 ret = (char **)malloc((n + 1) * sizeof(char *));
218 ret[n] = NULL; 233 ret[n] = NULL;
219 234
220 for (l = 0, t = s; l < n; l++) { 235 for (l = 0, t = s; l < n; l++)
236 {
221 for ( ; *t && *t != ','; t++) ; 237 for ( ; *t && *t != ','; t++) ;
222 p = t - s; 238 p = t - s;
223 ret[l] = (char *)malloc(p + 1); 239 ret[l] = (char *)malloc(p + 1);
224 strncpy(ret[l], s, p); 240 strncpy(ret[l], s, p);
225 ret[l][p] = '\0'; 241 ret[l][p] = '\0';
226 rxvt_Str_trim(ret[l]); 242 rxvt_Str_trim(ret[l]);
227 s = ++t; 243 s = ++t;
228 } 244 }
229 return ret; 245 return ret;
230} 246}
231 247
232/*----------------------------------------------------------------------* 248/*----------------------------------------------------------------------*
233 * file searching 249 * file searching
234 */ 250 */
244 */ 260 */
245/* INTPROTO */ 261/* INTPROTO */
246char * 262char *
247rxvt_File_search_path(const char *pathlist, const char *file, const char *ext) 263rxvt_File_search_path(const char *pathlist, const char *file, const char *ext)
248{ 264{
249 int maxpath, len; 265 int maxpath, len;
250 const char *p, *path; 266 const char *p, *path;
251 char name[256]; 267 char name[256];
252 268
253 if (!access(file, R_OK)) /* found (plain name) in current directory */ 269 if (!access(file, R_OK)) /* found (plain name) in current directory */
254 return STRDUP(file); 270 return STRDUP(file);
255 271
256/* semi-colon delimited */ 272 /* semi-colon delimited */
257 if ((p = STRCHR(file, ';'))) 273 if ((p = STRCHR(file, ';')))
258 len = (p - file); 274 len = (p - file);
259 else 275 else
260 len = STRLEN(file); 276 len = STRLEN(file);
261 277
262#ifdef DEBUG_SEARCH_PATH 278#ifdef DEBUG_SEARCH_PATH
263 getcwd(name, sizeof(name)); 279 getcwd(name, sizeof(name));
264 fprintf(stderr, "pwd: \"%s\"\n", name); 280 fprintf(stderr, "pwd: \"%s\"\n", name);
265 fprintf(stderr, "find: \"%.*s\"\n", len, file); 281 fprintf(stderr, "find: \"%.*s\"\n", len, file);
266#endif 282#endif
267 283
268/* leave room for an extra '/' and trailing '\0' */ 284 /* leave room for an extra '/' and trailing '\0' */
269 maxpath = sizeof(name) - (len + (ext ? STRLEN(ext) : 0) + 2); 285 maxpath = sizeof(name) - (len + (ext ? STRLEN(ext) : 0) + 2);
270 if (maxpath <= 0) 286 if (maxpath <= 0)
271 return NULL;
272
273/* check if we can find it now */
274 STRNCPY(name, file, len);
275 name[len] = '\0';
276
277 if (!access(name, R_OK))
278 return STRDUP(name);
279 if (ext) {
280 STRCAT(name, ext);
281 if (!access(name, R_OK))
282 return STRDUP(name);
283 }
284 for (path = pathlist; path != NULL && *path != '\0'; path = p) {
285 int n;
286
287 /* colon delimited */
288 if ((p = STRCHR(path, ':')) == NULL)
289 p = STRCHR(path, '\0');
290
291 n = (p - path);
292 if (*p != '\0')
293 p++;
294
295 if (n > 0 && n <= maxpath) {
296 STRNCPY(name, path, n);
297 if (name[n - 1] != '/')
298 name[n++] = '/';
299 name[n] = '\0';
300 STRNCAT(name, file, len);
301
302 if (!access(name, R_OK))
303 return STRDUP(name);
304 if (ext) {
305 STRCAT(name, ext);
306 if (!access(name, R_OK))
307 return STRDUP(name);
308 }
309 }
310 }
311 return NULL; 287 return NULL;
288
289 /* check if we can find it now */
290 STRNCPY(name, file, len);
291 name[len] = '\0';
292
293 if (!access(name, R_OK))
294 return STRDUP(name);
295 if (ext)
296 {
297 STRCAT(name, ext);
298 if (!access(name, R_OK))
299 return STRDUP(name);
300 }
301 for (path = pathlist; path != NULL && *path != '\0'; path = p)
302 {
303 int n;
304
305 /* colon delimited */
306 if ((p = STRCHR(path, ':')) == NULL)
307 p = STRCHR(path, '\0');
308
309 n = (p - path);
310 if (*p != '\0')
311 p++;
312
313 if (n > 0 && n <= maxpath)
314 {
315 STRNCPY(name, path, n);
316 if (name[n - 1] != '/')
317 name[n++] = '/';
318 name[n] = '\0';
319 STRNCAT(name, file, len);
320
321 if (!access(name, R_OK))
322 return STRDUP(name);
323 if (ext)
324 {
325 STRCAT(name, ext);
326 if (!access(name, R_OK))
327 return STRDUP(name);
328 }
329 }
330 }
331 return NULL;
312} 332}
313 333
314/* EXTPROTO */ 334/* EXTPROTO */
315char * 335char *
316rxvt_File_find(const char *file, const char *ext, const char *path) 336rxvt_File_find(const char *file, const char *ext, const char *path)
317{ 337{
318 char *f; 338 char *f;
319 339
320 if (file == NULL || *file == '\0') 340 if (file == NULL || *file == '\0')
321 return NULL; 341 return NULL;
322 342
323/* search environment variables here too */ 343 /* search environment variables here too */
324 if ((f = rxvt_File_search_path(path, file, ext)) == NULL) 344 if ((f = rxvt_File_search_path(path, file, ext)) == NULL)
325#ifdef PATH_ENV 345#ifdef PATH_ENV
326 if ((f = rxvt_File_search_path(getenv(PATH_ENV), file, ext)) == NULL) 346 if ((f = rxvt_File_search_path(getenv(PATH_ENV), file, ext)) == NULL)
327#endif 347#endif
328 f = rxvt_File_search_path(getenv("PATH"), file, ext); 348 f = rxvt_File_search_path(getenv("PATH"), file, ext);
329 349
330#ifdef DEBUG_SEARCH_PATH 350#ifdef DEBUG_SEARCH_PATH
331 if (f) 351 if (f)
332 fprintf(stderr, "found: \"%s\"\n", f); 352 fprintf(stderr, "found: \"%s\"\n", f);
333#endif 353#endif
334 354
335 return f; 355 return f;
336} 356}
337#endif /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */ 357#endif /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */
338 358
339/*----------------------------------------------------------------------* 359/*----------------------------------------------------------------------*
340 * miscellaneous drawing routines 360 * miscellaneous drawing routines
344 * Draw top/left and bottom/right border shadows around windows 364 * Draw top/left and bottom/right border shadows around windows
345 */ 365 */
346#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 366#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
347/* EXTPROTO */ 367/* EXTPROTO */
348void 368void
349rxvt_Draw_Shadow(Display *Xdisplay, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) 369rxvt_Draw_Shadow(Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h)
350{ 370{
351 int shadow; 371 int shadow;
352 372
353 shadow = (w == 0 || h == 0) ? 1 : SHADOW; 373 shadow = (w == 0 || h == 0) ? 1 : SHADOW;
354 w += x - 1; 374 w += x - 1;
355 h += y - 1; 375 h += y - 1;
356 for (; shadow-- > 0; x++, y++, w--, h--) { 376 for (; shadow-- > 0; x++, y++, w--, h--)
377 {
357 XDrawLine(Xdisplay, win, topShadow, x, y, w, y); 378 XDrawLine(display, win, topShadow, x, y, w, y);
358 XDrawLine(Xdisplay, win, topShadow, x, y, x, h); 379 XDrawLine(display, win, topShadow, x, y, x, h);
359 XDrawLine(Xdisplay, win, botShadow, w, h, w, y + 1); 380 XDrawLine(display, win, botShadow, w, h, w, y + 1);
360 XDrawLine(Xdisplay, win, botShadow, w, h, x + 1, h); 381 XDrawLine(display, win, botShadow, w, h, x + 1, h);
361 } 382 }
362} 383}
363#endif 384#endif
364 385
365/* button shapes */ 386/* button shapes */
366#ifdef MENUBAR 387#ifdef MENUBAR
367/* EXTPROTO */ 388/* EXTPROTO */
368void 389void
369rxvt_Draw_Triangle(Display *Xdisplay, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) 390rxvt_Draw_Triangle(Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type)
370{ 391{
371 switch (type) { 392 switch (type)
393 {
372 case 'r': /* right triangle */ 394 case 'r': /* right triangle */
373 XDrawLine(Xdisplay, win, topShadow, x, y, x, y + w); 395 XDrawLine(display, win, topShadow, x, y, x, y + w);
374 XDrawLine(Xdisplay, win, topShadow, x, y, x + w, y + w / 2); 396 XDrawLine(display, win, topShadow, x, y, x + w, y + w / 2);
375 XDrawLine(Xdisplay, win, botShadow, x, y + w, x + w, y + w / 2); 397 XDrawLine(display, win, botShadow, x, y + w, x + w, y + w / 2);
376 break; 398 break;
377 399
378 case 'l': /* left triangle */ 400 case 'l': /* left triangle */
379 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w, y); 401 XDrawLine(display, win, botShadow, x + w, y + w, x + w, y);
380 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x, y + w / 2); 402 XDrawLine(display, win, botShadow, x + w, y + w, x, y + w / 2);
381 XDrawLine(Xdisplay, win, topShadow, x, y + w / 2, x + w, y); 403 XDrawLine(display, win, topShadow, x, y + w / 2, x + w, y);
382 break; 404 break;
383 405
384 case 'd': /* down triangle */ 406 case 'd': /* down triangle */
385 XDrawLine(Xdisplay, win, topShadow, x, y, x + w / 2, y + w); 407 XDrawLine(display, win, topShadow, x, y, x + w / 2, y + w);
386 XDrawLine(Xdisplay, win, topShadow, x, y, x + w, y); 408 XDrawLine(display, win, topShadow, x, y, x + w, y);
387 XDrawLine(Xdisplay, win, botShadow, x + w, y, x + w / 2, y + w); 409 XDrawLine(display, win, botShadow, x + w, y, x + w / 2, y + w);
388 break; 410 break;
389 411
390 case 'u': /* up triangle */ 412 case 'u': /* up triangle */
391 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w / 2, y); 413 XDrawLine(display, win, botShadow, x + w, y + w, x + w / 2, y);
392 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x, y + w); 414 XDrawLine(display, win, botShadow, x + w, y + w, x, y + w);
393 XDrawLine(Xdisplay, win, topShadow, x, y + w, x + w / 2, y); 415 XDrawLine(display, win, topShadow, x, y + w, x + w / 2, y);
394 break; 416 break;
395#if 0 417#if 0
396 case 's': /* square */ 418 case 's': /* square */
397 XDrawLine(Xdisplay, win, topShadow, x + w, y, x, y); 419 XDrawLine(display, win, topShadow, x + w, y, x, y);
398 XDrawLine(Xdisplay, win, topShadow, x, y, x, y + w); 420 XDrawLine(display, win, topShadow, x, y, x, y + w);
399 XDrawLine(Xdisplay, win, botShadow, x, y + w, x + w, y + w); 421 XDrawLine(display, win, botShadow, x, y + w, x + w, y + w);
400 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w, y); 422 XDrawLine(display, win, botShadow, x + w, y + w, x + w, y);
401 break; 423 break;
402#endif 424#endif
425
403 } 426 }
404} 427}
405#endif 428#endif
406/*----------------------- end-of-file (C source) -----------------------*/ 429/*----------------------- end-of-file (C source) -----------------------*/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines