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.13 by pcg, Thu Apr 8 20:31:45 2004 UTC vs.
Revision 1.26 by root, Wed Feb 16 20:32:05 2005 UTC

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
73rxvt_vlog (const char *fmt, va_list arg_ptr) 186rxvt_vlog (const char *fmt, va_list arg_ptr)
74{ 187{
75 char msg[1024]; 188 char msg[1024];
76 189
80 (*GET_R->log_hook) (msg); 193 (*GET_R->log_hook) (msg);
81 else 194 else
82 write (STDOUT_FILENO, msg, strlen (msg)); 195 write (STDOUT_FILENO, msg, strlen (msg));
83} 196}
84 197
85/* EXTPROTO */ 198/* INTPROTO */
86void 199void
87rxvt_log (const char *fmt,...) 200rxvt_log (const char *fmt,...)
88{ 201{
89 va_list arg_ptr; 202 va_list arg_ptr;
90 203
94} 207}
95 208
96/* 209/*
97 * Print an error message 210 * Print an error message
98 */ 211 */
99/* EXTPROTO */ 212/* INTPROTO */
100void 213void
101rxvt_warn (const char *fmt,...) 214rxvt_warn (const char *fmt,...)
102{ 215{
103 va_list arg_ptr; 216 va_list arg_ptr;
104 217
107 va_start (arg_ptr, fmt); 220 va_start (arg_ptr, fmt);
108 rxvt_vlog (fmt, arg_ptr); 221 rxvt_vlog (fmt, arg_ptr);
109 va_end (arg_ptr); 222 va_end (arg_ptr);
110} 223}
111 224
112/* EXTPROTO */ 225/* INTPROTO */
113void 226void
114rxvt_fatal (const char *fmt,...) 227rxvt_fatal (const char *fmt,...)
115{ 228{
116 va_list arg_ptr; 229 va_list arg_ptr;
117 230
124 rxvt_exit_failure (); 237 rxvt_exit_failure ();
125} 238}
126 239
127class rxvt_failure_exception rxvt_failure_exception; 240class rxvt_failure_exception rxvt_failure_exception;
128 241
129/* EXTPROTO */ 242/* INTPROTO */
130void 243void
131rxvt_exit_failure () 244rxvt_exit_failure ()
132{ 245{
133 throw (rxvt_failure_exception); 246 throw (rxvt_failure_exception);
134} 247}
137 * check that the first characters of S1 match S2 250 * check that the first characters of S1 match S2
138 * 251 *
139 * No Match 252 * No Match
140 * return: 0 253 * return: 0
141 * Match 254 * Match
142 * return: STRLEN (S2) 255 * return: strlen (S2)
143 */ 256 */
144/* EXTPROTO */ 257/* INTPROTO */
145int 258int
146rxvt_Str_match (const char *s1, const char *s2) 259rxvt_Str_match (const char *s1, const char *s2)
147{ 260{
148 int n = STRLEN (s2); 261 int n = strlen (s2);
149 262
150 return ((STRNCMP (s1, s2, n) == 0) ? n : 0); 263 return ((strncmp (s1, s2, n) == 0) ? n : 0);
151} 264}
152 265
153/* EXTPROTO */ 266/* INTPROTO */
154const char * 267const char *
155rxvt_Str_skip_space (const char *str) 268rxvt_Str_skip_space (const char *str)
156{ 269{
157 if (str) 270 if (str)
158 while (*str && isspace (*str)) 271 while (*str && isspace (*str))
163 276
164/* 277/*
165 * remove leading/trailing space and strip-off leading/trailing quotes. 278 * remove leading/trailing space and strip-off leading/trailing quotes.
166 * in place. 279 * in place.
167 */ 280 */
168/* EXTPROTO */ 281/* INTPROTO */
169char * 282char *
170rxvt_Str_trim (char *str) 283rxvt_Str_trim (char *str)
171{ 284{
172 char *r, *s; 285 char *r, *s;
173 int n;
174 286
175 if (!str || !*str) /* shortcut */ 287 if (!str || !*str) /* shortcut */
176 return str; 288 return str;
177 289
178 /* skip leading spaces */ 290 /* skip leading spaces */
179 for (s = str; *s && isspace (*s); s++) ; 291 for (s = str; *s && isspace (*s); s++) ;
292
180 /* goto end of string */ 293 /* goto end of string */
181 for (n = 0, r = s; *r++; n++) ; 294 r = s + strlen (s) - 1;
182 r -= 2; 295
183 /* dump return */ 296 /* dump return and other trailing whitespace */
184 if (n > 0 && *r == '\n') 297 while (r > s && isspace (*r))
185 n--, r--; 298 r--;
186 /* backtrack along trailing spaces */ 299
187 for (; n > 0 && isspace (*r); r--, n--) ; 300#if 0
188 /* skip matching leading/trailing quotes */ 301 /* skip matching leading/trailing quotes */
189 if (*s == '"' && *r == '"' && n > 1) 302 if (*s == '"' && *r == '"' && n > 1)
190 { 303 {
191 s++; 304 s++;
192 n -= 2; 305 n -= 2;
193 } 306 }
307#endif
194 308
195 /* copy back over: forwards copy */ 309 memmove (str, s, r + 1 - s);
196 for (r = str; n; n--) 310 str[r + 1 - s] = 0;
197 *r++ = *s++;
198 *r = '\0';
199 311
200 return str; 312 return str;
201} 313}
202 314
203/* 315/*
212 * "M-x" prefixed strings, append "\r" if needed 324 * "M-x" prefixed strings, append "\r" if needed
213 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 325 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
214 * 326 *
215 * returns the converted string length 327 * returns the converted string length
216 */ 328 */
217/* EXTPROTO */ 329/* INTPROTO */
218int 330int
219rxvt_Str_escaped (char *str) 331rxvt_Str_escaped (char *str)
220{ 332{
221 char ch, *s, *d; 333 char ch, *s, *d;
222 int i, num, append = 0; 334 int i, num, append = 0;
286 return (d - str); 398 return (d - str);
287} 399}
288 400
289/* 401/*
290 * Split a comma-separated string into an array, stripping leading and 402 * Split a comma-separated string into an array, stripping leading and
291 * trailing spaces (and paired quotes) from each entry. Empty strings 403 * trailing spaces from each entry. Empty strings are properly returned
292 * are properly returned
293 * Caller should free each entry and array when done 404 * Caller should free each entry and array when done
294 */ 405 */
295/* EXTPROTO */ 406/* INTPROTO */
296char ** 407char **
297rxvt_splitcommastring (const char *cs) 408rxvt_splitcommastring (const char *cs)
298{ 409{
299 int l, n, p; 410 int l, n, p;
300 const char *s, *t; 411 const char *s, *t;
304 s = ""; 415 s = "";
305 416
306 for (n = 1, t = s; *t; t++) 417 for (n = 1, t = s; *t; t++)
307 if (*t == ',') 418 if (*t == ',')
308 n++; 419 n++;
420
309 ret = (char **)malloc ((n + 1) * sizeof (char *)); 421 ret = (char **)malloc ((n + 1) * sizeof (char *));
310 ret[n] = NULL; 422 ret[n] = NULL;
311 423
312 for (l = 0, t = s; l < n; l++) 424 for (l = 0, t = s; l < n; l++)
313 { 425 {
317 strncpy (ret[l], s, p); 429 strncpy (ret[l], s, p);
318 ret[l][p] = '\0'; 430 ret[l][p] = '\0';
319 rxvt_Str_trim (ret[l]); 431 rxvt_Str_trim (ret[l]);
320 s = ++t; 432 s = ++t;
321 } 433 }
434
322 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);
323} 445}
324 446
325/*----------------------------------------------------------------------* 447/*----------------------------------------------------------------------*
326 * file searching 448 * file searching
327 */ 449 */
342 int maxpath, len; 464 int maxpath, len;
343 const char *p, *path; 465 const char *p, *path;
344 char name[256]; 466 char name[256];
345 467
346 if (!access (file, R_OK)) /* found (plain name) in current directory */ 468 if (!access (file, R_OK)) /* found (plain name) in current directory */
347 return STRDUP (file); 469 return strdup (file);
348 470
349 /* semi-colon delimited */ 471 /* semi-colon delimited */
350 if ((p = STRCHR (file, ';'))) 472 if ((p = strchr (file, ';')))
351 len = (p - file); 473 len = (p - file);
352 else 474 else
353 len = STRLEN (file); 475 len = strlen (file);
354 476
355#ifdef DEBUG_SEARCH_PATH 477#ifdef DEBUG_SEARCH_PATH
356 getcwd (name, sizeof (name)); 478 getcwd (name, sizeof (name));
357 fprintf (stderr, "pwd: \"%s\"\n", name); 479 fprintf (stderr, "pwd: \"%s\"\n", name);
358 fprintf (stderr, "find: \"%.*s\"\n", len, file); 480 fprintf (stderr, "find: \"%.*s\"\n", len, file);
359#endif 481#endif
360 482
361 /* leave room for an extra '/' and trailing '\0' */ 483 /* leave room for an extra '/' and trailing '\0' */
362 maxpath = sizeof (name) - (len + (ext ? STRLEN (ext) : 0) + 2); 484 maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2);
363 if (maxpath <= 0) 485 if (maxpath <= 0)
364 return NULL; 486 return NULL;
365 487
366 /* check if we can find it now */ 488 /* check if we can find it now */
367 STRNCPY (name, file, len); 489 strncpy (name, file, len);
368 name[len] = '\0'; 490 name[len] = '\0';
369 491
370 if (!access (name, R_OK)) 492 if (!access (name, R_OK))
371 return STRDUP (name); 493 return strdup (name);
372 if (ext) 494 if (ext)
373 { 495 {
374 STRCAT (name, ext); 496 strcat (name, ext);
375 if (!access (name, R_OK)) 497 if (!access (name, R_OK))
376 return STRDUP (name); 498 return strdup (name);
377 } 499 }
378 for (path = pathlist; path != NULL && *path != '\0'; path = p) 500 for (path = pathlist; path != NULL && *path != '\0'; path = p)
379 { 501 {
380 int n; 502 int n;
381 503
382 /* colon delimited */ 504 /* colon delimited */
383 if ((p = STRCHR (path, ':')) == NULL) 505 if ((p = strchr (path, ':')) == NULL)
384 p = STRCHR (path, '\0'); 506 p = strchr (path, '\0');
385 507
386 n = (p - path); 508 n = (p - path);
387 if (*p != '\0') 509 if (*p != '\0')
388 p++; 510 p++;
389 511
390 if (n > 0 && n <= maxpath) 512 if (n > 0 && n <= maxpath)
391 { 513 {
392 STRNCPY (name, path, n); 514 strncpy (name, path, n);
393 if (name[n - 1] != '/') 515 if (name[n - 1] != '/')
394 name[n++] = '/'; 516 name[n++] = '/';
395 name[n] = '\0'; 517 name[n] = '\0';
396 STRNCAT (name, file, len); 518 strncat (name, file, len);
397 519
398 if (!access (name, R_OK)) 520 if (!access (name, R_OK))
399 return STRDUP (name); 521 return strdup (name);
400 if (ext) 522 if (ext)
401 { 523 {
402 STRCAT (name, ext); 524 strcat (name, ext);
403 if (!access (name, R_OK)) 525 if (!access (name, R_OK))
404 return STRDUP (name); 526 return strdup (name);
405 } 527 }
406 } 528 }
407 } 529 }
408 return NULL; 530 return NULL;
409} 531}
410 532
411/* EXTPROTO */ 533/* INTPROTO */
412char * 534char *
413rxvt_File_find (const char *file, const char *ext, const char *path) 535rxvt_File_find (const char *file, const char *ext, const char *path)
414{ 536{
415 char *f; 537 char *f;
416 538
439 561
440/* 562/*
441 * Draw top/left and bottom/right border shadows around windows 563 * Draw top/left and bottom/right border shadows around windows
442 */ 564 */
443#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 565#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
444/* EXTPROTO */ 566/* INTPROTO */
445void 567void
446rxvt_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)
447{ 569{
448 int shadow; 570 int shadow;
449 571
460} 582}
461#endif 583#endif
462 584
463/* button shapes */ 585/* button shapes */
464#ifdef MENUBAR 586#ifdef MENUBAR
465/* EXTPROTO */ 587/* INTPROTO */
466void 588void
467rxvt_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)
468{ 590{
469 switch (type) 591 switch (type)
470 { 592 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines