--- rxvt-unicode/src/misc.C 2004/02/01 01:34:41 1.5 +++ rxvt-unicode/src/misc.C 2005/08/05 16:42:44 1.28 @@ -1,11 +1,12 @@ /*--------------------------------*-C-*---------------------------------* - * File: misc.c + * File: misc.C *----------------------------------------------------------------------* * * All portions of code are copyright by their respective author/s. * Copyright (c) 1996 mj olesen Queen's Univ at Kingston * Copyright (c) 1997,1998 Oezguer Kesim * Copyright (c) 1998-2000 Geoff Wing + * Copyright (c) 2003-2004 Marc Lehmann * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,38 +25,219 @@ #include "../config.h" /* NECESSARY */ #include "rxvt.h" /* NECESSARY */ -#include "misc.intpro" /* PROTOS for internal routines */ -/* EXTPROTO */ +char * +rxvt_wcstombs (const wchar_t *str, int len) +{ + if (len < 0) len = wcslen (str); + + mbstate mbs; + char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); + + char *dst = r; + while (len--) + { + ssize_t l = wcrtomb (dst, *str++, mbs); + if (l < 0) + *dst++ = '?'; + else + dst += l; + } + + *dst++ = 0; + + return (char *)rxvt_realloc (r, dst - r); +} + +wchar_t * +rxvt_mbstowcs (const char *str, int len) +{ + if (len < 0) len = strlen (str); + + wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); + + if ((ssize_t)mbstowcs (r, str, len + 1) < 0) + *r = 0; + + return r; +} + +char * +rxvt_wcstoutf8 (const wchar_t *str, int len) +{ + if (len < 0) len = wcslen (str); + + char *r = (char *)rxvt_malloc (len * 4 + 1); + char *p = r; + + while (len--) + { + unicode_t w = *str++ & UNICODE_MASK; + + if (w < 0x000080) + *p++ = w; + else if (w < 0x000800) + *p++ = 0xc0 | ( w >> 6), + *p++ = 0x80 | ( w & 0x3f); + else if (w < 0x010000) + *p++ = 0xe0 | ( w >> 12 ), + *p++ = 0x80 | ((w >> 6) & 0x3f), + *p++ = 0x80 | ( w & 0x3f); + else if (w < 0x110000) + *p++ = 0xf0 | ( w >> 18), + *p++ = 0x80 | ((w >> 12) & 0x3f), + *p++ = 0x80 | ((w >> 6) & 0x3f), + *p++ = 0x80 | ( w & 0x3f); + else + *p++ = '?'; + } + + *p++ = 0; + + return (char *)rxvt_realloc (r, p - r); +} + +wchar_t * +rxvt_utf8towcs (const char *str, int len) +{ + if (len < 0) len = strlen (str); + + wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)), + *p = r; + + unsigned char *s = (unsigned char *)str, + *e = s + len; + + for (;;) + { + len = e - s; + + if (len == 0) + break; + else if (s[0] < 0x80) + *p++ = *s++; + else if (len >= 2 + && s[0] >= 0xc2 && s[0] <= 0xdf + && (s[1] & 0xc0) == 0x80) + { + *p++ = ((s[0] & 0x1f) << 6) + | (s[1] & 0x3f); + s += 2; + } + 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) + || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf) + ) + && (s[2] & 0xc0) == 0x80) + { + *p++ = ((s[0] & 0x0f) << 12) + | ((s[1] & 0x3f) << 6) + | (s[2] & 0x3f); + s += 3; + } + 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) + ) + && (s[2] & 0xc0) == 0x80 + && (s[3] & 0xc0) == 0x80) + { + *p++ = ((s[0] & 0x07) << 18) + | ((s[1] & 0x3f) << 12) + | ((s[2] & 0x3f) << 6) + | (s[3] & 0x3f); + s += 4; + } + else + { + *p++ = 0xfffd; + s++; + } + } + + *p = 0; + + return r; +} + char * rxvt_strdup (const char *str) { return str ? strdup (str) : 0; } -/* EXTPROTO */ -char * -rxvt_r_basename(const char *str) +char * +rxvt_r_basename (const char *str) { - char *base = STRRCHR(str, '/'); + char *base = strrchr (str, '/'); - return (char *)(base ? base + 1 : str); + return (char *) (base ? base + 1 : str); } /* * Print an error message */ -/* EXTPROTO */ void -rxvt_print_error(const char *fmt,...) +rxvt_vlog (const char *fmt, va_list arg_ptr) { - va_list arg_ptr; + char msg[1024]; - va_start(arg_ptr, fmt); - fprintf(stderr, APL_NAME ": "); - vfprintf(stderr, fmt, arg_ptr); - fprintf(stderr, "\n"); - va_end(arg_ptr); + vsnprintf (msg, sizeof msg, fmt, arg_ptr); + + if (GET_R && GET_R->log_hook) + (*GET_R->log_hook) (msg); + else + write (STDOUT_FILENO, msg, strlen (msg)); +} + +void +rxvt_log (const char *fmt,...) +{ + va_list arg_ptr; + + va_start (arg_ptr, fmt); + rxvt_vlog (fmt, arg_ptr); + va_end (arg_ptr); +} + +/* + * Print an error message + */ +void +rxvt_warn (const char *fmt,...) +{ + va_list arg_ptr; + + rxvt_log ("%s: ", RESNAME); + + va_start (arg_ptr, fmt); + rxvt_vlog (fmt, arg_ptr); + va_end (arg_ptr); +} + +void +rxvt_fatal (const char *fmt,...) +{ + va_list arg_ptr; + + rxvt_log ("%s: ", RESNAME); + + va_start (arg_ptr, fmt); + rxvt_vlog (fmt, arg_ptr); + va_end (arg_ptr); + + rxvt_exit_failure (); +} + +class rxvt_failure_exception rxvt_failure_exception; + +void +rxvt_exit_failure () +{ + throw (rxvt_failure_exception); } /* @@ -64,24 +246,23 @@ * No Match * return: 0 * Match - * return: STRLEN (S2) + * return: strlen (S2) */ -/* EXTPROTO */ int -rxvt_Str_match(const char *s1, const char *s2) +rxvt_Str_match (const char *s1, const char *s2) { - int n = STRLEN(s2); + int n = strlen (s2); - return ((STRNCMP(s1, s2, n) == 0) ? n : 0); + return ((strncmp (s1, s2, n) == 0) ? n : 0); } -/* EXTPROTO */ -const char * -rxvt_Str_skip_space(const char *str) +const char * +rxvt_Str_skip_space (const char *str) { if (str) - while (*str && isspace(*str)) + while (*str && isspace (*str)) str++; + return str; } @@ -89,36 +270,35 @@ * remove leading/trailing space and strip-off leading/trailing quotes. * in place. */ -/* EXTPROTO */ char * -rxvt_Str_trim(char *str) +rxvt_Str_trim (char *str) { - char *r, *s; - int n; + char *r, *s; if (!str || !*str) /* shortcut */ return str; /* skip leading spaces */ - for (s = str; *s && isspace(*s); s++) ; + 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; } - /* copy back over: forwards copy */ - for (r = str; n; n--) - *r++ = *s++; - *r = '\0'; +#endif + + memmove (str, s, r + 1 - s); + str[r + 1 - s] = 0; return str; } @@ -137,9 +317,8 @@ * * returns the converted string length */ -/* EXTPROTO */ int -rxvt_Str_escaped(char *str) +rxvt_Str_escaped (char *str) { char ch, *s, *d; int i, num, append = 0; @@ -154,9 +333,9 @@ /* Emacs convenience, replace leading `M-..' with `\E..' */ *d++ = C0_ESC; s += 2; - if (toupper(*s) == 'X') + if (toupper (*s) == 'X') /* append carriage-return for `M-xcommand' */ - for (*d++ = 'x', append = '\r', s++; isspace(*s); s++) ; + for (*d++ = 'x', append = '\r', s++; isspace (*s); s++) ; } for (; (ch = *s++);) { @@ -191,7 +370,7 @@ else if (ch == '^') { ch = *s++; - ch = toupper(ch); + ch = toupper (ch); ch = (ch == '?' ? 127 : (ch - '@')); } *d++ = ch; @@ -211,13 +390,11 @@ /* * 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 */ -/* EXTPROTO */ char ** -rxvt_splitcommastring(const char *cs) +rxvt_splitcommastring (const char *cs) { int l, n, p; const char *s, *t; @@ -229,22 +406,33 @@ for (n = 1, t = s; *t; t++) if (*t == ',') n++; - ret = (char **)malloc((n + 1) * sizeof(char *)); + + ret = (char **)malloc ((n + 1) * sizeof (char *)); ret[n] = NULL; for (l = 0, t = s; l < n; l++) { for ( ; *t && *t != ','; t++) ; p = t - s; - ret[l] = (char *)malloc(p + 1); - strncpy(ret[l], s, p); + ret[l] = (char *)malloc (p + 1); + strncpy (ret[l], s, p); ret[l][p] = '\0'; - rxvt_Str_trim(ret[l]); + rxvt_Str_trim (ret[l]); s = ++t; } + return ret; } +void +rxvt_freecommastring (char **cs) +{ + for (int i = 0; cs[i]; ++i) + free (cs[i]); + + free (cs); +} + /*----------------------------------------------------------------------* * file searching */ @@ -258,53 +446,52 @@ * * FILE is either semi-colon or zero terminated */ -/* INTPROTO */ char * -rxvt_File_search_path(const char *pathlist, const char *file, const char *ext) +rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) { int maxpath, len; const char *p, *path; char name[256]; - if (!access(file, R_OK)) /* found (plain name) in current directory */ - return STRDUP(file); + if (!access (file, R_OK)) /* found (plain name) in current directory */ + return strdup (file); /* semi-colon delimited */ - if ((p = STRCHR(file, ';'))) + if ((p = strchr (file, ';'))) len = (p - file); else - len = STRLEN(file); + len = strlen (file); #ifdef DEBUG_SEARCH_PATH - getcwd(name, sizeof(name)); - fprintf(stderr, "pwd: \"%s\"\n", name); - fprintf(stderr, "find: \"%.*s\"\n", len, file); + getcwd (name, sizeof (name)); + fprintf (stderr, "pwd: \"%s\"\n", name); + fprintf (stderr, "find: \"%.*s\"\n", len, file); #endif /* leave room for an extra '/' and trailing '\0' */ - maxpath = sizeof(name) - (len + (ext ? STRLEN(ext) : 0) + 2); + maxpath = sizeof (name) - (len + (ext ? strlen (ext) : 0) + 2); if (maxpath <= 0) return NULL; /* check if we can find it now */ - STRNCPY(name, file, len); + strncpy (name, file, len); name[len] = '\0'; - if (!access(name, R_OK)) - return STRDUP(name); + if (!access (name, R_OK)) + return strdup (name); if (ext) { - STRCAT(name, ext); - if (!access(name, R_OK)) - return STRDUP(name); + strcat (name, ext); + if (!access (name, R_OK)) + return strdup (name); } for (path = pathlist; path != NULL && *path != '\0'; path = p) { int n; /* colon delimited */ - if ((p = STRCHR(path, ':')) == NULL) - p = STRCHR(path, '\0'); + if ((p = strchr (path, ':')) == NULL) + p = strchr (path, '\0'); n = (p - path); if (*p != '\0') @@ -312,28 +499,27 @@ if (n > 0 && n <= maxpath) { - STRNCPY(name, path, n); + strncpy (name, path, n); if (name[n - 1] != '/') name[n++] = '/'; name[n] = '\0'; - STRNCAT(name, file, len); + strncat (name, file, len); - if (!access(name, R_OK)) - return STRDUP(name); + if (!access (name, R_OK)) + return strdup (name); if (ext) { - STRCAT(name, ext); - if (!access(name, R_OK)) - return STRDUP(name); + strcat (name, ext); + if (!access (name, R_OK)) + return strdup (name); } } } return NULL; } -/* EXTPROTO */ char * -rxvt_File_find(const char *file, const char *ext, const char *path) +rxvt_File_find (const char *file, const char *ext, const char *path) { char *f; @@ -341,15 +527,15 @@ return NULL; /* search environment variables here too */ - if ((f = rxvt_File_search_path(path, file, ext)) == NULL) + if ((f = rxvt_File_search_path (path, file, ext)) == NULL) #ifdef PATH_ENV - if ((f = rxvt_File_search_path(getenv(PATH_ENV), file, ext)) == NULL) + if ((f = rxvt_File_search_path (getenv (PATH_ENV), file, ext)) == NULL) #endif - f = rxvt_File_search_path(getenv("PATH"), file, ext); + f = rxvt_File_search_path (getenv ("PATH"), file, ext); #ifdef DEBUG_SEARCH_PATH if (f) - fprintf(stderr, "found: \"%s\"\n", f); + fprintf (stderr, "found: \"%s\"\n", f); #endif return f; @@ -364,9 +550,8 @@ * Draw top/left and bottom/right border shadows around windows */ #if defined(RXVT_SCROLLBAR) || defined(MENUBAR) -/* EXTPROTO */ void -rxvt_Draw_Shadow(Display *Xdisplay, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) +rxvt_Draw_Shadow (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int h) { int shadow; @@ -375,55 +560,75 @@ h += y - 1; for (; shadow-- > 0; x++, y++, w--, h--) { - XDrawLine(Xdisplay, win, topShadow, x, y, w, y); - XDrawLine(Xdisplay, win, topShadow, x, y, x, h); - XDrawLine(Xdisplay, win, botShadow, w, h, w, y + 1); - XDrawLine(Xdisplay, win, botShadow, w, h, x + 1, h); + XDrawLine (display, win, topShadow, x, y, w, y); + XDrawLine (display, win, topShadow, x, y, x, h); + XDrawLine (display, win, botShadow, w, h, w, y + 1); + XDrawLine (display, win, botShadow, w, h, x + 1, h); } } #endif /* button shapes */ #ifdef MENUBAR -/* EXTPROTO */ void -rxvt_Draw_Triangle(Display *Xdisplay, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) +rxvt_Draw_Triangle (Display *display, Window win, GC topShadow, GC botShadow, int x, int y, int w, int type) { switch (type) { case 'r': /* right triangle */ - XDrawLine(Xdisplay, win, topShadow, x, y, x, y + w); - XDrawLine(Xdisplay, win, topShadow, x, y, x + w, y + w / 2); - XDrawLine(Xdisplay, win, botShadow, x, y + w, x + w, y + w / 2); + XDrawLine (display, win, topShadow, x, y, x, y + w); + XDrawLine (display, win, topShadow, x, y, x + w, y + w / 2); + XDrawLine (display, win, botShadow, x, y + w, x + w, y + w / 2); break; case 'l': /* left triangle */ - XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w, y); - XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x, y + w / 2); - XDrawLine(Xdisplay, win, topShadow, x, y + w / 2, x + w, y); + XDrawLine (display, win, botShadow, x + w, y + w, x + w, y); + XDrawLine (display, win, botShadow, x + w, y + w, x, y + w / 2); + XDrawLine (display, win, topShadow, x, y + w / 2, x + w, y); break; case 'd': /* down triangle */ - XDrawLine(Xdisplay, win, topShadow, x, y, x + w / 2, y + w); - XDrawLine(Xdisplay, win, topShadow, x, y, x + w, y); - XDrawLine(Xdisplay, win, botShadow, x + w, y, x + w / 2, y + w); + XDrawLine (display, win, topShadow, x, y, x + w / 2, y + w); + XDrawLine (display, win, topShadow, x, y, x + w, y); + XDrawLine (display, win, botShadow, x + w, y, x + w / 2, y + w); break; case 'u': /* up triangle */ - XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w / 2, y); - XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x, y + w); - XDrawLine(Xdisplay, win, topShadow, x, y + w, x + w / 2, y); + XDrawLine (display, win, botShadow, x + w, y + w, x + w / 2, y); + XDrawLine (display, win, botShadow, x + w, y + w, x, y + w); + XDrawLine (display, win, topShadow, x, y + w, x + w / 2, y); break; #if 0 case 's': /* square */ - XDrawLine(Xdisplay, win, topShadow, x + w, y, x, y); - XDrawLine(Xdisplay, win, topShadow, x, y, x, y + w); - XDrawLine(Xdisplay, win, botShadow, x, y + w, x + w, y + w); - XDrawLine(Xdisplay, win, botShadow, x + w, y + w, x + w, y); + XDrawLine (display, win, topShadow, x + w, y, x, y); + XDrawLine (display, win, topShadow, x, y, x, y + w); + XDrawLine (display, win, botShadow, x, y + w, x + w, y + w); + XDrawLine (display, win, botShadow, x + w, y + w, x + w, y); break; #endif } } #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) -----------------------*/ +