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