--- rxvt-unicode/src/misc.C 2004/12/12 06:30:25 1.19 +++ rxvt-unicode/src/misc.C 2005/08/05 16:42:44 1.28 @@ -25,7 +25,6 @@ #include "../config.h" /* NECESSARY */ #include "rxvt.h" /* NECESSARY */ -#include "misc.intpro" /* PROTOS for internal routines */ char * rxvt_wcstombs (const wchar_t *str, int len) @@ -38,7 +37,7 @@ char *dst = r; while (len--) { - int l = wcrtomb (dst, *str++, mbs); + ssize_t l = wcrtomb (dst, *str++, mbs); if (l < 0) *dst++ = '?'; else @@ -47,7 +46,7 @@ *dst++ = 0; - return r; + return (char *)rxvt_realloc (r, dst - r); } wchar_t * @@ -57,7 +56,7 @@ wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); - if (mbstowcs (r, str, len + 1) < 0) + if ((ssize_t)mbstowcs (r, str, len + 1) < 0) *r = 0; return r; @@ -73,7 +72,7 @@ while (len--) { - unicode_t w = *str++; + unicode_t w = *str++ & UNICODE_MASK; if (w < 0x000080) *p++ = w; @@ -93,9 +92,9 @@ *p++ = '?'; } - *p = 0; + *p++ = 0; - return r; + return (char *)rxvt_realloc (r, p - r); } wchar_t * @@ -103,16 +102,21 @@ { if (len < 0) len = strlen (str); - wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); - wchar_t *p = r; + wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)), + *p = r; - unsigned char *s = (unsigned char *)str; + unsigned char *s = (unsigned char *)str, + *e = s + len; - while (len--) + for (;;) { - if (s[0] < 0x80) + len = e - s; + + if (len == 0) + break; + else if (s[0] < 0x80) *p++ = *s++; - else if (len > 0 + else if (len >= 2 && s[0] >= 0xc2 && s[0] <= 0xdf && (s[1] & 0xc0) == 0x80) { @@ -120,7 +124,7 @@ | (s[1] & 0x3f); s += 2; } - else if (len > 1 + else if (len >= 3 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf) || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) @@ -133,7 +137,7 @@ | (s[2] & 0x3f); s += 3; } - else if (len > 2 + else if (len >= 4 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf) || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) @@ -165,7 +169,6 @@ return str ? strdup (str) : 0; } -/* INTPROTO */ char * rxvt_r_basename (const char *str) { @@ -177,7 +180,6 @@ /* * Print an error message */ -/* INTPROTO */ void rxvt_vlog (const char *fmt, va_list arg_ptr) { @@ -191,7 +193,6 @@ write (STDOUT_FILENO, msg, strlen (msg)); } -/* INTPROTO */ void rxvt_log (const char *fmt,...) { @@ -205,7 +206,6 @@ /* * Print an error message */ -/* INTPROTO */ void rxvt_warn (const char *fmt,...) { @@ -218,7 +218,6 @@ va_end (arg_ptr); } -/* INTPROTO */ void rxvt_fatal (const char *fmt,...) { @@ -235,7 +234,6 @@ class rxvt_failure_exception rxvt_failure_exception; -/* INTPROTO */ void rxvt_exit_failure () { @@ -250,7 +248,6 @@ * Match * return: strlen (S2) */ -/* INTPROTO */ int rxvt_Str_match (const char *s1, const char *s2) { @@ -259,7 +256,6 @@ return ((strncmp (s1, s2, n) == 0) ? n : 0); } -/* INTPROTO */ const char * rxvt_Str_skip_space (const char *str) { @@ -274,37 +270,35 @@ * remove leading/trailing space and strip-off leading/trailing quotes. * in place. */ -/* INTPROTO */ char * rxvt_Str_trim (char *str) { char *r, *s; - int n; if (!str || !*str) /* shortcut */ return str; /* skip leading spaces */ for (s = str; *s && isspace (*s); s++) ; + /* goto end of string */ - for (n = 0, r = s; *r++; n++) ; - r -= 2; - /* dump return */ - if (n > 0 && *r == '\n') - n--, r--; - /* backtrack along trailing spaces */ - for (; n > 0 && isspace (*r); r--, n--) ; + r = s + strlen (s) - 1; + + /* dump return and other trailing whitespace */ + while (r > s && isspace (*r)) + r--; + +#if 0 /* skip matching leading/trailing quotes */ if (*s == '"' && *r == '"' && n > 1) { s++; n -= 2; } +#endif - /* copy back over: forwards copy */ - for (r = str; n; n--) - *r++ = *s++; - *r = '\0'; + memmove (str, s, r + 1 - s); + str[r + 1 - s] = 0; return str; } @@ -323,7 +317,6 @@ * * returns the converted string length */ -/* INTPROTO */ int rxvt_Str_escaped (char *str) { @@ -397,11 +390,9 @@ /* * Split a comma-separated string into an array, stripping leading and - * trailing spaces (and paired quotes) from each entry. Empty strings - * are properly returned + * trailing spaces from each entry. Empty strings are properly returned * Caller should free each entry and array when done */ -/* INTPROTO */ char ** rxvt_splitcommastring (const char *cs) { @@ -415,6 +406,7 @@ for (n = 1, t = s; *t; t++) if (*t == ',') n++; + ret = (char **)malloc ((n + 1) * sizeof (char *)); ret[n] = NULL; @@ -428,6 +420,7 @@ rxvt_Str_trim (ret[l]); s = ++t; } + return ret; } @@ -453,7 +446,6 @@ * * FILE is either semi-colon or zero terminated */ -/* INTPROTO */ char * rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) { @@ -526,7 +518,6 @@ return NULL; } -/* INTPROTO */ char * rxvt_File_find (const char *file, const char *ext, const char *path) { @@ -559,7 +550,6 @@ * Draw top/left and bottom/right border shadows around windows */ #if defined(RXVT_SCROLLBAR) || defined(MENUBAR) -/* INTPROTO */ void rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) { @@ -580,7 +570,6 @@ /* button shapes */ #ifdef MENUBAR -/* INTPROTO */ void rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) { @@ -621,4 +610,25 @@ } } #endif + +// should nto be use din interactive programs, for obvious reasons +void rxvt_usleep (int usecs) +{ +#if HAVE_NANOSLEEP + struct timespec ts; + + ts.tv_sec = 0; + ts.tv_nsec = usecs * 1000; + nanosleep (&ts, NULL); +#else + /* use select for timing */ + struct timeval tv; + + tv.tv_sec = 0; + tv.tv_usec = usecs; + select (0, NULL, NULL, NULL, &tv); +#endif +} + /*----------------------- end-of-file (C source) -----------------------*/ +