SD Core Solo for Linux LS2.0-0

SD Basic - Terminal Input and Output

Writing to the screen, reading from the keyboard, and positioning the cursor.

This page covers what a program says to the person running it and what it reads back: the output statements, the input statements, and the cursor and key handling underneath them.

SD folds case, so a program may be written in either case. Keywords are shown here in lower case. In the tables, italics mark something you supply and bold marks a word typed as it stands; braces mark an optional part.

Output#

print {on print.unit} expression {:}
crt expression {:}
display expression {:}

All three write a line. The differences matter only once a print unit is involved:

print goes to the current print unit — the terminal normally, the printer after printer on
crt always goes to the terminal, even while printer on is in force
display a synonym for crt

That is the whole reason crt exists. A report that prints its output and shows progress on screen uses print for the report and crt for the progress; using print for both puts "Processing record 400..." in the middle of the printed report.

A trailing colon suppresses the line ending, so the next statement continues on the same line:

crt 'Working' :
for i = 1 to 10
   crt '.' :
next i
crt ''

Laying output out#

The format qualifier goes straight after the value, with nothing between them:

print total '10r2'

42 'R#8' gives ␣␣␣␣␣␣42 — right-justified in eight columns. The full specification language is in SD Basic - Data Conversion, along with the warning that a value longer than the width wraps rather than truncating.

The cursor#

@(column, row)
@(code)

@() returns a string of terminal control characters. Printing it moves the cursor or changes the display; it is not a statement.

On this port it emits ansi escape sequences. @(0,0) is six bytes — 27 91 49 59 49 72, which is ESC [ 1 ; 1 H, the standard cursor positioning sequence. @(-1), clear screen, is also six bytes.

Code
@(-1) clear the screen and home the cursor
@(-2) home the cursor without clearing
@(-3) clear to end of screen
@(-4) clear to end of line
@(-5) · @(-6) cursor off, cursor on
@(-13) · @(-14) reverse video on, off

The sequences come from the terminal definition, not from a fixed table. A session whose terminal type is wrong gets sequences the terminal does not understand and displays as literal text. term at the command line reports and sets the type.

Screen geometry#

@crtwide
@crthigh
terminfo(key)

@crtwide and @crthigh are the current width and height. After term 200,9999 they read 200 and 9999 — they follow whatever term last set, which is why a program must read them rather than assume 80 by 24.

terminfo() reports a named capability of the terminal definition. In a session with no terminal, terminfo('name') and terminfo('cols') both return the null string — so a program that formats a screen from terminfo() must cope with getting nothing back when it is run from a script or the API.

Saving and restoring the terminal modes: ttyget() and ttyset#

settings = ttyget()
ttyset settings

ttyget() returns the modes that pterm sets as a field mark delimited array, and ttyset puts such an array back, so a program that changes the terminal can leave it as it found it:

saved = ttyget()
... change the terminal ...
ttyset saved
Field Mode pterm
1 is Ctrl-C the break key? (0 or 1) break on / off
2 always 0 (was: is case inversion on)
3 the break character, as a number (3 for Ctrl-C) break c
4 the output newline characters newline
5 the input return key, as a character code return
6 typed ctrl-\ ctrl-] ctrl-^ become the marks? mark
7 the erase key: 0 as at the start, 1 to 255 a character, 256 none erase
8 are control characters discarded by input? ctrl.filter
9, 10 Telnet binary mode, in and out binary
11 Telnet negotiation recognized? telnet

Fields 1 to 5 are openQM's; 6 to 11 are SD's own, so that a program that saves everything and puts it back changes nothing. A field that is null is left as it is, so an array that holds fewer fields sets fewer modes.

save.screen(column, row, width, height)
restore.screen(saved, column, row)

were for a program that pops a window over what is already displayed: they asked the terminal to keep the picture, which only AccuTerm and SDTerm could, and SD supports neither now. The calls are still accepted and the program still runs, but the terminal keeps nothing and restore.screen writes nothing. A program that needs what is under a window back should use the screen statement (page 12a), which keeps a picture of what it draws and puts it back itself.

Input#

input variable {, length {, fill}} {:} {with prompt} {format} {then ... else ...}
input @(column, row) {, length {, fill}} : variable
inputfield variable, length {, ...}
inputif variable {, length} then ... else ...

input reads a line. The optional length limits what may be typed and returns as soon as that many characters arrive, without waiting for Enter.

clause
append start with the variable's current value, so the user edits rather than retypes
overlay as append, but typing replaces from the first character
edit full line editing
hidden echo asterisks — for a password
no.echo show nothing of what is typed (the new line that ends the entry still appears); with hidden the asterisks go too
raw read exactly length characters just as they arrive: a newline or an erase key is data, nothing is echoed; input only, and the length is required
panning scroll a field wider than the space it is shown in
upcase fold what is typed to upper case
waiting n give up after n seconds and take the else branch (for and timeout, which meant the same, were removed in 2.0-0)

