Commit graph

1312 commits

Author SHA1 Message Date
yabu76
035b661bb8 Fix duplicate line checking bug in readline processing 2026-04-25 10:26:13 +09:00
yabu76
e3b8724479 Add Lua API tio.inkey,input,set_keymap,receive and etc.
Add following functions to Lua API

tio.inkey()
tio.input()
tio.inputline()
tio.set_keymap()
tio.receive()
tio.subcmd_println()
tio.subcmd_warning_println()
tio.subcmd_error_println()
tio.subcmd_puts()
tio.subcmd_warning_puts()
tio.subcmd_error_puts()
2026-04-04 16:20:22 +09:00
yabu76
c8df45ceff Add tty_inkey(),tty_simple() and tty_subcmd_readln()
Add following functions to handle input from a thread that uses standard
input.

tty_inkey() waits for a key to be pressed for a specified amount of
time, and then returns the pressed string.

tty_simple_readln() reads user input until Enter is pressed.
It supports Backspace key.

tty_subcmd_readln() reads user input until Enter is pressed.
It supports line editing (cursor keys, Backspace) and command history.

The script REPL history buffer is now a dedicated history buffer instead
of the subcommand history buffer. REPL prompt changes from ">>" to "->".

Reduced the history buffer size from 1000 lines to 500 lines.
2026-04-04 16:17:14 +09:00
yabu76
9703c25503 Add a mecanism to prevent unnecessary tcsetattr on changing state
Add a mechanism of tty_tcsetattr to skip the mode change and omit the
configuration change when the value of the termios structure does not
change.

In the cygwin environment, tcsetattr() resets DTR/RTS to their default
values.
Furthermore, add a mechanism of tty_tcsetattr to revert to the state
before tcsetattr() was called.
However, this method will still cause DTR/RTS spikes.
2026-03-14 14:39:50 +09:00
yabu76
0d1eaef794 Add examples of transferring files using gkermit command to sthe man page. 2026-03-08 22:27:58 +09:00
yabu76
2316c0216a Add documentation and examples for new features. 2026-03-08 11:17:50 +09:00
yabu76
1b0ef08d95 Minor fixes to help output and key command list output 2026-03-08 09:52:59 +09:00
yabu76
dc7e21b666 Add Lua API tio.line_get and tio.get_state
tio.line_get()
  Get state of multiple tty modem lines.
  It is equivalent to the key command ctrl-t L.

  Return 6 values DTR, RTS, CTS, DSR, CD, RI.
  Each return value is high (==tio.C.LN_HIGH) or low (==tio.C.LN_LOW).

tio.get_state()
  Return the main state of tio as a integer.
  Return value is one of tio.C.SA_INTERACTIVE, tio.C.SA_STARTING,
    tio.C.SA_PIPED_INPUT, tio.C.SA_PIPED_INPUT,
    tio.C.SA_EXEC_SHELL_COMMAND, tio.C.SA_XYMODEM.

refactoring
  rename line_set() to api_line_set().
2026-03-08 09:43:43 +09:00
yabu76
a19bf0aa44 Add Lua API tio.twrite's documentation and example 2026-02-22 16:40:57 +09:00
yabu76
d5b4e7d881 Keep the line input mode prompt on the local screen after input
It is for visibility.
Note: This line input mode prompt is not recorded by tio's logging
function.
2026-02-16 23:34:09 +09:00
yabu76
4153a4571a Add guide message to ctrl-t R interactive shell command execution 2026-02-15 17:20:24 +09:00
yabu76
2069e77123 Add some subcommand-equivalent functions to the Lua API
Add following functions to Lua API

tio.send_break()
tio.set_local_echo()
tio.set_log()
tio.flush_data_io_buffer()
tio.set_input_mode()
tio.set_output_mode()
tio.set_raw_mode()
tio.set_raw_mode_interactive()
tio.set_timestamp_mode()
tio.set_exec_shell_command()
tio.get_version()
2026-02-13 07:36:55 +09:00
yabu76
d28b5f4452 Fix Ctrl-t J raw-mode action is not reflected to device immediately 2026-02-11 18:28:37 +09:00
yabu76
2496676199 Fix minor bug of user key-script mapping function 2026-02-11 08:40:12 +09:00
yabu76
60bc7e9cfe Add user key-script mapping function (--keymap, Ctrl-t k)
User key-script mapping function:

You can specify the mappings as @<key-1>=<script-description-1>
@<key-2>=<script-description-2>... @<key-N>=<script-description-N>.

