File:  [gforth] / gforth / kernel / io.fs
Revision 1.34: download - view: text, annotated - select for diffs
Mon Dec 31 15:25:19 2012 UTC (9 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright year

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

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