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.19 by root, Sun Dec 12 06:30:25 2004 UTC vs.
Revision 1.28 by root, Fri Aug 5 16:42:44 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
30char * 29char *
31rxvt_wcstombs (const wchar_t *str, int len) 30rxvt_wcstombs (const wchar_t *str, int len)
32{ 31{
33 if (len < 0) len = wcslen (str); 32 if (len < 0) len = wcslen (str);
36 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); 35 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
37 36
38 char *dst = r; 37 char *dst = r;
39 while (len--) 38 while (len--)
40 { 39 {
41 int l = wcrtomb (dst, *str++, mbs); 40 ssize_t l = wcrtomb (dst, *str++, mbs);
42 if (l < 0) 41 if (l < 0)
43 *dst++ = '?'; 42 *dst++ = '?';
44 else 43 else
45 dst += l; 44 dst += l;
46 } 45 }
47 46
48 *dst++ = 0; 47 *dst++ = 0;
49 48
50 return r; 49 return (char *)rxvt_realloc (r, dst - r);
51} 50}
52 51
53wchar_t * 52wchar_t *
54rxvt_mbstowcs (const char *str, int len) 53rxvt_mbstowcs (const char *str, int len)
55{ 54{
56 if (len < 0) len = strlen (str); 55 if (len < 0) len = strlen (str);
57 56
58 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); 57 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t));
59 58
60 if (mbstowcs (r, str, len + 1) < 0) 59 if ((ssize_t)mbstowcs (r, str, len + 1) < 0)
61 *r = 0; 60 *r = 0;
62 61
63 return r; 62 return r;
64} 63}
65 64
71 char *r = (char *)rxvt_malloc (len * 4 + 1); 70 char *r = (char *)rxvt_malloc (len * 4 + 1);
72 char *p = r; 71 char *p = r;
73 72
74 while (len--) 73 while (len--)
75 { 74 {
76 unicode_t w = *str++; 75 unicode_t w = *str++ & UNICODE_MASK;
77 76
78 if (w < 0x000080) 77 if (w < 0x000080)
79 *p++ = w; 78 *p++ = w;
80 else if (w < 0x000800) 79 else if (w < 0x000800)
81 *p++ = 0xc0 | ( w >> 6), 80 *p++ = 0xc0 | ( w >> 6),
91 *p++ = 0x80 | ( w & 0x3f); 90 *p++ = 0x80 | ( w & 0x3f);
92 else 91 else
93 *p++ = '?'; 92 *p++ = '?';
94 } 93 }
95 94
96 *p = 0; 95 *p++ = 0;
97 96
98 return r; 97 return (char *)rxvt_realloc (r, p - r);
99} 98}
100 99
101wchar_t * 100wchar_t *
102rxvt_utf8towcs (const char *str, int len) 101rxvt_utf8towcs (const char *str, int len)
103{ 102{
104 if (len < 0) len = strlen (str); 103 if (len < 0) len = strlen (str);
105 104
106 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); 105 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)),
107 wchar_t *p = r; 106 *p = r;
108 107
109 unsigned char *s = (unsigned char *)str; 108 unsigned char *s = (unsigned char *)str,
109 *e = s + len;
110 110
111 while (len--) 111 for (;;)
112 { 112 {
113 len = e - s;
114
115 if (len == 0)
116 break;
113 if (s[0] < 0x80) 117 else if (s[0] < 0x80)
114 *p++ = *s++; 118 *p++ = *s++;
115 else if (len > 0 119 else if (len >= 2
116 && s[0] >= 0xc2 && s[0] <= 0xdf 120 && s[0] >= 0xc2 && s[0] <= 0xdf
117 && (s[1] & 0xc0) == 0x80) 121 && (s[1] & 0xc0) == 0x80)
118 { 122 {
119 *p++ = ((s[0] & 0x1f) << 6) 123 *p++ = ((s[0] & 0x1f) << 6)
120 | (s[1] & 0x3f); 124 | (s[1] & 0x3f);
121 s += 2; 125 s += 2;
122 } 126 }
123 else if (len > 1 127 else if (len >= 3
124 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) 128 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf)
125 || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf) 129 || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf)
126 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) 130 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f)
127 || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf) 131 || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf)
128 ) 132 )
131 *p++ = ((s[0] & 0x0f) << 12) 135 *p++ = ((s[0] & 0x0f) << 12)
132 | ((s[1] & 0x3f) << 6) 136 | ((s[1] & 0x3f) << 6)
133 | (s[2] & 0x3f); 137 | (s[2] & 0x3f);
134 s += 3; 138 s += 3;
135 } 139 }
136 else if (len > 2 140 else if (len >= 4
137 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) 141 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf)
138 || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf) 142 || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf)
139 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) 143 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f)
140 ) 144 )
141 && (s[2] & 0xc0) == 0x80 145 && (s[2] & 0xc0) == 0x80
163rxvt_strdup (const char *str) 167rxvt_strdup (const char *str)
164{ 168{
165 return str ? strdup (str) : 0; 169 return str ? strdup (str) : 0;
166} 170}
167 171
168/* INTPROTO */
169char * 172char *
170rxvt_r_basename (const char *str) 173rxvt_r_basename (const char *str)
171{ 174{
172 char *base = strrchr (str, '/'); 175 char *base = strrchr (str, '/');
173 176
175} 178}
176 179
177/* 180/*
178 * Print an error message 181 * Print an error message
179 */ 182 */
180/* INTPROTO */
181void 183void
182rxvt_vlog (const char *fmt, va_list arg_ptr) 184rxvt_vlog (const char *fmt, va_list arg_ptr)
183{ 185{
184 char msg[1024]; 186 char msg[1024];
185 187
189 (*GET_R->log_hook) (msg); 191 (*GET_R->log_hook) (msg);
190 else 192 else
191 write (STDOUT_FILENO, msg, strlen (msg)); 193 write (STDOUT_FILENO, msg, strlen (msg));
192} 194}
193 195
194/* INTPROTO */
195void 196void
196rxvt_log (const char *fmt,...) 197rxvt_log (const char *fmt,...)
197{ 198{
198 va_list arg_ptr; 199 va_list arg_ptr;
199 200
203} 204}
204 205
205/* 206/*
206 * Print an error message 207 * Print an error message
207 */ 208 */
208/* INTPROTO */
209void 209void
210rxvt_warn (const char *fmt,...) 210rxvt_warn (const char *fmt,...)
211{ 211{
212 va_list arg_ptr; 212 va_list arg_ptr;
213 213
216 va_start (arg_ptr, fmt); 216 va_start (arg_ptr, fmt);
217 rxvt_vlog (fmt, arg_ptr); 217 rxvt_vlog (fmt, arg_ptr);
218 va_end (arg_ptr); 218 va_end (arg_ptr);
219} 219}
220 220
221/* INTPROTO */
222void 221void
223rxvt_fatal (const char *fmt,...) 222rxvt_fatal (const char *fmt,...)
224{ 223{
225 va_list arg_ptr; 224 va_list arg_ptr;
226 225
233 rxvt_exit_failure (); 232 rxvt_exit_failure ();
234} 233}
235 234
236class rxvt_failure_exception rxvt_failure_exception; 235class rxvt_failure_exception rxvt_failure_exception;
237 236
238/* INTPROTO */
239void 237void
240rxvt_exit_failure () 238rxvt_exit_failure ()
241{ 239{
242 throw (rxvt_failure_exception); 240 throw (rxvt_failure_exception);
243} 241}
248 * No Match 246 * No Match
249 * return: 0 247 * return: 0
250 * Match 248 * Match
251 * return: strlen (S2) 249 * return: strlen (S2)
252 */ 250 */
253/* INTPROTO */
254int 251int
255rxvt_Str_match (const char *s1, const char *s2) 252rxvt_Str_match (const char *s1, const char *s2)
256{ 253{
257 int n = strlen (s2); 254 int n = strlen (s2);
258 255
259 return ((strncmp (s1, s2, n) == 0) ? n : 0); 256 return ((strncmp (s1, s2, n) == 0) ? n : 0);
260} 257}
261 258
262/* INTPROTO */
263const char * 259const char *
264rxvt_Str_skip_space (const char *str) 260rxvt_Str_skip_space (const char *str)
265{ 261{
266 if (str) 262 if (str)
267 while (*str && isspace (*str)) 263 while (*str && isspace (*str))
272 268
273/* 269/*
274 * remove leading/trailing space and strip-off leading/trailing quotes. 270 * remove leading/trailing space and strip-off leading/trailing quotes.
275 * in place. 271 * in place.
276 */ 272 */
277/* INTPROTO */
278char * 273char *
279rxvt_Str_trim (char *str) 274rxvt_Str_trim (char *str)
280{ 275{
281 char *r, *s; 276 char *r, *s;
282 int n;
283 277
284 if (!str || !*str) /* shortcut */ 278 if (!str || !*str) /* shortcut */
285 return str; 279 return str;
286 280
287 /* skip leading spaces */ 281 /* skip leading spaces */
288 for (s = str; *s && isspace (*s); s++) ; 282 for (s = str; *s && isspace (*s); s++) ;
283
289 /* goto end of string */ 284 /* goto end of string */
290 for (n = 0, r = s; *r++; n++) ; 285 r = s + strlen (s) - 1;
291 r -= 2; 286
292 /* dump return */ 287 /* dump return and other trailing whitespace */
293 if (n > 0 && *r == '\n') 288 while (r > s && isspace (*r))
294 n--, r--; 289 r--;
295 /* backtrack along trailing spaces */ 290
296 for (; n > 0 && isspace (*r); r--, n--) ; 291#if 0
297 /* skip matching leading/trailing quotes */ 292 /* skip matching leading/trailing quotes */
298 if (*s == '"' && *r == '"' && n > 1) 293 if (*s == '"' && *r == '"' && n > 1)
299 { 294 {
300 s++; 295 s++;
301 n -= 2; 296 n -= 2;
302 } 297 }
298#endif
303 299
304 /* copy back over: forwards copy */ 300 memmove (str, s, r + 1 - s);
305 for (r = str; n; n--) 301 str[r + 1 - s] = 0;
306 *r++ = *s++;
307 *r = '\0';
308 302
309 return str; 303 return str;
310} 304}
311 305
312/* 306/*
321 * "M-x" prefixed strings, append "\r" if needed 315 * "M-x" prefixed strings, append "\r" if needed
322 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 316 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
323 * 317 *
324 * returns the converted string length 318 * returns the converted string length
325 */ 319 */
326/* INTPROTO */
327int 320int
328rxvt_Str_escaped (char *str) 321rxvt_Str_escaped (char *str)
329{ 322{
330 char ch, *s, *d; 323 char ch, *s, *d;
331 int i, num, append = 0; 324 int i, num, append = 0;
395 return (d - str); 388 return (d - str);
396} 389}
397 390
398/* 391/*
399 * Split a comma-separated string into an array, stripping leading and 392 * Split a comma-separated string into an array, stripping leading and
400 * trailing spaces (and paired quotes) from each entry. Empty strings 393 * trailing spaces from each entry. Empty strings are properly returned
401 * are properly returned
402 * Caller should free each entry and array when done 394 * Caller should free each entry and array when done
403 */ 395 */
404/* INTPROTO */
405char ** 396char **
406rxvt_splitcommastring (const char *cs) 397rxvt_splitcommastring (const char *cs)
407{ 398{
408 int l, n, p; 399 int l, n, p;
409 const char *s, *t; 400 const char *s, *t;
413 s = ""; 404 s = "";
414 405
415 for (n = 1, t = s; *t; t++) 406 for (n = 1, t = s; *t; t++)
416 if (*t == ',') 407 if (*t == ',')
417 n++; 408 n++;
409
418 ret = (char **)malloc ((n + 1) * sizeof (char *)); 410 ret = (char **)malloc ((n + 1) * sizeof (char *));
419 ret[n] = NULL; 411 ret[n] = NULL;
420 412
421 for (l = 0, t = s; l < n; l++) 413 for (l = 0, t = s; l < n; l++)
422 { 414 {
426 strncpy (ret[l], s, p); 418 strncpy (ret[l], s, p);
427 ret[l][p] = '\0'; 419 ret[l][p] = '\0';
428 rxvt_Str_trim (ret[l]); 420 rxvt_Str_trim (ret[l]);
429 s = ++t; 421 s = ++t;
430 } 422 }
423
431 return ret; 424 return ret;
432} 425}
433 426
434void 427void
435rxvt_freecommastring (char **cs) 428rxvt_freecommastring (char **cs)
451 * search for FILE in the current working directory, and within the 444 * search for FILE in the current working directory, and within the
452 * colon-delimited PATHLIST, adding the file extension EXT if required. 445 * colon-delimited PATHLIST, adding the file extension EXT if required.
453 * 446 *
454 * FILE is either semi-colon or zero terminated 447 * FILE is either semi-colon or zero terminated
455 */ 448 */
456/* INTPROTO */
457char * 449char *
458rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) 450rxvt_File_search_path (const char *pathlist, const char *file, const char *ext)
459{ 451{
460 int maxpath, len; 452 int maxpath, len;
461 const char *p, *path; 453 const char *p, *path;
524 } 516 }
525 } 517 }
526 return NULL; 518 return NULL;
527} 519}
528 520
529/* INTPROTO */
530char * 521char *
531rxvt_File_find (const char *file, const char *ext, const char *path) 522rxvt_File_find (const char *file, const char *ext, const char *path)
532{ 523{
533 char *f; 524 char *f;
534 525
557 548
558/* 549/*
559 * Draw top/left and bottom/right border shadows around windows 550 * Draw top/left and bottom/right border shadows around windows
560 */ 551 */
561#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 552#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
562/* INTPROTO */
563void 553void
564rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) 554rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h)
565{ 555{
566 int shadow; 556 int shadow;
567 557
578} 568}
579#endif 569#endif
580 570
581/* button shapes */ 571/* button shapes */
582#ifdef MENUBAR 572#ifdef MENUBAR
583/* INTPROTO */
584void 573void
585rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) 574rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type)
586{ 575{
587 switch (type) 576 switch (type)
588 { 577 {
619#endif 608#endif
620 609
621 } 610 }
622} 611}
623#endif 612#endif
613
614// should nto be use din interactive programs, for obvious reasons
615void rxvt_usleep (int usecs)
616{
617#if HAVE_NANOSLEEP
618 struct timespec ts;
619
620 ts.tv_sec = 0;
621 ts.tv_nsec = usecs * 1000;
622 nanosleep (&ts, NULL);
623#else
624 /* use select for timing */
625 struct timeval tv;
626
627 tv.tv_sec = 0;
628 tv.tv_usec = usecs;
629 select (0, NULL, NULL, NULL, &tv);
630#endif
631}
632
624/*----------------------- end-of-file (C source) -----------------------*/ 633/*----------------------- end-of-file (C source) -----------------------*/
634

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines