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 |
42 | int32 Result;\r |
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 |
64 | void ioType( const char *s, int32 n )\r |
65 | {\r |
66 | int32 i;\r |
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 |
77 | cell ioKey( void )\r |
78 | {\r |
79 | cell c;\r |
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 |
90 | #define BACKSPACE (8)\r |
91 | #define DELETE (0x7F)\r |
92 | cell ioAccept( char *buffer, cell maxChars )\r |
93 | {\r |
94 | int c;\r |
95 | int len;\r |
96 | char *p;\r |
97 | \r |
98 | DBUGX(("ioAccept(0x%x, 0x%x)\n", buffer, len ));\r |
99 | \r |
100 | sdEnableInput();\r |
101 | \r |
102 | p = buffer;\r |
103 | len = 0;\r |
104 | while(len < maxChars)\r |
105 | {\r |
106 | c = sdTerminalIn();\r |
107 | switch(c)\r |
108 | {\r |
109 | case '\r':\r |
110 | case '\n':\r |
111 | DBUGX(("EOL\n"));\r |
112 | goto gotline;\r |
113 | break;\r |
114 | \r |
115 | case BACKSPACE:\r |
116 | case DELETE:\r |
117 | if( len > 0 ) /* Don't go beyond beginning of line. */\r |
118 | {\r |
119 | EMIT(BACKSPACE);\r |
120 | EMIT(' ');\r |
121 | EMIT(BACKSPACE);\r |
122 | p--;\r |
123 | len--;\r |
124 | }\r |
125 | break;\r |
126 | \r |
127 | default:\r |
128 | sdTerminalEcho( (char) c );\r |
129 | *p++ = (char) c;\r |
130 | len++;\r |
131 | break;\r |
132 | }\r |
133 | \r |
134 | }\r |
135 | \r |
136 | gotline:\r |
137 | sdDisableInput();\r |
138 | \r |
139 | /* NUL terminate line to simplify printing when debugging. */\r |
140 | if( len < maxChars ) p[len] = '\0';\r |
141 | \r |
142 | return len;\r |
143 | }\r |
144 | \r |
145 | #define UNIMPLEMENTED(name) { MSG(name); MSG("is unimplemented!\n"); }\r |
146 | \r |
147 | \r |
148 | /***********************************************************************************/\r |
149 | /*********** File I/O **************************************************************/\r |
150 | /***********************************************************************************/\r |
151 | #ifdef PF_NO_FILEIO\r |
152 | \r |
153 | /* Provide stubs for standard file I/O */\r |
154 | \r |
155 | FileStream *PF_STDIN;\r |
156 | FileStream *PF_STDOUT;\r |
157 | \r |
158 | int32 sdInputChar( FileStream *stream )\r |
159 | {\r |
160 | UNIMPLEMENTED("sdInputChar");\r |
161 | TOUCH(stream);\r |
162 | return -1;\r |
163 | }\r |
164 | \r |
165 | FileStream *sdOpenFile( const char *FileName, const char *Mode )\r |
166 | {\r |
167 | UNIMPLEMENTED("sdOpenFile");\r |
168 | TOUCH(FileName);\r |
169 | TOUCH(Mode);\r |
170 | return NULL;\r |
171 | }\r |
172 | int32 sdFlushFile( FileStream * Stream )\r |
173 | {\r |
174 | TOUCH(Stream);\r |
175 | return 0;\r |
176 | }\r |
177 | int32 sdReadFile( void *ptr, int32 Size, int32 nItems, FileStream * Stream ) \r |
178 | { \r |
179 | UNIMPLEMENTED("sdReadFile");\r |
180 | TOUCH(ptr);\r |
181 | TOUCH(Size);\r |
182 | TOUCH(nItems);\r |
183 | TOUCH(Stream);\r |
184 | return 0; \r |
185 | }\r |
186 | int32 sdWriteFile( void *ptr, int32 Size, int32 nItems, FileStream * Stream )\r |
187 | { \r |
188 | UNIMPLEMENTED("sdWriteFile");\r |
189 | TOUCH(ptr);\r |
190 | TOUCH(Size);\r |
191 | TOUCH(nItems);\r |
192 | TOUCH(Stream);\r |
193 | return 0; \r |
194 | }\r |
195 | int32 sdSeekFile( FileStream * Stream, int32 Position, int32 Mode ) \r |
196 | { \r |
197 | UNIMPLEMENTED("sdSeekFile");\r |
198 | TOUCH(Stream);\r |
199 | TOUCH(Position);\r |
200 | TOUCH(Mode);\r |
201 | return 0; \r |
202 | }\r |
203 | int32 sdTellFile( FileStream * Stream ) \r |
204 | { \r |
205 | UNIMPLEMENTED("sdTellFile");\r |
206 | TOUCH(Stream);\r |
207 | return 0; \r |
208 | }\r |
209 | int32 sdCloseFile( FileStream * Stream ) \r |
210 | { \r |
211 | UNIMPLEMENTED("sdCloseFile");\r |
212 | TOUCH(Stream);\r |
213 | return 0; \r |
214 | }\r |
215 | #endif\r |
216 | \r |