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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines