summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-07-25 05:17:59 +0200
committerkdx <kikoodx@paranoici.org>2023-07-25 05:17:59 +0200
commit6429fcae13a343372c7cff575be577702f059e74 (patch)
treed0f8da2f97789babef93365d2188b8e94a25f712
parentb988b13efdc01a73881c1b24db3c79b3f1df83ee (diff)
downloadegecs-6429fcae13a343372c7cff575be577702f059e74.tar.gz
improve prompt
-rw-r--r--egecs.golem33
-rw-r--r--iron.golem171
2 files changed, 133 insertions, 71 deletions
diff --git a/egecs.golem b/egecs.golem
index 378bdeb..44e5dce 100644
--- a/egecs.golem
+++ b/egecs.golem
@@ -5,9 +5,8 @@ main() {
egecs() {
board_init();
- loop {
- egecs_cycle();
- }
+ loop if (egecs_cycle())
+ break;
}
cls() {
@@ -15,6 +14,8 @@ cls() {
wrt 'c';
}
+cmd_quit(s) => (strcmp(s, ":q\n") == 0) | (strcmp(s, ":quit\n") == 0);
+
egecs_cycle() {
local from, to, repeat;
@@ -23,9 +24,11 @@ egecs_cycle() {
board_display(NULL);
col_green();
from = geadline("> ");
- if (white(board_get(from)))
- break;
if (from) {
+ if (cmd_quit(from))
+ return 1;
+ if (white(board_get(from)))
+ break;
free(from);
from = NULL;
}
@@ -34,9 +37,11 @@ egecs_cycle() {
board_display(from);
col_green();
to = geadline("> ");
- if ((to != NULL) & (board_get(to) != INVALID) & (white(board_get(to)) == 0))
- break;
if (to) {
+ if (cmd_quit(to))
+ return 1;
+ if ((board_get(to) != INVALID) & (white(board_get(to)) == 0))
+ break;
free(to);
to = NULL;
}
@@ -55,9 +60,11 @@ egecs_cycle() {
board_display(NULL);
col_red();
from = geadline("> ");
- if (black(board_get(from)))
- break;
if (from) {
+ if (cmd_quit(from))
+ return 1;
+ if (black(board_get(from)))
+ break;
free(from);
from = NULL;
}
@@ -66,9 +73,11 @@ egecs_cycle() {
board_display(from);
col_red();
to = geadline("> ");
- if ((to != NULL) & (board_get(to) != INVALID) & (black(board_get(to)) == 0))
- break;
if (to) {
+ if (cmd_quit(to))
+ return 1;
+ if ((board_get(to) != INVALID) & (black(board_get(to)) == 0))
+ break;
free(to);
to = NULL;
}
@@ -82,4 +91,6 @@ egecs_cycle() {
if (repeat == 0)
break;
}
+
+ return 0;
}
diff --git a/iron.golem b/iron.golem
index 0f4e3cf..9908d69 100644
--- a/iron.golem
+++ b/iron.golem
@@ -40,6 +40,20 @@ aton_s(str)
return out;
}
bzero(tab, size) memset(tab, size, 0);
+contain_only(to_big, to_find)
+{
+ local tmp;
+
+ tmp = to_big;
+ loop
+ {
+ if ([tmp] == 0)
+ return 1;
+ if (strchr(to_find, [tmp]) == 0)
+ return 0;
+ tmp++;
+ }
+}
define NUM_MAX = 0xffff;
define NUM_MIN = 0x0000;
define NUM_S_MIN = 0x8000;
@@ -59,7 +73,7 @@ free_tab(tab)
}
free(tab);
}
-define HEAP_SIZE = 0x4000;
+define HEAP_SIZE = 0x8000;
global heap[HEAP_SIZE] = 0;
define PADDING_SIZE = 4;
@@ -233,11 +247,13 @@ geadline(prompt) {
size = 0,
i = 0,
c,
+ a,
buf = galloc(capacity);
if (prompt)
putstr(prompt);
if (buf == NULL)
return NULL;
+ [buf] = 0;
loop {
red &c;
@@ -246,68 +262,96 @@ geadline(prompt) {
free(buf);
return 0;
}
- } else if (c == 0x007f) {
- if (size) {
+ } else if (c == 0x1b) {
+ // ESC code
+ red &c; // skip [
+ red &c; // value
+
+ if ((c == 'A') & (size > 0)) {
+ loop {
+ if (i == 0)
+ break;
+ i = i - 1;
+ esccode('D');
+ }
+ } else if ((c == 'B') & (i < size)) {
+ loop {
+ if (i >= size)
+ break;
+ i = i + 1;
+ esccode('C');
+ }
+ } else if ((c == 'C') & (i < size)) {
+ i = i + 1;
+ esccode('C');
+ } else if ((c == 'D') & (i > 0)) {
+ i = i - 1;
+ esccode('D');
+ }
+ wrt '\a';
+ } else if (c == 0x7f) {
+ if (i) {
+ a = i - 1;
+ loop {
+ if (a >= size)
+ break;
+ [buf + a] = [buf + a + 1];
+ a = a + 1;
+ }
size = size - 1;
i = i - 1;
[buf + size] = 0;
- wrt 0x1b;
- wrt 0x5b;
- wrt 0x44;
+ wrt '\r';
+ if (prompt)
+ putstr(prompt);
+ putstr(buf);
wrt ' ';
- wrt 0x1b;
- wrt 0x5b;
- wrt 0x44;
+ esccode('D');
+ a = size - i;
+ loop {
+ if (a == 0)
+ break;
+ a = a - 1;
+ esccode('D');
+ }
}
} else {
size = size + 1;
- if (size > capacity) {
- buf = reallocarray(buf, capacity, capacity * 2);
+ if (size >= capacity) {
+ buf = realloc(buf, capacity * 2);
if (buf == NULL)
return NULL;
capacity = capacity * 2;
}
+ a = size - i - 1;
+ loop {
+ if ((a == 0xffff) | (a == 0))
+ break;
+ [buf + i + a] = [buf + i + a - 1];
+ a = a - 1;
+ }
[buf + i] = c;
+ [buf + size] = 0;
+ putstr(buf + i);
+ a = strlen(buf + i) - 1;
+ loop {
+ if ((a == 0xffff) | (a == 0))
+ break;
+ a = a - 1;
+ esccode('D');
+ }
i = i + 1;
- [buf + i] = 0;
- wrt c;
if (c == '\n')
return buf;
}
}
}
-define GET_LINE_BUFFER=100;
-
-get_line()
-{
- local tmp;
- local out;
- local c;
- local i;
- out = NULL;
- i = 0;
- loop
- {
- if (i % GET_LINE_BUFFER == 0)
- {
- tmp = reallocarray(out, i, i + GET_LINE_BUFFER + 1);
- if (tmp == NULL)
- return NULL;
- }
- red &c;
- if (c == 0xffff)
- return NULL;
- [tmp + i] = c;
- i++;
- if (c == '\n')
- break;
- }
- [tmp + i] = 0;
- out = strdup(tmp);
- free(tmp);
- return out;
+esccode(c) {
+ wrt 0x1b;
+ wrt '[';
+ wrt c;
}
get_raw_bit(number)
{
@@ -494,26 +538,31 @@ puttab_num(tab, size)
}
putchar(']');
}
-reallocarray(ptr, nmemb, size)
+realloc(ptr, new_size)
{
- local tmp;
- local start;
+ local block_ptr;
+ local new_space;
+ local i;
- start = ptr;
- tmp = galloc(size);
- if (tmp == NULL)
- return NULL;
+ new_space = galloc(new_size);
+ if (new_space == NULL)
+ {
+ free(ptr);
+ return (NULL);
+ }
if (ptr == NULL)
- return tmp;
+ return new_space;
+ block_ptr = ptr - LOCATION_DATA;
+ i = 0;
loop
{
- if (start - ptr == nmemb)
+ if (i == new_size | i == [block_ptr + LOCATION_SIZE])
break;
- [tmp + start - ptr] = [start];
- start++;
+ [new_space + i] = [ptr + i];
+ i++;
}
free(ptr);
- return tmp;
+ return new_space;
}
replace(str, fill, start, stop)
{
@@ -620,14 +669,14 @@ strcat(dst, src)
}
strchr(str, c)
{
- local i = 0;
+ local start = str;
loop {
- if ([str + i] == c)
- return (str + i);
- if ([str + i] == 0)
+ if ([start] == c)
+ return (start);
+ if ([start] == 0)
return (0);
- i++;
+ start++;
}
}
strchri(str, c)
@@ -678,6 +727,8 @@ strdup(str)
strlen(str) {
local i = 0;
+ if (str == NULL)
+ return 0;
loop {
if ([str + i] == 0)
return (i);