diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 8baeeb0..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,27 +0,0 @@ -# Use the latest 2.1 version of CircleCI pipeline process engine. -# See: https://circleci.com/docs/2.0/configuration-reference -version: 2.1 - -# Define a job to be invoked later in a workflow. -# See: https://circleci.com/docs/2.0/configuration-reference/#jobs -jobs: - build-tio: - # Specify the execution environment. You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub. - # See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor - docker: - - image: cimg/base:edge - # Add steps to the job - # See: https://circleci.com/docs/2.0/configuration-reference/#steps - steps: - - checkout - - run: sudo apt-get -qq update - - run: sudo apt-get install -y bash-completion git meson libinih-dev liblua5.2-dev - - run: git clone https://github.com/tio/tio.git - - run: cd tio && meson build --prefix $HOME/test/tio && ninja -C build install - -# Invoke jobs via workflows -# See: https://circleci.com/docs/2.0/configuration-reference/#workflows -workflows: - build-tio-workflow: - jobs: - - build-tio diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..682ae09 --- /dev/null +++ b/.clang-format @@ -0,0 +1,5 @@ +BasedOnStyle: llvm +IndentWidth: 4 +AllowShortFunctionsOnASingleLine: None +KeepEmptyLinesAtTheStartOfBlocks: false +BreakBeforeBraces: Allman diff --git a/.github/workflows/codeql-buildscript.sh b/.github/workflows/codeql-buildscript.sh index 4b9e11d..720bd6b 100644 --- a/.github/workflows/codeql-buildscript.sh +++ b/.github/workflows/codeql-buildscript.sh @@ -2,6 +2,6 @@ pip3 install meson -U pip3 install ninja -U -sudo apt-get install -y liblua5.2-dev +sudo apt-get install -y liblua5.2-dev libglib2.0-dev meson setup build meson compile -C build diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 8164922..8a195f4 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -12,8 +12,8 @@ name: "CodeQL" on: - # push: - # branches: [ "main", "master" ] + push: + branches: [ "main", "master" ] schedule: - cron: '0 0 * * *' pull_request: @@ -27,7 +27,7 @@ jobs: # - https://gh.io/supported-runners-and-hardware-resources # - https://gh.io/using-larger-runners # Consider using larger runners for possible analysis time improvements. - runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-20.04' }} + runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-22.04' }} timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }} permissions: actions: read @@ -51,7 +51,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -66,7 +66,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift). # If this step fails, then you should remove it and run the build manually (see below) #- name: Autobuild - # uses: github/codeql-action/autobuild@v2 + # uses: github/codeql-action/autobuild@v3 # â„šī¸ Command-line programs to run using the OS shell. # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun @@ -78,7 +78,7 @@ jobs: ./.github/workflows/codeql-buildscript.sh - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3 with: category: "/language:${{matrix.language}}" upload: false @@ -107,14 +107,14 @@ jobs: output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif - name: Upload CodeQL results to code scanning - uses: github/codeql-action/upload-sarif@v2 + uses: github/codeql-action/upload-sarif@v3 with: sarif_file: ${{ steps.step1.outputs.sarif-output }} category: "/language:${{matrix.language}}" - name: Upload CodeQL results as an artifact if: success() || failure() - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: codeql-results path: ${{ steps.step1.outputs.sarif-output }} diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml new file mode 100644 index 0000000..8f54070 --- /dev/null +++ b/.github/workflows/macos.yml @@ -0,0 +1,30 @@ +name: MacOS build + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build: + runs-on: macos-latest + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: '3.x' + + - name: Install dependencies + run: | + brew install meson ninja lua + + - name: Build + run: | + meson setup build + meson compile -C build --verbose + meson install -C build diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml new file mode 100644 index 0000000..90e1a93 --- /dev/null +++ b/.github/workflows/ubuntu.yml @@ -0,0 +1,31 @@ +name: Ubuntu build + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: '3.x' + + - name: Install dependencies + run: | + sudo apt update + sudo apt install -y bash-completion git meson liblua5.2-dev libglib2.0-dev + + - name: Build + run: | + meson setup build --prefix $HOME/opt/tio + meson compile -C build --verbose + meson install -C build diff --git a/.typos.toml b/.typos.toml new file mode 100644 index 0000000..11f019f --- /dev/null +++ b/.typos.toml @@ -0,0 +1,2 @@ +[default] +extend-ignore-words-re = ["tio"] diff --git a/AUTHORS b/AUTHORS index e6f22c9..3754640 100644 --- a/AUTHORS +++ b/AUTHORS @@ -52,5 +52,19 @@ Fredrik Svedberg Sebastian Mingjie Shen Brian +Davis C +KhazAkar +Eliot Alan Foss +Robert Lipe +Heinrich Schuchardt +Tomka Gergely +Steve Marple +konosubakonoakua +Keith Hill +Lubov66 +V +Samuel Holland +David Ordnung + Thanks to everyone who has contributed to this project. diff --git a/LICENSE b/LICENSE index a793eef..d5fa60d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2014-2022 Martin Lund +Copyright (c) 2014-2025 Martin Lund This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License diff --git a/NEWS b/NEWS index e62ce4e..4289610 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,527 @@ +=== tio v3.9 (2025-04-13) === -=== tio v2.8 === + + +Changes since tio v3.8 (2024-11-30): + + * Fix parsing of timestamp options + + * codeql: Upgrade to upload-artifact@v4 + + * Update plaintext man page + + * Add character mapping examples + + * Fix pattern matching memory corruption + +Samuel Holland: + + * Don't add null characters to the expect buffer + + They prevent regexec() from seeing the remainder of the buffer. + +V: + + * Disable stdout buffering globally + + This makes it possible to pipe output to other programs cleanly. + +Lubov66: + + * docs: edited the license date + +Jakob Haufe: + + * Manpage: Fix backslash encoding + + Literal backslash needs to be written as \e. + + + +Changes since tio v3.7 (2024-08-31): + + * Rename git version to simply version + + * Clean up lua API + + Rename modem_send() to send() + Rename send to write() + + * Zero initialize buffer in read_string() + + * Use version from git + + * Fix memory leak in base62_encode() + + * Fix name declaration conflict with socket send() + + * Add clang-format spec + +Keith Hill: + + + Add system timestamps to lua read() and new lua read_line() per global options + + Add missing timestamp-format epoch + + Update send_ to use fsync and tcdrain like normal tty_sync does + + Rework read_line to save partial line at timeout + + Simplified read_line to reduce cyclomatic complexity + + renamed example files read.lua and read_line.lua + + moved #define READ_LINE_SIZE to top of file + + renamed g_linebuf to linebuf, and moved it into read_line as a static variable + + + +Changes since tio v3.6 (2024-07-19): + + * Remove unnecessary sync in line input mode + + This caused a problem for some highly timing sensitive modem read-eval-print + loops because the input line and line termination characters (cr/nl) would be + shifted out on the UART with too big delay inbetween because of two + syncs. + + * Fix socket send call on platforms without MSG_NOSIGNAL + + To fix build issue encountered on MacOS Catalina but may apply to other + platforms. + +Steve Marple: + + * Add "epoch" timestamp option + + Add an option that prints the timestamp as the number of seconds since + the Unix epoch. + +Tomka Gergely: + + * The log-directory options is not read from the configuration file. + + + +Changes since tio v3.5 (2024-06-29): + + * Add configuration file include directive + + To include the contents of another configuration file simply do e.g.: + + [include raspberrypi.conf] + + Also, included file can include other files which can include other + files etc. + + This feature is useful for managing many configuration files and sharing + configuration files with others. + + * Mention how to list key commands in help output + + * Fix hex output mode when using normal input mode + + In this combination of modes the input character was not forwarded to + the tty device. This fix makes sure it is forwarded. + + * Fix uptime on MacOS + + On MacOS the birth time is apparently not available so we use + modification time instead. + + * Improve warning upon failing connect + + Add device path to warning when connect fails. + + * Fix crashy search_reset() on macOS + + * Clean up shadow variable + + * Clean up readline code + + * Improve listing of long device names + + * Fix listing of serial devices on macOS + +Heinrich Schuchardt: + + * Print correct 'Done' timestamp for X- and Y-modem transfers + + Call tio_printf() after completing xymodem_send(). + +Robert Lipe: + + * Recompute listing_device_name_length_max for MacOS case, too. + + + +Changes since tio v3.4: + + * Clarify input and output direction of map flags + + * Rename mapping flag MSB2LSB to IMSB2LSB + + This is the correct naming since we are changing the input bit order on + input from the serial device. + + * Add OIGNCR mapping flag + + Ignores CR on output to serial device. + + * Fix line input mode ignoring characters ABCD + + * Fix tainted print + +Jakob Haufe: + + * Fix typos + + + +Changes since tio v3.3: + + * Update configuration output + + * Clean up script run interaction text + + * Fix unbounded writes + + * Add history and editing feature to line input mode + + Use up and down arrow keys to navigate history. + + Use left and right arrow keys to move cursor back and forth. + + We try mimic the behaviour of GNU readline which we can not use because + we also need to react to key commands. + + * Reuse socket address + + To avoid having to wait for socket timeout when restarting server. + + * Fix line input mode + + Fix so that ABCD are no longer ignored. + + * Make sure ICRNL, IGNCR, INLCR take effect + + * Include correct header for poll() + + * Add group write permission to xymodem received file + + * Fix missing open() flags in xymodem_receive() + +Vyacheslav Patkov: + + * Show current mappings in the configuration printout + + * Use "ctrl-t m" to change mappings interactively + + * Prompt for Lua script or shell command in interactive session + +Eliot Alan Foss: + + * Added support to receive XMODEM-CRC files from the connected serial port. + + + +Changes since tio v3.2: + + * Force destructive backspace when using local echo + + Only takes effect in normal output mode. + + * Fix local-echo in configuration file + + * Clean up includes + + * Force socket write operation to ignore any signals + + * Man page cleanup + + + +Changes since tio v3.1: + + * Do not print error when using --list with broken config file + + * Clean up completion script + + * Add option '--exec ' for running shell command + + Runs shell command with I/O redirected to device. + + * Make sure all error output is directed to stderr + + * Fix shadow variables + + * Update man page + + * Fix build on older GNU/Linux systems without statx + + * Fix line ending in --list output + + * Print location of configuration file in --list output + + * Fix alignment of profile listing + + + +Changes since tio v3.0: + + * Improve --list feature on non-linux platform + + * List available profiles in --list output + + * Always message when saving log file + + * Add support for using TID as device in config file + + * Fix use of invalid flag with regexec() + + * Fix potential buffer overflow in match_and_replace() + + * Fix profile autocompletion + + * Remove inih dependency from CI builds + + * Replace use of stat() with fstat() + + For better security. + + * Fix hexN output mode + + * Update pattern matching example + + * Fix submenu response when invalid key hit + + * Replace inih with glib key file parser + + After including the use of glib we might as well replace inih + with the glib key file parser. + + All configuration file parsing has been reworked and also the options + parsing has been cleaned up, resulting in better and stricter + configuration file and option value checks. + + Compared to old, configuration files now requires any default + configurations to be put in a group/section named [default]. + + Configuration file keywords such as "enable", "disable", "on", + "off", "yes", "no", "0", "1" have been retired. Now only "true" and + "false" apply to boolean configuration options. This is done to simplify + things and avoid any confusion. + + The pattern option feature has been reworked so now the user can now + access the full match string and any matching subexpression using the + %mN syntax. + + For example: + + [usb devices] + pattern = usb([0-9]*) + device = /dev/ttyUSB%m1 + + Then when using tio: + $ tio usb12 + + %m0 = 'usb12' // Full match string + %m1 = 12 // First match subexpression + + Which results in device = /dev/ttyUSB12 + + * Remove CircleCI + + Replaced with github workflow CI. + + * Add github workflow for Ubuntu build + + * Enable extended pattern matching + + So that the exclude options can also work as include using special + pattern syntax. + + For example, to only include /dev/ttyUSB* devices simply do: + + $ tio --exclude-devices=!(/dev/ttyUSB*) --list + + See the man page of fnmatch() for all available extended pattern + options. + + * Update lua read() description + +Rui Chen: + + * fix: add build patch for `FNM_EXTMATCH` + + * feat: add macOS workflow + + * fix: add macOS build patch for `fs_get_creation_time` + + + +Changes since tio v2.8: + + * Simplify lua line manipulation API + + Collapses lua high(), low(), toggle(), config_high(), config_low(), + config_apply() into one simple function: + + set{=, ...} + + Line can be any of DTR, RTS, CTS, DSR, CD, RI. + + State is high, low, or toggle. + + Example: + script = set{DTR=high, RTS=low}; msleep(100); set{DTR=low, RTS=high}; msleep(100); set{RTS=low} + + Notice the use of {} instad of () when calling the set function. This is + required to pass parameters by name in lua. + + * Disable DEC Special Graphics at exit if vt100 + + If a vt100 terminal receives the Shift In character '\016' it will + enable the 7 bit DEC Special Graphics character set used for line drawing. + + For most users this can happen due to line noise from the tty device and + will likely mess up your terminal even after tio exits. + + To better handle this we want to make sure that tio disables this mode + by sending the Shift Out character '\017' at exit. + + This mechanism will only activate if environment variable TERM assumes + value "vt100". + + * Add hexN output mode + + Adds support for hexN mode where N is a number in the range 1 to 4096 + which defines how many hex values will be printed before a line break. + + In short, it defines the width of the hex output. + + In this mode, if timestamps are enabled they will be added to each hex + line. + + * Rename sub-config to profile + + Because better naming. + + * Use lua io.write() instead of print() + + io.write() gives better output control as print() is hardcoded to always + print a newline. + + * Add new ways to manage serial devices + + * Rename --list-devices to --list + + * Rename --no-autoconnect to --no-reconnect + + * Switch -l and -L options + + * -l now lists available serial devices + + * -L enables log to file + + * Add option --auto-connect + + * Supported strategies: + + * "new" - Waits to connect first new appearing serial device + + * "latest" - Connects to latest registered serial device + + * "direct" - Connect directly to specified serial device (default) + + * Add options to exclude serial devices from auto connect strategy by + pattern + + * Supported exclude options: + + * --exclude-devices + + Example: '--exclude-devices "/dev/ttyUSB2,/dev/ttyS?"' + + * --exclude-drivers + + Example: '--exclude-drivers "cdc_acm"' + + * --exclude-tids + + Example: '--exclude-tids "yW07,bCC2"' + + * Patterns support '*' and '?' + + * Connect to same port/device combination via unique topology ID (TID) + + * Topology ID is a 4 digit base62 encoded hash of a device topology + string coming from the Linux kernel. This means that whenever you + plug in the same e.g. USB serial port device to the same USB hub + port connected via the exact same hub topology all the way to your + computer, you will get the same unique TID. + + * Useful for stable reconnections when serial device has no serial + device by ID + + * For now, only tested on Linux. + + * Reworked and improved listing of serial devices to show serial devices: + + * By device + + * Including TID, uptime, driver, and description. + + * Sorted by uptime (newest device listed last) + + * By unique topology ID + + * By ID + + * By path + + * Add script interface 'list = tty_search()' for searching for serial + devices. + + * Clean up timestamp enum definition + + * Add missing options to show configuration + + * Update description of mute option + + * Add lua read_string() function + + * Don't forget to log output in lua expect() + + * Generalize automatic login example for Linux + + * Fix log output in hex output mode + + * Add timeout based timestamps in hex output mode + + This change reintroduces timestamping in hex output mode but based on + timeout instead of new lines which made no sense. This means that + timestamps will only be printed when timeout time has elapsed with no + output activity from serial device. + + Adds option --timestamp-timeout for setting the timeout value in + milliseconds. + + Defaults to 200 ms. + + * Improve switched messages + + * Extend lua expect() to also return matched string + + * Add automatic login script example + + * Organize examples directory + + * Introduce basic line input mode + + * Cleanup global variable name shadowing + +Davis C: + + * Updated login example with new expect logic + + * Reset buffer size at start of expect + + * Return 1 when `expect` matches @@ -53,11 +575,11 @@ Changes since tio v2.7: * Add lua modem_send(file,protocol) - * Fix xymodem error print outs + * Fix xymodem error messages * Rework x/y-modem transfer command - Remove ctrl-t X optin and instead introduce submenu to ctrl-t x option + Remove ctrl-t X option and instead introduce submenu to ctrl-t x option for picking which xmodem protocol to use. * Update README @@ -66,22 +588,22 @@ Changes since tio v2.7: * Add independent input and output mode - Replaces -x, --hexadecimal option with --intput-mode and --output-mode + Replaces -x, --hexadecimal option with --input-mode and --output-mode so it is possible to select hex or normal mode for both input and output independently. - To obtain same behaviour as -x, --hexadecimal use the following + To obtain same behavior as -x, --hexadecimal use the following configuration: input-mode = hex output-mode = hex - * Fix file descriptor handling on MacOS + * Fix file descriptor handling on macOS * Add tty line configuration script API On some platforms calling high()/low() to switch line states result in - costly system calls whick makes it impossible to swith two or more tty + costly system calls which makes it impossible to switch two or more tty lines simultaneously. To help solve this timing issue we introduce a tty line state @@ -117,7 +639,7 @@ Changes since tio v2.7: lines. Script is activated automatically on connect or manually via in session key command. - The Lua scripting feature opens up for many posibilities in the future + The Lua scripting feature opens up for many possibilities in the future such as adding expect like functionality to easily and programatically interact with the connected device. @@ -214,7 +736,7 @@ Changes since tio v2.5: Add --log-append option which makes tio append to any existing log file. - This also changes the default behaviour of tio from appending to + This also changes the default behavior of tio from appending to overwriting any existing log file. Now you have to use this new option to make tio append. @@ -548,7 +1070,7 @@ Changes since tio v1.46: Victor Oliveira - * add macports install instructions + * add MacPorts install instructions @@ -1048,7 +1570,7 @@ Changes since tio v1.35: * Handle SIGHUP Handle SIGHUP so that the registered exit handlers are called to restore - the terminal back to its orignal state. + the terminal back to its original state. * Add color configuration support @@ -1104,10 +1626,10 @@ Changes since tio v1.34: * Add support for configurable timestamp format - Also changes default timestamp format from ISO8601 to classic 24-hour + Also changes default timestamp format from ISO 8601 to classic 24-hour format as this is assumed to be the format that most users would prefer. - And reintroduces strict but optional ISO8601 format. + And reintroduces strict but optional ISO 8601 format. This feature allows to easily add more timestamp formats in the future. @@ -1228,10 +1750,10 @@ Sylvain LAFRASSE: attila-v: - * Refine timestamps with milliseconds and ISO-8601 format (#129). + * Refine timestamps with milliseconds and ISO 8601 format (#129). * Show milliseconds too in the timestamp (#114) and log file (#124) - * Change timestamp format to ISO-8601. + * Change timestamp format to ISO 8601. Yin Fengwei: @@ -1310,7 +1832,7 @@ Lars Kellogg-Stedman: George Stark: - * dont show line state if ioctl failed + * don't show line state if ioctl failed * add serial lines manual control @@ -1323,9 +1845,9 @@ arichi: Mariusz Midor: - * Newline: handle booth NL and CR + * Newline: handle both NL and CR - Flag ONLCRNL expects code \n after press Enter, but on some systems \r is send instead. + Flag ONLCRNL expects code \n after press Enter, but on some systems \r is sent instead. @@ -1817,7 +2339,7 @@ Changes since tio v1.11: To display the total number of bytes transmitted/received simply perform the 'ctrl-t s' command sequence. - This feature can be useful when eg. trying to detect non-printable + This feature can be useful when e.g. trying to detect non-printable characters. * Further simplification of key handling diff --git a/README.md b/README.md index 76a7ef6..e3ab066 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,12 @@ # tio - a serial device I/O tool -[![](https://img.shields.io/circleci/build/github/tio/tio)](https://circleci.com/github/tio/tio/tree/master) +[![](https://img.shields.io/github/actions/workflow/status/tio/tio/ubuntu.yml?label=Ubuntu)](https://github.com/tio/tio/actions/workflows/ubuntu.yml) +[![](https://img.shields.io/github/actions/workflow/status/tio/tio/macos.yml?label=MacOS)](https://github.com/tio/tio/actions/workflows/macos.yml) +[![](https://github.com/tio/tio/actions/workflows/codeql.yml/badge.svg)](https://github.com/tio/tio/actions/workflows/codeql.yml) +[![](https://img.shields.io/codefactor/grade/github/tio/tio)](https://www.codefactor.io/repository/github/tio/tio) [![](https://img.shields.io/github/v/release/tio/tio?sort=semver)](https://github.com/tio/tio/releases) [![](https://img.shields.io/repology/repositories/tio)](https://repology.org/project/tio/versions) - ## 1. Introduction @@ -20,7 +22,7 @@ I/O operations. ### 1.1 Motivation -To make a simpler serial device tool for talking with serial TTY devices with +To make a simpler serial device tool for working with serial TTY devices with less focus on classic terminal/modem features and more focus on the needs of embedded developers and hackers. @@ -31,39 +33,66 @@ when used in combination with [tmux](https://tmux.github.io). ## 2. Features * Easily connect to serial TTY devices - * Automatic connect and reconnect * Sensible defaults (115200 8n1) + * Automatic connection management + * Automatic detection of serial ports + * Automatic reconnect + * Automatically connect to first new appearing serial device + * Automatically connect to latest registered serial device + * Connect to same port/device combination via unique topology ID (TID) + * Useful for reconnecting when serial device has no serial device by ID * Support for non-standard baud rates * Support for mark and space parity * X-modem (1K/CRC) and Y-modem file upload * Support for RS-485 mode - * List available serial devices by ID + * List available serial devices + * By device + * Including topology ID, uptime, driver, description + * Sorted by uptime (newest device listed last) + * By ID + * By path * Show RX/TX statistics * Toggle serial lines * Pulse serial lines with configurable pulse duration * Local echo support * Remapping of characters (nl, cr-nl, bs, lowercase to uppercase, etc.) - * Line timestamps - * Support for delayed output per character - * Support for delayed output per line - * Switchable independent input and output mode (normal vs hex) - * Log to file with automatic or manual naming of log file + * Switchable independent input and output + * Normal mode + * Hex mode (output supports variable width) + * Line mode (input only) + * Timestamp support + * Per line in normal output mode + * Output timeout timestamps in hex output mode + * Support for delayed output + * Per character + * Per line + * Log to file + * Automatic naming of log file (default) + * Configurable directory for saving automatic named log files + * Manual naming of log file + * Overwrite (default) or append to log file + * Strip control characters and escape sequences * Configuration file support - * Activate sub-configurations by name or pattern - * Redirect I/O to UNIX socket or IPv4/v6 network socket for scripting or TTY sharing + * Support for configuration profiles + * Activate configuration profiles by name or pattern + * Support for including other configuration files + * Redirect I/O of shell command to serial device + * Redirect I/O to UNIX socket or IPv4/v6 network socket + * Useful for scripting or TTY sharing * Pipe input and/or output - * Bash completion on options, serial device names, and sub-configuration names - * Configurable text color + * Bash completion on options, serial device names, and profile names + * Configurable tio message text color + * Supports NO_COLOR env variable as per [no-color.org](https://no-color.org) * Visual or audible alert on connect/disconnect * Remapping of prefix key - * Support NO_COLOR env variable as per no-color.org - * Man page documentation * Lua scripting support for automation - * Run script manually or automatically at connect once/always/never + * Run script manually or automatically at connect (once/always/never) * Simple expect/send like functionality with support for regular expressions - * Manipulate port control lines (useful for microcontroller reset/boot etc.) + * Manipulate port modem lines (useful for microcontroller reset/boot etc.) * Send files via x/y-modem protocol - * Plays nicely with [tmux](https://tmux.github.io) + * Search for serial devices + * Man page documentation + * Plays nicely with [tmux](https://tmux.github.io) and similar terminal multiplexers ## 3. Usage @@ -73,11 +102,11 @@ For more usage details please see the man page documentation ### 3.1 Command-line The command-line interface is straightforward as reflected in the output from -'tio --help': +```tio --help```: ``` -Usage: tio [] +Usage: tio [] -Connect to TTY device directly or via sub-configuration. +Connect to TTY device directly or via configuration profile or topology ID. Options: -b, --baudrate Baud rate (default: 115200) @@ -88,14 +117,19 @@ Options: -o, --output-delay Output character delay (default: 0) -O, --output-line-delay Output line delay (default: 0) --line-pulse-duration Set line pulse duration - -n, --no-autoconnect Disable automatic connect + -a, --auto-connect new|latest|direct Automatic connect strategy (default: direct) + --exclude-devices Exclude devices by pattern + --exclude-drivers Exclude drivers by pattern + --exclude-tids Exclude topology IDs by pattern + -n, --no-reconnect Do not reconnect -e, --local-echo Enable local echo - --input-mode normal|hex Select input mode (default: normal) - --output-mode normal|hex Select output mode (default: normal) + --input-mode normal|hex|line Select input mode (default: normal) + --output-mode normal|hex|hexN Select output mode (default: normal) -t, --timestamp Enable line timestamp --timestamp-format Set timestamp format (default: 24hour) - -L, --list-devices List available serial devices by ID - -l, --log Enable log to file + --timestamp-timeout Set timestamp timeout (default: 200) + -l, --list List available serial devices, TIDs, and profiles + -L, --log Enable log to file --log-file Set log filename --log-directory Set log directory path for automatic named logs --log-append Append to log file @@ -106,26 +140,27 @@ Options: --rs-485 Enable RS-485 mode --rs-485-config Set RS-485 configuration --alert bell|blink|none Alert on connect/disconnect (default: none) - --mute Mute tio + --mute Mute tio messages --script Run script from string --script-file Run script from file --script-run once|always|never Run script on connect (default: always) + --exec Execute shell command with I/O redirected to device -v, --version Display version -h, --help Display help -Options and sub-configurations may be set via configuration file. +Options and profiles may be set via configuration file. + +In session you can press ctrl-t ? to list available key commands. See the man page for more details. ``` -By default tio automatically connects to the provided TTY 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 is unplugged), it will wait for the -device to reappear and then reconnect. However, if the `--no-autoconnect` -option is provided, tio will exit if the device is not present or an -established connection is lost. - -tio features full bash autocompletion. +By default tio automatically connects to the provided TTY device. If the device +is not present, tio will wait for it to appear and then connect. If the +connection is lost (e.g. device is unplugged), it will wait for the device to +reappear and then reconnect. However, if the `--no-reconnect` option is +provided, tio will exit if the device is not present or an established +connection is lost. #### 3.1.1 Examples @@ -139,43 +174,126 @@ Which corresponds to the commonly used default options: $ tio --baudrate 115200 --databits 8 --flow none --stopbits 1 --parity none /dev/ttyUSB0 ``` +List available serial devices: +``` +$ tio --list +Device TID Uptime [s] Driver Description +----------------- ---- ------------- ---------------- -------------------------- +/dev/ttyS4 BaaB 19526.576 port 16550A UART +/dev/ttyS5 eV0Z 19525.845 port 16550A UART +/dev/ttyUSB1 bCC2 1023.274 ftdi_sio TTL232R-3V3 +/dev/ttyUSB0 SPpw 978.527 ftdi_sio TTL232RG-VREG3V3 +/dev/ttyACM0 i5q4 2.079 cdc_acm ST-Link VCP Ctrl + +By-id +-------------------------------------------------------------------------------- +/dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTCHUV56-if00-port0 +/dev/serial/by-id/usb-FTDI_TTL232RG-VREG3V3_FT1NELUB-if00-port0 +/dev/serial/by-id/usb-STMicroelectronics_STLINK-V3_004900343438510234313939-if02 + +By-path +-------------------------------------------------------------------------------- +/dev/serial/by-path/pci-0000:00:14.0-usb-0:8.1.3.1.4:1.0-port0 +/dev/serial/by-path/pci-0000:00:14.0-usbv2-0:8.1.3.1.4:1.0-port0 +/dev/serial/by-path/pci-0000:00:14.0-usbv2-0:6.4:1.0-port0 +/dev/serial/by-path/pci-0000:00:14.0-usb-0:6.4:1.0-port0 +/dev/serial/by-path/pci-0000:00:14.0-usbv2-0:6.3:1.2 +/dev/serial/by-path/pci-0000:00:14.0-usb-0:6.3:1.2 + +Configuration profiles (/home/lundmar/.config/tio/config) +-------------------------------------------------------------------------------- +rpi3 stm32 esp32 am64-evm +imx8mp-evk nucleo-h743zi2 usb-devices +``` + It is recommended to connect serial TTY devices by ID: ``` -$ tio /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0 +$ tio /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTCHUV56-if00-port0 ``` -Using serial devices by ID ensures that tio automatically reconnects to the -correct serial device if it is disconnected and then reconnected. +Note: Using serial devices by ID helps ensure that tio automatically reconnects +to the same serial device when reattached, even when it enumerates differently. -List available serial devices by ID: +If no serial device by ID is available it is recommended to connect via +topology ID (TID): ``` -$ tio --list-devices +$ tio bCC2 +``` +Note: The TID is unique and will stay the same as long as your USB serial port +device plugs into the same USB topology (same ports, same hubs, same +connections, etc.). This way it is possible for tio to successfully reconnect +to the same device. + +Connect automatically to first new appearing serial device: +``` +$ tio --auto-connect new +``` + +Connect automatically to latest registered serial device: +``` +$ tio --auto-connect latest +``` + +It is possible to use exclude options to affect which serial devices are +involved in the automatic connection strategy: +``` +$ tio --auto-connect new --exclude-devices "/dev/ttyACM?,/dev/ttyUSB2" +``` + +And to exclude drivers by pattern: +``` +$ tio --auto-connect new --exclude-drivers "cdc_acm,ftdi_sio" +``` +Note: Pattern matching supports '*' and '?'. Use comma separation to define +multiple patterns. + +To include drivers by specific pattern simply negate the exclude option: +``` +$ tio --auto-connect new --exclude-drivers !("cp2102") ``` -Note: One can also use tio shell completion on /dev which will automatically -list all available serial TTY devices. Log to file with autogenerated filename: ``` $ tio --log /dev/ttyUSB0 ``` +Log to file with specific filename: +``` +$ tio --log --log-file my-log.txt +``` + Enable ISO8601 timestamps per line: ``` $ tio --timestamp --timestamp-format iso8601 /dev/ttyUSB0 ``` +Output to hex with width 16: +``` +$ tio --output-mode hex16 /dev/ttyUSB0 +``` + Redirect I/O to IPv4 network socket on port 4242: ``` $ tio --socket inet:4242 /dev/ttyUSB0 ``` +Map NL to CR-NL on input from device and DEL to BS on output to device: +``` +$ tio --map INLCRNL,ODELBS /dev/ttyUSB0 +``` + Pipe data to the serial device: ``` $ cat data.bin | tio /dev/ttyUSB0 ``` +Manipulate modem lines on connect: +``` +$ tio --script "tio.set{DTR=high,RTS=low}; tio.msleep(100); tio.set{DTR=toggle,RTS=toggle}" /dev/ttyUSB0 +``` + Pipe command to serial device and wait for line response within 1 second: ``` -$ echo "*IDN?" | tio /dev/ttyACM0 --script "expect('\r\n', 1000)" --mute +$ echo "*IDN?" | tio /dev/ttyACM0 --script "tio.expect('\r\n', 1000)" --mute KORAD KD3305P V4.2 SN:32475045 ``` @@ -196,14 +314,14 @@ ctrl-t ? to list the available key commands. [15:02:53.269] ctrl-t i Toggle input mode [15:02:53.269] ctrl-t l Clear screen [15:02:53.269] ctrl-t L Show line states -[15:02:53.269] ctrl-t m Toggle MSB to LSB bit order +[15:02:53.269] ctrl-t m Change mapping of characters on input or output [15:02:53.269] ctrl-t o Toggle output mode [15:02:53.269] ctrl-t p Pulse serial port line [15:02:53.269] ctrl-t q Quit [15:02:53.269] ctrl-t r Run script +[15:02:53.269] ctrl-t R Execute shell command with I/O redirected to device [15:02:53.269] ctrl-t s Show statistics [15:02:53.269] ctrl-t t Toggle line timestamp mode -[15:02:53.269] ctrl-t U Toggle conversion to uppercase on output [15:02:53.269] ctrl-t v Show version [15:02:53.269] ctrl-t x Send file via Xmodem [15:02:53.269] ctrl-t y Send file via Ymodem @@ -212,63 +330,7 @@ ctrl-t ? to list the available key commands. If needed, the prefix key (ctrl-t) can be remapped via configuration file. -### 3.3 Lua script API - -Tio suppots Lua scripting to easily automate interaction with the tty device. - -In addition to the Lua API tio makes the following functions available: - -``` - expect(string, timeout) - Expect string - waits for string to match or timeout before continueing. - - Supports regular expressions. Special characters must be escaped with '\\'. - - Timeout is in milliseconds, defaults to 0 meaning it will wait forever. - - send(string) - Send string. - - modem_send(file, protocol) - Send file using x/y-modem protocol. - - Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM. - - exit(code) - Exit with code. - - high(line) - Set tty line high. - - low(line) - Set tty line low. - - toggle(line) - Toggle the tty line. - - sleep(seconds) - Sleep for seconds. - - msleep(ms) - Sleep for milliseconds. - - config_high(line) - Set tty line state configuration to high. - - config_low(line) - Set tty line state configuration to low. - - apply_config() - Apply tty line state configuration. - - Using the line state configuration API instead of high()/low() will - help to make the lines physically switch as simultaneously as possible. - This may solve timing issues on some platforms. - - Note: Line can be any of DTR, RTS, CTS, DSR, CD, RI -``` - -### 3.4 Configuration file +### 3.3 Configuration file Options can be set via the configuration file first found in any of the following locations in the order listed: @@ -276,17 +338,17 @@ following locations in the order listed: - $HOME/.config/tio/config - $HOME/.tioconfig -The configuration file supports sub-configurations using named sections which can -be activated via the command-line by name or pattern. A sub-configuration -specifies which TTY device to connect to and other options. +The configuration file supports profiles using named sections which can be +activated via the command-line by name or pattern. A profile specifies which +TTY device to connect to and other options. -### 3.4.1 Examples +### 3.3.1 Example Example configuration file: ``` -# Defaults -baudrate = 9600 +[default] +baudrate = 115200 databits = 8 parity = none stopbits = 1 @@ -294,31 +356,31 @@ color = 10 [rpi3] device = /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0 -baudrate = 115200 -no-autoconnect = enable -log = enable +no-reconnect = true +log = true log-file = rpi3.log line-pulse-duration = DTR=200,RTS=150 color = 11 [svf2] device = /dev/ttyUSB0 -script = expect("login: "); send("root\n"); expect("Password: "); send("root\n") +baudrate = 9600 +script = tio.expect("login: "); tio.write("root\n"); tio.expect("Password: "); tio.write("root\n") color = 12 [esp32] device = /dev/serial/by-id/usb-0403_6014-if00-port0 -script = high(DTR); low(RTS); msleep(100); low(DTR); high(RTS); msleep(100); low(RTS) +script = tio.set{DTR=high,RTS=low}; tio.msleep(100); tio.set{DTR=low,RTS=high}; tio.msleep(100); tio.set{RTS=low} script-run = once color = 13 -[usb devices] -pattern = usb([0-9]*) -device = /dev/ttyUSB%s +[usb-devices] +pattern = ^usb([0-9]*) +device = /dev/ttyUSB%m1 color = 14 ``` -To use a specific sub-configuration by name simply start tio like so: +To use a specific profile by name simply start tio like so: ``` $ tio rpi3 ``` @@ -327,7 +389,86 @@ Or by pattern match: $ tio usb12 ``` -Another more elaborate configuration file example is available [here](example/config). +Another more elaborate configuration file example is available [here](examples/config/config). + +### 3.4 Lua script API + +Tio suppots Lua scripting to easily automate interaction with the tty device. + +In addition to the standard Lua API tio makes the following functions +and variables available: + + +#### `tio.expect(pattern, timeout)` + +Waits for the Lua pattern to match or timeout before continuing. +Timeout is in milliseconds, defaults to 0 meaning it will wait forever. + +Returns the captures from the pattern or `nil` on timeout. + +#### `tio.read(size, timeout)` + +Read up to `size` bytes from serial device. If timeout is 0 or not provided it +will wait forever until data is ready to read. + +Returns a string up to `size` bytes long on success and `nil` on timeout. + +#### `tio.readline(timeout)` + +Read line from serial device. If timeout is 0 or not provided it will wait +forever until data is ready to read. + +Returns a string on success and `nil` on timeout. On timeout a partially read +line may be returned as a second return value. + +#### `tio.write(string)` + +Write string to serial device. + +Returns the `tio` table. + +#### `tio.send(file, protocol)` + +Send file using x/y-modem protocol. + +Protocol can be any of `XMODEM_1K`, `XMODEM_CRC`, `YMODEM`. + +#### `tio.ttysearch()` + +Search for serial devices. + +Returns a table of number indexed tables, one for each serial device found. +Each of these tables contains the serial device information accessible via the +following string indexed elements "path", "tid", "uptime", "driver", +"description". + +Returns `nil` if no serial devices are found. + +#### `tio.set{line=state, ...}` +Set state of one or multiple tty modem lines. + +Line can be any of `DTR`, `RTS`, `CTS`, `DSR`, `CD`, `RI` + +State is `high`, `low`, or `toggle`. + +#### `tio.sleep(seconds)` + +Sleep for seconds. + +#### `tio.msleep(ms)` + +Sleep for milliseconds. + +#### `tio.alwaysecho` + +A boolean value, defaults to `true`. + +If `tio.alwaysecho` is `false`, the result of `tio.read`, `tio.readline` or +`tio.expect` will only be returned from the function and not logged or printed. + +If `tio.alwaysecho` is set to `true`, reading functions also emit a single +timestamp to stdout and log file per `options.timestamp` and `options.log`. + ## 4. Installation @@ -367,6 +508,12 @@ $ pacman -S tio The latest source releases can be found [here](https://github.com/tio/tio/releases). +Before running the install steps make sure you have glib and lua libraries installed. For example: + +``` +$ sudo apt install libglib2.0-dev liblua5.2-dev +``` + Install steps: ``` $ meson setup build @@ -382,9 +529,13 @@ Note: The meson install steps may differ depending on your specific system. Getting permission access errors trying to open your serial device? -Add your user to the group which allows serial device access. For example, to add your user to the 'dialout' group do: +Add your user to the group which allows serial device access permanently. For example, to add your user to the 'dialout' group do: +```bash +sudo usermod -a -G dialout ``` -$ sudo usermod -a -G dialout +Switch to the "dialout" group, temporary but immediately for this session. +```bash +newgrp dialout ``` diff --git a/TODO b/TODO index 9fc3626..6036c7b 100644 --- a/TODO +++ b/TODO @@ -1,67 +1,54 @@ -* Add additional hex mode formats + * Add release support for arm and x86 binary tarballs - - Support traditional hex output format such as: + * Support input and input mapping from lua scripts - 00000000 74 65 73 74 20 74 65 73 74 20 74 65 73 74 20 74 |test test test t| - 00000010 65 73 74 64 66 0a 61 0a 66 61 0a 66 0a 61 73 66 |estdf.a.fa.f.asf| - 00000020 64 61 64 73 66 61 73 66 64 61 73 64 66 61 64 73 |dadsfasfdasdfads| - 00000030 66 0a 61 73 64 66 61 64 73 66 61 73 64 66 61 73 |f.asdfadsfasdfas| - 00000040 64 66 0a 61 73 64 66 61 64 73 66 61 73 64 66 61 |df.asdfadsfasdfa| - 00000050 73 64 66 66 64 61 73 64 66 0a 0a 31 32 33 31 0a |sdffdasdf..1231.| - 00000060 65 32 31 64 73 77 65 64 0a 0a |e21dswed..| - 0000006a + * Add option to send file raw (no modem protocol) - With configurable width output (4, 8, 16 bytes etc.). E.g. config file: + * Add loopback option - hex-mode-width = 16 + Send received serial input back to output (for testing etc.) - With and without timestamp support for each broken up line. + * Add loopback support between two serial ports - * Add support for activity based time stamping in both normal and hex-mode. + Useful for traffic monitoring - Will print a new timestamp if there is no input activity within the time - defined in the configuration file, e.g.: + * Add mapping feature for printing non-printable characters - timestamp-timeout = 200 + * Porting layer to support native win32 builds. - Time is in ms. + Some of the work that needs to be done: -* Line mode feature + All posix functions need to be platform independent, go though file by file: - Only send line when pressing enter. Maybe even add readline support so one - can use all the readline editing feature before sending the line. + termios.h + unistd.h has very limited functions + ENV different in config_file_resolve + errno + sys/ioctl.h + sys/poll.h + socket, may need a new thread + Serial, RS485, character mapping + Communication pipe -* Support for interaction using simple autoresponse strings + Port enumerate, all devices of the same type have the same name (eg. USB + Serial Device for ttyACM) -> which makes regex not meaningful (kind of a + good thing since libtre in Mingw has too much dependencies makes binary too + big) - Add support for simple autoresponse strings in the configuration file. For - example: + * Support traditional hex output format such as: - autoresponse = expect:'localhost login: ', send:'root\n', - expect:'Password: ', send:'abcd1234\n' + 00000000 74 65 73 74 20 74 65 73 74 20 74 65 73 74 20 74 |test test test t| + 00000010 65 73 74 64 66 0a 61 0a 66 61 0a 66 0a 61 73 66 |estdf.a.fa.f.asf| + 00000020 64 61 64 73 66 61 73 66 64 61 73 64 66 61 64 73 |dadsfasfdasdfads| + 00000030 66 0a 61 73 64 66 61 64 73 66 61 73 64 66 61 73 |f.asdfadsfasdfas| + 00000040 64 66 0a 61 73 64 66 61 64 73 66 61 73 64 66 61 |df.asdfadsfasdfa| + 00000050 73 64 66 66 64 61 73 64 66 0a 0a 31 32 33 31 0a |sdffdasdf..1231.| + 00000060 65 32 31 64 73 77 65 64 0a 0a |e21dswed..| + 0000006a - When expect line is matched tio will respond by writing the send string. + * Add support for activity based time stamping in normal output mode - When parsing the autoresponse variable make sure matching expect/send pairs - else provide warning. - - Maybe support regex matching in expect string to make feature more powerful. - - This is mostly a convenience feature. For more powerful scripted interaction - users can continue use the socket feature in combination with the expect tool - as described in the man page. - - Maybe provide a mechanism to disable autoresponse feature. Maybe by defining - maximum match count and/or in session key command to toggle feature. - - * Support for running external command - - Add key command e.g. 'ctrl-t r' which prompts user to run external command. - The command will be run in a process which stdin/stdout is redirected to the - serial port. - - This is the first step towards maybe also adding automatic support for - x/y/zmodem data transfer protocols by calling external programs such as - rb/sb, rx/sx, rz/sz, etc. + Already supported in hex output mode. * Allow tio to connect to socket diff --git a/example/control-lines-test.lua b/example/control-lines-test.lua deleted file mode 100644 index 9f0d493..0000000 --- a/example/control-lines-test.lua +++ /dev/null @@ -1,7 +0,0 @@ -high(DTR) -low(RTS) -msleep(100) -low(DTR) -high(RTS) -msleep(100) -low(RTS) diff --git a/example/config b/examples/config/config similarity index 69% rename from example/config rename to examples/config/config index 5f72f2a..7bd141e 100644 --- a/example/config +++ b/examples/config/config @@ -9,28 +9,30 @@ # $HOME/.config/tio/config # $HOME/.tioconfig -# Defaults +[default] baudrate = 115200 databits = 8 flow = none stopbits = 1 parity = none -prefix-ctrl-key = t output-delay = 0 output-line-delay = 0 -no-autoconnect = disable -hexadecimal = disable -timestamp = disable -log = disable -log-append = disable -log-strip = disable -local-echo = disable +auto-connect = direct +no-reconnect = false +local-echo = false +input-mode = normal +output-mode = normal +timestamp = false +log = false +log-append = false +log-strip = false color = bold -rs-485 = disable +rs-485 = false alert = none script-run = always +prefix-ctrl-key = t -# Sub-configurations +# Configuration profiles [rpi3] baudrate = 115200 @@ -48,25 +50,29 @@ color = 10 [tincan] baudrate = 9600 device = /dev/serial/by-id/usb-TinCanTools_Flyswatter2_FS20000-if00-port0 -log = enable +log = true log-file = tincan.log -log-strip = enable +log-strip = true color = 11 -[usb] -pattern = usb([0-9]*) -device = /dev/ttyUSB%s +[usb-devices] +pattern = ^usb([0-9]*) +device = /dev/ttyUSB%m1 color = 12 [rs-485-device] device = /dev/ttyUSB0 -rs-485 = enable +rs-485 = true rs-485-config = RTS_ON_SEND=1,RTS_AFTER_SEND=1,RTS_DELAY_BEFORE_SEND=60,RTS_DELAY_AFTER_SEND=80,RX_DURING_TX color = 13 [esp32] device = /dev/ttyUSB0 color = 14 -script = high(DTR); low(RTS); msleep(100); low(DTR); high(RTS); msleep(100); low(RTS) +script = tio.set{DTR=high,RTS=low}; tio.msleep(100); tio.set{DTR=low,RTS=high}; tio.msleep(100); tio.set{RTS=low} script-run = always +[buspirate] +device = /dev/ttyACM0 +map = INLCRNL,ODELBS +color = 15 diff --git a/examples/lua/automatic-linux-login.lua b/examples/lua/automatic-linux-login.lua new file mode 100644 index 0000000..aa7a990 --- /dev/null +++ b/examples/lua/automatic-linux-login.lua @@ -0,0 +1,28 @@ +local logins = { + ["foo"] = { + username = "foouser", + password = "foopass", + }, + ["bar"] = { + username = "baruser", + password = "barpass", + }, + ["baz"] = { + username = "bazuser", + password = "bazpass", + }, +} + +local hostname = tio.expect("^(%g+) login:", 10) +if hostname then + local login = logins[hostname] + if (nil ~= login) then + tio.write(login.username .. "\n") + tio.expect("Password:") + tio.write(login.password .. "\n") + else + io.write("\r\nDon't know login info for " .. hostname .. "\r\n") + end +else + io.write("\r\nDidn't find a login prompt\r\n") +end diff --git a/examples/lua/control-lines-test.lua b/examples/lua/control-lines-test.lua new file mode 100644 index 0000000..55d98b5 --- /dev/null +++ b/examples/lua/control-lines-test.lua @@ -0,0 +1,5 @@ +tio.set{DTR=high, RTS=low} +tio.msleep(100) +tio.set{DTR=low, RTS=high} +tio.msleep(100) +tio.set{RTS=toggle} diff --git a/examples/lua/read.lua b/examples/lua/read.lua new file mode 100644 index 0000000..6452816 --- /dev/null +++ b/examples/lua/read.lua @@ -0,0 +1,14 @@ +tio.read(1000, 6000) -- initial config +tio.write("\n") +tio.msleep(100) +tio.read(650, 60) -- main menu +tio.write("S") -- S menu +tio.msleep(30) +tio.read(650, 60) +tio.write("t") -- Parallel Value Table +tio.read(650, 60) +while true do + tio.msleep(1000) + tio.write("t") + tio.read(650, 50) -- repeat PVT forever +end diff --git a/examples/lua/read_line.lua b/examples/lua/read_line.lua new file mode 100644 index 0000000..a844b48 --- /dev/null +++ b/examples/lua/read_line.lua @@ -0,0 +1,15 @@ +tio.read(1000, 8000) -- read initial config +tio.write("\n") +tio.read(650, 100) -- main menu +tio.write("S") -- S menu +repeat + str = tio.readline(25) +until str == nil +while true do + tio.write("t") -- query PV table + tio.msleep(880) + repeat + str = tio.readline(60) + tio.msleep(60) + until str == nil +end diff --git a/examples/lua/serial-device-search.lua b/examples/lua/serial-device-search.lua new file mode 100644 index 0000000..77d9dbc --- /dev/null +++ b/examples/lua/serial-device-search.lua @@ -0,0 +1,13 @@ +io.write("Searching... ") + +local device = tio.ttysearch() + +io.write("done\r\n") + +for i in ipairs(device) do + io.write("\r\n" .. device[i]["path"] .. "\r\n") + io.write(" tid = " .. device[i]["tid"] .. "\r\n") + io.write(" uptime = " .. device[i]["uptime"] .. "\r\n") + io.write(" driver = " .. device[i]["driver"] .. "\r\n") + io.write(" description = " .. device[i]["description"] .. "\r\n") +end diff --git a/man/tio.1.in b/man/tio.1.in index 875a881..eb0338d 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -6,7 +6,7 @@ tio \- a serial device I/O tool .SH "SYNOPSIS" .PP .B tio -.RI "[" "] " "" +.RI "[" "] " "" .SH "DESCRIPTION" .PP @@ -80,17 +80,48 @@ The default pulse duration for each line is 100 ms. .RE .TP -.BR \-n ", " \-\-no\-autoconnect +.BR "\-a, \-\-auto\-connect new|latest|direct" -Disable automatic connect. +Automatically connect to serial device according to one of the following +strategies: -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. +.RS +.TP 10n +.IP "\fBnew" +Automatically connect to first new appearing serial device. +.IP "\fBlatest" +Automatically connect to latest registered serial device. +.IP "\fBdirect" +Connect directly to specified TTY device. +.P +All the listed strategies automatically reconnects according to strategy if +device is not available or connection is lost. +.P +Default value is "direct". +.RE -However, if the \fB\-\-no\-autoconnect\fR option is provided, tio will exit if -the device is not present or an established connection is lost. +.TP +.BR " \-\-exclude\-devices \fI" + +Exclude devices by pattern ('*' and '?' supported). + +.TP +.BR " \-\-exclude\-drivers \fI" + +Exclude drivers by pattern ('*' and '?' supported). + +.TP +.BR " \-\-exclude\-tids \fI" + +Exclude topology IDs by pattern ('*' and '?' supported). + +.TP +.BR \-n ", " \-\-no\-reconnect + +Do not reconnect. + +This means that tio will exit if it fails to connect to device or an +established connection is lost. .TP .BR \-e ", " "\-\-local\-echo @@ -103,7 +134,7 @@ Enable local echo. Enable line timestamp. .TP -.BR " \-\-timestamp\-format \fI +.BR " \-\-timestamp\-format \fI" Set timestamp format to any of the following timestamp formats: .RS @@ -117,22 +148,37 @@ Set timestamp format to any of the following timestamp formats: 24-hour format relative to previous timestamp .IP "\fBiso8601" ISO8601 format ("YYYY-MM-DDThh:mm:ss.sss") +.IP "\fBepoch" +Seconds since Unix epoch (1970-01-01) +.IP "\fBepoch-usec" +Seconds since Unix epoch (1970-01-01) with subdivision in microseconds .PP Default format is \fB24hour\fR .RE .TP -.BR \-L ", " \-\-list\-devices +.BR " \-\-timestamp\-timeout \fI" -List available serial devices by ID. +Set timestamp timeout value in milliseconds. + +This value only takes effect in hex output mode where timestamps are only +printed after elapsed timeout time of no output activity from tty device. + +Default value is 200. .TP -.BR \-l ", " \-\-log +.BR \-l ", " \-\-list + +List available targets (serial devices, TIDs, configuration profiles). + +.TP +.BR \-L ", " \-\-log Enable log to file. The log file will be automatically named using the following format -tio_DEVICE_YYYY-MM-DDTHH:MM:SS.log. +tio_TARGET_YYYY-MM-DDTHH:MM:SS.log. Target being the command line target such +as tty-device, tid, or configuration profile. The filename can be manually set using the \-\-log-file option. @@ -159,8 +205,8 @@ Strip control characters and escape sequences from log. .TP .BR \-m ", " "\-\-map " \fI -Map (replace, translate) characters on input or output. The following mapping -flags are supported: +Map (replace, translate) characters on input to the serial device or output +from the serial device. The following mapping flags are supported: .RS .TP 12n @@ -174,6 +220,10 @@ Map FF to ESC-c on input Map NL to CR on input .IP "\fBINLCRNL" Map NL to CR-NL on input +.IP "\fBICRCRNL" +Map CR to CR-NL on input +.IP "\fBIMSB2LSB" +Map MSB bit order to LSB on input .IP "\fBOCRNL" Map CR to NL on output .IP "\fBODELBS" @@ -184,25 +234,37 @@ Map NL to CR-NL on output Map lowercase characters to uppercase on output .IP "\fBONULBRK" Map nul (zero) to send break signal on output -.IP "\fBMSB2LSB" -Map MSB bit order to LSB on output +.IP "\fBOIGNCR" +Ignore CR on output .P If defining more than one flag, the flags must be comma separated. .RE .TP -.BR " \-\-input\-mode " normal|hex +.BR " \-\-input\-mode " normal|hex|line -Set input mode. In hex input mode bytes can be sent by typing the -\fBtwo-character hexadecimal\fR representation of the 1 byte value, e.g.: to -send \fI0xA\fR you must type \fI0a\fR or \fI0A\fR. +Set input mode. + +In normal mode input characters are sent immediately as they are typed. + +In hex input mode bytes can be sent by typing the \fBtwo-character +hexadecimal\fR representation of the 1 byte value, e.g.: to send \fI0xA\fR you +must type \fI0a\fR or \fI0A\fR. + +In line input mode input characters are sent when you press enter. The only +editing feature supported in this mode is backspace. Default value is "normal". .TP -.BR " \-\-output\-mode " normal|hex +.BR " \-\-output\-mode " normal|hex|hexN -Set output mode. In hex mode each incoming byte is printed out as a 1 byte hex value. +Set output mode. + +In hex mode each incoming byte is printed out as a 1 byte hex value. + +In hexN mode, N is a number less than or equal to 4096 which defines how many +hex values will be printed before a line break. Default value is "normal". @@ -282,6 +344,11 @@ will sound the bell twice or blink twice on disconnect. Default value is "none". +.TP +.BR "\-\-mute" + +Mute tio messages. + .TP .BR "\-\-script \fI @@ -299,6 +366,15 @@ Run script on connect once, always, or never. Default value is "always". +.TP +.BR "\-\-exec \fI + +Execute shell command with I/O redirected to device + +.TP +.BR "\-\-complete-profiles + +Prints profiles (for shell completion) .TP .BR \-v ", " \-\-version @@ -307,10 +383,10 @@ Display program version. .BR \-h ", " \-\-help Display help. -.SH "KEYS" +.SH "KEY COMMANDS" .PP .TP 16n -In session, all key strokes are forwarded to the serial device except the following key sequence: a prefix key (default: ctrl-t) followed by a command key. These sequences are intercepted as tio commands: +In session, all key strokes are forwarded to the serial device except the following key sequence: a prefix key (default: ctrl-t) followed by a command key. These sequences are intercepted as key commands: .IP "\fBctrl-t ?" List available key commands .IP "\fBctrl-t b" @@ -332,7 +408,7 @@ Clear screen .IP "\fBctrl-t L" Show line states (DTR, RTS, CTS, DSR, DCD, RI) .IP "\fBctrl-t m" -Toggle MSB to LSB bit order +Change mapping of characters on input or output .IP "\fBctrl-t o" Toggle output mode .IP "\fBctrl-t p" @@ -341,12 +417,12 @@ Pulse serial port line Quit .IP "\fBctrl-t r" Run script +.IP "\fBctrl-t R" +Execute shell command with I/O redirected to device .IP "\fBctrl-t s" Show TX/RX statistics .IP "\fBctrl-t t" Toggle line timestamp mode -.IP "\fBctrl-t U" -Toggle conversion to uppercase on output .IP "\fBctrl-t v" Show version .IP "\fBctrl-t x" @@ -360,43 +436,70 @@ Send ctrl-t character .PP Tio suppots Lua scripting to easily automate interaction with the tty device. -In addition to the Lua API tio makes the following functions available: +In addition to the standard Lua API tio makes the following functions +and variables available: .TP 6n -.IP "\fBexpect(string, timeout)" -Expect string - waits for string to match or timeout before continueing. -Supports regular expressions. Special characters must be escaped with '\\\\'. +.IP "\fBtio.expect(pattern, timeout)" +Waits for the Lua pattern to match or timeout before continuing. Timeout is in milliseconds, defaults to 0 meaning it will wait forever. -.IP "\fBsend(string)" -Send string. -.IP "\fBmodem_send(file, protocol)" + +Returns the captures from the pattern or nil on timeout. + +.IP "\fBtio.read(size, timeout)" +Read up to size bytes from serial device. If timeout is 0 or not provided it +will wait forever until data is ready to read. + +Returns a string up to size bytes long on success and nil on timeout. + +.IP "\fBtio.readline(timeout)" +Read line from serial device. If timeout is 0 or not provided it will wait +forever until data is ready to read. + +Returns a string on success and nil on timeout. On timeout a partially read +line may be returned as a second return value. + +.IP "\fBtio.write(string)" +Write string to serial device. + +Returns the tio table. + +.IP "\fBtio.send(file, protocol)" Send file using x/y-modem protocol. Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM. -.IP "\fBexit(code)" -Exit with exit code. -.IP "\fBhigh(line)" -Set tty line high. -.IP "\fBlow(line)" -Set tty line low. -.IP "\fBtoggle(line)" -Toggle the tty line. -.IP "\fBsleep(seconds)" -Sleep for seconds. -.IP "\fBmsleep(ms)" -Sleep for miliseconds. -.IP "\fBconfig_high(line)" -Set tty line state configuration to high. -.IP "\fBconfig_low(line)" -Set tty line state configuration to low. -.IP "\fBapply_config()" -Apply tty line state configuration. Using the line state configuration API -instead of high()/low() will help to make the lines physically switch as -simultaneously as possible. This may solve timing issues on some platforms. -.TP 0n -Note: Line can be any of DTR, RTS, CTS, DSR, CD, RI +.IP "\fBtio.ttysearch()" +Search for serial devices. + +Returns a table of number indexed tables, one for each serial device found. +Each of these tables contains the serial device information accessible via the +following string indexed elements "path", "tid", "uptime", "driver", +"description". + +Returns nil if no serial devices are found. + +.IP "\fBtio.set{line=state, ...}" +Set state of one or multiple tty modem lines. + +Line can be any of DTR, RTS, CTS, DSR, CD, RI + +State is high, low, or toggle. + +.IP "\fBtio.sleep(seconds)" +Sleep for seconds. +.IP "\fBtio.msleep(ms)" +Sleep for milliseconds. + +.IP "\fBtio.alwaysecho" +A boolean value, defaults to true. + +If tio.alwaysecho is false, the result of tio.read, tio.readline or tio.expect +will only be returned from the function and not logged or printed. + +If tio.alwaysecho is set to true, reading functions also emit a single +timestamp to stdout and log file per options.timestamp and options.log. .SH "CONFIGURATION FILE" .PP @@ -412,11 +515,11 @@ listed: .I $HOME/.tioconfig .PP -Labels can be used to group settings into named sub-configurations which can be -activated from the command-line when starting tio. +Labels can be used to group settings into named configuration profiles which +can be activated from the command-line when starting tio. .PP -\fBtio\fR will try to match the user input to a sub-configuration by name or by +\fBtio\fR will try to match the user input to a configuration profile by name or by pattern to get the TTY device and other options. .PP @@ -449,8 +552,8 @@ Set output character delay Set output line delay .IP "\fBline-pulse-duration" Set line pulse duration -.IP "\fBno-autoconnect" -Disable automatic connect +.IP "\fBno-reconnect" +Do not reconnect .IP "\fBlog" Enable log to file .IP "\fBlog-file" @@ -467,14 +570,16 @@ Enable local echo Enable line timestamp .IP "\fBtimestamp-format" Set timestamp format +.IP "\fBtimestamp-timeout" +Set timestamp timeout .IP "\fBmap" Map characters on input or output .IP "\fBcolor" Colorize tio text using ANSI color code ranging from 0 to 255 .IP "\fBinput-mode" -Set input mode. +Set input mode .IP "\fBoutput-mode" -Set output mode. +Set output mode .IP "\fBsocket" Set socket to redirect I/O to .IP "\fBprefix-ctrl-key" @@ -485,12 +590,20 @@ Enable RS-485 mode Set RS-485 configuration .IP "\fBalert" Set alert action on connect/disconnect +.IP "\fBmute" +Mute tio messages .IP "\fBscript" Run script from string .IP "\fBscript-file" Run script from file .IP "\fBscript-run" -Run script on connect. +Run script on connect +.IP "\fBexec" +Execute shell command with I/O redirected to device + +.PP +It is possible to include the content of other configuration files using the +include directive like so: "[include ]". .SH "CONFIGURATION FILE EXAMPLES" @@ -500,7 +613,7 @@ To change the default configuration simply set options like so: .RS .nf .eo -# Defaults +[default] baudrate = 9600 databits = 8 parity = none @@ -512,7 +625,7 @@ line-pulse-duration = DTR=200,RTS=400 .RE .TP -Named sub-configurations can be added via labels: +Named configuration profiles can be added via labels: .RS .nf @@ -526,7 +639,7 @@ color = 11 .RE .TP -Activate the sub-configuration by name: +Activate the configuration profile by name: $ tio rpi3 @@ -536,31 +649,31 @@ Which is equivalent to: $ tio -b 115200 -c 11 /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0 .TP -A sub-configuration can also be activated by its pattern which supports regular expressions: +A configuration profile can also be activated by its pattern which supports regular expressions: .RS .nf .eo -[usb device] -pattern = usb([0-9]*) -device = /dev/ttyUSB%s +[usb-devices] +pattern = ^usb([0-9]*) +device = /dev/ttyUSB%m1 baudrate = 115200 .ec .fi .RE .TP -Activate the sub-configuration by pattern match: +Activate the configuration profile by pattern match: $ tio usb12 .TP -Which is equivalent to: +Which becomes equivalent to: $ tio -b 115200 /dev/ttyUSB12 .TP -It is also possible to combine use of sub-configuration and command-line options. For example: +It is also possible to combine use of configuration profile and command-line options. For example: $ tio -l -t usb12 @@ -612,6 +725,11 @@ expect -i $uart "prompt> " .fi .RE +.TP +It is also possible to use tio's own simpler expect/send script functionality to e.g. automate logins: + +$ tio --script 'tio.expect("login: "); tio.write("root\\n"); tio.expect("Password: "); tio.write("root\\n")' /dev/ttyUSB0 + .TP Redirect device I/O to network file socket for remote TTY sharing: @@ -631,7 +749,7 @@ $ echo "ls -la" | tio /dev/serial/by\-id/usb\-FTDI_TTL232R-3V3_FTGQVXBL\-if00\-p .TP Pipe command to serial device and wait for line response within 1 second: -$ echo "*IDN?" | tio /dev/ttyACM0 --script "expect('\\r\\n', 1000)" --mute +$ echo "*IDN?" | tio /dev/ttyACM0 --script "tio.expect('\\r\\n', 1000)" --mute .TP .TP @@ -639,6 +757,11 @@ Likewise, to pipe data from file to the serial device: $ cat data.bin | tio /dev/serial/by\-id/usb\-FTDI_TTL232R-3V3_FTGQVXBL\-if00\-port0 +.TP +Map NL to CR-NL on input from device and DEL to BS on output to device: + +$ tio --map INLCRNL,ODELBS /dev/ttyUSB0 + .TP Enable RS-485 mode: @@ -647,12 +770,7 @@ $ tio --rs-485 --rs-485-config=RTS_ON_SEND=1,RX_DURING_TX /dev/ttyUSB0 .TP Manipulate DTR and RTS lines upon first connect to reset connected microcontroller: -$ tio --script "high(DTR); low(RTS); msleep(100); toggle(DTR)" --script-run once /dev/ttyUSB0 - -.TP -Automatically log in to connected OS: - -$ tio --script "expect('password:'); send('my_password\\n')" /dev/ttyUSB0 +$ tio --script "tio.set{DTR=high,RTS=low}; tio.msleep(100); tio.set{RTS=toggle}" --script-run once /dev/ttyUSB0 .SH "WEBSITE" .PP diff --git a/man/tio.1.txt b/man/tio.1.txt index 1da130f..eeac82c 100644 --- a/man/tio.1.txt +++ b/man/tio.1.txt @@ -1,10 +1,10 @@ -tio(1) User Commands tio(1) +tio(1) User Commands tio(1) NAME tio - a serial device I/O tool SYNOPSIS - tio [] + tio [] DESCRIPTION tio is a serial device tool which features a straightforward command-line and configuration file interface to easily connect to serial TTY devices for basic I/O operations. @@ -62,13 +62,37 @@ OPTIONS The default pulse duration for each line is 100 ms. - -n, --no-autoconnect + -a, --auto-connect new|latest|direct - Disable automatic connect. + Automatically connect to serial device according to one of the following strategies: - 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. + new Automatically connect to first new appearing serial device. - However, if the --no-autoconnect option is provided, tio will exit if the device is not present or an established connection is lost. + latest Automatically connect to latest registered serial device. + + direct Connect directly to specified TTY device. + + All the listed strategies automatically reconnects according to strategy if device is not available or connection is lost. + + Default value is "direct". + + --exclude-devices + + Exclude devices by pattern ('*' and '?' supported). + + --exclude-drivers + + Exclude drivers by pattern ('*' and '?' supported). + + --exclude-tids + + Exclude topology IDs by pattern ('*' and '?' supported). + + -n, --no-reconnect + + Do not reconnect. + + This means that tio will exit if it fails to connect to device or an established connection is lost. -e, --local-echo @@ -90,17 +114,29 @@ OPTIONS iso8601 ISO8601 format ("YYYY-MM-DDThh:mm:ss.sss") + epoch Seconds since Unix epoch (1970-01-01) + + epoch-usec Seconds since Unix epoch (1970-01-01) with subdivision microseconds + Default format is 24hour - -L, --list-devices + --timestamp-timeout - List available serial devices by ID. + Set timestamp timeout value in milliseconds. - -l, --log + This value only takes effect in hex output mode where timestamps are only printed after elapsed timeout time of no output activity from tty device. + + Default value is 200. + + -l, --list + + List available targets (serial devices, TIDs, configuration profiles). + + -L, --log Enable log to file. - The log file will be automatically named using the following format tio_DEVICE_YYYY-MM-DDTHH:MM:SS.log. + The log file will be automatically named using the following format tio_TARGET_YYYY-MM-DDTHH:MM:SS.log. Target being the command line target such as tty-device, tid, or configuration profile. The filename can be manually set using the --log-file option. @@ -122,7 +158,7 @@ OPTIONS -m, --map - Map (replace, translate) characters on input or output. The following mapping flags are supported: + Map (replace, translate) characters on input to the serial device or output from the serial device. The following mapping flags are supported: ICRNL Map CR to NL on input (unless IGNCR is set) @@ -134,6 +170,10 @@ OPTIONS INLCRNL Map NL to CR-NL on input + ICRCRNL Map CR to CR-NL on input + + IMSB2LSB Map MSB bit order to LSB on input + OCRNL Map CR to NL on output ODELBS Map DEL to BS on output @@ -144,19 +184,29 @@ OPTIONS ONULBRK Map nul (zero) to send break signal on output - MSB2LSB Map MSB bit order to LSB on output + OIGNCR Ignore CR on output If defining more than one flag, the flags must be comma separated. - --input-mode normal|hex + --input-mode normal|hex|line - Set input mode. In hex input mode bytes can be sent by typing the two-character hexadecimal representation of the 1 byte value, e.g.: to send 0xA you must type 0a or 0A. + Set input mode. + + In normal mode input characters are sent immediately as they are typed. + + In hex input mode bytes can be sent by typing the two-character hexadecimal representation of the 1 byte value, e.g.: to send 0xA you must type 0a or 0A. + + In line input mode input characters are sent when you press enter. The only editing feature supported in this mode is backspace. Default value is "normal". - --output-mode normal|hex + --output-mode normal|hex|hexN - Set output mode. In hex mode each incoming byte is printed out as a 1 byte hex value. + Set output mode. + + In hex mode each incoming byte is printed out as a 1 byte hex value. + + In hexN mode, N is a number less than or equal to 4096 which defines how many hex values will be printed before a line break. Default value is "normal". @@ -172,7 +222,8 @@ OPTIONS Redirect I/O to socket. - Any input from clients connected to the socket is sent on the serial port as if entered at the terminal where tio is running (except that ctrl-t sequences are not recognized), and any input from the serial port is multiplexed to the terminal and all connected clients. + Any input from clients connected to the socket is sent on the serial port as if entered at the terminal where tio is running (except that ctrl-t sequences are not recognized), and any input from the serial port is multi‐ + plexed to the terminal and all connected clients. Sockets remain open while the serial port is disconnected, and writes will block. @@ -216,6 +267,10 @@ OPTIONS Default value is "none". + --mute + + Mute tio messages. + --script Run script from string. @@ -230,6 +285,14 @@ OPTIONS Default value is "always". + --exec + + Execute shell command with I/O redirected to device + + --complete-profiles + + Prints profiles (for shell completion) + -v, --version Display program version. @@ -238,8 +301,8 @@ OPTIONS Display help. -KEYS - In session, all key strokes are forwarded to the serial device except the following key sequence: a prefix key (default: ctrl-t) followed by a command key. These sequences are intercepted as tio commands: +KEY COMMANDS + In session, all key strokes are forwarded to the serial device except the following key sequence: a prefix key (default: ctrl-t) followed by a command key. These sequences are intercepted as key commands: ctrl-t ? List available key commands @@ -261,7 +324,7 @@ KEYS ctrl-t L Show line states (DTR, RTS, CTS, DSR, DCD, RI) - ctrl-t m Toggle MSB to LSB bit order + ctrl-t m Change mapping of characters on input or output ctrl-t o Toggle output mode @@ -271,12 +334,12 @@ KEYS ctrl-t r Run script + ctrl-t R Execute shell command with I/O redirected to device + ctrl-t s Show TX/RX statistics ctrl-t t Toggle line timestamp mode - ctrl-t U Toggle conversion to uppercase on output - ctrl-t v Show version ctrl-t x Send file using the XMODEM-1K or XMODEM-CRC protocol (prompts for file name and protocol) @@ -288,47 +351,62 @@ KEYS SCRIPT API Tio suppots Lua scripting to easily automate interaction with the tty device. - In addition to the Lua API tio makes the following functions available: + In addition to the standard Lua API tio makes the following functions available: expect(string, timeout) - Expect string - waits for string to match or timeout before continueing. Supports regular expressions. Special characters must be escaped with '\\'. Timeout is in milliseconds, defaults to 0 meaning it will wait forever. + Expect string - waits for string to match or timeout before continuing. Supports regular expressions. Special characters must be escaped with '\\'. Timeout is in milliseconds, defaults to 0 meaning it will wait forever. - send(string) - Send string. + Returns 1 on successful match, 0 on timeout, or -1 on error. - modem_send(file, protocol) + On successful match it also returns the match string as second return value. + + read(size, timeout) + Read up to size bytes from serial device. If timeout is 0 or not provided it will wait forever until data is ready to read. + + Returns number of bytes read on success, 0 on timeout, or -1 on error. + + On success, returns read string as second return value. Also emits a single timestamp to stdout and log file per options.timestamp and options.log. + + read_line(timeout) + Read line from serial device. If timeout is 0 or not provided it will wait forever until data is ready to read. + + Returns number of bytes read on success, 0 on timeout, or -1 on error. + + On success, returns the string that was read as second return value. Also emits a single timestamp to stdout and log file per options.timestamp and options.log. + + write(string) + Write string to serial device. + + Returns number of bytes written on success or -1 on error. + + send(file, protocol) Send file using x/y-modem protocol. Protocol can be any of XMODEM_1K, XMODEM_CRC, YMODEM. - exit(code) - Exit with exit code. + tty_search() + Search for serial devices. - high(line) - Set tty line high. + Returns a table of number indexed tables, one for each serial device found. Each of these tables contains the serial device information accessible via the following string indexed elements "path", "tid", "uptime", "dri‐ + ver", "description". - low(line) - Set tty line low. + Returns nil if no serial devices are found. - toggle(line) - Toggle the tty line. + set{line=state, ...} + Set state of one or multiple tty modem lines. + + Line can be any of DTR, RTS, CTS, DSR, CD, RI + + State is high, low, or toggle. sleep(seconds) Sleep for seconds. msleep(ms) - Sleep for miliseconds. + Sleep for milliseconds. - config_high(line) - Set tty line state configuration to high. - - config_low(line) - Set tty line state configuration to low. - - apply_config() - Apply tty line state configuration. Using the line state configuration API instead of high()/low() will help to make the lines physically switch as simultaneously as possible. This may solve timing issues on some platforms. - - Note: Line can be any of DTR, RTS, CTS, DSR, CD, RI + exit(code) + Exit with exit code. CONFIGURATION FILE Options can be set via configuration file using the INI format. tio uses the configuration file first found in the following locations in the order listed: @@ -339,9 +417,9 @@ CONFIGURATION FILE $HOME/.tioconfig - Labels can be used to group settings into named sub-configurations which can be activated from the command-line when starting tio. + Labels can be used to group settings into named configuration profiles which can be activated from the command-line when starting tio. - tio will try to match the user input to a sub-configuration by name or by pattern to get the TTY device and other options. + tio will try to match the user input to a configuration profile by name or by pattern to get the TTY device and other options. Options without any label change the default options. @@ -369,7 +447,7 @@ CONFIGURATION FILE line-pulse-duration Set line pulse duration - no-autoconnect Disable automatic connect + no-reconnect Do not reconnect log Enable log to file @@ -387,13 +465,15 @@ CONFIGURATION FILE timestamp-format Set timestamp format + timestamp-timeout Set timestamp timeout + map Map characters on input or output color Colorize tio text using ANSI color code ranging from 0 to 255 - input-mode Set input mode. + input-mode Set input mode - output-mode Set output mode. + output-mode Set output mode socket Set socket to redirect I/O to @@ -405,16 +485,22 @@ CONFIGURATION FILE alert Set alert action on connect/disconnect + mute Mute tio messages + script Run script from string script-file Run script from file - script-run Run script on connect. + script-run Run script on connect + + exec Execute shell command with I/O redirected to device + + It is possible to include the content of other configuration files using the include directive like so: "[include ]". CONFIGURATION FILE EXAMPLES To change the default configuration simply set options like so: - # Defaults + [default] baudrate = 9600 databits = 8 parity = none @@ -422,14 +508,14 @@ CONFIGURATION FILE EXAMPLES color = 10 line-pulse-duration = DTR=200,RTS=400 - Named sub-configurations can be added via labels: + Named configuration profiles can be added via labels: [rpi3] device = /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0 baudrate = 115200 color = 11 - Activate the sub-configuration by name: + Activate the configuration profile by name: $ tio rpi3 @@ -437,22 +523,22 @@ CONFIGURATION FILE EXAMPLES $ tio -b 115200 -c 11 /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0 - A sub-configuration can also be activated by its pattern which supports regular expressions: + A configuration profile can also be activated by its pattern which supports regular expressions: - [usb device] - pattern = usb([0-9]*) - device = /dev/ttyUSB%s + [usb-devices] + pattern = ^usb([0-9]*) + device = /dev/ttyUSB%m1 baudrate = 115200 - Activate the sub-configuration by pattern match: + Activate the configuration profile by pattern match: $ tio usb12 - Which is equivalent to: + Which becomes equivalent to: $ tio -b 115200 /dev/ttyUSB12 - It is also possible to combine use of sub-configuration and command-line options. For example: + It is also possible to combine use of configuration profile and command-line options. For example: $ tio -l -t usb12 @@ -494,6 +580,10 @@ EXAMPLES send -i $uart "ls -la\n" expect -i $uart "prompt> " + It is also possible to use tio's own simpler expect/send script functionality to e.g. automate logins: + + $ tio --script 'expect("login: "); write("root\n"); expect("Password: "); write("root\n")' /dev/ttyUSB0 + Redirect device I/O to network file socket for remote TTY sharing: $ tio --socket inet:4444 /dev/ttyUSB0 @@ -514,22 +604,22 @@ EXAMPLES $ cat data.bin | tio /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0 + Map NL to CR-NL on input from device and DEL to BS on output to device: + + $ tio --map INLCRNL,ODELBS /dev/ttyUSB0 + Enable RS-485 mode: $ tio --rs-485 --rs-485-config=RTS_ON_SEND=1,RX_DURING_TX /dev/ttyUSB0 Manipulate DTR and RTS lines upon first connect to reset connected microcontroller: - $ tio --script "high(DTR); low(RTS); msleep(100); toggle(DTR)" --script-run once /dev/ttyUSB0 - - Automatically log in to connected OS: - - $ tio --script "expect('password:'); send('my_password\n')" /dev/ttyUSB0 + $ tio --script "set{DTR=high,RTS=low}; msleep(100); set{RTS=toggle}" --script-run once /dev/ttyUSB0 WEBSITE Visit https://tio.github.io AUTHOR - Created by Martin Lund . + Maintained by Martin Lund . -tio 2.8 2023-09-19 tio(1) +tio 3.9 2025-04-13 tio(1) diff --git a/meson.build b/meson.build index 183044d..b95b5c0 100644 --- a/meson.build +++ b/meson.build @@ -1,12 +1,12 @@ project('tio', 'c', - version : '2.8', - license : [ 'GPL-2'], + version : '3.9', + license : 'GPL-2.0-or-later', meson_version : '>= 0.53.2', default_options : [ 'warning_level=2', 'buildtype=release', 'c_std=gnu99' ] ) # The tag date of the project_version(), update when the version bumps. -version_date = '2024-04-14' +version_date = '2025-04-13' # Test for dynamic baudrate configuration interface compiler = meson.get_compiler('c') diff --git a/src/alert.c b/src/alert.c index cc6abdd..23a728e 100644 --- a/src/alert.c +++ b/src/alert.c @@ -19,37 +19,10 @@ * 02110-1301, USA. */ -#include "config.h" #include -#include #include -#include -#include "error.h" -#include "print.h" #include "options.h" - -enum alert_t alert_option_parse(const char *arg) -{ - enum alert_t alert = option.alert; // Default - - if (arg != NULL) - { - if (strcmp(arg, "none") == 0) - { - return ALERT_NONE; - } - else if (strcmp(arg, "bell") == 0) - { - return ALERT_BELL; - } - else if (strcmp(arg, "blink") == 0) - { - return ALERT_BLINK; - } - } - - return alert; -} +#include "alert.h" void blink_background(void) { diff --git a/src/alert.h b/src/alert.h index 10932ea..91258c9 100644 --- a/src/alert.h +++ b/src/alert.h @@ -21,14 +21,13 @@ #pragma once -enum alert_t +typedef enum { ALERT_NONE, ALERT_BELL, ALERT_BLINK, ALERT_END, -}; +} alert_t; -enum alert_t alert_option_parse(const char *arg); void alert_connect(void); void alert_disconnect(void); diff --git a/src/bash-completion/tio.in b/src/bash-completion/tio.in index 10bf6ef..b3b61cb 100644 --- a/src/bash-completion/tio.in +++ b/src/bash-completion/tio.in @@ -18,7 +18,11 @@ _tio() -o --output-delay \ -o --output-line-delay \ --line-pulse-duration \ - -n --no-autoconnect \ + -a --auto-connect \ + --exclude-devices \ + --exclude-drivers \ + --exclude-tids \ + -n --no-reconnect \ -e --local-echo \ -l --log \ --log-file \ @@ -28,7 +32,8 @@ _tio() -m --map \ -t --timestamp \ --timestamp-format \ - -L --list-devices \ + --timestamp-timeout \ + -L --list \ -c --color \ -S --socket \ --input-mode \ @@ -40,6 +45,8 @@ _tio() --script \ --script-file \ --script-run \ + --exec \ + --complete-profiles \ -v --version \ -h --help" @@ -74,52 +81,16 @@ _tio() COMPREPLY=( $(compgen -W "1 10 100" -- ${cur}) ) return 0 ;; - --line-pulse-duration) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; - -n | --no-autoconnect) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; - -e | --local-echo) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; - -l | --log) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; - --log-file) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; - --log-directory) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; - --log-append) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; - --log-strip) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + -a | --auto-connect) + COMPREPLY=( $(compgen -W "new latest none" -- ${cur}) ) return 0 ;; -m | --map) - COMPREPLY=( $(compgen -W "ICRNL IGNCR INLCR IFFESCC INLCRNL OCRNL ODELBS ONLCRNL MSB2LSB" -- ${cur}) ) - return 0 - ;; - -t | --timestamp) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + COMPREPLY=( $(compgen -W "ICRNL IGNCR INLCR IFFESCC INLCRNL ICRCRNL IMSB2LSB OCRNL ODELBS ONLCRNL OLTU ONULBRK OIGNCR" -- ${cur}) ) return 0 ;; --timestamp-format) - COMPREPLY=( $(compgen -W "24hour 24hour-start 24hour-delta iso8601" -- ${cur}) ) - return 0 - ;; - -L | --list-devices) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + COMPREPLY=( $(compgen -W "24hour 24hour-start 24hour-delta iso8601 epoch epoch-usec" -- ${cur}) ) return 0 ;; -c | --color) @@ -131,17 +102,13 @@ _tio() return 0 ;; --input-mode) - COMPREPLY=( $(compgen -W "normal hex" -- ${cur}) ) + COMPREPLY=( $(compgen -W "normal hex line" -- ${cur}) ) return 0 ;; --output-mode) COMPREPLY=( $(compgen -W "normal hex" -- ${cur}) ) return 0 ;; - --rs-485) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; --rs-485-config) COMPREPLY=( $(compgen -W "RTS_ON_SEND RTS_AFTER_SEND RTS_DELAY_BEFORE_SEND RTS_DELAY_AFTER_SEND RX_DURING_TX" -- ${cur}) ) return 0 @@ -150,30 +117,10 @@ _tio() COMPREPLY=( $(compgen -W "none bell blink" -- ${cur}) ) return 0 ;; - --mute) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; - --script) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; - --script-file) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; --script-run) COMPREPLY=( $(compgen -W "once always never" -- ${cur}) ) return 0 ;; - -v | --version) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; - -h | --help) - COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) - return 0 - ;; *) ;; esac @@ -184,14 +131,14 @@ _tio() ;; esac - sub_configs="`tio --complete-sub-configs`" + profiles="`tio --complete-profiles`" if [ -d /dev/serial/by-id ]; then ttys=$(printf '%s\n' /dev/tty* /dev/serial/by-id/*) else ttys=$(printf '%s\n' /dev/tty*) fi - COMPREPLY=( $(compgen -W "${ttys} ${sub_configs}" -- ${cur}) ) + COMPREPLY=( $(compgen -W "${ttys} ${profiles}" -- ${cur}) ) return 0 } diff --git a/src/configfile.c b/src/configfile.c index b123071..ca116bf 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -21,504 +21,768 @@ */ #define _GNU_SOURCE - -#include "config.h" -#include -#include -#include -#include -#include #include -#include -#include -#include -#include +#include #include +#include +#include +#include +#include #include -#include -#include "options.h" +#include #include "configfile.h" -#include "misc.h" -#include "options.h" -#include "error.h" +#include "timestamp.h" #include "print.h" #include "rs485.h" -#include "timestamp.h" -#include "alert.h" +#include "misc.h" -struct config_t +#define CONFIG_GROUP_NAME_DEFAULT "default" +#define CONFIG_GROUP_INCLUDE_PREFIX "include " +#define MAX_LINE_LENGTH 1024 + +struct config_t config = {}; + +static void config_file_load(const char *filename, GString *buffer, bool test); +static void config_file_process(const char *filename, GString *buffer, GList **included_files, bool test); + +static void config_get_string(GKeyFile *key_file, gchar *group, gchar *key, char **dest, char *allowed_string, ...) { - const char *user; + (void)dest; + GError *error = NULL; + bool mismatch = true; - char *path; - char *section_name; - char *match; - - char *tty; - char *flow; - char *parity; - char *log_filename; - char *socket; - char *map; - char *script; - char *script_filename; - bool script_run; -}; - -static struct config_t c; - -static int get_match(const char *input, const char *pattern, char **match) -{ - int ret; - int len = 0; - regex_t re; - regmatch_t m[2]; - char err[128]; - - /* compile a regex with the pattern */ - ret = regcomp(&re, pattern, REG_EXTENDED); - if (ret) + gchar *string = g_key_file_get_string(key_file, group, key, &error); + if (error != NULL) { - regerror(ret, &re, err, sizeof(err)); - tio_error_printf("Regex failure: %s", err); - return ret; - } - - /* try to match on input */ - ret = regexec(&re, input, 2, m, 0); - if (!ret) - { - len = m[1].rm_eo - m[1].rm_so; - } - - regfree(&re); - - if (len) - { - asprintf(match, "%s", &input[m[1].rm_so]); - } - - return len; -} - -static bool read_boolean(const char *value, const char *name) -{ - const char *true_values[] = { "true", "enable", "on", "yes", "1", NULL }; - const char *false_values[] = { "false", "disable", "off", "no", "0", NULL }; - - for (int i = 0; true_values[i] != NULL; i++) - if (strcmp(value, true_values[i]) == 0) - return true; - - for (int i = 0; false_values[i] != NULL; i++) - if (strcmp(value, false_values[i]) == 0) - return false; - - tio_error_printf("Invalid value '%s' for option '%s' in configuration file", - value, name); - exit(EXIT_FAILURE); -} - -static long read_integer(const char *value, const char *name, long min_value, long max_value) -{ - errno = 0; - char *endptr; - long result = strtol(value, &endptr, 10); - - if (errno || endptr == value || *endptr != '\0' || result < min_value || result > max_value) - { - tio_error_printf("Invalid value '%s' for option '%s' in configuration file", - value, name); + if (error->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND) + { + // Key not found - ignore key + g_error_free(error); + return; + } + tio_error_print("%s: %s", config.path, error->message); + g_error_free(error); exit(EXIT_FAILURE); } - return result; + va_list args; + const char* current_arg = allowed_string; + va_start(args, allowed_string); + + if (current_arg == NULL) + { + mismatch = false; + } + + // Iterate through variable arguments + while (current_arg != NULL) + { + if (strcmp(string, current_arg) == 0) + { + mismatch = false; + break; + } + current_arg = va_arg(args, const char *); + } + + if (mismatch) + { + tio_error_print("%s: Invalid %s value '%s' in %s profile", config.path, key, string, group); + exit(EXIT_FAILURE); + } + + va_end(args); + + *dest = string; } -/** - * data_handler() - walk config file to load parameters matching user input - * - * INIH handler used to get all parameters from a given section - */ -static int data_handler(void *user, const char *section, const char *name, - const char *value) +static void config_get_integer(GKeyFile *key_file, gchar *group, gchar *key, int *dest, int min, int max) { - UNUSED(user); + (void)dest; + GError *error = NULL; - // If section matches current section being parsed - if (!strcmp(section, c.section_name)) + int value = g_key_file_get_integer(key_file, group, key, &error); + if (error != NULL) { - // Set configuration parameter if found - if (!strcmp(name, "device") || !strcmp(name, "tty")) + if (error->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND) { - asprintf(&c.tty, value, c.match); - option.tty_device = c.tty; + // Key not found - ignore key + g_error_free(error); + return; } - else if (!strcmp(name, "baudrate")) - { - option.baudrate = read_integer(value, name, 0, LONG_MAX); - } - else if (!strcmp(name, "databits")) - { - option.databits = read_integer(value, name, 5, 8); - } - else if (!strcmp(name, "flow")) - { - asprintf(&c.flow, "%s", value); - option.flow = c.flow; - } - else if (!strcmp(name, "stopbits")) - { - option.stopbits = read_integer(value, name, 1, 2); - } - else if (!strcmp(name, "parity")) - { - asprintf(&c.parity, "%s", value); - option.parity = c.parity; - } - else if (!strcmp(name, "output-delay")) - { - option.output_delay = read_integer(value, name, 0, LONG_MAX); - } - else if (!strcmp(name, "output-line-delay")) - { - option.output_line_delay = read_integer(value, name, 0, LONG_MAX); - } - else if (!strcmp(name, "line-pulse-duration")) - { - line_pulse_duration_option_parse(value); - } - else if (!strcmp(name, "no-autoconnect")) - { - option.no_autoconnect = read_boolean(value, name); - } - else if (!strcmp(name, "log")) - { - option.log = read_boolean(value, name); - } - else if (!strcmp(name, "log-file")) - { - asprintf(&c.log_filename, "%s", value); - option.log_filename = c.log_filename; - } - else if (!strcmp(name, "log-append")) - { - option.log_append = read_boolean(value, name); - } - else if (!strcmp(name, "log-strip")) - { - option.log_strip = read_boolean(value, name); - } - else if (!strcmp(name, "local-echo")) - { - option.local_echo = read_boolean(value, name); - } - else if (!strcmp(name, "input-mode")) - { - option.input_mode = input_mode_option_parse(value); - } - else if (!strcmp(name, "output-mode")) - { - option.output_mode = output_mode_option_parse(value); - } - else if (!strcmp(name, "timestamp")) - { - option.timestamp = read_boolean(value, name) ? - TIMESTAMP_24HOUR : TIMESTAMP_NONE; - } - else if (!strcmp(name, "timestamp-format")) - { - option.timestamp = timestamp_option_parse(value); - } - else if (!strcmp(name, "map")) - { - asprintf(&c.map, "%s", value); - option.map = c.map; - } - else if (!strcmp(name, "color")) - { - if (!strcmp(value, "list")) - { - // Ignore - return 0; - } - else if (!strcmp(value, "none")) - { - option.color = -1; // No color - return 0; - } - else if (!strcmp(value, "bold")) - { - option.color = 256; // Bold - return 0; - } + tio_error_print("%s: %s", config.path, error->message); + g_error_free(error); + exit(EXIT_FAILURE); + } - option.color = atoi(value); - if ((option.color < 0) || (option.color > 255)) - { - option.color = -1; // No color - } - } - else if (!strcmp(name, "socket")) + if ((value < min) || (value > max)) + { + tio_error_print("%s: Invalid %s value '%d' in %s profile", config.path, key, value, group); + exit(EXIT_FAILURE); + } + + *dest = value; +} + +static void config_get_bool(GKeyFile *key_file, gchar *group, gchar *key, bool *dest) +{ + (void)dest; + GError *error = NULL; + + bool value = g_key_file_get_boolean(key_file, group, key, &error); + if (error != NULL) + { + if (error->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND) { - asprintf(&c.socket, "%s", value); - option.socket = c.socket; + // Key not found - ignore key + g_error_free(error); + return; } - else if (!strcmp(name, "prefix-ctrl-key")) + tio_error_print("%s: %s", config.path, error->message); + g_error_free(error); + exit(EXIT_FAILURE); + } + + *dest = value; +} + +static void config_parse_keys(GKeyFile *key_file, char *group) +{ + char *string = NULL; + + config_get_string(key_file, group, "device", &config.device, NULL); + config_get_integer(key_file, group, "baudrate", &option.baudrate, 0, INT_MAX); + config_get_integer(key_file, group, "databits", &option.databits, 5, 8); + config_get_string(key_file, group, "flow", &string, "none", "hard", "soft", NULL); + if (string != NULL) + { + option_parse_flow(string, &option.flow); + g_free((void *)string); + string = NULL; + } + config_get_integer(key_file, group, "stopbits", &option.stopbits, 1, 2); + config_get_string(key_file, group, "parity", &string, "odd", "even", "none", "mark", "space", NULL); + if (string != NULL) + { + option_parse_parity(string, &option.parity); + g_free((void *)string); + string = NULL; + } + + config_get_integer(key_file, group, "output-delay", &option.output_delay, 0, INT_MAX); + config_get_integer(key_file, group, "output-line-delay", &option.output_line_delay, 0, INT_MAX); + config_get_string(key_file, group, "line-pulse-duration", &string, NULL); + if (string != NULL) + { + option_parse_line_pulse_duration(string); + g_free((void *)string); + string = NULL; + } + config_get_string(key_file, group, "auto-connect", &string, "new", "latest", "direct", NULL); + if (string != NULL) + { + option_parse_auto_connect(string, &option.auto_connect); + g_free((void *)string); + string = NULL; + } + config_get_string(key_file, group, "exclude-devices", &option.exclude_devices, NULL); + config_get_string(key_file, group, "exclude-drivers", &option.exclude_devices, NULL); + config_get_string(key_file, group, "exclude-tids", &option.exclude_devices, NULL); + config_get_bool(key_file, group, "no-reconnect", &option.no_reconnect); + config_get_bool(key_file, group, "local-echo", &option.local_echo); + config_get_string(key_file, group, "input-mode", &string, "normal", "hex", "line", NULL); + if (string != NULL) + { + option_parse_input_mode(string, &option.input_mode); + g_free((void *)string); + string = NULL; + } + config_get_string(key_file, group, "output-mode", &string, NULL); + if (string != NULL) + { + option_parse_output_mode(string, &option.output_mode); + g_free((void *)string); + string = NULL; + } + config_get_bool(key_file, group, "timestamp", (bool*) &option.timestamp); + if (option.timestamp != TIMESTAMP_NONE) + { + config_get_string(key_file, group, "timestamp-format", &string, "24hour", "24hour-start", "24hour-delta", "iso8601", "epoch", "epoch-usec", NULL); + if (string != NULL) { - if (!strcmp(value, "none")) - { - option.prefix_enabled = false; - } - else if (ctrl_key_code(value[0]) > 0) - { - option.prefix_code = ctrl_key_code(value[0]); - option.prefix_key = value[0]; - } + option_parse_timestamp(string, &option.timestamp); + g_free((void *)string); + string = NULL; } - else if (!strcmp(name, "rs-485")) + } + config_get_integer(key_file, group, "timestamp-timeout", &option.timestamp_timeout, 0, INT_MAX); + config_get_bool(key_file, group, "log", &option.log); + config_get_string(key_file, group, "log-file", &option.log_filename, NULL); + config_get_string(key_file, group, "log-directory", &option.log_directory, NULL); + config_get_bool(key_file, group, "log-append", &option.log_append); + config_get_bool(key_file, group, "log-strip", &option.log_strip); + config_get_string(key_file, group, "map", &string, NULL); + if (string != NULL) + { + option_parse_mappings(string); + g_free((void *)string); + string = NULL; + } + config_get_string(key_file, group, "color", &string, NULL); + if (string != NULL) + { + if (strcmp(string, "list") == 0) { - option.rs485 = read_boolean(value, name); + // Ignore } - else if (!strcmp(name, "rs-485-config")) + else if (strcmp(string, "none") == 0) { - rs485_parse_config(value); + option.color = -1; // No color } - else if (!strcmp(name, "alert")) + else if (strcmp(string, "bold") == 0) { - option.alert = alert_option_parse(value); - } - else if (!strcmp(name, "mute")) - { - option.mute = read_boolean(value, name); - } - else if (!strcmp(name, "pattern")) - { - // Do nothing - } - else if (!strcmp(name, "script")) - { - asprintf(&c.script, "%s", value); - option.script = c.script; - } - else if (!strcmp(name, "script-file")) - { - asprintf(&c.script_filename, "%s", value); - option.script_filename = c.script_filename; - } - else if (!strcmp(name, "script-run")) - { - option.script_run = script_run_option_parse(value); + option.color = 256; // Bold } else { - tio_warning_printf("Unknown option '%s' in configuration file, ignored", name); + option.color = atoi(string); + if ((option.color < 0) || (option.color > 255)) + { + tio_error_print("%s: Invalid color value in %s profile", config.path, group); + exit(EXIT_FAILURE); + } } + g_free((void *)string); + string = NULL; + } + config_get_string(key_file, group, "socket", &option.socket, NULL); + config_get_bool(key_file, group, "rs-485", &option.rs485); + config_get_string(key_file, group, "rs-385-config", &string, NULL); + if (string != NULL) + { + rs485_parse_config(string); + g_free((void *)string); + string = NULL; + } + config_get_string(key_file, group, "alert", &string, "bell", "blink", "none", NULL); + if (string != NULL) + { + option_parse_alert(string, &option.alert); + g_free((void *)string); + string = NULL; + } + config_get_bool(key_file, group, "mute", &option.mute); + config_get_string(key_file, group, "script", &option.script, NULL); + config_get_string(key_file, group, "script-file", &option.script_filename, NULL); + config_get_string(key_file, group, "script-run", &string, NULL); + if (string != NULL) + { + option_parse_script_run(string, &option.script_run); + g_free((void *)string); + string = NULL; + } + config_get_string(key_file, group, "exec", &option.exec, NULL); + config_get_string(key_file, group, "prefix-ctrl-key", &string, NULL); + if (string != NULL) + { + if (strcmp(string, "none") == 0) + { + option.prefix_enabled = false; + } + else if (strlen(string) >= 2) + { + tio_error_print("%s: Invalid prefix-ctrl-key value in %s profile", config.path, group); + exit(EXIT_FAILURE); + } + else if (ctrl_key_code(string[0]) > 0) + { + option.prefix_enabled = true; + option.prefix_code = ctrl_key_code(string[0]); + option.prefix_key = string[0]; + } + else + { + tio_error_print("%s: Invalid prefix-ctrl-key value in %s profile", config.path, group); + exit(EXIT_FAILURE); + } + g_free((void *)string); + string = NULL; } - - return 0; } -/** - * section_pattern_search_handler() - walk config file to find section matching user input - * - * INIH handler used to resolve the section matching the user's input. - * This will look for the pattern element of each section and try to match it - * with the user input. - */ -static int section_pattern_search_handler(void *user, const char *section, const char *varname, - const char *varval) -{ - UNUSED(user); - - if (strcmp(varname, "pattern")) - return 0; - - if (!strcmp(varval, c.user)) - { - /* pattern matches as plain text */ - asprintf(&c.section_name, "%s", section); - } - else if (get_match(c.user, varval, &c.match) > 0) - { - /* pattern matches as regex */ - asprintf(&c.section_name, "%s", section); - } - - return 0; -} - -/** - * section_pattern_search_handler() - walk config file to find section matching user input - * - * INIH handler used to resolve the section matching the user's input. - * This will try to match the user input against a section with the name of the user input. - */ -static int section_name_search_handler(void *user, const char *section, const char *varname, - const char *varval) -{ - UNUSED(user); - UNUSED(varname); - UNUSED(varval); - - if (!strcmp(section, c.user)) - { - /* section name matches as plain text */ - asprintf(&c.section_name, "%s", section); - } - - return 0; -} - -static int section_name_print_handler(void *user, const char *section, const char *varname, - const char *varval) -{ - UNUSED(user); - UNUSED(varname); - UNUSED(varval); - - static char *section_previous = ""; - - if (strcmp(section, section_previous) != 0) - { - printf("%s ", section); - section_previous = strdup(section); - } - - return 0; -} - -static int resolve_config_file(void) +static int config_file_resolve(void) { char *xdg = getenv("XDG_CONFIG_HOME"); if (xdg) { - if (asprintf(&c.path, "%s/tio/config", xdg) != -1) + if (asprintf(&config.path, "%s/tio/config", xdg) != -1) { - if (access(c.path, F_OK) == 0) + if (access(config.path, F_OK) == 0) { return 0; } - free(c.path); + free(config.path); } } char *home = getenv("HOME"); if (home) { - if (asprintf(&c.path, "%s/.config/tio/config", home) != -1) + if (asprintf(&config.path, "%s/.config/tio/config", home) != -1) { - if (access(c.path, F_OK) == 0) + if (access(config.path, F_OK) == 0) { return 0; } - free(c.path); + free(config.path); } - if (asprintf(&c.path, "%s/.tioconfig", home) != -1) + if (asprintf(&config.path, "%s/.tioconfig", home) != -1) { - if (access(c.path, F_OK) == 0) + if (access(config.path, F_OK) == 0) { return 0; } - free(c.path); + free(config.path); } } - c.path = NULL; + config.path = NULL; return -EINVAL; } -void config_file_show_sub_configurations(void) +void config_file_show_profiles(void) { - memset(&c, 0, sizeof(struct config_t)); + GString *config_buffer; + GError *error = NULL; + GKeyFile *keyfile; + + // Reset configuration + memset(&config, 0, sizeof(struct config_t)); // Find config file - if (resolve_config_file() != 0) + if (config_file_resolve() != 0) { // None found - stop parsing return; } - ini_parse(c.path, section_name_print_handler, NULL); + // Load content of configuration file into buffer + config_buffer = g_string_new(NULL); + config_file_load(config.path, config_buffer, false); + + // Load configuration + keyfile = g_key_file_new(); + if (g_key_file_load_from_data(keyfile, config_buffer->str, config_buffer->len, G_KEY_FILE_NONE, &error) == false) + { + g_error_free(error); + goto error_load; + } + + // Get all group names + gsize num_groups; + gchar **group = g_key_file_get_groups(keyfile, &num_groups); + + for (gsize i = 0; i < num_groups; i++) + { + // Skip default group + if (strcmp(group[i], CONFIG_GROUP_NAME_DEFAULT) == 0) + { + continue; + } + + // Skip group with include directive + if (strncmp(group[i], CONFIG_GROUP_INCLUDE_PREFIX, strlen(CONFIG_GROUP_INCLUDE_PREFIX)) == 0) + { + continue; + } + + printf("%s ", group[i]); + } + + g_strfreev(group); +error_load: + g_key_file_free(keyfile); + g_string_free(config_buffer, TRUE); +} + +static void replace_substring(char *str, const char *substr, const char *replacement) +{ + char *pos = strstr(str, substr); + if (pos != NULL) + { + int substrLen = strlen(substr); + int replacementLen = strlen(replacement); + memmove(pos + replacementLen, pos + substrLen, strlen(pos + substrLen) + 1); + memcpy(pos, replacement, replacementLen); + } +} + +static char *match_and_replace(const char *str, const char *pattern, char *device) +{ + char replacement_str[PATH_MAX] = {}; + char m_key[14] = {}; + regex_t regex; + + assert(str != NULL); + assert(pattern != NULL); + assert(device != NULL); + + char *string = calloc(PATH_MAX, 1); + if (string == NULL) + { + tio_debug_printf("Failure allocating string memory\n"); + return NULL; + } + strncpy(string, device, PATH_MAX - 1); + + /* Find matches of pattern in str. For each match, replace any '%mN' in the + * copy of the device string with the corresponding match subexpression and + * return the new formed device string. + * + * Note: %m0 = Full match expression. + * %m1 = First subexpression + * %m2 = Second subexpression + * %m3 = etc.. + */ + + if (regcomp(®ex, pattern, REG_EXTENDED) != 0) + { + // Failure to compile regular expression + tio_error_print("Failure compiling regular expression '%s'\n", pattern); + exit(EXIT_FAILURE); + } + + regmatch_t matches[regex.re_nsub + 1]; + int status = regexec(®ex, str, regex.re_nsub + 1, matches, 0); + if (status == 0) + { + tio_debug_printf("Full match: "); + int j = 0; + for (int i = matches[0].rm_so; i < matches[0].rm_eo; i++) + { + tio_debug_printf_raw("%c", str[i]); + replacement_str[j++] = str[i]; + } + replacement_str[j] = '\0'; + replace_substring(string, "%m0", replacement_str); + tio_debug_printf_raw("\n"); + + for (int i = 1; i < ((int)regex.re_nsub + 1) && matches[i].rm_so != -1; i++) + { + tio_debug_printf("Subexpression %d match: ", i); + int k = 0; + for (int l = matches[i].rm_so; l < matches[i].rm_eo; l++) + { + tio_debug_printf_raw("%c", str[l]); + replacement_str[k++] = str[l]; + } + replacement_str[k] = '\0'; + sprintf(m_key, "%%m%d", i); + replace_substring(string, m_key, replacement_str); + tio_debug_printf_raw("\n"); + } + } + else if (status == REG_NOMATCH) + { + tio_debug_printf("No regex match\n"); + goto error; + } + else + { + char error_message[100]; + regerror(status, ®ex, error_message, sizeof(error_message)); + tio_debug_printf("Regex match failed: %s", error_message); + goto error; + } + + regfree(®ex); + return string; + +error: + regfree(®ex); + return NULL; +} + +static void config_file_process_line(const char *line, GString *buffer, GList **included_files, bool test) +{ + if (strncmp(line, "[include ", 9) == 0 && line[strlen(line) - 2] == ']') + { + char include_filename[MAX_LINE_LENGTH]; + + // Construct the format string safely + char format_string[50]; + snprintf(format_string, sizeof(format_string), "[include %%%ds]", MAX_LINE_LENGTH - 1); + + int ret = sscanf(line, format_string, include_filename); + if (ret != 1) + { + return; + } + + // Remove the trailing ']' character + include_filename[strlen(include_filename) - 1] = '\0'; + + if (g_list_find_custom(*included_files, include_filename, (GCompareFunc)strcmp) != NULL) + { + // Already included, avoid recursion + return; + } + + // Add to included files list + *included_files = g_list_append(*included_files, g_strdup(include_filename)); + + // Process the included file + config_file_process(include_filename, buffer, included_files, test); + } + else + { + // Normal line, add to buffer + g_string_append(buffer, line); + } +} + +static void config_file_process(const char *filename, GString *buffer, GList **included_files, bool test) +{ + if (test) + { + // Test that configuration file can be parsed + + GError *error = NULL; + GKeyFile *keyfile = g_key_file_new(); + + if (g_key_file_load_from_file(keyfile, filename, G_KEY_FILE_NONE, &error) == false) + { + tio_error_print("Failure loading file %s: %s", filename, error->message); + g_key_file_free(keyfile); + g_error_free(error); + exit(EXIT_FAILURE); + } + } + + FILE *file = fopen(filename, "r"); + if (file) + { + char line[MAX_LINE_LENGTH]; + while (fgets(line, sizeof(line), file)) + { + config_file_process_line(line, buffer, included_files, test); + } + fclose(file); + } +} + +static void config_file_load(const char *filename, GString *buffer, bool test) +{ + char current_dir[PATH_MAX] = "."; + char *config_file_dir = dirname(strdup(config.path)); + GList *included_files = NULL; + + getcwd(current_dir, PATH_MAX); + + // Change to the directory of the configuration file + chdir(config_file_dir); + + config_file_process(filename, buffer, &included_files, test); + + // Restore current directory + chdir(current_dir); + + // Free memory + g_list_free_full(included_files, g_free); + free(config_file_dir); } void config_file_parse(void) { - int ret; - - memset(&c, 0, sizeof(struct config_t)); - // Find config file - if (resolve_config_file() != 0) + if (config_file_resolve() != 0) { // None found - stop parsing return; } - // Set user input which may be tty device or sub config - c.user = option.tty_device; - - if (!c.user) + if (option.target == NULL) { return; } - // Parse default (unnamed) settings - asprintf(&c.section_name, "%s", ""); - ret = ini_parse(c.path, data_handler, NULL); - if (ret < 0) + GString *config_buffer = g_string_new(NULL); + GKeyFile *keyfile = g_key_file_new(); + GList *included_files = NULL; + GError *error = NULL; + + config_file_load(config.path, config_buffer, true); + + if (g_key_file_load_from_data(keyfile, config_buffer->str, config_buffer->len, G_KEY_FILE_NONE, &error) == false) { - tio_error_printf("Unable to parse configuration file (%d)", ret); + tio_error_print("Failure loading file %s: %s", config.path, error->message); + g_string_free(config_buffer, TRUE); + g_key_file_free(keyfile); + g_error_free(error); exit(EXIT_FAILURE); } - free(c.section_name); - c.section_name = NULL; - // Find matching section - ret = ini_parse(c.path, section_pattern_search_handler, NULL); - if (!c.section_name) + // Parse default group/section + if (g_key_file_has_group(keyfile, CONFIG_GROUP_NAME_DEFAULT)) { - ret = ini_parse(c.path, section_name_search_handler, NULL); - if (!c.section_name) + config_parse_keys(keyfile, CONFIG_GROUP_NAME_DEFAULT); + } + + // Parse target + if (g_key_file_has_group(keyfile, option.target)) + { + config.active_group = strdup(option.target); + config_parse_keys(keyfile, option.target); + } + else + { + // Find group by pattern + gsize num_groups; + gchar **group = g_key_file_get_groups(keyfile, &num_groups); + + for (gsize i = 0; i < num_groups; i++) { - tio_debug_printf("Unable to match user input to configuration section (%d)", ret); - return; + // Skip default group + if (strcmp(group[i], CONFIG_GROUP_NAME_DEFAULT) == 0) + { + continue; + } + + // Lookup 'pattern' key + gchar *pattern = g_key_file_get_string(keyfile, group[i], "pattern", &error); + if (error != NULL) + { + g_error_free(error); + error = NULL; + continue; + } + + // Lookup 'device' key + gchar *device = g_key_file_get_string(keyfile, group[i], "device", &error); + if (error != NULL) + { + g_error_free(error); + error = NULL; + continue; + } + + // Match pattern against target and replace any sub expression + // matches (%mN) in device string and return resulting string + // representing the new pattern based string. + config.device = match_and_replace(option.target, pattern, device); + if (config.device != NULL) + { + // Match found - save device + device = strdup(config.device); + + // Parse found group (may replace config.device) + config_parse_keys(keyfile, group[i]); + + // Update configuration + config.active_group = strdup(group[i]); + config.device = device; // Restore new device string + + break; + } } + + g_strfreev(group); } - // Parse settings of found section (sub config) - ret = ini_parse(c.path, data_handler, NULL); - if (ret < 0) - { - tio_error_printf("Unable to parse configuration file (%d)", ret); - exit(EXIT_FAILURE); - } + // Cleanup + g_key_file_free(keyfile); + g_string_free(config_buffer, TRUE); + g_list_free_full(included_files, g_free); atexit(&config_exit); } void config_exit(void) { - free(c.tty); - free(c.flow); - free(c.parity); - free(c.log_filename); - free(c.map); - - free(c.match); - free(c.section_name); - free(c.path); + free(config.active_group); + free(config.path); + free(config.device); } void config_file_print(void) { - if (c.path != NULL) + if (config.path != NULL) { - tio_printf(" Active configuration file: %s", c.path); - if (c.section_name != NULL) + tio_printf(" Active configuration file: %s", config.path); + if (config.active_group != NULL) { - tio_printf(" Active sub-configuration: %s", c.section_name); + tio_printf(" Active configuration profile: %s", config.active_group); } } } + +void config_list_targets(void) +{ + memset(&config, 0, sizeof(struct config_t)); + + // Find config file + if (config_file_resolve() != 0) + { + // None found + return; + } + + GKeyFile *keyfile; + GError *error = NULL; + + keyfile = g_key_file_new(); + + GString *config_buffer = g_string_new(NULL); + + config_file_load(config.path, config_buffer, false); + + if (g_key_file_load_from_data(keyfile, config_buffer->str, config_buffer->len, G_KEY_FILE_NONE, &error) == false) + { + g_error_free(error); + goto cleanup; + } + + // Get all group names + gsize num_groups; + gchar **group = g_key_file_get_groups(keyfile, &num_groups); + + if (num_groups == 0) + { + goto cleanup; + } + + printf("\nConfiguration profiles (%s)\n", config.path); + printf("--------------------------------------------------------------------------------\n"); + + int j = 1; + for (gsize i = 0; i < num_groups; i++) + { + // Skip default group + if (strcmp(group[i], CONFIG_GROUP_NAME_DEFAULT) == 0) + { + continue; + } + + // Skip group with include directive + if (strncmp(group[i], CONFIG_GROUP_INCLUDE_PREFIX, strlen(CONFIG_GROUP_INCLUDE_PREFIX)) == 0) + { + continue; + } + + printf("%-19s ", group[i]); + if (j++ % 4 == 0) + { + putchar('\n'); + } + } + if ((j-1) % 4 != 0) + { + putchar('\n'); + } + + g_strfreev(group); +cleanup: + g_key_file_free(keyfile); + g_string_free(config_buffer, TRUE); +} diff --git a/src/configfile.h b/src/configfile.h index 7748149..2cb9f15 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -22,7 +22,17 @@ #pragma once +struct config_t +{ + char *path; + char *active_group; + char *device; +}; + +extern struct config_t config; + void config_file_print(void); void config_file_parse(void); void config_exit(void); -void config_file_show_sub_configurations(void); +void config_file_show_profiles(void); +void config_list_targets(void); diff --git a/src/error.c b/src/error.c index 8049bc6..9f6bac2 100644 --- a/src/error.c +++ b/src/error.c @@ -19,22 +19,18 @@ * 02110-1301, USA. */ -#define __STDC_WANT_LIB_EXT2__ 1 // To access vasprintf - -#include "config.h" +#define _GNU_SOURCE // To access vasprintf #include -#include -#include -#include -#include -#include -#include "options.h" #include "print.h" -#include "error.h" -#include "timestamp.h" static char error[2][1000]; static bool in_session = false; +bool error_normal = true; + +void switch_error_output_mode(void) +{ + error_normal = false; +} void error_enter_session_mode(void) { @@ -93,7 +89,7 @@ void error_exit(void) /* Print error */ error_printf_("Error: %s", error[0]); } - else if ((error[1][0] != 0) && (option.no_autoconnect)) + else if ((error[1][0] != 0) && (option.no_reconnect)) { /* Print silent error */ error_printf_("Error: %s", error[1]); diff --git a/src/error.h b/src/error.h index ed81ba3..9a257fc 100644 --- a/src/error.h +++ b/src/error.h @@ -21,6 +21,10 @@ #pragma once +#include + +extern bool error_normal; + #define TIO_SUCCESS 0 #define TIO_ERROR 1 @@ -28,3 +32,4 @@ void tio_error_printf(const char *format, ...); void tio_error_printf_silent(const char *format, ...); void error_exit(void); void error_enter_session_mode(void); +void switch_error_output_mode(void); diff --git a/src/fs.c b/src/fs.c new file mode 100644 index 0000000..740d9a0 --- /dev/null +++ b/src/fs.c @@ -0,0 +1,226 @@ +/* + * tio - a serial device I/O tool + * + * Copyright (c) 2014-2024 Martin Lund + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#define _GNU_SOURCE // For statx() +#include "config.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "error.h" +#include "print.h" +#include "options.h" + +bool fs_dir_exists(const char *path) +{ + struct stat st; + + if (stat(path, &st) != 0) + { + return false; + } + else if (!S_ISDIR(st.st_mode)) + { + return false; + } + + return true; +} + +// Function to read the content of a file but stripped of newline +ssize_t fs_read_file_stripped(char *buf, size_t bufsiz, const char *format, ...) +{ + char filename[PATH_MAX]; + int bytes_printed = 0; + va_list args; + + va_start(args, format); + bytes_printed = vsnprintf(filename, sizeof(filename), format, args); + va_end(args); + + if (bytes_printed < 0) + { + return -1; + } + + FILE *file = fopen(filename, "r"); + if (!file) + { + return -1; + } + ssize_t length = fread(buf, 1, bufsiz - 1, file); + if (length == -1) + { + fclose(file); + return -1; + } + + // Strip any newline + buf[strcspn(buf, "\n")] = 0; + buf[length] = '\0'; // Make sure to null-terminate the string + + fclose(file); + + return length; +} + +bool fs_file_exists(const char *format, ...) +{ + char filename[PATH_MAX]; + int bytes_printed = 0; + struct stat st; + va_list args; + + va_start(args, format); + bytes_printed = vsnprintf(filename, sizeof(filename), format, args); + va_end(args); + + if (bytes_printed < 0) + { + return false; + } + + return stat(filename, &st) == 0; +} + +char* fs_search_directory(const char *dir_path, const char *dirname) +{ + struct dirent *entry; + char path[PATH_MAX]; + struct stat st; + DIR *dir; + + if ((dir = opendir(dir_path)) == NULL) + { + // Error opening directory + return NULL; + } + + while ((entry = readdir(dir)) != NULL) + { + snprintf(path, PATH_MAX, "%s/%s", dir_path, entry->d_name); + + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + { + continue; + } + + if (lstat(path, &st) == -1) + { + // Error getting directory status + closedir(dir); + return NULL; + } + + if (S_ISLNK(st.st_mode)) + { + // Skip symbolic links + continue; + } + + if (S_ISDIR(st.st_mode)) + { + // If it's a directory, check if it's the one we're looking for + if (strcmp(entry->d_name, dirname) == 0) + { + char *result = strndup(path, PATH_MAX); + closedir(dir); + return result; + } + else + { + // Recursively search within directories + char* result = fs_search_directory(path, dirname); + if (result != NULL) + { + closedir(dir); + return result; + } + } + } + } + + closedir(dir); + return NULL; +} + +#if defined(__linux__) && defined(STATX_BTIME) + +// Function to return creation time of file +double fs_get_creation_time(const char *path) +{ + struct statx stx; + int fd = open(path, O_RDONLY); + if (fd == -1) + { + return -1; + } + + if (statx(fd, "", AT_EMPTY_PATH, STATX_ALL, &stx) != 0) + { + close(fd); + return -1; + } + + // Close the file + close(fd); + + return stx.stx_btime.tv_sec + stx.stx_btime.tv_nsec / 1e9; +} + +#elif defined(__APPLE__) || defined(__MACH__) + +double fs_get_creation_time(const char *path) +{ + struct stat st; + + if (stat(path, &st) != 0) + { + return -1; + } + + return st.st_mtimespec.tv_sec + st.st_mtimespec.tv_nsec / 1e9; +} + +#else + +double fs_get_creation_time(const char *path) +{ + struct stat st; + + if (stat(path, &st) != 0) + { + return -1; + } + + return (double) st.st_ctime; +} + +#endif diff --git a/src/fs.h b/src/fs.h new file mode 100644 index 0000000..fa78f97 --- /dev/null +++ b/src/fs.h @@ -0,0 +1,31 @@ +/* + * tio - a serial device I/O tool + * + * Copyright (c) 2014-2024 Martin Lund + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#pragma once + +#include +#include + +bool fs_dir_exists(const char *path); +bool fs_file_exists(const char *format, ...); +char* fs_search_directory(const char *dir_path, const char *dirname); +ssize_t fs_read_file_stripped(char *buf, size_t bufsiz, const char *format, ...); +double fs_get_creation_time(const char *path); diff --git a/src/log.c b/src/log.c index 0962d26..4131617 100644 --- a/src/log.c +++ b/src/log.c @@ -19,21 +19,12 @@ * 02110-1301, USA. */ -#define __STDC_WANT_LIB_EXT2__ 1 // To access vasprintf - -#include "config.h" -#include -#include -#include -#include -#include -#include +#define _GNU_SOURCE // To access vasprintf #include #include -#include "options.h" +#include #include "print.h" -#include "error.h" -#include "misc.h" +#include "fs.h" #define IS_ESC_CSI_INTERMEDIATE_CHAR(c) ((c >= 0x20) && (c <= 0x3F)) #define IS_ESC_END_CHAR(c) ((c >= 0x30) && (c <= 0x7E)) @@ -64,8 +55,20 @@ int log_open(const char *filename) if (filename == NULL) { - // Generate filename if none provided ("tio_DEVICE_YYYY-MM-DDTHH:MM:SS.log") - asprintf(&automatic_filename, "tio_%s_%s.log", basename((char *)option.tty_device), date_time()); + // Generate filename if none provided + if (option.auto_connect == AUTO_CONNECT_DIRECT) + { + // File name format ("tio_TARGET_YYYY-MM-DDTHH:MM:SS.log") + asprintf(&automatic_filename, "tio_%s_%s.log", basename((char *)option.target), date_time()); + } + else + { + // If using 'new' or 'latest' autoconnect strategy we simply use strategy + // name to name autogenerated log name as device names may vary + asprintf(&automatic_filename, "tio_%s_%s.log", + option_auto_connect_state_to_string(option.auto_connect), + date_time()); + } if (option.log_directory != NULL) { @@ -194,16 +197,23 @@ void log_putc(char c) return; } - if (option.log_strip) + if (option.output_mode == OUTPUT_MODE_HEX) { - if (!log_strip(c)) - { - fputc(c, fp); - } + fprintf(fp, "%02x ", (unsigned char) c); } else { - fputc(c, fp); + if (option.log_strip) + { + if (log_strip(c) == false) + { + fputc(c, fp); + } + } + else + { + fputc(c, fp); + } } } @@ -212,6 +222,7 @@ void log_close(void) if (fp != NULL) { fclose(fp); + tio_printf("Saved log to file %s", log_filename); fp = NULL; log_filename = NULL; } @@ -221,7 +232,6 @@ void log_exit(void) { if ((option.log) && (log_filename != NULL)) { - tio_printf("Saved log to file %s", log_filename); log_close(); } } diff --git a/src/main.c b/src/main.c index eb9aa60..6676bb3 100644 --- a/src/main.c +++ b/src/main.c @@ -19,10 +19,11 @@ * 02110-1301, USA. */ -#include "config.h" #include #include #include +#include "version.h" +#include "config.h" #include "options.h" #include "configfile.h" #include "tty.h" @@ -45,9 +46,9 @@ int main(int argc, char *argv[]) /* Parse command-line options (1st pass) */ options_parse(argc, argv); - if (option.complete_sub_configs) + if (option.complete_profiles) { - config_file_show_sub_configurations(); + config_file_show_profiles(); return status; } @@ -60,6 +61,10 @@ int main(int argc, char *argv[]) /* Configure tty device */ tty_configure(); + /* Disable line buffering in stdout. This is necessary if we + * want things like local echo to work correctly. */ + setvbuf(stdout, NULL, _IONBF, 0); + /* Configure input terminal */ if (isatty(fileno(stdin))) { @@ -71,6 +76,9 @@ int main(int argc, char *argv[]) interactive_mode = false; } + /* Switch error output format */ + switch_error_output_mode(); + /* Configure output terminal */ if (isatty(fileno(stdout))) { @@ -98,7 +106,7 @@ int main(int argc, char *argv[]) error_enter_session_mode(); /* Print launch hints */ - tio_printf("tio v%s", VERSION); + tio_printf("tio %s", VERSION); if (interactive_mode) { tio_printf("Press ctrl-%c q to quit", option.prefix_key); @@ -121,8 +129,9 @@ int main(int argc, char *argv[]) tty_input_thread_wait_ready(); /* Connect to tty device */ - if (option.no_autoconnect) + if (option.no_reconnect) { + tty_search(); status = tty_connect(); } else @@ -131,7 +140,7 @@ int main(int argc, char *argv[]) while (true) { tty_wait_for_device(); - status = tty_connect(); + tty_connect(); } } diff --git a/src/meson.build b/src/meson.build index dddddd5..05168f7 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,5 +1,10 @@ +# Generate version header +version_h = vcs_tag(command : ['git', 'describe', '--tags', '--always', '--dirty'], + input : 'version.h.in', + output :'version.h', + replace_string:'@VERSION@') + config_h = configuration_data() -config_h.set_quoted('VERSION', meson.project_version()) config_h.set('BAUDRATE_CASES', baudrate_cases) configure_file(output: 'config.h', configuration: config_h) @@ -19,7 +24,10 @@ tio_sources = [ 'timestamp.c', 'alert.c', 'xymodem.c', - 'script.c' + 'script.c', + 'fs.c', + 'readline.c', + version_h ] @@ -35,13 +43,17 @@ endif tio_dep = [ dependency('threads', required: true), - dependency('inih', required: true, - fallback : ['libinih', 'inih_dep'], - default_options: ['default_library=static', 'distro_install=false']), + dependency('glib-2.0', required: true), lua_dep ] -tio_c_args = ['-Wno-unused-result'] +if host_machine.system() == 'darwin' + iokit_dep = dependency('appleframeworks', modules: ['IOKit'], required: true) + corefoundation_dep = dependency('appleframeworks', modules: ['CoreFoundation'], required: true) + tio_dep += [iokit_dep, corefoundation_dep] +endif + +tio_c_args = ['-Wshadow','-Wno-unused-result'] if enable_setspeed2 tio_c_args += '-DHAVE_TERMIOS2' diff --git a/src/misc.c b/src/misc.c index cf57724..bd0a429 100644 --- a/src/misc.c +++ b/src/misc.c @@ -19,19 +19,15 @@ * 02110-1301, USA. */ -#include "config.h" -#include -#include -#include +#define _GNU_SOURCE // For FNM_EXTMATCH #include #include -#include -#include +#include +#include +#include +#include #include -#include -#include "error.h" #include "print.h" -#include "options.h" void delay(long ms) { @@ -48,22 +44,6 @@ void delay(long ms) nanosleep(&ts, NULL); } -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)) - { - printf("Error: Invalid digit\n"); - exit(EXIT_FAILURE); - } - - return result; -} - int ctrl_key_code(unsigned char key) { if ((key >= 'a') && (key <= 'z')) @@ -74,22 +54,6 @@ int ctrl_key_code(unsigned char key) return -1; } -bool fs_dir_exists(const char *path) -{ - struct stat st; - - if (stat(path, &st) != 0) - { - return false; - } - else if (!S_ISDIR(st.st_mode)) - { - return false; - } - - return true; -} - bool regex_match(const char *string, const char *pattern) { regex_t regex; @@ -141,3 +105,149 @@ int read_poll(int fd, void *data, size_t len, int timeout) /* Timeout */ return ret; } + +// Function to calculate djb2 hash of string +unsigned long djb2_hash(const unsigned char *str) +{ + unsigned long hash = 5381; + int c; + + while ((c = *str++)) + { + hash = ((hash << 5) + hash) + c; // hash * 33 + c + } + + return hash; +} + +// Function to encode a number to base62 +void *base62_encode(unsigned long num, char *output) +{ + const char base62_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + if (output == NULL) + { + tio_error_print("Memory allocation failed"); + exit(EXIT_FAILURE); + } + + for (int i = 0; i < 4; ++i) + { + output[i] = base62_chars[num % 62]; + num /= 62; + } + output[4] = '\0'; + + return output; +} + +// Function to return current time +double get_current_time(void) +{ + struct timespec current_time_ts; + + if (clock_gettime(CLOCK_REALTIME, ¤t_time_ts) == -1) + { + // Error + return -1; + } + + return current_time_ts.tv_sec + current_time_ts.tv_nsec / 1e9; +} + +bool match_patterns(const char *string, const char *patterns) +{ + char *pattern; + char *patterns_copy; + + if ((string == NULL) || (patterns == NULL)) + { + return false; + } + + patterns_copy = strdup(patterns); + + // Tokenize the patterns string using strtok + pattern = strtok(patterns_copy, ","); + while (pattern != NULL) + { + // Check if the string matches the current pattern + #ifdef FNM_EXTMATCH + if (fnmatch(pattern, string, FNM_EXTMATCH) == 0) + #else + if (fnmatch(pattern, string, 0) == 0) + #endif + { + free(patterns_copy); + return true; + } + + // Move to the next pattern + pattern = strtok(NULL, ","); + } + + free(patterns_copy); + return false; +} + +// Function that forks subprocess, redirects its stdin and stdout to the +// specified filedescriptor, and runs command. +int execute_shell_command(int fd, const char *command) +{ + pid_t pid; + int status; + + // Fork a child process + pid = fork(); + if (pid == -1) + { + // Error occurred + tio_error_print("fork() failed (%s)", strerror(errno)); + exit(EXIT_FAILURE); + } + else if (pid == 0) + { + // Child process + + tio_printf("Executing shell command '%s'", command); + + // Redirect stdout and stderr to the file descriptor + if (dup2(fd, STDOUT_FILENO) == -1 || dup2(fd, STDERR_FILENO) == -1) + { + tio_error_print("dup2() failed (%s)", strerror(errno)); + exit(EXIT_FAILURE); + } + + // Execute the shell command + execl("/bin/sh", "sh", "-c", command, (char *)NULL); + + // If execlp() returns, it means an error occurred + perror("execlp"); + tio_error_print("execlp() failed (%s)", strerror(errno)); + exit(EXIT_FAILURE); + } + else + { + // Parent process + + // Wait for the child process to finish + waitpid(pid, &status, 0); + + if (WIFEXITED(status)) + { + tio_printf("Command exited with status %d", WEXITSTATUS(status)); + return WEXITSTATUS(status); + } + else + { + tio_error_printf("Child process exited abnormally\n"); + return -1; + } + } + + return 0; +} + +void clear_line() +{ + print("\r\033[K"); +} diff --git a/src/misc.h b/src/misc.h index c669cbd..08ae5eb 100644 --- a/src/misc.h +++ b/src/misc.h @@ -26,12 +26,13 @@ #define UNUSED(expr) do { (void)(expr); } while (0) -char * current_time(void); void delay(long ms); -long string_to_long(char *string); int ctrl_key_code(unsigned char key); -void alert_connect(void); -void alert_disconnect(void); -bool fs_dir_exists(const char *path); bool regex_match(const char *string, const char *pattern); +unsigned long djb2_hash(const unsigned char *str); +void *base62_encode(unsigned long num, char *output); int read_poll(int fd, void *data, size_t len, int timeout); +double get_current_time(void); +bool match_patterns(const char *string, const char *patterns); +int execute_shell_command(int fd, const char *command); +void clear_line(); diff --git a/src/options.c b/src/options.c index feef128..acff7ce 100644 --- a/src/options.c +++ b/src/options.c @@ -19,32 +19,27 @@ * 02110-1301, USA. */ -#include "config.h" -#include -#include -#include -#include -#include -#include -#include +#define _GNU_SOURCE // To access vasprintf + +#include +#include #include -#include -#include -#include "options.h" -#include "error.h" +#include +#include "version.h" +#include "config.h" #include "misc.h" #include "print.h" -#include "tty.h" #include "rs485.h" -#include "timestamp.h" -#include "alert.h" #include "log.h" -#include "script.h" +#include "configfile.h" + +#define HEX_N_VALUE_MAX 4096 enum opt_t { OPT_NONE, OPT_TIMESTAMP_FORMAT, + OPT_TIMESTAMP_TIMEOUT, OPT_LOG_FILE, OPT_LOG_DIRECTORY, OPT_LOG_STRIP, @@ -53,24 +48,28 @@ enum opt_t OPT_RS485, OPT_RS485_CONFIG, OPT_ALERT, - OPT_COMPLETE_SUB_CONFIGS, + OPT_COMPLETE_PROFILES, OPT_MUTE, OPT_SCRIPT, OPT_SCRIPT_FILE, OPT_SCRIPT_RUN, OPT_INPUT_MODE, OPT_OUTPUT_MODE, + OPT_EXCLUDE_DEVICES, + OPT_EXCLUDE_DRIVERS, + OPT_EXCLUDE_TIDS, + OPT_EXEC, }; /* Default options */ struct option_t option = { - .tty_device = "", + .target = "", .baudrate = 115200, .databits = 8, - .flow = "none", + .flow = FLOW_NONE, .stopbits = 1, - .parity = "none", + .parity = PARITY_NONE, .output_delay = 0, .output_line_delay = 0, .dtr_pulse_duration = 100, @@ -79,7 +78,8 @@ struct option_t option = .dsr_pulse_duration = 100, .dcd_pulse_duration = 100, .ri_pulse_duration = 100, - .no_autoconnect = false, + .no_reconnect = false, + .auto_connect = AUTO_CONNECT_DIRECT, .log = false, .log_append = false, .log_filename = NULL, @@ -88,7 +88,6 @@ struct option_t option = .local_echo = false, .timestamp = TIMESTAMP_NONE, .socket = NULL, - .map = "", .color = 256, // Bold .input_mode = INPUT_MODE_NORMAL, .output_mode = OUTPUT_MODE_NORMAL, @@ -101,19 +100,39 @@ struct option_t option = .rs485_delay_rts_before_send = -1, .rs485_delay_rts_after_send = -1, .alert = ALERT_NONE, - .complete_sub_configs = false, + .complete_profiles = false, .script = NULL, .script_filename = NULL, .script_run = SCRIPT_RUN_ALWAYS, + .timestamp_timeout = 200, + .exclude_devices = NULL, + .exclude_drivers = NULL, + .exclude_tids = NULL, + .hex_n_value = 0, + .vt100 = false, + .exec = NULL, + .map_i_nl_cr = false, + .map_i_cr_nl = false, + .map_ign_cr = false, + .map_i_ff_escc = false, + .map_i_nl_crnl = false, + .map_i_cr_crnl = false, + .map_o_cr_nl = false, + .map_o_nl_crnl = false, + .map_o_del_bs = false, + .map_o_ltu = false, + .map_o_nulbrk = false, + .map_i_msb2lsb = false, + .map_o_ign_cr = false, }; -void print_help(char *argv[]) +void option_print_help(char *argv[]) { UNUSED(argv); - printf("Usage: tio [] \n"); + printf("Usage: tio [] \n"); printf("\n"); - printf("Connect to TTY device directly or via sub-configuration.\n"); + printf("Connect to TTY device directly or via configuration profile or topology ID.\n"); printf("\n"); printf("Options:\n"); printf(" -b, --baudrate Baud rate (default: 115200)\n"); @@ -124,14 +143,19 @@ void print_help(char *argv[]) printf(" -o, --output-delay Output character delay (default: 0)\n"); printf(" -O, --output-line-delay Output line delay (default: 0)\n"); printf(" --line-pulse-duration Set line pulse duration\n"); - printf(" -n, --no-autoconnect Disable automatic connect\n"); + printf(" -a, --auto-connect new|latest|direct Automatic connect strategy (default: direct)\n"); + printf(" --exclude-devices Exclude devices by pattern\n"); + printf(" --exclude-drivers Exclude drivers by pattern\n"); + printf(" --exclude-tids Exclude topology IDs by pattern\n"); + printf(" -n, --no-reconnect Do not reconnect\n"); printf(" -e, --local-echo Enable local echo\n"); - printf(" --input-mode normal|hex Select input mode (default: normal)\n"); - printf(" --output-mode normal|hex Select output mode (default: normal)\n"); + printf(" --input-mode normal|hex|line Select input mode (default: normal)\n"); + printf(" --output-mode normal|hex|hexN Select output mode (default: normal)\n"); printf(" -t, --timestamp Enable line timestamp\n"); printf(" --timestamp-format Set timestamp format (default: 24hour)\n"); - printf(" -L, --list-devices List available serial devices by ID\n"); - printf(" -l, --log Enable log to file\n"); + printf(" --timestamp-timeout Set timestamp timeout (default: 200)\n"); + printf(" -l, --list List available serial devices, TIDs, and profiles\n"); + printf(" -L, --log Enable log to file\n"); printf(" --log-file Set log filename\n"); printf(" --log-directory Set log directory path for automatic named logs\n"); printf(" --log-append Append to log file\n"); @@ -142,24 +166,350 @@ void print_help(char *argv[]) printf(" --rs-485 Enable RS-485 mode\n"); printf(" --rs-485-config Set RS-485 configuration\n"); printf(" --alert bell|blink|none Alert on connect/disconnect (default: none)\n"); - printf(" --mute Mute tio\n"); + printf(" --mute Mute tio messages\n"); printf(" --script Run script from string\n"); printf(" --script-file Run script from file\n"); printf(" --script-run once|always|never Run script on connect (default: always)\n"); + printf(" --exec Execute shell command with I/O redirected to device\n"); + printf(" --complete-profiles Prints profiles (for shell completion)\n"); printf(" -v, --version Display version\n"); printf(" -h, --help Display help\n"); printf("\n"); - printf("Options and sub-configurations may be set via configuration file.\n"); + printf("Options and profiles may be set via configuration file.\n"); + printf("\n"); + printf("In session you can press ctrl-%c ? to list available key commands.\n", option.prefix_key); printf("\n"); printf("See the man page for more details.\n"); } -void line_pulse_duration_option_parse(const char *arg) +int option_string_to_integer(const char *string, int *value, const char *desc, int min, int max) +{ + int val; + char *end_token; + + errno = 0; + val = strtol(string, &end_token, 10); + if ((errno != 0) || (*end_token != 0)) + { + if (desc != NULL) + { + tio_error_print("Invalid %s '%s'", desc, string); + exit(EXIT_FAILURE); + } + else + { + tio_error_print("Invalid digit '%s'", string); + exit(EXIT_FAILURE); + } + return -1; + } + else + { + if ((val < min) || (val > max)) + { + if (desc != NULL) + { + tio_error_print("Invalid %s '%s'", desc, string); + exit(EXIT_FAILURE); + } + else + { + tio_error_print("Invalid digit '%s'", string); + exit(EXIT_FAILURE); + } + return -1; + } + else + { + *value = val; + } + } + + return 0; +} + +void option_parse_flow(const char *arg, flow_t *flow) +{ + assert(arg != NULL); + + /* Parse flow control */ + if (strcmp("hard", arg) == 0) + { + *flow = FLOW_HARD; + } + else if (strcmp("soft", arg) == 0) + { + *flow = FLOW_SOFT; + } + else if (strcmp("none", arg) == 0) + { + *flow = FLOW_NONE; + } + else + { + tio_error_print("Invalid flow control '%s'", arg); + exit(EXIT_FAILURE); + } +} + +const char *option_flow_to_string(flow_t flow) +{ + switch (flow) + { + case FLOW_NONE: + return "none"; + case FLOW_HARD: + return "hard"; + case FLOW_SOFT: + return "soft"; + default: + return "unknown"; + } +} + +void option_parse_parity(const char *arg, parity_t *parity) +{ + assert(arg != NULL); + + if (strcmp("none", arg) == 0) + { + *parity = PARITY_NONE; + } + else if (strcmp("odd", arg) == 0) + { + *parity = PARITY_ODD; + } + else if (strcmp("even", arg) == 0) + { + *parity = PARITY_EVEN; + } + else if (strcmp("mark", arg) == 0) + { + *parity = PARITY_MARK; + } + else if (strcmp("space", arg) == 0) + { + *parity = PARITY_SPACE; + } + else + { + tio_error_print("Invalid parity '%s'", arg); + exit(EXIT_FAILURE); + } +} + +const char *option_parity_to_string(parity_t parity) +{ + switch (parity) + { + case PARITY_NONE: + return "none"; + case PARITY_ODD: + return "odd"; + case PARITY_EVEN: + return "even"; + case PARITY_MARK: + return "mark"; + case PARITY_SPACE: + return "space"; + default: + return "unknown"; + } +} + +void option_parse_color(const char *arg, int *color) +{ + int value; + + assert(arg != NULL); + + if (strcmp(optarg, "list") == 0) + { + // Print available color codes + printf("Available color codes:\n"); + for (int i=0; i<=255; i++) + { + printf(" \e[1;38;5;%dmThis is color code %d\e[0m\n", i, i); + } + exit(EXIT_SUCCESS); + } + else if (strcmp(arg, "none") == 0) + { + *color = -1; // No color + } + else if (strcmp(arg, "bold") == 0) + { + *color = 256; // Bold + } + else + { + if (option_string_to_integer(arg, &value, "color code", 0, 255) == 0) + { + *color = value; + } + } +} + +void option_parse_alert(const char *arg, alert_t *alert) +{ + assert(arg != NULL); + + if (strcmp(arg, "none") == 0) + { + *alert = ALERT_NONE; + } + else if (strcmp(arg, "bell") == 0) + { + *alert = ALERT_BELL; + } + else if (strcmp(arg, "blink") == 0) + { + *alert = ALERT_BLINK; + } + else + { + tio_error_print("Invalid alert '%s'", arg); + exit(EXIT_FAILURE); + } +} + +const char* option_timestamp_format_to_string(timestamp_t timestamp) +{ + switch (timestamp) + { + case TIMESTAMP_NONE: + return "none"; + break; + + case TIMESTAMP_24HOUR: + return "24hour"; + break; + + case TIMESTAMP_24HOUR_START: + return "24hour-start"; + break; + + case TIMESTAMP_24HOUR_DELTA: + return "24hour-delta"; + break; + + case TIMESTAMP_ISO8601: + return "iso8601"; + break; + + case TIMESTAMP_EPOCH: + return "epoch"; + break; + + case TIMESTAMP_EPOCH_USEC: + return "epoch-usec"; + break; + + default: + return "unknown"; + break; + } +} + +void option_parse_timestamp(const char *arg, timestamp_t *timestamp) +{ + assert(arg != NULL); + + if (strcmp(arg, "24hour") == 0) + { + *timestamp = TIMESTAMP_24HOUR; + } + else if (strcmp(arg, "24hour-start") == 0) + { + *timestamp = TIMESTAMP_24HOUR_START; + } + else if (strcmp(arg, "24hour-delta") == 0) + { + *timestamp = TIMESTAMP_24HOUR_DELTA; + } + else if (strcmp(arg, "iso8601") == 0) + { + *timestamp = TIMESTAMP_ISO8601; + } + else if (strcmp(arg, "epoch") == 0) + { + *timestamp = TIMESTAMP_EPOCH; + } + else if (strcmp(arg, "epoch-usec") == 0) + { + *timestamp = TIMESTAMP_EPOCH_USEC; + } + else + { + tio_error_print("Invalid timestamp '%s'", arg); + exit(EXIT_FAILURE); + } +} + +const char *option_alert_state_to_string(alert_t state) +{ + switch (state) + { + case ALERT_NONE: + return "none"; + case ALERT_BELL: + return "bell"; + case ALERT_BLINK: + return "blink"; + default: + return "unknown"; + } +} + +const char *option_auto_connect_state_to_string(auto_connect_t strategy) +{ + switch (strategy) + { + case AUTO_CONNECT_DIRECT: + return "direct"; + case AUTO_CONNECT_NEW: + return "new"; + case AUTO_CONNECT_LATEST: + return "latest"; + default: + return "unknown"; + } +} + +void option_parse_auto_connect(const char *arg, auto_connect_t *auto_connect) +{ + assert(arg != NULL); + + if (arg != NULL) + { + if (strcmp(arg, "direct") == 0) + { + *auto_connect = AUTO_CONNECT_DIRECT; + } + else if (strcmp(arg, "new") == 0) + { + *auto_connect = AUTO_CONNECT_NEW; + } + else if (strcmp(arg, "latest") == 0) + { + *auto_connect = AUTO_CONNECT_LATEST; + } + else + { + tio_error_print("Invalid auto-connect strategy '%s'", arg); + exit(EXIT_FAILURE); + } + } +} + +void option_parse_line_pulse_duration(const char *arg) { bool token_found = true; char *token = NULL; char *buffer = strdup(arg); + assert(arg != NULL); + while (token_found == true) { if (token == NULL) @@ -202,6 +552,11 @@ void line_pulse_duration_option_parse(const char *arg) { option.ri_pulse_duration = value; } + else + { + tio_error_print("Invalid line '%s'", keyname); + exit(EXIT_FAILURE); + } } else { @@ -216,41 +571,107 @@ void line_pulse_duration_option_parse(const char *arg) free(buffer); } -input_mode_t input_mode_option_parse(const char *arg) +// Function to parse the input string +int option_parse_hexN_string(const char *input_string) { - if (strcmp("normal", arg) == 0) + regmatch_t match[2]; // One for entire match, one for the optional N + int n_value = 0; + regex_t regex; + int ret; + + // Compile the regular expression to match "hex" and optionally capture N + ret = regcomp(®ex, "^hex([0-9]+)?$", REG_EXTENDED); + if (ret) { - return INPUT_MODE_NORMAL; + tio_error_print("Could not compile regex"); + exit(EXIT_FAILURE); } - else if (strcmp("hex", arg) == 0) + + // Execute the regular expression + ret = regexec(®ex, input_string, 2, match, 0); + if (!ret) { - return INPUT_MODE_HEX; + // If there is a match, extract the N value if present + if (match[1].rm_so != -1) + { + char n_value_str[32]; // Assume max 32 digits for the numerical value + strncpy(n_value_str, input_string + match[1].rm_so, match[1].rm_eo - match[1].rm_so); + n_value_str[match[1].rm_eo - match[1].rm_so] = '\0'; // Null-terminate the string + n_value = atoi(n_value_str); + + if ((n_value > HEX_N_VALUE_MAX) || (n_value == 0)) + { + n_value = -1; + } + } + else + { + n_value = 0; + } + } + else if (ret == REG_NOMATCH) + { + n_value = -1; } else { - tio_error_printf("Invalid input mode option"); + char msgbuf[100]; + regerror(ret, ®ex, msgbuf, sizeof(msgbuf)); + tio_error_print("Regex match failed: %s", msgbuf); + exit(EXIT_FAILURE); + } + + regfree(®ex); + + return n_value; +} + +void option_parse_input_mode(const char *arg, input_mode_t *mode) +{ + assert(arg != NULL); + + if (strcmp("normal", arg) == 0) + { + *mode = INPUT_MODE_NORMAL; + } + else if (strcmp("hex", arg) == 0) + { + *mode = INPUT_MODE_HEX; + } + else if (strcmp("line", arg) == 0) + { + *mode = INPUT_MODE_LINE; + } + else + { + tio_error_print("Invalid input mode '%s'", arg); exit(EXIT_FAILURE); } } -output_mode_t output_mode_option_parse(const char *arg) +void option_parse_output_mode(const char *arg, output_mode_t *mode) { + int n = 0; + + assert(arg != NULL); + if (strcmp("normal", arg) == 0) { - return OUTPUT_MODE_NORMAL; + *mode = OUTPUT_MODE_NORMAL; } - else if (strcmp("hex", arg) == 0) + else if ((n = option_parse_hexN_string(arg)) != -1) { - return OUTPUT_MODE_HEX; + option.hex_n_value = n; + *mode = OUTPUT_MODE_HEX; } else { - tio_error_printf("Invalid output mode option"); + tio_error_print("Invalid output mode '%s'", arg); exit(EXIT_FAILURE); } } -const char *input_mode_by_string(input_mode_t mode) +const char *option_input_mode_to_string(input_mode_t mode) { switch (mode) { @@ -258,6 +679,8 @@ const char *input_mode_by_string(input_mode_t mode) return "normal"; case INPUT_MODE_HEX: return "hex"; + case INPUT_MODE_LINE: + return "line"; case INPUT_MODE_END: break; } @@ -265,7 +688,7 @@ const char *input_mode_by_string(input_mode_t mode) return NULL; } -const char *output_mode_by_string(output_mode_t mode) +const char *option_output_mode_to_string(output_mode_t mode) { switch (mode) { @@ -280,54 +703,164 @@ const char *output_mode_by_string(output_mode_t mode) return NULL; } -enum script_run_t script_run_option_parse(const char *arg) +void option_parse_script_run(const char *arg, script_run_t *script_run) { + assert(arg != NULL); + if (strcmp("once", arg) == 0) { - return SCRIPT_RUN_ONCE; + *script_run = SCRIPT_RUN_ONCE; } else if (strcmp("always", arg) == 0) { - return SCRIPT_RUN_ALWAYS; + *script_run = SCRIPT_RUN_ALWAYS; } else if (strcmp("never", arg) == 0) { - return SCRIPT_RUN_NEVER; + *script_run = SCRIPT_RUN_NEVER; } else { - tio_error_printf("Invalid script run option"); + tio_error_print("Invalid script run option '%s'", arg); exit(EXIT_FAILURE); } } +void option_parse_mappings(const char *map) +{ + bool token_found = true; + char *token = NULL; + char *buffer; + + if (map == NULL) + { + return; + } + + /* Parse any specified input or output mappings */ + buffer = strdup(map); + while (token_found == true) + { + if (token == NULL) + { + token = strtok(buffer,","); + } + else + { + token = strtok(NULL, ","); + } + + if (token != NULL) + { + if (strcmp(token,"INLCR") == 0) + { + option.map_i_nl_cr = true; + } + else if (strcmp(token,"IGNCR") == 0) + { + option.map_ign_cr = true; + } + else if (strcmp(token,"ICRNL") == 0) + { + option.map_i_cr_nl = true; + } + else if (strcmp(token,"OCRNL") == 0) + { + option.map_o_cr_nl = true; + } + else if (strcmp(token,"ODELBS") == 0) + { + option.map_o_del_bs = true; + } + else if (strcmp(token,"IFFESCC") == 0) + { + option.map_i_ff_escc = true; + } + else if (strcmp(token,"INLCRNL") == 0) + { + option.map_i_nl_crnl = true; + } + else if (strcmp(token,"ICRCRNL") == 0) + { + option.map_i_cr_crnl = true; + } + else if (strcmp(token, "ONLCRNL") == 0) + { + option.map_o_nl_crnl = true; + } + else if (strcmp(token, "OLTU") == 0) + { + option.map_o_ltu = true; + } + else if (strcmp(token, "ONULBRK") == 0) + { + option.map_o_nulbrk = true; + } + else if (strcmp(token, "OIGNCR") == 0) + { + option.map_o_ign_cr = true; + } + else if (strcmp(token, "IMSB2LSB") == 0) + { + option.map_i_msb2lsb = true; + } + else + { + printf("Error: Unknown mapping flag %s\n", token); + exit(EXIT_FAILURE); + } + } + else + { + token_found = false; + } + } + free(buffer); +} + void options_print() { - tio_printf(" Device: %s", option.tty_device); + tio_printf(" Device: %s", device_name); tio_printf(" Baudrate: %u", option.baudrate); tio_printf(" Databits: %d", option.databits); - tio_printf(" Flow: %s", option.flow); + tio_printf(" Flow: %s", option_flow_to_string(option.flow)); tio_printf(" Stopbits: %d", option.stopbits); - tio_printf(" Parity: %s", option.parity); - tio_printf(" Local echo: %s", option.local_echo ? "enabled" : "disabled"); - tio_printf(" Timestamp: %s", timestamp_state_to_string(option.timestamp)); + tio_printf(" Parity: %s", option_parity_to_string(option.parity)); + tio_printf(" Local echo: %s", option.local_echo ? "true" : "false"); + tio_printf(" Timestamp: %s", option_timestamp_format_to_string(option.timestamp)); + tio_printf(" Timestamp timeout: %u", option.timestamp_timeout); tio_printf(" Output delay: %d", option.output_delay); tio_printf(" Output line delay: %d", option.output_line_delay); - tio_printf(" Auto connect: %s", option.no_autoconnect ? "disabled" : "enabled"); + tio_printf(" Automatic connect strategy: %s", option_auto_connect_state_to_string(option.auto_connect)); + tio_printf(" Automatic reconnect: %s", option.no_reconnect ? "true" : "false"); tio_printf(" Pulse duration: DTR=%d RTS=%d CTS=%d DSR=%d DCD=%d RI=%d", option.dtr_pulse_duration, option.rts_pulse_duration, option.cts_pulse_duration, option.dsr_pulse_duration, option.dcd_pulse_duration, option.ri_pulse_duration); - tio_printf(" Input mode: %s", input_mode_by_string(option.input_mode)); - tio_printf(" Output mode: %s", output_mode_by_string(option.output_mode)); - if (option.map[0] != 0) - tio_printf(" Map flags: %s", option.map); + tio_printf(" Input mode: %s", option_input_mode_to_string(option.input_mode)); + tio_printf(" Output mode: %s", option_output_mode_to_string(option.output_mode)); + tio_printf(" Alert: %s", option_alert_state_to_string(option.alert)); if (option.log) + { tio_printf(" Log file: %s", log_get_filename()); + if (option.log_directory != NULL) + { + tio_printf(" Log file directory: %s", option.log_directory); + } + tio_printf(" Log append: %s", option.log_append ? "true" : "false"); + tio_printf(" Log strip: %s", option.log_strip ? "true" : "false"); + } if (option.socket) + { tio_printf(" Socket: %s", option.socket); + } + if (option.script_filename != NULL) + { + tio_printf(" Script file: %s", option.script_filename); + tio_printf(" Script run: %s", script_run_state_to_string(option.script_run)); + } } void options_parse(int argc, char *argv[]) @@ -336,14 +869,23 @@ void options_parse(int argc, char *argv[]) if (argc == 1) { - print_help(argv); + option_print_help(argv); exit(EXIT_SUCCESS); } // Support no-color.org informal spec char *no_color = getenv("NO_COLOR"); - if(no_color != NULL && no_color[0] != '\0') - option.color = -1; + if (no_color != NULL && no_color[0] != '\0') + { + option.color = -1; + } + + // Check for vt100 terminal + char *term = getenv("TERM"); + if ((term != NULL) && (!strcmp(term, "vt100"))) + { + option.vt100 = true; + } while (1) { @@ -357,12 +899,17 @@ void options_parse(int argc, char *argv[]) {"output-delay", required_argument, 0, 'o' }, {"output-line-delay" , required_argument, 0, 'O' }, {"line-pulse-duration", required_argument, 0, OPT_LINE_PULSE_DURATION }, - {"no-autoconnect", no_argument, 0, 'n' }, + {"auto-connect", required_argument, 0, 'a' }, + {"exclude-devices", required_argument, 0, OPT_EXCLUDE_DEVICES }, + {"exclude-drivers", required_argument, 0, OPT_EXCLUDE_DRIVERS }, + {"exclude-tids", required_argument, 0, OPT_EXCLUDE_TIDS }, + {"no-reconnect", no_argument, 0, 'n' }, {"local-echo", no_argument, 0, 'e' }, {"timestamp", no_argument, 0, 't' }, {"timestamp-format", required_argument, 0, OPT_TIMESTAMP_FORMAT }, - {"list-devices", no_argument, 0, 'L' }, - {"log", no_argument, 0, 'l' }, + {"timestamp-timeout", required_argument, 0, OPT_TIMESTAMP_TIMEOUT }, + {"list", no_argument, 0, 'l' }, + {"log", no_argument, 0, 'L' }, {"log-file", required_argument, 0, OPT_LOG_FILE }, {"log-directory", required_argument, 0, OPT_LOG_DIRECTORY }, {"log-append", no_argument, 0, OPT_LOG_APPEND }, @@ -379,9 +926,10 @@ void options_parse(int argc, char *argv[]) {"script", required_argument, 0, OPT_SCRIPT }, {"script-file", required_argument, 0, OPT_SCRIPT_FILE }, {"script-run", required_argument, 0, OPT_SCRIPT_RUN }, + {"exec", required_argument, 0, OPT_EXEC }, {"version", no_argument, 0, 'v' }, {"help", no_argument, 0, 'h' }, - {"complete-sub-configs", no_argument, 0, OPT_COMPLETE_SUB_CONFIGS}, + {"complete-profiles", no_argument, 0, OPT_COMPLETE_PROFILES }, {0, 0, 0, 0 } }; @@ -389,7 +937,7 @@ void options_parse(int argc, char *argv[]) int option_index = 0; /* Parse argument using getopt_long */ - c = getopt_long(argc, argv, "b:d:f:s:p:o:O:netLlS:m:c:xrvh", long_options, &option_index); + c = getopt_long(argc, argv, "b:d:f:s:p:o:O:a:netLlS:m:c:xrvh", long_options, &option_index); /* Detect the end of the options */ if (c == -1) @@ -408,39 +956,55 @@ void options_parse(int argc, char *argv[]) break; case 'b': - option.baudrate = string_to_long(optarg); + option_string_to_integer(optarg, &option.baudrate, "baudrate", 0, INT_MAX); break; case 'd': - option.databits = string_to_long(optarg); + option_string_to_integer(optarg, &option.databits, "databits", 5, 8); break; case 'f': - option.flow = optarg; + option_parse_flow(optarg, &option.flow); break; case 's': - option.stopbits = string_to_long(optarg); + option_string_to_integer(optarg, &option.stopbits, "stopbits", 1, 2); break; case 'p': - option.parity = optarg; + option_parse_parity(optarg, &option.parity); break; case 'o': - option.output_delay = string_to_long(optarg); + option_string_to_integer(optarg, &option.output_delay, "output delay", 0, INT_MAX); break; case 'O': - option.output_line_delay = string_to_long(optarg); + option_string_to_integer(optarg, &option.output_line_delay, "output line delay", 0, INT_MAX); break; case OPT_LINE_PULSE_DURATION: - line_pulse_duration_option_parse(optarg); + option_parse_line_pulse_duration(optarg); + break; + + case 'a': + option_parse_auto_connect(optarg, &option.auto_connect); + break; + + case OPT_EXCLUDE_DEVICES: + option.exclude_devices = optarg; + break; + + case OPT_EXCLUDE_DRIVERS: + option.exclude_drivers = optarg; + break; + + case OPT_EXCLUDE_TIDS: + option.exclude_tids = optarg; break; case 'n': - option.no_autoconnect = true; + option.no_reconnect = true; break; case 'e': @@ -448,20 +1012,28 @@ void options_parse(int argc, char *argv[]) break; case 't': - option.timestamp = TIMESTAMP_24HOUR; + if (option.timestamp == TIMESTAMP_NONE) + { + option.timestamp = TIMESTAMP_24HOUR; + } break; case OPT_TIMESTAMP_FORMAT: - option.timestamp = timestamp_option_parse(optarg); + option_parse_timestamp(optarg, &option.timestamp); + break; + + case OPT_TIMESTAMP_TIMEOUT: + option_string_to_integer(optarg, &option.timestamp_timeout, "timestamp timeout", 0, INT_MAX); break; case 'L': - list_serial_devices(); - exit(EXIT_SUCCESS); + option.log = true; break; case 'l': - option.log = true; + list_serial_devices(); + config_list_targets(); + exit(EXIT_SUCCESS); break; case OPT_LOG_FILE: @@ -485,45 +1057,19 @@ void options_parse(int argc, char *argv[]) break; case 'm': - option.map = optarg; + option_parse_mappings(optarg); break; case 'c': - if (!strcmp(optarg, "list")) - { - // Print available color codes - printf("Available color codes:\n"); - for (int i=0; i<=255; i++) - { - printf(" \e[1;38;5;%dmThis is color code %d\e[0m\n", i, i); - } - exit(EXIT_SUCCESS); - } - else if (!strcmp(optarg, "none")) - { - option.color = -1; // No color - break; - } - else if (!strcmp(optarg, "bold")) - { - option.color = 256; // Bold - break; - } - - option.color = string_to_long(optarg); - if ((option.color < 0) || (option.color > 255)) - { - tio_error_printf("Invalid color code"); - exit(EXIT_FAILURE); - } + option_parse_color(optarg, &option.color); break; case OPT_INPUT_MODE: - option.input_mode = input_mode_option_parse(optarg); + option_parse_input_mode(optarg, &option.input_mode); break; case OPT_OUTPUT_MODE: - option.output_mode = output_mode_option_parse(optarg); + option_parse_output_mode(optarg, &option.output_mode); break; case OPT_RS485: @@ -535,7 +1081,7 @@ void options_parse(int argc, char *argv[]) break; case OPT_ALERT: - option.alert = alert_option_parse(optarg); + option_parse_alert(optarg, &option.alert); break; case OPT_MUTE: @@ -551,21 +1097,25 @@ void options_parse(int argc, char *argv[]) break; case OPT_SCRIPT_RUN: - option.script_run = script_run_option_parse(optarg); + option_parse_script_run(optarg, &option.script_run); + break; + + case OPT_EXEC: + option.exec = optarg; break; case 'v': - printf("tio v%s\n", VERSION); + printf("tio %s\n", VERSION); exit(EXIT_SUCCESS); break; case 'h': - print_help(argv); + option_print_help(argv); exit(EXIT_SUCCESS); break; - case OPT_COMPLETE_SUB_CONFIGS: - option.complete_sub_configs = true; + case OPT_COMPLETE_PROFILES: + option.complete_profiles = true; break; case '?': @@ -578,24 +1128,33 @@ void options_parse(int argc, char *argv[]) } } - /* Assume first non-option is the tty device name */ - if (strcmp(option.tty_device, "")) + /* Assume first non-option is the target (tty device, profile, tid) */ + if (strcmp(option.target, "")) + { optind++; + } else if (optind < argc) - option.tty_device = argv[optind++]; + { + option.target = argv[optind++]; + } - if (option.complete_sub_configs) + if (option.complete_profiles) { return; } - if (strlen(option.tty_device) == 0) + if (option.auto_connect != AUTO_CONNECT_DIRECT) { - tio_error_printf("Missing tty device or sub-configuration name"); + return; + } + + if (strlen(option.target) == 0) + { + tio_error_print("Missing tty device, profile or topology ID"); exit(EXIT_FAILURE); } - /* Print any remaining command line arguments (unknown options) */ + /* Print any remaining command line arguments as unknown */ if (optind < argc) { fprintf(stderr, "Error: Unknown argument "); @@ -610,9 +1169,6 @@ void options_parse(int argc, char *argv[]) void options_parse_final(int argc, char *argv[]) { - /* Preserve tty device which may have been set by configuration file */ - const char *tty_device = option.tty_device; - /* Do 2nd pass to override settings set by configuration file */ optind = 1; // Reset option index to restart scanning of argv options_parse(argc, argv); @@ -620,16 +1176,13 @@ void options_parse_final(int argc, char *argv[]) #ifdef __CYGWIN__ unsigned char portnum; char *tty_win; - if ( ((strncmp("COM", tty_device, 3) == 0) - || (strncmp("com", tty_device, 3) == 0) ) - && (sscanf(tty_device + 3, "%hhu", &portnum) == 1) - && (portnum > 0) ) + if ( ((strncmp("COM", option.target, 3) == 0) + || (strncmp("com", option.target, 3) == 0) ) + && (sscanf(option.target + 3, "%hhu", &portnum) == 1) + && (portnum > 0) ) { asprintf(&tty_win, "/dev/ttyS%hhu", portnum - 1); - tty_device = tty_win; + option.target = tty_win; } #endif - - /* Restore tty device */ - option.tty_device = tty_device; } diff --git a/src/options.h b/src/options.h index 7a6b627..c552217 100644 --- a/src/options.h +++ b/src/options.h @@ -23,17 +23,16 @@ #include #include -#include -#include -#include #include "script.h" #include "timestamp.h" #include "alert.h" +#include "tty.h" typedef enum { INPUT_MODE_NORMAL, INPUT_MODE_HEX, + INPUT_MODE_LINE, INPUT_MODE_END, } input_mode_t; @@ -47,46 +46,66 @@ typedef enum /* Options */ struct option_t { - const char *tty_device; - unsigned int baudrate; + char *target; + int baudrate; int databits; - char *flow; + flow_t flow; int stopbits; - char *parity; + parity_t parity; int output_delay; int output_line_delay; - unsigned int dtr_pulse_duration; - unsigned int rts_pulse_duration; - unsigned int cts_pulse_duration; - unsigned int dsr_pulse_duration; - unsigned int dcd_pulse_duration; - unsigned int ri_pulse_duration; - bool no_autoconnect; + int dtr_pulse_duration; + int rts_pulse_duration; + int cts_pulse_duration; + int dsr_pulse_duration; + int dcd_pulse_duration; + int ri_pulse_duration; + bool no_reconnect; + auto_connect_t auto_connect; bool log; bool log_append; bool log_strip; bool local_echo; - enum timestamp_t timestamp; - const char *log_filename; - const char *log_directory; - const char *map; - const char *socket; + timestamp_t timestamp; + char *log_filename; + char *log_directory; + char *socket; int color; input_mode_t input_mode; output_mode_t output_mode; - unsigned char prefix_code; - unsigned char prefix_key; + char prefix_code; + char prefix_key; bool prefix_enabled; bool mute; bool rs485; uint32_t rs485_config_flags; int32_t rs485_delay_rts_before_send; int32_t rs485_delay_rts_after_send; - enum alert_t alert; - bool complete_sub_configs; - const char *script; - const char *script_filename; - enum script_run_t script_run; + alert_t alert; + bool complete_profiles; + char *script; + char *script_filename; + script_run_t script_run; + int timestamp_timeout; + char *exclude_devices; + char *exclude_drivers; + char *exclude_tids; + int hex_n_value; + bool vt100; + char *exec; + bool map_i_nl_cr; + bool map_i_cr_nl; + bool map_ign_cr; + bool map_i_ff_escc; + bool map_i_nl_crnl; + bool map_i_cr_crnl; + bool map_o_cr_nl; + bool map_o_nl_crnl; + bool map_o_del_bs; + bool map_o_ltu; + bool map_o_nulbrk; + bool map_i_msb2lsb; + bool map_o_ign_cr; }; extern struct option_t option; @@ -95,8 +114,22 @@ void options_print(); void options_parse(int argc, char *argv[]); void options_parse_final(int argc, char *argv[]); -void line_pulse_duration_option_parse(const char *arg); -enum script_run_t script_run_option_parse(const char *arg); +int option_string_to_integer(const char *string, int *value, const char *desc, int min, int max); -input_mode_t input_mode_option_parse(const char *arg); -output_mode_t output_mode_option_parse(const char *arg); +void option_parse_flow(const char *arg, flow_t *flow); +void option_parse_parity(const char *arg, parity_t *parity); + +void option_parse_output_mode(const char *arg, output_mode_t *mode); +void option_parse_input_mode(const char *arg, input_mode_t *mode); + +void option_parse_line_pulse_duration(const char *arg); +void option_parse_script_run(const char *arg, script_run_t *script_run); +void option_parse_alert(const char *arg, alert_t *alert); + +void option_parse_auto_connect(const char *arg, auto_connect_t *auto_connect); +const char *option_auto_connect_state_to_string(auto_connect_t strategy); + +void option_parse_timestamp(const char *arg, timestamp_t *timestamp); +const char* option_timestamp_format_to_string(timestamp_t timestamp); + +void option_parse_mappings(const char *map); diff --git a/src/print.c b/src/print.c index 1c891aa..06f7e73 100644 --- a/src/print.c +++ b/src/print.c @@ -19,11 +19,6 @@ * 02110-1301, USA. */ -#include -#include -#include -#include -#include "options.h" #include "print.h" bool print_tainted = false; @@ -31,11 +26,13 @@ char ansi_format[30]; void print_hex(char c) { + print_tainted = true; printf("%02x ", (unsigned char) c); } void print_normal(char c) { + print_tainted = true; putchar(c); } @@ -78,3 +75,38 @@ void print_tainted_set() { print_tainted = true; } + +void print(const char *format, ...) +{ + va_list args; + + va_start(args, format); + vprintf(format, args); + fflush(stdout); + va_end(args); + + print_tainted = true; +} + +void print_padded(char *string, size_t length, char pad_char) +{ + size_t padding = 0; + size_t string_length = 0; + size_t i; + + string_length = strlen(string); + + if (string_length < length) + { + padding += length - string_length; + printf("%s", string); + for (i=0; i #include -#include "misc.h" #include "error.h" #include "options.h" #include "timestamp.h" @@ -77,6 +76,7 @@ extern char ansi_format[]; fprintf (stdout, "\r[%s] Warning: " format "\r\n", timestamp_current_time(), ## args); \ else \ ansi_printf("[%s] Warning: " format, timestamp_current_time(), ## args); \ + print_tainted = false; \ } \ } @@ -86,10 +86,19 @@ extern char ansi_format[]; { \ if (print_tainted) \ putchar('\n'); \ - if (option.color < 0) \ - fprintf (stdout, "\r[%s] Error: " format "\r\n", timestamp_current_time(), ## args); \ - else \ - ansi_printf("[%s] Error: " format, timestamp_current_time(), ## args); \ + if (option.color < 0) { \ + if (error_normal) \ + fprintf (stderr, "Error: " format "\n", ## args); \ + else \ + fprintf (stderr, "\r[%s] Error: " format "\r\n", timestamp_current_time(), ## args); \ + } \ + else { \ + if (error_normal) \ + { ansi_error_printf("Error: " format, ## args); }\ + else \ + { ansi_error_printf("[%s] Error: " format, timestamp_current_time(), ## args); }\ + } \ + print_tainted = false; \ } \ } @@ -125,8 +134,10 @@ extern char ansi_format[]; #define tio_debug_printf_raw(format, args...) #endif +void print(const char *format, ...); void print_hex(char c); void print_normal(char c); void print_init_ansi_formatting(void); void tio_printf_array(const char *array); void print_tainted_set(void); +void print_padded(char *string, size_t length, char pad_char); diff --git a/src/readline.c b/src/readline.c new file mode 100644 index 0000000..8176a0b --- /dev/null +++ b/src/readline.c @@ -0,0 +1,276 @@ +/* + * tio - a serial device I/O tool + * + * Copyright (c) 2014-2024 Martin Lund + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#include "print.h" +#include "misc.h" + +#define RL_LINE_LENGTH_MAX PATH_MAX +#define RL_HISTORY_MAX 1000 + +static char rl_line[RL_LINE_LENGTH_MAX] = {}; +static char *rl_history[RL_HISTORY_MAX]; +static int rl_history_count = 0; +static int rl_history_index = 0; +static int rl_line_length = 0; +static int rl_cursor_pos = 0; +static int rl_escape = 0; + +static void print_line(const char *string, int cursor_pos) +{ + clear_line(); + print("%s", string); + print("\r"); // Move the cursor back to the beginning + for (int i = 0; i < cursor_pos; ++i) + { + print("\x1b[C"); // Move the cursor right + } +} + +void readline_init(void) +{ + rl_history_count = 0; + rl_history_index = 0; + + for (int i = 0; i < RL_HISTORY_MAX; ++i) + { + rl_history[i] = NULL; + } + + rl_line[0] = 0; + rl_line_length = 0; + rl_cursor_pos = 0; + rl_escape = 0; +} + +char * readline_get(void) +{ + return rl_line; +} + +static void readline_input_char(char input_char) +{ + if (rl_line_length < RL_LINE_LENGTH_MAX - 1) + { + memmove(&rl_line[rl_cursor_pos + 1], &rl_line[rl_cursor_pos], rl_line_length - rl_cursor_pos); + rl_line[rl_cursor_pos] = input_char; + rl_line_length++; + rl_cursor_pos++; + rl_line[rl_line_length] = '\0'; + print_line(rl_line, rl_cursor_pos); + } + rl_escape = 0; +} + +static void readline_input_cr(void) +{ + if (rl_line_length > 0) + { + // Save to history + if (rl_history_count < RL_HISTORY_MAX) + { + rl_history[rl_history_count] = strndup(rl_line, rl_line_length); + rl_history_count++; + } + else + { + free(rl_history[0]); + memmove(&rl_history[0], &rl_history[1], (RL_HISTORY_MAX - 1) * sizeof(char*)); + rl_history[RL_HISTORY_MAX - 1] = strndup(rl_line, rl_line_length); + } + } + + rl_line[rl_line_length] = '\0'; + if (option.local_echo == false) + { + clear_line(); + } + else + { + print("\r\n"); + } + + rl_line_length = 0; + rl_cursor_pos = 0; + rl_history_index = rl_history_count; + rl_escape = 0; +} + +static void readline_input_bs(void) +{ + if (rl_cursor_pos > 0) + { + memmove(&rl_line[rl_cursor_pos - 1], &rl_line[rl_cursor_pos], rl_line_length - rl_cursor_pos); + rl_line_length--; + rl_cursor_pos--; + rl_line[rl_line_length] = '\0'; + print_line(rl_line, rl_cursor_pos); + } + rl_escape = 0; +} + +static void readline_input_escape(void) +{ + rl_escape = 1; +} + +static void readline_input_left_bracket(void) +{ + if (rl_escape == 1) + { + rl_escape = 2; + } + else + { + rl_escape = 0; + } +} + +static void readline_input_A(void) +{ + if (rl_escape == 2) + { + // Up arrow + if (rl_history_index > 0) + { + rl_history_index--; + strncpy(rl_line, rl_history[rl_history_index], RL_LINE_LENGTH_MAX-1); + rl_line_length = strlen(rl_line); + rl_cursor_pos = rl_line_length; + print_line(rl_line, rl_cursor_pos); + } + } + else + { + readline_input_char('A'); + } + + rl_escape = 0; +} + +static void readline_input_B(void) +{ + if (rl_escape == 2) + { + // Down arrow + if (rl_history_index < rl_history_count - 1) + { + rl_history_index++; + strncpy(rl_line, rl_history[rl_history_index], RL_LINE_LENGTH_MAX-1); + rl_line_length = strlen(rl_line); + rl_cursor_pos = rl_line_length; + print_line(rl_line, rl_cursor_pos); + } + else if (rl_history_index == rl_history_count - 1) + { + rl_history_index++; + rl_line_length = 0; + rl_cursor_pos = 0; + rl_line[rl_line_length] = '\0'; + print_line(rl_line, rl_cursor_pos); + } + } + else + { + readline_input_char('B'); + } + + rl_escape = 0; +} + +static void readline_input_C(void) +{ + if (rl_escape == 2) + { + // Right arrow + if (rl_cursor_pos < rl_line_length) + { + rl_cursor_pos++; + print("\x1b[C"); + } + } + else + { + readline_input_char('C'); + } + + rl_escape = 0; +} + +static void readline_input_D(void) +{ + if (rl_escape == 2) + { + // Left arrow + if (rl_cursor_pos > 0) + { + rl_cursor_pos--; + print("\b"); + } + } + else + { + readline_input_char('D'); + } + + rl_escape = 0; +} + +void readline_input(char input_char) +{ + switch (input_char) + { + case '\r': // Carriage return + readline_input_cr(); + break; + + case 127: // Backspace + readline_input_bs(); + break; + + case 27: // Escape + readline_input_escape(); + break; + + case '[': + readline_input_left_bracket(); + break; + + case 'A': + readline_input_A(); + break; + + case 'B': + readline_input_B(); + break; + + case 'C': + readline_input_C(); + break; + + case 'D': + readline_input_D(); + break; + + default: + readline_input_char(input_char); + break; + } +} diff --git a/src/readline.h b/src/readline.h new file mode 100644 index 0000000..46c3f10 --- /dev/null +++ b/src/readline.h @@ -0,0 +1,26 @@ +/* + * tio - a serial device I/O tool + * + * Copyright (c) 2014-2024 Martin Lund + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#pragma once + +void readline_init(void); +void readline_input(char input_char); +char * readline_get(void); diff --git a/src/rs485.c b/src/rs485.c index c1b06db..3c9280c 100644 --- a/src/rs485.c +++ b/src/rs485.c @@ -19,14 +19,13 @@ * 02110-1301, USA. */ -#include -#include +#include #include +#include #include -#include #include "options.h" #include "print.h" -#include "error.h" +#include "misc.h" #ifdef HAVE_RS485 @@ -128,7 +127,7 @@ void rs485_print_config(void) tio_printf(" RTS_AFTER_SEND: %s", (rs485_config.flags & SER_RS485_RTS_AFTER_SEND) ? "high" : "low"); tio_printf(" RTS_DELAY_BEFORE_SEND = %d", rs485_config.delay_rts_before_send); tio_printf(" RTS_DELAY_AFTER_SEND = %d", rs485_config.delay_rts_after_send); - tio_printf(" RX_DURING_TX: %s", (rs485_config.flags & SER_RS485_RX_DURING_TX) ? "enabled" : "disabled"); + tio_printf(" RX_DURING_TX: %s", (rs485_config.flags & SER_RS485_RX_DURING_TX) ? "true" : "false"); } int rs485_mode_enable(int fd) diff --git a/src/script.c b/src/script.c index 5a8fd41..b69d55b 100644 --- a/src/script.c +++ b/src/script.c @@ -20,7 +20,6 @@ */ #include -#include #include #include #include @@ -29,20 +28,103 @@ #include #include #include +#include #include "misc.h" #include "print.h" #include "options.h" #include "tty.h" #include "xymodem.h" +#include "log.h" +#include "script.h" +#include "fs.h" +#include "timestamp.h" +#include "termios.h" #define MAX_BUFFER_SIZE 2000 // Maximum size of circular buffer +#define READ_LINE_SIZE 4096 // read_line buffer length static int device_fd; -static char circular_buffer[MAX_BUFFER_SIZE]; -static int buffer_size = 0; -// lua: sleep(seconds) -static int sleep_(lua_State *L) +static char script_init[] = +"tio.set = function(arg)\n" +" local dtr = arg.DTR or -1\n" +" local rts = arg.RTS or -1\n" +" local cts = arg.CTS or -1\n" +" local dsr = arg.DSR or -1\n" +" local cd = arg.CD or -1\n" +" local ri = arg.RI or -1\n" +" tio.line_set(dtr, rts, cts, dsr, cd, ri)\n" +"end\n" +"tio.expect = function(pattern, timeout)\n" +" local str = ''\n" +" while true do\n" +" local c = tio.read(1, timeout)\n" +" if c then\n" +" str = str .. c\n" +" if string.match(str, pattern) then\n" +" return string.match(str, pattern)\n" +" end\n" +" else\n" +" return nil, str\n" +" end\n" +" end\n" +"end\n" +"tio.alwaysecho = true\n" +"setmetatable(tio, tio)\n"; + +static bool alwaysecho(lua_State *L) +{ + bool b; + + lua_getglobal(L, "tio"); + lua_getfield(L, -1, "alwaysecho"); + b = lua_toboolean(L, -1); + lua_pop(L, 2); + + return b; +} + +static int api_echo(lua_State *L) +{ + size_t len = 0; + const char *str = luaL_checklstring(L, 1, &len); + + if (option.timestamp) + { + char *pTimeStampNow = timestamp_current_time(); + if (pTimeStampNow) + { + tio_printf("%s", str); + if (option.log) + { + log_printf("\n[%s] %s", pTimeStampNow, str); + } + } + } else { + for (size_t i=0; i 0 && --attempts); + + if (len > 0) + return luaL_error(L, "partial write"); + + fsync(device_fd); // flush these characters now + tcdrain(device_fd); //ensure we flushed characters to our device + + lua_getglobal(L, "tio"); + + return 1; +} + +// lua: tio.read(size, timeout) +static int api_read(lua_State *L) +{ + int size = luaL_checkinteger(L, 1); + int timeout = lua_tointeger(L, 2); + + if (timeout == 0) + { + timeout = -1; // Wait forever + } + + luaL_Buffer buffer; + luaL_buffinit(L, &buffer); + +#if LUA_VERSION_NUM >= 502 + char *p = luaL_prepbuffsize(&buffer, size); +#else + if (size > LUAL_BUFFERSIZE) + return luaL_error(L, "buffer overflow, max size is: %d", LUAL_BUFFERSIZE); + char *p = luaL_prepbuffer(&buffer); +#endif + + ssize_t ret = read_poll(device_fd, p, size, timeout); + if (ret < 0) + return luaL_error(L, "%s", strerror(errno)); + + luaL_addsize(&buffer, ret); + luaL_pushresult(&buffer); + + if (ret == 0) + { + // On timeout return nil instead of an empty string + lua_pop(L, 1); + lua_pushnil(L); + } + else + { + maybe_echo(L); + } + + return 1; +} + +// lua: string = tio.readline(timeout) +static int api_readline(lua_State *L) { + int timeout = lua_tointeger(L, 1); //ms + luaL_Buffer b; + char ch; + + if (timeout == 0) + { + timeout = -1; // Wait forever + } + + luaL_buffinit(L, &b); + luaL_prepbuffer(&b); + while (true) { + int ret = read_poll(device_fd, &ch, 1, timeout); + + if (ret < 0) + return luaL_error(L, "%s", strerror(errno)); + + if (ret == 0) + { + luaL_pushresult(&b); + maybe_echo(L); + lua_pushnil(L); + lua_insert(L, -2); + return 2; + } + + if (ch == '\n') + { + luaL_pushresult(&b); + maybe_echo(L); + return 1; + } + + luaL_addchar(&b, ch); + } +} + +// lua: table = tio.ttysearch() +static int api_ttysearch(lua_State *L) +{ + UNUSED(L); + GList *iter; + int i = 1; + + GList *device_list = tty_search_for_serial_devices(); + + if (device_list == NULL) { return 0; } - ret = write(device_fd, string, strlen(string)); - if (ret < 0) + // Create a new table + lua_newtable(L); + + // Iterate through found devices + for (iter = device_list; iter != NULL; iter = g_list_next(iter)) { - tio_error_print("%s\n", strerror(errno)); + device_t *device = (device_t *) iter->data; + + // Create a new sub-table for each serial device + lua_newtable(L); + + // Add elements to the table + lua_pushstring(L, "path"); + lua_pushstring(L, device->path); + lua_settable(L, -3); + + lua_pushstring(L, "tid"); + lua_pushstring(L, device->tid); + lua_settable(L, -3); + + lua_pushstring(L, "uptime"); + lua_pushnumber(L, device->uptime); + lua_settable(L, -3); + + lua_pushstring(L, "driver"); + lua_pushstring(L, device->driver); + lua_settable(L, -3); + + lua_pushstring(L, "description"); + lua_pushstring(L, device->description); + lua_settable(L, -3); + + // Set the sub-table as a row in the main table + lua_rawseti(L, -2, i++); } - lua_pushnumber(L, ret); - + // Return table return 1; } -// Function to add a character to the circular buffer -void add_to_buffer(char c) -{ - if (buffer_size < MAX_BUFFER_SIZE) - { - circular_buffer[buffer_size++] = c; - } - else - { - // Shift the buffer to accommodate the new character - memmove(circular_buffer, circular_buffer + 1, MAX_BUFFER_SIZE - 1); - circular_buffer[MAX_BUFFER_SIZE - 1] = c; - } -} - -// Function to match against the circular buffer using regex -bool match_regex(regex_t *regex) -{ - char buffer[MAX_BUFFER_SIZE + 1]; // Temporary buffer for regex matching - memcpy(buffer, circular_buffer, buffer_size); - buffer[buffer_size] = '\0'; // Null-terminate the buffer - - // Match against the regex - int ret = regexec(regex, buffer, 0, NULL, 0); - if (!ret) - { - // Match found - return true; - } - else if (ret == REG_NOMATCH) - { - // No match found, do nothing - } - else - { - // Error occurred during matching - tio_error_print("Regex match failed"); - } - - return false; -} - -// lua: expect(string, timeout) -static int expect(lua_State *L) -{ - const char *string = lua_tostring(L, 1); - long timeout = lua_tointeger(L, 2); - regex_t regex; - int ret = 0; - char c; - - if ((string == NULL) || (timeout < 0)) - { - ret = -1; - goto error; - } - - if (timeout == 0) - { - // Let poll() wait forever - timeout = -1; - } - - // Compile the regular expression - ret = regcomp(®ex, string, REG_EXTENDED); - if (ret) - { - tio_error_print("Could not compile regex"); - ret = -1; - goto error; - } - - // Main loop to read and match - while (true) - { - ssize_t bytes_read = read_poll(device_fd, &c, 1, timeout); - if (bytes_read > 0) - { - putchar(c); - add_to_buffer(c); - // Match against the entire buffer - if (match_regex(®ex)) - { - break; - } - } - else - { - // Timeout or error - break; - } - } - - // Cleanup - regfree(®ex); - -error: - lua_pushnumber(L, ret); - return 1; -} - -// lua: exit(code) -static int exit_(lua_State *L) -{ - long code = lua_tointeger(L, 1); - - exit(code); - - return 0; -} - static void script_buffer_run(lua_State *L, const char *script_buffer) { int error; @@ -334,57 +420,11 @@ static void script_buffer_run(lua_State *L, const char *script_buffer) if (error) { tio_warning_printf("lua: %s\n", lua_tostring(L, -1)); - lua_pop(L, 1); /* pop error message from the stack */ + lua_pop(L, 1); /* Pop error message from the stack */ } } -static const struct luaL_Reg tio_lib[] = -{ - { "sleep", sleep_}, - { "msleep", msleep}, - { "high", high}, - { "low", low}, - { "toggle", toggle}, - { "config_high", config_high}, - { "config_low", config_low}, - { "config_apply", config_apply}, - { "modem_send", modem_send}, - { "send", send}, - { "expect", expect}, - { "exit", exit_}, - {NULL, NULL} -}; - -#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501 -/* -** Adapted from Lua 5.2.0 (for backwards compatibility) -*/ -static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) -{ - luaL_checkstack(L, nup+1, "too many upvalues"); - for (; l->name != NULL; l++) { /* fill the table with given functions */ - int i; - lua_pushstring(L, l->name); - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -(nup+1)); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ - lua_settable(L, -(nup + 3)); - } - lua_pop(L, nup); /* remove upvalues */ -} -#endif - -int lua_register_tio(lua_State *L) -{ - // Register lxi functions - lua_getglobal(L, "_G"); - luaL_setfuncs(L, tio_lib, 0); - lua_pop(L, 1); - - return 0; -} - -void script_file_run(lua_State *L, const char *filename) +static void script_file_run(lua_State *L, const char *filename) { if (strlen(filename) == 0) { @@ -394,32 +434,63 @@ void script_file_run(lua_State *L, const char *filename) if (luaL_dofile(L, filename)) { - tio_warning_printf("lua: %s\n", lua_tostring(L, -1)); + tio_warning_printf("lua: %s", lua_tostring(L, -1)); lua_pop(L, 1); /* pop error message from the stack */ return; } } -void script_set_global(lua_State *L, const char *name, long value) +static const struct luaL_Reg tio_lib[] = +{ + { "echo", api_echo}, + { "sleep", api_sleep}, + { "msleep", api_msleep}, + { "line_set", line_set}, + { "send", api_send}, + { "write", api_write}, + { "read", api_read}, + { "readline", api_readline}, + { "ttysearch", api_ttysearch}, + {NULL, NULL} +}; + +static void script_load(lua_State *L) +{ + int error; + + error = luaL_loadbuffer(L, script_init, strlen(script_init), "tio") || lua_pcall(L, 0, 0, 0); + if (error) + { + tio_error_print("%s\n", lua_tostring(L, -1)); + lua_pop(L, 1); // Pop error message from the stack + } +} + +static void script_set_global(lua_State *L, const char *name, long value) { lua_pushnumber(L, value); lua_setglobal(L, name); } -void script_set_globals(lua_State *L) +static void script_set_globals(lua_State *L) { - script_set_global(L, "DTR", TIOCM_DTR); - script_set_global(L, "RTS", TIOCM_RTS); - script_set_global(L, "CTS", TIOCM_CTS); - script_set_global(L, "DSR", TIOCM_DSR); - script_set_global(L, "CD", TIOCM_CD); - script_set_global(L, "RI", TIOCM_RI); + script_set_global(L, "toggle", 2); + script_set_global(L, "high", 1); + script_set_global(L, "low", 0); script_set_global(L, "XMODEM_CRC", XMODEM_CRC); script_set_global(L, "XMODEM_1K", XMODEM_1K); script_set_global(L, "YMODEM", YMODEM); } -void script_run(int fd) +#if LUA_VERSION_NUM >= 502 +static int luaopen_tio(lua_State *L) +{ + luaL_newlib(L, tio_lib); + return 1; +} +#endif + +void script_run(int fd, const char *script_filename) { lua_State *L; @@ -428,13 +499,25 @@ void script_run(int fd) L = luaL_newstate(); luaL_openlibs(L); - // Bind tio functions - lua_register_tio(L); +#if LUA_VERSION_NUM >= 502 + luaL_requiref(L, "tio", luaopen_tio, 1); +#else + luaL_register(L, "tio", tio_lib); +#endif + lua_pop(L, 1); + + // Load lua init script + script_load(L); // Initialize globals script_set_globals(L); - if (option.script_filename != NULL) + if (script_filename != NULL) + { + tio_printf("Running script %s", script_filename); + script_file_run(L, script_filename); + } + else if (option.script_filename != NULL) { tio_printf("Running script %s", option.script_filename); script_file_run(L, option.script_filename); @@ -447,3 +530,18 @@ void script_run(int fd) lua_close(L); } + +const char *script_run_state_to_string(script_run_t state) +{ + switch (state) + { + case SCRIPT_RUN_ONCE: + return "once"; + case SCRIPT_RUN_ALWAYS: + return "always"; + case SCRIPT_RUN_NEVER: + return "never"; + default: + return "Unknown"; + } +} diff --git a/src/script.h b/src/script.h index a2778e7..58ba1e1 100644 --- a/src/script.h +++ b/src/script.h @@ -21,12 +21,13 @@ #pragma once -enum script_run_t +typedef enum { SCRIPT_RUN_ONCE, SCRIPT_RUN_ALWAYS, SCRIPT_RUN_NEVER, SCRIPT_RUN_END, -}; +} script_run_t; -void script_run(int fd); +void script_run(int fd, const char *script_filename); +const char *script_run_state_to_string(script_run_t state); diff --git a/src/socket.c b/src/socket.c index 595efd9..e51f937 100644 --- a/src/socket.c +++ b/src/socket.c @@ -52,23 +52,23 @@ static const char *socket_filename(void) static int socket_inet_port(void) { /* skip 'inet:' */ - int port_number = atoi(option.socket + 5); - if (port_number == 0) + int port = atoi(option.socket + 5); + if (port == 0) { - port_number = SOCKET_PORT_DEFAULT; + port = SOCKET_PORT_DEFAULT; } - return port_number; + return port; } static int socket_inet6_port(void) { /* skip 'inet6:' */ - int port_number = atoi(option.socket + 6); - if (port_number == 0) + int port = atoi(option.socket + 6); + if (port == 0) { - port_number = SOCKET_PORT_DEFAULT; + port = SOCKET_PORT_DEFAULT; } - return port_number; + return port; } static void socket_exit(void) @@ -83,14 +83,14 @@ static bool socket_stale(const char *path) { struct sockaddr_un addr; bool stale = false; - int sockfd; + int sfd; /* Test if socket file exists */ if (access(path, F_OK) == 0) { /* Create test socket */ - sockfd = socket(AF_UNIX, SOCK_STREAM, 0); - if (sockfd < 0) + sfd = socket(AF_UNIX, SOCK_STREAM, 0); + if (sfd < 0) { tio_warning_printf("Failure opening socket (%s)", strerror(errno)); return false; @@ -124,6 +124,7 @@ void socket_configure(void) struct sockaddr_in6 sockaddr_inet6 = {}; struct sockaddr *sockaddr_p; socklen_t socklen; + int optval; /* Parse socket string */ @@ -225,6 +226,16 @@ void socket_configure(void) exit(EXIT_FAILURE); } +#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR | SO_NOSIGPIPE, &optval, sizeof(optval))) +#else + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))) +#endif + { + tio_error_printf("Failed to set socket options (%s)", strerror(errno)); + exit(EXIT_FAILURE); + } + /* Bind */ if (bind(sockfd, sockaddr_p, socklen) < 0) { @@ -263,7 +274,12 @@ void socket_write(char input_char) { if (clientfds[i] != -1) { - if (write(clientfds[i], &input_char, 1) <= 0) + +#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) + if (send(clientfds[i], &input_char, 1, 0) <= 0) +#else + if (send(clientfds[i], &input_char, 1, MSG_NOSIGNAL) <= 0) +#endif { tio_error_printf_silent("Failed to write to socket (%s)", strerror(errno)); close(clientfds[i]); @@ -343,20 +359,20 @@ bool socket_handle_input(fd_set *rdfs, char *output_char) } /* If INLCR is set, a received NL character shall be translated into a CR character */ - if (*output_char == '\n' && map_i_nl_cr) + if (*output_char == '\n' && option.map_i_nl_cr) { *output_char = '\r'; } else if (*output_char == '\r') { /* If IGNCR is set, a received CR character shall be ignored (not read). */ - if (map_ign_cr) + if (option.map_ign_cr) { return false; } /* If IGNCR is not set and ICRNL is set, a received CR character shall be translated into an NL character. */ - if (map_i_cr_nl) + if (option.map_i_cr_nl) { *output_char = '\n'; } diff --git a/src/timestamp.c b/src/timestamp.c index aae80c3..9273758 100644 --- a/src/timestamp.c +++ b/src/timestamp.c @@ -22,8 +22,6 @@ #include "config.h" #include #include -#include -#include #include #include #include "error.h" @@ -31,8 +29,6 @@ #include "options.h" #include "timestamp.h" -#define TIME_STRING_SIZE_MAX 24 - char *timestamp_current_time(void) { static char time_string[TIME_STRING_SIZE_MAX]; @@ -78,75 +74,31 @@ char *timestamp_current_time(void) tm = localtime(&tv.tv_sec); len = strftime(time_string, sizeof(time_string), "%Y-%m-%dT%H:%M:%S", tm); break; + case TIMESTAMP_EPOCH: + case TIMESTAMP_EPOCH_USEC: + // "N.sss" (seconds since Unix epoch, 1970-01-01 00:00:00Z) + tv = tv_now; + tm = localtime(&tv.tv_sec); + len = strftime(time_string, sizeof(time_string), "%s", tm); + break; default: return NULL; } - // Append milliseconds to all timestamps + // Append millis-/microseconds to all timestamps if (len) { - len = snprintf(time_string + len, TIME_STRING_SIZE_MAX - len, ".%03ld", (long)tv.tv_usec / 1000); + if ( option.timestamp == TIMESTAMP_EPOCH_USEC ) + { + len = snprintf(time_string + len, TIME_STRING_SIZE_MAX - len, ".%06ld", (long)tv.tv_usec); + } + else + { + len = snprintf(time_string + len, TIME_STRING_SIZE_MAX - len, ".%03ld", (long)tv.tv_usec / 1000); + } } - // Save previous time value for next run tv_previous = tv_now; return (len < TIME_STRING_SIZE_MAX) ? time_string : NULL; } - -const char* timestamp_state_to_string(enum timestamp_t timestamp) -{ - switch (timestamp) - { - case TIMESTAMP_NONE: - return "disabled"; - break; - - case TIMESTAMP_24HOUR: - return "24hour"; - break; - - case TIMESTAMP_24HOUR_START: - return "24hour-start"; - break; - - case TIMESTAMP_24HOUR_DELTA: - return "24hour-delta"; - break; - - case TIMESTAMP_ISO8601: - return "iso8601"; - break; - - default: - return "unknown"; - break; - } -} - -enum timestamp_t timestamp_option_parse(const char *arg) -{ - enum timestamp_t timestamp = TIMESTAMP_24HOUR; // Default - - if (arg != NULL) - { - if (strcmp(arg, "24hour") == 0) - { - return TIMESTAMP_24HOUR; - } - else if (strcmp(arg, "24hour-start") == 0) - { - return TIMESTAMP_24HOUR_START; - } - else if (strcmp(arg, "24hour-delta") == 0) - { - return TIMESTAMP_24HOUR_DELTA; - } - else if (strcmp(arg, "iso8601") == 0) - { - return TIMESTAMP_ISO8601; - } - } - - return timestamp; -} diff --git a/src/timestamp.h b/src/timestamp.h index 572be3c..0544544 100644 --- a/src/timestamp.h +++ b/src/timestamp.h @@ -21,16 +21,19 @@ #pragma once -enum timestamp_t +typedef enum { TIMESTAMP_NONE, TIMESTAMP_24HOUR, TIMESTAMP_24HOUR_START, TIMESTAMP_24HOUR_DELTA, TIMESTAMP_ISO8601, + TIMESTAMP_EPOCH, + TIMESTAMP_EPOCH_USEC, TIMESTAMP_END, -}; +} timestamp_t; + +#define TIME_STRING_SIZE_MAX 24 char *timestamp_current_time(void); -const char* timestamp_state_to_string(enum timestamp_t timestamp); -enum timestamp_t timestamp_option_parse(const char *arg); + diff --git a/src/tty.c b/src/tty.c index 84c5b91..efda859 100644 --- a/src/tty.c +++ b/src/tty.c @@ -19,9 +19,24 @@ * 02110-1301, USA. */ +#if defined(__linux__) +#include +#endif + +#if defined(__APPLE__) || defined(__MACH__) +#include +#include +#include +#include +#include +#endif + +#include "version.h" #include "config.h" +#include #include #include +#include #include #include #include @@ -41,6 +56,7 @@ #include #include #include +#include #include "config.h" #include "configfile.h" #include "tty.h" @@ -57,42 +73,43 @@ #include "misc.h" #include "script.h" #include "xymodem.h" +#include "fs.h" +#include "readline.h" /* tty device listing configuration */ #if defined(__linux__) -#define PATH_SERIAL_DEVICES "/dev/serial/by-id/" -#define PREFIX_TTY_DEVICES "" +#define PATH_SERIAL_DEVICES "/dev" +#define PATH_SERIAL_DEVICES_BY_ID "/dev/serial/by-id" +#define PATH_SERIAL_DEVICES_BY_PATH "/dev/serial/by-path" #elif defined(__FreeBSD__) -#define PATH_SERIAL_DEVICES "/dev/" -#define PREFIX_TTY_DEVICES "cua" +#define PATH_SERIAL_DEVICES "/dev" #elif defined(__APPLE__) -#define PATH_SERIAL_DEVICES "/dev/" -#define PREFIX_TTY_DEVICES "tty." +#define PATH_SERIAL_DEVICES "/dev" #elif defined(__CYGWIN__) -#define PATH_SERIAL_DEVICES "/dev/" -#define PREFIX_TTY_DEVICES "ttyS" +#define PATH_SERIAL_DEVICES "/dev" #elif defined(__HAIKU__) -#define PATH_SERIAL_DEVICES "/dev/ports/" -#define PREFIX_TTY_DEVICES "" +#define PATH_SERIAL_DEVICES "/dev/ports" #else -#define PATH_SERIAL_DEVICES "/dev/" -#define PREFIX_TTY_DEVICES "tty" +#define PATH_SERIAL_DEVICES "/dev" #endif #ifndef CMSPAR #define CMSPAR 010000000000 #endif -#define LINE_SIZE_MAX 1000 - #define KEY_0 0x30 #define KEY_1 0x31 #define KEY_2 0x32 #define KEY_3 0x33 #define KEY_4 0x34 #define KEY_5 0x35 +#define KEY_6 0x36 +#define KEY_7 0x37 +#define KEY_8 0x38 +#define KEY_9 0x39 #define KEY_QUESTION 0x3f +#define KEY_A 0x61 #define KEY_B 0x62 #define KEY_C 0x63 #define KEY_E 0x65 @@ -107,9 +124,9 @@ #define KEY_P 0x70 #define KEY_Q 0x71 #define KEY_R 0x72 +#define KEY_SHIFT_R 0x52 #define KEY_S 0x73 #define KEY_T 0x74 -#define KEY_U 0x55 #define KEY_V 0x76 #define KEY_X 0x78 #define KEY_Y 0x79 @@ -127,15 +144,9 @@ typedef enum SUBCOMMAND_LINE_TOGGLE, SUBCOMMAND_LINE_PULSE, SUBCOMMAND_XMODEM, + SUBCOMMAND_MAP, } sub_command_t; -typedef struct -{ - int mask; - bool value; - bool reserved; -} tty_line_config_t; - const char random_array[] = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x20, 0x28, 0x0A, 0x20, @@ -151,26 +162,17 @@ const char random_array[] = }; bool interactive_mode = true; -bool map_i_nl_cr = false; -bool map_i_cr_nl = false; -bool map_ign_cr = false; char key_hit = 0xff; +const char* device_name = NULL; +GList *device_list = NULL; static struct termios tio, tio_old, stdout_new, stdout_old, stdin_new, stdin_old; static unsigned long rx_total = 0, tx_total = 0; static bool connected = false; static bool standard_baudrate = true; -static void (*print)(char c); +static void (*printchar)(char c); static int device_fd; -static bool map_i_ff_escc = false; -static bool map_i_nl_crnl = false; -static bool map_o_cr_nl = false; -static bool map_o_nl_crnl = false; -static bool map_o_del_bs = false; -static bool map_o_ltu = false; -static bool map_o_nulbrk = false; -static bool map_o_msblsb = false; static char hex_chars[2]; static unsigned char hex_char_index = 0; static char tty_buffer[BUFSIZ*2]; @@ -179,8 +181,8 @@ static char *tty_buffer_write_ptr = tty_buffer; static pthread_t thread; static int pipefd[2]; static pthread_mutex_t mutex_input_ready = PTHREAD_MUTEX_INITIALIZER; -static char line[LINE_SIZE_MAX]; -static tty_line_config_t line_config[6] = { }; +static char line[PATH_MAX]; +static size_t listing_device_name_length_max = 0; static void optional_local_echo(char c) { @@ -189,14 +191,18 @@ static void optional_local_echo(char c) return; } - print(c); + printchar(c); + + if ((option.output_mode == OUTPUT_MODE_NORMAL) && (c == 127)) + { + // Force destructive backspace + printf("\b \b"); + } if (option.log) { log_putc(c); } - - print_tainted_set(); } inline static bool is_valid_hex(char c) @@ -252,7 +258,7 @@ ssize_t tty_write(int fd, const void *buffer, size_t count) ssize_t retval = 0, bytes_written = 0; size_t i; - if (map_o_ltu) + if (option.map_o_ltu) { // Convert lower case to upper case for (i = 0; i 0) { if (*p == 0x08 || *p == 0x7f) { - if (p > line ) + if (p > line) { write(STDOUT_FILENO, "\b \b", 3); p--; @@ -649,11 +617,11 @@ void tty_output_mode_set(output_mode_t mode) switch (mode) { case OUTPUT_MODE_NORMAL: - print = print_normal; + printchar = print_normal; break; case OUTPUT_MODE_HEX: - print = print_hex; + printchar = print_hex; break; case OUTPUT_MODE_END: @@ -661,6 +629,35 @@ void tty_output_mode_set(output_mode_t mode) } } +static void mappings_print(void) +{ + if (option.map_i_cr_nl || option.map_ign_cr || option.map_i_ff_escc || + option.map_i_nl_cr || option.map_i_nl_crnl || option.map_i_cr_crnl || + option.map_o_cr_nl || option.map_o_del_bs || option.map_o_nl_crnl || + option.map_o_ltu || option.map_o_nulbrk || option.map_i_msb2lsb || + option.map_o_ign_cr) + { + tio_printf(" Mappings:%s%s%s%s%s%s%s%s%s%s%s%s%s", + option.map_i_cr_nl ? " ICRNL" : "", + option.map_ign_cr ? " IGNCR" : "", + option.map_i_ff_escc ? " IFFESCC" : "", + option.map_i_nl_cr ? " INLCR" : "", + option.map_i_nl_crnl ? " INLCRNL" : "", + option.map_i_cr_crnl ? " ICRCRNL" : "", + option.map_i_msb2lsb ? " IMSB2LSB" : "", + option.map_o_cr_nl ? " OCRNL" : "", + option.map_o_del_bs ? " ODELBS" : "", + option.map_o_nl_crnl ? " ONLCRNL" : "", + option.map_o_ltu ? " OLTU" : "", + option.map_o_nulbrk ? " ONULBRK" : "", + option.map_o_ign_cr ? " OIGNCR" : ""); + } + else + { + tio_printf(" Mappings: none"); + } +} + void handle_command_sequence(char input_char, char *output_char, bool *forward) { char unused_char; @@ -714,7 +711,7 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) tty_line_poke(device_fd, TIOCM_RI, line_mode, option.ri_pulse_duration); break; default: - tio_warning_printf("Invalid line number"); + tio_error_print("Invalid line number"); break; } break; @@ -727,9 +724,12 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) tio_printf_raw("Enter file name: "); if (tio_readln()) { + int ret; + tio_printf("Sending file '%s' ", line); tio_printf("Press any key to abort transfer"); - tio_printf("%s", xymodem_send(device_fd, line, XMODEM_CRC) < 0 ? "Aborted" : "Done"); + ret = xymodem_send(device_fd, line, XMODEM_1K); + tio_printf("%s", ret < 0 ? "Aborted" : "Done"); } break; @@ -738,11 +738,96 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) tio_printf_raw("Enter file name: "); if (tio_readln()) { + int ret; + tio_printf("Sending file '%s' ", line); tio_printf("Press any key to abort transfer"); - tio_printf("%s", xymodem_send(device_fd, line, XMODEM_CRC) < 0 ? "Aborted" : "Done"); + ret = xymodem_send(device_fd, line, XMODEM_CRC); + tio_printf("%s", ret < 0 ? "Aborted" : "Done"); } break; + + case KEY_2: + tio_printf("Receive file with XMODEM-CRC"); + tio_printf_raw("Enter file name: "); + if (tio_readln()) + { + int ret; + + tio_printf("Ready to receiving file '%s' ", line); + tio_printf("Press any key to abort transfer"); + ret = xymodem_send(device_fd, line, XMODEM_CRC); + tio_printf("%s", ret < 0 ? "Aborted" : "Done"); + } + break; + + default: + tio_error_print("Invalid protocol option"); + break; + } + break; + + case SUBCOMMAND_MAP: + switch (input_char) + { + case KEY_0: + option.map_i_cr_nl = !option.map_i_cr_nl; + tty_reconfigure(); + tio_printf("ICRNL is %s", option.map_i_cr_nl ? "set" : "unset"); + break; + case KEY_1: + option.map_ign_cr = !option.map_ign_cr; + tty_reconfigure(); + tio_printf("IGNCR is %s", option.map_ign_cr ? "set" : "unset"); + break; + case KEY_2: + option.map_i_ff_escc = !option.map_i_ff_escc; + tio_printf("IFFESCC is %s", option.map_i_ff_escc ? "set" : "unset"); + break; + case KEY_3: + option.map_i_nl_cr = !option.map_i_nl_cr; + tty_reconfigure(); + tio_printf("INLCR is %s", option.map_i_nl_cr ? "set" : "unset"); + break; + case KEY_4: + option.map_i_nl_crnl = !option.map_i_nl_crnl; + tio_printf("INLCRNL is %s", option.map_i_nl_crnl ? "set" : "unset"); + break; + case KEY_5: + option.map_i_cr_crnl = !option.map_i_cr_crnl; + tio_printf("ICRCRNL is %s", option.map_i_cr_crnl ? "set" : "unset"); + break; + case KEY_6: + option.map_i_msb2lsb = !option.map_i_msb2lsb; + tio_printf("IMSB2LSB is %s", option.map_i_msb2lsb ? "set" : "unset"); + break; + case KEY_7: + option.map_o_cr_nl = !option.map_o_cr_nl; + tio_printf("OCRNL is %s", option.map_o_cr_nl ? "set" : "unset"); + break; + case KEY_8: + option.map_o_del_bs = !option.map_o_del_bs; + tio_printf("ODELBS is %s", option.map_o_del_bs ? "set" : "unset"); + break; + case KEY_9: + option.map_o_nl_crnl = !option.map_o_nl_crnl; + tio_printf("ONLCRNL is %s", option.map_o_nl_crnl ? "set" : "unset"); + break; + case KEY_A: + option.map_o_ltu = !option.map_o_ltu; + tio_printf("OLTU is %s", option.map_o_ltu ? "set" : "unset"); + break; + case KEY_B: + option.map_o_nulbrk = !option.map_o_nulbrk; + tio_printf("ONULBRK is %s", option.map_o_nulbrk ? "set" : "unset"); + break; + case KEY_C: + option.map_o_ign_cr = !option.map_o_ign_cr; + tio_printf("OIGNCR is %s", option.map_o_ign_cr ? "set" : "unset"); + break; + default: + tio_error_print("Invalid input"); + break; } break; } @@ -782,16 +867,16 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) tio_printf(" ctrl-%c i Toggle input mode", option.prefix_key); tio_printf(" ctrl-%c l Clear screen", option.prefix_key); tio_printf(" ctrl-%c L Show line states", option.prefix_key); - tio_printf(" ctrl-%c m Toggle MSB to LSB bit order", option.prefix_key); + tio_printf(" ctrl-%c m Change mapping of characters on input or output", option.prefix_key); tio_printf(" ctrl-%c o Toggle output mode", option.prefix_key); tio_printf(" ctrl-%c p Pulse serial port line", option.prefix_key); tio_printf(" ctrl-%c q Quit", option.prefix_key); tio_printf(" ctrl-%c r Run script", option.prefix_key); + tio_printf(" ctrl-%c R Execute shell command with I/O redirected to device", option.prefix_key); tio_printf(" ctrl-%c s Show statistics", option.prefix_key); tio_printf(" ctrl-%c t Toggle line timestamp mode", option.prefix_key); - tio_printf(" ctrl-%c U Toggle conversion to uppercase on output", option.prefix_key); tio_printf(" ctrl-%c v Show version", option.prefix_key); - tio_printf(" ctrl-%c x Send file via Xmodem", option.prefix_key); + tio_printf(" ctrl-%c x Send/Receive file via Xmodem", option.prefix_key); tio_printf(" ctrl-%c y Send file via Ymodem", option.prefix_key); tio_printf(" ctrl-%c ctrl-%c Send ctrl-%c character", option.prefix_key, option.prefix_key, option.prefix_key); break; @@ -862,12 +947,13 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) case KEY_C: tio_printf("Configuration:"); - options_print(); config_file_print(); + options_print(); if (option.rs485) { rs485_print_config(); } + mappings_print(); break; case KEY_E: @@ -884,12 +970,17 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) case INPUT_MODE_HEX: option.input_mode = INPUT_MODE_HEX; - tio_printf("Switched to hex input mode"); + tio_printf("Switched input mode to hex"); + break; + + case INPUT_MODE_LINE: + option.input_mode = INPUT_MODE_LINE; + tio_printf("Switched input mode to line"); break; case INPUT_MODE_END: option.input_mode = INPUT_MODE_NORMAL; - tio_printf("Switched to normal input mode"); + tio_printf("Switched input mode to normal"); break; } break; @@ -903,13 +994,13 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) case OUTPUT_MODE_HEX: tty_output_mode_set(OUTPUT_MODE_HEX); - tio_printf("Switched to hex output mode"); + tio_printf("Switched output mode to hex"); break; case OUTPUT_MODE_END: option.output_mode = OUTPUT_MODE_NORMAL; tty_output_mode_set(OUTPUT_MODE_NORMAL); - tio_printf("Switched to normal output mode"); + tio_printf("Switched output mode to normal"); break; } break; @@ -920,17 +1011,37 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) break; case KEY_M: - /* Toggle bit order */ - if (!map_o_msblsb) - { - map_o_msblsb = true; - tio_printf("Switched to reverse bit order"); - } - else - { - map_o_msblsb = false; - tio_printf("Switched to normal bit order"); - } + /* Change mapping of characters on input or output */ + tio_printf("Please enter which mapping to set or unset:"); + tio_printf(" (0) ICRNL: %s mapping CR to NL on input (unless IGNCR is set)", + option.map_i_cr_nl ? "Unset" : "Set"); + tio_printf(" (1) IGNCR: %s ignoring CR on input", + option.map_ign_cr ? "Unset" : "Set"); + tio_printf(" (2) IFFESCC: %s mapping FF to ESC-c on input", + option.map_i_ff_escc ? "Unset" : "Set"); + tio_printf(" (3) INLCR: %s mapping NL to CR on input", + option.map_i_nl_cr ? "Unset" : "Set"); + tio_printf(" (4) INLCRNL: %s mapping NL to CR-NL on input", + option.map_i_nl_crnl ? "Unset" : "Set"); + tio_printf(" (5) ICRCRNL: %s mapping CR to CR-NL on input", + option.map_i_cr_crnl ? "Unset" : "Set"); + tio_printf(" (6) IMSB2LSB: %s mapping MSB bit order to LSB on input", + option.map_i_msb2lsb ? "Unset" : "Set"); + tio_printf(" (7) OCRNL: %s mapping CR to NL on output", + option.map_o_cr_nl ? "Unset" : "Set"); + tio_printf(" (8) ODELBS: %s mapping DEL to BS on output", + option.map_o_del_bs ? "Unset" : "Set"); + tio_printf(" (9) ONLCRNL: %s mapping NL to CR-NL on output", + option.map_o_nl_crnl ? "Unset" : "Set"); + tio_printf(" (a) OLTU: %s mapping lowercase to uppercase on output", + option.map_o_ltu ? "Unset" : "Set"); + tio_printf(" (b) ONULBRK: %s mapping NUL to send break signal on output", + option.map_o_nulbrk ? "Unset" : "Set"); + tio_printf(" (c) OIGNCR: %s ignoring CR on output", + option.map_o_ign_cr ? "Unset" : "Set"); + + // Process next input character as sub command + sub_command = SUBCOMMAND_MAP; break; case KEY_Q: @@ -939,7 +1050,26 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) case KEY_R: /* Run script */ - script_run(device_fd); + tio_printf("Run Lua script") + tio_printf_raw("Enter file name: "); + if (tio_readln()) + { + clear_line(); + script_run(device_fd, line); + } + else + { + clear_line(); + script_run(device_fd, NULL); + } + break; + + case KEY_SHIFT_R: + /* Execute shell command */ + tio_printf("Execute shell command with I/O redirected to device"); + tio_printf_raw("Enter command: "); + if (tio_readln()) + execute_shell_command(device_fd, line); break; case KEY_S: @@ -956,36 +1086,39 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) case TIMESTAMP_NONE: break; case TIMESTAMP_24HOUR: - tio_printf("Switched to 24hour timestamp mode"); + tio_printf("Switched timestamp mode to 24hour"); break; case TIMESTAMP_24HOUR_START: - tio_printf("Switched to 24hour-start timestamp mode"); + tio_printf("Switched timestamp mode to 24hour-start"); break; case TIMESTAMP_24HOUR_DELTA: - tio_printf("Switched to 24hour-delta timestamp mode"); + tio_printf("Switched timestamp mode to 24hour-delta"); break; case TIMESTAMP_ISO8601: - tio_printf("Switched to iso8601 timestamp mode"); + tio_printf("Switched timestamp mode to iso8601"); + break; + case TIMESTAMP_EPOCH: + tio_printf("Switched timestamp mode to epoch"); + break; + case TIMESTAMP_EPOCH_USEC: + tio_printf("Switched timestamp mode to epoch with subdivision in microseconds"); break; case TIMESTAMP_END: option.timestamp = TIMESTAMP_NONE; - tio_printf("Switched timestamp off"); + tio_printf("Switched timestamp mode off"); break; } break; - case KEY_U: - map_o_ltu = !map_o_ltu; - break; - case KEY_V: - tio_printf("tio v%s", VERSION); + tio_printf("tio %s", VERSION); break; case KEY_X: tio_printf("Please enter which X modem protocol to use:"); - tio_printf(" (0) XMODEM-1K"); - tio_printf(" (1) XMODEM-CRC"); + tio_printf(" (0) XMODEM-1K send"); + tio_printf(" (1) XMODEM-CRC send"); + tio_printf(" (2) XMODEM-CRC receive"); // Process next input character as sub command sub_command = SUBCOMMAND_XMODEM; break; @@ -994,9 +1127,12 @@ void handle_command_sequence(char input_char, char *output_char, bool *forward) tio_printf("Send file with YMODEM"); tio_printf_raw("Enter file name: "); if (tio_readln()) { + int ret; + tio_printf("Sending file '%s' ", line); tio_printf("Press any key to abort transfer"); - tio_printf("%s", xymodem_send(device_fd, line, YMODEM) < 0 ? "Aborted" : "Done"); + ret = xymodem_send(device_fd, line, YMODEM); + tio_printf("%s", ret < 0 ? "Aborted" : "Done"); } break; @@ -1054,16 +1190,20 @@ void stdin_configure(void) void stdout_restore(void) { tcsetattr(STDOUT_FILENO, TCSANOW, &stdout_old); + + // If terminal is vt100 + if (option.vt100) + { + // Disable DEC Special Graphics character set just in case it was randomly + // enabled by noise from serial device. + putchar('\017'); + } } void stdout_configure(void) { int status; - /* Disable line buffering in stdout. This is necessary if we - * want things like local echo to work correctly. */ - setvbuf(stdout, NULL, _IONBF, 0); - /* Save current stdout settings */ if (tcgetattr(STDOUT_FILENO, &stdout_old) < 0) { @@ -1096,7 +1236,7 @@ void stdout_configure(void) } /* At start use normal print function */ - print = print_normal; + printchar = print_normal; /* Make sure we restore old stdout settings on exit */ atexit(&stdout_restore); @@ -1104,9 +1244,6 @@ void stdout_configure(void) void tty_configure(void) { - bool token_found = true; - char *token = NULL; - char *buffer; int status; speed_t baudrate; @@ -1179,25 +1316,26 @@ void tty_configure(void) } /* Set flow control */ - if (strcmp("hard", option.flow) == 0) + switch (option.flow) { - 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 - { - tio_error_printf("Invalid flow control"); - exit(EXIT_FAILURE); + case FLOW_NONE: + tio.c_cflag &= ~CRTSCTS; + tio.c_iflag &= ~(IXON | IXOFF | IXANY); + break; + + case FLOW_HARD: + tio.c_cflag |= CRTSCTS; + tio.c_iflag &= ~(IXON | IXOFF | IXANY); + break; + + case FLOW_SOFT: + tio.c_cflag &= ~CRTSCTS; + tio.c_iflag |= IXON | IXOFF; + break; + + default: + tio_error_printf("Invalid flow control"); + exit(EXIT_FAILURE); } /* Set stopbits */ @@ -1215,36 +1353,37 @@ void tty_configure(void) } /* Set parity */ - if (strcmp("odd", option.parity) == 0) + switch (option.parity) { - 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 if ( strcmp("mark", option.parity) == 0) - { - tio.c_cflag |= PARENB; - tio.c_cflag |= PARODD; - tio.c_cflag |= CMSPAR; - } - else if ( strcmp("space", option.parity) == 0) - { - tio.c_cflag |= PARENB; - tio.c_cflag &= ~PARODD; - tio.c_cflag |= CMSPAR; - } - else - { - tio_error_printf("Invalid parity"); - exit(EXIT_FAILURE); + case PARITY_NONE: + tio.c_cflag &= ~PARENB; + break; + + case PARITY_ODD: + tio.c_cflag |= PARENB; + tio.c_cflag |= PARODD; + break; + + case PARITY_EVEN: + tio.c_cflag |= PARENB; + tio.c_cflag &= ~PARODD; + break; + + case PARITY_MARK: + tio.c_cflag |= PARENB; + tio.c_cflag |= PARODD; + tio.c_cflag |= CMSPAR; + break; + + case PARITY_SPACE: + tio.c_cflag |= PARENB; + tio.c_cflag &= ~PARODD; + tio.c_cflag |= CMSPAR; + break; + + default: + tio_error_printf("Invalid parity"); + exit(EXIT_FAILURE); } /* Control, input, output, local modes for tty device */ @@ -1256,80 +1395,906 @@ void tty_configure(void) tio.c_cc[VTIME] = 0; // Inter-character timer unused tio.c_cc[VMIN] = 1; // Blocking read until 1 character received - /* Configure any specified input or output mappings */ - buffer = strdup(option.map); - while (token_found == true) + /* Configure input mappings */ + if (option.map_i_nl_cr) { - if (token == NULL) + tio.c_iflag |= INLCR; + } + if (option.map_ign_cr) + { + tio.c_iflag |= IGNCR; + } + if (option.map_i_cr_nl) + { + tio.c_iflag |= ICRNL; + } +} + +void tty_reconfigure(void) +{ + tty_configure(); + + if (connected) + { + /* Activate new port settings */ + tcsetattr(device_fd, TCSANOW, &tio); + } +} + +static bool is_serial_device(const char *format, ...) +{ + char filename[PATH_MAX]; + struct winsize ws; + int bytes_printed; + int status = true; + struct stat st; + va_list args; + int fd = -1; + + va_start(args, format); + bytes_printed = vsnprintf(filename, sizeof(filename), format, args); + va_end(args); + + if (bytes_printed < 0) + { + return false; + } + +#if defined(__APPLE__) + // Make sure device name is on the form /dev/cu.* or /dev/tty.* + if ((strncmp(filename, "/dev/cu.", 8) != 0) && (strncmp(filename, "/dev/tty.", 9) != 0)) + { + return false; + } +#endif + + fd = open(filename, O_RDONLY | O_NONBLOCK | O_NOCTTY); + if (fd == -1) + { + return false; + } + + if (fstat(fd, &st) == -1) + { + return false; + } + + // Make sure it is a character device + if ((st.st_mode & S_IFMT) != S_IFCHR) + { + return false; + } + + // Make sure it is a tty + status = isatty(fd); + if (status == 0) + { + goto error; + } + + // Serial devices do not have rows and columns + status = ioctl(fd, TIOCGWINSZ, &ws); + if (status == 0) + { + status = true; + if (ws.ws_row && ws.ws_col) { - token = strtok(buffer,","); + status = false; + goto error; } - else + } + +error: + close(fd); + return status; +} + +static void list_serial_devices_by_id(void) +{ +#ifdef PATH_SERIAL_DEVICES_BY_ID + DIR *d = opendir(PATH_SERIAL_DEVICES_BY_ID); + if (d) + { + struct dirent *dir; + + printf("By-id\n"); + printf("--------------------------------------------------------------------------------\n"); + + while ((dir = readdir(d)) != NULL) { - token = strtok(NULL, ","); + if ((strcmp(dir->d_name, ".")) && (strcmp(dir->d_name, ".."))) + { + if (is_serial_device("%s/%s", PATH_SERIAL_DEVICES_BY_ID, dir->d_name)) + { + printf("%s/%s\n", PATH_SERIAL_DEVICES_BY_ID, dir->d_name); + } + } + } + closedir(d); + } +#endif +} + +static void list_serial_devices_by_path(void) +{ +#ifdef PATH_SERIAL_DEVICES_BY_PATH + DIR *d = opendir(PATH_SERIAL_DEVICES_BY_PATH); + if (d) + { + struct dirent *dir; + + printf("\nBy-path\n"); + printf("--------------------------------------------------------------------------------\n"); + + while ((dir = readdir(d)) != NULL) + { + if ((strcmp(dir->d_name, ".")) && (strcmp(dir->d_name, ".."))) + { + if (is_serial_device("%s/%s", PATH_SERIAL_DEVICES_BY_PATH, dir->d_name)) + { + printf("%s/%s\n", PATH_SERIAL_DEVICES_BY_PATH, dir->d_name); + } + } + } + closedir(d); + } +#endif +} + +static gint compare_uptime(gconstpointer a, gconstpointer b) +{ + device_t *device_a = (device_t *) a; + device_t *device_b = (device_t *) b; + + // Make sure we end up with device with smallest uptime last in list + if (device_a->uptime > device_b->uptime) + return -1; + else if (device_a->uptime < device_b->uptime) + return 1; + else + return 0; +} + +#if defined(__linux__) + +// Function to get serial port type as a string +const char* get_serial_port_type(const char* port_name) +{ + int fd; + static struct serial_struct serial_info; + + // Open the serial port + fd = open(port_name, O_RDWR); + if (fd == -1) + { + return ""; + } + + // Get serial port information + if (ioctl(fd, TIOCGSERIAL, &serial_info) == -1) + { + close(fd); + return ""; + } + + // Close the serial port + close(fd); + + // Return the serial port type as a string + switch (serial_info.type) + { + case PORT_UNKNOWN: + return "Unknown"; + + case PORT_8250: + return "8250 UART"; + + case PORT_16450: + return "16450 UART"; + + case PORT_16550: + return "16550 UART"; + + case PORT_16550A: + return "16550A UART"; + + case PORT_16650: + return "16650 UART"; + + case PORT_16650V2: + return "16650V2 UART"; + + case PORT_16750: + return "16750 UART"; + + case PORT_STARTECH: + return "Startech UART"; + + case PORT_16850: + return "16850 UART"; + + case PORT_16C950: + return "16C950 UART"; + + case PORT_16654: + return "16654 UART"; + + case PORT_RSA: + return "RSA UART"; + + default: + return ""; + } +} + +#else + +const char* get_serial_port_type(const char* port_name) +{ + (void)port_name; + return ""; +} + +#endif + +static void search_reset(void) +{ + GList *iter; + + if (g_list_length(device_list) == 0) + { + return; + } + + // Free data of all list elements + for (iter = device_list; iter != NULL; iter = g_list_next(iter)) + { + device_t *device = (device_t *) iter->data; + g_free(device->tid); + g_free(device->path); + g_free(device->driver); + g_free(device->description); + } + + // Free all list elements + g_list_free_full(device_list, g_free); + + // Indicate an empty list + device_list = NULL; + + // Reset max device name length + listing_device_name_length_max = 0; +} + +#if defined(__linux__) + +GList *tty_search_for_serial_devices(void) +{ + DIR *dir; + char path[PATH_MAX] = {}; + char device_path[PATH_MAX] = {}; + char driver_path[PATH_MAX] = {}; + double current_time, creation_time; + ssize_t length; + + search_reset(); + + // Open the sysfs directory for the tty subsystem + dir = opendir("/sys/class/tty"); + if (!dir) + { + return NULL; + } + + current_time = get_current_time(); + + // Iterate through each device in the subsystem directory + struct dirent *entry; + while ((entry = readdir(dir)) != NULL) + { + // Skip . and .. entries + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + { + continue; } - if (token != NULL) + // Skip non serial devices + if (is_serial_device("/dev/%s", entry->d_name) == false) { - if (strcmp(token,"INLCR") == 0) + continue; + } + + // Construct the path to the device's device symlink + snprintf(path, sizeof(path), "/sys/class/tty/%s/device", entry->d_name); + + // Read the device symlink to get the device path + // Example symlinks: + // /sys/class/tty/ttyUSB0/device -> ../../../ttyUSB0 + // /sys/class/tty/ttyACM0/device -> ../../../3-6.4:1.2 + length = readlink(path, device_path, sizeof(device_path) - 1); + if (length == -1) + { + continue; + } + + // Null-terminate the string + device_path[length] = '\0'; + + // Extract last part of device path (string after last '/') + // Example resulting device_name: + // "ttyUSB0" + // "3-6.4:1.2" + char *last_part = strrchr(device_path, '/'); + last_part++; // Move past the '/' + + // Find that part in /sys/devices and return first result string + // Example devices_path: + // "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-6/3-6.3/3-6.3:1.0/ttyUSB0" + // "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-6/3-6.4/3-6.4:1.2" + char *devices_path = fs_search_directory("/sys/devices", last_part); + if (devices_path == NULL) + { + continue; + } + + // Remove last part if it contains device short name (e.g ttyUSB0) + // Example resulting devices_path: + // "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-6/3-6.3/3-6.3:1.0" + // "/sys/devices/pci0000:00/0000:00:14.0/usb3/3-6/3-6.4/3-6.4:1.2" + last_part = strrchr(devices_path, '/'); + last_part++; + if (strcmp(last_part, entry->d_name) == 0) + { + // Remove last part (string after last '/') + char *slash = strrchr(devices_path, '/'); + int index = (int) (slash - devices_path); + devices_path[index] = '\0'; + } + + // Hash remaining string to get unique topology ID + unsigned long hash = djb2_hash((const unsigned char *)devices_path); + char tid[5]; + base62_encode(hash, tid); + free(devices_path); + + // Construct the path to the device's driver symlink + snprintf(path, sizeof(path), "/sys/class/tty/%s/device/driver", entry->d_name); + + // Read the symlink to get the driver's path + length = readlink(path, driver_path, sizeof(driver_path) - 1); + if (length == -1) + { + continue; + } + + // Null-terminate the string + driver_path[length] = '\0'; + + // Extract the driver name from the path + char *driver = strrchr(driver_path, '/'); + if (driver == NULL) + { + continue; + } + driver++; // Move past the last '/' + + // Construct the path to the TTY device file + snprintf(path, sizeof(path), "/dev/%s", entry->d_name); + + // Calculate uptime + creation_time = fs_get_creation_time(path); + double uptime = current_time - creation_time; + + // Read sysfs files to get best possible description + char description[50] = {}; + length = fs_read_file_stripped(description, sizeof(description), "/sys/class/tty/%s/device/../product", entry->d_name); + if (length == -1) + { + length = fs_read_file_stripped(description, sizeof(description), "/sys/class/tty/%s/device/../../product", entry->d_name); + } + if (length == -1) + { + length = fs_read_file_stripped(description, sizeof(description), "/sys/class/tty/%s/device/interface", entry->d_name); + } + if (length == -1) + { + length = fs_read_file_stripped(description, sizeof(description), "/sys/class/tty/%s/device/../interface", entry->d_name); + } + if (length == -1) + { + snprintf(description, sizeof(description), "%s", get_serial_port_type(path)); + } + + // Do not add devices excluded by exclude patterns + if (match_patterns(path, option.exclude_devices)) + { + continue; + } + if (match_patterns(driver, option.exclude_drivers)) + { + continue; + } + if (match_patterns(tid, option.exclude_tids)) + { + continue; + } + + // Allocate new device item for device list + device_t *device = g_new0(device_t, 1); + if (device == NULL) + { + continue; + } + + // Fill in device information + device->path = g_strdup(path); + device->tid = g_strdup(tid); + device->uptime = uptime; + device->driver = g_strdup(driver); + device->description = g_strdup(description); + + // Add device information to device list + device_list = g_list_append(device_list, device); + + // Update length of longest device name string + if (strlen(device->path) > listing_device_name_length_max) + { + listing_device_name_length_max = strlen(device->path); + } + } + + if (g_list_length(device_list) == 0) + { + // Return NULL if no serial devices found + return NULL; + } + + // Sort device list device with respect to uptime + device_list = g_list_sort(device_list, compare_uptime); + + closedir(dir); + + return device_list; +} + +#elif defined(__APPLE__) || defined(__MACH__) + +char *getPropertyString(io_object_t device, CFStringRef property) +{ + /* Validate inputs */ + if (device == IO_OBJECT_NULL || property == NULL) + { + return NULL; + } + + /* Attempt to get property */ + CFTypeRef valueRef = IORegistryEntryCreateCFProperty( + device, property, kCFAllocatorDefault, 0); + if (!valueRef) + { + return NULL; + } + + /* Ensure it's a CFString */ + if (CFGetTypeID(valueRef) != CFStringGetTypeID()) + { + CFRelease(valueRef); + return NULL; + } + + /* Convert to C string */ + CFIndex length = CFStringGetLength(valueRef); + CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1; + char *result = malloc(maxSize); + + if (!result) + { + CFRelease(valueRef); + return NULL; + } + + bool converted = CFStringGetCString( + (CFStringRef)valueRef, + result, + maxSize, + kCFStringEncodingUTF8 + ); + + CFRelease(valueRef); + + if (!converted) + { + free(result); + return NULL; + } + + return result; +} + +char *getDeviceLocation(io_object_t device) +{ + /* Validate device */ + if (device == IO_OBJECT_NULL) + { + return strdup("Invalid Device"); + } + + /* Attempt to get location */ + io_string_t location = {0}; + kern_return_t result = IORegistryEntryGetLocationInPlane( + device, kIOServicePlane, location); + + if (result != KERN_SUCCESS) + { + return strdup("Unknown Location"); + } + + /* Safely copy location */ + size_t len = strnlen(location, sizeof(io_string_t)); + char *trimmed_location = calloc(1, len + 1); + + if (!trimmed_location) + { + return strdup("Memory Error"); + } + + memcpy(trimmed_location, location, len); + return trimmed_location; +} + +// for __APPLE__ +GList *tty_search_for_serial_devices(void) +{ + search_reset(); + io_iterator_t iter = IO_OBJECT_NULL; + CFMutableDictionaryRef matchingDict = NULL; + listing_device_name_length_max = 0; + + /* Create matching dictionary for serial devices */ + if (!(matchingDict = IOServiceMatching(kIOSerialBSDServiceValue))) + { + tio_error_print("Failed to create matching dictionary for serial devices"); + return NULL; + } + + /* Get matching services */ + kern_return_t kernResult = IOServiceGetMatchingServices( + kIOMainPortDefault, matchingDict, &iter); + matchingDict = NULL; /* Dictionary ownership transferred */ + + if (kernResult != KERN_SUCCESS) + { + tio_error_print("IOServiceGetMatchingServices failed: %d", kernResult); + return NULL; + } + + /* Defensive check for iterator */ + if (iter == IO_OBJECT_NULL) + { + tio_error_print("Invalid device iterator"); + return NULL; + } + + /* Iterate through serial devices and collect information */ + for (io_object_t device; (device = IOIteratorNext(iter));) + { + char *devicePath = NULL, *locationID = NULL; + char *productName = NULL, *vendorName = NULL; + char tid[5] = {0}; + double uptime = 0.0; + + /* Get device path - key determines if we get tty. or cu. */ + //if (!(devicePath = getPropertyString(device, CFSTR(kIODialinDeviceKey)))) + if (!(devicePath = getPropertyString(device, CFSTR(kIOCalloutDeviceKey)))) + { + IOObjectRelease(device); + continue; /* Skip devices without a path */ + } + + /* Update length of longest device name string */ + listing_device_name_length_max = + strlen(devicePath) > listing_device_name_length_max + ? strlen(devicePath) + : listing_device_name_length_max; + + /* Calculate uptime */ + uptime = get_current_time() - fs_get_creation_time(devicePath); + + /* Find USB device (if applicable) */ + io_object_t usbDevice = IO_OBJECT_NULL; + kern_return_t usbResult = IORegistryEntryGetParentEntry( + device, kIOServicePlane, &usbDevice); + + /* Traverse up the device tree to find a USB device */ + while (usbResult == KERN_SUCCESS && + !IOObjectConformsTo(usbDevice, "IOUSBDevice")) + { + io_object_t oldUsbDevice = usbDevice; + usbResult = IORegistryEntryGetParentEntry( + usbDevice, kIOServicePlane, &usbDevice); + IOObjectRelease(oldUsbDevice); + } + + /* If we found a USB device */ + if (usbResult == KERN_SUCCESS) + { + locationID = getDeviceLocation(usbDevice); + + unsigned long hash2 = djb2_hash((const unsigned char *)(locationID ?: "")); + base62_encode(hash2, tid); + + /* Get product and vendor names */ + productName = getPropertyString(usbDevice, CFSTR("USB Product Name")); + vendorName = getPropertyString(usbDevice, CFSTR("USB Vendor Name")); + + IOObjectRelease(usbDevice); + } + + /* Create device structure */ + device_t *device_info = g_new0(device_t, 1); + if (!device_info) + { + tio_error_print("Memory allocation failed for device_info"); + free(devicePath); + free(locationID); + free(productName); + free(vendorName); + IOObjectRelease(device); + continue; + } + + /* Populate device info */ + *device_info = (device_t) { + .path = devicePath, + .tid = g_strdup(tid), + .uptime = uptime, + .driver = g_strdup(vendorName), + .description = g_strdup(productName ?: vendorName ?: "") + }; + + /* Add to device list */ + device_list = g_list_append(device_list, device_info); + + /* Clean up */ + free(locationID); + free(productName); + free(vendorName); + IOObjectRelease(device); + } + + /* Clean up iterator */ + IOObjectRelease(iter); + + /* Check if device list is empty */ + if (!device_list) + { + return NULL; + } + + /* Sort device list by uptime */ + device_list = g_list_sort(device_list, compare_uptime); + + return device_list; +} + +#else + +GList *tty_search_for_serial_devices(void) +{ + DIR *dir; + char path[PATH_MAX] = {}; + double current_time, creation_time; + + search_reset(); + + // Open the directory containing serial devices + dir = opendir(PATH_SERIAL_DEVICES); + if (!dir) + { + return NULL; + } + + current_time = get_current_time(); + + // Iterate through each device in the subsystem directory + struct dirent *entry; + while ((entry = readdir(dir)) != NULL) + { + // Skip . and .. entries + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + { + continue; + } + + // Construct the path to the TTY device file + snprintf(path, sizeof(path), PATH_SERIAL_DEVICES "/%s", entry->d_name); + + // Skip non serial devices + if (is_serial_device(path) == false) + { + continue; + } + + // Calculate uptime + creation_time = fs_get_creation_time(path); + double uptime = current_time - creation_time; + + // Do not add devices excluded by exclude patterns + if (match_patterns(path, option.exclude_devices)) + { + continue; + } + + // Allocate new device item for device list + device_t *device = g_new0(device_t, 1); + if (device == NULL) + { + continue; + } + + // Fill in device information + device->path = g_strdup(path); + device->tid = g_strdup(""); + device->uptime = uptime; + device->driver = g_strdup(""); + device->description = g_strdup(""); + + // Add device information to device list + device_list = g_list_append(device_list, device); + + // Update length of longest device name string + if (strlen(device->path) > listing_device_name_length_max) + { + listing_device_name_length_max = strlen(device->path); + } + } + + if (g_list_length(device_list) == 0) + { + // Return NULL if no serial devices found + return NULL; + } + + // Sort device list device with respect to uptime + device_list = g_list_sort(device_list, compare_uptime); + + closedir(dir); + + return device_list; +} + +#endif + +void list_serial_devices(void) +{ + tty_search_for_serial_devices(); + + if (g_list_length(device_list) > 0) + { + if (listing_device_name_length_max < 17) + { + listing_device_name_length_max = 17; + } + print_padded("Device", listing_device_name_length_max, ' '); + printf(" TID Uptime [s] Driver Description\n"); + print_padded("", listing_device_name_length_max, '-'); + printf(" ---- ------------- ---------------- --------------------------\n"); + + // Iterate through the device list + GList *iter; + for (iter = device_list; iter != NULL; iter = g_list_next(iter)) + { + device_t *device = (device_t *) iter->data; + + // Print device information + print_padded(device->path, listing_device_name_length_max, ' '); + printf(" %4s %13.3f %-16s %s\n", device->tid, device->uptime, device->driver, device->description); + } + printf("\n"); + } + + list_serial_devices_by_id(); + list_serial_devices_by_path(); +} + +void tty_search(void) +{ + GList *iter; + device_t *device = NULL; + double uptime_minimum = 0; + bool no_new = true; + + switch (option.auto_connect) + { + case AUTO_CONNECT_NEW: + tty_search_for_serial_devices(); + + // Save smallest uptime + if (g_list_length(device_list) > 0) { - tio.c_iflag |= INLCR; - map_i_nl_cr = true; + // Get latest registered device (smallest uptime) + GList *last = g_list_last(device_list); + device = last->data; + uptime_minimum = device->uptime; } - else if (strcmp(token,"IGNCR") == 0) + + tio_printf("Waiting for tty device.."); + + while (no_new) { - tio.c_iflag |= IGNCR; - map_ign_cr = true; + tty_search_for_serial_devices(); + + // Iterate through the device list generated by search + for (iter = device_list; iter != NULL; iter = g_list_next(iter)) + { + device = (device_t *) iter->data; + + // Find first new device + if (device->uptime < uptime_minimum) + { + // Match found -> update device + device_name = device->path; + no_new = false; + break; + } + } + if (no_new) + { + usleep(500*1000); // Sleep 0.5 s + } } - else if (strcmp(token,"ICRNL") == 0) + return; + + case AUTO_CONNECT_LATEST: + tty_search_for_serial_devices(); + if (g_list_length(device_list) > 0) { - tio.c_iflag |= ICRNL; - map_i_cr_nl = true; + // Get latest registered device (smallest uptime) + GList *last = g_list_last(device_list); + device = last->data; + device_name = device->path; } - else if (strcmp(token,"OCRNL") == 0) + return; + + case AUTO_CONNECT_DIRECT: + if (config.device != NULL) { - map_o_cr_nl = true; - } - else if (strcmp(token,"ODELBS") == 0) - { - map_o_del_bs = true; - } - else if (strcmp(token,"IFFESCC") == 0) - { - map_i_ff_escc = true; - } - else if (strcmp(token,"INLCRNL") == 0) - { - map_i_nl_crnl = true; - } - else if (strcmp(token, "ONLCRNL") == 0) - { - map_o_nl_crnl = true; - } - else if (strcmp(token, "OLTU") == 0) - { - map_o_ltu = true; - } - else if (strcmp(token, "ONULBRK") == 0) - { - map_o_nulbrk = true; - } - else if (strcmp(token, "MSB2LSB") == 0) - { - map_o_msblsb = true; + // Prioritize any device result of the configuration file first + // Meaning a pattern or section/group have been matched the cmdline target. + device_name = config.device; } else { - printf("Error: Unknown mapping flag %s\n", token); - exit(EXIT_FAILURE); + // Fallback to use the target direcly + device_name = option.target; } - } - else - { - token_found = false; - } + + if (strlen(device_name) == TOPOLOGY_ID_SIZE) + { + // Potential topology ID detected -> trigger device search + tty_search_for_serial_devices(); + + // Iterate through the device list generated by search + for (iter = device_list; iter != NULL; iter = g_list_next(iter)) + { + device = (device_t *) iter->data; + + if (strcmp(device->tid, device_name) == 0) + { + // Topology ID match found -> use corresponding device name + device_name = device->path; + + return; + } + } + } + break; + + default: + // Should never be reached + tio_printf("Unknown connection strategy"); + exit(EXIT_FAILURE); } - free(buffer); } void tty_wait_for_device(void) @@ -1345,6 +2310,8 @@ void tty_wait_for_device(void) /* Loop until device pops up */ while (true) { + tty_search(); + if (interactive_mode) { /* In interactive mode, while waiting for tty device, we need to @@ -1390,13 +2357,26 @@ void tty_wait_for_device(void) } else if (status == -1) { +#if defined(__CYGWIN__) + // Happens when port unpluged + if (errno == EACCES) + { + goto error; + } +#elif defined(__APPLE__) + if (errno == EBADF) + { + break; // tty_disconnect() will be naturally triggered by atexit() + } +#else tio_error_printf("select() failed (%s)", strerror(errno)); exit(EXIT_FAILURE); +#endif } } /* Test for accessible device file */ - status = access(option.tty_device, R_OK); + status = access(device_name, R_OK); if (status == 0) { last_errno = 0; @@ -1404,7 +2384,7 @@ void tty_wait_for_device(void) } else if (last_errno != errno) { - tio_warning_printf("Could not open tty device (%s)", strerror(errno)); + tio_warning_printf("Could not open %s (%s)", device_name, strerror(errno)); tio_printf("Waiting for tty device.."); last_errno = errno; } @@ -1454,17 +2434,21 @@ void forward_to_tty(int fd, char output_char) int status; /* Map output character */ - if ((output_char == 127) && (map_o_del_bs)) + if ((output_char == 127) && (option.map_o_del_bs)) { output_char = '\b'; } - if ((output_char == '\r') && (map_o_cr_nl)) + if ((output_char == '\r') && (option.map_o_cr_nl)) { output_char = '\n'; } + if ((output_char == '\r') && (option.map_o_ign_cr)) + { + return; + } /* Map newline character */ - if ((output_char == '\n' || output_char == '\r') && (map_o_nl_crnl)) + if ((output_char == '\n' || output_char == '\r') && (option.map_o_nl_crnl)) { const char *crlf = "\r\n"; @@ -1490,8 +2474,12 @@ void forward_to_tty(int fd, char output_char) else { /* Send output to tty device */ - optional_local_echo(output_char); - if ((output_char == 0) && (map_o_nulbrk)) + if (option.input_mode != INPUT_MODE_LINE) + { + optional_local_echo(output_char); + } + + if ((output_char == 0) && (option.map_o_nulbrk)) { status = tcsendbreak(fd, 0); } @@ -1499,6 +2487,7 @@ void forward_to_tty(int fd, char output_char) { status = tty_write(fd, &output_char, 1); } + if (status < 0) { tio_warning_printf("Could not write to tty device"); @@ -1514,9 +2503,18 @@ void forward_to_tty(int fd, char output_char) { handle_hex_prompt(output_char); } - else + else if (option.input_mode == INPUT_MODE_NORMAL) { - optional_local_echo(output_char); + status = tty_write(device_fd, &output_char, 1); + if (status < 0) + { + tio_warning_printf("Could not write to tty device"); + } + else + { + optional_local_echo(output_char); + tx_total++; + } } break; @@ -1531,14 +2529,15 @@ int tty_connect(void) fd_set rdfs; /* Read file descriptor set */ int maxfd; /* Maximum file descriptor used */ char input_char, output_char; - char input_buffer[BUFSIZ]; + char input_buffer[BUFSIZ] = {}; static bool first = true; int status; - bool next_timestamp = false; + bool do_timestamp = false; char* now = NULL; + struct timeval tval_before = {}, tval_now, tval_result; /* Open tty device */ - device_fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK); + device_fd = open(device_name, O_RDWR | O_NOCTTY | O_NONBLOCK); if (device_fd < 0) { tio_error_printf_silent("Could not open tty device (%s)", strerror(errno)); @@ -1564,7 +2563,7 @@ int tty_connect(void) tcflush(device_fd, TCIOFLUSH); /* Print connect status */ - tio_printf("Connected"); + tio_printf("Connected to %s", device_name); connected = true; print_tainted = false; @@ -1573,7 +2572,7 @@ int tty_connect(void) if (option.timestamp) { - next_timestamp = true; + do_timestamp = true; } /* Manage print output mode */ @@ -1658,7 +2657,7 @@ int tty_connect(void) /* Manage script activation */ if (option.script_run != SCRIPT_RUN_NEVER) { - script_run(device_fd); + script_run(device_fd, NULL); if (option.script_run == SCRIPT_RUN_ONCE) { @@ -1672,6 +2671,15 @@ int tty_connect(void) exit(EXIT_SUCCESS); } + if (option.exec != NULL) + { + status = execute_shell_command(device_fd, option.exec); + exit(status); + } + + // Initialize readline like history + readline_init(); + /* Input loop */ while (true) { @@ -1689,7 +2697,10 @@ int tty_connect(void) bool forward = false; if (FD_ISSET(device_fd, &rdfs)) { + /*******************************/ /* Input from tty device ready */ + /*******************************/ + ssize_t bytes_read = read(device_fd, input_buffer, BUFSIZ); if (bytes_read <= 0) { @@ -1701,28 +2712,116 @@ int tty_connect(void) /* Update receive statistics */ rx_total += bytes_read; + // Manage timeout based timestamping in hex mode + if ((option.output_mode == OUTPUT_MODE_HEX) && (option.hex_n_value == 0)) + { + if (option.timestamp != TIMESTAMP_NONE) + { + gettimeofday(&tval_now, NULL); + timersub(&tval_now, &tval_before, &tval_result); + if ((tval_result.tv_sec * 1000 + tval_result.tv_usec / 1000) > option.timestamp_timeout) + { + now = timestamp_current_time(); + if (now) + { + ansi_printf_raw("\r\n[%s] ", now); + if (option.log) + { + log_printf("\r\n[%s] ", now); + } + do_timestamp = false; + } + } + tval_before = tval_now; + } + } + /* Process input byte by byte */ for (int i=0; i 0) + { + static bool first_ = true; + if ((count % option.hex_n_value) == 0) + { + if (option.timestamp != TIMESTAMP_NONE) + { + now = timestamp_current_time(); + if (first_) + { + ansi_printf_raw("[%s] ", now); + if (option.log) + { + log_printf("[%s] ", now); + } + first_ = false; + } + else + { + ansi_printf_raw("\r\n[%s] ", now); + if (option.log) + { + log_printf("\n[%s] ", now); + } + } + } + else + { + if (first_) + { + // Do nothing + first_ = false; + } + else + { + putchar('\r'); + putchar('\n'); + + if (option.log) + { + log_putc('\n'); + } + } + } + } + } + count++; + break; + + default: + tio_error_printf("Unknown output mode"); + exit(EXIT_FAILURE); + break; } /* Convert MSB to LSB bit order */ - if (map_o_msblsb) + if (option.map_i_msb2lsb) { char ch = input_char; input_char = 0; @@ -1733,24 +2832,33 @@ int tty_connect(void) } /* Map input character */ - if ((input_char == '\n') && (map_i_nl_crnl) && (!map_o_msblsb)) + if ((input_char == '\n') && (option.map_i_nl_crnl) && (!option.map_i_msb2lsb)) { - print('\r'); - print('\n'); + printchar('\r'); + printchar('\n'); if (option.timestamp) { - next_timestamp = true; + do_timestamp = true; } } - else if ((input_char == '\f') && (map_i_ff_escc) && (!map_o_msblsb)) + else if ((input_char == '\r') && (option.map_i_cr_crnl) && (!option.map_i_msb2lsb)) { - print('\e'); - print('c'); + printchar('\r'); + printchar('\n'); + if (option.timestamp) + { + do_timestamp = true; + } + } + else if ((input_char == '\f') && (option.map_i_ff_escc) && (!option.map_i_msb2lsb)) + { + printchar('\e'); + printchar('c'); } else { /* Print received tty character to stdout */ - print(input_char); + printchar(input_char); } /* Write to log */ @@ -1765,13 +2873,16 @@ int tty_connect(void) if (input_char == '\n' && option.timestamp) { - next_timestamp = true; + do_timestamp = true; } } } else if (FD_ISSET(pipefd[0], &rdfs)) { + /**************************/ /* Input from stdin ready */ + /**************************/ + ssize_t bytes_read = read(pipefd[0], input_buffer, BUFSIZ); if (bytes_read < 0) { @@ -1805,12 +2916,37 @@ int tty_connect(void) /* Handle commands */ handle_command_sequence(input_char, &output_char, &forward); - if ((option.input_mode == INPUT_MODE_HEX) && (forward)) + if (forward) { - if (!is_valid_hex(input_char)) + switch (option.input_mode) { - tio_warning_printf("Invalid hex character: '%d' (0x%02x)", input_char, input_char); - forward = false; + case INPUT_MODE_HEX: + if (!is_valid_hex(input_char)) + { + tio_warning_printf("Invalid hex character: '%d' (0x%02x)", input_char, input_char); + forward = false; + } + break; + + case INPUT_MODE_LINE: + if (input_char == '\r') + { + // Carriage return + readline_input(input_char); + + // Write current line to tty device + char *rl_line = readline_get(); + tty_write(device_fd, rl_line, strlen(rl_line)); + } + else + { + readline_input(input_char); + forward = false; + } + break; + + default: + break; } } } @@ -1825,7 +2961,10 @@ int tty_connect(void) } else { + /***************************/ /* Input from socket ready */ + /***************************/ + forward = socket_handle_input(&rdfs, &output_char); if (forward) @@ -1871,23 +3010,3 @@ error_read: error_open: return TIO_ERROR; } - -void list_serial_devices(void) -{ - DIR *d = opendir(PATH_SERIAL_DEVICES); - if (d) - { - struct dirent *dir; - while ((dir = readdir(d)) != NULL) - { - if ((strcmp(dir->d_name, ".")) && (strcmp(dir->d_name, ".."))) - { - if (!strncmp(dir->d_name, PREFIX_TTY_DEVICES, sizeof(PREFIX_TTY_DEVICES) - 1)) - { - printf("%s%s\n", PATH_SERIAL_DEVICES, dir->d_name); - } - } - } - closedir(d); - } -} diff --git a/src/tty.h b/src/tty.h index 633e51c..39c64af 100644 --- a/src/tty.h +++ b/src/tty.h @@ -22,24 +22,65 @@ #pragma once #include +#include #define LINE_HIGH true #define LINE_LOW false +#define TOPOLOGY_ID_SIZE 4 + +typedef enum +{ + FLOW_NONE, + FLOW_HARD, + FLOW_SOFT, +} flow_t; + +typedef enum +{ + PARITY_NONE, + PARITY_ODD, + PARITY_EVEN, + PARITY_MARK, + PARITY_SPACE, +} parity_t; + +typedef enum +{ + AUTO_CONNECT_DIRECT, + AUTO_CONNECT_NEW, + AUTO_CONNECT_LATEST, + AUTO_CONNECT_END, +} auto_connect_t; + +typedef struct +{ + char *tid; + double uptime; + char *path; + char *driver; + char *description; +} device_t; + +typedef struct +{ + int mask; + int value; + bool reserved; +} tty_line_config_t; + +extern const char *device_name; extern bool interactive_mode; -extern bool map_i_nl_cr; -extern bool map_i_cr_nl; -extern bool map_ign_cr; void stdout_configure(void); void stdin_configure(void); void tty_configure(void); +void tty_reconfigure(void); int tty_connect(void); void tty_wait_for_device(void); void list_serial_devices(void); void tty_input_thread_create(void); void tty_input_thread_wait_ready(void); -void tty_line_set(int fd, int mask, bool value); -void tty_line_toggle(int fd, int mask); -void tty_line_config(int mask, bool value); -void tty_line_config_apply(void); +void tty_line_set(int fd, tty_line_config_t line_config[]); +void tty_search(void); +GList *tty_search_for_serial_devices(void); diff --git a/src/version.h.in b/src/version.h.in new file mode 100644 index 0000000..459952d --- /dev/null +++ b/src/version.h.in @@ -0,0 +1,3 @@ +#pragma once + +#define VERSION "@VERSION@" diff --git a/src/xymodem.c b/src/xymodem.c index 83ac6dc..6bc3da3 100644 --- a/src/xymodem.c +++ b/src/xymodem.c @@ -7,30 +7,36 @@ * */ -#include -#include -#include -#include +#include #include #include -#include -#include +#include #include #include -#include -#include +#include #include "xymodem.h" #include "print.h" +#include "misc.h" #define SOH 0x01 #define STX 0x02 #define ACK 0x06 #define NAK 0x15 #define CAN 0x18 -#define EOT "\004" +#define EOT 0x04 + +#define SOH_STR "\001" +#define ACK_STR "\006" +#define NAK_STR "\025" +#define CAN_STR "\030" +#define EOT_STR "\004" #define OK 0 #define ERR (-1) +#define ERR_FATAL (-2) +#define USER_CAN (-5) + +#define RX_IGNORE 5 #define min(a, b) ((a) < (b) ? (a) : (b)) @@ -166,7 +172,7 @@ static int xmodem_1k(int sio, const void *data, size_t len, int seq) while (seq) { if (key_hit) return ERR; - if (write(sio, EOT, 1) < 0) { + if (write(sio, EOT_STR, 1) < 0) { tio_error_print("Write EOT to serial failed"); return ERR; } @@ -285,7 +291,7 @@ static int xmodem(int sio, const void *data, size_t len) while (1) { if (key_hit) return ERR; - if (write(sio, EOT, 1) < 0) { + if (write(sio, EOT_STR, 1) < 0) { tio_error_print("Write EOT to serial failed"); return ERR; } @@ -306,6 +312,345 @@ static int xmodem(int sio, const void *data, size_t len) return 0; /* not reached */ } +int start_receive(int sio) +{ + int rc; + struct pollfd fds; + fds.events = POLLIN; + fds.fd = sio; + for (int n = 0; n < 20; n++) + { + /* Send the 'C' byte until the sender of the file responds with + something. The start character will be sent once a second for a number of + seconds. If nothing is received in that time then return false to indicate + that the transfer did not start. */ + rc = write(sio, "C", 1); + if (rc < 0) { + if (errno == EWOULDBLOCK) { + usleep(1000); + continue; + } + tio_error_print("Write packet to serial failed"); + return ERR; + } + /* Wait until data is available */ + rc = poll(&fds, 1, 3000); + if (rc < 0) + { + tio_error_print("%s", strerror(errno)); + return rc; + } + else if (rc > 0) + { + if (fds.revents & POLLIN) + { + return rc; + } + } + if (key_hit) + return USER_CAN; + } + return rc; +} + +uint16_t update_CRC(uint16_t crc, char data_char) +{ + uint8_t data = data_char; + crc = crc ^ ((uint16_t)data << 8); + for (int ix = 0; (ix < 8); ix++) + { + if (crc & 0x8000) + { + crc = (crc << 1) ^ 0x1021; + } + else + { + crc <<= 1; + } + } + return crc; +} + +int receive_packet(int sio, struct xpacket packet, int fd) +{ + char rxSeq1, rxSeq2 = 0; + char resp = 0; + uint16_t calcCrc = 0; + uint16_t rxCrc = 0; + int rc; + + struct pollfd fds; + fds.events = POLLIN; + fds.fd = sio; + + /* Read seq bytes*/ + rc = read_poll(sio, &rxSeq1, 1, 3000); + if (rc == 0) { + tio_error_print("Timeout waiting for first seq byte"); + return ERR; + } else if (rc < 0) { + tio_error_print("Error reading first seq byte") + return ERR_FATAL; + } + rc = read_poll(sio, &rxSeq2, 1, 3000); + if (rc == 0) { + tio_error_print("Timeout waiting for second seq byte"); + return ERR; + } else if (rc < 0) { + tio_error_print("Error reading second seq byte") + return ERR_FATAL; + } + if (key_hit) + return USER_CAN; + + /* Read packet Data */ + for (unsigned ix = 0; (ix < sizeof(packet.data)); ix++) + { + rc = read_poll(sio, &resp, 1, 3000); + /* If the read times out or fails then fail this packet. */ + if (rc == 0) + { + tio_error_print("Timeout waiting for next packet char"); + rc = write(sio, CAN_STR, 1); + if (rc < 0) { + tio_error_print("Write cancel packet to serial failed"); + return ERR_FATAL; + } + return ERR; + } else if (rc < 0) { + tio_error_print("Error reading next packet char") + rc = write(sio, CAN_STR, 1); + if (rc < 0) { + tio_error_print("Write cancel packet to serial failed"); + } + return ERR_FATAL; + } + packet.data[ix] = (uint8_t) resp; + calcCrc = update_CRC(calcCrc, resp); + if (key_hit) + return USER_CAN; + } + + /* Read CRC */ + rc = read_poll(sio, &resp, 1, 3000); + if (rc == 0) { + tio_error_print("Timeout waiting for first CRC byte"); + return ERR; + } else if (rc < 0) { + tio_error_print("Error reading first CRC byte") + return ERR_FATAL; + } + + uint8_t uresp = resp; + uint16_t uresp16 = uresp; + rxCrc = uresp16 << 8; + + rc = read_poll(sio, &resp, 1, 3000); + if (rc == 0) { + tio_error_print("Timeout waiting for second CRC byte"); + return ERR; + } else if (rc < 0) { + tio_error_print("Error reading second CRC byte") + return ERR_FATAL; + } + + uresp = resp; + uresp16 = uresp; + rxCrc |= uresp16; + + if (key_hit) + return USER_CAN; + + /* At this point in the code, there should not be anything in the receive buffer + because the sender has just sent a complete packet and is waiting on a response. */ + rc = poll(&fds, 1, 10); + if (rc < 0) + { + tio_error_print("%s", strerror(errno)); + tio_error_print("Poll check error after packet finish"); + rc = write(sio, CAN_STR, 1); + if (rc < 0) { + tio_error_print("Write cancel packet to serial failed"); + } + return ERR_FATAL; + } + else if (rc > 0) + { + if (fds.revents & POLLIN) + { + tio_error_print("RX sync error"); + char dummy = 0; + /* Drain buffer */ + while (read_poll(sio, &dummy, 1, 100) > 0) {} + return ERR; + } + } + + uint8_t tester = 0xff; + uint8_t seq1 = rxSeq1; + uint8_t seq2 = rxSeq2; + + if ((calcCrc == rxCrc) && (seq1 == packet.seq - 1) && ((seq1 ^ seq2) == tester)) + { + /* Resend of previously processed packet. */ + rc = write(sio, ACK_STR, 1); + if (rc < 0) { + tio_error_print("Write acknowlegdement packet to serial failed"); + return ERR_FATAL; + } + return RX_IGNORE; + } + else if ((calcCrc != rxCrc) || (seq1 != packet.seq) || ((seq1 ^ seq2) != tester)) + { + /* Fail if the CRC or sequence number is not correct or if the two received + sequence numbers are not the complement of one another. */ + tio_error_print("Bad CRC or sequence number"); + tio_debug_printf("CRC read: %u", rxCrc); + tio_debug_printf("CRC calculated: %u", calcCrc); + tio_debug_printf("Seq read: %hhu", rxSeq1); + tio_debug_printf("Seq should be: %hhu", packet.seq); + tio_debug_printf("inv seq: %hhu", rxSeq2); + return ERR; + } + else + { + /* The data is good. Process the packet then ACK it to the sender. */ + rc = write(fd, packet.data, sizeof(packet.data)); + if (rc < 0) + { + tio_error_print("Problem writing to file"); + rc = write(sio, CAN_STR, 1); + if (rc < 0) { + tio_error_print("Write cancel packet to serial failed"); + } + return ERR_FATAL; + } + rc = write(sio, ACK_STR, 1); + if (rc < 0) + { + tio_error_print("Write acknowlegdement packet to serial failed"); + return ERR_FATAL; + } + } + + return OK; +} + +int xmodem_receive(int sio, int fd) +{ + struct xpacket packet; + char resp = 0; + int rc; + bool complete = false; + char status; + + /* Drain pending characters from serial line.*/ + while(1) { + if (key_hit) + return -1; + rc = read_poll(sio, &resp, 1, 50); + if (rc == 0) { + if (resp == CAN) return ERR; + break; + } + else if (rc < 0) { + if (rc != USER_CAN) { + tio_error_print("Read sync from serial failed"); + } + return ERR; + } + } + + /* Always work with 128b packets */ + packet.seq = 1; + packet.type = SOH; + + /* Start Receive*/ + rc = start_receive(sio); + if (rc == 0) + { + tio_error_print("Timeout waiting for transfer to start"); + return ERR; + } else if (rc < 0) { + tio_error_print("Error starting XMODEM receive"); + return ERR; + } + + while (!complete) { + /* Poll for 1 new byte for 3 seconds */ + rc = read_poll(sio, &resp, 1, 3000); + if (rc == 0) { + tio_error_print("Timeout waiting for start of next packet"); + return ERR; + } else if (rc < 0) { + tio_error_print("Error reading start of next packet") + return ERR; + } + if (key_hit) + return USER_CAN; + + switch(resp) + { + case SOH: + /* Start of a packet */ + rc = receive_packet(sio, packet, fd); + if (rc == OK) { + packet.seq++; + status = '.'; + } else if (rc == ERR) { + rc = write(sio, NAK_STR, 1); + if (rc < 0) { + tio_error_print("Writing not acknowledge packet to serial failed"); + return ERR; + } + status = 'N'; + } else if (rc == ERR_FATAL) { + tio_error_print("Receive cancelled due to fatal error"); + return ERR; + } else if (rc == USER_CAN) { + rc = write(sio, CAN_STR, 1); + if (rc < 0) { + tio_error_print("Writing cancel to serial failed"); + return ERR; + } + return USER_CAN; + } else if (rc == RX_IGNORE) { + status = ':'; + } + break; + + case EOT: + /* End of Transfer */ + rc = write(sio, ACK_STR, 1); + if (rc < 0) + { + tio_error_print("Write acknowlegdement packet to serial failed"); + return ERR; + } + complete = true; + status = '\0'; + write(STDOUT_FILENO, "|\r\n", 3); + break; + + case CAN: + /* Cancel from sender */ + tio_error_print("Transmission cancelled from sender"); + return ERR; + break; + + default: + tio_error_print("Unexpected character received waiting for next packet"); + return ERR; + break; + } + + + /* Update "progress bar" */ + write(STDOUT_FILENO, &status, 1); + } + return OK; +} + int xymodem_send(int sio, const char *filename, modem_mode_t mode) { size_t len; @@ -343,7 +688,7 @@ int xymodem_send(int sio, const char *filename, modem_mode_t mode) rc = -1; if (strlen(filename) > 977) break; /* hdr block overrun */ - p = stpcpy(hdr, filename) + 1; + p = stpncpy(hdr, filename, 1024) + 1; p += sprintf(p, "%ld %lo %o", len, stat.st_mtime, stat.st_mode); if (xmodem_1k(sio, hdr, p - hdr, 0) < 0) break; /* hdr with metadata */ @@ -360,3 +705,35 @@ int xymodem_send(int sio, const char *filename, modem_mode_t mode) close(fd); return rc; } + +int xymodem_receive(int sio, const char *filename, modem_mode_t mode) +{ + int rc, fd; + + /* Create new file */ + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); + if (fd < 0) { + tio_error_print("Could not open file"); + return ERR; + } + + /* Do transfer */ + key_hit = 0; + if (mode == XMODEM_1K) { + tio_error_print("Not supported"); + rc = -1; + } + else if (mode == XMODEM_CRC) { + rc = xmodem_receive(sio, fd); + } + else { + tio_error_print("Not supported"); + rc = -1; + } + key_hit = 0xff; + + /* Flush serial and release resources */ + tcflush(sio, TCIOFLUSH); + close(fd); + return rc; +} diff --git a/src/xymodem.h b/src/xymodem.h index ef7f9a3..1b46cd7 100644 --- a/src/xymodem.h +++ b/src/xymodem.h @@ -30,3 +30,5 @@ typedef enum { extern char key_hit; int xymodem_send(int sio, const char *filename, modem_mode_t mode); + +int xymodem_receive(int sio, const char *filename, modem_mode_t mode); diff --git a/subprojects/libinih.wrap b/subprojects/libinih.wrap deleted file mode 100644 index fc5f690..0000000 --- a/subprojects/libinih.wrap +++ /dev/null @@ -1,4 +0,0 @@ -[wrap-git] -directory=libinih -url=https://github.com/benhoyt/inih.git -revision=r58