Skip to content

Commit

Permalink
streamline streamio in console.h
Browse files Browse the repository at this point in the history
  • Loading branch information
pzembrod committed Dec 16, 2023
1 parent ba87947 commit a4e878e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
3 changes: 3 additions & 0 deletions RunCPM/abstraction_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state)
error(EXIT_FAILURE, errno,
"error opening console input file %s", arg);
}
streamInActive = TRUE;
break;
case 'o':
streamOutFile = fopen(arg, "w");
Expand All @@ -508,6 +509,8 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state)
case 's':
streamInFile = stdin;
streamOutFile = stdout;
streamInActive = TRUE;
consoleOutActive = FALSE;
break;
case ARGP_KEY_ARG:
case ARGP_KEY_END:
Expand Down
31 changes: 15 additions & 16 deletions RunCPM/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ uint8 mask8bit = 0x7f; // TO be used for masking 8 bit characters (XMODEM relat
void _putcon(uint8 ch) // Puts a character
{
#ifdef STREAMIO
if (streamOutFile != stdout) _putch(ch & mask8bit);
if (consoleOutActive) _putch(ch & mask8bit);
if (streamOutFile) fputc(ch & mask8bit, streamOutFile);
#else
_putch(ch & mask8bit);
Expand Down Expand Up @@ -42,34 +42,33 @@ void _puthex16(uint16 w) // puts a HHHH hex string
#ifdef STREAMIO
int _nextStreamInChar;

void _getNextConInChar(void)
void _getNextStreamInChar(void)
{
_nextStreamInChar = streamInFile ? fgetc(streamInFile) : EOF;
if (EOF == _nextStreamInChar) {
streamInActive = FALSE;
}
}

uint8 _conInCharReady(void)
{
return EOF != _nextStreamInChar;
}

uint8 _getConInChar(void)
uint8 _getStreamInChar(void)
{
uint8 result = _nextStreamInChar;
_getNextConInChar();
_getNextStreamInChar();
// TODO: delegate to abstrction_posix.h
if (0x0a == result) result = 0x0d;
return result;
}

uint8 _getConInCharEcho()
uint8 _getStreamInCharEcho()
{
uint8 result = _getConInChar();
uint8 result = _getStreamInChar();
_putcon(result);
return result;
}

void _streamioInit(void)
{
_getNextConInChar();
_getNextStreamInChar();
}

void _streamioReset(void)
Expand All @@ -81,31 +80,31 @@ void _streamioReset(void)
uint8 _chready(void) // Checks if there's a character ready for input
{
#ifdef STREAMIO
if (_conInCharReady()) return 0xff;
if (streamInActive) return 0xff;
#endif
return(_kbhit() ? 0xff : 0x00);
}

uint8 _getconNB(void) // Gets a character, non-blocking, no echo
{
#ifdef STREAMIO
if (_conInCharReady()) return _getConInChar();
if (streamInActive) return _getStreamInChar();
#endif
return(_kbhit() ? _getch() : 0x00);
}

uint8 _getcon(void) // Gets a character, blocking, no echo
{
#ifdef STREAMIO
if (_conInCharReady()) return _getConInChar();
if (streamInActive) return _getStreamInChar();
#endif
return _getch();
}

uint8 _getconE(void) // Gets a character, blocking, with echo
{
#ifdef STREAMIO
if (_conInCharReady()) return _getConInCharEcho();
if (streamInActive) return _getStreamInCharEcho();
#endif
return _getche();
}
Expand Down
5 changes: 2 additions & 3 deletions RunCPM/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,8 @@ static uint32 timer;
#include <stdio.h>
static FILE *streamInFile = NULL;
static FILE *streamOutFile = NULL;
static bool streamInput = FALSE;
static bool streamOutput = FALSE;
static bool echoOutput = FALSE;
static uint8 streamInActive = FALSE;
static uint8 consoleOutActive = TRUE;
#endif


Expand Down

0 comments on commit a4e878e

Please sign in to comment.