diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-07-13 11:07:05 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-07-13 16:01:00 -0400 |
commit | 36fed99f5929782b61018d2d2e5a2800fba5e530 (patch) | |
tree | e866181499ef7df2e7d8b417873252fcdb876ceb /src | |
parent | 2cb1ae0b2ecc356706318948839cc80def53fb5e (diff) | |
download | bfs-36fed99f5929782b61018d2d2e5a2800fba5e530.tar.xz |
bfstd: Use $'\n' etc. over $'\x0A'
Diffstat (limited to 'src')
-rw-r--r-- | src/bfstd.c | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/src/bfstd.c b/src/bfstd.c index e338831..e125be6 100644 --- a/src/bfstd.c +++ b/src/bfstd.c @@ -619,6 +619,31 @@ static size_t unprintable_len(const char *str, size_t len) { return cur - str; } +/** Convert a special char into a well-known escape sequence like "\n". */ +static const char *c_esc(char c) { + // https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html + switch (c) { + case '\a': + return "\\a"; + case '\b': + return "\\b"; + case '\033': + return "\\e"; + case '\f': + return "\\f"; + case '\n': + return "\\n"; + case '\r': + return "\\r"; + case '\t': + return "\\t"; + case '\v': + return "\\v"; + default: + return NULL; + } +} + char *wordesc(const char *str) { size_t len = strlen(str); @@ -666,11 +691,16 @@ char *wordesc(const char *str) { cur = xstpecpy(cur, end, "$'"); size_t uplen = unprintable_len(str, len); for (size_t i = 0; i < uplen; ++i) { - static const char *hex[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; - unsigned char byte = str[i]; - cur = xstpecpy(cur, end, "\\x"); - cur = xstpecpy(cur, end, hex[byte / 0x10]); - cur = xstpecpy(cur, end, hex[byte % 0x10]); + const char *esc = c_esc(str[i]); + if (esc) { + cur = xstpecpy(cur, end, esc); + } else { + static const char *hex[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; + unsigned char byte = str[i]; + cur = xstpecpy(cur, end, "\\x"); + cur = xstpecpy(cur, end, hex[byte / 0x10]); + cur = xstpecpy(cur, end, hex[byte % 0x10]); + } } cur = xstpecpy(cur, end, "'"); |