summaryrefslogtreecommitdiff
path: root/src/user
diff options
context:
space:
mode:
Diffstat (limited to 'src/user')
-rw-r--r--src/user/app/kbasic/basic.h2
-rw-r--r--src/user/app/kbasic/commands.c24
-rw-r--r--src/user/app/kbasic/control.c45
-rw-r--r--src/user/app/kbasic/count.bas7
-rw-r--r--src/user/app/kbasic/guess.bas14
-rw-r--r--src/user/app/kbasic/lex.c23
-rw-r--r--src/user/app/kbasic/main.c24
-rw-r--r--src/user/app/kbasic/pppg.bas14
-rw-r--r--src/user/app/kbasic/prime.bas20
-rw-r--r--src/user/app/kbasic/tables.bas6
-rw-r--r--src/user/app/kbasic/var.c2
-rw-r--r--src/user/app/test/main.c10
-rw-r--r--src/user/app/yosh/main.c2
-rw-r--r--src/user/lib/libc/include/readline.h6
-rw-r--r--src/user/lib/libc/include/tce/syscall.h2
-rw-r--r--src/user/lib/libc/std/ctype.c4
-rw-r--r--src/user/lib/libc/std/readline.c38
-rw-r--r--src/user/lib/libc/std/stdio.c2
-rw-r--r--src/user/lib/libc/std/stdlib.c1
-rw-r--r--src/user/lib/libc/tce/syscall.c2
20 files changed, 142 insertions, 106 deletions
diff --git a/src/user/app/kbasic/basic.h b/src/user/app/kbasic/basic.h
index b3491da..0cc5f0c 100644
--- a/src/user/app/kbasic/basic.h
+++ b/src/user/app/kbasic/basic.h
@@ -35,6 +35,7 @@
#define LIST 13
#define PRINT 20 // Built-in commands
#define INPUT 21
+#define COLOR 22
#define RND 50 // Built-in functions
// Buffer size definitions
@@ -67,6 +68,7 @@ void serror(int);
// BASIC commands
void exec_print(), input();
+void color(); void set_color();
void assignment();
void get_exp(int*);
diff --git a/src/user/app/kbasic/commands.c b/src/user/app/kbasic/commands.c
index f60f32a..b88e57e 100644
--- a/src/user/app/kbasic/commands.c
+++ b/src/user/app/kbasic/commands.c
@@ -4,6 +4,9 @@
#include "basic.h"
+static int fg_color = 7, bg_color = 0;
+
+
/* Assign a variable a value. */
void assignment()
{
@@ -33,8 +36,6 @@ void assignment()
*var = value;
}
-
-
/* Execute a simple version of the BASIC PRINT statement */
void exec_print()
{
@@ -95,9 +96,26 @@ void input()
var = find_var(token); /* get the input var */
- scanf("%d\n", var); /* read input */
+ scanf("%d", var); /* read input */
/* flush input */
while ((tmp = getchar()) != '\n' && tmp != EOF);
}
+
+/* COLOR statement */
+void color() {
+ get_exp(&fg_color);
+
+ get_token();
+ if (*token == ',') {
+ get_exp(&bg_color);
+ } else if (token_type != DELIMITER) {
+ serror(0);
+ }
+ set_color();
+}
+
+void set_color() {
+ printf("\x1b[%dm\x1b[%dm\x1b[%dm", fg_color % 8 + 30, (fg_color > 7 ? 1 : 22), bg_color % 16 + 40);
+}
diff --git a/src/user/app/kbasic/control.c b/src/user/app/kbasic/control.c
index 1ab22ff..87731c4 100644
--- a/src/user/app/kbasic/control.c
+++ b/src/user/app/kbasic/control.c
@@ -1,7 +1,7 @@
+#include <tce/syscall.h>
+
#include <stdio.h>
#include <string.h>
-// #include <sys/wait.h>
-// #include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <setjmp.h>
@@ -94,18 +94,25 @@ void insert_line(char *l, int n) {
}
}
- p = malloc(prog_l + strlen(l) + 3);
+ p = malloc(prog_l + strlen(l) + 4);
strncpy(p, program, (a - program)); // copy [0, a[
- p[a - program] = 0;
+
+ char *p2 = p + (a - program);
+ if (*(p2-1) != '\n') *(p2++) = '\n';
+ *p2 = 0;
+
// if l is an empty line, do not copy.
for (i_n = 0; l[i_n]; i_n++) {
if (!isdigit(l[i_n]) && l[i_n] != '\n' && l[i_n] != '\r') {
- strcpy(p + (a - program), l); // copy l
+ strcpy(p2, l); // copy l
break;
}
}
- strcat(p + (a - program), b); // copy [b, end[
+ if (p2[strlen(p2)-1] != '\n') strcat(p2, "\n");
+
+ strcat(p2, b); // copy [b, end[
+ if (p2[strlen(p2)-1] != '\n') strcat(p2, "\n");
load_program(p); // reparse labels
}
@@ -140,6 +147,9 @@ int start(char *entry) {
case PRINT:
exec_print();
break;
+ case COLOR:
+ color();
+ break;
case GOTO:
exec_goto();
break;
@@ -455,30 +465,25 @@ char *gpop()
Forks and runs another instance of the interpreter
*/
void exec_exec() {
-
- /* TODO
int pid;
get_token();
if (token_type==QUOTE) {
- pid = fork();
- if (pid < 0) {
- printf("Error: could not fork.\n");
+ char *args[2];
+ args[0] = token;
+ args[1] = 0;
+
+ pid = run(interp_name, args, term.fd);
+ if (pid <= 0) {
+ printf("Error: could not run '%s'.\n", interp_name);
} else {
- if (pid == 0) {
- execlp(interp_name, interp_name, token, 0);
- printf("Error: could not exec.\n");
- exit(0);
- } else {
- wait(0);
- }
+ int ret = waitpid(pid, 1);
+ printf("[exited: %i]\n", ret);
}
get_token();
} else {
serror(0);
}
- */
- printf("EXEC not available.\n");
}
/* LOAD function
diff --git a/src/user/app/kbasic/count.bas b/src/user/app/kbasic/count.bas
new file mode 100644
index 0000000..5741a72
--- /dev/null
+++ b/src/user/app/kbasic/count.bas
@@ -0,0 +1,7 @@
+05 print "Let me count to 100:"
+10 a = 0
+20 print a;
+30 a = a + 1
+40 if a = 100 then goto 60
+50 goto 20
+60 end
diff --git a/src/user/app/kbasic/guess.bas b/src/user/app/kbasic/guess.bas
new file mode 100644
index 0000000..f15ba49
--- /dev/null
+++ b/src/user/app/kbasic/guess.bas
@@ -0,0 +1,14 @@
+
+n = RND % 100
+e = 0
+
+20 input "Guess the number: ", i
+e = e + 1
+if i > n then print "Too big!"
+if i < n then print "Too small!"
+if i = n then goto 42
+goto 20
+
+42 print "That's right! You got it with only ", e, " guesses!"
+end
+
diff --git a/src/user/app/kbasic/lex.c b/src/user/app/kbasic/lex.c
index 055b502..5d3487b 100644
--- a/src/user/app/kbasic/lex.c
+++ b/src/user/app/kbasic/lex.c
@@ -11,6 +11,7 @@ struct commands { /* keyword lookup table */
} table[] = { /* Commands must be entered lowercase */
{"print", PRINT}, /* in this table. */
{"input", INPUT},
+ {"color", COLOR},
{"if", IF},
{"then", THEN},
@@ -84,7 +85,8 @@ int get_token()
if(*prog_ip=='\0') { /* end of file */
*token=0;
tok = FINISHED;
- return(token_type=DELIMITER);
+ token_type=DELIMITER;
+ goto get_tok_end;
}
while(iswhite(*prog_ip)) ++prog_ip; /* skip over white space */
@@ -93,12 +95,14 @@ int get_token()
++prog_ip; ++prog_ip;
tok = EOL; *token='\r';
token[1]='\n'; token[2]=0;
- return (token_type = DELIMITER);
+ token_type = DELIMITER;
+ goto get_tok_end;
}
if (*prog_ip=='\n') { /* lf (unix newline) */
++prog_ip;
tok = EOL; *token='\n'; token[1] = 0;
- return (token_type = DELIMITER);
+ token_type = DELIMITER;
+ goto get_tok_end;
}
if(strchr("+-*^/%=;(),><", *prog_ip)){ /* delimiter */
@@ -106,7 +110,8 @@ int get_token()
prog_ip++; /* advance to next position */
temp++;
*temp=0;
- return (token_type=DELIMITER);
+ token_type=DELIMITER;
+ goto get_tok_end;
}
if(*prog_ip=='"') { /* quoted string */
@@ -117,7 +122,8 @@ int get_token()
}
if(*prog_ip=='\r' || *prog_ip=='\n') serror(1);
prog_ip++;*temp=0;
- return(token_type=QUOTE);
+ token_type=QUOTE;
+ goto get_tok_end;
}
if(isdigit(*prog_ip)) { /* number */
@@ -126,7 +132,8 @@ int get_token()
prog_ip++;
}
*temp = '\0';
- return(token_type = NUMBER);
+ token_type = NUMBER;
+ goto get_tok_end;
}
if(isalpha(*prog_ip)) { /* var or command */
@@ -145,6 +152,10 @@ int get_token()
if(!tok) token_type = VARIABLE;
else token_type = COMMAND; /* is a command */
}
+
+get_tok_end:
+ //printf("[%d.%d.%s]", token_type, tok, token);
+
return token_type;
}
diff --git a/src/user/app/kbasic/main.c b/src/user/app/kbasic/main.c
index 1003a7b..c76980c 100644
--- a/src/user/app/kbasic/main.c
+++ b/src/user/app/kbasic/main.c
@@ -7,10 +7,7 @@
int main(int argc, char **argv) {
char *p_buf;
- srand(0); // not very usefull...
-
- readline_history hist;
- hist.str = 0; hist.max = 10;
+ srand(1); // not very usefull...
interp_name = argv[0];
init();
@@ -22,18 +19,19 @@ int main(int argc, char **argv) {
load_program(p_buf);
start(p_buf);
} else if (argc == 1) {
+ readline_history hist;
+ hist.str = 0; hist.max = 10;
+
printf("KBASIC 0.1\n"
"Press ^D to quit interpreter.\n\n");
for (;;) {
- printf("> ");
+ printf("\x1b[0m\x1b[32m> \x1b[1m");
char *l = freadline(stdin, &hist);
- if (l == NULL) break;
+ set_color();
- if (!strcmp(l, "exit")) {
- free(l);
- break;
- }
+ if (l == NULL) break;
+ if (!strcmp(l, "exit")) break;
prog_ip = l;
get_token();
@@ -42,11 +40,11 @@ int main(int argc, char **argv) {
} else { // directly execute
start(l);
}
- free(l);
}
} else {
- printf("usage:\n\t%s <filename>\trun BASIC file\n", argv[0]);
- printf("\n\t%s\t\t\tinteractive BASIC interpreter\n", argv[0]);
+ printf("usage:\n");
+ printf(" %s <filename> run BASIC file\n", argv[0]);
+ printf(" %s interactive BASIC interpreter\n", argv[0]);
exit(1);
}
diff --git a/src/user/app/kbasic/pppg.bas b/src/user/app/kbasic/pppg.bas
deleted file mode 100644
index 32fc050..0000000
--- a/src/user/app/kbasic/pppg.bas
+++ /dev/null
@@ -1,14 +0,0 @@
-
-n = RND % 100
-e = 0
-
-20 input "Devinez le nombre: ", i
-e = e + 1
-if i > n then print "Trop grand !"
-if i < n then print "Trop petit !"
-if i = n then goto 42
-goto 20
-
-42 print "Bravo, vous avez trouvé en ", e, " essais!"
-end
-
diff --git a/src/user/app/kbasic/prime.bas b/src/user/app/kbasic/prime.bas
new file mode 100644
index 0000000..bbe4bd0
--- /dev/null
+++ b/src/user/app/kbasic/prime.bas
@@ -0,0 +1,20 @@
+10 i = 2
+20 gosub 100
+30 if p = 1 then print i;
+40 i = i + 1
+50 if i > 1000 then goto 300
+60 goto 20
+
+100 p = 1
+110 d = 2
+120 if d * d > i then return
+130 if i % d = 0 then goto 200
+140 d = d + 1
+150 goto 120
+200 p = 0
+210 return
+
+300 print
+310 print "That's all primes for today!"
+320 end
+
diff --git a/src/user/app/kbasic/tables.bas b/src/user/app/kbasic/tables.bas
index 31e9ce3..a6aaf9c 100644
--- a/src/user/app/kbasic/tables.bas
+++ b/src/user/app/kbasic/tables.bas
@@ -1,12 +1,12 @@
-input "Taille de la table: ", k
-print "Table d'addition:"
+input "Table size: ", k
+print "Addition table:"
for i = 0 to k
for j = 0 to k
print i+j;
next
print
next
-print "Table de multiplication:"
+print "Multiplication table:"
for i = 1 to k
for j = 1 to k
print i * j;
diff --git a/src/user/app/kbasic/var.c b/src/user/app/kbasic/var.c
index a49ce61..6ed2255 100644
--- a/src/user/app/kbasic/var.c
+++ b/src/user/app/kbasic/var.c
@@ -17,3 +17,5 @@ int* find_var(char *s)
}
return variables + (toupper(*s)-'A');
}
+
+
diff --git a/src/user/app/test/main.c b/src/user/app/test/main.c
index 3db24b3..2c42cf8 100644
--- a/src/user/app/test/main.c
+++ b/src/user/app/test/main.c
@@ -1,11 +1,15 @@
#include <tce/syscall.h>
#include <stdio.h>
+#include <stdlib.h>
int threads = 1;
void thread_cascade(void* d) {
int n = (int)d;
+ char *v = malloc(2048);
+ if (!v) printk("!");
+
if (d == 0) {
//printk("{#} 0 cascade element started => end\n");
printk("*");
@@ -29,6 +33,8 @@ void thread_cascade(void* d) {
printk(".");
}
+ free(v);
+
threads--;
}
@@ -50,9 +56,9 @@ int main(int argc, char** args) {
printk("\n");
}
- printk("(test) Creating thread cascade (total 2**5 = 32 threads)\n");
+ printk("(test) Creating thread cascade (total 2**6 = 64 threads)\n");
thread_new(useless_thread, 0);
- thread_new(thread_cascade, (void*)5);
+ thread_new(thread_cascade, (void*)6);
while (1) {
schedule();
diff --git a/src/user/app/yosh/main.c b/src/user/app/yosh/main.c
index 7c9b875..f7ee7e7 100644
--- a/src/user/app/yosh/main.c
+++ b/src/user/app/yosh/main.c
@@ -265,7 +265,7 @@ int main(int argc, char **sh_args) {
}
first_arg++;
- int pid = run(c, (const char**)first_arg, vt_fd);
+ int pid = run(c, first_arg, vt_fd);
if (pid <= 0) {
if (pid == E_NOT_FOUND) {
printf("Error: no such file %s\n", c);
diff --git a/src/user/lib/libc/include/readline.h b/src/user/lib/libc/include/readline.h
index 9fa2ee7..b5e0a1a 100644
--- a/src/user/lib/libc/include/readline.h
+++ b/src/user/lib/libc/include/readline.h
@@ -13,9 +13,11 @@ typedef struct _rdln_hist {
#ifdef __cplusplus
extern "C" { namespace libc {
#endif
-char *readln();
-char* freadln(FILE *f); // minimal line-reading function. user must free the returned value.
+
+
char* freadline(FILE *f, readline_history *h);
+
+
#ifdef __cplusplus
} }
#endif
diff --git a/src/user/lib/libc/include/tce/syscall.h b/src/user/lib/libc/include/tce/syscall.h
index 4a1c6ef..478ed3e 100644
--- a/src/user/lib/libc/include/tce/syscall.h
+++ b/src/user/lib/libc/include/tce/syscall.h
@@ -23,7 +23,7 @@ int proc_priv();
void* sbrk(ptrdiff_t size);
void brk(void* ptr);
-int run(const char* file, const char** args, int zero_fd);
+int run(const char* file, char** args, int zero_fd);
int waitpid(int pid, int block);
int open(const char* filename, int mode);
diff --git a/src/user/lib/libc/std/ctype.c b/src/user/lib/libc/std/ctype.c
index f0e7750..13f2ab0 100644
--- a/src/user/lib/libc/std/ctype.c
+++ b/src/user/lib/libc/std/ctype.c
@@ -15,14 +15,14 @@ int isalnum(int c) {
int tolower(int c) {
if (c >= 'A' && c <= 'Z') {
- return c + ('a' - 'A');
+ c += ('a' - 'A');
}
return c;
}
int toupper(int c) {
if (c >= 'a' && c <= 'z') {
- return c - ('a' - 'A');
+ c -= ('a' - 'A');
}
return c;
}
diff --git a/src/user/lib/libc/std/readline.c b/src/user/lib/libc/std/readline.c
index 4177bf6..5dfd442 100644
--- a/src/user/lib/libc/std/readline.c
+++ b/src/user/lib/libc/std/readline.c
@@ -1,44 +1,6 @@
#include <readline.h>
#include <stdlib.h>
-char* freadln(FILE *f) {
- int i;
-
- char *p = (char*)malloc(256);
- char *b = p;
-
- while (1) {
- int l = fread(b, 255, 1, f);
- if (l < 0) {
- free(b);
- return 0;
- }
-
- for (i = 0; i < l; i++) {
- if (b[i] == '\n') {
- b[i+1] = 0;
- return p;
- } else if (b[i] == 27) { // ignore escape sequences
- b[i] = 0;
- l = i;
- }
- }
-
- int d = b - p + l;
-
- char* newp = (char*)malloc(d + 256);
- memcpy(newp, p, d);
- free(p);
- p = newp;
- b = p + d;
- }
-}
-
-char* readln() {
- return freadln(&term);
-}
-
-
// ** READLINE
char *freadline(FILE *f, readline_history *h) {
diff --git a/src/user/lib/libc/std/stdio.c b/src/user/lib/libc/std/stdio.c
index be65505..043cc59 100644
--- a/src/user/lib/libc/std/stdio.c
+++ b/src/user/lib/libc/std/stdio.c
@@ -42,6 +42,8 @@ void __tce_libc_fsetup(FILE *f) {
f->tib_u_begin = f->tib_begin;
f->tib_u_end = f->tib_u_begin;
f->term_echo = 1;
+ } else {
+ f->tib_begin = f->tib_end = f->tib_u_begin = f->tib_u_end = 0;
}
}
diff --git a/src/user/lib/libc/std/stdlib.c b/src/user/lib/libc/std/stdlib.c
index 15f9ddc..10bce0a 100644
--- a/src/user/lib/libc/std/stdlib.c
+++ b/src/user/lib/libc/std/stdlib.c
@@ -33,6 +33,7 @@ int atoi(char *s) {
while (*s >= '0' && *s <= '9') {
r *= 10;
r += (*s) - '0';
+ s++;
};
return r;
}
diff --git a/src/user/lib/libc/tce/syscall.c b/src/user/lib/libc/tce/syscall.c
index f4f1333..780b573 100644
--- a/src/user/lib/libc/tce/syscall.c
+++ b/src/user/lib/libc/tce/syscall.c
@@ -75,7 +75,7 @@ void brk(void* ptr) {
// ********** proc
-int run(const char* filename, const char** args, int zero_fd) {
+int run(const char* filename, char** args, int zero_fd) {
return call(SC_RUN, (unsigned)filename, (unsigned)args, (unsigned)zero_fd, 0, 0);
}