From 1daa008ff3d070022c1096bdeb8d6cd3df115e09 Mon Sep 17 00:00:00 2001 From: Jakob Haufe Date: Fri, 3 Jun 2016 00:14:19 +0200 Subject: [PATCH] Imported Upstream version 1.18 --- AUTHORS | 1 + ChangeLog | 128 ++++++- Makefile.in | 7 +- README | 6 +- configure | 736 ++++++++++++++++++++++++++++++++++++- configure.ac | 70 +++- man/Makefile.in | 2 + man/tio.1 | 13 +- src/Makefile.am | 4 +- src/Makefile.in | 2 + src/bash-completion/tio | 30 +- src/bash-completion/tio.in | 76 ++++ src/error.c | 1 + src/include/config.h.in | 7 + src/include/tio/options.h | 11 +- src/include/tio/tty.h | 9 +- src/log.c | 1 + src/main.c | 12 +- src/options.c | 224 ++--------- src/time.c | 1 + src/tty.c | 251 ++++++++++--- 21 files changed, 1274 insertions(+), 318 deletions(-) create mode 100644 src/bash-completion/tio.in diff --git a/AUTHORS b/AUTHORS index cf76a65..1c6fb9c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -7,5 +7,6 @@ Jeppe Ledet-Pedersen Jakob Haufe Jakub Wilk Martin Hundeboll +Nick Østergaard Thanks to everyone who has contributed to this project. diff --git a/ChangeLog b/ChangeLog index ee4a8d6..7f41639 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,130 @@ -=== tio v1.14 === + +=== tio v1.18 === + +Changes since tio v1.17: + + * Updated man page + + * Added support for non-standard baud rates + + Only enabled when possible, that is, when the BOTHER definition is + available. + + It is untested but it should work as described here: + https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=683826 + + Some Cypress USB<->serial devices supposedly supports arbitrary speeds. + + * 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). + + * Minor cleanup + + * Exit when not a tty device in autoconnect mode + +Jakub Wilk: + + * Added non-standard baud rates that are defined on FreeBSD + + * Capitalized "GitHub" in README + + + +Changes since tio v1.16: + + * Compacted tty_configure() a bit + + * Fixed automatic baud rate enablement + + * Minor cleanups + + * Added autodetection of available baud rates + + Various platforms support different baud rates. + + To avoid adding platform specific handling generic baud rate detection + tests are introduced in the configure script. Successfully detected baud + rates are automatically enabled. This applies to both the C code and the + bash completion script. + + Note: + Baud rates below 57600 are defined by POSIX-1 and supported by most + platforms so only baud rate 57600 and above are tested. + + * Updated bash-completion + + * Fixed printf() format type + + * Added Travis build configuration + +Jakub Wilk: + + * Generated bash completion at configure time + + * Reduce code duplication in baud rate detection + + * Add support for baud rates 200 and 1800 + + * Fixed baudrate type + + + +Changes since tio v1.15: + + * Updated man page + + * Updated README + + * Removed obsolete packaging files + + * Removed use of deprecated bzero() + + + +Changes since tio v1.14: + + * Removed + to remove potential confusion + + * Added input digit checks + + * Fixed license string + + * Introduced tty_configure() + + Moved tty configuration actions to tty_configure() in tty.c. This way + options.c is strictly about parsing options nothing else. + + * Function names cleanup + + * Updated AUTHORS file + + Added Nick who created the new tio package for Arch Linux. + + * Fixed tx/rx counters type + +Jakob Haufe: + + * Include config.h before standard headers + + This makes use of 8d6d202 (Enable large file support) for real. + +Jakub Wilk: + + * Fixed printf directives for tx/rx counters + + In 9a66de0affda, types of tx/rx counters were changed from "long" to + "unsigned long", but their printf directives remained "%ld". + Change them to "%lu" to match the actual types. diff --git a/Makefile.in b/Makefile.in index 4f73e0c..44e7ced 100644 --- a/Makefile.in +++ b/Makefile.in @@ -96,7 +96,7 @@ am__CONFIG_DISTCLEAN_FILES = config.status config.cache config.log \ configure.lineno config.status.lineno mkinstalldirs = $(install_sh) -d CONFIG_HEADER = $(top_builddir)/src/include/config.h -CONFIG_CLEAN_FILES = +CONFIG_CLEAN_FILES = src/bash-completion/tio CONFIG_CLEAN_VPATH_FILES = AM_V_P = $(am__v_P_@AM_V@) am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) @@ -155,6 +155,7 @@ CTAGS = ctags CSCOPE = cscope DIST_SUBDIRS = $(SUBDIRS) am__DIST_COMMON = $(srcdir)/Makefile.in \ + $(top_srcdir)/src/bash-completion/tio.in \ $(top_srcdir)/src/include/config.h.in AUTHORS COPYING \ ChangeLog INSTALL README compile depcomp install-sh missing DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) @@ -209,6 +210,8 @@ AWK = @AWK@ BASH_COMPLETION_CFLAGS = @BASH_COMPLETION_CFLAGS@ BASH_COMPLETION_DIR = @BASH_COMPLETION_DIR@ BASH_COMPLETION_LIBS = @BASH_COMPLETION_LIBS@ +BAUDRATES = @BAUDRATES@ +BAUDRATE_CASES = @BAUDRATE_CASES@ CC = @CC@ CCDEPMODE = @CCDEPMODE@ CFLAGS = @CFLAGS@ @@ -342,6 +345,8 @@ $(top_srcdir)/src/include/config.h.in: $(am__configure_deps) distclean-hdr: -rm -f src/include/config.h src/include/stamp-h1 +src/bash-completion/tio: $(top_builddir)/config.status $(top_srcdir)/src/bash-completion/tio.in + cd $(top_builddir) && $(SHELL) ./config.status $@ # This directory's subdirectories are mostly independent; you can cd # into them and run 'make' without going through this Makefile. diff --git a/README b/README index 79e563a..7438f11 100644 --- a/README +++ b/README @@ -31,7 +31,7 @@ -v, --version Display version -h, --help Display help - In session, press ctrl-t + q to quit. + In session, press ctrl-t q to quit. The only option which requires a bit of elaboration is perhaps the @@ -52,7 +52,7 @@ Find the latest release tarball at https://tio.github.io - The latest source is available on github: https://github.com/tio/tio + The latest source is available on GitHub: https://github.com/tio/tio 4. Installation @@ -74,7 +74,7 @@ 6. Support - Submit bug reports on github: https://github.com/tio/tio/issues + Submit bug reports on GitHub: https://github.com/tio/tio/issues 7. License diff --git a/configure b/configure index 96758e1..ab01973 100755 --- a/configure +++ b/configure @@ -1,6 +1,6 @@ #! /bin/sh # Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.69 for tio 1.14. +# Generated by GNU Autoconf 2.69 for tio 1.18. # # # Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc. @@ -576,8 +576,8 @@ MAKEFLAGS= # Identity of this package. PACKAGE_NAME='tio' PACKAGE_TARNAME='tio' -PACKAGE_VERSION='1.14' -PACKAGE_STRING='tio 1.14' +PACKAGE_VERSION='1.18' +PACKAGE_STRING='tio 1.18' PACKAGE_BUGREPORT='' PACKAGE_URL='https://tio.github.io' @@ -585,6 +585,8 @@ ac_subst_vars='am__EXEEXT_FALSE am__EXEEXT_TRUE LTLIBOBJS LIBOBJS +BAUDRATE_CASES +BAUDRATES ENABLE_BASH_COMPLETION_FALSE ENABLE_BASH_COMPLETION_TRUE BASH_COMPLETION_DIR @@ -1247,7 +1249,7 @@ if test "$ac_init_help" = "long"; then # Omit some internal or obsolete options to make the list less imposing. # This message is too long to be a string in the A/UX 3.1 sh. cat <<_ACEOF -\`configure' configures tio 1.14 to adapt to many kinds of systems. +\`configure' configures tio 1.18 to adapt to many kinds of systems. Usage: $0 [OPTION]... [VAR=VALUE]... @@ -1314,7 +1316,7 @@ fi if test -n "$ac_init_help"; then case $ac_init_help in - short | recursive ) echo "Configuration of tio 1.14:";; + short | recursive ) echo "Configuration of tio 1.18:";; esac cat <<\_ACEOF @@ -1422,7 +1424,7 @@ fi test -n "$ac_init_help" && exit $ac_status if $ac_init_version; then cat <<\_ACEOF -tio configure 1.14 +tio configure 1.18 generated by GNU Autoconf 2.69 Copyright (C) 2012 Free Software Foundation, Inc. @@ -1473,11 +1475,57 @@ fi as_fn_set_status $ac_retval } # ac_fn_c_try_compile + +# ac_fn_c_check_decl LINENO SYMBOL VAR INCLUDES +# --------------------------------------------- +# Tests whether SYMBOL is declared in INCLUDES, setting cache variable VAR +# accordingly. +ac_fn_c_check_decl () +{ + as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack + as_decl_name=`echo $2|sed 's/ *(.*//'` + as_decl_use=`echo $2|sed -e 's/(/((/' -e 's/)/) 0&/' -e 's/,/) 0& (/g'` + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $as_decl_name is declared" >&5 +$as_echo_n "checking whether $as_decl_name is declared... " >&6; } +if eval \${$3+:} false; then : + $as_echo_n "(cached) " >&6 +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +$4 +int +main () +{ +#ifndef $as_decl_name +#ifdef __cplusplus + (void) $as_decl_use; +#else + (void) $as_decl_name; +#endif +#endif + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + eval "$3=yes" +else + eval "$3=no" +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext +fi +eval ac_res=\$$3 + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 +$as_echo "$ac_res" >&6; } + eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno + +} # ac_fn_c_check_decl cat >config.log <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. -It was created by tio $as_me 1.14, which was +It was created by tio $as_me 1.18, which was generated by GNU Autoconf 2.69. Invocation command line was $ $0 $@ @@ -2342,7 +2390,7 @@ fi # Define the identity of the package. PACKAGE='tio' - VERSION='1.14' + VERSION='1.18' cat >>confdefs.h <<_ACEOF @@ -3728,7 +3776,6 @@ fi - if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then if test -n "$ac_tool_prefix"; then # Extract the first word of "${ac_tool_prefix}pkg-config", so it can be a program name with args. @@ -3843,6 +3890,8 @@ $as_echo "no" >&6; } fi fi +# Handle bash completion + # Check whether --with-bash-completion-dir was given. if test "${with_bash_completion_dir+set}" = set; then : withval=$with_bash_completion_dir; @@ -3936,10 +3985,674 @@ else fi +# TIO_CHECK_BAUDRATE(N) + + +# TIO_CHECK_BAUDRATES(N1, N2, ...) + + +# Check for available terminal I/O speeds +BAUDRATES= +BAUDRATE_CASES= + + ac_fn_c_check_decl "$LINENO" "B0" "ac_cv_have_decl_B0" "#include +" +if test "x$ac_cv_have_decl_B0" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 0" + + BAUDRATE_CASES="$BAUDRATE_CASES case 0: baudrate = B0; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B50" "ac_cv_have_decl_B50" "#include +" +if test "x$ac_cv_have_decl_B50" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 50" + + BAUDRATE_CASES="$BAUDRATE_CASES case 50: baudrate = B50; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B75" "ac_cv_have_decl_B75" "#include +" +if test "x$ac_cv_have_decl_B75" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 75" + + BAUDRATE_CASES="$BAUDRATE_CASES case 75: baudrate = B75; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B110" "ac_cv_have_decl_B110" "#include +" +if test "x$ac_cv_have_decl_B110" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 110" + + BAUDRATE_CASES="$BAUDRATE_CASES case 110: baudrate = B110; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B134" "ac_cv_have_decl_B134" "#include +" +if test "x$ac_cv_have_decl_B134" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 134" + + BAUDRATE_CASES="$BAUDRATE_CASES case 134: baudrate = B134; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B150" "ac_cv_have_decl_B150" "#include +" +if test "x$ac_cv_have_decl_B150" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 150" + + BAUDRATE_CASES="$BAUDRATE_CASES case 150: baudrate = B150; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B200" "ac_cv_have_decl_B200" "#include +" +if test "x$ac_cv_have_decl_B200" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 200" + + BAUDRATE_CASES="$BAUDRATE_CASES case 200: baudrate = B200; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B300" "ac_cv_have_decl_B300" "#include +" +if test "x$ac_cv_have_decl_B300" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 300" + + BAUDRATE_CASES="$BAUDRATE_CASES case 300: baudrate = B300; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B600" "ac_cv_have_decl_B600" "#include +" +if test "x$ac_cv_have_decl_B600" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 600" + + BAUDRATE_CASES="$BAUDRATE_CASES case 600: baudrate = B600; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B1200" "ac_cv_have_decl_B1200" "#include +" +if test "x$ac_cv_have_decl_B1200" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 1200" + + BAUDRATE_CASES="$BAUDRATE_CASES case 1200: baudrate = B1200; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B1800" "ac_cv_have_decl_B1800" "#include +" +if test "x$ac_cv_have_decl_B1800" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 1800" + + BAUDRATE_CASES="$BAUDRATE_CASES case 1800: baudrate = B1800; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B2400" "ac_cv_have_decl_B2400" "#include +" +if test "x$ac_cv_have_decl_B2400" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 2400" + + BAUDRATE_CASES="$BAUDRATE_CASES case 2400: baudrate = B2400; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B4800" "ac_cv_have_decl_B4800" "#include +" +if test "x$ac_cv_have_decl_B4800" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 4800" + + BAUDRATE_CASES="$BAUDRATE_CASES case 4800: baudrate = B4800; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B7200" "ac_cv_have_decl_B7200" "#include +" +if test "x$ac_cv_have_decl_B7200" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 7200" + + BAUDRATE_CASES="$BAUDRATE_CASES case 7200: baudrate = B7200; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B9600" "ac_cv_have_decl_B9600" "#include +" +if test "x$ac_cv_have_decl_B9600" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 9600" + + BAUDRATE_CASES="$BAUDRATE_CASES case 9600: baudrate = B9600; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B14400" "ac_cv_have_decl_B14400" "#include +" +if test "x$ac_cv_have_decl_B14400" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 14400" + + BAUDRATE_CASES="$BAUDRATE_CASES case 14400: baudrate = B14400; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B19200" "ac_cv_have_decl_B19200" "#include +" +if test "x$ac_cv_have_decl_B19200" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 19200" + + BAUDRATE_CASES="$BAUDRATE_CASES case 19200: baudrate = B19200; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B28800" "ac_cv_have_decl_B28800" "#include +" +if test "x$ac_cv_have_decl_B28800" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 28800" + + BAUDRATE_CASES="$BAUDRATE_CASES case 28800: baudrate = B28800; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B38400" "ac_cv_have_decl_B38400" "#include +" +if test "x$ac_cv_have_decl_B38400" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 38400" + + BAUDRATE_CASES="$BAUDRATE_CASES case 38400: baudrate = B38400; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B57600" "ac_cv_have_decl_B57600" "#include +" +if test "x$ac_cv_have_decl_B57600" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 57600" + + BAUDRATE_CASES="$BAUDRATE_CASES case 57600: baudrate = B57600; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B76800" "ac_cv_have_decl_B76800" "#include +" +if test "x$ac_cv_have_decl_B76800" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 76800" + + BAUDRATE_CASES="$BAUDRATE_CASES case 76800: baudrate = B76800; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B115200" "ac_cv_have_decl_B115200" "#include +" +if test "x$ac_cv_have_decl_B115200" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 115200" + + BAUDRATE_CASES="$BAUDRATE_CASES case 115200: baudrate = B115200; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B230400" "ac_cv_have_decl_B230400" "#include +" +if test "x$ac_cv_have_decl_B230400" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 230400" + + BAUDRATE_CASES="$BAUDRATE_CASES case 230400: baudrate = B230400; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B460800" "ac_cv_have_decl_B460800" "#include +" +if test "x$ac_cv_have_decl_B460800" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 460800" + + BAUDRATE_CASES="$BAUDRATE_CASES case 460800: baudrate = B460800; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B500000" "ac_cv_have_decl_B500000" "#include +" +if test "x$ac_cv_have_decl_B500000" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 500000" + + BAUDRATE_CASES="$BAUDRATE_CASES case 500000: baudrate = B500000; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B576000" "ac_cv_have_decl_B576000" "#include +" +if test "x$ac_cv_have_decl_B576000" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 576000" + + BAUDRATE_CASES="$BAUDRATE_CASES case 576000: baudrate = B576000; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B921600" "ac_cv_have_decl_B921600" "#include +" +if test "x$ac_cv_have_decl_B921600" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 921600" + + BAUDRATE_CASES="$BAUDRATE_CASES case 921600: baudrate = B921600; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B1000000" "ac_cv_have_decl_B1000000" "#include +" +if test "x$ac_cv_have_decl_B1000000" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 1000000" + + BAUDRATE_CASES="$BAUDRATE_CASES case 1000000: baudrate = B1000000; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B1152000" "ac_cv_have_decl_B1152000" "#include +" +if test "x$ac_cv_have_decl_B1152000" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 1152000" + + BAUDRATE_CASES="$BAUDRATE_CASES case 1152000: baudrate = B1152000; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B1500000" "ac_cv_have_decl_B1500000" "#include +" +if test "x$ac_cv_have_decl_B1500000" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 1500000" + + BAUDRATE_CASES="$BAUDRATE_CASES case 1500000: baudrate = B1500000; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B2000000" "ac_cv_have_decl_B2000000" "#include +" +if test "x$ac_cv_have_decl_B2000000" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 2000000" + + BAUDRATE_CASES="$BAUDRATE_CASES case 2000000: baudrate = B2000000; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B2500000" "ac_cv_have_decl_B2500000" "#include +" +if test "x$ac_cv_have_decl_B2500000" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 2500000" + + BAUDRATE_CASES="$BAUDRATE_CASES case 2500000: baudrate = B2500000; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B3000000" "ac_cv_have_decl_B3000000" "#include +" +if test "x$ac_cv_have_decl_B3000000" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 3000000" + + BAUDRATE_CASES="$BAUDRATE_CASES case 3000000: baudrate = B3000000; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B3500000" "ac_cv_have_decl_B3500000" "#include +" +if test "x$ac_cv_have_decl_B3500000" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 3500000" + + BAUDRATE_CASES="$BAUDRATE_CASES case 3500000: baudrate = B3500000; break;" + + +fi + + + ac_fn_c_check_decl "$LINENO" "B4000000" "ac_cv_have_decl_B4000000" "#include +" +if test "x$ac_cv_have_decl_B4000000" = xyes; then : + tio_have_decl=1 +else + tio_have_decl=0 +fi + + if test $tio_have_decl = 1; then : + + BAUDRATES="$BAUDRATES 4000000" + + BAUDRATE_CASES="$BAUDRATE_CASES case 4000000: baudrate = B4000000; break;" + + +fi + + + + + +cat >>confdefs.h <<_ACEOF +#define AUTOCONF_BAUDRATE_CASES $BAUDRATE_CASES +_ACEOF + + +# Check that it is possible to set arbitrary baud rates using BOTHER +ac_fn_c_check_decl "$LINENO" "BOTHER" "ac_cv_have_decl_BOTHER" "#include +" +if test "x$ac_cv_have_decl_BOTHER" = xyes; then : + ac_have_decl=1 +else + ac_have_decl=0 +fi + +cat >>confdefs.h <<_ACEOF +#define HAVE_DECL_BOTHER $ac_have_decl +_ACEOF + + ac_config_files="$ac_config_files Makefile" ac_config_files="$ac_config_files src/Makefile" +ac_config_files="$ac_config_files src/bash-completion/tio" + ac_config_files="$ac_config_files man/Makefile" cat >confcache <<\_ACEOF @@ -4476,7 +5189,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 # report actual input values of CONFIG_FILES etc. instead of their # values after options handling. ac_log=" -This file was extended by tio $as_me 1.14, which was +This file was extended by tio $as_me 1.18, which was generated by GNU Autoconf 2.69. Invocation command line was CONFIG_FILES = $CONFIG_FILES @@ -4543,7 +5256,7 @@ _ACEOF cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`" ac_cs_version="\\ -tio config.status 1.14 +tio config.status 1.18 configured by $0, generated by GNU Autoconf 2.69, with options \\"\$ac_cs_config\\" @@ -4676,6 +5389,7 @@ do "depfiles") CONFIG_COMMANDS="$CONFIG_COMMANDS depfiles" ;; "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;; "src/Makefile") CONFIG_FILES="$CONFIG_FILES src/Makefile" ;; + "src/bash-completion/tio") CONFIG_FILES="$CONFIG_FILES src/bash-completion/tio" ;; "man/Makefile") CONFIG_FILES="$CONFIG_FILES man/Makefile" ;; *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;; diff --git a/configure.ac b/configure.ac index d76809b..e80cb75 100644 --- a/configure.ac +++ b/configure.ac @@ -1,5 +1,5 @@ AC_PREREQ([2.68]) -AC_INIT([tio], [1.14], [], [tio], [https://tio.github.io]) +AC_INIT([tio], [1.18], [], [tio], [https://tio.github.io]) AC_CONFIG_HEADERS([src/include/config.h]) AM_INIT_AUTOMAKE([1.11 foreign dist-xz no-dist-gzip -Wall -Werror]) AM_SILENT_RULES([yes]) @@ -7,8 +7,9 @@ AC_PROG_CC AC_LANG([C]) AC_PROG_INSTALL AC_SYS_LARGEFILE - PKG_PROG_PKG_CONFIG + +# Handle bash completion AC_ARG_WITH([bash-completion-dir], AS_HELP_STRING([--with-bash-completion-dir[=PATH]], [Install the bash auto-completion script in this directory. @<:@default=yes@:>@]), @@ -25,7 +26,72 @@ fi AC_SUBST([BASH_COMPLETION_DIR]) AM_CONDITIONAL([ENABLE_BASH_COMPLETION],[test "x$with_bash_completion_dir" != "xno"]) +# TIO_CHECK_BAUDRATE(N) +AC_DEFUN( + [TIO_CHECK_BAUDRATE], + [ + 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;"])] + ) + ] +) + +# TIO_CHECK_BAUDRATES(N1, N2, ...) +AC_DEFUN( + [TIO_CHECK_BAUDRATES], + [m4_foreach([n], [$@], [TIO_CHECK_BAUDRATE(m4_normalize(n))])] +) + +# Check for available terminal I/O speeds +BAUDRATES= +BAUDRATE_CASES= +TIO_CHECK_BAUDRATES( + 0, + 50, + 75, + 110, + 134, + 150, + 200, + 300, + 600, + 1200, + 1800, + 2400, + 4800, + 7200, + 9600, + 14400, + 19200, + 28800, + 38400, + 57600, + 76800, + 115200, + 230400, + 460800, + 500000, + 576000, + 921600, + 1000000, + 1152000, + 1500000, + 2000000, + 2500000, + 3000000, + 3500000, + 4000000 +) + +AC_DEFINE_UNQUOTED([AUTOCONF_BAUDRATE_CASES],[$BAUDRATE_CASES],[Switch cases for detected baud rates]) + +# Check that it is possible to set arbitrary baud rates using BOTHER +AC_CHECK_DECLS([BOTHER], [], [], [[#include ]]) + AC_CONFIG_FILES([Makefile]) AC_CONFIG_FILES([src/Makefile]) +AC_CONFIG_FILES([src/bash-completion/tio]) AC_CONFIG_FILES([man/Makefile]) AC_OUTPUT diff --git a/man/Makefile.in b/man/Makefile.in index 290ef3d..62fbb06 100644 --- a/man/Makefile.in +++ b/man/Makefile.in @@ -158,6 +158,8 @@ AWK = @AWK@ BASH_COMPLETION_CFLAGS = @BASH_COMPLETION_CFLAGS@ BASH_COMPLETION_DIR = @BASH_COMPLETION_DIR@ BASH_COMPLETION_LIBS = @BASH_COMPLETION_LIBS@ +BAUDRATES = @BAUDRATES@ +BAUDRATE_CASES = @BAUDRATE_CASES@ CC = @CC@ CCDEPMODE = @CCDEPMODE@ CFLAGS = @CFLAGS@ diff --git a/man/tio.1 b/man/tio.1 index 4fd81ab..c71ae84 100644 --- a/man/tio.1 +++ b/man/tio.1 @@ -1,4 +1,4 @@ -.TH "tio" "1" "7 May 2016" +.TH "tio" "1" "29 June 2016" .SH "NAME" tio \- a simple TTY terminal I/O application @@ -39,11 +39,18 @@ Set parity (default: none). .TP .BR \-o ", " "\-\-output\-delay " \fI -Set output delay [ms] \(em delay inserted between each transmitted character (default: 0). +Set output delay [ms] inserted between each transmitted character (default: 0). .TP .BR \-n ", " \-\-no\-autoconnect Disable automatic connect. + +By default tio automatically connects to the provided device if present. If the device is not present it will wait for it to appear and then connect. If the connection is lost (eg. device disconnects) it will wait for the device to reappear and then reconnect. + +However, if the +.B \-\-no\-autoconnect +option is provided tio will exit if the device is not present or exit if an established connection is lost. + .TP .BR \-l ", " "\-\-log " \fI @@ -74,7 +81,7 @@ Send ctrl-t key code .SH "EXAMPLES" .TP -A typical use is without options. For example: +Typical use is without options. For example: tio /dev/ttyUSB0 .TP diff --git a/src/Makefile.am b/src/Makefile.am index 6129033..f09db52 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ -AM_CFLAGS=-Wall +AM_CFLAGS = -Wall bin_PROGRAMS = tio tio_SOURCES = tty.c \ options.c \ @@ -14,6 +14,8 @@ tio_SOURCES = tty.c \ include/tio/error.h if ENABLE_BASH_COMPLETION + bashcompletiondir=@BASH_COMPLETION_DIR@ dist_bashcompletion_DATA=bash-completion/tio + endif diff --git a/src/Makefile.in b/src/Makefile.in index 3232857..dc35dec 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -201,6 +201,8 @@ AWK = @AWK@ BASH_COMPLETION_CFLAGS = @BASH_COMPLETION_CFLAGS@ BASH_COMPLETION_DIR = @BASH_COMPLETION_DIR@ BASH_COMPLETION_LIBS = @BASH_COMPLETION_LIBS@ +BAUDRATES = @BAUDRATES@ +BAUDRATE_CASES = @BAUDRATE_CASES@ CC = @CC@ CCDEPMODE = @CCDEPMODE@ CFLAGS = @CFLAGS@ diff --git a/src/bash-completion/tio b/src/bash-completion/tio index 31f3b89..84e21b0 100644 --- a/src/bash-completion/tio +++ b/src/bash-completion/tio @@ -24,35 +24,7 @@ _tio() # Complete the arguments to the options. case "${prev}" in -b | --baudrate) - local baudrates="0 \ - 50 \ - 75 \ - 110 \ - 134 \ - 150 \ - 300 \ - 600 \ - 1200 \ - 2400 \ - 4800 \ - 9600 \ - 19200 \ - 38400 \ - 57600 \ - 115200 \ - 230400 \ - 460800 \ - 500000 \ - 576000 \ - 921600 \ - 1000000 \ - 1152000 \ - 1500000 \ - 2000000 \ - 2500000 \ - 3000000 \ - 3500000 \ - 4000000" + local baudrates=" 0 50 75 110 134 150 200 300 600 1200 1800 2400 4800 9600 19200 38400 57600 115200 230400 460800 500000 576000 921600 1000000 1152000 1500000 2000000 2500000 3000000 3500000 4000000" COMPREPLY=( $(compgen -W "$baudrates" -- ${cur}) ) return 0 ;; diff --git a/src/bash-completion/tio.in b/src/bash-completion/tio.in new file mode 100644 index 0000000..0f8dde4 --- /dev/null +++ b/src/bash-completion/tio.in @@ -0,0 +1,76 @@ +# +# Bash completion script for tio. +# + +_tio() +{ + local cur prev opts base + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + + # The options we'll complete. + opts="-b --baudrate \ + -d --databits \ + -f --flow \ + -s --stopbits \ + -p --parity \ + -o --output-delay \ + -n --no-autoconnect \ + -l --log \ + -v --version \ + -h --help" + + # Complete the arguments to the options. + case "${prev}" in + -b | --baudrate) + local baudrates="@BAUDRATES@" + COMPREPLY=( $(compgen -W "$baudrates" -- ${cur}) ) + return 0 + ;; + -d | --databits) + COMPREPLY=( $(compgen -W "5 6 7 8" -- ${cur}) ) + return 0 + ;; + -f | --flow) + COMPREPLY=( $(compgen -W "hard soft none" -- ${cur}) ) + return 0 + ;; + -s | --stopbits) + COMPREPLY=( $(compgen -W "1 2" -- ${cur}) ) + return 0 + ;; + -p | --parity) + COMPREPLY=( $(compgen -W "even odd none" -- ${cur}) ) + return 0 + ;; + -o | --output-delay) + COMPREPLY=( $(compgen -W "0 1 10 100" -- ${cur}) ) + return 0 + ;; + -n | --no-autoconnect) + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + -l | --log) + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + -v | --version) + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + -h | --help) + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 + ;; + *) + ;; + esac + + COMPREPLY=($(compgen -W "${opts}" -- ${cur})) + return 0 +} + +# Bind completion to tio command +complete -o default -F _tio tio diff --git a/src/error.c b/src/error.c index 3bcadde..9f1fbd7 100644 --- a/src/error.c +++ b/src/error.c @@ -19,6 +19,7 @@ * 02110-1301, USA. */ +#include "config.h" #include #include #include diff --git a/src/include/config.h.in b/src/include/config.h.in index e316f10..213f300 100644 --- a/src/include/config.h.in +++ b/src/include/config.h.in @@ -1,5 +1,12 @@ /* src/include/config.h.in. Generated from configure.ac by autoheader. */ +/* Switch cases for detected baud rates */ +#undef AUTOCONF_BAUDRATE_CASES + +/* Define to 1 if you have the declaration of `BOTHER', and to 0 if you don't. + */ +#undef HAVE_DECL_BOTHER + /* Name of package */ #undef PACKAGE diff --git a/src/include/tio/options.h b/src/include/tio/options.h index 925a93f..b89ad1a 100644 --- a/src/include/tio/options.h +++ b/src/include/tio/options.h @@ -31,16 +31,15 @@ struct option_t { const char *tty_device; - bool log; - const char *log_filename; - bool no_autoconnect; - int output_delay; - struct termios tio; - int baudrate; + unsigned int baudrate; int databits; char *flow; int stopbits; char *parity; + int output_delay; + bool no_autoconnect; + bool log; + const char *log_filename; }; extern struct option_t option; diff --git a/src/include/tio/tty.h b/src/include/tio/tty.h index 0e4d651..f0339a0 100644 --- a/src/include/tio/tty.h +++ b/src/include/tio/tty.h @@ -29,9 +29,10 @@ #define KEY_T 0x74 #define KEY_CTRL_T 0x14 -void configure_stdout(void); -void restore_stdout(void); -int connect_tty(void); -void wait_for_tty_device(void); +void stdout_configure(void); +void stdout_restore(void); +void tty_configure(void); +int tty_connect(void); +void tty_wait_for_device(void); #endif diff --git a/src/log.c b/src/log.c index 1f04b34..7850381 100644 --- a/src/log.c +++ b/src/log.c @@ -19,6 +19,7 @@ * 02110-1301, USA. */ +#include "config.h" #include #include #include diff --git a/src/main.c b/src/main.c index d379f98..02019cc 100644 --- a/src/main.c +++ b/src/main.c @@ -19,6 +19,7 @@ * 02110-1301, USA. */ +#include "config.h" #include #include #include @@ -38,8 +39,11 @@ int main(int argc, char *argv[]) /* Parse options */ parse_options(argc, argv); + /* Configure tty device */ + tty_configure(); + /* Configure output terminal */ - configure_stdout(); + stdout_configure(); /* Install log exit handler */ atexit(&log_exit); @@ -50,14 +54,14 @@ int main(int argc, char *argv[]) /* Connect to tty device */ if (option.no_autoconnect) - status = connect_tty(); + status = tty_connect(); else { /* Enter connect loop */ while (true) { - wait_for_tty_device(); - status = connect_tty(); + tty_wait_for_device(); + status = tty_connect(); } } diff --git a/src/options.c b/src/options.c index 7eb964b..e280fff 100644 --- a/src/options.c +++ b/src/options.c @@ -19,6 +19,7 @@ * 02110-1301, USA. */ +#include "config.h" #include #include #include @@ -28,26 +29,25 @@ #include #include #include -#include "config.h" #include "tio/options.h" #include "tio/error.h" +/* Default options */ struct option_t option = { "", // Device name - false, // No log - "", // Log filename - false, // No autoconnect - 0, // No output delay - {}, 115200, // Baudrate 8, // Databits "none", // Flow 1, // Stopbits - "none" // Parity + "none", // Parity + 0, // No output delay + false, // No autoconnect + false, // No log + "" // Log filename }; -void print_options_help(char *argv[]) +void print_help(char *argv[]) { printf("Usage: %s [] \n", argv[0]); printf("\n"); @@ -63,29 +63,36 @@ void print_options_help(char *argv[]) printf(" -v, --version Display version\n"); printf(" -h, --help Display help\n"); printf("\n"); - printf("In session, press ctrl-t + q to quit.\n"); + printf("In session, press ctrl-t q to quit.\n"); printf("\n"); } +long string_to_long(char *string) +{ + long result; + char *end_token; + + errno = 0; + result = strtol(string, &end_token, 10); + if ((errno != 0) || (*end_token != 0)) + { + error_printf("Invalid digit"); + exit(EXIT_FAILURE); + } + + return result; +} + void parse_options(int argc, char *argv[]) { int c; - int baudrate; if (argc == 1) { - print_options_help(argv); + print_help(argv); exit(EXIT_SUCCESS); } - /* Set default termios settings: - * (115200 baud, 8 data bits, no flow control, 1 stop bit, no parity) */ - bzero(&option.tio, sizeof(option.tio)); - option.tio.c_cflag = B115200 | CS8; - - /* Set default output delay */ - option.output_delay = 0; - while (1) { static struct option long_options[] = @@ -126,194 +133,27 @@ void parse_options(int argc, char *argv[]) break; case 'b': - option.baudrate = baudrate = atoi(optarg); - switch (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 300: - baudrate = B300; - break; - case 600: - baudrate = B600; - break; - case 1200: - baudrate = B1200; - 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; - case 57600: - baudrate = B57600; - break; - case 115200: - baudrate = B115200; - break; - case 230400: - baudrate = B230400; - break; - case 460800: - baudrate = B460800; - break; - case 500000: - baudrate = B500000; - break; - case 576000: - baudrate = B576000; - break; - case 921600: - baudrate = B921600; - break; - case 1000000: - baudrate = B1000000; - break; - case 1152000: - baudrate = B1152000; - break; - case 1500000: - baudrate = B1500000; - break; - case 2000000: - baudrate = B2000000; - break; - case 2500000: - baudrate = B2500000; - break; - case 3000000: - baudrate = B3000000; - break; - case 3500000: - baudrate = B3500000; - break; - case 4000000: - baudrate = B4000000; - break; - default: - error_printf("Invalid baud rate"); - exit(EXIT_FAILURE); - } - - cfsetispeed(&option.tio, baudrate); - cfsetospeed(&option.tio, baudrate); - + option.baudrate = string_to_long(optarg); break; case 'd': - option.databits = atoi(optarg); - option.tio.c_cflag &= ~CSIZE; - switch (option.databits) - { - case 5: - option.tio.c_cflag |= CS5; - break; - case 6: - option.tio.c_cflag |= CS6; - break; - case 7: - option.tio.c_cflag |= CS7; - break; - case 8: - option.tio.c_cflag |= CS8; - break; - default: - error_printf("Invalid data bits"); - exit(EXIT_FAILURE); - } + option.databits = string_to_long(optarg); break; case 'f': option.flow = optarg; - - if (strcmp("hard", optarg) == 0) - { - option.tio.c_cflag |= CRTSCTS; - option.tio.c_iflag &= ~(IXON | IXOFF | IXANY); - } - else if (strcmp("soft", optarg) == 0) - { - option.tio.c_cflag &= ~CRTSCTS; - option.tio.c_iflag |= IXON | IXOFF; - } - else if (strcmp("none", optarg) == 0) - { - option.tio.c_cflag &= ~CRTSCTS; - option.tio.c_iflag &= ~(IXON | IXOFF | IXANY); - } - else - { - error_printf("Invalid flow control"); - exit(EXIT_FAILURE); - } break; case 's': - option.stopbits = atoi(optarg); - switch (option.stopbits) - { - case 1: - option.tio.c_cflag &= ~CSTOPB; - break; - case 2: - option.tio.c_cflag |= CSTOPB; - break; - default: - error_printf("Invalid stop bits"); - exit(EXIT_FAILURE); - } + option.stopbits = string_to_long(optarg); break; case 'p': option.parity = optarg; - - if (strcmp("odd", optarg) == 0) - { - option.tio.c_cflag |= PARENB; - option.tio.c_cflag |= PARODD; - } - else if (strcmp("even", optarg) == 0) - { - option.tio.c_cflag |= PARENB; - option.tio.c_cflag &= ~PARODD; - } - else if (strcmp("none", optarg) == 0) - option.tio.c_cflag &= ~PARENB; - else - { - error_printf("Invalid parity"); - exit(EXIT_FAILURE); - } break; case 'o': - option.output_delay = atoi(optarg); + option.output_delay = string_to_long(optarg); break; case 'n': @@ -329,14 +169,14 @@ void parse_options(int argc, char *argv[]) printf("tio v%s\n", VERSION); printf("Copyright (c) 2014-2016 Martin Lund\n"); printf("\n"); - printf("License GPLv2: GNU GPL version 2 or later .\n"); + printf("License GPLv2+: GNU GPL version 2 or later .\n"); printf("This is free software: you are free to change and redistribute it.\n"); printf("There is NO WARRANTY, to the extent permitted by law.\n"); exit(EXIT_SUCCESS); break; case 'h': - print_options_help(argv); + print_help(argv); exit(EXIT_SUCCESS); break; diff --git a/src/time.c b/src/time.c index fd609b8..0acc9f8 100644 --- a/src/time.c +++ b/src/time.c @@ -19,6 +19,7 @@ * 02110-1301, USA. */ +#include "config.h" #include #include #include diff --git a/src/tty.c b/src/tty.c index 51c609c..fccd5ff 100644 --- a/src/tty.c +++ b/src/tty.c @@ -19,6 +19,7 @@ * 02110-1301, USA. */ +#include "config.h" #include #include #include @@ -40,12 +41,17 @@ #include "tio/log.h" #include "tio/error.h" -static struct termios new_stdout, old_stdout, old_tio; -static long rx_total = 0, tx_total = 0; +static struct termios tio, new_stdout, old_stdout, old_tio; +static unsigned long rx_total = 0, tx_total = 0; static bool connected = false; static bool tainted = false; +static bool standard_baudrate = true; static int fd; +#ifndef BOTHER +#define BOTHER 0010000 +#endif + #define tio_printf(format, args...) \ { \ if (tainted) putchar('\n'); \ @@ -82,7 +88,7 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c case KEY_I: tio_printf("Settings information:"); tio_printf(" TTY device: %s", option.tty_device); - tio_printf(" Baudrate: %d", option.baudrate); + tio_printf(" Baudrate: %u", option.baudrate); tio_printf(" Databits: %d", option.databits); tio_printf(" Flow: %s", option.flow); tio_printf(" Stopbits: %d", option.stopbits); @@ -102,7 +108,7 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c case KEY_S: /* Show tx/rx statistics upon ctrl-t s sequence */ tio_printf("Statistics:"); - tio_printf(" Sent %ld bytes, received %ld bytes", tx_total, rx_total); + tio_printf(" Sent %lu bytes, received %lu bytes", tx_total, rx_total); *forward = false; break; default: @@ -113,7 +119,167 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c } } -void wait_for_tty_device(void) +void stdout_configure(void) +{ + /* Save current stdout settings */ + if (tcgetattr(STDOUT_FILENO, &old_stdout) < 0) + { + error_printf("Saving current stdio settings failed"); + exit(EXIT_FAILURE); + } + + /* Prepare new stdout settings */ + memset(&new_stdout, 0, sizeof(new_stdout)); + + /* Control, input, output, local modes for stdout */ + new_stdout.c_cflag = 0; + new_stdout.c_iflag = 0; + new_stdout.c_oflag = 0; + new_stdout.c_lflag = 0; + + /* Control characters */ + new_stdout.c_cc[VTIME] = 0; /* Inter-character timer unused */ + new_stdout.c_cc[VMIN] = 1; /* Blocking read until 1 character received */ + + /* Activate new stdout settings */ + tcsetattr(STDOUT_FILENO, TCSANOW, &new_stdout); + tcsetattr(STDOUT_FILENO, TCSAFLUSH, &new_stdout); + + /* Print launch hints */ + tio_printf("tio v%s", VERSION); + tio_printf("Press ctrl-t q to quit"); + + /* Make sure we restore old stdout settings on exit */ + atexit(&stdout_restore); +} + +void stdout_restore(void) +{ + tcsetattr(STDOUT_FILENO, TCSANOW, &old_stdout); + tcsetattr(STDOUT_FILENO, TCSAFLUSH, &old_stdout); +} + +void tty_configure(void) +{ + speed_t baudrate = 0; + + memset(&tio, 0, sizeof(tio)); + + /* Set speed */ + switch (option.baudrate) + { + /* 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: +#if !HAVE_DECL_BOTHER + error_printf("Invalid baud rate"); + exit(EXIT_FAILURE); +#else + standard_baudrate = false; + break; +#endif + } + + if (standard_baudrate) + { + cfsetispeed(&tio, baudrate); + cfsetospeed(&tio, baudrate); + } else + { + tio.c_ispeed = tio.c_ospeed = baudrate; + tio.c_cflag &= ~CBAUD; + tio.c_cflag |= BOTHER; + } + + /* Set databits */ + tio.c_cflag &= ~CSIZE; + switch (option.databits) + { + case 5: + tio.c_cflag |= CS5; + break; + case 6: + tio.c_cflag |= CS6; + break; + case 7: + tio.c_cflag |= CS7; + break; + case 8: + tio.c_cflag |= CS8; + break; + default: + error_printf("Invalid data bits"); + exit(EXIT_FAILURE); + } + + /* Set flow control */ + if (strcmp("hard", option.flow) == 0) + { + tio.c_cflag |= CRTSCTS; + tio.c_iflag &= ~(IXON | IXOFF | IXANY); + } + else if (strcmp("soft", option.flow) == 0) + { + tio.c_cflag &= ~CRTSCTS; + tio.c_iflag |= IXON | IXOFF; + } + else if (strcmp("none", option.flow) == 0) + { + tio.c_cflag &= ~CRTSCTS; + tio.c_iflag &= ~(IXON | IXOFF | IXANY); + } + else + { + error_printf("Invalid flow control"); + exit(EXIT_FAILURE); + } + + /* Set stopbits */ + switch (option.stopbits) + { + case 1: + tio.c_cflag &= ~CSTOPB; + break; + case 2: + tio.c_cflag |= CSTOPB; + break; + default: + error_printf("Invalid stop bits"); + exit(EXIT_FAILURE); + } + + /* Set parity */ + if (strcmp("odd", option.parity) == 0) + { + tio.c_cflag |= PARENB; + tio.c_cflag |= PARODD; + } + else if (strcmp("even", option.parity) == 0) + { + tio.c_cflag |= PARENB; + tio.c_cflag &= ~PARODD; + } + else if (strcmp("none", option.parity) == 0) + tio.c_cflag &= ~PARENB; + else + { + error_printf("Invalid parity"); + exit(EXIT_FAILURE); + } +} + +void tty_wait_for_device(void) { fd_set rdfs; int status; @@ -171,47 +337,7 @@ void wait_for_tty_device(void) } } -void configure_stdout(void) -{ - /* Save current stdout settings */ - if (tcgetattr(STDOUT_FILENO, &old_stdout) < 0) - { - error_printf("Saving current stdio settings failed"); - exit(EXIT_FAILURE); - } - - /* Prepare new stdout settings */ - bzero(&new_stdout, sizeof(new_stdout)); - - /* Control, input, output, local modes for stdout */ - new_stdout.c_cflag = 0; - new_stdout.c_iflag = 0; - new_stdout.c_oflag = 0; - new_stdout.c_lflag = 0; - - /* Control characters */ - new_stdout.c_cc[VTIME] = 0; /* Inter-character timer unused */ - new_stdout.c_cc[VMIN] = 1; /* Blocking read until 1 character received */ - - /* Activate new stdout settings */ - tcsetattr(STDOUT_FILENO, TCSANOW, &new_stdout); - tcsetattr(STDOUT_FILENO, TCSAFLUSH, &new_stdout); - - /* Print launch hints */ - tio_printf("tio v%s", VERSION); - tio_printf("Press ctrl-t + q to quit"); - - /* Make sure we restore old stdout settings on exit */ - atexit(&restore_stdout); -} - -void restore_stdout(void) -{ - tcsetattr(STDOUT_FILENO, TCSANOW, &old_stdout); - tcsetattr(STDOUT_FILENO, TCSAFLUSH, &old_stdout); -} - -void disconnect_tty(void) +void tty_disconnect(void) { if (connected) { @@ -222,16 +348,16 @@ void disconnect_tty(void) } } -void restore_tty(void) +void tty_restore(void) { tcsetattr(fd, TCSANOW, &old_tio); tcsetattr(fd, TCSAFLUSH, &old_tio); if (connected) - disconnect_tty(); + tty_disconnect(); } -int connect_tty(void) +int tty_connect(void) { fd_set rdfs; /* Read file descriptor set */ int maxfd; /* Maximum file descriptor used */ @@ -251,8 +377,8 @@ int connect_tty(void) /* Make sure device is of tty type */ if (!isatty(fd)) { - error_printf_silent("Not a tty device"); - goto error_isatty; + error_printf("Not a tty device"); + exit(EXIT_FAILURE);; } /* Lock device file */ @@ -266,6 +392,10 @@ int connect_tty(void) /* Flush stale I/O data (if any) */ tcflush(fd, TCIOFLUSH); + /* Warn if non standard baud rate is used */ + if (!standard_baudrate) + tio_printf("Warning: Using a non standard baud rate"); + /* Print connect status */ tio_printf("Connected"); connected = true; @@ -278,22 +408,22 @@ int connect_tty(void) /* Make sure we restore tty settings on exit */ if (first) { - atexit(&restore_tty); + atexit(&tty_restore); first = false; } /* Control, input, output, local modes for tty device */ - option.tio.c_cflag |= CLOCAL | CREAD; - option.tio.c_oflag = 0; - option.tio.c_lflag = 0; + tio.c_cflag |= CLOCAL | CREAD; + tio.c_oflag = 0; + tio.c_lflag = 0; /* Control characters */ - option.tio.c_cc[VTIME] = 0; /* Inter-character timer unused */ - option.tio.c_cc[VMIN] = 1; /* Blocking read until 1 character received */ + tio.c_cc[VTIME] = 0; /* Inter-character timer unused */ + tio.c_cc[VMIN] = 1; /* Blocking read until 1 character received */ /* Activate new port settings */ - tcsetattr(fd, TCSANOW, &option.tio); - tcsetattr(fd, TCSAFLUSH, &option.tio); + tcsetattr(fd, TCSANOW, &tio); + tcsetattr(fd, TCSAFLUSH, &tio); maxfd = MAX(fd, STDIN_FILENO) + 1; /* Maximum bit entry (fd) to test */ @@ -381,9 +511,8 @@ int connect_tty(void) return TIO_SUCCESS; error_tcgetattr: -error_isatty: error_read: - disconnect_tty(); + tty_disconnect(); error_open: return TIO_ERROR; } -- 2.39.2