From 7ab92ff5a136e5ba0ba2bdba969c3e80cab9ffd4 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 28 Apr 2022 07:03:21 -0700 Subject: Use constants and macros for control chars. --- pw.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pw.c b/pw.c index 8aa8cf9..f4d7ad1 100644 --- a/pw.c +++ b/pw.c @@ -15,6 +15,12 @@ #include #include +#define ctrl(ch) ((ch) & 0x1f) +#define BS 8 +#define CR 13 +#define ESC 27 +#define DEL 127 + enum status_flags { stat_dirty = 1, // display needs refresh stat_eof = 2, // end of data reached @@ -137,7 +143,7 @@ static char *addch(char *line, int ch) static char *addchesc(char *line, int ch) { - if (ch == 127) { + if (ch == DEL) { line = addch(line, '^'); line = addch(line, '?'); } else if (ch < 32) { @@ -636,11 +642,11 @@ int main(int argc, char **argv) if ((stat & stat_eof) == 0) stat |= (stat_dirty | stat_susp); break; - case 13: + case CR: stat &= ~stat_susp; stat |= stat_dirty; break; - case 27: + case ESC: kbd_state = kbd_esc; break; case ':': @@ -693,9 +699,9 @@ int main(int argc, char **argv) // fallthrough case kbd_colon: switch (ch) { - case 27: case 13: case 3: + case ESC: case CR: case ctrl('c'): stat |= stat_dirty; - if (ch == 13) { + if (ch == CR) { if (kbd_state == kbd_colon && cmdbuf[1]) { execute(cmdbuf, &stat); if (cmdbuf[0] != 0) { @@ -722,7 +728,7 @@ int main(int argc, char **argv) kbd_state = kbd_cmd; curcmd = 0; break; - case 8: case 127: + case BS: case DEL: { size_t len = strlen(cmdbuf); if (len == 1) { @@ -734,10 +740,10 @@ int main(int argc, char **argv) } } break; - case 21: + case ctrl('u'): cmdbuf[1] = 0; break; - case 23: + case ctrl('w'): { size_t len = strlen(cmdbuf); while (len > 1 && isspace((unsigned char) cmdbuf[len - 1])) -- cgit v1.2.3