| 1 |
/* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 |
/* |
| 3 |
* Escape character print handling derived from procps |
| 4 |
* Copyright 1998-2002 by Albert Cahalan |
| 5 |
* Copyright 2020-2022 Jim Warner <[email protected]> |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <limits.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <string.h> |
| 12 |
#include <langinfo.h> |
| 13 |
|
| 14 |
#include "utils.h" |
| 15 |
|
| 16 |
static const char UTF_tab[] = { |
| 17 |
1, 1, 1, 1, 1, 1, 1, 1, |
| 18 |
1, 1, 1, 1, 1, 1, 1, 1, // 0x00 - 0x0F |
| 19 |
1, 1, 1, 1, 1, 1, 1, 1, |
| 20 |
1, 1, 1, 1, 1, 1, 1, 1, // 0x10 - 0x1F |
| 21 |
1, 1, 1, 1, 1, 1, 1, 1, |
| 22 |
1, 1, 1, 1, 1, 1, 1, 1, // 0x20 - 0x2F |
| 23 |
1, 1, 1, 1, 1, 1, 1, 1, |
| 24 |
1, 1, 1, 1, 1, 1, 1, 1, // 0x30 - 0x3F |
| 25 |
1, 1, 1, 1, 1, 1, 1, 1, |
| 26 |
1, 1, 1, 1, 1, 1, 1, 1, // 0x40 - 0x4F |
| 27 |
1, 1, 1, 1, 1, 1, 1, 1, |
| 28 |
1, 1, 1, 1, 1, 1, 1, 1, // 0x50 - 0x5F |
| 29 |
1, 1, 1, 1, 1, 1, 1, 1, |
| 30 |
1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F |
| 31 |
1, 1, 1, 1, 1, 1, 1, 1, |
| 32 |
1, 1, 1, 1, 1, 1, 1, 1, // 0x70 - 0x7F |
| 33 |
-1, -1, -1, -1, -1, -1, -1, -1, |
| 34 |
-1, -1, -1, -1, -1, -1, -1, -1, // 0x80 - 0x8F |
| 35 |
-1, -1, -1, -1, -1, -1, -1, -1, |
| 36 |
-1, -1, -1, -1, -1, -1, -1, -1, // 0x90 - 0x9F |
| 37 |
-1, -1, -1, -1, -1, -1, -1, -1, |
| 38 |
-1, -1, -1, -1, -1, -1, -1, -1, // 0xA0 - 0xAF |
| 39 |
-1, -1, -1, -1, -1, -1, -1, -1, |
| 40 |
-1, -1, -1, -1, -1, -1, -1, -1, // 0xB0 - 0xBF |
| 41 |
-1, -1, 2, 2, 2, 2, 2, 2, |
| 42 |
2, 2, 2, 2, 2, 2, 2, 2, // 0xC0 - 0xCF |
| 43 |
2, 2, 2, 2, 2, 2, 2, 2, |
| 44 |
2, 2, 2, 2, 2, 2, 2, 2, // 0xD0 - 0xDF |
| 45 |
3, 3, 3, 3, 3, 3, 3, 3, |
| 46 |
3, 3, 3, 3, 3, 3, 3, 3, // 0xE0 - 0xEF |
| 47 |
4, 4, 4, 4, 4, -1, -1, -1, |
| 48 |
-1, -1, -1, -1, -1, -1, -1, -1, // 0xF0 - 0xFF |
| 49 |
}; |
| 50 |
|
| 51 |
static const unsigned char ESC_tab[] = { |
| 52 |
"@..............................." // 0x00 - 0x1F |
| 53 |
"||||||||||||||||||||||||||||||||" // 0x20 - 0x3F |
| 54 |
"||||||||||||||||||||||||||||||||" // 0x40 - 0x5f |
| 55 |
"|||||||||||||||||||||||||||||||." // 0x60 - 0x7F |
| 56 |
"????????????????????????????????" // 0x80 - 0x9F |
| 57 |
"????????????????????????????????" // 0xA0 - 0xBF |
| 58 |
"????????????????????????????????" // 0xC0 - 0xDF |
| 59 |
"????????????????????????????????" // 0xE0 - 0xFF |
| 60 |
}; |
| 61 |
|
| 62 |
static void esc_all(unsigned char *str) |
| 63 |
{ |
| 64 |
// if bad locale/corrupt str, replace non-printing stuff |
| 65 |
while (*str) { |
| 66 |
unsigned char c = ESC_tab[*str]; |
| 67 |
|
| 68 |
if (c != '|') |
| 69 |
*str = c; |
| 70 |
++str; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static void esc_ctl(unsigned char *str, int len) |
| 75 |
{ |
| 76 |
int i; |
| 77 |
|
| 78 |
for (i = 0; i < len;) { |
| 79 |
// even with a proper locale, strings might be corrupt |
| 80 |
int n = UTF_tab[*str]; |
| 81 |
|
| 82 |
if (n < 0 || i + n > len) { |
| 83 |
esc_all(str); |
| 84 |
return; |
| 85 |
} |
| 86 |
// and eliminate those non-printing control characters |
| 87 |
if (*str < 0x20 || *str == 0x7f) |
| 88 |
*str = '?'; |
| 89 |
str += n; |
| 90 |
i += n; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
int escape_str(char *dst, const char *src, int bufsize) |
| 95 |
{ |
| 96 |
static int utf_sw; |
| 97 |
|
| 98 |
if (utf_sw == 0) { |
| 99 |
char *enc = nl_langinfo(CODESET); |
| 100 |
|
| 101 |
utf_sw = enc && strcasecmp(enc, "UTF-8") == 0 ? 1 : -1; |
| 102 |
} |
| 103 |
|
| 104 |
int n = strlcpy(dst, src, bufsize); |
| 105 |
|
| 106 |
if (utf_sw < 0) |
| 107 |
esc_all((unsigned char *)dst); |
| 108 |
else |
| 109 |
esc_ctl((unsigned char *)dst, n); |
| 110 |
return n; |
| 111 |
} |