Chapter 4
Input/Output

 4.1 Terminal I/O
 4.2 Text File I/O
 4.3 Screen Mode
 4.4 Keyboard Facilities in Screen Mode

4.1 Terminal I/O

We have already met the PRINT command for output to the terminal. The analogous commands for terminal input are called INPUT, INPUTR, INPUTI and INPUTL. INPUT reads a line of text from the terminal into a string variable and has the form:

  
     ICL> INPUT Enter your name>  (name)
  

The last parameter must specify a variable in which the input will be returned and must therefore be in parentheses. The earlier parameters form a prompt string.

INPUTR, INPUTI and INPUTL are used to input real, integer or logical values. A single line of text may be used to supply values for more than one variables so these commands have the form:

  
     ICL> INPUTR  Prompt  (X)  (Y)  (Z)
  

Only the first parameter is used to provide the prompt string, so if it has spaces in it the string must be enclosed in quotes.

INPUTL will accept values of TRUE, FALSE, YES and NO in either upper or lower case, as well as abbreviations.

4.2 Text File I/O

ICL can also read and write text files. In order to access files they must first be opened using one of the commands CREATE, OPEN or APPEND.

  
      CREATE MYFILE
  

will create a file called MYFILE and open it for output. MYFILE is the name used within ICL for the file. The file will appear in your default VMS directory as MYFILE.DAT.

The VMS file name may be specified explicitly by adding a second parameter to the CREATE, OPEN or APPEND command.

  
      OPEN  INFILE  DISK$DATA:[ABC]FOR008.DAT
  

opens an existing file for input and the file is known internally as INFILE.

The APPEND command opens an existing file for output. Anything written to the file is appended to the existing contents.

A line of text is written to a text file with the WRITE command. WRITE is similar to PRINT, the only difference being that its first parameter specifies the internal name of the file to which the data will be written.

Text is read from files with the commands READ, READR, READI and READL. These are analogous to the INPUT commands for terminal input. The first parameter specifies the internal name of the file. READ reads a line of text into a single string variable. The other commands read one or more real, integer or logical values.

When a file is no longer required it may be closed using the CLOSE command which has a single parameter, the internal name of the file.

The following example uses these procedures to read an input file containing three real numbers in free format, and output the same numbers as a formatted table.

      PROC REFORMAT
  
      {  Open input file and create output file  }
  
          OPEN INFILE
          CREATE OUTFILE
  
      {  Loop copying lines from input to output  }
  
          LOOP
             READR INFILE (R1) (R2) (R3)
             WRITE OUTFILE (R1:10:2) (R2:10:2) (R3:10:2)
          END LOOP
  
      END PROC
  

Note that no specific test for completion of the loop is included. When an end of file condition is detected on the input file the procedure will exit and return to the ICL > prompt with an appropriate message. 1

4.3 Screen Mode

ICL’s screen mode allows more control over the terminal screen than is possible in the normal mode. It also allows more control over the use of the keyboard. Screen mode is implemented using the DEC screen management (SMG$) routines of the run time library, and will work on any terminal compatible with these routines.

Screen mode is selected by the SET SCREEN command. In screen mode the terminal screen is divided into an upper fixed region and a lower scrolling region. The size of the scrolling region may be specified by an optional parameter to SET SCREEN.

  
      ICL> SET SCREEN 10
  

will select 10 lines of scrolling region. The size of the scrolling region can be changed by further SET SCREEN commands. SET NOSCREEN is used to leave screen mode and return to normal mode.

Standard terminal I/O operations work exactly as normal in the scrolling region of the screen. However, an additional facility is the ability to examine text which has scrolled off the top of the scrolling region. The Next Screen and Prev Screen keys on a VT200 (or CTRL/N, CTRL/P on other terminals) may be used to move through the text. Any output since screen mode was started is available in this way.

To write to the fixed part of the screen the command LOCATE is used.

  
      LOCATE 6 10   This text will be written starting at Row 6 Column 10
  

The first two parameters specify the row and column at which the text will start. The remaining parameters form the text to be written.

The SET ATTRIBUTES command provides further control over text written with the LOCATE command. This command has a parameter string composed of any combination of the letters R (Reverse Video), B (Bold), U (Underlined), F (Flashing) and D (Double Size). These attributes apply to all LOCATE commands until the next SET ATTRIBUTES command. SET ATTRIBUTES with no parameter gives normal text.

The CLEAR command is used to clear all or part of the fixed region of the screen. For example:

      CLEAR 6 10

clears lines 6 to 10 of the screen.

4.4 Keyboard Facilities in Screen Mode

The KEY command may be used to define an equivalence string for any key on the keyboard. For example:

      KEY  PF1  PROCS#

defines the PF1 key so as to issue the PROCS command. The # character is used to indicate a Return character in the equivalence string.

The name of a main keyboard key may be specified as a single character or as an integer representing the ASCII code for the key. Keypad of Function keys are specified by names as follows:

Keypad keys — PF1, PF2, PF3, PF4, KP0, KP1, KP2, KP3, KP4, KP5, KP6, KP7, KP8, KP9, ENTER, MINUS, COMMA, PERIOD.

Function Keys (VT200) — F6, F7, F8, F9, F10, F11, F12, F13, F14, HELP, DO, F17, F18, F19, F20.

Editing Keypad (VT200) — FIND, INSERT_HERE, REMOVE, SELECT, PREV_SCREEN, NEXT_SCREEN.

Cursor Keys — UP, DOWN, LEFT, RIGHT.

KEYTRAP and INKEY allow an ICL procedure to test for keyboard input during its execution, without having to issue an INPUT command and thus wait for input to complete. KEYTRAP specifies the name of a key to be trapped. INKEY is an integer function which returns zero if no key has been pressed or the key value if a key has been pressed since the last call. The key value is the ASCII value for ASCII characters, or a number between 256 and 511 for keypad and function keys. The KEYVAL function may be used to obtain the value from the key name.

  
  {  Trap ENTER and LEFT and RIGHT arrow keys  }
  
      KEYTRAP ENTER
      KEYTRAP LEFT
      KEYTRAP RIGHT
  
      LOOP
  
        K = INKEY()
        IF K = KEYVAL(’ENTER’) THEN
        .
        ELSE IF K = KEYVAL(’LEFT’) THEN
        .
        ELSE IF K = KEYVAL(’RIGHT’) THEN
        .
        ELSE
        .
        ENDIF
  
      END LOOP
  

The command

  
   KEYOFF keyname
  

may be used to turn off trapping of a key.

1A tidier exit can be arranged by using an exception handler for the EOF exception.