Classic Computer Magazine Archive COMPUTE! ISSUE 34 / MARCH 1983 / PAGE 163

Input Into Apple's EXEC

Wally Hubbard

This simulation of the INPUT command, written in Applesoft BASIC, can be used to make EXEC files take input from the Apple keyboard.

Normally, the command INPUT A$ in an EXEC file ignores the keyboard and uses the next line in the EXEC file as its input. As an example, the file

INPUT A$
INPUT B$
PRINT A$, B$

would set A$ = "INPUT B$".

Program 1 shows a text file, EXPUT, which issues a prompt and then puts the response from the keyboard into XX$. The second line then RUNs the file named by XX$. Program 2 shows an Applesoft BASIC program which can be used to make EXEC files. It could be used to enter Program 1, but because EXPUT is so long, Program 2 contains a subroutine that automatically enters EXPUT whenever you type CTRL-I.

Let me explain how EXPUT works. It uses two FOR/NEXT loops as WHILE-WENDs, which are not explicitly available in Applesoft. The FOR/NEXT loop using B keeps cycling until B = 1. B does not equal 1 until a key has been pressed. The statement B = (X>127) sets B = 1 if the statement in parentheses is true, otherwise B = 0. And X, the value at the keyboard port, is always less than 128 until a key is pressed. The B loop gets each character, and the A loop, which is around it, puts each character into XX$ until RETURN is pressed. The sequence from FLASH to NORMAL puts the flashing cursor on the screen. The segment

XX$ = LEFT$(XX$, LEN(XX$)-(X = 13)-2*(X = 8))

subtracts one character from the end of XX$ if that last character is a carriage return [CHR$(13)], two characters if it is a backspace [CHR$(8)].

If a one-character response is all that is needed, you can simulate a GET command by eliminating the segments that affect XX$ and the statements that refer to A, including the last NEXT. This will put the character in X$.

EXPUT allows use of the left-arrow (BACKSPACE) key but does not allow use of the right-arrow or ESCape functions. A RUN, LOAD, CLEAR, or NEW command will erase the contents of XX$ and the other variables.

Using Make Exec

"Make Exec" (Program 2) is a simple, general-purpose text-entry program. The familiar Apple editing features (right-arrow, left-arrow, and pure cursor moves via the ESCape key) are available. Tap the space bar twice instead of once to get out of the ESCape functions. To back up to a previous line, type CTRL-B. To go forward one line, without changing the contents of the current line, enter a RETURN as the first character on the line. When you have finished entering all of the text, enter a ! as the first character on a new line; you will be prompted for the name the file is to be saved under. If you want to resume editing, don't enter a file name, just press RETURN. If you want to exit the program, type CTRL-C.

Most of EXPUT is automatically entered on the current line when you type CTRL-I. You must designate the contents of PR$, which is used as the prompt, and if desired, use HOME and VTAB before typing CTRL-I. Keep in mind that EXPUT is long, and lines cannot exceed 255 characters. To eliminate a chance of syntax errors, EXPUT begins with a colon.

Program 1.

THE FILE ‘EXPUT’ CONSISTS OF TWO LINES. THEY ARE BROKEN INTO SEGMENTS IN THIS LISTING FOR CLARITY.
THE FIRST LINE GIVES THE PROMPT AND TAKES THE INPUT. THE SECOND EXECUTES A COMMAND USING THE INPUT AS
        A PARAMETER. (IF THE FOR-NEXT LOOPS ARE NOT ON THE SAME LINE THEY WILL NOT BE EXECUTED.)
XX$ = " " :
HOME :
VTAB 15 :
?"ENTER FILE TO BE RUN: "; :
    FOR A = 0 TO 1 :
    FLASH :
    ?" "; CHR$(8); :
    NORMAL :
    POKE-16368, 0 :
         FOR B = 0 TO 1 :
         X = PEEK (-16384) :
         B = (X> 127) :
         NEXT :
         X = X-128 :
         X$ = CHR$ (X) :         ?X$ ; :
         XX$ = XX$ + X$ :
         A = (X = 13):
         XX$ = LEFT$(XX$, LEN(XX$) - (X = 13) - 2 * (X = 8)) : NEXT
PRINT CHR$(4) ; "RUN " ; XX$

Program 2.

110 VTAB 1 : INVERSE : INPUT "CLEAR SCREEN? (Y/N) " ; A$ : NORMAL : IF LEFT$ (A$, 1) = "Y" THEN HOME
120 VTAB 5
130 DIM C * (100)
140 GOTO 320
150 REM GET EACH LETTER
160 GET A$ : PRINT A$;
170 IF A$ = CHR$ (13) AND LEN (B$) = 0 THEN A = A + 1 : CALL - 958 : PRINT : PRINT A ; " " ; C$(A)
        ; : FOR B = 0 TO LEN (C$(A)) : PRINT CHR$ (8) ; : NEXT : PRINT " " ; : GOTO 160 : REM
        GO FORWARD ONE LINE
180 IF A$ = CHR$ (13) THEN CALL - 958 : GOTO 300 : REM RETURN
190 IF A$ = CHR$ (8) AND LEN (B$) < 2 THEN B$ = "" : GOTO 160 : REM BACKSPACE IF LEN (B$) < = 1
200 IF A$ = CHR$ (8) THEN B$ = LEFT$ (B$, LEN (B$) - 1) : GOTO 160 : REM BACKSPACE IF LEN(B$) <> 1
210 IF A$ = CHR$ (21) THEN A$ = CHR$ ( PEEK (PEEK (40) + 256 * PEEK (41) + PEEK (36))) : PRINT A$
        ; : REM RIGHT-ARROW
220 IF A$ = CHR$ (27) THEN CALL - 721 : GOTO 160 : REM ESCAPE
230 IF A$ = CHR$ (2) THEN A = A - 1 : B$ = C$(A) : CALL - 958 : PRINT : PRINT A ;" " ;
        B$ ; : FOR B = 0 TO LEN (B$) : PRINT CHR$ (8) ; : NEXT : PRINT " " ; : B$ = ""
        : GOTO 160 : REM BACK UP ONE LINE
240 IF A$ = CHR$ (3) THEN STOP : GOTO 160 : REM CTRL-C
250 IF A$ = "!" THEN 340
260 IF A$ = CHR$ (9) THEN 500 : REM CTRL-I
270 B$ = B$ + A$
280 GOTO 160
290 REM STORE A LINE
300 C$(A) = B$
310 B$ = ""
320 A = A + 1 : PRINT : PRINT A ; " ";
330 GOTO 160
340 REM SAVE IT ALL
350 D$ = CHR$ (4)
360 PRINT : PRINT
370 INPUT "WHAT IS THE FILE'S NAME? " ; FL$
380 IF FL$ = "" THEN 160 : REM NULL
390 PRINT : PRINT "SAVING " ; FL$
400 PRINT D$ ; "OPEN" ; FL$
410 PRINT D$ ; "DELETE" ; FL$
420 PRINT D$ ; "OPEN" ; FL$
430 PRINT D$ ; "WRITE" ; FL$
440 FOR B = 1 TO A
450 PRINT C$(B)
460 NEXT
470 PRINT D$ ; "CLOSE" ; FL$
480 END
490 REM CTRL-I CALLS EXPUT
500 A$ = " : XX$ = " + CHR$ (34) + CHR$ (34) + " : ?PR$ ; : FOR A = 0 TO 1 : FLASH : ?
        CHR$(32) ; CHR$(8) ; : NORMAL : POKE - 16368, 0 : FOR B = 0 TO 1 : X = PEEK(-16384) :"
510 A$ = A$ + "B = (X > 127) : NEXT : X = X - 128 : X$ = CHR$(X) : ?X$ ; : XX$ = XX$ + X$ :
        A = (X = 13) : XX$ = LEFT$ (XX$, LEN(XX$) - (X = 13) -2 * (X = 8)) : NEXT"
520 PRINT A$;
530 B$ = B$ + A$
540 GOTO 160