From:	CSBVAX::MRGATE!t_wade@ccvax.ucd.ie@SMTP 16-FEB-1988 06:41
To:	ARISIA::EVERHART
Subj:	Reading Chars


Received: from CUNYVM.CUNY.EDU by KL.SRI.COM with TCP; Fri 12 Feb 88 12:01:55-PST
Received: from IRLEARN.BITNET by CUNYVM.CUNY.EDU ; Fri, 12 Feb 88 15:01:50 EST
Received: by IRLEARN (Mailer X1.24) id 5006; Fri, 12 Feb 88 18:21:20 GMT
Received: By IRLEARN(GREYRECV 2.5) Greybook Gateway  12 Feb 1988 18:21:19 GMT
Date:     12-FEB-1988 18:20:50 GMT
From:      <T_WADE@CCVAX.UCD.IE>
To:       info-vax@kl.sri.com
Subject:    Reading Chars
sender:    hea"t_wade@ccvax.ucd.ie"  <t_wade@ccvax.ucd.ie>

Greetings.

There were two separate requests for routines to read characters from
a terminal. The first wanted something equivalent to BASIC's INKEY
(I think) function, i.e. wait for and read a keystroke WITHOUT waiting
for the carriage return. The second wanted a routine to check to see
if a key had been pressed (presumably don't wait around if one hasn't
been).  I just happen to have routines that will do just that, and
I include them below.  In return, perhaps someone can satisfy my
curiosity. Why are "symbionts" so called ?

-------------------------------------------------------------------------
Tom Wade                      Heanet:   T-Wade@ccvax.ucd.ie
Systems Programmer            Ean:      t-wade@ccvax.ucd.irl
Computer Center               PSI:      PSI%+27243154000712::T-WADE
University College Belfield   Telex:    (0500) 91196 UCD EI
Dublin 4.                     Uucp:     t-wade%ccvax.ucd.ie@tcdcs.uucp
Ireland                      Bitnet:    twade@irlearn.bitnet
                              Voice:    +353-1-693244 Ext 2456
                             ICBMnet:   53 18 20 N; 06 13 38 W; 25m
-------------------------------------------------------------------------
"How many Systems Programmers are needed to change a light bulb ?
 None -- a *real* systems programmer would patch the old one."

Routines:

        READ_KEY (code)

Code:    Longword passed by reference. Returns the ASCII code of the
    character typed.

Return Code:    SS$_Normal or any code returned by $QIO service.

Description:    READ_KEY will wait until the user presses a key. It will
        not echo that character, and will not wait for CR. It will
        not detect CTRL/C CTRL/Y CTRL/O CTRL/T (if SET CONTROL=T
        is on) CTRL/Y CTRL/S (if XON/XOFF is enabled) CTRL/Q (as
        CTRL/S). It will work for all printables, and most control
        characters such as DEL, CTRL/U CTRL/X CTRL/R and NULL.

        CHECK_KEY (Code)

Code:    Longword passed by reference. Returns the ASCII code of the
    character, if one was typed.

Return code:    SS$_Normal is a char was typed,  0 if not.

Description:    Returns (without echo or CR confirmation) the ASCII code
        of the char typed in. If no char has been typed, then
        it will return 0 in the status return.

Modifications:    You might want the routine to wait for a certain number
        of seconds before deciding no char is to be returned. If
        so, change the p3 parameter of the $QIO call. As distributed,
        the char will only be returned if it already is in the
        typeahead buffer.  You might want to purge the typeahead
        before checking in that case. You can do this by the
        following:

        $QIOW_S -
            chan = ttchan, -
            func = #<IO$_Readvblk!IO$M_Purge>, - ; clear buffer.
            p1 = #0, -        ; input buffer not needed.
            p2 = #0            ; read no characters.

------- Cut Here X-------------------------------------------------------
;****************************************************************************
;   Routines to read characters from terminal.

Char:        .Byte    0
Iosb:        .Quad    0
Ttchan:        .Long    0
Tty:        .Ascid    "SYS$COMMAND"


;****************************************************************************
;   Read a single character from the keyboard. Wait for the character to
;   be typed. Do not echo it, and return its ASCII value in the first
;   longword argument.

        .Entry    READ_KEY    M <>

        Tstl    ttchan            ; channel open ?
        Bneq    get_key            ; yes.
        Bsbw    OPEN_CHAN        ; no - open it.

Get_key:
        $QIOW_S -
            chan = ttchan, -
            func = #<IO$_Readvblk!IO$M_Noecho!IO$M_Nofiltr>, -
            iosb = iosb, -
            p1 = char, -
            p2 = #1            ; read only 1 char.

        Blbc    R0,    exit        ; check for errors.
        Movzwl    iosb,    R0
        Blbc    R0,    exit

        Movl    #SS$_Normal,    R0    ; indicate success.
        Movzbl    char,    @4(AP)        ; return char value.

Exit:        Ret


;****************************************************************************
;   Read a character from the keyboard ONLY if one has been typed. If not,
;   return a zero code.

        .Entry    CHECK_KEY    M <>

        Tstl    ttchan            ; channel open ?
        Bneq    test_key        ; yes.
        Bsbw    OPEN_CHAN        ; no - open it.

Test_key:
        $QIOW_S -
            chan = ttchan, -
             func = #<IO$_Readvblk!IO$M_Noecho!IO$M_Timed!IO$M_NOFILTR>, -
            iosb = iosb, -
            p1 = char, -
            p2 = #1, -        ; only 1 char.
            p3 = #0            ; timeout of zero seconds.
                        ; (only get typeahead).
        Blbc    iosb,    nothing

        Movzbl    char,    @4(AP)
        Movl    #SS$_Normal,    R0    ; function return in R0
        Ret

Nothing:    Clrl    R0            ; return false
        Ret


;****************************************************************************
;   Subroutine to open channel to terminal.

OPEN_CHAN:
        $ASSIGN_S -            ; open a channel to TTY
            chan = ttchan, -
            devnam = tty
        Rsb

        .End

