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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines