Imported Upstream version 21
[debian/pforth] / csrc / pf_io.c
1 /* @(#) pf_io.c 96/12/23 1.12 */
2 /***************************************************************
3 ** I/O subsystem for PForth based on 'C'
4 **
5 ** Author: Phil Burk
6 ** Copyright 1994 3DO, Phil Burk, Larry Polansky, Devid Rosenboom
7 **
8 ** The pForth software code is dedicated to the public domain,
9 ** and any third party may reproduce, distribute and modify
10 ** the pForth software code or any derivative works thereof
11 ** without any compensation or license.  The pForth software
12 ** code is provided on an "as is" basis without any warranty
13 ** of any kind, including, without limitation, the implied
14 ** warranties of merchantability and fitness for a particular
15 ** purpose and their equivalents under the laws of any jurisdiction.
16 **
17 ****************************************************************
18 ** 941004 PLB Extracted IO calls from pforth_main.c
19 ***************************************************************/
20
21 #include "pf_all.h"
22
23 /***************************************************************
24 ** Send single character to output stream.
25 */
26 void ioEmit( char c )
27 {
28         int32 Result;
29         Result = sdTerminalOut(c);
30         
31         if( Result < 0 ) EXIT(1);
32         if(c == '\n')
33         {
34                 gCurrentTask->td_OUT = 0;
35                 sdTerminalFlush();
36         }
37         else
38         {
39                 gCurrentTask->td_OUT++;
40         }
41 }
42
43 void ioType( const char *s, int32 n )
44 {
45         int32 i;
46
47         for( i=0; i<n; i++)
48         {
49                 ioEmit ( *s++ );
50         }
51 }
52
53 /***************************************************************
54 ** Return single character from input device, always keyboard.
55 */
56 cell ioKey( void )
57 {
58         return sdTerminalIn();
59 }
60
61 /**************************************************************
62 ** Receive line from input stream.
63 ** Return length, or -1 for EOF.
64 */
65 #define BACKSPACE  (8)
66 cell ioAccept( char *Target, cell MaxLen, FileStream *stream )
67 {
68         int32 c;
69         int32 Len;
70         char *p;
71
72 DBUGX(("ioAccept(0x%x, 0x%x, 0x%x)\n", Target, Len, stream ));
73         p = Target;
74         Len = MaxLen;
75         while(Len > 0)
76         {
77                 if( stream == PF_STDIN )
78                 {
79                         c = ioKey();
80 /* If KEY does not echo, then echo here. If using getchar(), KEY will echo. */
81 #ifndef PF_KEY_ECHOS
82                         ioEmit( c );
83                         if( c == '\r') ioEmit('\n'); /* Send LF after CR */
84 #endif
85                 }
86                 else
87                 {
88                         c = sdInputChar(stream);
89                 }
90                 switch(c)
91                 {
92                         case EOF:
93                                 DBUG(("EOF\n"));
94                                 return -1;
95                                 break;
96                                 
97                         case '\r':
98                         case '\n':
99                                 *p++ = (char) c;
100                                 DBUGX(("EOL\n"));
101                                 goto gotline;
102                                 break;
103                                 
104                         case BACKSPACE:
105                                 if( Len < MaxLen )  /* Don't go beyond beginning of line. */
106                                 {
107                                         EMIT(' ');
108                                         EMIT(BACKSPACE);
109                                         p--;
110                                         Len++;
111                                 }
112                                 break;
113                                 
114                         default:
115                                 *p++ = (char) c;
116                                 Len--;
117                                 break;
118                 }
119                 
120         }
121 gotline:
122         *p = '\0';
123                 
124         return pfCStringLength( Target );
125 }
126
127 #define UNIMPLEMENTED(name) { MSG(name); MSG("is unimplemented!\n"); }
128
129 #ifdef PF_NO_CHARIO
130 int  sdTerminalOut( char c )
131 {
132         TOUCH(c);
133         return 0;
134 }
135 int  sdTerminalIn( void )
136 {
137         return -1;
138 }
139 int  sdTerminalFlush( void )
140 {
141         return -1;
142 }
143 #endif
144
145 /***********************************************************************************/
146 #ifdef PF_NO_FILEIO
147
148 /* Provide stubs for standard file I/O */
149
150 FileStream *PF_STDIN;
151 FileStream *PF_STDOUT;
152
153 int32  sdInputChar( FileStream *stream )
154 {
155         UNIMPLEMENTED("sdInputChar");
156         TOUCH(stream);
157         return -1;
158 }
159
160 FileStream *sdOpenFile( const char *FileName, const char *Mode )
161 {
162         UNIMPLEMENTED("sdOpenFile");
163         TOUCH(FileName);
164         TOUCH(Mode);
165         return NULL;
166 }
167 int32 sdFlushFile( FileStream * Stream  )
168 {
169         TOUCH(Stream);
170         return 0;
171 }
172 int32 sdReadFile( void *ptr, int32 Size, int32 nItems, FileStream * Stream  ) 
173
174         UNIMPLEMENTED("sdReadFile");
175         TOUCH(ptr);
176         TOUCH(Size);
177         TOUCH(nItems);
178         TOUCH(Stream);
179         return 0; 
180 }
181 int32 sdWriteFile( void *ptr, int32 Size, int32 nItems, FileStream * Stream  )
182
183         UNIMPLEMENTED("sdWriteFile");
184         TOUCH(ptr);
185         TOUCH(Size);
186         TOUCH(nItems);
187         TOUCH(Stream);
188         return 0; 
189 }
190 int32 sdSeekFile( FileStream * Stream, int32 Position, int32 Mode ) 
191
192         UNIMPLEMENTED("sdSeekFile");
193         TOUCH(Stream);
194         TOUCH(Position);
195         TOUCH(Mode);
196         return 0; 
197 }
198 int32 sdTellFile( FileStream * Stream ) 
199
200         UNIMPLEMENTED("sdTellFile");
201         TOUCH(Stream);
202         return 0; 
203 }
204 int32 sdCloseFile( FileStream * Stream ) 
205
206         UNIMPLEMENTED("sdCloseFile");
207         TOUCH(Stream);
208         return 0; 
209 }
210 #endif
211