--- rxvt-unicode/src/misc.C 2004/12/13 07:26:18 1.21 +++ rxvt-unicode/src/misc.C 2006/02/20 22:42:00 1.40 @@ -1,4 +1,4 @@ -/*--------------------------------*-C-*---------------------------------* +/*----------------------------------------------------------------------* * File: misc.C *----------------------------------------------------------------------* * @@ -6,7 +6,7 @@ * 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 + * Copyright (c) 2003-2006 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 @@ -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,8 @@ 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 +47,7 @@ *dst++ = 0; - return r; + return (char *)rxvt_realloc (r, dst - r); } wchar_t * @@ -57,7 +57,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 +73,7 @@ while (len--) { - unicode_t w = *str++; + unicode_t w = *str++ & UNICODE_MASK; if (w < 0x000080) *p++ = w; @@ -93,9 +93,9 @@ *p++ = '?'; } - *p = 0; + *p++ = 0; - return r; + return (char *)rxvt_realloc (r, p - r); } wchar_t * @@ -165,14 +165,7 @@ } char * -rxvt_strdup (const char *str) -{ - return str ? strdup (str) : 0; -} - -/* INTPROTO */ -char * -rxvt_r_basename (const char *str) +rxvt_r_basename (const char *str) NOTHROW { char *base = strrchr (str, '/'); @@ -182,9 +175,8 @@ /* * Print an error message */ -/* INTPROTO */ void -rxvt_vlog (const char *fmt, va_list arg_ptr) +rxvt_vlog (const char *fmt, va_list arg_ptr) NOTHROW { char msg[1024]; @@ -196,9 +188,8 @@ write (STDOUT_FILENO, msg, strlen (msg)); } -/* INTPROTO */ void -rxvt_log (const char *fmt,...) +rxvt_log (const char *fmt,...) NOTHROW { va_list arg_ptr; @@ -210,9 +201,8 @@ /* * Print an error message */ -/* INTPROTO */ void -rxvt_warn (const char *fmt,...) +rxvt_warn (const char *fmt,...) NOTHROW { va_list arg_ptr; @@ -223,9 +213,8 @@ va_end (arg_ptr); } -/* INTPROTO */ void -rxvt_fatal (const char *fmt,...) +rxvt_fatal (const char *fmt,...) THROW ((class rxvt_failure_exception)) { va_list arg_ptr; @@ -238,12 +227,10 @@ rxvt_exit_failure (); } -class rxvt_failure_exception rxvt_failure_exception; - -/* INTPROTO */ void -rxvt_exit_failure () +rxvt_exit_failure () THROW ((class rxvt_failure_exception)) { + static class rxvt_failure_exception rxvt_failure_exception; throw (rxvt_failure_exception); } @@ -255,18 +242,16 @@ * Match * return: strlen (S2) */ -/* INTPROTO */ int -rxvt_Str_match (const char *s1, const char *s2) +rxvt_Str_match (const char *s1, const char *s2) NOTHROW { int n = strlen (s2); return ((strncmp (s1, s2, n) == 0) ? n : 0); } -/* INTPROTO */ const char * -rxvt_Str_skip_space (const char *str) +rxvt_Str_skip_space (const char *str) NOTHROW { if (str) while (*str && isspace (*str)) @@ -279,37 +264,35 @@ * remove leading/trailing space and strip-off leading/trailing quotes. * in place. */ -/* INTPROTO */ char * -rxvt_Str_trim (char *str) +rxvt_Str_trim (char *str) NOTHROW { 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; } @@ -328,9 +311,8 @@ * * returns the converted string length */ -/* INTPROTO */ int -rxvt_Str_escaped (char *str) +rxvt_Str_escaped (char *str) NOTHROW { char ch, *s, *d; int i, num, append = 0; @@ -402,13 +384,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 */ -/* INTPROTO */ char ** -rxvt_splitcommastring (const char *cs) +rxvt_splitcommastring (const char *cs) NOTHROW { int l, n, p; const char *s, *t; @@ -420,6 +400,7 @@ for (n = 1, t = s; *t; t++) if (*t == ',') n++; + ret = (char **)malloc ((n + 1) * sizeof (char *)); ret[n] = NULL; @@ -433,11 +414,12 @@ rxvt_Str_trim (ret[l]); s = ++t; } + return ret; } void -rxvt_freecommastring (char **cs) +rxvt_freecommastring (char **cs) NOTHROW { for (int i = 0; cs[i]; ++i) free (cs[i]); @@ -449,18 +431,15 @@ * file searching */ -/* #define DEBUG_SEARCH_PATH */ - -#if defined (XPM_BACKGROUND) || (MENUBAR_MAX) +#ifdef XPM_BACKGROUND /* * search for FILE in the current working directory, and within the * colon-delimited PATHLIST, adding the file extension EXT if required. * * FILE is either semi-colon or zero terminated */ -/* INTPROTO */ -char * -rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) +char * +rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) NOTHROW { int maxpath, len; const char *p, *path; @@ -475,12 +454,6 @@ else len = strlen (file); -#ifdef DEBUG_SEARCH_PATH - 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); if (maxpath <= 0) @@ -531,99 +504,18 @@ return NULL; } -/* INTPROTO */ -char * -rxvt_File_find (const char *file, const char *ext, const char *path) +char * +rxvt_File_find (const char *file, const char *ext, const char *path) NOTHROW { char *f; if (file == NULL || *file == '\0') return NULL; - /* search environment variables here too */ - 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) -#endif - f = rxvt_File_search_path (getenv ("PATH"), file, ext); - -#ifdef DEBUG_SEARCH_PATH - if (f) - fprintf (stderr, "found: \"%s\"\n", f); -#endif + f = rxvt_File_search_path (path, file, ext); return f; } -#endif /* defined (XPM_BACKGROUND) || (MENUBAR_MAX) */ - -/*----------------------------------------------------------------------* - * miscellaneous drawing routines - */ - -/* - * 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) -{ - int shadow; - - shadow = (w == 0 || h == 0) ? 1 : SHADOW; - w += x - 1; - h += y - 1; - for (; shadow-- > 0; x++, y++, w--, 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 -/* INTPROTO */ -void -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 (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 (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 (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 (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 (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 -/*----------------------- end-of-file (C source) -----------------------*/