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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines