File:  [gforth] / gforth / kernel / io.fs
Revision 1.11: download - view: text, annotated - select for diffs
Wed May 5 12:07:11 1999 UTC (24 years, 11 months ago) by jwilke
Branches: MAIN
CVS tags: HEAD
deleted doubled definitions of (key) and (key?)

    1: \ input output basics				(extra since)	02mar97jaw
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
    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: 
   21: \ Output                                               13feb93py
   22: 
   23: has? os [IF]
   24: 0 Value outfile-id ( -- file-id ) \ gforth
   25: 0 Value infile-id ( -- file-id ) \ gforth
   26:     
   27: : (type) ( c-addr u -- ) \ gforth
   28:     outfile-id write-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
   29: ;
   30: 
   31: : (emit) ( c -- ) \ gforth
   32:     outfile-id emit-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
   33: ;
   34: 
   35: : (key) ( -- c ) \ gforth
   36:     infile-id key-file ;
   37: 
   38: : (key?) ( -- flag ) \ gforth
   39:     infile-id key?-file ;
   40: [THEN]
   41: 
   42: undef-words
   43:     
   44: Defer type ( c-addr u -- ) \ core
   45:   \G If @var{u}>0, display @var{u} characters from a string starting
   46:   \G with the character stored at @var{c-addr}.
   47: : (type) BEGIN dup WHILE
   48:     >r dup c@ (emit) 1+ r> 1- REPEAT 2drop ;
   49: 
   50: [IFDEF] (type) ' (type) IS Type [THEN]
   51: 
   52: Defer emit ( c -- ) \ core
   53:   \G Display the character associated with character value c.
   54: : (emit) ( c -- ) \ gforth
   55:     0 emit-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
   56: ;
   57: 
   58: [IFDEF] (emit) ' (emit) IS emit [THEN]
   59: 
   60: Defer key ( -- char ) \ core
   61: \G Receive (but do not display) one character, @var{char}.
   62: 
   63: [IFDEF] (key) ' (key) IS key [THEN]
   64: 
   65: Defer key? ( -- flag ) \ facility
   66: \G Determine whether a character is available. If a character is
   67: \G available, @var{flag} is true; the next call to @code{key} will
   68: \G yield the character. Once @code{key?} returns true, subsequent
   69: \G calls to @code{key?} without calling @code{key} or @code{ekey} will
   70: \G also return true.
   71: 
   72: [IFDEF] (key?) ' (key?) IS key? [THEN]
   73: 
   74: all-words
   75: 
   76: : (.")     "lit count type ;
   77: : (S")     "lit count ;
   78: 
   79: \ Input                                                13feb93py
   80: 
   81: 07 constant #bell ( -- c ) \ gforth
   82: 08 constant #bs ( -- c ) \ gforth
   83: 09 constant #tab ( -- c ) \ gforth
   84: 7F constant #del ( -- c ) \ gforth
   85: 0D constant #cr   ( -- c ) \ gforth
   86: \ the newline key code
   87: 0C constant #ff ( -- c ) \ gforth
   88: 0A constant #lf ( -- c ) \ gforth
   89: 
   90: : bell  #bell emit [ has? os [IF] ] outfile-id flush-file drop [ [THEN] ] ;
   91: : cr ( -- ) \ core
   92:   \G Output a carriage-return and (if appropriate for the host operating system)
   93:   \G a line feed.
   94: [ has? crlf [IF] ]	#cr emit #lf emit 
   95: [ [ELSE] ]		#lf emit
   96: [ [THEN] ]
   97:     ;
   98: 
   99: : space ( -- ) \ core
  100:   \G Display one space.
  101:   bl emit ;
  102: 
  103: has? ec [IF]
  104: : spaces ( n -- ) \ core
  105:   \G If n > 0, display n spaces. 
  106:   0 max 0 ?DO space LOOP ;
  107: : backspaces  0 max 0 ?DO  #bs emit  LOOP ;
  108: [ELSE]
  109: \ space spaces		                                21mar93py
  110: decimal
  111: Create spaces ( u -- ) \ core
  112:   \G If @var{n} > 0, display @var{n} spaces. 
  113:   bl 80 times \ times from target compiler! 11may93jaw
  114: DOES>   ( u -- )
  115:   swap
  116:   0 max 0 ?DO  I' I - &80 min 2dup type  +LOOP  drop ;
  117: Create backspaces
  118:   08 80 times \ times from target compiler! 11may93jaw
  119: DOES>   ( u -- )
  120:   swap
  121:   0 max 0 ?DO  I' I - &80 min 2dup type  +LOOP  drop ;
  122: hex
  123: [THEN]
  124: 

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