aboutsummaryrefslogtreecommitdiffstats
path: root/pw.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-04-28 07:03:21 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-04-28 07:03:21 -0700
commit7ab92ff5a136e5ba0ba2bdba969c3e80cab9ffd4 (patch)
treec5132353d3143cf5cfb0841b560afd804674e9b3 /pw.c
parent00a1977ad6e28ddfc59064accba0801e1627915b (diff)
downloadpw-7ab92ff5a136e5ba0ba2bdba969c3e80cab9ffd4.tar.gz
pw-7ab92ff5a136e5ba0ba2bdba969c3e80cab9ffd4.tar.bz2
pw-7ab92ff5a136e5ba0ba2bdba969c3e80cab9ffd4.zip
Use constants and macros for control chars.
Diffstat (limited to 'pw.c')
-rw-r--r--pw.c22
1 files 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 <errno.h>
#include <regex.h>
+#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]))