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.8 by pcg, Fri Feb 27 02:52:51 2004 UTC vs.
Revision 1.26 by root, Wed Feb 16 20:32:05 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 */ 172/* INTPROTO */
38char * 173char *
39rxvt_r_basename (const char *str) 174rxvt_r_basename (const char *str)
40{ 175{
41 char *base = STRRCHR (str, '/'); 176 char *base = strrchr (str, '/');
42 177
43 return (char *) (base ? base + 1 : str); 178 return (char *) (base ? base + 1 : str);
44} 179}
45 180
46/* 181/*
47 * Print an error message 182 * Print an error message
48 */ 183 */
49/* EXTPROTO */ 184/* INTPROTO */
50void 185void
186rxvt_vlog (const char *fmt, va_list arg_ptr)
187{
188 char msg[1024];
189
190 vsnprintf (msg, sizeof msg, fmt, arg_ptr);
191
192 if (GET_R && GET_R->log_hook)
193 (*GET_R->log_hook) (msg);
194 else
195 write (STDOUT_FILENO, msg, strlen (msg));
196}
197
198/* INTPROTO */
199void
51rxvt_print_error (const char *fmt,...) 200rxvt_log (const char *fmt,...)
52{ 201{
53 va_list arg_ptr; 202 va_list arg_ptr;
54 203
55 va_start (arg_ptr, fmt); 204 va_start (arg_ptr, fmt);
56 fprintf (stderr, APL_NAME ": "); 205 rxvt_vlog (fmt, arg_ptr);
57 vfprintf (stderr, fmt, arg_ptr);
58 fprintf (stderr, "\n");
59 va_end (arg_ptr); 206 va_end (arg_ptr);
207}
208
209/*
210 * Print an error message
211 */
212/* INTPROTO */
213void
214rxvt_warn (const char *fmt,...)
215{
216 va_list arg_ptr;
217
218 rxvt_log ("%s: ", RESNAME);
219
220 va_start (arg_ptr, fmt);
221 rxvt_vlog (fmt, arg_ptr);
222 va_end (arg_ptr);
223}
224
225/* INTPROTO */
226void
227rxvt_fatal (const char *fmt,...)
228{
229 va_list arg_ptr;
230
231 rxvt_log ("%s: ", RESNAME);
232
233 va_start (arg_ptr, fmt);
234 rxvt_vlog (fmt, arg_ptr);
235 va_end (arg_ptr);
236
237 rxvt_exit_failure ();
238}
239
240class rxvt_failure_exception rxvt_failure_exception;
241
242/* INTPROTO */
243void
244rxvt_exit_failure ()
245{
246 throw (rxvt_failure_exception);
60} 247}
61 248
62/* 249/*
63 * check that the first characters of S1 match S2 250 * check that the first characters of S1 match S2
64 * 251 *
65 * No Match 252 * No Match
66 * return: 0 253 * return: 0
67 * Match 254 * Match
68 * return: STRLEN (S2) 255 * return: strlen (S2)
69 */ 256 */
70/* EXTPROTO */ 257/* INTPROTO */
71int 258int
72rxvt_Str_match (const char *s1, const char *s2) 259rxvt_Str_match (const char *s1, const char *s2)
73{ 260{
74 int n = STRLEN (s2); 261 int n = strlen (s2);
75 262
76 return ((STRNCMP (s1, s2, n) == 0) ? n : 0); 263 return ((strncmp (s1, s2, n) == 0) ? n : 0);
77} 264}
78 265
79/* EXTPROTO */ 266/* INTPROTO */
80const char * 267const char *
81rxvt_Str_skip_space (const char *str) 268rxvt_Str_skip_space (const char *str)
82{ 269{
83 if (str) 270 if (str)
84 while (*str && isspace (*str)) 271 while (*str && isspace (*str))
89 276
90/* 277/*
91 * remove leading/trailing space and strip-off leading/trailing quotes. 278 * remove leading/trailing space and strip-off leading/trailing quotes.
92 * in place. 279 * in place.
93 */ 280 */
94/* EXTPROTO */ 281/* INTPROTO */
95char * 282char *
96rxvt_Str_trim (char *str) 283rxvt_Str_trim (char *str)
97{ 284{
98 char *r, *s; 285 char *r, *s;
99 int n;
100 286
101 if (!str || !*str) /* shortcut */ 287 if (!str || !*str) /* shortcut */
102 return str; 288 return str;
103 289
104 /* skip leading spaces */ 290 /* skip leading spaces */
105 for (s = str; *s && isspace (*s); s++) ; 291 for (s = str; *s && isspace (*s); s++) ;
292
106 /* goto end of string */ 293 /* goto end of string */
107 for (n = 0, r = s; *r++; n++) ; 294 r = s + strlen (s) - 1;
108 r -= 2; 295
109 /* dump return */ 296 /* dump return and other trailing whitespace */
110 if (n > 0 && *r == '\n') 297 while (r > s && isspace (*r))
111 n--, r--; 298 r--;
112 /* backtrack along trailing spaces */ 299
113 for (; n > 0 && isspace (*r); r--, n--) ; 300#if 0
114 /* skip matching leading/trailing quotes */ 301 /* skip matching leading/trailing quotes */
115 if (*s == '"' && *r == '"' && n > 1) 302 if (*s == '"' && *r == '"' && n > 1)
116 { 303 {
117 s++; 304 s++;
118 n -= 2; 305 n -= 2;
119 } 306 }
307#endif
120 308
121 /* copy back over: forwards copy */ 309 memmove (str, s, r + 1 - s);
122 for (r = str; n; n--) 310 str[r + 1 - s] = 0;
123 *r++ = *s++;
124 *r = '\0';
125 311
126 return str; 312 return str;
127} 313}
128 314
129/* 315/*
138 * "M-x" prefixed strings, append "\r" if needed 324 * "M-x" prefixed strings, append "\r" if needed
139 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed 325 * "\E]" prefixed strings (XTerm escape sequence) append ST if needed
140 * 326 *
141 * returns the converted string length 327 * returns the converted string length
142 */ 328 */
143/* EXTPROTO */ 329/* INTPROTO */
144int 330int
145rxvt_Str_escaped (char *str) 331rxvt_Str_escaped (char *str)
146{ 332{
147 char ch, *s, *d; 333 char ch, *s, *d;
148 int i, num, append = 0; 334 int i, num, append = 0;
212 return (d - str); 398 return (d - str);
213} 399}
214 400
215/* 401/*
216 * Split a comma-separated string into an array, stripping leading and 402 * Split a comma-separated string into an array, stripping leading and
217 * trailing spaces (and paired quotes) from each entry. Empty strings 403 * trailing spaces from each entry. Empty strings are properly returned
218 * are properly returned
219 * Caller should free each entry and array when done 404 * Caller should free each entry and array when done
220 */ 405 */
221/* EXTPROTO */ 406/* INTPROTO */
222char ** 407char **
223rxvt_splitcommastring (const char *cs) 408rxvt_splitcommastring (const char *cs)
224{ 409{
225 int l, n, p; 410 int l, n, p;
226 const char *s, *t; 411 const char *s, *t;
230 s = ""; 415 s = "";
231 416
232 for (n = 1, t = s; *t; t++) 417 for (n = 1, t = s; *t; t++)
233 if (*t == ',') 418 if (*t == ',')
234 n++; 419 n++;
420
235 ret = (char **)malloc ((n + 1) * sizeof (char *)); 421 ret = (char **)malloc ((n + 1) * sizeof (char *));
236 ret[n] = NULL; 422 ret[n] = NULL;
237 423
238 for (l = 0, t = s; l < n; l++) 424 for (l = 0, t = s; l < n; l++)
239 { 425 {
243 strncpy (ret[l], s, p); 429 strncpy (ret[l], s, p);
244 ret[l][p] = '\0'; 430 ret[l][p] = '\0';
245 rxvt_Str_trim (ret[l]); 431 rxvt_Str_trim (ret[l]);
246 s = ++t; 432 s = ++t;
247 } 433 }
434
248 return ret; 435 return ret;
436}
437
438void
439rxvt_freecommastring (char **cs)
440{
441 for (int i = 0; cs[i]; ++i)
442 free (cs[i]);
443
444 free (cs);
249} 445}
250 446
251/*----------------------------------------------------------------------* 447/*----------------------------------------------------------------------*
252 * file searching 448 * file searching
253 */ 449 */
268 int maxpath, len; 464 int maxpath, len;
269 const char *p, *path; 465 const char *p, *path;
270 char name[256]; 466 char name[256];
271 467
272 if (!access (file, R_OK)) /* found (plain name) in current directory */ 468 if (!access (file, R_OK)) /* found (plain name) in current directory */
273 return STRDUP (file); 469 return strdup (file);
274 470
275 /* semi-colon delimited */ 471 /* semi-colon delimited */
276 if ((p = STRCHR (file, ';'))) 472 if ((p = strchr (file, ';')))
277 len = (p - file); 473 len = (p - file);
278 else 474 else
279 len = STRLEN (file); 475 len = strlen (file);
280 476
281#ifdef DEBUG_SEARCH_PATH 477#ifdef DEBUG_SEARCH_PATH
282 getcwd (name, sizeof (name)); 478 getcwd (name, sizeof (name));
283 fprintf (stderr, "pwd: \"%s\"\n", name); 479 fprintf (stderr, "pwd: \"%s\"\n", name);
284 fprintf (stderr, "find: \"%.*s\"\n", len, file); 480 fprintf (stderr, "find: \"%.*s\"\n", len, file);
285#endif 481#endif
286 482
287 /* leave room for an extra '/' and trailing '\0' */ 483 /* leave room for an extra '/' and trailing '\0' */
288 maxpath = sizeof (name) - (len + (ext ? STRLEN (ext) : 0) + 2); 484 maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2);
289 if (maxpath <= 0) 485 if (maxpath <= 0)
290 return NULL; 486 return NULL;
291 487
292 /* check if we can find it now */ 488 /* check if we can find it now */
293 STRNCPY (name, file, len); 489 strncpy (name, file, len);
294 name[len] = '\0'; 490 name[len] = '\0';
295 491
296 if (!access (name, R_OK)) 492 if (!access (name, R_OK))
297 return STRDUP (name); 493 return strdup (name);
298 if (ext) 494 if (ext)
299 { 495 {
300 STRCAT (name, ext); 496 strcat (name, ext);
301 if (!access (name, R_OK)) 497 if (!access (name, R_OK))
302 return STRDUP (name); 498 return strdup (name);
303 } 499 }
304 for (path = pathlist; path != NULL && *path != '\0'; path = p) 500 for (path = pathlist; path != NULL && *path != '\0'; path = p)
305 { 501 {
306 int n; 502 int n;
307 503
308 /* colon delimited */ 504 /* colon delimited */
309 if ((p = STRCHR (path, ':')) == NULL) 505 if ((p = strchr (path, ':')) == NULL)
310 p = STRCHR (path, '\0'); 506 p = strchr (path, '\0');
311 507
312 n = (p - path); 508 n = (p - path);
313 if (*p != '\0') 509 if (*p != '\0')
314 p++; 510 p++;
315 511
316 if (n > 0 && n <= maxpath) 512 if (n > 0 && n <= maxpath)
317 { 513 {
318 STRNCPY (name, path, n); 514 strncpy (name, path, n);
319 if (name[n - 1] != '/') 515 if (name[n - 1] != '/')
320 name[n++] = '/'; 516 name[n++] = '/';
321 name[n] = '\0'; 517 name[n] = '\0';
322 STRNCAT (name, file, len); 518 strncat (name, file, len);
323 519
324 if (!access (name, R_OK)) 520 if (!access (name, R_OK))
325 return STRDUP (name); 521 return strdup (name);
326 if (ext) 522 if (ext)
327 { 523 {
328 STRCAT (name, ext); 524 strcat (name, ext);
329 if (!access (name, R_OK)) 525 if (!access (name, R_OK))
330 return STRDUP (name); 526 return strdup (name);
331 } 527 }
332 } 528 }
333 } 529 }
334 return NULL; 530 return NULL;
335} 531}
336 532
337/* EXTPROTO */ 533/* INTPROTO */
338char * 534char *
339rxvt_File_find (const char *file, const char *ext, const char *path) 535rxvt_File_find (const char *file, const char *ext, const char *path)
340{ 536{
341 char *f; 537 char *f;
342 538
365 561
366/* 562/*
367 * Draw top/left and bottom/right border shadows around windows 563 * Draw top/left and bottom/right border shadows around windows
368 */ 564 */
369#if defined(RXVT_SCROLLBAR) || defined(MENUBAR) 565#if defined(RXVT_SCROLLBAR) || defined(MENUBAR)
370/* EXTPROTO */ 566/* INTPROTO */
371void 567void
372rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) 568rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h)
373{ 569{
374 int shadow; 570 int shadow;
375 571
386} 582}
387#endif 583#endif
388 584
389/* button shapes */ 585/* button shapes */
390#ifdef MENUBAR 586#ifdef MENUBAR
391/* EXTPROTO */ 587/* INTPROTO */
392void 588void
393rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) 589rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type)
394{ 590{
395 switch (type) 591 switch (type)
396 { 592 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines