| 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, David 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 | /*************************************************************** |
| 25 | ** Initialize I/O system. |
| 26 | */ |
| 27 | void ioInit( void ) |
| 28 | { |
| 29 | /* System dependant terminal initialization. */ |
| 30 | sdTerminalInit(); |
| 31 | } |
| 32 | void ioTerm( void ) |
| 33 | { |
| 34 | sdTerminalTerm(); |
| 35 | } |
| 36 | |
| 37 | /*************************************************************** |
| 38 | ** Send single character to output stream. |
| 39 | */ |
| 40 | void ioEmit( char c ) |
| 41 | { |
| 42 | cell_t Result; |
| 43 | |
| 44 | Result = sdTerminalOut(c); |
| 45 | if( Result < 0 ) EXIT(1); |
| 46 | |
| 47 | if( gCurrentTask ) |
| 48 | { |
| 49 | if(c == '\n') |
| 50 | { |
| 51 | gCurrentTask->td_OUT = 0; |
| 52 | sdTerminalFlush(); |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | gCurrentTask->td_OUT++; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /*************************************************************** |
| 62 | ** Send an entire string.. |
| 63 | */ |
| 64 | void ioType( const char *s, cell_t n ) |
| 65 | { |
| 66 | cell_t i; |
| 67 | |
| 68 | for( i=0; i<n; i++) |
| 69 | { |
| 70 | ioEmit ( *s++ ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /*************************************************************** |
| 75 | ** Return single character from input device, always keyboard. |
| 76 | */ |
| 77 | cell_t ioKey( void ) |
| 78 | { |
| 79 | cell_t c; |
| 80 | sdEnableInput(); |
| 81 | c = sdTerminalIn(); |
| 82 | sdDisableInput(); |
| 83 | return c; |
| 84 | } |
| 85 | |
| 86 | /************************************************************** |
| 87 | ** Receive line from keyboard. |
| 88 | ** Return number of characters enterred. |
| 89 | */ |
| 90 | #define SPACE (0x20) |
| 91 | #define BACKSPACE (0x08) |
| 92 | #define DELETE (0x7F) |
| 93 | cell_t ioAccept( char *buffer, cell_t maxChars ) |
| 94 | { |
| 95 | int c; |
| 96 | int len; |
| 97 | char *p; |
| 98 | |
| 99 | DBUGX(("ioAccept(0x%x, 0x%x)\n", buffer, len )); |
| 100 | |
| 101 | sdEnableInput(); |
| 102 | |
| 103 | p = buffer; |
| 104 | len = 0; |
| 105 | while(len < maxChars) |
| 106 | { |
| 107 | c = sdTerminalIn(); |
| 108 | switch(c) |
| 109 | { |
| 110 | case '\r': |
| 111 | case '\n': |
| 112 | DBUGX(("EOL\n")); |
| 113 | goto gotline; |
| 114 | break; |
| 115 | |
| 116 | case BACKSPACE: |
| 117 | case DELETE: |
| 118 | if( len > 0 ) /* Don't go beyond beginning of line. */ |
| 119 | { |
| 120 | EMIT(BACKSPACE); |
| 121 | EMIT(' '); |
| 122 | EMIT(BACKSPACE); |
| 123 | p--; |
| 124 | len--; |
| 125 | } |
| 126 | break; |
| 127 | |
| 128 | default: |
| 129 | sdTerminalEcho( (char) c ); |
| 130 | *p++ = (char) c; |
| 131 | len++; |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | |
| 137 | gotline: |
| 138 | sdDisableInput(); |
| 139 | sdTerminalEcho( SPACE ); |
| 140 | |
| 141 | /* NUL terminate line to simplify printing when debugging. */ |
| 142 | if( len < maxChars ) p[len] = '\0'; |
| 143 | |
| 144 | return len; |
| 145 | } |
| 146 | |
| 147 | #define UNIMPLEMENTED(name) { MSG(name); MSG("is unimplemented!\n"); } |
| 148 | |
| 149 | |
| 150 | /***********************************************************************************/ |
| 151 | /*********** File I/O **************************************************************/ |
| 152 | /***********************************************************************************/ |
| 153 | #ifdef PF_NO_FILEIO |
| 154 | |
| 155 | /* Provide stubs for standard file I/O */ |
| 156 | |
| 157 | FileStream *PF_STDIN; |
| 158 | FileStream *PF_STDOUT; |
| 159 | |
| 160 | cell_t sdInputChar( FileStream *stream ) |
| 161 | { |
| 162 | UNIMPLEMENTED("sdInputChar"); |
| 163 | TOUCH(stream); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | FileStream *sdOpenFile( const char *FileName, const char *Mode ) |
| 168 | { |
| 169 | UNIMPLEMENTED("sdOpenFile"); |
| 170 | TOUCH(FileName); |
| 171 | TOUCH(Mode); |
| 172 | return NULL; |
| 173 | } |
| 174 | cell_t sdFlushFile( FileStream * Stream ) |
| 175 | { |
| 176 | TOUCH(Stream); |
| 177 | return 0; |
| 178 | } |
| 179 | cell_t sdReadFile( void *ptr, cell_t Size, int32_t nItems, FileStream * Stream ) |
| 180 | { |
| 181 | UNIMPLEMENTED("sdReadFile"); |
| 182 | TOUCH(ptr); |
| 183 | TOUCH(Size); |
| 184 | TOUCH(nItems); |
| 185 | TOUCH(Stream); |
| 186 | return 0; |
| 187 | } |
| 188 | cell_t sdWriteFile( void *ptr, cell_t Size, int32_t nItems, FileStream * Stream ) |
| 189 | { |
| 190 | UNIMPLEMENTED("sdWriteFile"); |
| 191 | TOUCH(ptr); |
| 192 | TOUCH(Size); |
| 193 | TOUCH(nItems); |
| 194 | TOUCH(Stream); |
| 195 | return 0; |
| 196 | } |
| 197 | cell_t sdSeekFile( FileStream * Stream, file_offset_t Position, int32_t Mode ) |
| 198 | { |
| 199 | UNIMPLEMENTED("sdSeekFile"); |
| 200 | TOUCH(Stream); |
| 201 | TOUCH(Position); |
| 202 | TOUCH(Mode); |
| 203 | return 0; |
| 204 | } |
| 205 | file_offset_t sdTellFile( FileStream * Stream ) |
| 206 | { |
| 207 | UNIMPLEMENTED("sdTellFile"); |
| 208 | TOUCH(Stream); |
| 209 | return 0; |
| 210 | } |
| 211 | cell_t sdCloseFile( FileStream * Stream ) |
| 212 | { |
| 213 | UNIMPLEMENTED("sdCloseFile"); |
| 214 | TOUCH(Stream); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | cell_t sdDeleteFile( const char *FileName ) |
| 219 | { |
| 220 | UNIMPLEMENTED("sdDeleteFile"); |
| 221 | TOUCH(FileName); |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | cell_t sdRenameFile( const char *OldName, const char *NewName ) |
| 226 | { |
| 227 | UNIMPLEMENTED("sdRenameFile"); |
| 228 | TOUCH(OldName); |
| 229 | TOUCH(NewName); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | ThrowCode sdResizeFile( FileStream * File, uint64_t NewSize ) |
| 234 | { |
| 235 | UNIMPLEMENTED("sdResizeFile"); |
| 236 | TOUCH(NewSize); |
| 237 | return THROW_RESIZE_FILE; |
| 238 | } |
| 239 | |
| 240 | #endif |
| 241 | |