Annotation of gforth/kernel/io.fs, revision 1.12

1.1       anton       1: \ input output basics                          (extra since)   02mar97jaw
                      2: 
1.6       anton       3: \ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
1.1       anton       4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
                     19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     20: 
1.12    ! jwilke     21: require ./basics.fs
        !            22: 
1.2       jwilke     23: \ Output                                               13feb93py
                     24: 
1.3       jwilke     25: has? os [IF]
1.2       jwilke     26: 0 Value outfile-id ( -- file-id ) \ gforth
1.5       pazsan     27: 0 Value infile-id ( -- file-id ) \ gforth
1.7       pazsan     28:     
1.2       jwilke     29: : (type) ( c-addr u -- ) \ gforth
                     30:     outfile-id write-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
                     31: ;
                     32: 
                     33: : (emit) ( c -- ) \ gforth
                     34:     outfile-id emit-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
                     35: ;
1.5       pazsan     36: 
                     37: : (key) ( -- c ) \ gforth
                     38:     infile-id key-file ;
                     39: 
                     40: : (key?) ( -- flag ) \ gforth
                     41:     infile-id key?-file ;
1.2       jwilke     42: [THEN]
                     43: 
1.7       pazsan     44: undef-words
                     45:     
                     46: Defer type ( c-addr u -- ) \ core
1.9       crook      47:   \G If @var{u}>0, display @var{u} characters from a string starting
                     48:   \G with the character stored at @var{c-addr}.
                     49: : (type) BEGIN dup WHILE
                     50:     >r dup c@ (emit) 1+ r> 1- REPEAT 2drop ;
1.4       pazsan     51: 
1.7       pazsan     52: [IFDEF] (type) ' (type) IS Type [THEN]
1.2       jwilke     53: 
                     54: Defer emit ( c -- ) \ core
1.8       crook      55:   \G Display the character associated with character value c.
1.7       pazsan     56: : (emit) ( c -- ) \ gforth
                     57:     0 emit-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
                     58: ;
                     59: 
                     60: [IFDEF] (emit) ' (emit) IS emit [THEN]
1.2       jwilke     61: 
1.10      crook      62: Defer key ( -- char ) \ core
                     63: \G Receive (but do not display) one character, @var{char}.
1.7       pazsan     64: 
                     65: [IFDEF] (key) ' (key) IS key [THEN]
1.5       pazsan     66: 
1.10      crook      67: Defer key? ( -- flag ) \ facility
                     68: \G Determine whether a character is available. If a character is
                     69: \G available, @var{flag} is true; the next call to @code{key} will
                     70: \G yield the character. Once @code{key?} returns true, subsequent
                     71: \G calls to @code{key?} without calling @code{key} or @code{ekey} will
                     72: \G also return true.
1.7       pazsan     73: 
                     74: [IFDEF] (key?) ' (key?) IS key? [THEN]
                     75: 
                     76: all-words
1.2       jwilke     77: 
                     78: : (.")     "lit count type ;
                     79: : (S")     "lit count ;
                     80: 
1.1       anton      81: \ Input                                                13feb93py
                     82: 
                     83: 07 constant #bell ( -- c ) \ gforth
                     84: 08 constant #bs ( -- c ) \ gforth
                     85: 09 constant #tab ( -- c ) \ gforth
                     86: 7F constant #del ( -- c ) \ gforth
                     87: 0D constant #cr   ( -- c ) \ gforth
                     88: \ the newline key code
                     89: 0C constant #ff ( -- c ) \ gforth
                     90: 0A constant #lf ( -- c ) \ gforth
                     91: 
1.3       jwilke     92: : bell  #bell emit [ has? os [IF] ] outfile-id flush-file drop [ [THEN] ] ;
1.1       anton      93: : cr ( -- ) \ core
1.8       crook      94:   \G Output a carriage-return and (if appropriate for the host operating system)
                     95:   \G a line feed.
1.3       jwilke     96: [ has? crlf [IF] ]     #cr emit #lf emit 
1.1       anton      97: [ [ELSE] ]             #lf emit
                     98: [ [THEN] ]
                     99:     ;
                    100: 
1.8       crook     101: : space ( -- ) \ core
                    102:   \G Display one space.
                    103:   bl emit ;
                    104: 
1.7       pazsan    105: has? ec [IF]
1.8       crook     106: : spaces ( n -- ) \ core
                    107:   \G If n > 0, display n spaces. 
                    108:   0 max 0 ?DO space LOOP ;
1.7       pazsan    109: : backspaces  0 max 0 ?DO  #bs emit  LOOP ;
                    110: [ELSE]
1.1       anton     111: \ space spaces                                         21mar93py
                    112: decimal
                    113: Create spaces ( u -- ) \ core
1.9       crook     114:   \G If @var{n} > 0, display @var{n} spaces. 
1.8       crook     115:   bl 80 times \ times from target compiler! 11may93jaw
1.1       anton     116: DOES>   ( u -- )
1.8       crook     117:   swap
                    118:   0 max 0 ?DO  I' I - &80 min 2dup type  +LOOP  drop ;
1.1       anton     119: Create backspaces
1.8       crook     120:   08 80 times \ times from target compiler! 11may93jaw
1.1       anton     121: DOES>   ( u -- )
1.8       crook     122:   swap
                    123:   0 max 0 ?DO  I' I - &80 min 2dup type  +LOOP  drop ;
1.1       anton     124: hex
                    125: [THEN]
                    126: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>