diff options
author | Jefferson Delfes <jefferson.delfes@gmail.com> | 2013-11-01 11:40:53 -0400 |
---|---|---|
committer | Jefferson Delfes <jefferson.delfes@gmail.com> | 2013-11-04 09:35:34 -0400 |
commit | 345e88e7f323c4527de88dd9167aac89f38fd40f (patch) | |
tree | dc37a31bec76616c7355f76bab88aa0084cf60f8 | |
parent | be59240e3b9bfcfdba80b21e6ff27c16a53a3620 (diff) |
rl_helper: add ctrl-d parsing
When we press ctrl-d, the terminal send us an end-of-transmission
command. So we can exit from program.
-rw-r--r-- | btctl.c | 4 | ||||
-rw-r--r-- | rl_helper.c | 12 | ||||
-rw-r--r-- | rl_helper.h | 4 |
3 files changed, 14 insertions, 6 deletions
@@ -1200,8 +1200,8 @@ int main (int argc, char * argv[]) { c == 'Y' ? true : false, 0); } change_prompt_state(NORMAL_PSTATE); - } else - rl_feed(c); + } else if (!rl_feed(c)) + break; /* user pressed ctrl-d */ } /* Disable adapter on exit */ diff --git a/rl_helper.c b/rl_helper.c index f34639d..b066600 100644 --- a/rl_helper.c +++ b/rl_helper.c @@ -21,7 +21,9 @@ #include <ctype.h> #include <stdarg.h> +#include <stdbool.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <termios.h> #include "rl_helper.h" @@ -37,6 +39,7 @@ }) typedef enum { + K_EOT = 0x04, /* end-of-transmission (ctrl-d) */ K_TAB = 0x09, K_ESC = 0x1b, K_BACKSPACE = 0x7f, @@ -196,12 +199,15 @@ int rl_parse_seq(int *c) { } } -void rl_feed(int c) { +bool rl_feed(int c) { if (rl_parse_seq(&c)) - return; + return true; switch (c) { + case K_EOT: + putchar('\n'); + return false; case K_TAB: if (tab_completer_cb) { const char *str = tab_completer_cb(lnbuf, pos); @@ -272,6 +278,8 @@ void rl_feed(int c) { printf(" %x ", c); break; } + + return true; } void rl_printf(const char *fmt, ...) { diff --git a/rl_helper.h b/rl_helper.h index 756a79a..beeed8c 100644 --- a/rl_helper.h +++ b/rl_helper.h @@ -13,8 +13,8 @@ void rl_set_prompt(const char *str); void rl_set_tab_completer(tab_completer_callback cb); /* close resources */ void rl_quit(); -/* add char to line buffer */ -void rl_feed(int c); +/* add char to line buffer, returns false on ctrl-d */ +bool rl_feed(int c); /* printf version */ void rl_printf(const char *fmt, ...); |