DEFINITION MODULE VT100;

(*
 The POSIX function cfmakeraw() sets the canonical
 combination. Applications typically use tcgetattr()
 to save the current settings, call cfmakeraw() 
 to switch to raw mode, and call tcsetattr() with 
 the saved settings on exit to restore the terminal.
*)

TYPE 
    screenModes = (M40x25BW,M40x25C,M80x25BW,M80x25C,M320x200C4,M320x200BW,M640x200BW,
                   M320x200C256,M640x200C,M640x350BW,M640x350C,M640x480BW,M640x480C,M320x200C);
    Attribut    = (boldAt,dimAt,italicAt,underlineAt,blinkingAt,inverseAt,hiddenAt,
                   strikethroughAt,doubleunderlineAt);

    Colors      = (Black, Red, Green, Yellow, Blue, Magenta, Cyan, White, Default,
                    BrightBlack, BrightRed, BrightGreen, BrightYellow, 
                    BrightBlue, BrighMagenta, BrightCyan, BrightWhite);
(*
PROCEDURE setnl;
(* LMN             Set new line mode                      ^[[20h *)

PROCEDURE setappl;
(* DECCKM        Set cursor key to application          ^[[?1h *)

PROCEDURE setansi;
(* DECANM        Set ANSI (versus VT52)                 none *)

PROCEDURE setcol;
(* DECCOLM        Set number of columns to 132           ^[[?3h *)

PROCEDURE setsmooth;
(* DECSCLM     Set smooth scrolling                   ^[[?4h *)

PROCEDURE setrevscrn;
(* DECSCNM    Set reverse video on screen            ^[[?5h *)

PROCEDURE setorgrel;
(* DECOM       Set origin to relative                 ^[[?6h *)

PROCEDURE setwrap;
(* DECAWM        Set auto-wrap mode                     ^[[?7h *)

PROCEDURE setrep;
(* DECARM         Set auto-repeat mode                   ^[[?8h *)

PROCEDURE setinter;
(* DECINLM      Set interlacing mode                   ^[[?9h *)

PROCEDURE setlf;
(* LMN             Set line feed mode                     ^[[20l *)

PROCEDURE setcursor;
(* DECCKM      Set cursor key to cursor               ^[[?1l *)

PROCEDURE setvt52;
(* DECANM        Set VT52 (versus ANSI)                 ^[[?2l *)

PROCEDURE resetcol;
(* DECCOLM      Set number of columns to 80            ^[[?3l *)

PROCEDURE setjump;
(* DECSCLM       Set jump scrolling                     ^[[?4l *)

PROCEDURE setnormscrn;
(* DECSCNM   Set normal video on screen             ^[[?5l *)

PROCEDURE setorgabs;
(* DECOM       Set origin to absolute                 ^[[?6l *)

PROCEDURE resetwrap;
(* DECAWM      Reset auto-wrap mode                   ^[[?7l *)

PROCEDURE resetrep;
(* DECARM       Reset auto-repeat mode                 ^[[?8l *)

PROCEDURE resetinter;
(* DECINLM    Reset interlacing mode                 ^[[?9l *)

PROCEDURE altkeypad;
(* DECKPAM     Set alternate keypad mode              ^[= *)

PROCEDURE numkeypad;
(* DECKPNM     Set numeric keypad mode                ^[> *)

PROCEDURE setukg0;
    (* Set United Kingdom G0 character set    ^[(A *)

PROCEDURE setukg1;
    (* Set United Kingdom G1 character set    ^[)A *)

PROCEDURE setusg0;
(* Set United States G0 character set     ^[(B *)

PROCEDURE setusg1;
(* Set United States G1 character set     ^[)B *)

PROCEDURE setspecg0;
(* Set G0 special chars. & line set       ^[(0 *)

PROCEDURE setspecg1;
(* Set G1 special chars. & line set       ^[)0 *)

PROCEDURE setaltg0;
(* Set G0 alternate character ROM         ^[(1 *)

PROCEDURE setaltg1;
(* Set G1 alternate character ROM         ^[)1 *)

PROCEDURE setaltspecg0;
(* Set G0 alt char ROM and spec. graphics ^[(2 *)

PROCEDURE setaltspecg1;
(* Set G1 alt char ROM and spec. graphics ^[)2 *)

PROCEDURE setss2 ;
(* SS2 Set single shift 2                 ^[N *)

PROCEDURE setss3;
(* SS3 Set single shift 3                  ^[O *)
*)

(*
ESC Code Sequence 	Reset Sequence 	Description
ESC[1;34;{...}m 		Set graphics modes for cell, separated by semicolon (;).
ESC[1m 	ESC[22m 	set bold mode.
ESC[2m 	ESC[22m 	set dim/faint mode.
ESC[3m 	ESC[23m 	set italic mode.
ESC[4m 	ESC[24m 	set underline mode.
ESC[5m 	ESC[25m 	set blinking mode
ESC[7m 	ESC[27m 	set inverse/reverse mode
ESC[8m 	ESC[28m 	set hidden/invisible mode
ESC[9m 	ESC[29m 	set strikethrough mode.
ESC[21m ESC[24m    set double underline
*)

PROCEDURE modesoff; (*OK*)
(* SGR0 Turn off character attributes          ^[[m *)

PROCEDURE modesoff1; (*OK*)
(* SGR0 Turn off character attributes          ^[[0m *)

PROCEDURE bold; (*OK*)
(* SGR1 Turn bold mode on                      ^[[1m *)

PROCEDURE lowint; (*OK*)
(* SGR2 Turn low intensity mode on             ^[[2m *)

PROCEDURE italic; (*OK*)
(* set italic mode                              ESC[3m *)

PROCEDURE underline; (*OK*)
(* SGR4 Turn underline mode on                 ^[[4m *)

PROCEDURE blink; (*OK*)
(* SGR5 Turn blinking mode on                  ^[[5m *)

PROCEDURE reverse; (*OK*)
(* SGR7 Turn reverse video on                  ^[[7m *)

PROCEDURE invisible; (*OK*)
(* SGR8 Turn invisible text mode on            ^[[8m *)

PROCEDURE strikethrough; (*OK*)
(* ESC[9m 	ESC[29m 	set strikethrough mode. *)

PROCEDURE doubleUnderline;
(* ESC[21m *)

PROCEDURE resetbold; (*OK*)
(* SGR1 Turn bold mode on                      ^[[1m *)

PROCEDURE resetlowint; (*OK*)
(* SGR2 Turn low intensity mode on             ^[[2m *)

PROCEDURE resetitalic; (*OK*)
(* set italic mode                              ESC[3m *)

PROCEDURE resetunderline; (*OK*)
(* SGR4 Turn underline mode on                 ^[[4m *)

PROCEDURE resetblink; (*OK*)
(* SGR5 Turn blinking mode on                  ^[[5m *)

PROCEDURE resetreverse; (*OK*)
(* SGR7 Turn reverse video on                  ^[[7m *)

PROCEDURE resetinvisible; (*OK*)
(* SGR8 Turn invisible text mode on            ^[[8m *)

PROCEDURE resetstrikethrough; (*OK*)
(* ESC[9m 	ESC[29m 	set strikethrough mode. *)

PROCEDURE resetDoubleUnderline;
(* ESC[24m. *)

(*
PROCEDURE setwin;
(* DECSTBM Set top and bottom line#s of a window  ^[[<v>;<v>r *)
*)

(*
ESC Code Sequence 	Description
ESC[H 	moves cursor to home position (0, 0)
ESC[{line};{column}H
ESC[{line};{column}f 	moves cursor to line #, column #
ESC[#A 	moves cursor up # lines
ESC[#B 	moves cursor down # lines
ESC[#C 	moves cursor right # columns
ESC[#D 	moves cursor left # columns
ESC[#E 	moves cursor to beginning of next line, # lines down
ESC[#F 	moves cursor to beginning of previous line, # lines up
ESC[#G 	moves cursor to column #

ESC[6n 	request cursor position (reports as ESC[#;#R)
ESC M 	moves cursor one line up, scrolling if needed
ESC 7 	save cursor position (DEC)
ESC 8 	restores the cursor to the last saved position (DEC)
ESC[s 	save cursor position (SCO)
ESC[u 	restores the cursor to the last saved position (SCO)
*)

PROCEDURE cursorup(n : CARDINAL); (*OK*)
(* CUU Move cursor up n lines                 ^[[<n>A *)

PROCEDURE cursordn(n : CARDINAL); (*OK*)
(* CUD Move cursor down n lines               ^[[<n>B *)

PROCEDURE cursorrt(n : CARDINAL); (*OK*)
(* CUF Move cursor right n lines              ^[[<n>C *)

PROCEDURE cursor1lf(n : CARDINAL); (*OK*)
(* CUB Move cursor left n lines               ^[[<n>D *)

PROCEDURE cursorbnl(n : CARDINAL);
(* ESC[#E 	moves cursor to beginning of next line, # lines down *)

PROCEDURE cursorblpl(n : CARDINAL);
(* ESC[#F 	moves cursor to beginning of previous line, # lines up *)

PROCEDURE cursottocol(n : CARDINAL);
(* ESC[#G 	moves cursor to column # *)

PROCEDURE requestCursorPosition(VAR x,y : CARDINAL);
(* ESC[6n 	request cursor position (reports as ESC[#;#R) *)

PROCEDURE cursorhome; (*OK*)
(* Move cursor to upper left corner       ^[[H *)

PROCEDURE cursorhome1; (*OK*)
(* Move cursor to upper left corner       ^[[;H *)

PROCEDURE cursorpos(x,y: CARDINAL); (*OK*)
(* CUP Move cursor to screen location v,h     ^[[<v>;<h>H *)
(*
PROCEDURE hvhome;
(* Move cursor to upper left corner       ^[[f *)

PROCEDURE hvhome;
(* Move cursor to upper left corner       ^[[;f *)

PROCEDURE hvpos(v,h);
(* CUP Move cursor to screen location v,h     ^[[<v>;<h>f *)

PROCEDURE index;
(* IND Move/scroll window up one line         ^[D *)

PROCEDURE revindex;
(* RI Move/scroll window down one line       ^[M *)

PROCEDURE nextline;
(* NEL Move to next line                      ^[E *)

PROCEDURE savecursor;
(* DECSC Save cursor position and attributes    ^[7 *)

PROCEDURE restorecursor;
(* DECSC Restore cursor position and attributes ^[8 *)

PROCEDURE tabset;
(* HTS Set a tab at the current column        ^[H *)

PROCEDURE tabclr;
(* TBC Clear a tab at the current column      ^[[g *)

PROCEDURE tabclr;
(* TBC Clear a tab at the current column      ^[[0g *)

PROCEDURE tabclrall;
(* TBC Clear all tabs                         ^[[3g *)

PROCEDURE dhtop;
(* DECDHL Double-height letters, top half        ^[#3 *)

PROCEDURE dhbot;
(* DECDHL Double-height letters, bottom half     ^[#4 *)

PROCEDURE swsh;
(* DECSWL Single width, single height letters    ^[#5 *)

PROCEDURE dwsh;
(* DECDWL Double width, single height letters    ^[#6 *)

PROCEDURE cleareol;
(* EL0 Clear line from cursor right           ^[[K *)

PROCEDURE cleareol;
(* EL0 Clear line from cursor right           ^[[0K *)

PROCEDURE clearbol;
(* EL1 Clear line from cursor left            ^[[1K *)

PROCEDURE clearline;
(* EL2 Clear entire line                      ^[[2K *)

PROCEDURE cleareos;
(* ED0 Clear screen from cursor down          ^[[J *)

PROCEDURE cleareos;
(* ED0 Clear screen from cursor down          ^[[0J *)

PROCEDURE clearbos;
(* ED1 Clear screen from cursor up            ^[[1J *)
*)
PROCEDURE clearscreen; (* OK *)
(* ED2 Clear entire screen                    ^[[2J *)
(*
PROCEDURE devstat;
(* DSR Device status report                   ^[5n *)

PROCEDURE termok;
(* DSR Response: terminal is OK            ^[0n *)

PROCEDURE termnok;
(* DSR Response: terminal is not OK        ^[3n *)

PROCEDURE getcursor;
(* DSR Get cursor position                    ^[6n *)

PROCEDURE cursorpos;
(* CPR Response: cursor is at v,h          ^[<v>;<h>R *)

PROCEDURE ident;
(* DA Identify what terminal type            ^[[c *)

PROCEDURE ident;
(* DA Identify what terminal type (another)  ^[[0c *)

PROCEDURE gettype;
(* DA Response: terminal type code n      ^[[?1;<n>0c *)

PROCEDURE reset;
(* RIS Reset terminal to initial state        ^[c *)

PROCEDURE align;
(* DECALN Screen alignment display               ^[#8 *)

PROCEDURE testpu;
(* DECTST Confidence power up test               ^[[2;1y *)

PROCEDURE testlb;
(* DECTST Confidence loopback test               ^[[2;2y *)

PROCEDURE testpurep;
(* DECTST Repeat power up test                   ^[[2;9y *)

PROCEDURE testlbrep;
(* DECTST Repeat loopback test                   ^[[2;10y *)

PROCEDURE ledsoff;
(* DECLL0 Turn off all four leds                 ^[[0q *)

PROCEDURE led1;
(* DECLL1 Turn on LED #1                         ^[[1q *)

PROCEDURE led2;
(* DECLL2 Turn on LED #2                         ^[[2q *)

PROCEDURE led3;
(* DECLL3 Turn on LED #3                         ^[[3q *)

PROCEDURE led4;
(* DECLL4 Turn on LED #4                         ^[[4q *)
*)

(*
Screen Modes
Set Mode
ESC Code Sequence 	Description
ESC[={value}h 	Changes the screen width or type to the mode specified by value.
ESC[=0h 	40 x 25 monochrome (text)
ESC[=1h 	40 x 25 color (text)
ESC[=2h 	80 x 25 monochrome (text)
ESC[=3h 	80 x 25 color (text)
ESC[=4h 	320 x 200 4-color (graphics)
ESC[=5h 	320 x 200 monochrome (graphics)
ESC[=6h 	640 x 200 monochrome (graphics)
ESC[=7h 	Enables line wrapping
ESC[=13h 	320 x 200 color (graphics)
ESC[=14h 	640 x 200 color (16-color graphics)
ESC[=15h 	640 x 350 monochrome (2-color graphics)
ESC[=16h 	640 x 350 color (16-color graphics)
ESC[=17h 	640 x 480 monochrome (2-color graphics)
ESC[=18h 	640 x 480 color (16-color graphics)
ESC[=19h 	320 x 200 color (256-color graphics)
ESC[={value}l 	Resets the mode by using the same values that Set Mode uses, except for 7, which disables line wrapping. The last character in this escape sequence is a lowercase L.
Common Private Modes
*)
PROCEDURE screenMode(mode : screenModes);

PROCEDURE resetScreenMode(mode : screenModes);

PROCEDURE lineWrapping;

PROCEDURE resetLineWrapping;

(*
These are some examples of private modes, which are not defined by the specification, but are implemented in most terminals.
ESC Code Sequence 	Description
ESC[?25l 	make cursor invisible
ESC[?25h 	make cursor visible
ESC[?47l 	restore screen
ESC[?47h 	save screen
ESC[?1049h 	enables the alternative buffer
ESC[?1049l 	disables the alternative buffer
*)

PROCEDURE makeCursorInvisible;

PROCEDURE makeCursorVisible;

PROCEDURE restoreScreen;

PROCEDURE saveScreen;

PROCEDURE enableAlternativeBuffer;

PROCEDURE disableAlternativeBuffer;

(*
Color codes

Most terminals support 8 and 16 colors, as well as 256 (8-bit) colors. These colors are set by the user, but have commonly defined meanings.
8-16 Colors
Color Name 	Foreground Color Code 	Background Color Code
Black 	30 	40
Red 	31 	41
Green 	32 	42
Yellow 	33 	43
Blue 	34 	44
Magenta 	35 	45
Cyan 	36 	46
White 	37 	47
Default 	39 	49
Reset 	0 	0

    Note: the Reset color is the reset code that resets all colors and text effects, Use Default color to reset colors only.

Most terminals, apart from the basic set of 8 colors, also support the "bright" or "bold" colors. These have their own set of codes, mirroring the normal colors, but with an additional ;1 in their codes:

# Set style to bold, red foreground.
\x1b[1;31mHello
# Set style to dimmed white foreground with red background.
\x1b[2;37;41mWorld

Terminals that support the aixterm specification provides bright versions of the ISO colors, without the need to use the bold modifier:
Color Name 	Foreground Color Code 	Background Color Code
Bright Black 	90 	100
Bright Red 	91 	101
Bright Green 	92 	102
Bright Yellow 	93 	103
Bright Blue 	94 	104
Bright Magenta 	95 	105
Bright Cyan 	96 	106
Bright White 	97 	107
256 Colors

*)

PROCEDURE setAttribut ( attribut: Attribut; front, back : Colors);

(* 
#
#  All codes below are for use in VT52 compatibility mode.
# 
*)

PROCEDURE VT52setansi; (*OK*)
               (* Enter/exit ANSI mode (VT52)            ^[< *)

PROCEDURE VT52altkeypad;
             (* Enter alternate keypad mode            ^[= *)

PROCEDURE VT52numkeypad;
             (* Exit alternate keypad mode             ^[> *)

PROCEDURE VT52setgr; (* NOT OK*)
                 (* Use special graphics character set     ^[F *)

PROCEDURE VT52resetgr; (* NOT OK*)
               (* Use normal US/UK character set         ^[G *)

PROCEDURE VT52cursorup; (*OK*)
              (* Move cursor up one line                ^[A *)

PROCEDURE VT52cursordn; (*OK*)
              (* Move cursor down one line              ^[B *)

PROCEDURE VT52cursorrt; (*OK*)
              (* Move cursor right one char             ^[C *)

PROCEDURE VT52cursorlf; (*OK*)
              (* Move cursor left one char              ^[D *)

PROCEDURE VT52cursorhome; (*OK*)
            (* Move cursor to upper left corner       ^[H *)

PROCEDURE VT52cursorpos(x, y : CARDINAL); (* NOT OK*)
        (* Move cursor to v,h location            ^[<v><h> *)

PROCEDURE VT52revindex;
              (* Generate a reverse line-feed           ^[I *)

PROCEDURE VT52cleareol; (*OK*)
              (* Erase to end of current line           ^[K *)

PROCEDURE VT52cleareos; (*OK*)
              (* Erase to end of screen                 ^[J *)

PROCEDURE VT52ident;
                 (* Identify what the terminal is          ^[Z *)

PROCEDURE VT52identresp;
             (*Correct response to ident             ^[/Z   *) 

PROCEDURE CloseTerminal;
    (* PROCEDURE tcsetattr (fd: INTEGER; option: INTEGER; t: TERMIOS) : INTEGER ; *)

PROCEDURE InitTerminal;
(* PROCEDURE tcgetattr (fd: INTEGER; t: TERMIOS) : INTEGER ; *)


END VT100.