File:  [gforth] / gforth / kernel / io.fs
Revision 1.30: download - view: text, annotated - select for diffs
Mon Dec 31 18:40:26 2007 UTC (16 years, 3 months ago) by anton
Branches: MAIN
CVS tags: v0-7-0, HEAD
updated copyright notices for GPL v3

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

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