All Classes Namespaces Functions Variables Groups Pages
Qore::TermIOS Class Reference

This class allows Qore scripts to get or set terminal settings on UNIX platforms. More...

## Public Member Functions

| constructor ()
| Creates the TermIOS object with random contents.
| copy ()
| Returns a copy of the object.
int | getCC (softint cc)
| Returns the integer value for the given control character from the given control character code.
int | getCFlag ()
| Returns the control mode flag for the object.
int | getIFlag ()
| Returns the input mode flag for the object.
int | getLFlag ()
| Returns the local mode flag for the object.
int | getOFlag ()
| Returns the output mode flag for the object.
bool | isEqual (Termios termios)
| Returns True if the TermIOS object passed as an argument is equal to the current object; False if not.
nothing | setCC (softint offset, softint value)
| Sets the value of the given control character.
nothing | setCFlag (softint flag)
| Sets the control mode flag for the object from a mask of Terminal Attribute Control Mode Constants.
nothing | setIFlag (softint flag)
| Sets the input mode flag for the object from a mask of Terminal Attributes Input Mode Constants.
nothing | setLFlag (softint flag)
| Sets the local mode flag for the object from a mask of Terminal Attribute Local Mode Constants.
nothing | setOFlag (softint flag)
| Sets the output mode flag for the object from a mask of Terminal Attributes Output Mode Constants.

## Static Public Member Functions

static hash | getWindowSize ()
| Returns a hash giving the current terminal window size in hash keys `"rows"` and `"columns"`.

## Detailed Description

This class allows Qore scripts to get or set terminal settings on UNIX platforms.

Restrictions:
  Qore::PO_NO_TERMINAL_IO

On platforms without TermIOS support (such as Windows), none of the methods in this class are available; to write a portable program, check the Qore::Option::HAVE_TERMIOS constant at runtime before using this functionality.

This class contains the data structure used to read and set terminal attributes on terminal I/O constants (stdin, stdout, stderr).

This class is used with File::getTerminalAttributes(), File::setTerminalAttributes(), and the terminal I/O constants to manipulate terminal attributes.

For example, here is some code to set terminal attributes, read in a character from standard input with a timeout, and reset the terminal attributes:

my TermIOS $t();
stdin.getTerminalAttributes($t);
my TermIOS $orig = $t.copy();
on_exit
stdin.setTerminalAttributes(TCSADRAIN, $orig);
my $lflag = $t.getLFlag();
$lflag &= ~ICANON;
$lflag &= ~ECHO;
$lflag &= ~ISIG;
$t.setLFlag($lflag);
$t.setCC(VMIN, 1);
$t.setCC(VTIME, 0);
stdin.setTerminalAttributes(TCSADRAIN, $t);
stdout.printf("Press any key: ");
while (!stdin.isDataAvailable(20ms)) {
stdout.printf(".");
stdout.sync();
usleep(1ms);
}
my $c = stdin.read(1);
stdout.printf(" GOT ASCII 0x%02x (%d) '%s'\n", ord($c), ord($c), $c);

For more information on terminal attributes, see your system's manual pages for "termios".

Note
  This class is not available with the PO_NO_TERMINAL_IO parse option.

## Member Function Documentation

Qore::TermIOS::constructor | ( |  | ) |

Creates the TermIOS object with random contents.

Use File::getTerminalAttributes() with a terminal I/O constant to initialize the object with terminal settings

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  my TermIOS $termios();
  stdin.getTerminalAttributes($termios);

Qore::TermIOS::copy | ( |  | ) |

Returns a copy of the object.

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  my TermIOS $t2 = $t.copy();

int Qore::TermIOS::getCC | ( | softint | cc | ) |

Returns the integer value for the given control character from the given control character code.

Parameters

  cc | the control character code for the character to retrieve

Returns
  the integer value for the given control character from the given control character code

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  # get minimum characters to return on a read
  my int $vmin = $t.getCC(VMIN);
  # get character input timer in 0.1 second increments
  my int $vtime = $t.getCC(VTIME);

Exceptions

  TERMIOS-CC-ERROR | the control character is invalid

int Qore::TermIOS::getCFlag | ( |  | ) |

Returns the control mode flag for the object.

Returns
  the control mode flag for the object

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  my int $flag = $termios.getCFlag();

int Qore::TermIOS::getIFlag | ( |  | ) |

Returns the input mode flag for the object.

Returns
  the input mode flag for the object

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  my int $flag = $termios.getIFlag();

int Qore::TermIOS::getLFlag | ( |  | ) |

Returns the local mode flag for the object.

Returns
  the local mode flag for the object

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  my int $flag = $termios.getLFlag();

int Qore::TermIOS::getOFlag | ( |  | ) |

Returns the output mode flag for the object.

Returns
  the output mode flag for the object

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  my int $flag = $termios.getOFlag();

static hash Qore::TermIOS::getWindowSize | ( |  | ) |

static

Returns a hash giving the current terminal window size in hash keys `"rows"` and `"columns"`.

Returns
  a hash giving the current terminal window size in hash keys `"rows"` and `"columns"`

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Restrictions:
  Qore::PO_NO_TERMINAL_IO

Code Flags:
  RET_VALUE_ONLY

Example:
  my hash $wh = $termios.getWindowSize();

bool Qore::TermIOS::isEqual | ( | Termios | termios | ) |

Returns True if the TermIOS object passed as an argument is equal to the current object; False if not.

Returns
  True if the TermIOS object passed as an argument is equal to the current object; False if not

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Code Flags:
  CONSTANT

Example:
  my bool $b = $termios.isEqual($termios2);

nothing Qore::TermIOS::setCC | ( | softint | offset,
|  | softint | value
| ) |  |

Sets the value of the given control character.

Parameters

  offset | the control character to set
  value | the value to set for the control character

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  # set minimum characters to return on a read
  $t.setCC(VMIN, 1);
  # set character input timer in 0.1 second increments (= no timer)
  $t.setCC(VTIME, 0);

Exceptions

  TERMIOS-CC-ERROR | the control character is invalid

nothing Qore::TermIOS::setCFlag | ( | softint | flag | ) |

Sets the control mode flag for the object from a mask of Terminal Attribute Control Mode Constants.

Parameters

  flag | the control mode (see Terminal Attribute Control Mode Constants for masks for this value)

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  my int $cflag = $termios.getCFlag();
  $cflag = ($cflag & ~CSIZE) | CS8 | CSTOPB;
  $cflag = ($cflag | CLOCAL | CREAD) & ~CRTSCTS;
  $termios.setCFlag($cflag);

nothing Qore::TermIOS::setIFlag | ( | softint | flag | ) |

Sets the input mode flag for the object from a mask of Terminal Attributes Input Mode Constants.

Parameters

  flag | the input mode flag for the object from a mask of Terminal Attributes Input Mode Constants

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  my int $iflag = $termios.getIFlag();
  # ring bell on input queue full
  $iflag |= IMAXBEL;
  $termios.setIFlag($iflag);

nothing Qore::TermIOS::setLFlag | ( | softint | flag | ) |

Sets the local mode flag for the object from a mask of Terminal Attribute Local Mode Constants.

Parameters

  flag | the local mode (see Terminal Attribute Local Mode Constants for masks for this value)

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  my int $lflag = $termios.getLFlag();
  # disable canonical input mode (= turn on "raw" mode)
  $lflag &= ~ICANON;
  # turn off echo mode
  $lflag &= ~ECHO;
  # do not check for special input characters (INTR, QUIT, and SUSP)
  $lflag &= ~ISIG;
  $termios.setLFlag($lflag);

nothing Qore::TermIOS::setOFlag | ( | softint | flag | ) |

Sets the output mode flag for the object from a mask of Terminal Attributes Output Mode Constants.

Parameters

  flag | the output mode (see Terminal Attributes Output Mode Constants for masks for this value)

Platform Availability:
  Qore::Option::HAVE_TERMIOS

Example:
  my int $oflag = $termios.getOFlag();
  # translate linefeeds to crlf
  $oflag |= ONLCR;
  $termios.setOFlag($oflag);