| 1 | /* @(#) pf_io.h 98/01/26 1.2 */ |
| 2 | #ifndef _pf_io_h |
| 3 | #define _pf_io_h |
| 4 | |
| 5 | /*************************************************************** |
| 6 | ** Include file for PForth IO |
| 7 | ** |
| 8 | ** Author: Phil Burk |
| 9 | ** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom |
| 10 | ** |
| 11 | ** The pForth software code is dedicated to the public domain, |
| 12 | ** and any third party may reproduce, distribute and modify |
| 13 | ** the pForth software code or any derivative works thereof |
| 14 | ** without any compensation or license. The pForth software |
| 15 | ** code is provided on an "as is" basis without any warranty |
| 16 | ** of any kind, including, without limitation, the implied |
| 17 | ** warranties of merchantability and fitness for a particular |
| 18 | ** purpose and their equivalents under the laws of any jurisdiction. |
| 19 | ** |
| 20 | ***************************************************************/ |
| 21 | |
| 22 | #include "pf_types.h" |
| 23 | |
| 24 | #define PF_CHAR_XON (0x11) |
| 25 | #define PF_CHAR_XOFF (0x13) |
| 26 | |
| 27 | int sdTerminalOut( char c ); |
| 28 | int sdTerminalEcho( char c ); |
| 29 | int sdTerminalFlush( void ); |
| 30 | int sdTerminalIn( void ); |
| 31 | int sdQueryTerminal( void ); |
| 32 | void sdTerminalInit( void ); |
| 33 | void sdTerminalTerm( void ); |
| 34 | |
| 35 | void ioInit( void ); |
| 36 | void ioTerm( void ); |
| 37 | |
| 38 | #ifdef PF_NO_CHARIO |
| 39 | void sdEnableInput( void ); |
| 40 | void sdDisableInput( void ); |
| 41 | |
| 42 | #else /* PF_NO_CHARIO */ |
| 43 | #ifdef PF_USER_CHARIO |
| 44 | /* Get user prototypes or macros from include file. |
| 45 | ** API must match that defined above for the stubs. |
| 46 | */ |
| 47 | /* If your sdTerminalIn echos, define PF_KEY_ECHOS. */ |
| 48 | #include PF_USER_CHARIO |
| 49 | #else |
| 50 | #define sdEnableInput() /* sdTerminalOut( PF_CHAR_XON ) */ |
| 51 | #define sdDisableInput() /* sdTerminalOut( PF_CHAR_XOFF ) */ |
| 52 | |
| 53 | #endif |
| 54 | #endif /* PF_NO_CHARIO */ |
| 55 | |
| 56 | /* Define file access modes. */ |
| 57 | /* User can #undef and re#define using PF_USER_FILEIO if needed. */ |
| 58 | #define PF_FAM_READ_ONLY (0) |
| 59 | #define PF_FAM_READ_WRITE (1) |
| 60 | #define PF_FAM_WRITE_ONLY (2) |
| 61 | #define PF_FAM_BINARY_FLAG (8) |
| 62 | |
| 63 | #define PF_FAM_CREATE_WO ("w") |
| 64 | #define PF_FAM_CREATE_RW ("w+") |
| 65 | #define PF_FAM_OPEN_RO ("r") |
| 66 | #define PF_FAM_OPEN_RW ("r+") |
| 67 | #define PF_FAM_BIN_CREATE_WO ("wb") |
| 68 | #define PF_FAM_BIN_CREATE_RW ("wb+") |
| 69 | #define PF_FAM_BIN_OPEN_RO ("rb") |
| 70 | #define PF_FAM_BIN_OPEN_RW ("rb+") |
| 71 | |
| 72 | #ifdef PF_NO_FILEIO |
| 73 | |
| 74 | typedef void FileStream; |
| 75 | |
| 76 | extern FileStream *PF_STDIN; |
| 77 | extern FileStream *PF_STDOUT; |
| 78 | |
| 79 | #ifdef __cplusplus |
| 80 | extern "C" { |
| 81 | #endif |
| 82 | |
| 83 | /* Prototypes for stubs. */ |
| 84 | FileStream *sdOpenFile( const char *FileName, const char *Mode ); |
| 85 | cell_t sdFlushFile( FileStream * Stream ); |
| 86 | cell_t sdReadFile( void *ptr, cell_t Size, int32_t nItems, FileStream * Stream ); |
| 87 | cell_t sdWriteFile( void *ptr, cell_t Size, int32_t nItems, FileStream * Stream ); |
| 88 | cell_t sdSeekFile( FileStream * Stream, file_offset_t Position, int32_t Mode ); |
| 89 | cell_t sdRenameFile( const char *OldName, const char *NewName ); |
| 90 | cell_t sdDeleteFile( const char *FileName ); |
| 91 | ThrowCode sdResizeFile( FileStream *, uint64_t Size); |
| 92 | file_offset_t sdTellFile( FileStream * Stream ); |
| 93 | cell_t sdCloseFile( FileStream * Stream ); |
| 94 | cell_t sdInputChar( FileStream *stream ); |
| 95 | |
| 96 | #ifdef __cplusplus |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | #define PF_SEEK_SET (0) |
| 101 | #define PF_SEEK_CUR (1) |
| 102 | #define PF_SEEK_END (2) |
| 103 | /* |
| 104 | ** printf() is only used for debugging purposes. |
| 105 | ** It is not required for normal operation. |
| 106 | */ |
| 107 | #define PRT(x) /* No printf(). */ |
| 108 | |
| 109 | #else |
| 110 | |
| 111 | #ifdef PF_USER_FILEIO |
| 112 | /* Get user prototypes or macros from include file. |
| 113 | ** API must match that defined above for the stubs. |
| 114 | */ |
| 115 | #include PF_USER_FILEIO |
| 116 | |
| 117 | #else |
| 118 | typedef FILE FileStream; |
| 119 | |
| 120 | #define sdOpenFile fopen |
| 121 | #define sdDeleteFile remove |
| 122 | #define sdFlushFile fflush |
| 123 | #define sdReadFile fread |
| 124 | #define sdWriteFile fwrite |
| 125 | |
| 126 | /* |
| 127 | * Note that fseek() and ftell() only support a long file offset. |
| 128 | * So 64-bit offsets may not be supported on some platforms. |
| 129 | * At one point we supported fseeko() and ftello() but they require |
| 130 | * the off_t data type, which is not very portable. |
| 131 | * So we decided to sacrifice vary large file support in |
| 132 | * favor of portability. |
| 133 | */ |
| 134 | #define sdSeekFile fseek |
| 135 | #define sdTellFile ftell |
| 136 | |
| 137 | #define sdCloseFile fclose |
| 138 | #define sdRenameFile rename |
| 139 | #define sdInputChar fgetc |
| 140 | |
| 141 | #define PF_STDIN ((FileStream *) stdin) |
| 142 | #define PF_STDOUT ((FileStream *) stdout) |
| 143 | |
| 144 | #define PF_SEEK_SET (SEEK_SET) |
| 145 | #define PF_SEEK_CUR (SEEK_CUR) |
| 146 | #define PF_SEEK_END (SEEK_END) |
| 147 | |
| 148 | /* TODO review the Size data type. */ |
| 149 | ThrowCode sdResizeFile( FileStream *, uint64_t Size); |
| 150 | |
| 151 | /* |
| 152 | ** printf() is only used for debugging purposes. |
| 153 | ** It is not required for normal operation. |
| 154 | */ |
| 155 | #define PRT(x) { printf x; sdFlushFile(PF_STDOUT); } |
| 156 | #endif |
| 157 | |
| 158 | #endif /* PF_NO_FILEIO */ |
| 159 | |
| 160 | |
| 161 | #ifdef __cplusplus |
| 162 | extern "C" { |
| 163 | #endif |
| 164 | |
| 165 | cell_t ioAccept( char *Target, cell_t n1 ); |
| 166 | cell_t ioKey( void); |
| 167 | void ioEmit( char c ); |
| 168 | void ioType( const char *s, cell_t n); |
| 169 | |
| 170 | #ifdef __cplusplus |
| 171 | } |
| 172 | #endif |
| 173 | |
| 174 | #endif /* _pf_io_h */ |