Script-description is script-filename or '!'script-commands.

After that,
When you press ctrl-t and <key-n>, tio executes <script-description-n>.
This user keymap takes precedence over the default settings (except for
ctrl-t q).

Example of startup option:
tio /dev/ttyUSB1 --keymap '@1=!print(tio.banner())
@2=!tio.write("test\r") @ctrl-a=!tio.write("ctrl-a\r")
@ctrl-j=test-script.tio'

Example of .tioconfig: (note: backslash escape needed.)
keymap = @1=!print(tio.banner()) @2=!tio.write("test\\r")
@ctrl-a=!tio.write("ctrl-a\\r") @ctrl-j=test-script.tio
2026-02-08 16:17:30 +09:00
yabu76
8722b410a7 Add description of script-init-file option to help 2026-02-07 17:06:43 +09:00
yabu76
d2eea48f88 Merge branch 'luaapi_twrite' of https://github.com/yabu76/tio into luaapi_twrite 2026-02-03 23:17:24 +09:00
yabu76
2996e411fe Remove the interpreter-restart in the script/script-file option
The lua interpreter is started immediately after tio started, so this
was redundant.
2026-02-03 23:10:56 +09:00
yabu76
b7ccd28300 Add script-init-file option
The script-init-file is the file to be loaded and executed when the
interpreter starts.

If this option is defined, it will be executed after the tio module is
loaded as a built-in module.

Example of .tioconfig:
script-init-file = /home/user/.tio-script-init.lua

