sim_console: Use cfmakeraw() and add option for ctrl-c to exit sim
authorAnton Blanchard <anton@linux.ibm.com>
Wed, 22 Jan 2020 03:27:40 +0000 (14:27 +1100)
committerAnton Blanchard <anton@ozlabs.org>
Wed, 22 Jan 2020 03:34:20 +0000 (14:34 +1100)
No need to open code a bunch of random termios flags, use cfmakeraw().

Most of the time we want ctrl-c to exit the current simulation, so
make that the default.

Signed-off-by: Anton Blanchard <anton@linux.ibm.com>
sim_console_c.c

index 869c8962505a0b8f847c9a80a2a47cfe68babe42..20c1f11ee00b431f425992f26d9cca9a2abc3c19 100644 (file)
@@ -7,6 +7,8 @@
 #include <unistd.h>
 #include <poll.h>
 
+/* Should we exit simulation on ctrl-c or pass it through? */
+#define EXIT_ON_CTRL_C
 
 #define vhpi0  2       /* forcing 0 */
 #define vhpi1  3       /* forcing 1 */
@@ -59,12 +61,12 @@ static void to_std_logic_vector(unsigned long val, unsigned char *p,
 
 static struct termios oldt;
 
-static void restore_termios(void)
+static void disable_raw_mode(void)
 {
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
 }
 
-static void nonblocking(void)
+static void enable_raw_mode(void)
 {
        static bool initialized = false;
 
@@ -73,16 +75,13 @@ static void nonblocking(void)
 
                tcgetattr(STDIN_FILENO, &oldt);
                newt = oldt;
-               newt.c_lflag &= ~(ICANON|ECHO);
-
-               newt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
-               newt.c_oflag &= ~(OPOST);
-               newt.c_cflag |= (CS8);
-               newt.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
-
+               cfmakeraw(&newt);
+#ifdef EXIT_ON_CTRL_C
+               newt.c_lflag |= ISIG;
+#endif
                tcsetattr(STDIN_FILENO, TCSANOW, &newt);
                initialized = true;
-               atexit(restore_termios);
+               atexit(disable_raw_mode);
        }
 }
 
@@ -91,7 +90,7 @@ void sim_console_read(unsigned char *__rt)
        int ret;
        unsigned long val = 0;
 
-       nonblocking();
+       enable_raw_mode();
 
        ret = read(STDIN_FILENO, &val, 1);
        if (ret != 1) {
@@ -110,7 +109,7 @@ void sim_console_poll(unsigned char *__rt)
        struct pollfd fdset[1];
        uint8_t val = 0;
 
-       nonblocking();
+       enable_raw_mode();
 
        memset(fdset, 0, sizeof(fdset));