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.6 by pcg, Mon Feb 9 07:11:49 2004 UTC vs.
Revision 1.10 by pcg, Sat Mar 6 00:05:01 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines