some input upgrades

This commit is contained in:
Luxferre
2022-08-17 21:30:09 +03:00
parent efb6b7a3d6
commit 991dff3eb1
2 changed files with 46 additions and 16 deletions
+44 -13
View File
@@ -18,12 +18,33 @@
#include <conio.h>
#define CRLF "\n"
#else
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#define cgetc() (getchar())
#define cputc(c) (putchar(c))
#pragma pack(2)
#define CRLF "\r\n"
/* also, attempt to emulate two other conio methods */
int kbhit() {
struct timeval tv = { 0L, 0L };
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv) > 0;
}
int getch() {
int r;
unsigned char c;
if((r = read(0, &c, 1)) < 0) return r;
else return c;
}
#endif
/* Definitions section */
@@ -474,7 +495,7 @@ struct EquiCtx* equi_load_task(uchar priv, ushort len, ushort progStart) {
/* Main interpreter loop */
void equi_main_loop() {
uchar instr;
uchar instr, bc;
ushort lhash, pbuf, pbuf2;
/* try to open the persistent sandbox file */
FILE *pfd = fopen(PERSIST_FILE, "r+b");
@@ -675,9 +696,14 @@ void equi_main_loop() {
case INS_NHOUT: /* output the hex number from the stack top */
printf("%04X", popMain());
break;
case INS_NBKIN: /* non-blocking key input - in this implementation it's equal to: */
case INS_NBKIN: /* non-blocking key input - best attempt to implement it with kbhit or our haphazard emulation of it */
pushMain(kbhit() ? (ushort)cgetc() : 0);
break;
case INS_BKIN: /* blocking key input (again, no Unicode support yet, assuming a single byte incoming to the 16-bit value on the stack) */
pushMain((ushort)cgetc());
bc = 0;
bc = cgetc();
fprintf(stderr, "[in] %c" CRLF, bc);
pushMain((ushort) bc);
break;
case INS_PORTIO: /* ( p1 p2 port -- r1 r2 status ) - simulate/execute port I/O according to the spec */
if(curtask->msp < 3)
@@ -709,10 +735,8 @@ void equi_main_loop() {
/* Equi VM entry point */
int main(int argc, char* argv[]) {
uchar instr, bc, smode = 0;
/* detect host endianness */
int host_islittle = 1;
host_islittle = (*((char *)&host_islittle) == 1) ? 1 : 0;
uchar instr, bc, modec, smode = 0;
FILE *prog = stdin;
/* _attempt_ to disable buffering for char input/output */
#if !defined __CC65__
struct termios tty_opts_raw, tty_opts_backup;
@@ -729,12 +753,18 @@ int main(int argc, char* argv[]) {
ram.cmd_size = CMD_BUF_SIZE;
ram.II = ram.MM = 0; /* reset all flags */
/* process command line params */
if(argc > 1 && argv[1][0] == 'm') { /* enter minification mode, don't run the programs */
ram.MM = 1;
smode = 1; /* also behave as if in the silent mode */
if(argc > 1 && argv[1][0] != '-') { /* we passed the input file, - means "just use stdin" */
prog = fopen(argv[1], "r");
}
else if(argc > 1 && argv[1][0] == 's') /* enter silent mode, don't print the banners and prompts */
smode = 1;
if(argc > 2) { /* the first is the file, the second is the mode */
if(argv[2][0] == 'm') { /* enter minification mode, don't run the programs */
ram.MM = 1;
smode = 1; /* also behave as if in the silent mode */
}
else if(argv[2][0] == 's') /* enter silent mode, don't print the banners and prompts */
smode = 1;
}
/* Start input buffering from the start of command buffer (-1 because we use prefix increment) */
ram.ibp = 65535U;
if(!ram.MM && !smode) { /* skip the terminal init and the greeting if in minification or silent mode */
@@ -749,7 +779,7 @@ int main(int argc, char* argv[]) {
}
while(1) { /* Now, we're in the command mode loop */
instr = cgetc(); /* Fetch the next instruction from the keyboard/stdin */
instr = fgetc(prog); /* Fetch the next instruction from the keyboard/file */
if(instr == 0xFFU || instr == 0U || instr == 3U || instr == 4U) /* exit on zero byte or ctrl+C or ctrl+D */
break;
else if(instr == BS || instr == DEL || instr == CR || instr == LF || instr == ' ' || instr == '\t') { /* ignore the backspace or whitespaces */
@@ -813,5 +843,6 @@ int main(int argc, char* argv[]) {
/* restore the terminal settings */
tcsetattr(STDIN_FILENO, TCSANOW, &tty_opts_backup);
#endif
fclose(prog); /* close the input file */
return 0;
}