From c8a677e3d979c6412772a499cfeae83a7dceeb1c Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Sat, 28 May 2016 08:52:12 +0200 Subject: [PATCH] Generate baudrate switch cases based on detection Support a single source of baud rate configuration as discussed in https://github.com/tio/tio/issues/45 . To do so, autogeneration of the switch cases which do the baud rate option value check and configuration/conversion in tty_configure() is introduced via a single macro. Just to be safe, this change also enables configure detection of all baud rates, including the ones previously assumed supported by most/all systems (POSIX). --- configure.ac | 14 +++++----- src/tty.c | 78 ++++++++++------------------------------------------ 2 files changed, 22 insertions(+), 70 deletions(-) diff --git a/configure.ac b/configure.ac index 1df5908..e674b0f 100644 --- a/configure.ac +++ b/configure.ac @@ -30,14 +30,11 @@ AM_CONDITIONAL([ENABLE_BASH_COMPLETION],[test "x$with_bash_completion_dir" != "x AC_DEFUN( [TIO_CHECK_BAUDRATE], [ - tio_have_decl=0 - AS_IF([test $1 -le 38400], - # Baud rates up to 38400 are defined by POSIX, - # so we don't have to check for them. - [tio_have_decl=1], - [AC_CHECK_DECLS([B$1], [tio_have_decl=1], [], [[#include ]])] + AC_CHECK_DECL([B$1], [tio_have_decl=1], [tio_have_decl=0], [[#include ]]) + AS_IF([test $tio_have_decl = 1], [ + AC_SUBST([BAUDRATES], ["$BAUDRATES $1"]) + AC_SUBST([BAUDRATE_CASES], ["$BAUDRATE_CASES case $1: baudrate = B$1; break;"])] ) - AS_IF([test $tio_have_decl = 1], [AC_SUBST([BAUDRATES], ["$BAUDRATES $1"])]) ] ) @@ -49,6 +46,7 @@ AC_DEFUN( # Check for available terminal I/O speeds BAUDRATES= +BAUDRATE_CASES= TIO_CHECK_BAUDRATES( 0, 50, @@ -83,6 +81,8 @@ TIO_CHECK_BAUDRATES( 4000000 ) +AC_DEFINE_UNQUOTED([AUTOCONF_BAUDRATE_CASES],[$BAUDRATE_CASES],[Switch cases for detected baud rates]) + AC_CONFIG_FILES([Makefile]) AC_CONFIG_FILES([src/Makefile]) AC_CONFIG_FILES([src/bash-completion/tio]) diff --git a/src/tty.c b/src/tty.c index f70121a..f60d8da 100644 --- a/src/tty.c +++ b/src/tty.c @@ -163,70 +163,22 @@ void tty_configure(void) /* Set speed */ switch (option.baudrate) { - case 0: baudrate = B0; break; - case 50: baudrate = B50; break; - case 75: baudrate = B75; break; - case 110: baudrate = B110; break; - case 134: baudrate = B134; break; - case 150: baudrate = B150; break; - case 200: baudrate = B200; break; - case 300: baudrate = B300; break; - case 600: baudrate = B600; break; - case 1200: baudrate = B1200; break; - case 1800: baudrate = B1800; break; - case 2400: baudrate = B2400; break; - case 4800: baudrate = B4800; break; - case 9600: baudrate = B9600; break; - case 19200: baudrate = B19200; break; - case 38400: baudrate = B38400; break; -#if HAVE_DECL_B57600 - case 57600: baudrate = B57600; break; -#endif -#if HAVE_DECL_B115200 - case 115200: baudrate = B115200; break; -#endif -#if HAVE_DECL_B230400 - case 230400: baudrate = B230400; break; -#endif -#if HAVE_DECL_B460800 - case 460800: baudrate = B460800; break; -#endif -#if HAVE_DECL_B500000 - case 500000: baudrate = B500000; break; -#endif -#if HAVE_DECL_B576000 - case 576000: baudrate = B576000; break; -#endif -#if HAVE_DECL_B921600 - case 921600: baudrate = B921600; break; -#endif -#if HAVE_DECL_B1000000 - case 1000000: baudrate = B1000000; break; -#endif -#if HAVE_DECL_B1152000 - case 1152000: baudrate = B1152000; break; -#endif -#if HAVE_DECL_B1500000 - case 1500000: baudrate = B1500000; break; -#endif -#if HAVE_DECL_B2000000 - case 2000000: baudrate = B2000000; break; -#endif -#if HAVE_DECL_B2500000 - case 2500000: baudrate = B2500000; break; -#endif -#if HAVE_DECL_B3000000 - case 3000000: baudrate = B3000000; break; -#endif -#if HAVE_DECL_B3500000 - case 3500000: baudrate = B3500000; break; -#endif -#if HAVE_DECL_B4000000 - case 4000000: baudrate = B4000000; break; -#endif + /* The macro below expands into switch cases autogenerated by the + * configure script. Each switch case verifies and configures the baud + * rate and is of the form: + * + * case $baudrate: baudrate = B$baudrate; break; + * + * Only switch cases for baud rates detected supported by the host + * system are inserted. + * + * To see which baud rates are being probed see configure.ac + */ + AUTOCONF_BAUDRATE_CASES + default: - error_printf("Invalid baud rate"); - exit(EXIT_FAILURE); + error_printf("Invalid baud rate"); + exit(EXIT_FAILURE); } cfsetispeed(&tio, baudrate);