if ((cells) <= 0) return 0; \
} while (0)
+static const unsigned char ESC_tab[] = {
+ "@..............................."
+ "||||||||||||||||||||||||||||||||"
+ "||||||||||||||||||||||||||||||||"
+ "|||||||||||||||||||||||||||||||."
+ "????????????????????????????????"
+ "????????????????????????????????"
+ "????????????????????????????????"
+ "????????????????????????????????"
+};
+
+
#if (__GNU_LIBRARY__ >= 6) && (!defined(__UCLIBC__) || defined(__UCLIBC_HAS_WCHAR__))
+
static int escape_str_utf8(char *restrict dst, const char *restrict src, int bufsize, int *maxcells){
int my_cells = 0;
int my_bytes = 0;
*maxcells -= my_cells;
return my_bytes; // bytes of text, excluding the NUL
}
-
#endif /* __GNU_LIBRARY__ */
+
/* sanitize a string via one-way mangle */
int escape_str(char *restrict dst, const char *restrict src, int bufsize, int *maxcells){
unsigned char c;
int my_cells = 0;
int my_bytes = 0;
- const char codes[] =
- "Z..............................."
- "||||||||||||||||||||||||||||||||"
- "||||||||||||||||||||||||||||||||"
- "|||||||||||||||||||||||||||||||."
- "????????????????????????????????"
- "????????????????????????????????"
- "????????????????????????????????"
- "????????????????????????????????";
#if (__GNU_LIBRARY__ >= 6) && (!defined(__UCLIBC__) || defined(__UCLIBC_HAS_WCHAR__))
static int utf_init=0;
break;
c = (unsigned char) *(src++);
if(!c) break;
- if(codes[c]!='|') c=codes[c];
+ if(ESC_tab[c]!='|') c=ESC_tab[c];
my_cells++;
my_bytes++;
*(dst++) = c;
// copy a string, but 'escape' any control characters
// using the traditional escape.h calling conventions
int escaped_copy(char *restrict dst, const char *restrict src, int bufsize, int *maxroom){
+ static int utf_sw = 0;
int i, n;
+ char c;
+ if(utf_sw == 0){
+ char *enc = nl_langinfo(CODESET);
+ utf_sw = enc && strcasecmp(enc, "UTF-8")==0 ? 1 : -1;
+ }
SECURE_ESCAPE_ARGS(dst, bufsize, *maxroom);
if (bufsize > *maxroom+1) bufsize = *maxroom+1;
}
if (n >= bufsize) n = bufsize-1;
- // control chars, especially tabs, create alignment problems for ps & top ...
- for (i = 0; i < n; i++)
- if ((unsigned char)dst[i] < 0x20 || dst[i] == 0x7f)
- dst[i] = '?';
-
+ if (utf_sw < 0) {
+ // if bad locale, replace the non-printing stuff
+ for (i = 0; i < n; i++)
+ if ((c = ESC_tab[(unsigned char)dst[i]]) != '|')
+ dst[i] = c;
+ } else {
+ // or eliminate those non-printing control chars
+ for (i = 0; i < n; i++)
+ if ((unsigned char)dst[i] < 0x20 || dst[i] == 0x7f)
+ dst[i] = '?';
+ }
*maxroom -= n;
return n;
}