1 #ifndef __STDIO_H__
2 #define __STDIO_H__
3 
4 #include <sys/compiler.h>
5 #include <stdint.h>
6 
7 /* $Id: stdio.h */
8 
9 #undef __STDIO_BINARY      /* By default don't consider binary/text file differences */
10 #undef __STDIO_CRLF        /* By default don't insert automatic linefeed in text mode */
11 
12 #ifdef __SPECTRUM__
13 #include <spectrum.h>
14 #endif
15 
16 #ifdef __LAMBDA__
17 #include <zx81.h>
18 #endif
19 
20 #ifdef __ZX81__
21 #include <zx81.h>
22 #endif
23 
24 #ifdef __ZX80__
25 #include <zx81.h>
26 #endif
27 
28 #ifdef __CPM__
29 /* This will define __STDIO_BINARY, __STDIO_EOFMARKER and __STDIO_CRLF  */
30 #include <cpm.h>
31 #endif
32 
33 #ifdef __MSX__
34 /* This will define __STDIO_BINARY, __STDIO_EOFMARKER and __STDIO_CRLF  */
35 #include <cpm.h>
36 #include <msx.h>
37 #endif
38 
39 #ifdef __OSCA__
40 /* This will define __STDIO_BINARY, __STDIO_EOFMARKER and __STDIO_CRLF  */
41 #include <flos.h>
42 #endif
43 
44 #ifdef __SOS__
45 #include <sos.h>
46 #endif
47 
48 #ifdef ZXVGS
49 #include <zxvgs.h>
50 #endif
51 
52 
53 #ifdef AMALLOC
54 #include <malloc.h>
55 #endif
56 #ifdef AMALLOC1
57 #include <malloc.h>
58 #endif
59 #ifdef AMALLOC2
60 #include <malloc.h>
61 #endif
62 #ifdef AMALLOC3
63 #include <malloc.h>
64 #endif
65 
66 
67 /*
68  * This is the new stdio library - everything is pretty much
69  * generic - just a few machine specific routines are needed
70  * and these are clearly marked
71  */
72 
73 #include <sys/types.h>
74 #include <fcntl.h>
75 
76 #ifndef NULL
77 #define NULL ((void *)0)
78 #endif
79 
80 #ifndef EOF
81 #define EOF (-1)
82 #endif
83 
84 #define FILENAME_MAX    128
85 
86 
87 struct filestr {
88         union f0xx {
89                 int         fd;
90                 uint8_t    *ptr;
91         } desc;
92         uint8_t   flags;
93         uint8_t   ungetc;
94         intptr_t  extra;
95         uint8_t   flags2;
96         uint8_t   reserved;
97         uint8_t   reserved1;
98         uint8_t   reserved2;
99 };
100 
101 
102 /* extra may point to an asm label that can be used to add extra stdio functionality
103  * Entry: ix = fp for all
104  */
105 
106 /* Exit: hl = byte read, c = error, nc = success */
107 #define __STDIO_MSG_GETC        1
108 /* Entry: bc = byte to write, Exit: hl = byte written (or EOF) */
109 #define __STDIO_MSG_PUTC        2
110 /* Entry: de = buf, bc = len, Exit: hl = bytes read */
111 #define __STDIO_MSG_READ        3
112 /* Entry: de = buf, bc = len, Exit: hl = bytes written */
113 #define __STDIO_MSG_WRITE       4
114 /* Entry: debc = offset, a' = whence */
115 #define __STDIO_MSG_SEEK        5
116 #define __STDIO_MSG_FLUSH       6
117 #define __STDIO_MSG_CLOSE       7
118 #define __STDIO_MSG_IOCTL       8
119 
120 
121 /* For asm routines kinda handy to have a nice DEFVARS of the structure*/
122 #ifdef STDIO_ASM
123 #asm
124 DEFVARS 0 {
125     fp_desc         ds.w    1
126     fp_flags        ds.b    1
127     fp_ungetc       ds.b    1
128     fp_extra        ds.w    1
129     fp_flags2       ds.b    1
130     reserved        ds.b    1
131     reserved2       ds.b    1
132     reserved3       ds.b    1
133 }
134 #endasm
135 #endif
136 
137 typedef struct filestr FILE;
138 
139 /* System is used for initial std* streams
140  * Network streams do not set IOREAD/IOWRITE, it is assumed that
141  * they are read/write always
142  */
143 
144 #define _IOUSE          1
145 #define _IOREAD         2
146 #define _IOWRITE        4
147 #define _IOEOF          8
148 #define _IOSYSTEM      16
149 #define _IOEXTRA       32
150 #define _IOTEXT        64
151 #define _IOSTRING     128
152 
153 
154 /* Number of open files, this can be overridden by the crt0, but the 10 is the default for classic */
155 #ifndef FOPEN_MAX
156 extern void *_FOPEN_MAX;
157 #define FOPEN_MAX &_FOPEN_MAX
158 #endif
159 
160 
161 extern struct filestr _sgoioblk[10];
162 extern struct filestr _sgoioblk_end;
163 
164 
165 #define stdin  &_sgoioblk[0]
166 #define stdout &_sgoioblk[1]
167 #define stderr &_sgoioblk[2]
168 
169 
170 #define clearerr(f)
171 extern FILE __LIB__ *fopen_zsock(char *name);
172 
173 /* Get a file file descriptor from a file pointer */
174 extern int __LIB__ fileno(FILE *stream) __smallc __z88dk_fastcall;
175 
176 /* Our new and improved functions!! */
177 
178 extern FILE __LIB__ *fopen(const char *name, const char *mode) __smallc;
179 extern FILE __LIB__ *freopen(const char *name, const char *mode, FILE *fp) __smallc;
180 extern FILE __LIB__ *fdopen(const int fildes, const char *mode) __smallc;
181 extern FILE __LIB__ *_freopen1(const char *name, int fd, const char *mode, FILE *fp) __smallc;
182 extern FILE __LIB__ *fmemopen(void *buf, size_t size, const char *mode) __smallc;
183 extern FILE __LIB__ *funopen(const void     *cookie, int (*readfn)(void *, char *, int),
184                     int (*writefn)(void *, const char *, int),
185                     fpos_t (*seekfn)(void *, fpos_t, int), int (*closefn)(void *)) __smallc;
186 
187 extern int __LIB__  fclose(FILE *fp);
188 extern int __LIB__  fflush(FILE *);
189 
190 extern void __LIB__ closeall();
191 
192 
193 #ifdef SIMPLIFIED_STDIO
194 
195 /* --------------------------------------------------------------*/
196 /* The "8080" stdio lib is at the moment used only by the        */
197 /* Rabbit Control Module, which is not fully z80 compatible      */
198 
199 extern char __LIB__ *fgets( char *s, int, FILE *fp) __smallc;
200 extern int __LIB__ fputs( char *s,  FILE *fp) __smallc;
201 extern int __LIB__ fputc(int c, FILE *fp) __smallc;
202 extern int __LIB__ fgetc(FILE *fp);
203 #define getc(f) fgetc(f)
204 extern int __LIB__ ungetc(int c, FILE *) __smallc;
205 extern int __LIB__ feof(FILE *fp) __z88dk_fastcall;
206 extern int __LIB__ ferror(FILE *fp) __z88dk_fastcall;
207 extern int __LIB__ puts(char *s);
208 
209 /* Some standard macros */
210 #define putc(bp,fp) fputc(bp,fp)
211 #define putchar(bp) fputc(bp,stdout)
212 #define getchar()  fgetc(stdin)
213 
214 /* --------------------------------------------------------------*/
215 
216 #else
217 
218 /* --------------------------------------------------------------*/
219 /* Optimized stdio uses the 'CALLEE' convention here and there   */
220 
221 extern char __LIB__ *fgets(char *s, int, FILE *fp) __smallc;
222 
223 extern int __LIB__ fputs(const char *s,  FILE *fp) __smallc;
224 extern int __LIB__ fputc(int c, FILE *fp) __smallc;
225 
226 extern int __LIB__  fputs_callee(const char *s,  FILE *fp) __smallc __z88dk_callee;
227 extern int __LIB__  fputc_callee(int c, FILE *fp) __smallc __z88dk_callee;
228 extern int __LIB__ fgetc(FILE *fp);
229 
230 #define getc(f) fgetc(f)
231 extern int __LIB__ ungetc(int c, FILE *) __smallc;
232 extern int __LIB__ feof(FILE *fp) __z88dk_fastcall;
233 extern int __LIB__ ferror(FILE *fp) __z88dk_fastcall;
234 extern int __LIB__ puts(const char *);
235 
236 #define fputs(a,b)   fputs_callee(a,b)
237 #define fputc(a,b)   fputc_callee(a,b)
238 
239 /* Some standard macros */
240 #define putc(bp,fp) fputc_callee(bp,fp)
241 #define putchar(bp) fputc_callee(bp,stdout)
242 #define getchar()  fgetc(stdin)
243 
244 /* --------------------------------------------------------------*/
245 
246 #endif
247 
248 /* Routines for file positioning */
249 extern fpos_t __LIB__ ftell(FILE *fp);
250 extern int __LIB__ fgetpos(FILE *fp, fpos_t *pos) __smallc;
251 #define fsetpos(fp,pos) fseek(fp,pos,SEEK_SET)
252 #define rewind(fp) fseek(fp,0L,SEEK_SET)
253 extern int __LIB__ __SAVEFRAME__ fseek(FILE *fp, fpos_t offset, int whence) __smallc;
254 
255 /* Block read/writing */
256 extern int __LIB__  fread(void *ptr, size_t size, size_t num, FILE *) __smallc;
257 extern int __LIB__  fwrite(const void *ptr, size_t size, size_t num, FILE *) __smallc;
258 
259 
260 /* You shouldn't use gets. z88 gets() is limited to 255 characters */
261 //#ifdef __STDIO_CRLF
262 //#define gets(x) fgets_cons(x,255)
263 //#else
264 extern char __LIB__ *gets(char *s);
265 //#endif
266 
267 extern int __LIB__ printf(const char *fmt,...) __vasmallc;
268 extern int __LIB__ fprintf(FILE *f,const char *fmt,...) __vasmallc;
269 extern int __LIB__ sprintf(char *s,const char *fmt,...) __vasmallc;
270 extern int __LIB__ snprintf(char *s,size_t n,const char *fmt,...) __vasmallc;
271 extern int __LIB__ vfprintf(FILE *f,const char *fmt,void *ap);
272 extern int __LIB__ vsnprintf(char *str, size_t n,const char *fmt,void *ap);
273 
274 #define vprintf(ctl,arg) vfprintf(stdout,ctl,arg)
275 #define vsprintf(buf,ctl,arg) vsnprintf(buf,65535,ctl,arg)
276 
277 /* Routines used by the old printf - will be removed soon */
278 extern void __LIB__ printn(int number, int radix,FILE *file) __smallc;
279 
280 
281 /*
282  * Scanf family
283  */
284 
285 extern int __LIB__ scanf(const char *fmt,...) __vasmallc;
286 extern int __LIB__ fscanf(FILE *,const char *fmt,...) __vasmallc;
287 extern int __LIB__ sscanf(char *,const char *fmt,...) __vasmallc;
288 extern int __LIB__ vfscanf(FILE *, const char *fmt, void *ap);
289 extern int __LIB__ vsscanf(char *str, const char *fmt, void *ap);
290 #define vscanf(ctl,arg) vfscanf(stdin,ctl,arg)
291 
292 
293 /*
294  * Used in variable argument lists
295  */
296 
297 #ifndef DEF_GETARG
298 #define DEF_GETARG
299 extern int __LIB__ getarg(void);
300 #endif
301 
302 
303 /* Check whether a file is for the console */
304 extern int __LIB__ fchkstd(FILE *);
305 
306 /* All functions below here are machine specific */
307 
308 /* Get a key press using the default keyboard driver */
309 extern int __LIB__ fgetc_cons();
310 
311 /* Get a key press using the "inkey" keyboard driver */
312 extern int __LIB__ fgetc_cons_inkey();
313 
314 /* Output a character to the console using the default driver */
315 extern int __LIB__ fputc_cons(char c);
316 
317 /* Read a string using the default keyboard driver */
318 extern char __LIB__ *fgets_cons(char *s, size_t n) __smallc;
319 
320 extern int __LIB__ puts_cons(char *s);
321 
322 /* Abandon file - can be the generic version */
323 extern void __LIB__ fabandon(FILE *);
324 /* Get file position for file handle fd */
325 extern long __LIB__ fdtell(int fd);
326 extern int __LIB__ fdgetpos(int fd, fpos_t *posn) __smallc;
327 /* Rename a file */
328 extern int __LIB__ rename(const char *s, const char *d) __smallc;
329 /* Remove a file */
330 extern int __LIB__ remove(const char *name);
331 
332 
333 /* Scan for a keypress using the default keyboard driver */
334 extern int __LIB__ getk();
335 /* Scan for a keypress using the "inkey" keyboard driver */
336 extern int __LIB__ getk_inkey();
337 #define getkey() fgetc_cons()
338 
339 /* Print a formatted string directly to the console using the default driver */
340 extern int __LIB__ printk(const char *fmt,...) __vasmallc;
341 
342 /* Error handler (mostly an empty fn) */
343 extern void __LIB__ perror(char *msg) __z88dk_fastcall;
344 
345 
346 /* We have multiple methods of outputting a character to the console.
347    Normally they are setup at the linking stage, but sometimes we may
348    need multiple methods linked into the program (for example systems
349    with a serial port and a graphics card).
350  */
351 
352 typedef int (*fputc_cons_func)(char c);
353 /* Set the fputc_cons implementation, return the old one */
354 extern fputc_cons_func __LIB__ set_fputc_cons(fputc_cons_func func);
355 
356 /* Implementation that uses the ROM/firmware */
357 extern int __LIB__ fputc_cons_native(char c);
358 /* Implementation that uses the generic console */
359 extern int __LIB__ fputc_cons_generic(char c);
360 /* Implementation that uses the ansi terminal */
361 extern int __LIB__ fputc_cons_ansi(char c);
362 
363 
364 /*
365  *  MICRO C compatibility:  keep at bottom of this file
366  *  Some of Dunfield's Micro C code can be ported with the '-DMICROC' parameter
367  */
368 
369 #ifdef MICROC
370 #include <microc.h>
371 #endif
372 
373 #endif /* _STDIO_H */
374