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.29 by root, Thu Aug 11 12:31:15 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 */
60char * 172char *
61rxvt_r_basename (const char *str) 173rxvt_r_basename (const char *str)
62{ 174{
63 char *base = STRRCHR (str, '/'); 175 char *base = strrchr (str, '/');
64 176
65 return (char *) (base ? base + 1 : str); 177 return (char *) (base ? base + 1 : str);
66} 178}
67 179
68/* 180/*
69 * Print an error message 181 * Print an error message
70 */ 182 */
71/* EXTPROTO */
72void 183void
184rxvt_vlog (const char *fmt, va_list arg_ptr)
185{
186 char msg[1024];
187
188 vsnprintf (msg, sizeof msg, fmt, arg_ptr);
189
190 if (GET_R && GET_R->log_hook)
191 (*GET_R->log_hook) (msg);
192 else
193 write (STDOUT_FILENO, msg, strlen (msg));
194}
195
196void
73rxvt_print_error (const char *fmt,...) 197rxvt_log (const char *fmt,...)
74{ 198{
75 va_list arg_ptr; 199 va_list arg_ptr;
76 200
77 va_start (arg_ptr, fmt); 201 va_start (arg_ptr, fmt);
78 fprintf (stderr, RESNAME ": "); 202 rxvt_vlog (fmt, arg_ptr);
79 vfprintf (stderr, fmt, arg_ptr);
80 fprintf (stderr, "\n");
81 va_end (arg_ptr); 203 va_end (arg_ptr);
204}
205
206/*
207 * Print an error message
208 */
209void
210rxvt_warn (const char *fmt,...)
211{
212 va_list arg_ptr;
213
214 rxvt_log ("%s: ", RESNAME);
215
216 va_start (arg_ptr, fmt);
217 rxvt_vlog (fmt, arg_ptr);
218 va_end (arg_ptr);
219}
220
221void
222rxvt_fatal (const char *fmt,...)
223{
224 va_list arg_ptr;
225
226 rxvt_log ("%s: ", RESNAME);
227
228 va_start (arg_ptr, fmt);
229 rxvt_vlog (fmt, arg_ptr);
230 va_end (arg_ptr);
231
232 rxvt_exit_failure ();
233}
234
235class rxvt_failure_exception rxvt_failure_exception;
236
237void
238rxvt_exit_failure ()
239{
240 throw (rxvt_failure_exception);
82} 241}
83 242
84/* 243/*
85 * check that the first characters of S1 match S2 244 * check that the first characters of S1 match S2
86 * 245 *
87 * No Match 246 * No Match
88 * return: 0 247 * return: 0
89 * Match 248 * Match
90 * return: STRLEN (S2) 249 * return: strlen (S2)
91 */ 250 */
92/* EXTPROTO */
93int 251int
94rxvt_Str_match (const char *s1, const char *s2) 252rxvt_Str_match (const char *s1, const char *s2)
95{ 253{
96 int n = STRLEN (s2); 254 int n = strlen (s2);
97 255
98 return ((STRNCMP (s1, s2, n) == 0) ? n : 0); 256 return ((strncmp (s1, s2, n) == 0) ? n : 0);
99} 257}
100 258
101/* EXTPROTO */
102const char * 259const char *
103rxvt_Str_skip_space (const char *str) 260rxvt_Str_skip_space (const char *str)
104{ 261{
105 if (str) 262 if (str)
106 while (*str && isspace (*str)) 263 while (*str && isspace (*str))
111 268
112/* 269/*
113 * remove leading/trailing space and strip-off leading/trailing quotes. 270 * remove leading/trailing space and strip-off leading/trailing quotes.
114 * in place. 271 * in place.
115 */ 272 */
116/* EXTPROTO */
117char * 273char *
118rxvt_Str_trim (char *str) 274rxvt_Str_trim (char *str)
119{ 275{
120 char *r, *s; 276 char *r, *s;
121 int n;
122 277
123 if (!str || !*str) /* shortcut */ 278 if (!str || !*str) /* shortcut */
124 return str; 279 return str;
125 280
126 /* skip leading spaces */ 281 /* skip leading spaces */
127 for (s = str; *s && isspace (*s); s++) ; 282 for (s = str; *s && isspace (*s); s++) ;
283
128 /* goto end of string */ 284 /* goto end of string */
129 for (n = 0, r = s; *r++; n++) ; 285 r = s + strlen (s) - 1;
130 r -= 2; 286
131 /* dump return */ 287 /* dump return and other trailing whitespace */
132 if (n > 0 && *r == '\n') 288 while (r > s && isspace (*r))
133 n--, r--; 289 r--;
134 /* backtrack along trailing spaces */ 290
135 for (; n > 0 && isspace (*r); r--, n--) ; 291#if 0
136 /* skip matching leading/trailing quotes */ 292 /* skip matching leading/trailing quotes */
137 if (*s == '"' && *r == '"' && n > 1) 293 if (*s == '"' && *r == '"' && n > 1)
138 { 294 {
139 s++; 295 s++;
140 n -= 2; 296 n -= 2;
141 } 297 }
298#endif
142 299
143 /* copy back over: forwards copy */ 300 memmove (str, s, r + 1 - s);
144 for (r = str; n; n--) 301 str[r + 1 - s] = 0;
145 *r++ = *s++;
146 *r = '\0';
147 302
148 return str; 303 return str;
149} 304}
150 305
151/* 306/*
160 * "M-x" prefixed strings, append "\r" if needed 315 * "M-x" prefixed strings, append "\r" if needed
161 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 316 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
162 * 317 *
163 * returns the converted string length 318 * returns the converted string length
164 */ 319 */
165/* EXTPROTO */
166int 320int
167rxvt_Str_escaped (char *str) 321rxvt_Str_escaped (char *str)
168{ 322{
169 char ch, *s, *d; 323 char ch, *s, *d;
170 int i, num, append = 0; 324 int i, num, append = 0;
234 return (d - str); 388 return (d - str);
235} 389}
236 390
237/* 391/*
238 * Split a comma-separated string into an array, stripping leading and 392 * Split a comma-separated string into an array, stripping leading and
239 * trailing spaces (and paired quotes) from each entry. Empty strings 393 * trailing spaces from each entry. Empty strings are properly returned
240 * are properly returned
241 * Caller should free each entry and array when done 394 * Caller should free each entry and array when done
242 */ 395 */
243/* EXTPROTO */
244char ** 396char **
245rxvt_splitcommastring (const char *cs) 397rxvt_splitcommastring (const char *cs)
246{ 398{
247 int l, n, p; 399 int l, n, p;
248 const char *s, *t; 400 const char *s, *t;
252 s = ""; 404 s = "";
253 405
254 for (n = 1, t = s; *t; t++) 406 for (n = 1, t = s; *t; t++)
255 if (*t == ',') 407 if (*t == ',')
256 n++; 408 n++;
409
257 ret = (char **)malloc ((n + 1) * sizeof (char *)); 410 ret = (char **)malloc ((n + 1) * sizeof (char *));
258 ret[n] = NULL; 411 ret[n] = NULL;
259 412
260 for (l = 0, t = s; l < n; l++) 413 for (l = 0, t = s; l < n; l++)
261 { 414 {
265 strncpy (ret[l], s, p); 418 strncpy (ret[l], s, p);
266 ret[l][p] = '\0'; 419 ret[l][p] = '\0';
267 rxvt_Str_trim (ret[l]); 420 rxvt_Str_trim (ret[l]);
268 s = ++t; 421 s = ++t;
269 } 422 }
423
270 return ret; 424 return ret;
425}
426
427void
428rxvt_freecommastring (char **cs)
429{
430 for (int i = 0; cs[i]; ++i)
431 free (cs[i]);
432
433 free (cs);
271} 434}
272 435
273/*----------------------------------------------------------------------* 436/*----------------------------------------------------------------------*
274 * file searching 437 * file searching
275 */ 438 */
281 * search for FILE in the current working directory, and within the 444 * search for FILE in the current working directory, and within the
282 * colon-delimited PATHLIST, adding the file extension EXT if required. 445 * colon-delimited PATHLIST, adding the file extension EXT if required.
283 * 446 *
284 * FILE is either semi-colon or zero terminated 447 * FILE is either semi-colon or zero terminated
285 */ 448 */
286/* INTPROTO */
287char * 449char *
288rxvt_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)
289{ 451{
290 int maxpath, len; 452 int maxpath, len;
291 const char *p, *path; 453 const char *p, *path;
292 char name[256]; 454 char name[256];
293 455
294 if (!access (file, R_OK)) /* found (plain name) in current directory */ 456 if (!access (file, R_OK)) /* found (plain name) in current directory */
295 return STRDUP (file); 457 return strdup (file);
296 458
297 /* semi-colon delimited */ 459 /* semi-colon delimited */
298 if ((p = STRCHR (file, ';'))) 460 if ((p = strchr (file, ';')))
299 len = (p - file); 461 len = (p - file);
300 else 462 else
301 len = STRLEN (file); 463 len = strlen (file);
302 464
303#ifdef DEBUG_SEARCH_PATH 465#ifdef DEBUG_SEARCH_PATH
304 getcwd (name, sizeof (name)); 466 getcwd (name, sizeof (name));
305 fprintf (stderr, "pwd: \"%s\"\n", name); 467 fprintf (stderr, "pwd: \"%s\"\n", name);
306 fprintf (stderr, "find: \"%.*s\"\n", len, file); 468 fprintf (stderr, "find: \"%.*s\"\n", len, file);
307#endif 469#endif
308 470
309 /* leave room for an extra '/' and trailing '\0' */ 471 /* leave room for an extra '/' and trailing '\0' */
310 maxpath = sizeof (name) - (len + (ext ? STRLEN (ext) : 0) + 2); 472 maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2);
311 if (maxpath <= 0) 473 if (maxpath <= 0)
312 return NULL; 474 return NULL;
313 475
314 /* check if we can find it now */ 476 /* check if we can find it now */
315 STRNCPY (name, file, len); 477 strncpy (name, file, len);
316 name[len] = '\0'; 478 name[len] = '\0';
317 479
318 if (!access (name, R_OK)) 480 if (!access (name, R_OK))
319 return STRDUP (name); 481 return strdup (name);
320 if (ext) 482 if (ext)
321 { 483 {
322 STRCAT (name, ext); 484 strcat (name, ext);
323 if (!access (name, R_OK)) 485 if (!access (name, R_OK))
324 return STRDUP (name); 486 return strdup (name);
325 } 487 }
326 for (path = pathlist; path != NULL && *path != '\0'; path = p) 488 for (path = pathlist; path != NULL && *path != '\0'; path = p)
327 { 489 {
328 int n; 490 int n;
329 491
330 /* colon delimited */ 492 /* colon delimited */
331 if ((p = STRCHR (path, ':')) == NULL) 493 if ((p = strchr (path, ':')) == NULL)
332 p = STRCHR (path, '\0'); 494 p = strchr (path, '\0');
333 495
334 n = (p - path); 496 n = (p - path);
335 if (*p != '\0') 497 if (*p != '\0')
336 p++; 498 p++;
337 499
338 if (n > 0 && n <= maxpath) 500 if (n > 0 && n <= maxpath)
339 { 501 {
340 STRNCPY (name, path, n); 502 strncpy (name, path, n);
341 if (name[n - 1] != '/') 503 if (name[n - 1] != '/')
342 name[n++] = '/'; 504 name[n++] = '/';
343 name[n] = '\0'; 505 name[n] = '\0';
344 STRNCAT (name, file, len); 506 strncat (name, file, len);
345 507
346 if (!access (name, R_OK)) 508 if (!access (name, R_OK))
347 return STRDUP (name); 509 return strdup (name);
348 if (ext) 510 if (ext)
349 { 511 {
350 STRCAT (name, ext); 512 strcat (name, ext);
351 if (!access (name, R_OK)) 513 if (!access (name, R_OK))
352 return STRDUP (name); 514 return strdup (name);
353 } 515 }
354 } 516 }
355 } 517 }
356 return NULL; 518 return NULL;
357} 519}
358 520
359/* EXTPROTO */
360char * 521char *
361rxvt_File_find (const char *file, const char *ext, const char *path) 522rxvt_File_find (const char *file, const char *ext, const char *path)
362{ 523{
363 char *f; 524 char *f;
364 525
387 548
388/* 549/*
389 * Draw top/left and bottom/right border shadows around windows 550 * Draw top/left and bottom/right border shadows around windows
390 */ 551 */
391#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 552#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
392/* EXTPROTO */
393void 553void
394rxvt_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)
395{ 555{
396 int shadow; 556 int shadow;
397 557
408} 568}
409#endif 569#endif
410 570
411/* button shapes */ 571/* button shapes */
412#ifdef MENUBAR 572#ifdef MENUBAR
413/* EXTPROTO */
414void 573void
415rxvt_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)
416{ 575{
417 switch (type) 576 switch (type)
418 { 577 {
449#endif 608#endif
450 609
451 } 610 }
452} 611}
453#endif 612#endif
613
614// should not be used in 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
454/*----------------------- end-of-file (C source) -----------------------*/ 633/*----------------------- end-of-file (C source) -----------------------*/
634

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines