diff options
author | Thomas Voss <mail@thomasvoss.com> | 2023-12-10 17:22:08 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2023-12-10 17:22:08 +0100 |
commit | 4ab5cfd1efd080b9c5d428679952ba70e7ea77de (patch) | |
tree | 6a7cda6b0ebcf62caf651faed2491fbca15c9389 | |
parent | 5030524170d2cb402c4ee0c8ba852fb8f064afd9 (diff) |
Support basic escape ASCII escape sequences
-rw-r--r-- | grab.c | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -69,6 +69,7 @@ static char *xstrchrnul(const char *, char); static char delim = '\n'; static int rv = EXIT_SUCCESS; + static const cmd_func op_table[UCHAR_MAX] = { ['g'] = cmdg, ['v'] = cmdg, @@ -76,6 +77,11 @@ static const cmd_func op_table[UCHAR_MAX] = { ['y'] = cmdy, }; +static const char esc_table[UCHAR_MAX] = { + ['\\'] = '\\', ['a'] = '\a', ['b'] = '\b', ['f'] = '\f', + ['n'] = '\n', ['r'] = '\r', ['t'] = '\t', ['v'] = '\v', +}; + static void usage(const char *s) { @@ -319,6 +325,18 @@ mkregex(char *s, size_t n) int ret; regex_t r; + for (size_t i = 0; i < n - 1; i++) { + if (s[i] == '\\') { + char c = esc_table[(uchar)s[i + 1]]; + if (c) { + for (size_t j = i; j < n - 1; j++) + s[j] = s[j + 1]; + s[i] = c; + n--; + } + } + } + s[n] = 0; if ((ret = regcomp(&r, s, REG_EXTENDED | REG_NEWLINE)) != 0) { char emsg[128]; |