--- rxvt-unicode/src/misc.C 2007/09/03 07:31:12 1.44 +++ rxvt-unicode/src/misc.C 2009/04/26 01:59:53 1.53 @@ -40,7 +40,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 +84,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 +121,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 +149,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,8 +167,8 @@ return r; } -char * -rxvt_r_basename (const char *str) NOTHROW +const char * +rxvt_basename (const char *str) NOTHROW { char *base = strrchr (str, '/'); @@ -237,8 +240,8 @@ /* * remove leading/trailing space in place. */ -char * -rxvt_Str_trim (char *str) NOTHROW +char * +rxvt_strtrim (char *str) NOTHROW { char *r, *s; @@ -262,96 +265,10 @@ } /* - * 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 * trailing spaces from each entry. Empty strings are properly returned - * Caller should free each entry and array when done */ -char ** +char ** rxvt_splitcommastring (const char *cs) NOTHROW { int l, n, p; @@ -373,9 +290,9 @@ for ( ; *t && *t != ','; t++) ; p = t - s; ret[l] = (char *)malloc (p + 1); - strncpy (ret[l], s, p); + memcpy (ret[l], s, p); ret[l][p] = '\0'; - rxvt_Str_trim (ret[l]); + rxvt_strtrim (ret[l]); s = ++t; } @@ -391,5 +308,35 @@ free (cs); } +void * +rxvt_malloc (size_t size) +{ + void *p = malloc (size); + + if (!p) + rxvt_fatal ("memory allocation failure. aborting.\n"); + return p; +} +void * +rxvt_calloc (size_t number, size_t size) +{ + void *p = calloc (number, size); + + if (!p) + rxvt_fatal ("memory allocation failure. aborting.\n"); + + return p; +} + +void * +rxvt_realloc (void *ptr, size_t size) +{ + void *p = realloc (ptr, size); + + if (!p) + rxvt_fatal ("memory allocation failure. aborting.\n"); + + return p; +}