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.10 by pcg, Sat Mar 6 00:05:01 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>
25 25
26#include "../config.h" /* NECESSARY */ 26#include "../config.h" /* NECESSARY */
27#include "rxvt.h" /* NECESSARY */ 27#include "rxvt.h" /* NECESSARY */
28#include "misc.intpro" /* PROTOS for internal routines */ 28#include "misc.intpro" /* PROTOS for internal routines */
29 29
30/* EXTPROTO */ 30/* INTPROTO */
31char * 31char *
32rxvt_wcstombs (const wchar_t *str, int len) 32rxvt_wcstombs (const wchar_t *str, int len)
33{ 33{
34 mbstate mbs; 34 mbstate mbs;
35 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); 35 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
47 *dst++ = 0; 47 *dst++ = 0;
48 48
49 return r; 49 return r;
50} 50}
51 51
52/* EXTPROTO */ 52/* INTPROTO */
53char * 53char *
54rxvt_strdup (const char *str) 54rxvt_strdup (const char *str)
55{ 55{
56 return str ? strdup (str) : 0; 56 return str ? strdup (str) : 0;
57} 57}
58 58
59/* EXTPROTO */ 59/* INTPROTO */
60char * 60char *
61rxvt_r_basename (const char *str) 61rxvt_r_basename (const char *str)
62{ 62{
63 char *base = STRRCHR (str, '/'); 63 char *base = strrchr (str, '/');
64 64
65 return (char *) (base ? base + 1 : str); 65 return (char *) (base ? base + 1 : str);
66} 66}
67 67
68/* 68/*
69 * Print an error message 69 * Print an error message
70 */ 70 */
71/* EXTPROTO */ 71/* INTPROTO */
72void 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
73rxvt_print_error (const char *fmt,...) 87rxvt_log (const char *fmt,...)
74{ 88{
75 va_list arg_ptr; 89 va_list arg_ptr;
76 90
77 va_start (arg_ptr, fmt); 91 va_start (arg_ptr, fmt);
78 fprintf (stderr, RESNAME ": "); 92 rxvt_vlog (fmt, arg_ptr);
79 vfprintf (stderr, fmt, arg_ptr);
80 fprintf (stderr, "\n");
81 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);
82} 134}
83 135
84/* 136/*
85 * check that the first characters of S1 match S2 137 * check that the first characters of S1 match S2
86 * 138 *
87 * No Match 139 * No Match
88 * return: 0 140 * return: 0
89 * Match 141 * Match
90 * return: STRLEN (S2) 142 * return: strlen (S2)
91 */ 143 */
92/* EXTPROTO */ 144/* INTPROTO */
93int 145int
94rxvt_Str_match (const char *s1, const char *s2) 146rxvt_Str_match (const char *s1, const char *s2)
95{ 147{
96 int n = STRLEN (s2); 148 int n = strlen (s2);
97 149
98 return ((STRNCMP (s1, s2, n) == 0) ? n : 0); 150 return ((strncmp (s1, s2, n) == 0) ? n : 0);
99} 151}
100 152
101/* EXTPROTO */ 153/* INTPROTO */
102const char * 154const char *
103rxvt_Str_skip_space (const char *str) 155rxvt_Str_skip_space (const char *str)
104{ 156{
105 if (str) 157 if (str)
106 while (*str && isspace (*str)) 158 while (*str && isspace (*str))
111 163
112/* 164/*
113 * remove leading/trailing space and strip-off leading/trailing quotes. 165 * remove leading/trailing space and strip-off leading/trailing quotes.
114 * in place. 166 * in place.
115 */ 167 */
116/* EXTPROTO */ 168/* INTPROTO */
117char * 169char *
118rxvt_Str_trim (char *str) 170rxvt_Str_trim (char *str)
119{ 171{
120 char *r, *s; 172 char *r, *s;
121 int n; 173 int n;
160 * "M-x" prefixed strings, append "\r" if needed 212 * "M-x" prefixed strings, append "\r" if needed
161 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 213 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
162 * 214 *
163 * returns the converted string length 215 * returns the converted string length
164 */ 216 */
165/* EXTPROTO */ 217/* INTPROTO */
166int 218int
167rxvt_Str_escaped (char *str) 219rxvt_Str_escaped (char *str)
168{ 220{
169 char ch, *s, *d; 221 char ch, *s, *d;
170 int i, num, append = 0; 222 int i, num, append = 0;
238 * Split a comma-separated string into an array, stripping leading and 290 * Split a comma-separated string into an array, stripping leading and
239 * trailing spaces (and paired quotes) from each entry. Empty strings 291 * trailing spaces (and paired quotes) from each entry. Empty strings
240 * are properly returned 292 * are properly returned
241 * Caller should free each entry and array when done 293 * Caller should free each entry and array when done
242 */ 294 */
243/* EXTPROTO */ 295/* INTPROTO */
244char ** 296char **
245rxvt_splitcommastring (const char *cs) 297rxvt_splitcommastring (const char *cs)
246{ 298{
247 int l, n, p; 299 int l, n, p;
248 const char *s, *t; 300 const char *s, *t;
268 s = ++t; 320 s = ++t;
269 } 321 }
270 return ret; 322 return ret;
271} 323}
272 324
325void
326rxvt_freecommastring (char **cs)
327{
328 for (int i = 0; cs[i]; ++i)
329 free (cs[i]);
330
331 free (cs);
332}
333
273/*----------------------------------------------------------------------* 334/*----------------------------------------------------------------------*
274 * file searching 335 * file searching
275 */ 336 */
276 337
277/* #define DEBUG_SEARCH_PATH */ 338/* #define DEBUG_SEARCH_PATH */
290 int maxpath, len; 351 int maxpath, len;
291 const char *p, *path; 352 const char *p, *path;
292 char name[256]; 353 char name[256];
293 354
294 if (!access (file, R_OK)) /* found (plain name) in current directory */ 355 if (!access (file, R_OK)) /* found (plain name) in current directory */
295 return STRDUP (file); 356 return strdup (file);
296 357
297 /* semi-colon delimited */ 358 /* semi-colon delimited */
298 if ((p = STRCHR (file, ';'))) 359 if ((p = strchr (file, ';')))
299 len = (p - file); 360 len = (p - file);
300 else 361 else
301 len = STRLEN (file); 362 len = strlen (file);
302 363
303#ifdef DEBUG_SEARCH_PATH 364#ifdef DEBUG_SEARCH_PATH
304 getcwd (name, sizeof (name)); 365 getcwd (name, sizeof (name));
305 fprintf (stderr, "pwd: \"%s\"\n", name); 366 fprintf (stderr, "pwd: \"%s\"\n", name);
306 fprintf (stderr, "find: \"%.*s\"\n", len, file); 367 fprintf (stderr, "find: \"%.*s\"\n", len, file);
307#endif 368#endif
308 369
309 /* leave room for an extra '/' and trailing '\0' */ 370 /* leave room for an extra '/' and trailing '\0' */
310 maxpath = sizeof (name) - (len + (ext ? STRLEN (ext) : 0) + 2); 371 maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2);
311 if (maxpath <= 0) 372 if (maxpath <= 0)
312 return NULL; 373 return NULL;
313 374
314 /* check if we can find it now */ 375 /* check if we can find it now */
315 STRNCPY (name, file, len); 376 strncpy (name, file, len);
316 name[len] = '\0'; 377 name[len] = '\0';
317 378
318 if (!access (name, R_OK)) 379 if (!access (name, R_OK))
319 return STRDUP (name); 380 return strdup (name);
320 if (ext) 381 if (ext)
321 { 382 {
322 STRCAT (name, ext); 383 strcat (name, ext);
323 if (!access (name, R_OK)) 384 if (!access (name, R_OK))
324 return STRDUP (name); 385 return strdup (name);
325 } 386 }
326 for (path = pathlist; path != NULL && *path != '\0'; path = p) 387 for (path = pathlist; path != NULL && *path != '\0'; path = p)
327 { 388 {
328 int n; 389 int n;
329 390
330 /* colon delimited */ 391 /* colon delimited */
331 if ((p = STRCHR (path, ':')) == NULL) 392 if ((p = strchr (path, ':')) == NULL)
332 p = STRCHR (path, '\0'); 393 p = strchr (path, '\0');
333 394
334 n = (p - path); 395 n = (p - path);
335 if (*p != '\0') 396 if (*p != '\0')
336 p++; 397 p++;
337 398
338 if (n > 0 && n <= maxpath) 399 if (n > 0 && n <= maxpath)
339 { 400 {
340 STRNCPY (name, path, n); 401 strncpy (name, path, n);
341 if (name[n - 1] != '/') 402 if (name[n - 1] != '/')
342 name[n++] = '/'; 403 name[n++] = '/';
343 name[n] = '\0'; 404 name[n] = '\0';
344 STRNCAT (name, file, len); 405 strncat (name, file, len);
345 406
346 if (!access (name, R_OK)) 407 if (!access (name, R_OK))
347 return STRDUP (name); 408 return strdup (name);
348 if (ext) 409 if (ext)
349 { 410 {
350 STRCAT (name, ext); 411 strcat (name, ext);
351 if (!access (name, R_OK)) 412 if (!access (name, R_OK))
352 return STRDUP (name); 413 return strdup (name);
353 } 414 }
354 } 415 }
355 } 416 }
356 return NULL; 417 return NULL;
357} 418}
358 419
359/* EXTPROTO */ 420/* INTPROTO */
360char * 421char *
361rxvt_File_find (const char *file, const char *ext, const char *path) 422rxvt_File_find (const char *file, const char *ext, const char *path)
362{ 423{
363 char *f; 424 char *f;
364 425
387 448
388/* 449/*
389 * Draw top/left and bottom/right border shadows around windows 450 * Draw top/left and bottom/right border shadows around windows
390 */ 451 */
391#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 452#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
392/* EXTPROTO */ 453/* INTPROTO */
393void 454void
394rxvt_Draw_Shadow (Display *display, 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)
395{ 456{
396 int shadow; 457 int shadow;
397 458
408} 469}
409#endif 470#endif
410 471
411/* button shapes */ 472/* button shapes */
412#ifdef MENUBAR 473#ifdef MENUBAR
413/* EXTPROTO */ 474/* INTPROTO */
414void 475void
415rxvt_Draw_Triangle (Display *display, 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)
416{ 477{
417 switch (type) 478 switch (type)
418 { 479 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines