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).
This commit is contained in:
Martin Lund 2016-05-28 08:52:12 +02:00
parent ab4c4fb151
commit c8a677e3d9
2 changed files with 22 additions and 70 deletions

View file

@ -30,14 +30,11 @@ AM_CONDITIONAL([ENABLE_BASH_COMPLETION],[test "x$with_bash_completion_dir" != "x
AC_DEFUN( AC_DEFUN(
[TIO_CHECK_BAUDRATE], [TIO_CHECK_BAUDRATE],
[ [
tio_have_decl=0 AC_CHECK_DECL([B$1], [tio_have_decl=1], [tio_have_decl=0], [[#include <termios.h>]])
AS_IF([test $1 -le 38400], AS_IF([test $tio_have_decl = 1], [
# Baud rates up to 38400 are defined by POSIX, AC_SUBST([BAUDRATES], ["$BAUDRATES $1"])
# so we don't have to check for them. AC_SUBST([BAUDRATE_CASES], ["$BAUDRATE_CASES case $1: baudrate = B$1; break;"])]
[tio_have_decl=1],
[AC_CHECK_DECLS([B$1], [tio_have_decl=1], [], [[#include <termios.h>]])]
) )
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 # Check for available terminal I/O speeds
BAUDRATES= BAUDRATES=
BAUDRATE_CASES=
TIO_CHECK_BAUDRATES( TIO_CHECK_BAUDRATES(
0, 0,
50, 50,
@ -83,6 +81,8 @@ TIO_CHECK_BAUDRATES(
4000000 4000000
) )
AC_DEFINE_UNQUOTED([AUTOCONF_BAUDRATE_CASES],[$BAUDRATE_CASES],[Switch cases for detected baud rates])
AC_CONFIG_FILES([Makefile]) AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([src/Makefile]) AC_CONFIG_FILES([src/Makefile])
AC_CONFIG_FILES([src/bash-completion/tio]) AC_CONFIG_FILES([src/bash-completion/tio])

View file

@ -163,70 +163,22 @@ void tty_configure(void)
/* Set speed */ /* Set speed */
switch (option.baudrate) switch (option.baudrate)
{ {
case 0: baudrate = B0; break; /* The macro below expands into switch cases autogenerated by the
case 50: baudrate = B50; break; * configure script. Each switch case verifies and configures the baud
case 75: baudrate = B75; break; * rate and is of the form:
case 110: baudrate = B110; break; *
case 134: baudrate = B134; break; * case $baudrate: baudrate = B$baudrate; break;
case 150: baudrate = B150; break; *
case 200: baudrate = B200; break; * Only switch cases for baud rates detected supported by the host
case 300: baudrate = B300; break; * system are inserted.
case 600: baudrate = B600; break; *
case 1200: baudrate = B1200; break; * To see which baud rates are being probed see configure.ac
case 1800: baudrate = B1800; break; */
case 2400: baudrate = B2400; break; AUTOCONF_BAUDRATE_CASES
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
default: default:
error_printf("Invalid baud rate"); error_printf("Invalid baud rate");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
cfsetispeed(&tio, baudrate); cfsetispeed(&tio, baudrate);