Example of option specification:
tio --script-init-file /home/user/.tio-script-init.lua
2026-02-03 22:42:45 +09:00
yabu76
43a94026fe
Merge branch 'tio:master' into luaapi_twrite 2026-02-01 18:02:21 +09:00
yabu76
c009ed755c Modified the Lua API timeout argument specification to be an extension of the v3.9 specification
Fix the v3.9-incompatible changes in 8e02cde ("Lua API Timeout
Specification Changes to Enable Non-Blocking Reads", November 15, 2025)
to make them compatible.

The timeout arguments for tio.expect/s(), tio.read(), and tio.readline()
have been changed as follows:
- The timeout is in milliseconds, and the default is 0, which means to
wait forever (as in v3.9).
- A negative value means to nowait.

A constant table (tio.C) has also been added,
defining tio.C.FOREVER to 0 and tio.C.NOWAIT to -1.
2026-02-01 11:56:29 +09:00
Jakob Haufe
6fb3a64ba2 Fix license in meson.build
- Make license here match LICENSE
- According to meson docs, it should not be an array
2026-01-22 12:41:01 +01:00
yabu76
0da8731c0b Fix tty_configure to change settings depending on the state of the raw option 2026-01-19 22:54:34 +09:00
yabu76
e1d1c6f2bc Fix incorrect condition for clearing input map and soft-flow in raw mode 2026-01-19 22:48:19 +09:00
yabu76
445a21b807 Add bidirectional redirection to shell command execution function (Ctrl-t R and --exec option)
The "shell command execution feature" previously only supported the
ability to transfer a command's stdout and stderr to a device.

To support bidirectional commands, a feature will be added to connect
input from a device to the command's stdin.

Since some communication commands (sz, rz) require stderr to be kept
local, a feature will also be added to not transfer stderr to a device
if the command string begins with a '?'.

On Linux, you can terminate a running command by pressing Ctrl-t R again
while the command is running (this uses the command /usr/bin/pkill
internally).
2026-01-18 15:39:12 +09:00
yabu76
321494b4e6 Add option --raw/--raw-interactive and Ctrl-t j/J subcommand
Non-interactive operations (input from a pipe, running a shell command,
XYMODEM transfers) often fail when mapping, soft flow control, or output
delay are enabled.

To reduce the hassle of switching settings, add the function shown in
the title.

The raw option can be set to one of the following:
- off ... no effects
- on ... soft-flow off, character mapping off, output delay enabled
- on-nodelay ... soft-flow off, character mapping off, output delay
disable

raw option is for Piped-input / Shell command execution / XYMODEM
transfering. default is on.
raw-interactive option is for socket-mode and normal terminal use.
default is off.

You can type Ctrl-t j if you need to change raw setting for
non-interactive case. it toggles the raw setting.

You can type Ctrl-t J if you need to change raw setting of interactive
case. it toggles the raw setting.
It is useful when transferring files in socket mode.
2026-01-17 11:41:10 +09:00
yabu76
3b7fe3d258 Reorganize forward_to_tty, tty_write, tty_raw_write functions
Add tty_raw_write function and reorganize roles of them.

tty_raw_write:
Performs any processing that must be done character-by-character on the
fly. That is, output delays and ONULBRK processing.

tty_write :
Converts input data using output mapping and passes it to tty_raw_write.

forward_to_tty :
Converts input data according to the input/output mode and passes it to
tty_write. (output-mapping function move to tty_write. tx_total
statistics counting move to tty_raw_write and tty_sync.)
2026-01-02 00:35:52 +09:00
yabu76
6425ffe66a Fix Ctrl-t c to display the yes/no of the function instead of true/false of the setting
When options.no_reconnect is true, it displays "Automatic reconnect: no"
2025-12-31 15:44:42 +09:00
yabu76
c74273ecaa Add option --no-tty-restore / -N that omits tty_restore() call #334
On exit, do not reset the tty device settings to the state they were in
before the program started.
This is effective when sending characters at a low baud rate and then
immediately exiting, to ensure that the last character is output
correctly.
2025-12-31 15:14:18 +09:00
yabu76
e73ff6d208 Fix tty_write to return error code (negative value) in error case 2025-12-30 21:04:29 +09:00
yabu76
5045ca1768 Fix issue that output-delay doen't work in non-interactive mode 2025-12-30 20:58:06 +09:00
yabu76
2b0e674ee4 Fix execute_shell_command to use tty_write 2025-12-30 20:30:41 +09:00
yabu76
eab0f6245b Add write_poll to improve tty_write and tty_sync
Implement blockable write with poll + nonblock write.
2025-12-30 20:19:26 +09:00
yabu76
b5656112d7 Fix clear_line and print_line to untaint line status 2025-12-30 12:07:43 +09:00
yabu76
266338a926 Add Lua REPL mode and multiline edit function in the mode.
Typing "@repl" after Ctrl-t r, enter Lua REPL mode.
Typing "@exit" in REPL mode will return you to normal mode.

Note:
- the determination of continuation lines is not done automatically, and
  if the end of a line is \, it is determined to be a continuation
  instruction.
- In REPL mode, tio's main loop is blocked. (Ctrl-t q works.)

Example:
>> t = {1,2,3,4,5}
>> for _,v in ipairs(t) do\
>>     print(v,"\r")\
>> end

Implementation improvements:
- Add tty_init() and script_interp_init() in order to only once
  initialization.
- Fix script function to work without device_fd.
2025-12-30 11:41:39 +09:00
yabu76
c25a379fbb Add some socket examples of bidirectional command helpers and pexpect
bidir_cmd_herlper.sh: Bidirectional command helper by socat.
bidir_cmd_helper.py: Bidirectional command helper by python and netcat.

pexpect-ping.py, pexpect-pong.py: Scripts for throwing Ping-Pong between
cross-connected tios.
2025-12-07 11:21:55 +09:00
yabu76
a9f05a66f7 Change Ctrl-t r lua funtcion to start with the previous call state
When calling the Lua interpreter with Ctrl-t r, change to start with the
previous call state.

This allows you to use it like REPL:
(example)
  Ctrl-t r : my_library.lua
  Ctrl-t r : !result1 = my_func("test1"); result2 = my_func("test2")
  Ctrl-t r : !if (result1 == result2) then print("OK") else print("NG")
  Ctrl-t r : @new

When you enter strings using Ctrl-t r, the string is interpreted as
follows:
- If the string does not begin with either "!" or "@", the string is
assumed to be the file name of a script and is executed.
- If the string begins with "!", the string excluding the "!" is
interpreted as Lua commands.
- If the string begins with "@", Strings beginning with "@" are
considered instructions to the interpreter. Currently valid instructions
are:
@new: Clear the Lua state. (== reset the Lua interpreter.)
@doopt: Execute the Lua script action specified by the option that start
with clearing the Lua state.
@nuldo=opt: do @doopt action when an empty string is entered (default).
@nuldo=none: Do nothing when an empty string is entered.

And now, lua interpreter start with GC.
If you need to stop GC, do lua function collectgarbage("stop").
2025-11-29 18:03:30 +09:00
yabu76
0983ce6eeb Add history/line-edit function to Ctrl-t command string input
Ctrl-t r/R/x/y commands require string input. To reduce the hassle of
repeatedly entering this information, I apply readline functions
(line-editing and history) to the string input.

Now tio have two histories: one for the line input-mode and one for the
ctrl-t command's string input.

And the following changes are made to the line input:
- Add prompts.
  "> ": line input-mode, ">> " Ctrl-t string input.
- Omits duplicate lines from the history.

To implement the above functionality, we will modify the readline
functions to use the multi-instance style.
2025-11-29 16:41:04 +09:00
yabu76
8a1b739ae6 Add cleanup read to Lua API tio.expect[s]
tio.expect[s]() performs pattern matching for each character received,
but there are cases where the character reception speed exceeds the
pattern matching speed, resulting in characters being missed.

Add non-blocking reads to read all characters that have been received at
that time.
2025-11-15 23:26:43 +09:00
yabu76
8e02cded17 Change the Lua API timeout specification to allow for non-blocking reads.
Change timeout argument of tio.read / readline / expect[s] to be similar
to the timeout specification for poll(2) and LuaSocket.

If timeout is negative value or not provided or nil, it will wait
indefinitely until data is ready to read.
If timeout is 0, it will read data that has arrived with non-blocking.

Prior to this fix, non-blocking reads could not be specified.
2025-11-15 22:46:54 +09:00
yabu76
56c378f5f9 Add multipul patterns function tio.expects() to Lua API
Add tio.expects() which has a table of patterns in the arguments to
support "OR" pattern waiting.

The return values of tio.expects() are:
1st: index of the matched pattern in the table. nil if unmatched.
2nd: a table of captures if one of the patterns are matched.
     nil if any of the patterns are unmatched and timeout.
3rd: all received strings in tio.expects() to treat the strings after
     match.

Add all received string to tio.expect()'s return values too.

For example,
  idx, captures, all_received = tio.expects( {"\nOK", "\nERROR"}, 1000 )
  captures, all_received = tio.expect( "\nPROMPT>", 1000 )
2025-11-02 10:15:16 +09:00
yabu76
b5944275d3 Revert "Add multipul patterns function to Lua API tio.expect()"
This reverts commit d1ff5f7142.
2025-11-02 09:50:44 +09:00
yabu76
67bd6b1a8c Fix map option's input/output description of tio man page. 2025-10-26 22:28:49 +09:00
yabu76
d1ff5f7142 Add multipul patterns function to Lua API tio.expect()
Lua regular expressions do not have an OR specification, so the current
tio.expect() cannot wait until any of multiple patterns match.

This will cause OR waiting behavior when a table of multiple regular
expressions is passed as a pattern.
If you pass a string as a pattern, it will treats a table has single
regular expression.

and this will change return values.
1st: table of captures if one of the patterns matched
     nil if any pattern unmatched and timeout.
2nd: whole input strings
3rd: index of the matched pattern. 0 if unmatched.

For example,
    captures, whole, idx = tio.expect( {"\nOK", "\nERROR"} )
    captures, whole, idx = tio.expect( "\nOK" )
2025-10-05 20:43:06 +09:00
yabu76
58aae5511f Add --script function to key command ctrl-t r "Run script"
If filename starts with '!', do filename's remain parts as lua commands.
2025-10-05 18:52:49 +09:00
yabu76
e62a23f320 Add option '--output-line-delay-char cr|lf' 2025-10-05 15:25:47 +09:00
yabu76
9e51d2e4ee Add Lua API twrite
The twrite Lua API is a variation of the write API, with the output
character mapping and output-delay / output-line-delay features enabled.
Since it processes each character internally, writing speed is slower
than the write API.

~~~~~~~~~~~~~~~~~~~~~~~
twrite(string)
    Translate string with output character mapping and write the
    translated string to serial device with output-delay /
    output-line-delay.
2025-09-23 19:15:30 +09:00
yabu76
6a590bd598 Fix output-delay and input-mode line combination bug 2025-09-20 22:09:16 +09:00
yabu76
3922ce3447 Fix output-line-delay behavior
If output-line-delay is non-zero and output-delay is any, line delay
time set output-line-delay.
If output-line-delay is zero and output-delay is non-zero, line delay
time set output-delay.

It was previously (output-line-delay + output-delay) in any case.
Also, since the buffer was flushed after the line delay, there was a
possibility that the delay would be reduced or not applied at all.
2025-09-20 22:08:25 +09:00
yabu76
9784d3c277 Fix compile error in MSYS2/Cygwin 2025-08-16 18:05:56 +09:00