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.5 by pcg, Sun Feb 1 01:34:41 2004 UTC vs.
Revision 1.16 by root, Sun Nov 21 19:04:07 2004 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
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 * 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.
24 25
25#include "../config.h" /* NECESSARY */ 26#include "../config.h" /* NECESSARY */
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/* INTPROTO */
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/* INTPROTO */
30char * 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/* INTPROTO */
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/* INTPROTO */
49void 72void
73rxvt_vlog (const char *fmt, va_list arg_ptr)
74{
75 char msg[1024];
76
77 vsnprintf (msg, sizeof msg, fmt, arg_ptr);
78
79 if (GET_R && GET_R->log_hook)
80 (*GET_R->log_hook) (msg);
81 else
82 write (STDOUT_FILENO, msg, strlen (msg));
83}
84
85/* INTPROTO */
86void
50rxvt_print_error(const char *fmt,...) 87rxvt_log (const char *fmt,...)
51{ 88{
52 va_list arg_ptr; 89 va_list arg_ptr;
53 90
54 va_start(arg_ptr, fmt); 91 va_start (arg_ptr, fmt);
55 fprintf(stderr, APL_NAME ": "); 92 rxvt_vlog (fmt, arg_ptr);
56 vfprintf(stderr, fmt, arg_ptr);
57 fprintf(stderr, "\n");
58 va_end(arg_ptr); 93 va_end (arg_ptr);
94}
95
96/*
97 * Print an error message
98 */
99/* INTPROTO */
100void
101rxvt_warn (const char *fmt,...)
102{
103 va_list arg_ptr;
104
105 rxvt_log ("%s: ", RESNAME);
106
107 va_start (arg_ptr, fmt);
108 rxvt_vlog (fmt, arg_ptr);
109 va_end (arg_ptr);
110}
111
112/* INTPROTO */
113void
114rxvt_fatal (const char *fmt,...)
115{
116 va_list arg_ptr;
117
118 rxvt_log ("%s: ", RESNAME);
119
120 va_start (arg_ptr, fmt);
121 rxvt_vlog (fmt, arg_ptr);
122 va_end (arg_ptr);
123
124 rxvt_exit_failure ();
125}
126
127class rxvt_failure_exception rxvt_failure_exception;
128
129/* INTPROTO */
130void
131rxvt_exit_failure ()
132{
133 throw (rxvt_failure_exception);
59} 134}
60 135
61/* 136/*
62 * check that the first characters of S1 match S2 137 * check that the first characters of S1 match S2
63 * 138 *
64 * No Match 139 * No Match
65 * return: 0 140 * return: 0
66 * Match 141 * Match
67 * return: STRLEN (S2) 142 * return: strlen (S2)
68 */ 143 */
69/* EXTPROTO */ 144/* INTPROTO */
70int 145int
71rxvt_Str_match(const char *s1, const char *s2) 146rxvt_Str_match (const char *s1, const char *s2)
72{ 147{
73 int n = STRLEN(s2); 148 int n = strlen (s2);
74 149
75 return ((STRNCMP(s1, s2, n) == 0) ? n : 0); 150 return ((strncmp (s1, s2, n) == 0) ? n : 0);
76} 151}
77 152
78/* EXTPROTO */ 153/* INTPROTO */
79const char * 154const char *
80rxvt_Str_skip_space(const char *str) 155rxvt_Str_skip_space (const char *str)
81{ 156{
82 if (str) 157 if (str)
83 while (*str && isspace(*str)) 158 while (*str && isspace (*str))
84 str++; 159 str++;
160
85 return str; 161 return str;
86} 162}
87 163
88/* 164/*
89 * remove leading/trailing space and strip-off leading/trailing quotes. 165 * remove leading/trailing space and strip-off leading/trailing quotes.
90 * in place. 166 * in place.
91 */ 167 */
92/* EXTPROTO */ 168/* INTPROTO */
93char * 169char *
94rxvt_Str_trim(char *str) 170rxvt_Str_trim (char *str)
95{ 171{
96 char *r, *s; 172 char *r, *s;
97 int n; 173 int n;
98 174
99 if (!str || !*str) /* shortcut */ 175 if (!str || !*str) /* shortcut */
100 return str; 176 return str;
101 177
102 /* skip leading spaces */ 178 /* skip leading spaces */
103 for (s = str; *s && isspace(*s); s++) ; 179 for (s = str; *s && isspace (*s); s++) ;
104 /* goto end of string */ 180 /* goto end of string */
105 for (n = 0, r = s; *r++; n++) ; 181 for (n = 0, r = s; *r++; n++) ;
106 r -= 2; 182 r -= 2;
107 /* dump return */ 183 /* dump return */
108 if (n > 0 && *r == '\n') 184 if (n > 0 && *r == '\n')
109 n--, r--; 185 n--, r--;
110 /* backtrack along trailing spaces */ 186 /* backtrack along trailing spaces */
111 for (; n > 0 && isspace(*r); r--, n--) ; 187 for (; n > 0 && isspace (*r); r--, n--) ;
112 /* skip matching leading/trailing quotes */ 188 /* skip matching leading/trailing quotes */
113 if (*s == '"' && *r == '"' && n > 1) 189 if (*s == '"' && *r == '"' && n > 1)
114 { 190 {
115 s++; 191 s++;
116 n -= 2; 192 n -= 2;
117 } 193 }
194
118 /* copy back over: forwards copy */ 195 /* copy back over: forwards copy */
119 for (r = str; n; n--) 196 for (r = str; n; n--)
120 *r++ = *s++; 197 *r++ = *s++;
121 *r = '\0'; 198 *r = '\0';
122 199
135 * "M-x" prefixed strings, append "\r" if needed 212 * "M-x" prefixed strings, append "\r" if needed
136 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 213 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
137 * 214 *
138 * returns the converted string length 215 * returns the converted string length
139 */ 216 */
140/* EXTPROTO */ 217/* INTPROTO */
141int 218int
142rxvt_Str_escaped(char *str) 219rxvt_Str_escaped (char *str)
143{ 220{
144 char ch, *s, *d; 221 char ch, *s, *d;
145 int i, num, append = 0; 222 int i, num, append = 0;
146 223
147 if (!str || !*str) 224 if (!str || !*str)
152 if (*s == 'M' && s[1] == '-') 229 if (*s == 'M' && s[1] == '-')
153 { 230 {
154 /* Emacs convenience, replace leading `M-..' with `\E..' */ 231 /* Emacs convenience, replace leading `M-..' with `\E..' */
155 *d++ = C0_ESC; 232 *d++ = C0_ESC;
156 s += 2; 233 s += 2;
157 if (toupper(*s) == 'X') 234 if (toupper (*s) == 'X')
158 /* append carriage-return for `M-xcommand' */ 235 /* append carriage-return for `M-xcommand' */
159 for (*d++ = 'x', append = '\r', s++; isspace(*s); s++) ; 236 for (*d++ = 'x', append = '\r', s++; isspace (*s); s++) ;
160 } 237 }
161 for (; (ch = *s++);) 238 for (; (ch = *s++);)
162 { 239 {
163 if (ch == '\\') 240 if (ch == '\\')
164 { 241 {
189 ch = C0_HT; /* tab */ 266 ch = C0_HT; /* tab */
190 } 267 }
191 else if (ch == '^') 268 else if (ch == '^')
192 { 269 {
193 ch = *s++; 270 ch = *s++;
194 ch = toupper(ch); 271 ch = toupper (ch);
195 ch = (ch == '?' ? 127 : (ch - '@')); 272 ch = (ch == '?' ? 127 : (ch - '@'));
196 } 273 }
197 *d++ = ch; 274 *d++ = ch;
198 } 275 }
199 276
213 * Split a comma-separated string into an array, stripping leading and 290 * Split a comma-separated string into an array, stripping leading and
214 * trailing spaces (and paired quotes) from each entry. Empty strings 291 * trailing spaces (and paired quotes) from each entry. Empty strings
215 * are properly returned 292 * are properly returned
216 * Caller should free each entry and array when done 293 * Caller should free each entry and array when done
217 */ 294 */
218/* EXTPROTO */ 295/* INTPROTO */
219char ** 296char **
220rxvt_splitcommastring(const char *cs) 297rxvt_splitcommastring (const char *cs)
221{ 298{
222 int l, n, p; 299 int l, n, p;
223 const char *s, *t; 300 const char *s, *t;
224 char **ret; 301 char **ret;
225 302
227 s = ""; 304 s = "";
228 305
229 for (n = 1, t = s; *t; t++) 306 for (n = 1, t = s; *t; t++)
230 if (*t == ',') 307 if (*t == ',')
231 n++; 308 n++;
232 ret = (char **)malloc((n + 1) * sizeof(char *)); 309 ret = (char **)malloc ((n + 1) * sizeof (char *));
233 ret[n] = NULL; 310 ret[n] = NULL;
234 311
235 for (l = 0, t = s; l < n; l++) 312 for (l = 0, t = s; l < n; l++)
236 { 313 {
237 for ( ; *t && *t != ','; t++) ; 314 for ( ; *t && *t != ','; t++) ;
238 p = t - s; 315 p = t - s;
239 ret[l] = (char *)malloc(p + 1); 316 ret[l] = (char *)malloc (p + 1);
240 strncpy(ret[l], s, p); 317 strncpy (ret[l], s, p);
241 ret[l][p] = '\0'; 318 ret[l][p] = '\0';
242 rxvt_Str_trim(ret[l]); 319 rxvt_Str_trim (ret[l]);
243 s = ++t; 320 s = ++t;
244 } 321 }
245 return ret; 322 return ret;
323}
324
325void
326rxvt_freecommastring (char **cs)
327{
328 for (int i = 0; cs[i]; ++i)
329 free (cs[i]);
330
331 free (cs);
246} 332}
247 333
248/*----------------------------------------------------------------------* 334/*----------------------------------------------------------------------*
249 * file searching 335 * file searching
250 */ 336 */
258 * 344 *
259 * FILE is either semi-colon or zero terminated 345 * FILE is either semi-colon or zero terminated
260 */ 346 */
261/* INTPROTO */ 347/* INTPROTO */
262char * 348char *
263rxvt_File_search_path(const char *pathlist, const char *file, const char *ext) 349rxvt_File_search_path (const char *pathlist, const char *file, const char *ext)
264{ 350{
265 int maxpath, len; 351 int maxpath, len;
266 const char *p, *path; 352 const char *p, *path;
267 char name[256]; 353 char name[256];
268 354
269 if (!access(file, R_OK)) /* found (plain name) in current directory */ 355 if (!access (file, R_OK)) /* found (plain name) in current directory */
270 return STRDUP(file); 356 return strdup (file);
271 357
272 /* semi-colon delimited */ 358 /* semi-colon delimited */
273 if ((p = STRCHR(file, ';'))) 359 if ((p = strchr (file, ';')))
274 len = (p - file); 360 len = (p - file);
275 else 361 else
276 len = STRLEN(file); 362 len = strlen (file);
277 363
278#ifdef DEBUG_SEARCH_PATH 364#ifdef DEBUG_SEARCH_PATH
279 getcwd(name, sizeof(name)); 365 getcwd (name, sizeof (name));
280 fprintf(stderr, "pwd: \"%s\"\n", name); 366 fprintf (stderr, "pwd: \"%s\"\n", name);
281 fprintf(stderr, "find: \"%.*s\"\n", len, file); 367 fprintf (stderr, "find: \"%.*s\"\n", len, file);
282#endif 368#endif
283 369
284 /* leave room for an extra '/' and trailing '\0' */ 370 /* leave room for an extra '/' and trailing '\0' */
285 maxpath = sizeof(name) - (len + (ext ? STRLEN(ext) : 0) + 2); 371 maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2);
286 if (maxpath <= 0) 372 if (maxpath <= 0)
287 return NULL; 373 return NULL;
288 374
289 /* check if we can find it now */ 375 /* check if we can find it now */
290 STRNCPY(name, file, len); 376 strncpy (name, file, len);
291 name[len] = '\0'; 377 name[len] = '\0';
292 378
293 if (!access(name, R_OK)) 379 if (!access (name, R_OK))
294 return STRDUP(name); 380 return strdup (name);
295 if (ext) 381 if (ext)
296 { 382 {
297 STRCAT(name, ext); 383 strcat (name, ext);
298 if (!access(name, R_OK)) 384 if (!access (name, R_OK))
299 return STRDUP(name); 385 return strdup (name);
300 } 386 }
301 for (path = pathlist; path != NULL && *path != '\0'; path = p) 387 for (path = pathlist; path != NULL && *path != '\0'; path = p)
302 { 388 {
303 int n; 389 int n;
304 390
305 /* colon delimited */ 391 /* colon delimited */
306 if ((p = STRCHR(path, ':')) == NULL) 392 if ((p = strchr (path, ':')) == NULL)
307 p = STRCHR(path, '\0'); 393 p = strchr (path, '\0');
308 394
309 n = (p - path); 395 n = (p - path);
310 if (*p != '\0') 396 if (*p != '\0')
311 p++; 397 p++;
312 398
313 if (n > 0 && n <= maxpath) 399 if (n > 0 && n <= maxpath)
314 { 400 {
315 STRNCPY(name, path, n); 401 strncpy (name, path, n);
316 if (name[n - 1] != '/') 402 if (name[n - 1] != '/')
317 name[n++] = '/'; 403 name[n++] = '/';
318 name[n] = '\0'; 404 name[n] = '\0';
319 STRNCAT(name, file, len); 405 strncat (name, file, len);
320 406
321 if (!access(name, R_OK)) 407 if (!access (name, R_OK))
322 return STRDUP(name); 408 return strdup (name);
323 if (ext) 409 if (ext)
324 { 410 {
325 STRCAT(name, ext); 411 strcat (name, ext);
326 if (!access(name, R_OK)) 412 if (!access (name, R_OK))
327 return STRDUP(name); 413 return strdup (name);
328 } 414 }
329 } 415 }
330 } 416 }
331 return NULL; 417 return NULL;
332} 418}
333 419
334/* EXTPROTO */ 420/* INTPROTO */
335char * 421char *
336rxvt_File_find(const char *file, const char *ext, const char *path) 422rxvt_File_find (const char *file, const char *ext, const char *path)
337{ 423{
338 char *f; 424 char *f;
339 425
340 if (file == NULL || *file == '\0') 426 if (file == NULL || *file == '\0')
341 return NULL; 427 return NULL;
342 428
343 /* search environment variables here too */ 429 /* search environment variables here too */
344 if ((f = rxvt_File_search_path(path, file, ext)) == NULL) 430 if ((f = rxvt_File_search_path (path, file, ext)) == NULL)
345#ifdef PATH_ENV 431#ifdef PATH_ENV
346 if ((f = rxvt_File_search_path(getenv(PATH_ENV), file, ext)) == NULL) 432 if ((f = rxvt_File_search_path (getenv (PATH_ENV), file, ext)) == NULL)
347#endif 433#endif
348 f = rxvt_File_search_path(getenv("PATH"), file, ext); 434 f = rxvt_File_search_path (getenv ("PATH"), file, ext);
349 435
350#ifdef DEBUG_SEARCH_PATH 436#ifdef DEBUG_SEARCH_PATH
351 if (f) 437 if (f)
352 fprintf(stderr, "found: \"%s\"\n", f); 438 fprintf (stderr, "found: \"%s\"\n", f);
353#endif 439#endif
354 440
355 return f; 441 return f;
356} 442}
357#endif /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */ 443#endif /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */
362 448
363/* 449/*
364 * Draw top/left and bottom/right border shadows around windows 450 * Draw top/left and bottom/right border shadows around windows
365 */ 451 */
366#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 452#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
367/* EXTPROTO */ 453/* INTPROTO */
368void 454void
369rxvt_Draw_Shadow(Display *Xdisplay, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) 455rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h)
370{ 456{
371 int shadow; 457 int shadow;
372 458
373 shadow = (w == 0 || h == 0) ? 1 : SHADOW; 459 shadow = (w == 0 || h == 0) ? 1 : SHADOW;
374 w += x - 1; 460 w += x - 1;
375 h += y - 1; 461 h += y - 1;
376 for (; shadow-- > 0; x++, y++, w--, h--) 462 for (; shadow-- > 0; x++, y++, w--, h--)
377 { 463 {
378 XDrawLine(Xdisplay, win, topShadow, x, y, w, y); 464 XDrawLine (display, win, topShadow, x, y, w, y);
379 XDrawLine(Xdisplay, win, topShadow, x, y, x, h); 465 XDrawLine (display, win, topShadow, x, y, x, h);
380 XDrawLine(Xdisplay, win, botShadow, w, h, w, y + 1); 466 XDrawLine (display, win, botShadow, w, h, w, y + 1);
381 XDrawLine(Xdisplay, win, botShadow, w, h, x + 1, h); 467 XDrawLine (display, win, botShadow, w, h, x + 1, h);
382 } 468 }
383} 469}
384#endif 470#endif
385 471
386/* button shapes */ 472/* button shapes */
387#ifdef MENUBAR 473#ifdef MENUBAR
388/* EXTPROTO */ 474/* INTPROTO */
389void 475void
390rxvt_Draw_Triangle(Display *Xdisplay, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) 476rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type)
391{ 477{
392 switch (type) 478 switch (type)
393 { 479 {
394 case 'r': /* right triangle */ 480 case 'r': /* right triangle */
395 XDrawLine(Xdisplay, win, topShadow, x, y, x, y + w); 481 XDrawLine (display, win, topShadow, x, y, x, y + w);
396 XDrawLine(Xdisplay, win, topShadow, x, y, x + w, y + w / 2); 482 XDrawLine (display, win, topShadow, x, y, x + w, y + w / 2);
397 XDrawLine(Xdisplay, win, botShadow, x, y + w, x + w, y + w / 2); 483 XDrawLine (display, win, botShadow, x, y + w, x + w, y + w / 2);
398 break; 484 break;
399 485
400 case 'l': /* left triangle */ 486 case 'l': /* left triangle */
401 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w, y); 487 XDrawLine (display, win, botShadow, x + w, y + w, x + w, y);
402 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x, y + w / 2); 488 XDrawLine (display, win, botShadow, x + w, y + w, x, y + w / 2);
403 XDrawLine(Xdisplay, win, topShadow, x, y + w / 2, x + w, y); 489 XDrawLine (display, win, topShadow, x, y + w / 2, x + w, y);
404 break; 490 break;
405 491
406 case 'd': /* down triangle */ 492 case 'd': /* down triangle */
407 XDrawLine(Xdisplay, win, topShadow, x, y, x + w / 2, y + w); 493 XDrawLine (display, win, topShadow, x, y, x + w / 2, y + w);
408 XDrawLine(Xdisplay, win, topShadow, x, y, x + w, y); 494 XDrawLine (display, win, topShadow, x, y, x + w, y);
409 XDrawLine(Xdisplay, win, botShadow, x + w, y, x + w / 2, y + w); 495 XDrawLine (display, win, botShadow, x + w, y, x + w / 2, y + w);
410 break; 496 break;
411 497
412 case 'u': /* up triangle */ 498 case 'u': /* up triangle */
413 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w / 2, y); 499 XDrawLine (display, win, botShadow, x + w, y + w, x + w / 2, y);
414 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x, y + w); 500 XDrawLine (display, win, botShadow, x + w, y + w, x, y + w);
415 XDrawLine(Xdisplay, win, topShadow, x, y + w, x + w / 2, y); 501 XDrawLine (display, win, topShadow, x, y + w, x + w / 2, y);
416 break; 502 break;
417#if 0 503#if 0
418 case 's': /* square */ 504 case 's': /* square */
419 XDrawLine(Xdisplay, win, topShadow, x + w, y, x, y); 505 XDrawLine (display, win, topShadow, x + w, y, x, y);
420 XDrawLine(Xdisplay, win, topShadow, x, y, x, y + w); 506 XDrawLine (display, win, topShadow, x, y, x, y + w);
421 XDrawLine(Xdisplay, win, botShadow, x, y + w, x + w, y + w); 507 XDrawLine (display, win, botShadow, x, y + w, x + w, y + w);
422 XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w, y); 508 XDrawLine (display, win, botShadow, x + w, y + w, x + w, y);
423 break; 509 break;
424#endif 510#endif
425 511
426 } 512 }
427} 513}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines