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.9 by pcg, Wed Mar 3 04:07:52 2004 UTC vs.
Revision 1.27 by root, Wed Feb 16 21:37:10 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 */ 29char *
30rxvt_wcstombs (const wchar_t *str, int len)
31{
32 if (len < 0) len = wcslen (str);
33
34 mbstate mbs;
35 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
36
37 char *dst = r;
38 while (len--)
39 {
40 ssize_t l = wcrtomb (dst, *str++, mbs);
41 if (l < 0)
42 *dst++ = '?';
43 else
44 dst += l;
45 }
46
47 *dst++ = 0;
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
62 return r;
63}
64
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
31char * 166char *
32rxvt_strdup (const char *str) 167rxvt_strdup (const char *str)
33{ 168{
34 return str ? strdup (str) : 0; 169 return str ? strdup (str) : 0;
35} 170}
36 171
37/* EXTPROTO */
38char * 172char *
39rxvt_r_basename (const char *str) 173rxvt_r_basename (const char *str)
40{ 174{
41 char *base = STRRCHR (str, '/'); 175 char *base = strrchr (str, '/');
42 176
43 return (char *) (base ? base + 1 : str); 177 return (char *) (base ? base + 1 : str);
44} 178}
45 179
46/* 180/*
47 * Print an error message 181 * Print an error message
48 */ 182 */
49/* EXTPROTO */
50void 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
51rxvt_print_error (const char *fmt,...) 197rxvt_log (const char *fmt,...)
52{ 198{
53 va_list arg_ptr; 199 va_list arg_ptr;
54 200
55 va_start (arg_ptr, fmt); 201 va_start (arg_ptr, fmt);
56 fprintf (stderr, RESNAME ": "); 202 rxvt_vlog (fmt, arg_ptr);
57 vfprintf (stderr, fmt, arg_ptr);
58 fprintf (stderr, "\n");
59 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);
60} 241}
61 242
62/* 243/*
63 * check that the first characters of S1 match S2 244 * check that the first characters of S1 match S2
64 * 245 *
65 * No Match 246 * No Match
66 * return: 0 247 * return: 0
67 * Match 248 * Match
68 * return: STRLEN (S2) 249 * return: strlen (S2)
69 */ 250 */
70/* EXTPROTO */
71int 251int
72rxvt_Str_match (const char *s1, const char *s2) 252rxvt_Str_match (const char *s1, const char *s2)
73{ 253{
74 int n = STRLEN (s2); 254 int n = strlen (s2);
75 255
76 return ((STRNCMP (s1, s2, n) == 0) ? n : 0); 256 return ((strncmp (s1, s2, n) == 0) ? n : 0);
77} 257}
78 258
79/* EXTPROTO */
80const char * 259const char *
81rxvt_Str_skip_space (const char *str) 260rxvt_Str_skip_space (const char *str)
82{ 261{
83 if (str) 262 if (str)
84 while (*str && isspace (*str)) 263 while (*str && isspace (*str))
89 268
90/* 269/*
91 * remove leading/trailing space and strip-off leading/trailing quotes. 270 * remove leading/trailing space and strip-off leading/trailing quotes.
92 * in place. 271 * in place.
93 */ 272 */
94/* EXTPROTO */
95char * 273char *
96rxvt_Str_trim (char *str) 274rxvt_Str_trim (char *str)
97{ 275{
98 char *r, *s; 276 char *r, *s;
99 int n;
100 277
101 if (!str || !*str) /* shortcut */ 278 if (!str || !*str) /* shortcut */
102 return str; 279 return str;
103 280
104 /* skip leading spaces */ 281 /* skip leading spaces */
105 for (s = str; *s && isspace (*s); s++) ; 282 for (s = str; *s && isspace (*s); s++) ;
283
106 /* goto end of string */ 284 /* goto end of string */
107 for (n = 0, r = s; *r++; n++) ; 285 r = s + strlen (s) - 1;
108 r -= 2; 286
109 /* dump return */ 287 /* dump return and other trailing whitespace */
110 if (n > 0 && *r == '\n') 288 while (r > s && isspace (*r))
111 n--, r--; 289 r--;
112 /* backtrack along trailing spaces */ 290
113 for (; n > 0 && isspace (*r); r--, n--) ; 291#if 0
114 /* skip matching leading/trailing quotes */ 292 /* skip matching leading/trailing quotes */
115 if (*s == '"' && *r == '"' && n > 1) 293 if (*s == '"' && *r == '"' && n > 1)
116 { 294 {
117 s++; 295 s++;
118 n -= 2; 296 n -= 2;
119 } 297 }
298#endif
120 299
121 /* copy back over: forwards copy */ 300 memmove (str, s, r + 1 - s);
122 for (r = str; n; n--) 301 str[r + 1 - s] = 0;
123 *r++ = *s++;
124 *r = '\0';
125 302
126 return str; 303 return str;
127} 304}
128 305
129/* 306/*
138 * "M-x" prefixed strings, append "\r" if needed 315 * "M-x" prefixed strings, append "\r" if needed
139 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 316 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
140 * 317 *
141 * returns the converted string length 318 * returns the converted string length
142 */ 319 */
143/* EXTPROTO */
144int 320int
145rxvt_Str_escaped (char *str) 321rxvt_Str_escaped (char *str)
146{ 322{
147 char ch, *s, *d; 323 char ch, *s, *d;
148 int i, num, append = 0; 324 int i, num, append = 0;
212 return (d - str); 388 return (d - str);
213} 389}
214 390
215/* 391/*
216 * Split a comma-separated string into an array, stripping leading and 392 * Split a comma-separated string into an array, stripping leading and
217 * trailing spaces (and paired quotes) from each entry. Empty strings 393 * trailing spaces from each entry. Empty strings are properly returned
218 * are properly returned
219 * Caller should free each entry and array when done 394 * Caller should free each entry and array when done
220 */ 395 */
221/* EXTPROTO */
222char ** 396char **
223rxvt_splitcommastring (const char *cs) 397rxvt_splitcommastring (const char *cs)
224{ 398{
225 int l, n, p; 399 int l, n, p;
226 const char *s, *t; 400 const char *s, *t;
230 s = ""; 404 s = "";
231 405
232 for (n = 1, t = s; *t; t++) 406 for (n = 1, t = s; *t; t++)
233 if (*t == ',') 407 if (*t == ',')
234 n++; 408 n++;
409
235 ret = (char **)malloc ((n + 1) * sizeof (char *)); 410 ret = (char **)malloc ((n + 1) * sizeof (char *));
236 ret[n] = NULL; 411 ret[n] = NULL;
237 412
238 for (l = 0, t = s; l < n; l++) 413 for (l = 0, t = s; l < n; l++)
239 { 414 {
243 strncpy (ret[l], s, p); 418 strncpy (ret[l], s, p);
244 ret[l][p] = '\0'; 419 ret[l][p] = '\0';
245 rxvt_Str_trim (ret[l]); 420 rxvt_Str_trim (ret[l]);
246 s = ++t; 421 s = ++t;
247 } 422 }
423
248 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);
249} 434}
250 435
251/*----------------------------------------------------------------------* 436/*----------------------------------------------------------------------*
252 * file searching 437 * file searching
253 */ 438 */
259 * search for FILE in the current working directory, and within the 444 * search for FILE in the current working directory, and within the
260 * colon-delimited PATHLIST, adding the file extension EXT if required. 445 * colon-delimited PATHLIST, adding the file extension EXT if required.
261 * 446 *
262 * FILE is either semi-colon or zero terminated 447 * FILE is either semi-colon or zero terminated
263 */ 448 */
264/* INTPROTO */
265char * 449char *
266rxvt_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)
267{ 451{
268 int maxpath, len; 452 int maxpath, len;
269 const char *p, *path; 453 const char *p, *path;
270 char name[256]; 454 char name[256];
271 455
272 if (!access (file, R_OK)) /* found (plain name) in current directory */ 456 if (!access (file, R_OK)) /* found (plain name) in current directory */
273 return STRDUP (file); 457 return strdup (file);
274 458
275 /* semi-colon delimited */ 459 /* semi-colon delimited */
276 if ((p = STRCHR (file, ';'))) 460 if ((p = strchr (file, ';')))
277 len = (p - file); 461 len = (p - file);
278 else 462 else
279 len = STRLEN (file); 463 len = strlen (file);
280 464
281#ifdef DEBUG_SEARCH_PATH 465#ifdef DEBUG_SEARCH_PATH
282 getcwd (name, sizeof (name)); 466 getcwd (name, sizeof (name));
283 fprintf (stderr, "pwd: \"%s\"\n", name); 467 fprintf (stderr, "pwd: \"%s\"\n", name);
284 fprintf (stderr, "find: \"%.*s\"\n", len, file); 468 fprintf (stderr, "find: \"%.*s\"\n", len, file);
285#endif 469#endif
286 470
287 /* leave room for an extra '/' and trailing '\0' */ 471 /* leave room for an extra '/' and trailing '\0' */
288 maxpath = sizeof (name) - (len + (ext ? STRLEN (ext) : 0) + 2); 472 maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2);
289 if (maxpath <= 0) 473 if (maxpath <= 0)
290 return NULL; 474 return NULL;
291 475
292 /* check if we can find it now */ 476 /* check if we can find it now */
293 STRNCPY (name, file, len); 477 strncpy (name, file, len);
294 name[len] = '\0'; 478 name[len] = '\0';
295 479
296 if (!access (name, R_OK)) 480 if (!access (name, R_OK))
297 return STRDUP (name); 481 return strdup (name);
298 if (ext) 482 if (ext)
299 { 483 {
300 STRCAT (name, ext); 484 strcat (name, ext);
301 if (!access (name, R_OK)) 485 if (!access (name, R_OK))
302 return STRDUP (name); 486 return strdup (name);
303 } 487 }
304 for (path = pathlist; path != NULL && *path != '\0'; path = p) 488 for (path = pathlist; path != NULL && *path != '\0'; path = p)
305 { 489 {
306 int n; 490 int n;
307 491
308 /* colon delimited */ 492 /* colon delimited */
309 if ((p = STRCHR (path, ':')) == NULL) 493 if ((p = strchr (path, ':')) == NULL)
310 p = STRCHR (path, '\0'); 494 p = strchr (path, '\0');
311 495
312 n = (p - path); 496 n = (p - path);
313 if (*p != '\0') 497 if (*p != '\0')
314 p++; 498 p++;
315 499
316 if (n > 0 && n <= maxpath) 500 if (n > 0 && n <= maxpath)
317 { 501 {
318 STRNCPY (name, path, n); 502 strncpy (name, path, n);
319 if (name[n - 1] != '/') 503 if (name[n - 1] != '/')
320 name[n++] = '/'; 504 name[n++] = '/';
321 name[n] = '\0'; 505 name[n] = '\0';
322 STRNCAT (name, file, len); 506 strncat (name, file, len);
323 507
324 if (!access (name, R_OK)) 508 if (!access (name, R_OK))
325 return STRDUP (name); 509 return strdup (name);
326 if (ext) 510 if (ext)
327 { 511 {
328 STRCAT (name, ext); 512 strcat (name, ext);
329 if (!access (name, R_OK)) 513 if (!access (name, R_OK))
330 return STRDUP (name); 514 return strdup (name);
331 } 515 }
332 } 516 }
333 } 517 }
334 return NULL; 518 return NULL;
335} 519}
336 520
337/* EXTPROTO */
338char * 521char *
339rxvt_File_find (const char *file, const char *ext, const char *path) 522rxvt_File_find (const char *file, const char *ext, const char *path)
340{ 523{
341 char *f; 524 char *f;
342 525
365 548
366/* 549/*
367 * Draw top/left and bottom/right border shadows around windows 550 * Draw top/left and bottom/right border shadows around windows
368 */ 551 */
369#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 552#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
370/* EXTPROTO */
371void 553void
372rxvt_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)
373{ 555{
374 int shadow; 556 int shadow;
375 557
386} 568}
387#endif 569#endif
388 570
389/* button shapes */ 571/* button shapes */
390#ifdef MENUBAR 572#ifdef MENUBAR
391/* EXTPROTO */
392void 573void
393rxvt_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)
394{ 575{
395 switch (type) 576 switch (type)
396 { 577 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines