-#ifndef to_upper
-# define to_upper(c) (islower(c) ? toupper(c) : (c))
-# define to_lower(c) (isupper(c) ? tolower(c) : (c))
-#endif
-
-#define ISOCTAL(c) ((c) >= '0' && (c) <= '7')
-#define OCTVALUE(c) ((c) - '0')
-
-#ifndef isxdigit
-# define isxdigit(c) (isdigit((c)) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
-#endif
-
-#define HEXVALUE(c) \
- ((c) >= 'a' && (c) <= 'f' ? (c)-'a'+10 : (c) >= 'A' && (c) <= 'F' ? (c)-'A'+10 : (c)-'0')
-
-/* Convert STRING by expanding the escape sequences specified by the
- ANSI C standard. If SAWC is non-null, recognize `\c' and use that
- as a string terminator. If we see \c, set *SAWC to 1 before
- returning. LEN is the length of STRING. FOR_ECHO is a flag that
- means, if non-zero, that we're translating a string for `echo -e',
- and therefore should not treat a single quote as a character that
- may be escaped with a backslash. */
-char *
-ansicstr (string, len, for_echo, sawc, rlen)
- char *string;
- int len, for_echo, *sawc, *rlen;
-{
- int c, temp;
- char *ret, *r, *s;
-
- if (string == 0 || *string == '\0')
- return ((char *)NULL);
-
- ret = xmalloc (len + 1);
- for (r = ret, s = string; s && *s; )
- {
- c = *s++;
- if (c != '\\' || *s == '\0')
- *r++ = c;
- else
- {
- switch (c = *s++)
- {
-#if defined (__STDC__)
- case 'a': c = '\a'; break;
- case 'v': c = '\v'; break;
-#else
- case 'a': c = '\007'; break;
- case 'v': c = (int) 0x0B; break;
-#endif
- case 'b': c = '\b'; break;
- case 'e': case 'E': /* ESC -- non-ANSI */
- c = '\033'; break;
- case 'f': c = '\f'; break;
- case 'n': c = '\n'; break;
- case 'r': c = '\r'; break;
- case 't': c = '\t'; break;
- case '0': case '1': case '2': case '3':
- case '4': case '5': case '6': case '7':
- for (temp = 2, c -= '0'; ISOCTAL (*s) && temp--; s++)
- c = (c * 8) + OCTVALUE (*s);
- break;
- case 'x': /* Hex digit -- non-ANSI */
- for (temp = 3, c = 0; isxdigit (*s) && temp--; s++)
- c = (c * 16) + HEXVALUE (*s);
- /* \x followed by non-hex digits is passed through unchanged */
- if (temp == 3)
- {
- *r++ = '\\';
- c = 'x';
- }
- break;
- case '\\':
- break;
- case '\'':
- if (for_echo)
- *r++ = '\\';
- break;
- case 'c':
- if (sawc)
- {
- *sawc = 1;
- *r = '\0';
- if (rlen)
- *rlen = r - ret;
- return ret;
- }
- default: *r++ = '\\'; break;
- }
- *r++ = c;
- }
- }
- *r = '\0';
- if (rlen)
- *rlen = r - ret;
- return ret;
-}
-