aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2017-01-14 10:49:28 +0100
committerAlex Auvolat <alex@adnab.me>2017-01-14 10:49:28 +0100
commit809d660944cfb515e18bb7f1c1519392b4bccd53 (patch)
tree6ca443116167b5911daad27550bb0a9a1dd0bce6
parentc451e6d95b4d0a7ce27fcd97bc94fd3540649b73 (diff)
downloadkogata-809d660944cfb515e18bb7f1c1519392b4bccd53.tar.gz
kogata-809d660944cfb515e18bb7f1c1519392b4bccd53.zip
More Lua works
-rwxr-xr-xmake_cdrom.sh3
-rw-r--r--src/lib/include/stdlib.h2
-rw-r--r--src/lib/libc/stdio.c47
-rw-r--r--src/lib/libc/stdlib.c16
-rw-r--r--src/syslua/test.lua19
5 files changed, 80 insertions, 7 deletions
diff --git a/make_cdrom.sh b/make_cdrom.sh
index d8302a9..961ae7b 100755
--- a/make_cdrom.sh
+++ b/make_cdrom.sh
@@ -46,6 +46,9 @@ mkdir -p cdrom/sys/keymaps
cp build/keymaps/*.km cdrom/sys/keymaps
cp build/keymaps/fr.km cdrom/sys/keymaps/default.km
+mkdir -p cdrom/sys/lua
+cp -r src/syslua/* cdrom/sys/lua
+
cp README.md cdrom
# Setup config files
diff --git a/src/lib/include/stdlib.h b/src/lib/include/stdlib.h
index e451c52..7b8a392 100644
--- a/src/lib/include/stdlib.h
+++ b/src/lib/include/stdlib.h
@@ -12,7 +12,7 @@ int rand(void);
//int rand_r(unsigned int *seedp);
void srand(unsigned int seed);
-#define RAND_MAX 1
+#define RAND_MAX INT_MAX
void abort() __attribute__((__noreturn__));
diff --git a/src/lib/libc/stdio.c b/src/lib/libc/stdio.c
index fe3bd2c..d90c30b 100644
--- a/src/lib/libc/stdio.c
+++ b/src/lib/libc/stdio.c
@@ -151,11 +151,11 @@ FILE *fopen(const char *path, const char *mode) {
if (f == NULL) return NULL;
f->fd = sc_open(path, flags);
- dbg_printf("FOPEN %s %s: fd=%d\n", path, mode, f->fd);
+ dbg_printf("FOPEN %s %s: fd=%d, file=%p\n", path, mode, f->fd, f);
if (f->fd == 0) goto error;
if (!sc_stat_open(f->fd, &f->st)) goto error;
- dbg_printf("FOPEN %s %s: stat ok\n");
+ dbg_printf("FOPEN %s %s: stat ok\n", path, mode);
f->file_mode = flags;
f->flags = 0;
@@ -176,10 +176,13 @@ error:
return NULL;
}
FILE *freopen(const char *path, const char *mode, FILE *stream) {
+ dbg_printf("FREOPEN %s %s %p (UNIMPLEMENTED)\n", path, mode, stream);
// TODO
return NULL;
}
int fclose(FILE* f) {
+ dbg_printf("FCLOSE %p\n", f);
+
if (fflush(f) != 0) return EOF;
sc_close(f->fd);
@@ -198,6 +201,8 @@ int fclose(FILE* f) {
// ---------------
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
+ dbg_printf("FREAD %d * %d, file=%p\n", size, nmemb, stream);
+
ASSERT(size == 1); //TODO all cases
if (fflush(stream) == EOF) return 0;
@@ -209,19 +214,32 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
if (!(stream->st.type & (FT_CHARDEV | FT_CHANNEL | FT_DIR))) {
stream->pos += ret;
}
+
+ if (ret < size * nmemb) {
+ stream->flags |= STDIO_FL_EOF;
+ }
+
+ dbg_printf("fread = %d\n", ret);
+
return ret;
}
int fgetc(FILE *stream) {
+ dbg_printf("FGETC %p\n", stream);
+
// TODO buffering && ungetc
char x;
if (fread(&x, 1, 1, stream)) {
+ dbg_printf("fgetc = %d\n", x);
return x;
} else {
+ dbg_printf("fgetc = EOF\n");
return EOF;
}
}
char *fgets(char *s, int size, FILE *stream) {
+ dbg_printf("FGETS %p\n", stream);
+
int l = 0;
while (l < size - 1) {
int c = fgetc(stream);
@@ -249,6 +267,7 @@ int getc(FILE *stream) {
return fgetc(stream);
}
int ungetc(int c, FILE *stream) {
+ dbg_printf("UNGETC %p, %d (UNIMPLEMENTED)\n", stream, c);
// TODO
return 0;
}
@@ -269,6 +288,8 @@ void setlinebuf(FILE *stream) {
setvbuf(stream, NULL, _IOLBF, 0);
}
int setvbuf(FILE *stream, char *buf, int mode, size_t size) {
+ dbg_printf("SETVBUF %p, %p, %d, %d\n", stream, buf, mode, size);
+
if (stream == NULL || stream->fd == 0
|| !(stream->file_mode & FM_WRITE)
|| size == 0) return EOF;
@@ -298,8 +319,10 @@ int setvbuf(FILE *stream, char *buf, int mode, size_t size) {
}
int fflush(FILE* stream) {
- if (stream == NULL || stream->fd == 0
- || !(stream->file_mode & FM_WRITE)) return EOF;
+ dbg_printf("FFLUSH %p\n", stream);
+
+ if (!(stream->file_mode & FM_WRITE)) return 0;
+ if (stream == NULL || stream->fd == 0) return EOF;
if (stream->buf_mode != 0 && stream->out_buf_used > 0) {
size_t ret = sc_write(stream->fd, stream->pos, stream->out_buf_used, stream->out_buf);
@@ -329,6 +352,8 @@ int buffered_putc(int c, FILE* stream) {
// real output functions
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
+ dbg_printf("FWRITE %d * %d, %p\n", size, nmemb, stream);
+
if (stream == NULL || stream->fd == 0
|| !(stream->file_mode & FM_WRITE)) return 0;
@@ -342,6 +367,8 @@ size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
}
int fputc(int c, FILE *stream) {
+ dbg_printf("FPUTC %p %d\n", stream, c);
+
if (stream == NULL || stream->fd == 0
|| !(stream->file_mode & FM_WRITE)) return EOF;
@@ -352,6 +379,8 @@ int fputc(int c, FILE *stream) {
return c;
}
int fputs(const char *s, FILE *stream) {
+ dbg_printf("FPUTS %p\n", stream);
+
if (stream == NULL || stream->fd == 0
|| !(stream->file_mode & FM_WRITE)) return EOF;
@@ -412,9 +441,13 @@ void clearerr(FILE *stream) {
stream->flags = 0;
}
int feof(FILE *stream) {
+ dbg_printf("FEOF %p = %d\n", stream, (stream->flags & STDIO_FL_EOF));
+
return (stream->flags & STDIO_FL_EOF);
}
int ferror(FILE *stream) {
+ dbg_printf("FERROR %p = %d\n", stream, (stream->flags & STDIO_FL_ERR));
+
return (stream->flags & STDIO_FL_ERR);
}
int fileno(FILE *stream) {
@@ -428,6 +461,8 @@ int fileno(FILE *stream) {
int fseek(FILE *stream, long offset, int whence) {
+ dbg_printf("FSEEK %p, %d, %d\n", stream, offset, whence);
+
fpos_t pos;
if (whence == SEEK_SET) {
pos = offset;
@@ -445,6 +480,8 @@ long ftell(FILE *stream) {
return stream->pos;
}
void rewind(FILE *stream) {
+ dbg_printf("REWIND %p\n", stream);
+
fflush(stream);
stream->pos = 0;
}
@@ -453,6 +490,8 @@ int fgetpos(FILE *stream, fpos_t *pos) {
return 0;
}
int fsetpos(FILE *stream, const fpos_t *pos) {
+ dbg_printf("FSETPOS %p, %d\n", stream, pos);
+
fflush(stream);
if (*pos <= stream->st.size) {
stream->pos = *pos;
diff --git a/src/lib/libc/stdlib.c b/src/lib/libc/stdlib.c
index 7d08cd1..bc543be 100644
--- a/src/lib/libc/stdlib.c
+++ b/src/lib/libc/stdlib.c
@@ -3,12 +3,24 @@
#include <kogata/debug.h>
+static unsigned long
+x=123456789,y=362436069,z=521288629,w=88675123,v=886756453;
+/* replace defaults with five random seed values in calling program */
+unsigned long xorshift(void) {
+ unsigned long t;
+ t=(x^(x>>7)); x=y; y=z; z=w; w=v;
+ v=(v^(v<<6))^(t^(t<<13));
+ return (y+y+1)*v;
+}
+
int rand(void) {
- return 0; // TODO
+ int i = xorshift();
+ if (i < 0) i = -i;
+ return i;
}
void srand(unsigned int seed) {
- //TODO
+ x = seed;
}
void abort() {
diff --git a/src/syslua/test.lua b/src/syslua/test.lua
new file mode 100644
index 0000000..67b2852
--- /dev/null
+++ b/src/syslua/test.lua
@@ -0,0 +1,19 @@
+print("Hello, world! (printed)")
+
+
+return function(n)
+ local a = math.random(n)
+ while true do
+ io.stdout:write("Guess? ")
+ local x = tonumber(io.stdin:read())
+ if x < a then
+ print("Too small!")
+ elseif x > a then
+ print("Too big!")
+ else
+ print("Bingo!")
+ break
+ end
+ end
+end
+