Module eco.termios

Terminal I/O settings (termios).

This module exposes a small subset of POSIX termios APIs.

The main value type is an attr userdata which wraps a struct termios. Use tcgetattr to obtain an attribute object, modify it via attr:* methods, then apply it with tcsetattr.

Exported constants (from the system <termios.h>):

  • tcsetattr actions: TCSANOW, TCSADRAIN, TCSAFLUSH
  • input flags: IGNBRK, BRKINT, IGNPAR, PARMRK, INPCK, ISTRIP, INLCR, IGNCR, ICRNL, IUCLC, IXON, IXANY, IXOFF, IMAXBEL, IUTF8
  • output flags: OPOST, OLCUC, ONLCR, OCRNL, ONOCR, ONLRET, OFILL, OFDEL, NLDLY, CRDLY, TABDLY, BSDLY, VTDLY, FFDLY
  • control flags: CBAUD, CBAUDEX, CSIZE, CSTOPB, CREAD, PARENB, PARODD, HUPCL, CLOCAL, CIBAUD, CMSPAR, CRTSCTS
  • local flags: ISIG, ICANON, XCASE, ECHO, ECHOE, ECHOK, ECHONL, ECHOCTL, ECHOPRT, ECHOKE, FLUSHO, NOFLSH, TOSTOP, PENDIN, IEXTEN
  • control character indices: VDISCARD, VEOF, VEOL, VEOL2, VERASE, VINTR, VKILL, VLNEXT, VMIN, VQUIT, VREPRINT, VSTART, VSTOP, VSUSP, VTIME, VWERASE
  • baud rate constants: B0 ... B230400
  • tcflush selectors: TCIFLUSH, TCOFLUSH, TCIOFLUSH
  • tcflow actions: TCOOFF, TCOON, TCIOFF, TCION

Functions

tcgetattr (fd) Get terminal attributes.
tcsetattr (fd, actions, attr) Set terminal attributes.
tcflush (fd, queue_selector) Flush terminal I/O queues.
tcflow (fd, action) Suspend or restart terminal I/O.

Class attr

attr:set_flag (type, flag) Set a flag bit in the attributes.
attr:clr_flag (type, flag) Clear a flag bit in the attributes.
attr:set_cc (name, value) Set a control character.
attr:get_ispeed () Get input baud rate.
attr:get_ospeed () Get output baud rate.
attr:set_ispeed (speed) Set input baud rate.
attr:set_ospeed (speed) Set output baud rate.
attr:set_speed (speed) Set both input and output baud rate.
attr:clone () Clone attributes.


Functions

tcgetattr (fd)
Get terminal attributes.

Parameters:

  • fd int File descriptor.

Returns:

    attr attr

Or

  1. nil On failure.
  2. string Error message.
tcsetattr (fd, actions, attr)
Set terminal attributes.

Parameters:

  • fd int File descriptor.
  • actions int One of termios.TCSANOW, termios.TCSADRAIN, termios.TCSAFLUSH.
  • attr attr Attributes.

Returns:

    boolean true On success

Or

  1. nil On failure.
  2. string Error message.

Usage:

    local termios = require 'eco.termios'
    local file = require 'eco.file'
    local eco = require 'eco'
    
    local f<close>, err = file.open('/dev/ttyUSB0')
    assert(f, err)
    
    local attr, err = termios.tcgetattr(f.fd)
    assert(attr, err)
    
    local nattr = attr:clone()
    
    nattr:clr_flag('l', termios.ECHO)
    nattr:set_speed(termios.B115200)
    
    local ok, err = termios.tcsetattr(f.fd, termios.TCSANOW, nattr)
    assert(ok, err)
    
    eco.run(function()
        local data, err = f:read(1024)
        assert(data, err)
    
        print('read:', data)
    
        -- recover term attr
        termios.tcsetattr(f.fd, termios.TCSANOW, attr)
    end)
    
    eco.loop()
tcflush (fd, queue_selector)
Flush terminal I/O queues.

Parameters:

  • fd int File descriptor.
  • queue_selector int One of termios.TCIFLUSH, termios.TCOFLUSH, termios.TCIOFLUSH.

Returns:

    boolean true On success

Or

  1. nil On failure.
  2. string Error message.
tcflow (fd, action)
Suspend or restart terminal I/O.

Parameters:

  • fd int File descriptor.
  • action int One of termios.TCOOFF, termios.TCOON, termios.TCIOFF, termios.TCION.

Returns:

    boolean true On success

Or

  1. nil On failure.
  2. string Error message.

Class attr

Termios attributes.

This userdata wraps a struct termios.

attr:set_flag (type, flag)

Set a flag bit in the attributes.

type selects which flag field is modified:

  • "i": input flags (c_iflag)
  • "o": output flags (c_oflag)
  • "c": control flags (c_cflag)
  • "l": local flags (c_lflag)

Parameters:

  • type string One of "i", "o", "c", "l".
  • flag int Bitmask value.
attr:clr_flag (type, flag)
Clear a flag bit in the attributes.

Parameters:

  • type string One of "i", "o", "c", "l".
  • flag int Bitmask value.
attr:set_cc (name, value)
Set a control character.

name is one of the V* indices (e.g. termios.VMIN, termios.VTIME).

Parameters:

  • name int Control character index.
  • value int Value to set.
attr:get_ispeed ()
Get input baud rate.

Returns:

    int speed
attr:get_ospeed ()
Get output baud rate.

Returns:

    int speed
attr:set_ispeed (speed)
Set input baud rate.

Parameters:

  • speed int Baud rate constant (e.g. termios.B115200).

Returns:

    boolean true On success.

Or

  1. nil On failure.
  2. string Error message.
attr:set_ospeed (speed)
Set output baud rate.

Parameters:

  • speed int Baud rate constant.

Returns:

    boolean true On success.

Or

  1. nil On failure.
  2. string Error message.
attr:set_speed (speed)
Set both input and output baud rate.

Parameters:

  • speed int Baud rate constant.

Returns:

    boolean true On success.

Or

  1. nil On failure.
  2. string Error message.
attr:clone ()
Clone attributes.

Returns:

    attr new_attr
generated by LDoc 1.5.0 Last updated 2026-04-09 14:48:22