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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines