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.20 by root, Mon Dec 13 06:44:18 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 {
115 *p++ = *s++; len--; 118 *p++ = *s++;
116 }
117 else if (len > 0 119 else if (len >= 2
118 && s[0] >= 0xc2 && s[0] <= 0xdf 120 && s[0] >= 0xc2 && s[0] <= 0xdf
119 && (s[1] & 0xc0) == 0x80) 121 && (s[1] & 0xc0) == 0x80)
120 { 122 {
121 *p++ = ((s[0] & 0x1f) << 6) 123 *p++ = ((s[0] & 0x1f) << 6)
122 | (s[1] & 0x3f); 124 | (s[1] & 0x3f);
123 s += 2; len -= 2; 125 s += 2;
124 } 126 }
125 else if (len > 1 127 else if (len >= 3
126 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) 128 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf)
127 || (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)
128 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) 130 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f)
129 || (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)
130 ) 132 )
131 && (s[2] & 0xc0) == 0x80) 133 && (s[2] & 0xc0) == 0x80)
132 { 134 {
133 *p++ = ((s[0] & 0x0f) << 12) 135 *p++ = ((s[0] & 0x0f) << 12)
134 | ((s[1] & 0x3f) << 6) 136 | ((s[1] & 0x3f) << 6)
135 | (s[2] & 0x3f); 137 | (s[2] & 0x3f);
136 s += 3; len -= 3; 138 s += 3;
137 } 139 }
138 else if (len > 2 140 else if (len >= 4
139 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) 141 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf)
140 || (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)
141 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) 143 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f)
142 ) 144 )
143 && (s[2] & 0xc0) == 0x80 145 && (s[2] & 0xc0) == 0x80
145 { 147 {
146 *p++ = ((s[0] & 0x07) << 18) 148 *p++ = ((s[0] & 0x07) << 18)
147 | ((s[1] & 0x3f) << 12) 149 | ((s[1] & 0x3f) << 12)
148 | ((s[2] & 0x3f) << 6) 150 | ((s[2] & 0x3f) << 6)
149 | (s[3] & 0x3f); 151 | (s[3] & 0x3f);
150 s += 4; len -= 4; 152 s += 4;
151 } 153 }
152 else 154 else
153 { 155 {
154 *p++ = 0xfffd; 156 *p++ = 0xfffd;
155 s++; len--; 157 s++;
156 } 158 }
157 } 159 }
158 160
159 *p = 0; 161 *p = 0;
160 162
165rxvt_strdup (const char *str) 167rxvt_strdup (const char *str)
166{ 168{
167 return str ? strdup (str) : 0; 169 return str ? strdup (str) : 0;
168} 170}
169 171
170/* INTPROTO */
171char * 172char *
172rxvt_r_basename (const char *str) 173rxvt_r_basename (const char *str)
173{ 174{
174 char *base = strrchr (str, '/'); 175 char *base = strrchr (str, '/');
175 176
177} 178}
178 179
179/* 180/*
180 * Print an error message 181 * Print an error message
181 */ 182 */
182/* INTPROTO */
183void 183void
184rxvt_vlog (const char *fmt, va_list arg_ptr) 184rxvt_vlog (const char *fmt, va_list arg_ptr)
185{ 185{
186 char msg[1024]; 186 char msg[1024];
187 187
191 (*GET_R->log_hook) (msg); 191 (*GET_R->log_hook) (msg);
192 else 192 else
193 write (STDOUT_FILENO, msg, strlen (msg)); 193 write (STDOUT_FILENO, msg, strlen (msg));
194} 194}
195 195
196/* INTPROTO */
197void 196void
198rxvt_log (const char *fmt,...) 197rxvt_log (const char *fmt,...)
199{ 198{
200 va_list arg_ptr; 199 va_list arg_ptr;
201 200
205} 204}
206 205
207/* 206/*
208 * Print an error message 207 * Print an error message
209 */ 208 */
210/* INTPROTO */
211void 209void
212rxvt_warn (const char *fmt,...) 210rxvt_warn (const char *fmt,...)
213{ 211{
214 va_list arg_ptr; 212 va_list arg_ptr;
215 213
218 va_start (arg_ptr, fmt); 216 va_start (arg_ptr, fmt);
219 rxvt_vlog (fmt, arg_ptr); 217 rxvt_vlog (fmt, arg_ptr);
220 va_end (arg_ptr); 218 va_end (arg_ptr);
221} 219}
222 220
223/* INTPROTO */
224void 221void
225rxvt_fatal (const char *fmt,...) 222rxvt_fatal (const char *fmt,...)
226{ 223{
227 va_list arg_ptr; 224 va_list arg_ptr;
228 225
235 rxvt_exit_failure (); 232 rxvt_exit_failure ();
236} 233}
237 234
238class rxvt_failure_exception rxvt_failure_exception; 235class rxvt_failure_exception rxvt_failure_exception;
239 236
240/* INTPROTO */
241void 237void
242rxvt_exit_failure () 238rxvt_exit_failure ()
243{ 239{
244 throw (rxvt_failure_exception); 240 throw (rxvt_failure_exception);
245} 241}
250 * No Match 246 * No Match
251 * return: 0 247 * return: 0
252 * Match 248 * Match
253 * return: strlen (S2) 249 * return: strlen (S2)
254 */ 250 */
255/* INTPROTO */
256int 251int
257rxvt_Str_match (const char *s1, const char *s2) 252rxvt_Str_match (const char *s1, const char *s2)
258{ 253{
259 int n = strlen (s2); 254 int n = strlen (s2);
260 255
261 return ((strncmp (s1, s2, n) == 0) ? n : 0); 256 return ((strncmp (s1, s2, n) == 0) ? n : 0);
262} 257}
263 258
264/* INTPROTO */
265const char * 259const char *
266rxvt_Str_skip_space (const char *str) 260rxvt_Str_skip_space (const char *str)
267{ 261{
268 if (str) 262 if (str)
269 while (*str && isspace (*str)) 263 while (*str && isspace (*str))
274 268
275/* 269/*
276 * remove leading/trailing space and strip-off leading/trailing quotes. 270 * remove leading/trailing space and strip-off leading/trailing quotes.
277 * in place. 271 * in place.
278 */ 272 */
279/* INTPROTO */
280char * 273char *
281rxvt_Str_trim (char *str) 274rxvt_Str_trim (char *str)
282{ 275{
283 char *r, *s; 276 char *r, *s;
284 int n;
285 277
286 if (!str || !*str) /* shortcut */ 278 if (!str || !*str) /* shortcut */
287 return str; 279 return str;
288 280
289 /* skip leading spaces */ 281 /* skip leading spaces */
290 for (s = str; *s && isspace (*s); s++) ; 282 for (s = str; *s && isspace (*s); s++) ;
283
291 /* goto end of string */ 284 /* goto end of string */
292 for (n = 0, r = s; *r++; n++) ; 285 r = s + strlen (s) - 1;
293 r -= 2; 286
294 /* dump return */ 287 /* dump return and other trailing whitespace */
295 if (n > 0 && *r == '\n') 288 while (r > s && isspace (*r))
296 n--, r--; 289 r--;
297 /* backtrack along trailing spaces */ 290
298 for (; n > 0 && isspace (*r); r--, n--) ; 291#if 0
299 /* skip matching leading/trailing quotes */ 292 /* skip matching leading/trailing quotes */
300 if (*s == '"' && *r == '"' && n > 1) 293 if (*s == '"' && *r == '"' && n > 1)
301 { 294 {
302 s++; 295 s++;
303 n -= 2; 296 n -= 2;
304 } 297 }
298#endif
305 299
306 /* copy back over: forwards copy */ 300 memmove (str, s, r + 1 - s);
307 for (r = str; n; n--) 301 str[r + 1 - s] = 0;
308 *r++ = *s++;
309 *r = '\0';
310 302
311 return str; 303 return str;
312} 304}
313 305
314/* 306/*
323 * "M-x" prefixed strings, append "\r" if needed 315 * "M-x" prefixed strings, append "\r" if needed
324 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 316 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
325 * 317 *
326 * returns the converted string length 318 * returns the converted string length
327 */ 319 */
328/* INTPROTO */
329int 320int
330rxvt_Str_escaped (char *str) 321rxvt_Str_escaped (char *str)
331{ 322{
332 char ch, *s, *d; 323 char ch, *s, *d;
333 int i, num, append = 0; 324 int i, num, append = 0;
397 return (d - str); 388 return (d - str);
398} 389}
399 390
400/* 391/*
401 * Split a comma-separated string into an array, stripping leading and 392 * Split a comma-separated string into an array, stripping leading and
402 * trailing spaces (and paired quotes) from each entry. Empty strings 393 * trailing spaces from each entry. Empty strings are properly returned
403 * are properly returned
404 * Caller should free each entry and array when done 394 * Caller should free each entry and array when done
405 */ 395 */
406/* INTPROTO */
407char ** 396char **
408rxvt_splitcommastring (const char *cs) 397rxvt_splitcommastring (const char *cs)
409{ 398{
410 int l, n, p; 399 int l, n, p;
411 const char *s, *t; 400 const char *s, *t;
415 s = ""; 404 s = "";
416 405
417 for (n = 1, t = s; *t; t++) 406 for (n = 1, t = s; *t; t++)
418 if (*t == ',') 407 if (*t == ',')
419 n++; 408 n++;
409
420 ret = (char **)malloc ((n + 1) * sizeof (char *)); 410 ret = (char **)malloc ((n + 1) * sizeof (char *));
421 ret[n] = NULL; 411 ret[n] = NULL;
422 412
423 for (l = 0, t = s; l < n; l++) 413 for (l = 0, t = s; l < n; l++)
424 { 414 {
428 strncpy (ret[l], s, p); 418 strncpy (ret[l], s, p);
429 ret[l][p] = '\0'; 419 ret[l][p] = '\0';
430 rxvt_Str_trim (ret[l]); 420 rxvt_Str_trim (ret[l]);
431 s = ++t; 421 s = ++t;
432 } 422 }
423
433 return ret; 424 return ret;
434} 425}
435 426
436void 427void
437rxvt_freecommastring (char **cs) 428rxvt_freecommastring (char **cs)
453 * search for FILE in the current working directory, and within the 444 * search for FILE in the current working directory, and within the
454 * colon-delimited PATHLIST, adding the file extension EXT if required. 445 * colon-delimited PATHLIST, adding the file extension EXT if required.
455 * 446 *
456 * FILE is either semi-colon or zero terminated 447 * FILE is either semi-colon or zero terminated
457 */ 448 */
458/* INTPROTO */
459char * 449char *
460rxvt_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)
461{ 451{
462 int maxpath, len; 452 int maxpath, len;
463 const char *p, *path; 453 const char *p, *path;
526 } 516 }
527 } 517 }
528 return NULL; 518 return NULL;
529} 519}
530 520
531/* INTPROTO */
532char * 521char *
533rxvt_File_find (const char *file, const char *ext, const char *path) 522rxvt_File_find (const char *file, const char *ext, const char *path)
534{ 523{
535 char *f; 524 char *f;
536 525
559 548
560/* 549/*
561 * Draw top/left and bottom/right border shadows around windows 550 * Draw top/left and bottom/right border shadows around windows
562 */ 551 */
563#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 552#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
564/* INTPROTO */
565void 553void
566rxvt_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)
567{ 555{
568 int shadow; 556 int shadow;
569 557
580} 568}
581#endif 569#endif
582 570
583/* button shapes */ 571/* button shapes */
584#ifdef MENUBAR 572#ifdef MENUBAR
585/* INTPROTO */
586void 573void
587rxvt_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)
588{ 575{
589 switch (type) 576 switch (type)
590 { 577 {
621#endif 608#endif
622 609
623 } 610 }
624} 611}
625#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
626/*----------------------- end-of-file (C source) -----------------------*/ 633/*----------------------- end-of-file (C source) -----------------------*/
634

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines