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.22 by root, Mon Dec 20 18:07:19 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 */
31char * 30char *
32rxvt_wcstombs (const wchar_t *str, int len) 31rxvt_wcstombs (const wchar_t *str, int len)
33{ 32{
33 if (len < 0) len = wcslen (str);
34
34 mbstate mbs; 35 mbstate mbs;
35 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); 36 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
36 37
37 char *dst = r; 38 char *dst = r;
38 while (len--) 39 while (len--)
47 *dst++ = 0; 48 *dst++ = 0;
48 49
49 return r; 50 return r;
50} 51}
51 52
52/* EXTPROTO */ 53wchar_t *
54rxvt_mbstowcs (const char *str, int len)
55{
56 if (len < 0) len = strlen (str);
57
58 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t));
59
60 if (mbstowcs (r, str, len + 1) < 0)
61 *r = 0;
62
63 return r;
64}
65
66char *
67rxvt_wcstoutf8 (const wchar_t *str, int len)
68{
69 if (len < 0) len = wcslen (str);
70
71 char *r = (char *)rxvt_malloc (len * 4 + 1);
72 char *p = r;
73
74 while (len--)
75 {
76 unicode_t w = *str++ & UNICODE_MASK;
77
78 if (w < 0x000080)
79 *p++ = w;
80 else if (w < 0x000800)
81 *p++ = 0xc0 | ( w >> 6),
82 *p++ = 0x80 | ( w & 0x3f);
83 else if (w < 0x010000)
84 *p++ = 0xe0 | ( w >> 12 ),
85 *p++ = 0x80 | ((w >> 6) & 0x3f),
86 *p++ = 0x80 | ( w & 0x3f);
87 else if (w < 0x110000)
88 *p++ = 0xf0 | ( w >> 18),
89 *p++ = 0x80 | ((w >> 12) & 0x3f),
90 *p++ = 0x80 | ((w >> 6) & 0x3f),
91 *p++ = 0x80 | ( w & 0x3f);
92 else
93 *p++ = '?';
94 }
95
96 *p = 0;
97
98 return r;
99}
100
101wchar_t *
102rxvt_utf8towcs (const char *str, int len)
103{
104 if (len < 0) len = strlen (str);
105
106 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)),
107 *p = r;
108
109 unsigned char *s = (unsigned char *)str,
110 *e = s + len;
111
112 for (;;)
113 {
114 len = e - s;
115
116 if (len == 0)
117 break;
118 else if (s[0] < 0x80)
119 *p++ = *s++;
120 else if (len >= 2
121 && s[0] >= 0xc2 && s[0] <= 0xdf
122 && (s[1] & 0xc0) == 0x80)
123 {
124 *p++ = ((s[0] & 0x1f) << 6)
125 | (s[1] & 0x3f);
126 s += 2;
127 }
128 else if (len >= 3
129 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf)
130 || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf)
131 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f)
132 || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf)
133 )
134 && (s[2] & 0xc0) == 0x80)
135 {
136 *p++ = ((s[0] & 0x0f) << 12)
137 | ((s[1] & 0x3f) << 6)
138 | (s[2] & 0x3f);
139 s += 3;
140 }
141 else if (len >= 4
142 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf)
143 || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf)
144 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f)
145 )
146 && (s[2] & 0xc0) == 0x80
147 && (s[3] & 0xc0) == 0x80)
148 {
149 *p++ = ((s[0] & 0x07) << 18)
150 | ((s[1] & 0x3f) << 12)
151 | ((s[2] & 0x3f) << 6)
152 | (s[3] & 0x3f);
153 s += 4;
154 }
155 else
156 {
157 *p++ = 0xfffd;
158 s++;
159 }
160 }
161
162 *p = 0;
163
164 return r;
165}
166
53char * 167char *
54rxvt_strdup (const char *str) 168rxvt_strdup (const char *str)
55{ 169{
56 return str ? strdup (str) : 0; 170 return str ? strdup (str) : 0;
57} 171}
58 172
59/* EXTPROTO */ 173/* INTPROTO */
60char * 174char *
61rxvt_r_basename (const char *str) 175rxvt_r_basename (const char *str)
62{ 176{
63 char *base = STRRCHR (str, '/'); 177 char *base = strrchr (str, '/');
64 178
65 return (char *) (base ? base + 1 : str); 179 return (char *) (base ? base + 1 : str);
66} 180}
67 181
68/* 182/*
69 * Print an error message 183 * Print an error message
70 */ 184 */
71/* EXTPROTO */ 185/* INTPROTO */
72void 186void
187rxvt_vlog (const char *fmt, va_list arg_ptr)
188{
189 char msg[1024];
190
191 vsnprintf (msg, sizeof msg, fmt, arg_ptr);
192
193 if (GET_R && GET_R->log_hook)
194 (*GET_R->log_hook) (msg);
195 else
196 write (STDOUT_FILENO, msg, strlen (msg));
197}
198
199/* INTPROTO */
200void
73rxvt_print_error (const char *fmt,...) 201rxvt_log (const char *fmt,...)
74{ 202{
75 va_list arg_ptr; 203 va_list arg_ptr;
76 204
77 va_start (arg_ptr, fmt); 205 va_start (arg_ptr, fmt);
78 fprintf (stderr, RESNAME ": "); 206 rxvt_vlog (fmt, arg_ptr);
79 vfprintf (stderr, fmt, arg_ptr);
80 fprintf (stderr, "\n");
81 va_end (arg_ptr); 207 va_end (arg_ptr);
208}
209
210/*
211 * Print an error message
212 */
213/* INTPROTO */
214void
215rxvt_warn (const char *fmt,...)
216{
217 va_list arg_ptr;
218
219 rxvt_log ("%s: ", RESNAME);
220
221 va_start (arg_ptr, fmt);
222 rxvt_vlog (fmt, arg_ptr);
223 va_end (arg_ptr);
224}
225
226/* INTPROTO */
227void
228rxvt_fatal (const char *fmt,...)
229{
230 va_list arg_ptr;
231
232 rxvt_log ("%s: ", RESNAME);
233
234 va_start (arg_ptr, fmt);
235 rxvt_vlog (fmt, arg_ptr);
236 va_end (arg_ptr);
237
238 rxvt_exit_failure ();
239}
240
241class rxvt_failure_exception rxvt_failure_exception;
242
243/* INTPROTO */
244void
245rxvt_exit_failure ()
246{
247 throw (rxvt_failure_exception);
82} 248}
83 249
84/* 250/*
85 * check that the first characters of S1 match S2 251 * check that the first characters of S1 match S2
86 * 252 *
87 * No Match 253 * No Match
88 * return: 0 254 * return: 0
89 * Match 255 * Match
90 * return: STRLEN (S2) 256 * return: strlen (S2)
91 */ 257 */
92/* EXTPROTO */ 258/* INTPROTO */
93int 259int
94rxvt_Str_match (const char *s1, const char *s2) 260rxvt_Str_match (const char *s1, const char *s2)
95{ 261{
96 int n = STRLEN (s2); 262 int n = strlen (s2);
97 263
98 return ((STRNCMP (s1, s2, n) == 0) ? n : 0); 264 return ((strncmp (s1, s2, n) == 0) ? n : 0);
99} 265}
100 266
101/* EXTPROTO */ 267/* INTPROTO */
102const char * 268const char *
103rxvt_Str_skip_space (const char *str) 269rxvt_Str_skip_space (const char *str)
104{ 270{
105 if (str) 271 if (str)
106 while (*str && isspace (*str)) 272 while (*str && isspace (*str))
111 277
112/* 278/*
113 * remove leading/trailing space and strip-off leading/trailing quotes. 279 * remove leading/trailing space and strip-off leading/trailing quotes.
114 * in place. 280 * in place.
115 */ 281 */
116/* EXTPROTO */ 282/* INTPROTO */
117char * 283char *
118rxvt_Str_trim (char *str) 284rxvt_Str_trim (char *str)
119{ 285{
120 char *r, *s; 286 char *r, *s;
121 int n; 287 int n;
160 * "M-x" prefixed strings, append "\r" if needed 326 * "M-x" prefixed strings, append "\r" if needed
161 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 327 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
162 * 328 *
163 * returns the converted string length 329 * returns the converted string length
164 */ 330 */
165/* EXTPROTO */ 331/* INTPROTO */
166int 332int
167rxvt_Str_escaped (char *str) 333rxvt_Str_escaped (char *str)
168{ 334{
169 char ch, *s, *d; 335 char ch, *s, *d;
170 int i, num, append = 0; 336 int i, num, append = 0;
238 * Split a comma-separated string into an array, stripping leading and 404 * Split a comma-separated string into an array, stripping leading and
239 * trailing spaces (and paired quotes) from each entry. Empty strings 405 * trailing spaces (and paired quotes) from each entry. Empty strings
240 * are properly returned 406 * are properly returned
241 * Caller should free each entry and array when done 407 * Caller should free each entry and array when done
242 */ 408 */
243/* EXTPROTO */ 409/* INTPROTO */
244char ** 410char **
245rxvt_splitcommastring (const char *cs) 411rxvt_splitcommastring (const char *cs)
246{ 412{
247 int l, n, p; 413 int l, n, p;
248 const char *s, *t; 414 const char *s, *t;
268 s = ++t; 434 s = ++t;
269 } 435 }
270 return ret; 436 return ret;
271} 437}
272 438
439void
440rxvt_freecommastring (char **cs)
441{
442 for (int i = 0; cs[i]; ++i)
443 free (cs[i]);
444
445 free (cs);
446}
447
273/*----------------------------------------------------------------------* 448/*----------------------------------------------------------------------*
274 * file searching 449 * file searching
275 */ 450 */
276 451
277/* #define DEBUG_SEARCH_PATH */ 452/* #define DEBUG_SEARCH_PATH */
290 int maxpath, len; 465 int maxpath, len;
291 const char *p, *path; 466 const char *p, *path;
292 char name[256]; 467 char name[256];
293 468
294 if (!access (file, R_OK)) /* found (plain name) in current directory */ 469 if (!access (file, R_OK)) /* found (plain name) in current directory */
295 return STRDUP (file); 470 return strdup (file);
296 471
297 /* semi-colon delimited */ 472 /* semi-colon delimited */
298 if ((p = STRCHR (file, ';'))) 473 if ((p = strchr (file, ';')))
299 len = (p - file); 474 len = (p - file);
300 else 475 else
301 len = STRLEN (file); 476 len = strlen (file);
302 477
303#ifdef DEBUG_SEARCH_PATH 478#ifdef DEBUG_SEARCH_PATH
304 getcwd (name, sizeof (name)); 479 getcwd (name, sizeof (name));
305 fprintf (stderr, "pwd: \"%s\"\n", name); 480 fprintf (stderr, "pwd: \"%s\"\n", name);
306 fprintf (stderr, "find: \"%.*s\"\n", len, file); 481 fprintf (stderr, "find: \"%.*s\"\n", len, file);
307#endif 482#endif
308 483
309 /* leave room for an extra '/' and trailing '\0' */ 484 /* leave room for an extra '/' and trailing '\0' */
310 maxpath = sizeof (name) - (len + (ext ? STRLEN (ext) : 0) + 2); 485 maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2);
311 if (maxpath <= 0) 486 if (maxpath <= 0)
312 return NULL; 487 return NULL;
313 488
314 /* check if we can find it now */ 489 /* check if we can find it now */
315 STRNCPY (name, file, len); 490 strncpy (name, file, len);
316 name[len] = '\0'; 491 name[len] = '\0';
317 492
318 if (!access (name, R_OK)) 493 if (!access (name, R_OK))
319 return STRDUP (name); 494 return strdup (name);
320 if (ext) 495 if (ext)
321 { 496 {
322 STRCAT (name, ext); 497 strcat (name, ext);
323 if (!access (name, R_OK)) 498 if (!access (name, R_OK))
324 return STRDUP (name); 499 return strdup (name);
325 } 500 }
326 for (path = pathlist; path != NULL && *path != '\0'; path = p) 501 for (path = pathlist; path != NULL && *path != '\0'; path = p)
327 { 502 {
328 int n; 503 int n;
329 504
330 /* colon delimited */ 505 /* colon delimited */
331 if ((p = STRCHR (path, ':')) == NULL) 506 if ((p = strchr (path, ':')) == NULL)
332 p = STRCHR (path, '\0'); 507 p = strchr (path, '\0');
333 508
334 n = (p - path); 509 n = (p - path);
335 if (*p != '\0') 510 if (*p != '\0')
336 p++; 511 p++;
337 512
338 if (n > 0 && n <= maxpath) 513 if (n > 0 && n <= maxpath)
339 { 514 {
340 STRNCPY (name, path, n); 515 strncpy (name, path, n);
341 if (name[n - 1] != '/') 516 if (name[n - 1] != '/')
342 name[n++] = '/'; 517 name[n++] = '/';
343 name[n] = '\0'; 518 name[n] = '\0';
344 STRNCAT (name, file, len); 519 strncat (name, file, len);
345 520
346 if (!access (name, R_OK)) 521 if (!access (name, R_OK))
347 return STRDUP (name); 522 return strdup (name);
348 if (ext) 523 if (ext)
349 { 524 {
350 STRCAT (name, ext); 525 strcat (name, ext);
351 if (!access (name, R_OK)) 526 if (!access (name, R_OK))
352 return STRDUP (name); 527 return strdup (name);
353 } 528 }
354 } 529 }
355 } 530 }
356 return NULL; 531 return NULL;
357} 532}
358 533
359/* EXTPROTO */ 534/* INTPROTO */
360char * 535char *
361rxvt_File_find (const char *file, const char *ext, const char *path) 536rxvt_File_find (const char *file, const char *ext, const char *path)
362{ 537{
363 char *f; 538 char *f;
364 539
387 562
388/* 563/*
389 * Draw top/left and bottom/right border shadows around windows 564 * Draw top/left and bottom/right border shadows around windows
390 */ 565 */
391#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 566#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
392/* EXTPROTO */ 567/* INTPROTO */
393void 568void
394rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) 569rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h)
395{ 570{
396 int shadow; 571 int shadow;
397 572
408} 583}
409#endif 584#endif
410 585
411/* button shapes */ 586/* button shapes */
412#ifdef MENUBAR 587#ifdef MENUBAR
413/* EXTPROTO */ 588/* INTPROTO */
414void 589void
415rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) 590rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type)
416{ 591{
417 switch (type) 592 switch (type)
418 { 593 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines