Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
0.23
Browse files Browse the repository at this point in the history
Improved getCurPos()
Fixed an internal typo
Ported "DIM" and "REVERSE" text attributes to non-VT Windows
Fixed ELSEIF
  • Loading branch information
PQCraft authored Sep 30, 2021
1 parent 1cd49a0 commit 809b6a4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
45 changes: 28 additions & 17 deletions clibasic.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@
// Useful macros

/* Swap function to swap values */
#define swap(a, b) __typeof__(a) c = a; a = b; b = c
#define swap(a, b) {__typeof__(a) c = a; a = b; b = c;}

/* Free & set to NULL combo */
#define nfree(ptr) free(ptr); ptr = NULL

// Base defines

char VER[] = "0.22.6";
char VER[] = "0.23";

#if defined(__linux__)
char OSVER[] = "Linux";
Expand Down Expand Up @@ -332,7 +332,7 @@ static inline void* setsig(int sig, void* func) {
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, sig);
act.sa_handler = func;
//act.sa_flags = SA_RESTART;
act.sa_flags = SA_RESTART;
sigaction(sig, &act, &old);
return old.sa_handler;
#else
Expand Down Expand Up @@ -507,7 +507,8 @@ void cleanExit() {
txtqunlock();
if (inprompt) {
int i = kbhit();
while (i > 0) {getchar(); i--;}
if (i) {read(0, &gpbuf, i);}
getCurPos();
unloadAllProg();
cmdint = true;
inProg = false;
Expand Down Expand Up @@ -552,11 +553,7 @@ void cleanExit() {
}

void cmdIntHndl() {
setsig(SIGINT, cmdIntHndl);
if (cmdint) setsig(SIGINT, cleanExit);
int i = kbhit();
if (i) {read(0, &gpbuf, i);}
if (kbhit()) read(0, &gpbuf, kbhit());
cmdint = true;
}

Expand Down Expand Up @@ -637,6 +634,8 @@ static inline void readyTerm() {
tcgetattr(0, &kbhterm);
kbhterm2 = kbhterm;
kbhterm2.c_lflag &= ~ICANON;
//kbhterm2.c_cc[VMIN] = 1;
//kbhterm2.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &kbhterm2);
sigemptyset(&intmask);
sigaddset(&intmask, SIGINT);
Expand All @@ -645,7 +644,7 @@ static inline void readyTerm() {

int main(int argc, char** argv) {
#if defined(GUI_CHECK) && defined(__unix__)
if (system("tty -s 1> /dev/null 2> /dev/null")) {
if (!isatty(STDERR_FILENO) && !isatty(STDIN_FILENO) && !isatty(STDOUT_FILENO)) {
char* command = malloc(CB_BUF_SIZE);
copyStr("xterm -T CLIBASIC -b 0 -bg black -bcn 200 -bcf 200 -e $'clear;", command);
for (int i = 0; i < argc; ++i) {
Expand Down Expand Up @@ -1117,11 +1116,16 @@ static inline int isFile(char* path) {
return !(S_ISDIR(pathstat.st_mode));
}

#ifndef _WIN32
bool gcp_sig = true;
#endif

static inline void getCurPos() {
fflush(stdout);
cury = 0; curx = 0;
#ifndef _WIN32
static char buf[16] = {0};
if (gcp_sig) sigprocmask(SIG_SETMASK, &intmask, &oldmask);
static char buf[16];
for (int i = 0; i < 16; ++i) {buf[i] = 0;}
register int i = 0;
if (!textlock) {
Expand All @@ -1135,14 +1139,18 @@ static inline void getCurPos() {
while (i > 0) {getchar(); --i;}
for (int r = 0; r < 2; ++r) {
i = 0;
resend:
fputs("\e[6n", stdout);
fflush(stdout);
int j = 0;
while (!(j = kbhit())) {}
while (1) {
buf[i] = getchar();
if (buf[i] == 'R' || buf[i] == '\n' || i >= j - 1) {++i; goto gcplexit;}
++i;
uint64_t tmpus = usTime();
while (!kbhit()) {if (usTime() - tmpus > 50000) {goto resend;}}
while (kbhit()) {
//buf[i] = getchar();
if (kbhit()) {
read(0, &buf[i], 1);
if (buf[i] == 'R' || buf[i] == '\n' || kbhit() < 1) {++i; goto gcplexit;}
++i;
}
}
gcplexit:
buf[i] = 0;
Expand All @@ -1160,7 +1168,8 @@ static inline void getCurPos() {
} else {
sscanf(buf, "[%d;%dR", &cury, &curx);
}
if (curx == 0 || cury == 0) {getCurPos();}
if (curx == 0 || cury == 0) {gcp_sig = false; getCurPos(); gcp_sig = true;}
if (gcp_sig) sigprocmask(SIG_SETMASK, &oldmask, NULL);
#else
CONSOLE_SCREEN_BUFFER_INFO con;
GetConsoleScreenBufferInfo(hConsole, &con);
Expand Down Expand Up @@ -1585,6 +1594,8 @@ void updateTxtAttrib() {
b1 = bgc & 1; b2 = (bgc >> 2) & 1; tmpbgc = (b1 ^ b2);
tmpbgc = (tmpbgc) | (tmpbgc << 2); tmpbgc = bgc ^ tmpbgc;
}
if (txt_dim) {tmpfgc %= 8; tmpbgc %= 8;}
if (txt_reverse) swap(tmpfgc, tmpbgc);
SetConsoleTextAttribute(hConsole, (tmpfgc % 16) + ((tmpbgc % 16) << 4));
#endif
fflush(stdout);
Expand Down
2 changes: 1 addition & 1 deletion commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ if (chkCmd(1, "_TXTATTRIB")) {
if (!strcmp(arg[1], "UNDERLINE")) attrib = 3; else
if (!strcmp(arg[1], "DBL_UNDERLINE") || !strcmp(arg[1], "DOUBLE_UNDERLINE")) attrib = 4; else
if (!strcmp(arg[1], "SQG_UNDERLINE") || !strcmp(arg[1], "SQUIGGLY_UNDERLINE")) attrib = 5; else
if (!strcmp(arg[1], "STRIKETROUGH")) attrib = 6; else
if (!strcmp(arg[1], "STRIKETHROUGH")) attrib = 6; else
if (!strcmp(arg[1], "OVERLINE")) attrib = 7; else
if (!strcmp(arg[1], "DIM")) attrib = 8; else
if (!strcmp(arg[1], "BLINK")) attrib = 9; else
Expand Down
2 changes: 2 additions & 0 deletions examples/input.bas
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ DO
STR$ = STR$ + "\\e"
ELSEIF C$ = "\a"
STR$ = STR$ + "\\a"
ELSEIF ASC(C$) < 32 | ASC(C$) > 126
STR$ = STR$ + "\\x" + PAD$(HEX$(ASC(C$)), 2, "0")
ELSE
STR$ = STR$ + C$
ENDIF
Expand Down
15 changes: 8 additions & 7 deletions logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ if (chkCmd(2, "WHILE", "DOWHILE")) {
updatechars();
#endif
}
dldcmd[dlstackp] = (bool)testval;
dldcmd[dlstackp] = testval;
dldcmd[dlstackp] = !dldcmd[dlstackp];
return true;
}
Expand Down Expand Up @@ -189,8 +189,8 @@ if (chkCmd(1, "IF")) {
if (getArgCt(ltmp[1]) != 1) {cerr = 3; return true;}
uint8_t testval = logictest(ltmp[1]);
if (testval == 255) return true;
itdcmd[itstackp] = (bool)!testval;
didelseif = !itdcmd[itstackp];
didelseif = testval;
itdcmd[itstackp] = !testval;
return true;
}
if (chkCmd(1, "ELSE")) {
Expand Down Expand Up @@ -224,10 +224,11 @@ if (chkCmd(1, "ELSEIF")) {
}
copyStrSnip(cmd, j + 1, strlen(cmd), ltmp[1]);
if (getArgCt(ltmp[1]) != 1) {cerr = 3; return true;}
if (didelseif) {itdcmd[itstackp] = true; return true;}
uint8_t testval = logictest(ltmp[1]);
if (testval == 255) return true;
itdcmd[itstackp] = (bool)(!testval);
if (!didelseif) didelseif = !itdcmd[itstackp];
didelseif = testval;
itdcmd[itstackp] = !testval;
return true;
}
if (chkCmd(1, "ENDIF")) {
Expand Down Expand Up @@ -277,8 +278,8 @@ if (chkCmd(1, "FOR")) {
}
int testval = logictest(forbuf[2]);
if (testval == 255) return true;
fndcmd[fnstackp] = !(bool)testval;
if (!(fninfor[fnstackp] = (bool)testval)) {cerr = 0; return true;}
fndcmd[fnstackp] = !testval;
if (!(fninfor[fnstackp] = testval)) {cerr = 0; return true;}
if (!fninfor[fnstackp] && fnstack[fnstackp].cp == -1) {
sprintf(forbuf[0], "%lf", atof(forbuf[0]) - atof(forbuf[3]));
setVar(fnvar, forbuf[0], 2, -1);
Expand Down

0 comments on commit 809b6a4

Please sign in to comment.