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.3 by pcg, Sat Jan 31 00:20:21 2004 UTC vs.
Revision 1.7 by pcg, Fri Feb 13 12:16:21 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines