/* Copyright 1990,1991 Eric R. Smith. All rights reserved. */ /* MiNT routines for doing console I/O */ #include "mint.h" /* * These routines are what Cconout, Cauxout, etc. ultimately call. * They take an integer argument which is the user's file handle, * and do the translation to a file ptr (and return an appropriate error * message if necessary. * "mode" may be RAW, COOKED, ECHO, or COOKED|ECHO. * note that the user may call them directly via Finstat(), Foutstat(), * Fgetchar(), and Fputchar() */ long file_instat(h) int h; { FILEPTR *f; long r; PROC *proc; int fh = h; #if O_GLOBAL if (fh >= 100) { proc = rootproc; fh -= 100; } else #endif proc = curproc; if (fh < MIN_HANDLE || fh >=MAX_OPEN || !(f = proc->handle[fh])) { DEBUG("Finstat: bad handle %d", h); return EIHNDL; } r = 1; /* default is to assume input waiting (e.g. TOS files)*/ (void)(*f->dev->ioctl)(f, FIONREAD, &r); return r; } long file_outstat(h) int h; { FILEPTR *f; long r; int fh = h; PROC *proc; #if O_GLOBAL if (fh >= 100) { fh -= 100; proc = rootproc; } else #endif proc = curproc; if (fh < MIN_HANDLE || fh >=MAX_OPEN || !(f = proc->handle[fh])) { DEBUG("Foutstat: bad handle %d", h); return EIHNDL; } r = 1; /* default is to assume output OK (e.g. TOS files) */ (void)(*f->dev->ioctl)(f, FIONWRITE, &r); return r; } long file_getchar(h, mode) int h, mode; { FILEPTR *f; char c; long r; int fh = h; PROC *proc; #if O_GLOBAL if (fh >= 100) { fh -= 100; proc = rootproc; } else #endif proc = curproc; if (fh < MIN_HANDLE || fh >=MAX_OPEN || !(f = proc->handle[fh])) { DEBUG("Fgetchar: bad handle %d", h); return EIHNDL; } if (is_terminal(f)) { return tty_getchar(f, mode); } r = (*f->dev->read)(f, &c, 1L); if (r != 1) return MiNTEOF; else return ((long)c) & 0xff; } long file_putchar(h, c, mode) int h; long c; int mode; { FILEPTR *f; char ch; int fh = h; PROC *proc; #if O_GLOBAL if (fh >= 100) { fh -= 100; proc = rootproc; } else #endif proc = curproc; if (fh < MIN_HANDLE || fh >=MAX_OPEN || !(f = proc->handle[fh])) { DEBUG("Fputchar: bad handle %d", h); return EIHNDL; } if (is_terminal(f)) { return tty_putchar(f, c & 0x7fffffffL, mode); } ch = c & 0x00ff; return (*f->dev->write)(f, &ch, 1L); } /* * OK, here are the GEMDOS console I/O routines */ long c_conin() { return file_getchar(0, COOKED|ECHO); } long c_conout(c) int c; { return file_putchar(1, (long)c, COOKED); } long c_auxin() { return file_getchar(2, RAW); } long c_auxout(c) int c; { return file_putchar(2, (long)c, RAW); } long c_prnout(c) int c; { return file_putchar(3, (long)c, RAW); } long c_rawio(c) int c; { long r; if (c == 0x00ff) { if (!file_instat(0)) return 0; r = file_getchar(0, RAW); if (r <= 0) return 0; return r; } else return file_putchar(1, (long)c, RAW); } long c_rawcin() { return file_getchar(0, RAW); } long c_necin() { return file_getchar(0,COOKED|NOECHO); } long c_conws(str) const char *str; { const char *p = str; long cnt = 0; while (*p++) cnt++; return f_write(1, cnt, str); } long c_conrs(buf) char *buf; { long size, r; char *s; size = ((long)*buf) & 0xff; r = f_read(0, size, buf+2); if (r < 0) { buf[1] = 0; return r; } /* if reading from a file, stop at first CR or LF encountered */ s = buf+2; size = 0; while(r-- > 0) { if (*s == '\r' || *s == '\n') break; s++; size++; } buf[1] = (char)size; return 0; } long c_conis() { return -(!!file_instat(0)); } long c_conos() { return -(!!file_outstat(1)); } long c_prnos() { return -(!!file_outstat(3)); } long c_auxis() { return -(!!file_instat(2)); } long c_auxos() { return -(!!file_outstat(2)); }