aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2016-07-16 18:07:24 +0200
committerAlex Auvolat <alex@adnab.me>2016-07-16 18:07:24 +0200
commitbb2dd23f315bafa7b0b64845c2fe25d7a0893b10 (patch)
tree51657cd1ad8b6f1d5941604e75c251a246809034
parent3d6a857b9186ef6304ea6cf04627c2b787169f29 (diff)
downloadkogata-bb2dd23f315bafa7b0b64845c2fe25d7a0893b10.tar.gz
kogata-bb2dd23f315bafa7b0b64845c2fe25d7a0893b10.zip
Got Lua running \o/
-rw-r--r--gdb_cmd2
-rw-r--r--src/bin/lua/lua.c63
-rw-r--r--src/common/libc/printf.c56
-rw-r--r--src/lib/include/setjmp.h5
-rw-r--r--src/lib/include/stdio.h26
-rw-r--r--src/lib/libc/malloc.c13
-rw-r--r--src/lib/libc/readline.c1
-rw-r--r--src/lib/libc/setjmp.s75
-rw-r--r--src/lib/libc/start.c3
-rw-r--r--src/lib/libc/stdio.c191
-rw-r--r--src/lib/libc/stdlib.c2
-rw-r--r--src/sysbin/login/main.c3
12 files changed, 271 insertions, 169 deletions
diff --git a/gdb_cmd b/gdb_cmd
index 994e6c0..1db6ff3 100644
--- a/gdb_cmd
+++ b/gdb_cmd
@@ -1,3 +1,5 @@
target remote localhost:1234
symbol-file build/kernel.bin
break core/sys.c:panic_do
+break user/process.c:764
+break user/process.c:773
diff --git a/src/bin/lua/lua.c b/src/bin/lua/lua.c
index 545d23d..8f64451 100644
--- a/src/bin/lua/lua.c
+++ b/src/bin/lua/lua.c
@@ -4,6 +4,11 @@
** See Copyright Notice in lua.h
*/
+// {{ Customization for Kogata
+#define LUA_USE_READLINE 1
+// Other customization includes: removing static qualifiers
+// }}
+
#define lua_c
#include "lprefix.h"
@@ -98,15 +103,15 @@
-static lua_State *globalL = NULL;
+lua_State *globalL = NULL;
-static const char *progname = LUA_PROGNAME;
+const char *progname = LUA_PROGNAME;
/*
** Hook set by signal function to stop the interpreter.
*/
-static void lstop (lua_State *L, lua_Debug *ar) {
+void lstop (lua_State *L, lua_Debug *ar) {
(void)ar; /* unused arg. */
lua_sethook(L, NULL, 0, 0); /* reset hook */
luaL_error(L, "interrupted!");
@@ -119,13 +124,13 @@ static void lstop (lua_State *L, lua_Debug *ar) {
** this function only sets a hook that, when called, will stop the
** interpreter.
*/
-static void laction (int i) {
+void laction (int i) {
signal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
}
-static void print_usage (const char *badoption) {
+void print_usage (const char *badoption) {
lua_writestringerror("%s: ", progname);
if (badoption[1] == 'e' || badoption[1] == 'l')
lua_writestringerror("'%s' needs argument\n", badoption);
@@ -150,7 +155,7 @@ static void print_usage (const char *badoption) {
** Prints an error message, adding the program name in front of it
** (if present)
*/
-static void l_message (const char *pname, const char *msg) {
+void l_message (const char *pname, const char *msg) {
if (pname) lua_writestringerror("%s: ", pname);
lua_writestringerror("%s\n", msg);
}
@@ -161,7 +166,7 @@ static void l_message (const char *pname, const char *msg) {
** message on the top of the stack. It assumes that the error object
** is a string, as it was either generated by Lua or by 'msghandler'.
*/
-static int report (lua_State *L, int status) {
+int report (lua_State *L, int status) {
if (status != LUA_OK) {
const char *msg = lua_tostring(L, -1);
l_message(progname, msg);
@@ -174,7 +179,7 @@ static int report (lua_State *L, int status) {
/*
** Message handler used to run all chunks
*/
-static int msghandler (lua_State *L) {
+int msghandler (lua_State *L) {
const char *msg = lua_tostring(L, 1);
if (msg == NULL) { /* is error object not a string? */
if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
@@ -193,7 +198,7 @@ static int msghandler (lua_State *L) {
** Interface to 'lua_pcall', which sets appropriate message function
** and C-signal handler. Used to run all chunks.
*/
-static int docall (lua_State *L, int narg, int nres) {
+int docall (lua_State *L, int narg, int nres) {
int status;
int base = lua_gettop(L) - narg; /* function index */
lua_pushcfunction(L, msghandler); /* push message handler */
@@ -207,7 +212,7 @@ static int docall (lua_State *L, int narg, int nres) {
}
-static void print_version (void) {
+void print_version (void) {
lua_writestring(LUA_COPYRIGHT, strlen(LUA_COPYRIGHT));
lua_writeline();
}
@@ -221,7 +226,7 @@ static void print_version (void) {
** other arguments (before the script name) go to negative indices.
** If there is no script name, assume interpreter's name as base.
*/
-static void createargtable (lua_State *L, char **argv, int argc, int script) {
+void createargtable (lua_State *L, char **argv, int argc, int script) {
int i, narg;
if (script == argc) script = 0; /* no script name? */
narg = argc - (script + 1); /* number of positive indices */
@@ -234,18 +239,18 @@ static void createargtable (lua_State *L, char **argv, int argc, int script) {
}
-static int dochunk (lua_State *L, int status) {
+int dochunk (lua_State *L, int status) {
if (status == LUA_OK) status = docall(L, 0, 0);
return report(L, status);
}
-static int dofile (lua_State *L, const char *name) {
+int dofile (lua_State *L, const char *name) {
return dochunk(L, luaL_loadfile(L, name));
}
-static int dostring (lua_State *L, const char *s, const char *name) {
+int dostring (lua_State *L, const char *s, const char *name) {
return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
}
@@ -254,7 +259,7 @@ static int dostring (lua_State *L, const char *s, const char *name) {
** Calls 'require(name)' and stores the result in a global variable
** with the given name.
*/
-static int dolibrary (lua_State *L, const char *name) {
+int dolibrary (lua_State *L, const char *name) {
int status;
lua_getglobal(L, "require");
lua_pushstring(L, name);
@@ -268,7 +273,7 @@ static int dolibrary (lua_State *L, const char *name) {
/*
** Returns the string to be used as a prompt by the interpreter.
*/
-static const char *get_prompt (lua_State *L, int firstline) {
+const char *get_prompt (lua_State *L, int firstline) {
const char *p;
lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
p = lua_tostring(L, -1);
@@ -286,7 +291,7 @@ static const char *get_prompt (lua_State *L, int firstline) {
** message at the top of the stack ends with the above mark for
** incomplete statements.
*/
-static int incomplete (lua_State *L, int status) {
+int incomplete (lua_State *L, int status) {
if (status == LUA_ERRSYNTAX) {
size_t lmsg;
const char *msg = lua_tolstring(L, -1, &lmsg);
@@ -302,7 +307,7 @@ static int incomplete (lua_State *L, int status) {
/*
** Prompt the user, read a line, and push it into the Lua stack.
*/
-static int pushline (lua_State *L, int firstline) {
+int pushline (lua_State *L, int firstline) {
char buffer[LUA_MAXINPUT];
char *b = buffer;
size_t l;
@@ -327,7 +332,7 @@ static int pushline (lua_State *L, int firstline) {
** Try to compile line on the stack as 'return <line>;'; on return, stack
** has either compiled chunk or original line (if compilation failed).
*/
-static int addreturn (lua_State *L) {
+int addreturn (lua_State *L) {
const char *line = lua_tostring(L, -1); /* original line */
const char *retline = lua_pushfstring(L, "return %s;", line);
int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
@@ -345,7 +350,7 @@ static int addreturn (lua_State *L) {
/*
** Read multiple lines until a complete Lua statement
*/
-static int multiline (lua_State *L) {
+int multiline (lua_State *L) {
for (;;) { /* repeat until gets a complete statement */
size_t len;
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
@@ -367,7 +372,7 @@ static int multiline (lua_State *L) {
** the final status of load/call with the resulting function (if any)
** in the top of the stack.
*/
-static int loadline (lua_State *L) {
+int loadline (lua_State *L) {
int status;
lua_settop(L, 0);
if (!pushline(L, 1))
@@ -383,7 +388,7 @@ static int loadline (lua_State *L) {
/*
** Prints (calling the Lua 'print' function) any values on the stack
*/
-static void l_print (lua_State *L) {
+void l_print (lua_State *L) {
int n = lua_gettop(L);
if (n > 0) { /* any result to be printed? */
luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
@@ -400,7 +405,7 @@ static void l_print (lua_State *L) {
** Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
** print any results.
*/
-static void doREPL (lua_State *L) {
+void doREPL (lua_State *L) {
int status;
const char *oldprogname = progname;
progname = NULL; /* no 'progname' on errors in interactive mode */
@@ -419,7 +424,7 @@ static void doREPL (lua_State *L) {
/*
** Push on the stack the contents of table 'arg' from 1 to #arg
*/
-static int pushargs (lua_State *L) {
+int pushargs (lua_State *L) {
int i, n;
if (lua_getglobal(L, "arg") != LUA_TTABLE)
luaL_error(L, "'arg' is not a table");
@@ -432,7 +437,7 @@ static int pushargs (lua_State *L) {
}
-static int handle_script (lua_State *L, char **argv) {
+int handle_script (lua_State *L, char **argv) {
int status;
const char *fname = argv[0];
if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
@@ -460,7 +465,7 @@ static int handle_script (lua_State *L, char **argv) {
** any invalid argument). 'first' returns the first not-handled argument
** (either the script name or a bad argument in case of error).
*/
-static int collectargs (char **argv, int *first) {
+int collectargs (char **argv, int *first) {
int args = 0;
int i;
for (i = 1; argv[i] != NULL; i++) {
@@ -509,7 +514,7 @@ static int collectargs (char **argv, int *first) {
** Processes options 'e' and 'l', which involve running Lua code.
** Returns 0 if some code raises an error.
*/
-static int runargs (lua_State *L, char **argv, int n) {
+int runargs (lua_State *L, char **argv, int n) {
int i;
for (i = 1; i < n; i++) {
int option = argv[i][1];
@@ -529,7 +534,7 @@ static int runargs (lua_State *L, char **argv, int n) {
}
-static int handle_luainit (lua_State *L) {
+int handle_luainit (lua_State *L) {
const char *name = "=" LUA_INITVARVERSION;
const char *init = getenv(name + 1);
if (init == NULL) {
@@ -548,7 +553,7 @@ static int handle_luainit (lua_State *L) {
** Main body of stand-alone interpreter (to be called in protected mode).
** Reads the options and handles them all.
*/
-static int pmain (lua_State *L) {
+int pmain (lua_State *L) {
int argc = (int)lua_tointeger(L, 1);
char **argv = (char **)lua_touserdata(L, 2);
int script;
diff --git a/src/common/libc/printf.c b/src/common/libc/printf.c
index d1671c3..0498b2d 100644
--- a/src/common/libc/printf.c
+++ b/src/common/libc/printf.c
@@ -1,4 +1,5 @@
#include <stdarg.h>
+#include <stdbool.h>
#include <kogata/printf.h>
@@ -29,31 +30,56 @@ int vsnprintf(char *buff, size_t len, const char* format, va_list ap){
for(i = 0; format[i] != '\0' ; i++) {
if (format[i] == '%') {
i++;
+ int u = 0;
+ int l = 0;
+ bool spec = true;
+ while (spec) {
+ switch(format[i]) {
+ case 'l': {
+ l++;
+ i++;
+ break;
+ }
+ case 'u': {
+ u = 1;
+ i++;
+ break;
+ }
+ default:
+ spec = false;
+ }
+ }
switch(format[i]) {
case '%':
PUTCHAR('%');
break;
-
case 'i':;
case 'd': {
- int integer = va_arg(ap,int);
- int cpt2 = 0;
- char buff_int[16];
+ if (u) {
+ // TODO
+ } else {
+ long long int integer;
+ if (l == 0) integer = va_arg(ap, int);
+ if (l == 1) integer = va_arg(ap, long int);
+ if (l == 2) integer = va_arg(ap, long long int);
- if (integer<0)
- PUTCHAR('-');
+ int cpt2 = 0;
+ char buff_int[32];
- do {
- int m10 = integer%10;
- m10 = (m10 < 0)? -m10:m10;
- buff_int[cpt2++] = (char)('0'+ m10);
- integer = integer/10;
- } while(integer != 0);
+ if (integer<0)
+ PUTCHAR('-');
- for(cpt2 = cpt2 - 1; cpt2 >= 0; cpt2--)
- PUTCHAR(buff_int[cpt2]);
+ do {
+ int m10 = integer%10;
+ m10 = (m10 < 0)? -m10:m10;
+ buff_int[cpt2++] = (char)('0'+ m10);
+ integer = integer/10;
+ } while(integer != 0);
- break;
+ for(cpt2 = cpt2 - 1; cpt2 >= 0; cpt2--)
+ PUTCHAR(buff_int[cpt2]);
+ }
+ break;
}
case 'c': {
int value = va_arg(ap,int);
diff --git a/src/lib/include/setjmp.h b/src/lib/include/setjmp.h
index 46c3b5d..904ae4a 100644
--- a/src/lib/include/setjmp.h
+++ b/src/lib/include/setjmp.h
@@ -2,10 +2,7 @@
#include <stdint.h>
-struct _jmp_buf {
- uint32_t stuff[10]; // 40 bytes
-};
-typedef struct _jmp_buf jmp_buf;
+typedef uint32_t jmp_buf[10];
int setjmp(jmp_buf env);
diff --git a/src/lib/include/stdio.h b/src/lib/include/stdio.h
index c6b636d..a67553c 100644
--- a/src/lib/include/stdio.h
+++ b/src/lib/include/stdio.h
@@ -2,6 +2,8 @@
#include <stdarg.h>
+#include <proto/fs.h>
+
#include <kogata/printf.h>
#include <kogata/syscall.h>
@@ -11,7 +13,10 @@ void setup_libc_stdio();
//TODO below
struct file_t {
- // TODO
+ fd_t fd;
+ stat_t st;
+ int mode;
+ int flags;
};
typedef struct file_t FILE;
@@ -47,15 +52,15 @@ int fclose(FILE* f);
extern FILE *stdin, *stdout, *stderr;
-#define BUFSIZ 0
+#define BUFSIZ 1024
void setbuf(FILE *stream, char *buf);
void setbuffer(FILE *stream, char *buf, size_t size);
void setlinebuf(FILE *stream);
int setvbuf(FILE *stream, char *buf, int mode, size_t size);
-#define _IOFBF 0
-#define _IOLBF 1
-#define _IONBF 2
+#define _IOFBF 1
+#define _IOLBF 2
+#define _IONBF 4
typedef size_t fpos_t; //TODO
@@ -65,9 +70,9 @@ void rewind(FILE *stream);
int fgetpos(FILE *stream, fpos_t *pos);
int fsetpos(FILE *stream, const fpos_t *pos);
-#define SEEK_SET 0
-#define SEEK_CUR 1
-#define SEEK_END 2
+#define SEEK_SET 1
+#define SEEK_CUR 2
+#define SEEK_END 4
#define L_tmpnam 128
FILE *tmpfile(void);
@@ -76,11 +81,10 @@ char *tmpnam(char *s);
int rename(const char *old, const char *new);
int remove(const char *pathname);
-
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
-int dprintf(int fd, const char *format, ...);
-int sprintf(char *str, const char *format, ...);
+int vfprintf(FILE *stream, const char *format, va_list ap);
+
/* vim: set ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/libc/malloc.c b/src/lib/libc/malloc.c
index 473404b..fd37f76 100644
--- a/src/lib/libc/malloc.c
+++ b/src/lib/libc/malloc.c
@@ -54,7 +54,10 @@ void malloc_setup() {
void* malloc(size_t size) {
if (size == 0) return 0;
- return slab_alloc(mem_allocator, size);
+ //dbg_printf("malloc 0x%p -> ", size);
+ void* ret = slab_alloc(mem_allocator, size);
+ //dbg_printf("0x%p\n", ret);
+ return ret;
}
void* calloc(size_t nmemb, size_t sz) {
@@ -64,10 +67,16 @@ void* calloc(size_t nmemb, size_t sz) {
}
void* realloc(void* ptr, size_t sz) {
- return slab_realloc(mem_allocator, ptr, sz);
+ //dbg_printf("realloc 0x%p 0x%p -> ", ptr, sz);
+ void* ret = slab_realloc(mem_allocator, ptr, sz);
+ //dbg_printf("0x%p\n", ret);
+ return ret;
}
void free(void* ptr) {
+ if (ptr == 0) return;
+
+ //dbg_printf("free 0x%p\n", ptr);
slab_free(mem_allocator, ptr);
}
diff --git a/src/lib/libc/readline.c b/src/lib/libc/readline.c
index 20c2007..c93bfe2 100644
--- a/src/lib/libc/readline.c
+++ b/src/lib/libc/readline.c
@@ -21,6 +21,7 @@ char *readline(const char* prompt) {
// readline_history *h = &stdio_history;
puts(prompt);
+ fflush(stdout);
char* buf = malloc(READLINE_MAX_LEN);
if (buf == NULL) return NULL;
diff --git a/src/lib/libc/setjmp.s b/src/lib/libc/setjmp.s
index 164cc5b..a9b5f48 100644
--- a/src/lib/libc/setjmp.s
+++ b/src/lib/libc/setjmp.s
@@ -1,24 +1,18 @@
[GLOBAL setjmp]
setjmp:
- ; Store general purpose registers
- ; (in new stack frame)
- mov [esp+4], eax
- mov [esp+8], ebx
- mov [esp+12], ecx
- mov [esp+16], edx
- mov [esp+20], edi
- mov [esp+24], esi
- mov [esp+28], ebp
- mov [esp+32], esp
-
- ; Store flags
- pushf
- pop eax
- mov [esp+36], eax
+ ; get return address
+ mov edx, [esp]
+ ; get address of jmpbuf structure
+ mov ecx, [esp+4]
- ; Store return address
- mov eax, [esp]
- mov [esp+40], eax
+ ; Store general purpose registers
+ mov [ecx], ebx
+ mov [ecx+4], edx
+ mov [ecx+8], ebp
+ mov [ecx+12], esp
+ mov [ecx+16], esi
+ mov [ecx+20], edi
+ mov [ecx+24], eax
; return 0
xor eax, eax
@@ -27,27 +21,26 @@ setjmp:
[GLOBAL longjmp]
longjmp:
- ; on previous stack, resume return address
- mov eax, [esp+32]
- mov ebx, [esp+40]
- mov [eax], ebx
-
- ; resume flags
- mov eax, [esp+36]
- push eax
- popf
-
- ; load return value in eax
- mov eax, [esp+44]
- ; resume geneal purpose registers, except eax/esp
- mov ebx, [esp+8]
- mov ecx, [esp+12]
- mov edx, [esp+16]
- mov edi, [esp+20]
- mov esi, [esp+24]
- mov ebp, [esp+28]
-
- ; resume previous esp
- mov esp, [esp+32]
- ; return as if we were the setjmp call
+ ; get address of jmpbuf structure
+ mov ecx, [esp+4]
+ ; get retun value
+ mov eax, [esp+8]
+
+ ; load general purpose registers
+ ; (edx contains the return address)
+ mov ebx, [ecx]
+ mov edx, [ecx+4]
+ mov ebp, [ecx+8]
+ mov esp, [ecx+12]
+ mov esi, [ecx+16]
+ mov edi, [ecx+20]
+
+ ; make sure return value is nonzero
+ test eax, eax
+ jnz _doret
+ inc eax
+_doret:
+ ; store back return address
+ mov [esp], edx
ret
+
diff --git a/src/lib/libc/start.c b/src/lib/libc/start.c
index 185d90f..49bf6a3 100644
--- a/src/lib/libc/start.c
+++ b/src/lib/libc/start.c
@@ -12,8 +12,9 @@ void __libc_start() {
setup_libc_stdio();
// TODO : more setup ? yes, for args, for env...
+ char *argv[] = {"bin"};
- int ret = main(0, 0);
+ int ret = main(1, argv);
sc_exit(ret);
}
diff --git a/src/lib/libc/stdio.c b/src/lib/libc/stdio.c
index 8d4577e..c849683 100644
--- a/src/lib/libc/stdio.c
+++ b/src/lib/libc/stdio.c
@@ -10,15 +10,31 @@ FILE *stdin = 0;
FILE *stdout = 0;
FILE *stderr = 0;
-void setup_libc_stdio() {
- fd_t tty_io = STD_FD_TTY_STDIO;
- // fd_t tty_in = STD_FD_STDIN;
- // fd_t tty_out = STD_FD_STDOUT;
- // fd_t tty_err = STD_FD_STDERR;
+FILE libc_tty_stdio, libc_stdin, libc_stdout, libc_stderr;
- // TODO
- if (true) {
- sc_fctl(tty_io, FC_SET_BLOCKING, 0);
+void setup_libc_stdio() {
+ if (sc_stat_open(STD_FD_TTY_STDIO, &libc_tty_stdio.st)) {
+ libc_tty_stdio.fd = STD_FD_TTY_STDIO;
+ sc_fctl(libc_tty_stdio.fd, FC_SET_BLOCKING, 0);
+ // TODO: initialize libc_tty_stdio as a TTY
+ stdin = &libc_tty_stdio;
+ stdout = &libc_tty_stdio;
+ stderr = &libc_tty_stdio;
+ }
+ if (sc_stat_open(STD_FD_STDIN, &libc_stdin.st)) {
+ libc_stdin.fd = STD_FD_STDIN;
+ // TODO: initialize
+ stdin = &libc_stdin;
+ }
+ if (sc_stat_open(STD_FD_STDOUT, &libc_stdout.st)) {
+ libc_stdout.fd = STD_FD_STDOUT;
+ // TODO: initialize
+ stdout = &libc_stdout;
+ }
+ if (sc_stat_open(STD_FD_STDERR, &libc_stderr.st)) {
+ libc_stderr.fd = STD_FD_STDERR;
+ // TODO: initialize
+ stderr = &libc_stderr;
}
}
@@ -37,13 +53,12 @@ int puts(const char* s) {
int printf(const char* fmt, ...) {
va_list ap;
- char buffer[256];
va_start(ap, fmt);
- vsnprintf(buffer, 256, fmt, ap);
+ int ret = vfprintf(stdout, fmt, ap);
va_end(ap);
- return puts(buffer);
+ return ret;
}
// ==================
@@ -51,14 +66,59 @@ int printf(const char* fmt, ...) {
// ==================
-int fgetc(FILE *stream) {
+FILE *fopen(const char *path, const char *mode) {
// TODO
return 0;
}
-char *fgets(char *s, int size, FILE *stream) {
+FILE *freopen(const char *path, const char *mode, FILE *stream) {
// TODO
return 0;
}
+int fclose(FILE* f) {
+ // TODO
+ return 0;
+}
+
+
+// ---------------
+// INPUT FUNCTIONS
+// ---------------
+
+size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
+ if (stream == NULL || stream->fd == 0) return 0;
+
+ // TODO buffering
+ // TODO position
+ return sc_read(stream->fd, 0, size * nmemb, ptr);
+}
+
+int fgetc(FILE *stream) {
+ // TODO buffering && ungetc
+ char x;
+ if (fread(&x, 1, 1, stream)) {
+ return x;
+ } else {
+ return EOF;
+ }
+}
+char *fgets(char *s, int size, FILE *stream) {
+ int l = 0;
+ while (l < size - 1) {
+ int c = fgetc(stream);
+ if (c == EOF) {
+ break;
+ } else if (c == '\b') {
+ if (l > 0) l--;
+ // TODO if terminal write back space or something
+ } else {
+ s[l] = c;
+ l++;
+ if (c == '\n') break;
+ }
+ }
+ s[l] = 0;
+ return s;
+}
int getc(FILE *stream) {
// TODO
return 0;
@@ -68,77 +128,90 @@ int ungetc(int c, FILE *stream) {
return 0;
}
+// ----------------
+// OUTPUT FUNCTIONS
+// ----------------
+
+size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
+ if (stream == NULL || stream->fd == 0) return 0;
+
+ // TODO buffering
+ // TODO position
+ return sc_write(stream->fd, 0, size * nmemb, ptr);
+}
+
int fputc(int c, FILE *stream) {
- // TODO
- return 0;
+ unsigned char x = c;
+ return fwrite(&x, 1, 1, stream);
}
int fputs(const char *s, FILE *stream) {
- // TODO
- return 0;
+ return fwrite(s, strlen(s), 1, stream);
}
int putc(int c, FILE *stream) {
- // TODO
- return 0;
+ return fputc(c, stream);
}
-FILE *fopen(const char *path, const char *mode) {
- // TODO
- return 0;
+int fprintf(FILE *stream, const char *fmt, ...) {
+ va_list ap;
+
+ va_start(ap, fmt);
+ int ret = vfprintf(stream, fmt, ap);
+ va_end(ap);
+
+ return ret;
}
-FILE *freopen(const char *path, const char *mode, FILE *stream) {
- // TODO
- return 0;
+int vfprintf(FILE *stream, const char *format, va_list ap) {
+ char buf[1024];
+ vsnprintf(buf, 1024, format, ap);
+
+ return fputs(buf, stream);
}
-void clearerr(FILE *stream) {
- // TODO
+// buffering
+
+void setbuf(FILE *stream, char *buf) {
+ setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
}
-int feof(FILE *stream) {
+void setbuffer(FILE *stream, char *buf, size_t size) {
// TODO
- return 0;
}
-int ferror(FILE *stream) {
+void setlinebuf(FILE *stream) {
+ setvbuf(stream, NULL, _IOLBF, 0);
+}
+int setvbuf(FILE *stream, char *buf, int mode, size_t size) {
// TODO
return 0;
}
-int fileno(FILE *stream) {
+
+int fflush(FILE* f) {
// TODO
return 0;
}
+// ---------------------
+// COMPLICATED FUNCTIONS
+// ---------------------
-size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
+void clearerr(FILE *stream) {
// TODO
- return 0;
}
-size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
+int feof(FILE *stream) {
// TODO
return 0;
}
-
-int fflush(FILE* f) {
+int ferror(FILE *stream) {
// TODO
return 0;
}
-int fclose(FILE* f) {
+int fileno(FILE *stream) {
// TODO
return 0;
}
-void setbuf(FILE *stream, char *buf) {
- // TODO
-}
-void setbuffer(FILE *stream, char *buf, size_t size) {
- // TODO
-}
-void setlinebuf(FILE *stream) {
- // TODO
-}
-int setvbuf(FILE *stream, char *buf, int mode, size_t size) {
- // TODO
- return 0;
-}
+// ------------------
+// POSITION FUNCTIONS
+// ------------------
int fseek(FILE *stream, long offset, int whence) {
@@ -161,6 +234,10 @@ int fsetpos(FILE *stream, const fpos_t *pos) {
return 0;
}
+// ---------------------
+// PATH & FILE FUNCTIONS
+// ---------------------
+
FILE *tmpfile(void) {
// TODO
return 0;
@@ -181,18 +258,4 @@ int remove(const char *pathname) {
-int fprintf(FILE *stream, const char *format, ...) {
- // TODO
- return 0;
-}
-int dprintf(int fd, const char *format, ...) {
- // TODO
- return 0;
-}
-int sprintf(char *str, const char *format, ...) {
- // TODO
- return 0;
-}
-
-
/* vim: set ts=4 sw=4 tw=0 noet :*/
diff --git a/src/lib/libc/stdlib.c b/src/lib/libc/stdlib.c
index 540d53c..7d08cd1 100644
--- a/src/lib/libc/stdlib.c
+++ b/src/lib/libc/stdlib.c
@@ -57,7 +57,7 @@ double strtod(const char *nptr, char **endptr) {
for (int i = 0; i < exp; i++) val /= 10;
}
}
- if (endptr != NULL) *endptr = (char*)(p-1);
+ if (endptr != NULL) *endptr = (char*)p;
return val * sign;
}
diff --git a/src/sysbin/login/main.c b/src/sysbin/login/main.c
index c4cd260..d86eb39 100644
--- a/src/sysbin/login/main.c
+++ b/src/sysbin/login/main.c
@@ -41,7 +41,8 @@ int main(int argc, char **argv) {
&& sc_bind_fd(shell_pid, STD_FD_TTY_STDIO, tc.b);
if (!ok) PANIC("[login] Could not bind to shell process.");
- ok = sc_proc_exec(shell_pid, "sys:/bin/shell.bin");
+ // ok = sc_proc_exec(shell_pid, "sys:/bin/shell.bin");
+ ok = sc_proc_exec(shell_pid, "root:/bin/lua.bin");
if (!ok) PANIC("[login] Could not run shell.bin");
proc_status_t s;