Qore Programming Language Reference Manual  1.12.0
Qore::TermIOS Class Reference

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

Public Member Methods

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

Static Public Member Methods

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

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:

TermIOS t();
stdin.getTerminalAttributes(t);
TermIOS orig = t.copy();
on_exit
stdin.setTerminalAttributes(TCSADRAIN, orig);
int 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);
}
string c = stdin.read(1);
stdout.printf(" GOT ASCII 0x%02x (%d) '%s'\n", ord(c), ord(c), c);
int ord(softstring str, softint offset=0)
Gives the positive numeric value of the given byte in the string passed; if no string is passed or th...

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

◆ constructor()

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:
TermIOS termios();
stdin.getTerminalAttributes(termios);

◆ copy()

Qore::TermIOS::copy ( )

Returns a copy of the object.

Platform Availability:
Qore::Option::HAVE_TERMIOS
Example:
TermIOS t2 = t.copy();

◆ getCC()

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

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

Parameters
ccthe 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
int vmin = t.getCC(VMIN);
# get character input timer in 0.1 second increments
int vtime = t.getCC(VTIME);
Exceptions
TERMIOS-CC-ERRORthe control character is invalid

◆ getCFlag()

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:
int flag = termios.getCFlag();

◆ getIFlag()

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:
int flag = termios.getIFlag();

◆ getLFlag()

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:
int flag = termios.getLFlag();

◆ getOFlag()

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:
int flag = termios.getOFlag();

◆ getWindowSize()

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:
hash wh = termios.getWindowSize();

◆ isEqual()

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:
bool b = termios.isEqual(termios2);

◆ setCC()

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

Sets the value of the given control character.

Parameters
offsetthe control character to set
valuethe 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-ERRORthe control character is invalid

◆ setCFlag()

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

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

Parameters
flagthe control mode (see Terminal Attribute Control Mode Constants for masks for this value)
Platform Availability:
Qore::Option::HAVE_TERMIOS
Example:
int cflag = termios.getCFlag();
cflag = (cflag & ~CSIZE) | CS8 | CSTOPB;
cflag = (cflag | CLOCAL | CREAD) & ~CRTSCTS;
termios.setCFlag(cflag);

◆ setIFlag()

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

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

Parameters
flagthe input mode flag for the object from a mask of Terminal Attributes Input Mode Constants
Platform Availability:
Qore::Option::HAVE_TERMIOS
Example:
int iflag = termios.getIFlag();
# ring bell on input queue full
iflag |= IMAXBEL;
termios.setIFlag(iflag);

◆ setLFlag()

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

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

Parameters
flagthe local mode (see Terminal Attribute Local Mode Constants for masks for this value)
Platform Availability:
Qore::Option::HAVE_TERMIOS
Example:
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);

◆ setOFlag()

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

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

Parameters
flagthe output mode (see Terminal Attributes Output Mode Constants for masks for this value)
Platform Availability:
Qore::Option::HAVE_TERMIOS
Example:
int oflag = termios.getOFlag();
# translate linefeeds to crlf
oflag |= ONLCR;
termios.setOFlag(oflag);