--- rxvt-unicode/src/misc.C 2006/02/20 22:42:00 1.40 +++ rxvt-unicode/src/misc.C 2021/05/13 19:40:19 1.60 @@ -6,11 +6,11 @@ * 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-2006 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 - * the Free Software Foundation; either version 2 of the License, or + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -26,6 +26,21 @@ #include "../config.h" /* NECESSARY */ #include "rxvt.h" /* NECESSARY */ +#include + +// alas new/delete cannot be specified as inline in C++11 (see 17.6.4.6) +void * +operator new (size_t s) +{ + return rxvt_malloc (s); +} + +void +operator delete (void *p) noexcept +{ + free (p); +} + char * rxvt_wcstombs (const wchar_t *str, int len) { @@ -40,7 +55,10 @@ ssize_t l = wcrtomb (dst, *str++, mbs); if (l < 0) - *dst++ = '?'; + { + *dst++ = '?'; + wcrtomb (0, 0, mbs); // reset undefined state + } else dst += l; } @@ -81,7 +99,7 @@ *p++ = 0xc0 | ( w >> 6), *p++ = 0x80 | ( w & 0x3f); else if (w < 0x010000) - *p++ = 0xe0 | ( w >> 12 ), + *p++ = 0xe0 | ( w >> 12), *p++ = 0x80 | ((w >> 6) & 0x3f), *p++ = 0x80 | ( w & 0x3f); else if (w < 0x110000) @@ -118,7 +136,7 @@ else if (s[0] < 0x80) *p++ = *s++; else if (len >= 2 - && s[0] >= 0xc2 && s[0] <= 0xdf + && s[0] >= 0xc2 && s[0] <= 0xdf && (s[1] & 0xc0) == 0x80) { *p++ = ((s[0] & 0x1f) << 6) @@ -146,9 +164,9 @@ && (s[2] & 0xc0) == 0x80 && (s[3] & 0xc0) == 0x80) { - *p++ = ((s[0] & 0x07) << 18) - | ((s[1] & 0x3f) << 12) - | ((s[2] & 0x3f) << 6) + *p++ = ((s[0] & 0x07) << 18) + | ((s[1] & 0x3f) << 12) + | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f); s += 4; } @@ -164,19 +182,19 @@ return r; } -char * -rxvt_r_basename (const char *str) NOTHROW +const char * +rxvt_basename (const char *str) noexcept { - char *base = strrchr (str, '/'); + const char *base = strrchr (str, '/'); - return (char *) (base ? base + 1 : str); + return base ? base + 1 : str; } /* * Print an error message */ void -rxvt_vlog (const char *fmt, va_list arg_ptr) NOTHROW +rxvt_vlog (const char *fmt, va_list arg_ptr) noexcept { char msg[1024]; @@ -189,7 +207,7 @@ } void -rxvt_log (const char *fmt,...) NOTHROW +rxvt_log (const char *fmt,...) noexcept { va_list arg_ptr; @@ -202,7 +220,7 @@ * Print an error message */ void -rxvt_warn (const char *fmt,...) NOTHROW +rxvt_warn (const char *fmt,...) noexcept { va_list arg_ptr; @@ -214,7 +232,7 @@ } void -rxvt_fatal (const char *fmt,...) THROW ((class rxvt_failure_exception)) +rxvt_fatal (const char *fmt,...) { va_list arg_ptr; @@ -228,44 +246,17 @@ } void -rxvt_exit_failure () THROW ((class rxvt_failure_exception)) +rxvt_exit_failure () { static class rxvt_failure_exception rxvt_failure_exception; throw (rxvt_failure_exception); } /* - * check that the first characters of S1 match S2 - * - * No Match - * return: 0 - * Match - * return: strlen (S2) + * remove leading/trailing space in place. */ -int -rxvt_Str_match (const char *s1, const char *s2) NOTHROW -{ - int n = strlen (s2); - - return ((strncmp (s1, s2, n) == 0) ? n : 0); -} - -const char * -rxvt_Str_skip_space (const char *str) NOTHROW -{ - if (str) - while (*str && isspace (*str)) - str++; - - return str; -} - -/* - * remove leading/trailing space and strip-off leading/trailing quotes. - * in place. - */ -char * -rxvt_Str_trim (char *str) NOTHROW +char * +rxvt_strtrim (char *str) noexcept { char *r, *s; @@ -282,15 +273,6 @@ while (r > s && isspace (*r)) r--; -#if 0 - /* skip matching leading/trailing quotes */ - if (*s == '"' && *r == '"' && n > 1) - { - s++; - n -= 2; - } -#endif - memmove (str, s, r + 1 - s); str[r + 1 - s] = 0; @@ -298,107 +280,20 @@ } /* - * in-place interpretation of string: - * - * backslash-escaped: "\a\b\E\e\n\r\t", "\octal" - * Ctrl chars: ^@ .. ^_, ^? - * - * Emacs-style: "M-" prefix - * - * Also, - * "M-x" prefixed strings, append "\r" if needed - * "\E]" prefixed strings (XTerm escape sequence) append ST if needed - * - * returns the converted string length - */ -int -rxvt_Str_escaped (char *str) NOTHROW -{ - char ch, *s, *d; - int i, num, append = 0; - - if (!str || !*str) - return 0; - - d = s = str; - - if (*s == 'M' && s[1] == '-') - { - /* Emacs convenience, replace leading `M-..' with `\E..' */ - *d++ = C0_ESC; - s += 2; - if (toupper (*s) == 'X') - /* append carriage-return for `M-xcommand' */ - for (*d++ = 'x', append = '\r', s++; isspace (*s); s++) ; - } - for (; (ch = *s++);) - { - if (ch == '\\') - { - ch = *s++; - if (ch >= '0' && ch <= '7') - { /* octal */ - num = ch - '0'; - for (i = 0; i < 2; i++, s++) - { - ch = *s; - if (ch < '0' || ch > '7') - break; - num = num * 8 + ch - '0'; - } - ch = (char)num; - } - else if (ch == 'a') - ch = C0_BEL; /* bell */ - else if (ch == 'b') - ch = C0_BS; /* backspace */ - else if (ch == 'E' || ch == 'e') - ch = C0_ESC; /* escape */ - else if (ch == 'n') - ch = '\n'; /* newline */ - else if (ch == 'r') - ch = '\r'; /* carriage-return */ - else if (ch == 't') - ch = C0_HT; /* tab */ - } - else if (ch == '^') - { - ch = *s++; - ch = toupper (ch); - ch = (ch == '?' ? 127 : (ch - '@')); - } - *d++ = ch; - } - - /* ESC] is an XTerm escape sequence, must be terminated */ - if (*str == '\0' && str[1] == C0_ESC && str[2] == ']') - append = CHAR_ST; - - /* add trailing character as required */ - if (append && d[-1] != append) - *d++ = append; - *d = '\0'; - - return (d - str); -} - -/* - * Split a comma-separated string into an array, stripping leading and + * Split a string into an array based on the given delimiter, stripping leading and * trailing spaces from each entry. Empty strings are properly returned - * Caller should free each entry and array when done */ -char ** -rxvt_splitcommastring (const char *cs) NOTHROW +char ** +rxvt_strsplit (char delim, const char *str) noexcept { - int l, n, p; - const char *s, *t; - char **ret; + int l, n; + char *s, *t; + char **ret; - if ((s = cs) == NULL) - s = ""; + s = strdup (str ? str : ""); for (n = 1, t = s; *t; t++) - if (*t == ',') + if (*t == delim) n++; ret = (char **)malloc ((n + 1) * sizeof (char *)); @@ -406,116 +301,56 @@ 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][p] = '\0'; - rxvt_Str_trim (ret[l]); + for (; *t && *t != delim; t++) + ; + *t = '\0'; + ret[l] = s; + rxvt_strtrim (ret[l]); s = ++t; } return ret; } -void -rxvt_freecommastring (char **cs) NOTHROW +void * +rxvt_malloc (size_t size) { - for (int i = 0; cs[i]; ++i) - free (cs[i]); + void *p = malloc (size); - free (cs); -} + if (!p) + rxvt_fatal ("memory allocation failure. aborting.\n"); -/*----------------------------------------------------------------------* - * file searching - */ + return p; +} -#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 - */ -char * -rxvt_File_search_path (const char *pathlist, const char *file, const char *ext) NOTHROW +void * +rxvt_calloc (size_t number, size_t size) { - int maxpath, len; - const char *p, *path; - char name[256]; - - if (!access (file, R_OK)) /* found (plain name) in current directory */ - return strdup (file); - - /* semi-colon delimited */ - if ((p = strchr (file, ';'))) - len = (p - file); - else - len = strlen (file); - - /* leave room for an extra '/' and trailing '\0' */ - 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); - name[len] = '\0'; - - if (!access (name, R_OK)) - return strdup (name); - if (ext) - { - strcat (name, ext); - if (!access (name, R_OK)) - return strdup (name); - } - for (path = pathlist; path != NULL && *path != '\0'; path = p) - { - int n; + void *p = calloc (number, size); - /* colon delimited */ - if ((p = strchr (path, ':')) == NULL) - p = strchr (path, '\0'); + if (!p) + rxvt_fatal ("memory allocation failure. aborting.\n"); - n = (p - path); - if (*p != '\0') - p++; - - if (n > 0 && n <= maxpath) - { - strncpy (name, path, n); - if (name[n - 1] != '/') - name[n++] = '/'; - name[n] = '\0'; - strncat (name, file, len); - - if (!access (name, R_OK)) - return strdup (name); - if (ext) - { - strcat (name, ext); - if (!access (name, R_OK)) - return strdup (name); - } - } - } - return NULL; + return p; } -char * -rxvt_File_find (const char *file, const char *ext, const char *path) NOTHROW +void * +rxvt_realloc (void *ptr, size_t size) { - char *f; + void *p = realloc (ptr, size); - if (file == NULL || *file == '\0') - return NULL; + if (!p) + rxvt_fatal ("memory allocation failure. aborting.\n"); - f = rxvt_File_search_path (path, file, ext); - - return f; + return p; } -#endif - +KeySym +rxvt_XKeycodeToKeysym (Display *dpy, KeyCode code, int index) +{ + int size; + KeySym *mapping = XGetKeyboardMapping (dpy, code, 1, &size); + KeySym keysym = IN_RANGE_EXC (index, 0, size) ? mapping[index] : NoSymbol; + XFree (mapping); + return keysym; +}