1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-09-29 13:20:57 +10:00

emul/forth: allow running commands from file

This commit is contained in:
Virgil Dupras 2020-03-11 19:03:47 -04:00
parent 3996f0c825
commit abdf2c8adc

View File

@ -10,10 +10,11 @@
#define STDIO_PORT 0x00 #define STDIO_PORT 0x00
static int running; static int running;
static FILE *fp;
static uint8_t iord_stdio() static uint8_t iord_stdio()
{ {
int c = getchar(); int c = getc(fp);
if (c == EOF) { if (c == EOF) {
running = 0; running = 0;
} }
@ -31,19 +32,31 @@ static void iowr_stdio(uint8_t val)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
bool tty = isatty(fileno(stdin)); bool tty = false;
struct termios termInfo; struct termios termInfo;
if (tty) { if (argc == 2) {
// Turn echo off: the shell takes care of its own echoing. fp = fopen(argv[1], "r");
if (tcgetattr(0, &termInfo) == -1) { if (fp == NULL) {
printf("Can't setup terminal.\n"); fprintf(stderr, "Can't open %s\n", argv[1]);
return 1; return 1;
} }
termInfo.c_lflag &= ~ECHO; } else if (argc == 1) {
termInfo.c_lflag &= ~ICANON; fp = stdin;
tcsetattr(0, TCSAFLUSH, &termInfo); tty = isatty(fileno(stdin));
if (tty) {
// Turn echo off: the shell takes care of its own echoing.
if (tcgetattr(0, &termInfo) == -1) {
printf("Can't setup terminal.\n");
return 1;
}
termInfo.c_lflag &= ~ECHO;
termInfo.c_lflag &= ~ICANON;
tcsetattr(0, TCSAFLUSH, &termInfo);
}
} else {
fprintf(stderr, "Usage: ./forth [filename]\n");
return 1;
} }
Machine *m = emul_init(); Machine *m = emul_init();
m->ramstart = RAMSTART; m->ramstart = RAMSTART;
m->iord[STDIO_PORT] = iord_stdio; m->iord[STDIO_PORT] = iord_stdio;
@ -58,11 +71,12 @@ int main(int argc, char *argv[])
while (running && emul_step()); while (running && emul_step());
if (tty) { if (tty) {
printf("Done!\n"); printf("\nDone!\n");
termInfo.c_lflag |= ECHO; termInfo.c_lflag |= ECHO;
termInfo.c_lflag |= ICANON; termInfo.c_lflag |= ICANON;
tcsetattr(0, TCSAFLUSH, &termInfo); tcsetattr(0, TCSAFLUSH, &termInfo);
emul_printdebug(); emul_printdebug();
} }
fclose(fp);
return 0; return 0;
} }