A fill for the field#

A third item after the length is a fill string. Its first character is shown in every cell of the field that has nothing in it, so the width of the field is clear while the user types; the erase key puts it back. When the entry is finished the cells left over take the second character (a blank if there is only one). A third character leaves the cursor at the end of the field instead of after the last character typed. A fill needs a length, and cannot be used with hidden (the compiler refuses it).

input name, 20, '.-'        ;* shows ....................  and, when done,  Anna----------------

Reading only what is already there#

inputif takes a line only if one is waiting, in the data stack or typed ahead at the keyboard; otherwise it takes the else branch at once, writes no prompt and leaves the variable alone. It has the options of input and input @. When SD's input is a pipe or a file rather than a terminal there is always something to read (the end of the file counts), so inputif behaves as input there.

Single characters that mean something#

Two statements give a single character, typed alone as the whole answer to input @ or inputfield, a special meaning. What was echoed stays on the screen as typed.

inputnull expression
inputtrap expression goto label{, label...}
inputtrap expression gosub label{, label...}
inputnull the first character of the expression makes the variable a null string. A null expression cancels it, and so does returning to the command prompt
inputtrap each character of the expression (up to 16) sends the program to the label at the same position in the list; a character past the end of the labels takes the last one. The variable is set to null. A null expression cancels the trap

With gosub, the return comes back to the statement after the inputtrap, not to the one after the input. If the input @ has a then or else clause, neither runs when a trap is taken. The trap belongs to the program that set it: a subroutine it calls is not trapped, and neither is a plain input. When both statements name a character, inputnull wins.

hidden is the only correct way to read a password. Turning the echo off by hand with echo off leaves it off if the program aborts between the two statements, and the user then types blind at the command prompt.

inputerr message
clearinput
cleardata
data expression
inputerr show a message on the status line without disturbing the screen
clearinput discard anything typed ahead (inputclear was a second name and was removed in 2.0-0)
data stack a value for the next input to consume
cleardata discard what data stacked

data is how one program answers another's prompts:

data 'Y'
execute 'DELETE.FILE OLDSTUFF'

A data value that is not consumed stays stacked. The next input anywhere in the session takes it — including a prompt the user was meant to answer. cleardata after anything that might not have consumed it.

Keys#

keyin()
keyinc()
keyinr()
keyready()
keycode()
keyin() wait for one keystroke and return it
keyinc() the same as keyin()
keyinr() raw — no translation at all
keyready() true if a keystroke is waiting; does not consume it
keycode() the code of the key that ended the last input
keyedit code, key.sequence
keyexit code, key.sequence
keytrap code, key.sequence
bindkey(key.string, action)

These attach a key to an editing action, an exit from input, or a trap. keycode() then tells the program which one fired, so a form can act on F3 without reading raw escape sequences.

bindkey('', -7) asks keycode() to return an Alt-letter — a terminal sends Alt+x as Esc followed by x, which nothing is bound to, so keycode() would ring the bell and drop both — as those two characters, Esc first; bindkey('', -8) puts back the bell and the drop (the default). Each answers the state it found (1 or 0). The screen library asks for it for the length of every key it reads, so a screen program sees alt-x and nothing else needs to change.

Prompts and headings#

prompt character
heading {no.eject} {on print.unit} text
footing {on print.unit} text
page {on print.unit} {page.number}

prompt sets the character SD shows when it wants input — a colon by default; prompt '' suppresses it.

heading and footing are covered with the rest of the paging machinery in SD Basic - Printing, because they apply to the terminal and the printer in the same way.

Echo and silence#

echo on | off | expression
hush on | off | expression
echo whether typed characters appear
hush whether output appears

hush on suppresses output entirely — used around an execute whose chatter you do not want. Both nest badly: they are a session setting, not a stack, so a program that aborts between hush on and hush off leaves the session silent and the user with no way to know why.

Reading the command line#

@sentence holds the command line that started the program, so a verb written in BASIC can parse its own arguments.

The port's terminal, in one paragraph#

Unlike SD Core for Windows, there is no console-mode switch to turn on here — a Linux terminal (a real console, xterm, gnome-terminal, an ssh client) already understands ANSI/terminfo escape sequences natively, so @() sequences just work, with nothing for SD or the user to configure first. SD is a terminal system throughout; the API is the only way to reach it without one, and there a program is reading a command's output rather than driving a screen. Such a session has no geometry and no capabilities, which is why terminfo() returns the null string above rather than guessing.

What is not here#

ttyget() · ttyset removed — they exposed a POSIX terminal structure this port does not have
connect.port() removed

See also#

SD Basic - Printing · SD Basic - Data Conversion · SD Basic - Program Control.