[gforth] / gforth / kernel / io.fs  

gforth: gforth/kernel/io.fs


1 : anton 1.1 \ input output basics (extra since) 02mar97jaw
2 :    
3 : anton 1.29 \ Copyright (C) 1995,1996,1997,1998,2000,2003,2006,2007 Free Software Foundation, Inc.
4 : anton 1.1
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 : anton 1.21 \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20 : anton 1.1
21 : jwilke 1.12 require ./basics.fs
22 :    
23 : jwilke 1.2 \ Output 13feb93py
24 :    
25 : jwilke 1.3 has? os [IF]
26 : jwilke 1.2 0 Value outfile-id ( -- file-id ) \ gforth
27 : pazsan 1.5 0 Value infile-id ( -- file-id ) \ gforth
28 : pazsan 1.7
29 : jwilke 1.2 : (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 : pazsan 1.5
37 :     : (key) ( -- c ) \ gforth
38 :     infile-id key-file ;
39 :    
40 :     : (key?) ( -- flag ) \ gforth
41 :     infile-id key?-file ;
42 : jwilke 1.2 [THEN]
43 :    
44 : pazsan 1.7 undef-words
45 : pazsan 1.13
46 : pazsan 1.7 Defer type ( c-addr u -- ) \ core
47 : crook 1.9 \G If @var{u}>0, display @var{u} characters from a string starting
48 :     \G with the character stored at @var{c-addr}.
49 : pazsan 1.13 [IFDEF] write-file
50 :     : (type) 0 write-file drop ;
51 :     [ELSE]
52 : crook 1.9 : (type) BEGIN dup WHILE
53 :     >r dup c@ (emit) 1+ r> 1- REPEAT 2drop ;
54 : pazsan 1.13 [THEN]
55 : pazsan 1.4
56 : pazsan 1.7 [IFDEF] (type) ' (type) IS Type [THEN]
57 : jwilke 1.2
58 :     Defer emit ( c -- ) \ core
59 : crook 1.8 \G Display the character associated with character value c.
60 : pazsan 1.7 : (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 : jwilke 1.2
66 : crook 1.10 Defer key ( -- char ) \ core
67 :     \G Receive (but do not display) one character, @var{char}.
68 : pazsan 1.28 : (key) ( -- c ) \ gforth
69 :     infile-id key-file ;
70 :     : infile-id stdin ;
71 : pazsan 1.7
72 :     [IFDEF] (key) ' (key) IS key [THEN]
73 : pazsan 1.5
74 : crook 1.14 Defer key? ( -- flag ) \ facility key-question
75 : crook 1.10 \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 : anton 1.19 \G calls to @code{key?} before calling @code{key} or @code{ekey} will
79 : crook 1.10 \G also return true.
80 : pazsan 1.28 : (key?) ( -- flag ) \ gforth
81 :     infile-id key?-file ;
82 :     : infile-id stdin ;
83 : pazsan 1.7
84 :     [IFDEF] (key?) ' (key?) IS key? [THEN]
85 :    
86 :     all-words
87 : jwilke 1.2
88 :     : (.") "lit count type ;
89 :     : (S") "lit count ;
90 :    
91 : anton 1.1 \ Input 13feb93py
92 :    
93 : pazsan 1.25 04 constant #eof ( -- c ) \ gforth
94 : anton 1.1 07 constant #bell ( -- c ) \ gforth
95 :     08 constant #bs ( -- c ) \ gforth
96 :     09 constant #tab ( -- c ) \ gforth
97 :     7F constant #del ( -- c ) \ gforth
98 :     0D constant #cr ( -- c ) \ gforth
99 :     \ the newline key code
100 :     0C constant #ff ( -- c ) \ gforth
101 :     0A constant #lf ( -- c ) \ gforth
102 :    
103 : jwilke 1.3 : bell #bell emit [ has? os [IF] ] outfile-id flush-file drop [ [THEN] ] ;
104 : crook 1.14 : cr ( -- ) \ core c-r
105 : anton 1.17 \G Output a newline (of the favourite kind of the host OS). Note
106 :     \G that due to the way the Forth command line interpreter inserts
107 :     \G newlines, the preferred way to use @code{cr} is at the start
108 :     \G of a piece of text; e.g., @code{cr ." hello, world"}.
109 :     newline type ;
110 : anton 1.1
111 : crook 1.8 : space ( -- ) \ core
112 :     \G Display one space.
113 :     bl emit ;
114 :    
115 : pazsan 1.27 has? os 0= [IF]
116 : crook 1.8 : spaces ( n -- ) \ core
117 :     \G If n > 0, display n spaces.
118 :     0 max 0 ?DO space LOOP ;
119 : pazsan 1.7 : backspaces 0 max 0 ?DO #bs emit LOOP ;
120 :     [ELSE]
121 : anton 1.1 \ space spaces 21mar93py
122 :     decimal
123 :     Create spaces ( u -- ) \ core
124 : anton 1.18 \G Display @var{n} spaces.
125 : crook 1.8 bl 80 times \ times from target compiler! 11may93jaw
126 : anton 1.1 DOES> ( u -- )
127 : crook 1.8 swap
128 :     0 max 0 ?DO I' I - &80 min 2dup type +LOOP drop ;
129 : anton 1.1 Create backspaces
130 : crook 1.8 08 80 times \ times from target compiler! 11may93jaw
131 : anton 1.1 DOES> ( u -- )
132 : crook 1.8 swap
133 :     0 max 0 ?DO I' I - &80 min 2dup type +LOOP drop ;
134 : anton 1.1 hex
135 :     [THEN]
136 :    

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help