diff options
author | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-19 11:45:49 +0200 |
---|---|---|
committer | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-19 11:45:49 +0200 |
commit | 499ca6c243b05da176a2d4bd9a2317f0b28afc7f (patch) | |
tree | f55ff788632b017ab8de83b71ad02b0998e1dda5 | |
parent | 7b466345af0d3a7dc5622617ce443a90c64e34a4 (diff) | |
download | TCE-499ca6c243b05da176a2d4bd9a2317f0b28afc7f.tar.gz TCE-499ca6c243b05da176a2d4bd9a2317f0b28afc7f.zip |
Introducing FWIK, the userland C++ framework. Far from complete.
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | doc/fwik.txt | 9 | ||||
-rw-r--r-- | doc/vfs.txt | 6 | ||||
-rw-r--r-- | src/common.make | 8 | ||||
-rw-r--r-- | src/include/tce/vfs.h | 3 | ||||
-rw-r--r-- | src/include/types.h | 1 | ||||
-rw-r--r-- | src/kernel/Makefile | 2 | ||||
-rw-r--r-- | src/kernel/lib/sched.cpp (renamed from src/kernel/lib/sched.c) | 1 | ||||
-rw-r--r-- | src/kernel/task/task.cpp | 4 | ||||
-rw-r--r-- | src/kernel/task/task.h | 4 | ||||
-rw-r--r-- | src/user/app/common.make | 6 | ||||
-rw-r--r-- | src/user/app/fwik.make | 6 | ||||
-rw-r--r-- | src/user/app/init/Makefile | 6 | ||||
-rw-r--r-- | src/user/app/init/main.cpp | 22 | ||||
-rw-r--r-- | src/user/app/prime/Makefile | 6 | ||||
-rw-r--r-- | src/user/app/prime/main.cpp | 17 | ||||
-rw-r--r-- | src/user/app/test/Makefile (renamed from src/user/test/Makefile) | 2 | ||||
-rw-r--r-- | src/user/app/test/main.c (renamed from src/user/test/main.c) | 2 | ||||
-rw-r--r-- | src/user/app/yosh/Makefile | 6 | ||||
-rw-r--r-- | src/user/app/yosh/main.cpp (renamed from src/user/yosh/main.c) | 87 | ||||
-rw-r--r-- | src/user/app_common.make | 6 | ||||
-rw-r--r-- | src/user/init/Makefile | 6 | ||||
-rw-r--r-- | src/user/init/main.c | 24 | ||||
-rw-r--r-- | src/user/lib/fwik/Makefile | 11 | ||||
-rw-r--r-- | src/user/lib/fwik/String.cpp | 1 | ||||
-rw-r--r-- | src/user/lib/fwik/include/IO/IOStream.h | 33 | ||||
-rw-r--r-- | src/user/lib/fwik/include/IO/Node.h | 25 | ||||
-rw-r--r-- | src/user/lib/fwik/include/IO/Term.h | 32 | ||||
-rw-r--r-- | src/user/lib/fwik/include/String.h | 5 | ||||
-rw-r--r-- | src/user/lib/fwik/include/cpp.h | 16 | ||||
-rw-r--r-- | src/user/lib/fwik/io/IOStream.cpp | 19 | ||||
-rw-r--r-- | src/user/lib/fwik/io/Node.cpp | 23 | ||||
-rw-r--r-- | src/user/lib/fwik/io/Term.cpp | 51 | ||||
-rw-r--r-- | src/user/lib/fwik/main.cpp | 30 | ||||
-rw-r--r-- | src/user/lib/libc/Makefile (renamed from src/user/lib/Makefile) | 4 | ||||
-rw-r--r-- | src/user/lib/libc/include/_dlmalloc.h (renamed from src/user/lib/include/_dlmalloc.h) | 0 | ||||
-rw-r--r-- | src/user/lib/libc/include/readline.h (renamed from src/user/lib/include/readline.h) | 11 | ||||
-rw-r--r-- | src/user/lib/libc/include/sched.h (renamed from src/user/lib/include/sched.h) | 4 | ||||
-rw-r--r-- | src/user/lib/libc/include/stdarg.h (renamed from src/user/lib/include/stdarg.h) | 0 | ||||
-rw-r--r-- | src/user/lib/libc/include/stdio.h (renamed from src/user/lib/include/stdio.h) | 8 | ||||
-rw-r--r-- | src/user/lib/libc/include/stdlib.h (renamed from src/user/lib/include/stdlib.h) | 7 | ||||
-rw-r--r-- | src/user/lib/libc/include/string.h (renamed from src/user/lib/include/string.h) | 4 | ||||
-rw-r--r-- | src/user/lib/libc/include/tce/syscall.h (renamed from src/user/lib/include/tce/syscall.h) | 10 | ||||
-rw-r--r-- | src/user/lib/libc/start.c (renamed from src/user/lib/start.c) | 0 | ||||
-rw-r--r-- | src/user/lib/libc/std/_dlmalloc.c (renamed from src/user/lib/std/_dlmalloc.c) | 0 | ||||
-rw-r--r-- | src/user/lib/libc/std/readline.c (renamed from src/user/lib/std/readline.c) | 2 | ||||
-rw-r--r-- | src/user/lib/libc/std/sched.c (renamed from src/user/lib/std/sched.c) | 1 | ||||
-rw-r--r-- | src/user/lib/libc/std/stdio.c (renamed from src/user/lib/std/stdio.c) | 1 | ||||
-rw-r--r-- | src/user/lib/libc/std/stdlib.c (renamed from src/user/lib/std/stdlib.c) | 0 | ||||
-rw-r--r-- | src/user/lib/libc/std/string.c (renamed from src/user/lib/std/string.c) | 1 | ||||
-rw-r--r-- | src/user/lib/libc/tce/syscall.c (renamed from src/user/lib/tce/syscall.c) | 4 | ||||
-rw-r--r-- | src/user/yosh/Makefile | 6 |
52 files changed, 440 insertions, 114 deletions
@@ -1,6 +1,6 @@ .PHONY: clean, mrproper, Init.rfs, floppy, commit -Projects = tools/makeinitrd kernel user/lib user/init user/test user/yosh +Projects = tools/makeinitrd kernel user/lib/libc user/lib/fwik user/app/init user/app/test user/app/yosh user/app/prime QemuCmd = qemu-system-i386 BasePath = $(shell pwd) @@ -36,19 +36,20 @@ commit: mrproper git commit -a; exit 0 git push origin -$(Cdrom): menu_cdrom.lst src/kernel/kernel.elf src/user/test/test.elf src/user/init/init.elf src/user/yosh/yosh.elf +$(Cdrom): menu_cdrom.lst src/kernel/kernel.elf src/user/app/test/test.elf src/user/app/init/init.elf src/user/app/yosh/yosh.elf src/user/app/prime/prime.elf mkdir -p cdrom/boot/grub if [ ! -e cdrom/boot/grub/stage2_eltorito ]; then \ echo "Please copy grub's stage2_eltorito to cdrom/boot/grub."; \ exit -1; \ fi src/tools/makeinitrd/makeinitrd cdrom/initrd \ - src/user/test/test.elf:/bin/test \ - src/user/yosh/yosh.elf:/bin/yosh \ + src/user/app/test/test.elf:/bin/test \ + src/user/app/yosh/yosh.elf:/bin/yosh \ + src/user/app/prime/prime.elf:/bin/prime \ README:/readme cp menu_cdrom.lst cdrom/boot/grub/menu.lst cp src/kernel/kernel.elf cdrom - cp src/user/init/init.elf cdrom + cp src/user/app/init/init.elf cdrom genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 \ -boot-info-table -input-charset ascii -A TCE -o $(Cdrom) cdrom diff --git a/doc/fwik.txt b/doc/fwik.txt new file mode 100644 index 0000000..d038b2a --- /dev/null +++ b/doc/fwik.txt @@ -0,0 +1,9 @@ +FWIK + +FWIK (is not an acronym) is a C++ library for userland applications, built +especially to run on the T/CE kernel. + +It includes : +- I/O (files, directories, terminals) +- Strings, vectors, sets, maps +- ... diff --git a/doc/vfs.txt b/doc/vfs.txt index 5a6a0b2..680df73 100644 --- a/doc/vfs.txt +++ b/doc/vfs.txt @@ -32,13 +32,17 @@ struct file_info { File open modes flags : - FM_READ open for reading - FM_WRITE open for writing -- FM_APPEND append data - FM_TRUNC truncate existing file - FM_CREATE create file if it doesn't exist - FM_DELETE delete the file - incompatible with everything else note : if mode contains neither FM_READ nor FM_WRITE, then the file will be created/truncated according to the flags given, but will not be actually oppenned, so the return value of open will be 0. +Important remark : +The kernel does NOT keep track of what your current position in the file is. And it's a bit hard, too, +because terminals behave differently than files, and so do directories, etc. So that's why there is no +FM_APPEND : when you open a file, you can do whatever you want. + Link modes : - LM_SYMLINK - LM_HARDLINK (don't count on that really being implemented) diff --git a/src/common.make b/src/common.make index be6a302..96a1a7f 100644 --- a/src/common.make +++ b/src/common.make @@ -1,10 +1,10 @@ # ============== ENVIRONMENT VARIABLES CC = i586-elf-gcc -CFLAGS = -nostdlib -nostartfiles -nodefaultlibs -fno-builtin -fno-stack-protector -Wall -Wextra -g +CFLAGS = -nostdlib -nostartfiles -nodefaultlibs -fno-builtin -fno-stack-protector -Wall -Werror -g -Wno-error=main CCFLAGS = CXX = i586-elf-g++ -CXXFLAGS = -fno-rtti -fno-exceptions -Werror -Wno-write-strings -Wno-error=unused-parameter -g +CXXFLAGS = -fno-rtti -fno-exceptions -Wall -Werror -Wno-write-strings -Wno-main -Wno-error=unused-parameter -Wno-error=main -g LD = i586-elf-ld .PHONY: clean, mrproper @@ -18,9 +18,9 @@ AFLAGS = -f elf all: $(Out) -$(Out): $(Obj) +$(Out): $(Obj) $(ExtObj) echo ""; echo "- Linking $@..." - $(LD) $(LDFLAGS) $^ -o $@ $(ExtObj) + $(LD) $(LDFLAGS) $^ -o $@ # ============== GENERAL CLEAINING PROCEDURES diff --git a/src/include/tce/vfs.h b/src/include/tce/vfs.h index 00c80d7..329bd8b 100644 --- a/src/include/tce/vfs.h +++ b/src/include/tce/vfs.h @@ -16,10 +16,9 @@ typedef struct _file_info { // file open flags #define FM_READ 0x00000001 #define FM_WRITE 0x00000002 -#define FM_APPEND 0x00000004 +#define FM_DELETE 0x00000004 #define FM_TRUNC 0x00000008 #define FM_CREATE 0x00000010 -#define FM_DELETE 0x00000020 // link modes #define LM_SYMLINK 1 diff --git a/src/include/types.h b/src/include/types.h index e7c1a35..373a3b5 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -11,6 +11,7 @@ typedef short int16_t; typedef char int8_t; typedef long unsigned int size_t; +typedef long int ptrdiff_t; #define NULL 0 diff --git a/src/kernel/Makefile b/src/kernel/Makefile index a6ad58a..ba65580 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -10,7 +10,7 @@ Obj = core/loader_.o core/kmain.o core/sys.o \ include $(SrcPath)/common.make -CFLAGS += -I $(SrcPath)/include -I . -I ./lib +CFLAGS += -I $(SrcPath)/include -I . -I ./lib -Wno-error=unused-variable LDFLAGS += -T link.ld -Map kernel.map diff --git a/src/kernel/lib/sched.c b/src/kernel/lib/sched.cpp index 29ea8a8..06900d1 100644 --- a/src/kernel/lib/sched.c +++ b/src/kernel/lib/sched.cpp @@ -1,4 +1,5 @@ #include <sched.h> +#include <task/task.h> /* Internal use only. This function is atomic, meaning it cannot be interrupted by a system task switch. */ static uint32_t atomic_exchange(uint32_t* ptr, uint32_t newval) { diff --git a/src/kernel/task/task.cpp b/src/kernel/task/task.cpp index 7eb2b36..03425b5 100644 --- a/src/kernel/task/task.cpp +++ b/src/kernel/task/task.cpp @@ -349,7 +349,7 @@ static void process_finish(process *pr, int retval) { if (proc_count == 0) PANIC("No more processes are running! This is bad! Or is it?"); } -size_t process::sbrk(size_t size) { +size_t process::sbrk(ptrdiff_t size) { if (data == 0) return -1; ASSERT(data < K_HIGHHALF_ADDR); if (data + size >= K_HIGHHALF_ADDR) return -1; @@ -420,7 +420,7 @@ void* process::set_args(char** args) { // ===================== SYSTEM CALLS -size_t process_sbrk(size_t size) { +size_t process_sbrk(ptrdiff_t size) { return current_process->sbrk(size); } diff --git a/src/kernel/task/task.h b/src/kernel/task/task.h index 8a0e94a..e6e0c6e 100644 --- a/src/kernel/task/task.h +++ b/src/kernel/task/task.h @@ -44,7 +44,7 @@ class process { process(process *parent, uint32_t uid, uint32_t privilege); void* set_args(char** args); - size_t sbrk(size_t size); + size_t sbrk(ptrdiff_t size); }; class thread { @@ -81,7 +81,7 @@ int proc_priv(); //Returns current privilege level // syscalls void thread_exit(); void process_exit(size_t retval); -size_t process_sbrk(size_t size); +size_t process_sbrk(ptrdiff_t size); void process_brk(size_t ptr); int process_run(char* file, char** args, FILE zero_fd); int process_waitpid(int pid, int block); diff --git a/src/user/app/common.make b/src/user/app/common.make new file mode 100644 index 0000000..6e1c78f --- /dev/null +++ b/src/user/app/common.make @@ -0,0 +1,6 @@ +ExtObj = $(SrcPath)/user/lib/libc/_libc.o + +include $(SrcPath)/common.make + +CFLAGS += -I $(SrcPath)/include -I $(SrcPath)/user/lib/libc/include +LDFLAGS += -T $(SrcPath)/user/link.ld diff --git a/src/user/app/fwik.make b/src/user/app/fwik.make new file mode 100644 index 0000000..292440d --- /dev/null +++ b/src/user/app/fwik.make @@ -0,0 +1,6 @@ +ExtObj = $(SrcPath)/user/lib/fwik/_fwik.o + +include $(SrcPath)/common.make + +CFLAGS += -I $(SrcPath)/include -I $(SrcPath)/user/lib/libc/include -I $(SrcPath)/user/lib/fwik/include +LDFLAGS += -T $(SrcPath)/user/link.ld diff --git a/src/user/app/init/Makefile b/src/user/app/init/Makefile new file mode 100644 index 0000000..6ab7cf8 --- /dev/null +++ b/src/user/app/init/Makefile @@ -0,0 +1,6 @@ +Obj = main.o +Out = init.elf + +include $(SrcPath)/user/app/fwik.make + +LDFLAGS += -Map init.map diff --git a/src/user/app/init/main.cpp b/src/user/app/init/main.cpp new file mode 100644 index 0000000..4bb12da --- /dev/null +++ b/src/user/app/init/main.cpp @@ -0,0 +1,22 @@ +#include <tce/syscall.h> +#include <IO/IOStream.h> + +int Main(char** args) { + FILE home_term = libc::open("/.ui/home", 0); + if (home_term < 0) return -1; + + stdio << "(init) Trivial/Computing Environment says hello. Press super to go home.\n"; + + for (int i = 0; args[i] != 0; i++) { + if (i == 0) continue; + stdio.printf("(init) Spawning %s...\n", args[i]); + int pid = libc::run(args[i], 0, home_term); + if (pid < 0) { + stdio << "(init) Error. Sorry.\n"; + } else { + libc::waitpid(pid, 1); + } + } + stdio << "(init) Goodbye.\n"; + return 0; +} diff --git a/src/user/app/prime/Makefile b/src/user/app/prime/Makefile new file mode 100644 index 0000000..0e86c31 --- /dev/null +++ b/src/user/app/prime/Makefile @@ -0,0 +1,6 @@ +Obj = main.o +Out = prime.elf + +include $(SrcPath)/user/app/fwik.make + +LDFLAGS += -Map prime.map diff --git a/src/user/app/prime/main.cpp b/src/user/app/prime/main.cpp new file mode 100644 index 0000000..b2b08a8 --- /dev/null +++ b/src/user/app/prime/main.cpp @@ -0,0 +1,17 @@ +#include <IO/IOStream.h> + +bool is_prime(int i) { + for (int j = 2; j*j <= i; j++) { + if (i % j == 0) return false; + } + return true; +} + +int Main(char** args) { + for (int i = 2; i < 1000000; i++) { + if (is_prime(i)) stdio.printf("%d\t", i); + } + stdio.printf("\nThat's all prime numbers for today.\n"); + + return 0; +} diff --git a/src/user/test/Makefile b/src/user/app/test/Makefile index 5c8464a..a64af01 100644 --- a/src/user/test/Makefile +++ b/src/user/app/test/Makefile @@ -1,6 +1,6 @@ Obj = main.o Out = test.elf -include $(SrcPath)/user/app_common.make +include $(SrcPath)/user/app/common.make LDFLAGS += -Map test.map diff --git a/src/user/test/main.c b/src/user/app/test/main.c index 7ca4802..5940cae 100644 --- a/src/user/test/main.c +++ b/src/user/app/test/main.c @@ -1,6 +1,4 @@ #include <tce/syscall.h> -#include <stdlib.h> -#include <stdio.h> int threads = 0; diff --git a/src/user/app/yosh/Makefile b/src/user/app/yosh/Makefile new file mode 100644 index 0000000..bf55ce1 --- /dev/null +++ b/src/user/app/yosh/Makefile @@ -0,0 +1,6 @@ +Obj = main.o +Out = yosh.elf + +include $(SrcPath)/user/app/fwik.make + +LDFLAGS += -Map yosh.map diff --git a/src/user/yosh/main.c b/src/user/app/yosh/main.cpp index d334d1e..fe3eeed 100644 --- a/src/user/yosh/main.c +++ b/src/user/app/yosh/main.cpp @@ -3,34 +3,37 @@ #include <stdlib.h> #include <stdio.h> #include <readline.h> +#include <IO/IOStream.h> + +using namespace libc; FILE cwd_f; char *cwd; void about() { - print("Trivial/Computing Environment v0.1.0 - yosh 3.\n"); + stdio << "Trivial/Computing Environment v0.1.0 - yosh 3.\n"; } void help(char *args[]) { if (args[1] == 0) { - print("Available commands: about, help, exit, ls, cd, goto.\n"); + stdio << "Available commands: about, help, exit, ls, cd, goto.\n"; } else if (strcmp(args[1], "about") == 0) { - print("Usage:\tabout\nShows some info about yosh. Very usefull.\n"); + stdio << "Usage:\tabout\nShows some info about yosh. Very usefull.\n"; } else if (strcmp(args[1], "help") == 0) { - print("Usage:\thelp\n\thelp <command>\n"); - print("Shows some info about the command you want, or commands in general.\n"); + stdio << "Usage:\thelp\n\thelp <command>\n" + << "Shows some info about the command you want, or commands in general.\n"; } else if (strcmp(args[1], "exit") == 0) { - print("Usage:\texit\n"); - print("Exits the shell.\nWill probably make your system panic if you only have a shell running.\n"); + stdio << "Usage:\texit\n" + << "Exits the shell.\nWill probably make your system panic if you only have a shell running.\n"; } else if (strcmp(args[1], "cd") == 0) { - print("Usage:\tcd <location>\nGoes to the location specified.\n"); + stdio << "Usage:\tcd <location>\nGoes to the location specified.\n"; } else if (strcmp(args[1], "ls") == 0) { - print("Usage:\tls\n\tls <location>...\nShows the content of the current/specified directory.\n"); + stdio << "Usage:\tls\n\tls <location>...\nShows the content of the current/specified directory.\n"; } else if (strcmp(args[1], "goto") == 0) { - print("Usage:\tgoto <vt name>\nSwitchs focus to specified virtual terminal.\n"); - print("Type `ls /.ui` to see available terminals.\n"); + stdio << "Usage:\tgoto <vt name>\nSwitchs focus to specified virtual terminal.\n" + << "Type `ls /.ui` to see available terminals.\n"; } else { - printf("No such command: %s\n", args[1]); + stdio.printf("No such command: %s\n", args[1]); } } @@ -133,7 +136,7 @@ void cd(char **args) { print("No such file or directory.\n"); free(newcwd); } else if (i < 0) { - printf("Error stating: %i\n", i); + stdio.printf("Error stating: %i\n", i); free(newcwd); } else if ((info.type & FT_DIR) == 0) { print("Not a directory.\n"); @@ -141,7 +144,7 @@ void cd(char **args) { } else { FILE nf = open(newcwd, 0); if (nf < 0) { - printf("Error opening: %i\n", nf); + stdio.printf("Error opening: %i\n", nf); free(newcwd); } else { free(cwd); @@ -161,7 +164,7 @@ void ls_dir(FILE f) { if (strcmp(buf, ".") == 0 || strcmp(buf, "..") == 0) continue; - printf(" %s", buf); + stdio.printf(" %s", buf); stat_relative(f, buf, &info); if (info.type & FT_DIR) print("/"); @@ -175,9 +178,9 @@ void ls_dir(FILE f) { print("\t"); if (info.type & FT_TERMINAL) { - printf("%ix%i", info.size >> 16, info.size & 0xFFFF); + stdio.printf("%ix%i", info.size >> 16, info.size & 0xFFFF); } else if ((info.type & FT_DEV) == 0) { - printf("%i", info.size); + stdio.printf("%i", info.size); } print("\n"); @@ -190,20 +193,20 @@ void ls(char **args) { } else { int i; for (i = 1; args[i] != 0; i++) { - printf("Contents of %s :\n", args[i]); + stdio.printf("Contents of %s :\n", args[i]); char* d = path_cat(cwd, args[i], 1); file_info info; int e = stat(d, &info); if (e == E_NOT_FOUND) { print(" No such file or directory\n"); } else if (e < 0) { - printf(" Error stating: %i\n", e); + stdio.printf(" Error stating: %i\n", e); } else if ((info.type & FT_DIR) == 0) { - printf(" Not a directory.\n"); + stdio.printf(" Not a directory.\n"); } else { FILE ff = open(d, 0); if (ff < 0) { - printf(" Error opening: %i\n", ff); + stdio.printf(" Error opening: %i\n", ff); } else { ls_dir(ff); close(ff); @@ -221,11 +224,11 @@ void cat(char **args) { file_info info; int e = stat(d, &info); if (e == E_NOT_FOUND) { - printf("No such file: %s\n", d); + stdio.printf("No such file: %s\n", d); } else if (e < 0) { - printf("Error stating %s : %i\n", d, e); + stdio.printf("Error stating %s : %i\n", d, e); } else if ((info.type & FT_FILE) == 0) { - printf("Not a file: %s\n", d); + stdio.printf("Not a file: %s\n", d); } else { FILE ff = open(d, 0); char* buff = (char*)malloc(info.size); @@ -247,11 +250,11 @@ void t_goto(char** args) { if ((i = link(p, "/.dev/vgatxt", LM_OUTPUT_TO)) == 0) { link("/.dev/ps2kbd", p, LM_OUTPUT_TO); } else { - printf("Error %i\n", i); + stdio.printf("Error %i\n", i); } } -int main(char **sh_args) { +int Main(char **sh_args) { about(); cwd = strdup("/"); @@ -261,13 +264,17 @@ int main(char **sh_args) { int bg_pr[128], bg_pr_c = 0; - readline_history h; h.str = 0; h.max = 4; + Term *term = stdio.term; + if (term == 0) { + stdio << "Error: no terminal...\n"; + return -1; + } while (1) { - printf("\x1b[33m %s \x1b[1m", cwd); + stdio.printf("\x1b[33m %s \x1b[1m", cwd); - char *s = readline(term, &h); - printf("\x1b[0m"); + char *s = term->readline(); + stdio.printf("\x1b[0m"); if (s == 0) return -1; if (*s == 0) continue; @@ -301,17 +308,17 @@ int main(char **sh_args) { } else if (strcmp(c_args[0], "cat") == 0) { cat(c_args); } else { - FILE vt = term; + FILE vt = term->fd; char *t = 0; if (strcmp(c_args[0], "on") == 0) { if (c_args[1] == 0 || c_args[2] == 0) { - printf("Usage:\ton <vt> <command>\n"); + stdio.printf("Usage:\ton <vt> <command>\n"); continue; } t = path_cat("/.ui/", c_args[1], 0); vt = open(t, 0); - if (vt < 0 || vt == term) { - printf("Error: cannot open terminal %s (%i)\n", t, vt); + if (vt < 0 || vt == term->fd) { + stdio.printf("Error: cannot open terminal %s (%i)\n", t, vt); free(t); continue; } @@ -332,16 +339,16 @@ int main(char **sh_args) { int pid = run(c, c_args + 1, vt); if (pid <= 0) { if (pid == E_NOT_FOUND) { - printf("Error: no such file %s\n", c); + stdio.printf("Error: no such file %s\n", c); } else { - printf("Error %i\n", pid); + stdio.printf("Error %i\n", pid); } } else { - if (vt == term) { + if (vt == term->fd) { int ret = waitpid(pid, 1); - printf(" -> child (pid %i) exited with status %i\n", pid, ret); + stdio.printf("(yosh) child (pid %i) exited with status %i\n", pid, ret); } else { - printf(" -> running on terminal %s, pid:%i\n", t, pid); + stdio.printf("(yosh) running on terminal %s, pid:%i\n", t, pid); bg_pr[bg_pr_c++] = pid; free(t); } @@ -355,7 +362,7 @@ int main(char **sh_args) { while (i < bg_pr_c) { int ret = waitpid(bg_pr[i], 0); if (ret != E_NOT_FINISHED) { - printf(" -> child (pid %i) exited with status %i\n", bg_pr[i], ret); + stdio.printf("(yosh) child (pid %i) exited with status %i\n", bg_pr[i], ret); bg_pr_c--; bg_pr[i] = bg_pr[bg_pr_c]; } else { diff --git a/src/user/app_common.make b/src/user/app_common.make deleted file mode 100644 index f2f0c46..0000000 --- a/src/user/app_common.make +++ /dev/null @@ -1,6 +0,0 @@ -ExtObj = $(SrcPath)/user/lib/_user.o - -include $(SrcPath)/common.make - -CFLAGS += -I $(SrcPath)/include -I $(SrcPath)/user/lib/include -LDFLAGS += -T $(SrcPath)/user/link.ld diff --git a/src/user/init/Makefile b/src/user/init/Makefile deleted file mode 100644 index 286de06..0000000 --- a/src/user/init/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -Obj = main.o -Out = init.elf - -include $(SrcPath)/user/app_common.make - -LDFLAGS += -Map test.map diff --git a/src/user/init/main.c b/src/user/init/main.c deleted file mode 100644 index 7300efe..0000000 --- a/src/user/init/main.c +++ /dev/null @@ -1,24 +0,0 @@ -#include <tce/syscall.h> -#include <stdlib.h> -#include <stdio.h> - -int main(char** args) { - int i; - - FILE home_term = open("/.ui/home", 0); - if (home_term < 0) return -1; - - print("(init) Trivial/Computing Environment says hello. Press super to go home.\n"); - - for (i = 0; args[i] != 0; i++) { - if (i == 0) continue; - printf("(init) Spawning %s...\n", args[i]); - int pid = run(args[i], 0, home_term); - if (pid < 0) { - print("(init) Error. Sorry.\n"); - } else { - waitpid(pid, 1); - } - } - print("(init) Goodbye.\n"); -} diff --git a/src/user/lib/fwik/Makefile b/src/user/lib/fwik/Makefile new file mode 100644 index 0000000..cf8896a --- /dev/null +++ b/src/user/lib/fwik/Makefile @@ -0,0 +1,11 @@ +Out = _fwik.o +Obj = String.o io/Node.o io/Term.o io/IOStream.o main.o + +ExtObj = $(SrcPath)/user/lib/libc/_libc.o + +include $(SrcPath)/common.make + +CFLAGS += -I$(SrcPath)/include -I$(SrcPath)/user/lib/libc/include -I$(SrcPath)/user/lib/fwik/include + +LDFLAGS += -r + diff --git a/src/user/lib/fwik/String.cpp b/src/user/lib/fwik/String.cpp new file mode 100644 index 0000000..75114c2 --- /dev/null +++ b/src/user/lib/fwik/String.cpp @@ -0,0 +1 @@ +#include <String.h> diff --git a/src/user/lib/fwik/include/IO/IOStream.h b/src/user/lib/fwik/include/IO/IOStream.h new file mode 100644 index 0000000..e9ae246 --- /dev/null +++ b/src/user/lib/fwik/include/IO/IOStream.h @@ -0,0 +1,33 @@ +#ifndef DEF_FWIK_IO_IOSTREAM_H +#define DEF_FWIK_IO_IOSTREAM_H + +#include "Term.h" + +class IOStream { + public: + Term *term; + + IOStream() : term(0) {} + IOStream(Term *t) : term(t) {} + + void print(char* str); + void printf(char* fmt, ...); + char* readln(); + + IOStream &operator<<(char* s) { + print(s); + return *this; + } + IOStream &operator<<(int i) { + printf("%d", i); + return *this; + } + IOStream &operator<<(void* p) { + printf("%p", p); + return *this; + } +}; + +extern IOStream stdio; + +#endif diff --git a/src/user/lib/fwik/include/IO/Node.h b/src/user/lib/fwik/include/IO/Node.h new file mode 100644 index 0000000..6100fc9 --- /dev/null +++ b/src/user/lib/fwik/include/IO/Node.h @@ -0,0 +1,25 @@ +#ifndef DEF_FWIK_IO_NODE_H +#define DEF_FWIK_IO_NODE_H + +#include <tce/syscall.h> +#include <tce/vfs.h> +#include <stdio.h> +#include <cpp.h> + +class Term; +class Node { + public: + FILE fd; + file_info info; + bool valid; + + Node(FILE f); + Node(char* filename, int mode); + virtual ~Node() {} + + void close(); + + virtual Term* as_term() { return 0; } +}; + +#endif diff --git a/src/user/lib/fwik/include/IO/Term.h b/src/user/lib/fwik/include/IO/Term.h new file mode 100644 index 0000000..cf90789 --- /dev/null +++ b/src/user/lib/fwik/include/IO/Term.h @@ -0,0 +1,32 @@ +#ifndef DEF_FWIK_IO_TERM_H +#define DEF_FWIK_IO_TERM_H + +#include <stdio.h> +#include "Node.h" + +#include <readline.h> + +class Term : public Node { + int w, h; + + readline_history hist; + + void _init(); + + public: + Term(FILE f); + Term(char* filename, int mode); + Term(const Node &n); + virtual ~Term(); + + virtual void print(char *s); + virtual void printf(char* fmt, ...); + virtual void vprintf(char* fmt, va_list ap); + virtual char* readln(); + char* readline(); + + virtual Term* as_term() { return this; } +}; + +#endif + diff --git a/src/user/lib/fwik/include/String.h b/src/user/lib/fwik/include/String.h new file mode 100644 index 0000000..2a44046 --- /dev/null +++ b/src/user/lib/fwik/include/String.h @@ -0,0 +1,5 @@ +#ifndef DEF_FWIK_STRING_H +#define DEF_FWIK_STRING_H + + +#endif diff --git a/src/user/lib/fwik/include/cpp.h b/src/user/lib/fwik/include/cpp.h new file mode 100644 index 0000000..5b66ba1 --- /dev/null +++ b/src/user/lib/fwik/include/cpp.h @@ -0,0 +1,16 @@ +#ifndef DEF_FWIK_CPPSUPPORT_H +#define DEF_FWIK_CPPSUPPORT_H + +#include <stdlib.h> + +inline void* operator new(size_t, void* p) throw() { return p; } +inline void* operator new[](size_t, void* p) throw() { return p; } +inline void operator delete (void*, void*) throw() { }; +inline void operator delete[](void*, void*) throw() { }; + +inline void* operator new (size_t size) { return malloc(size); } +inline void* operator new[] (size_t size) { return malloc(size); } +inline void operator delete (void* ptr) { return free(ptr); } +inline void operator delete[] (void* ptr) { return free(ptr); } + +#endif diff --git a/src/user/lib/fwik/io/IOStream.cpp b/src/user/lib/fwik/io/IOStream.cpp new file mode 100644 index 0000000..bcaa581 --- /dev/null +++ b/src/user/lib/fwik/io/IOStream.cpp @@ -0,0 +1,19 @@ +#include <IO/IOStream.h> + +void IOStream::print(char* str) { + if (term == 0) return; + term->print(str); +} + +void IOStream::printf(char* fmt, ...) { + if (term == 0) return; + va_list ap; + va_start(ap, fmt); + term->vprintf(fmt, ap); + va_end(ap); +} + +char* IOStream::readln() { + if (term == 0) return 0; + return term->readln(); +} diff --git a/src/user/lib/fwik/io/Node.cpp b/src/user/lib/fwik/io/Node.cpp new file mode 100644 index 0000000..1d8b7f5 --- /dev/null +++ b/src/user/lib/fwik/io/Node.cpp @@ -0,0 +1,23 @@ +#include <IO/Node.h> + +Node::Node(FILE f) { + fd = f; + int i = libc::statf(f, &info); + valid = (i == 0); +} + +Node::Node(char* filename, int mode) { + fd = libc::open(filename, mode); + if (fd < 0) { + valid = false; + } else { + int i = libc::statf(fd, &info); + valid = (i == 0); + if (!valid) libc::close(fd); + } +} + +void Node::close() { + if (valid) libc::close(fd); + valid = false; +} diff --git a/src/user/lib/fwik/io/Term.cpp b/src/user/lib/fwik/io/Term.cpp new file mode 100644 index 0000000..1c6db66 --- /dev/null +++ b/src/user/lib/fwik/io/Term.cpp @@ -0,0 +1,51 @@ +#include <IO/Term.h> + +Term::Term(const Node &n) : Node(n) { + _init(); +} + +Term::Term(FILE f) : Node(f) { + _init(); +} + +Term::Term(char* filename, int mode) : Node(filename, mode) { + _init(); +} + +void Term::_init() { + if (info.type & FT_TERMINAL) { + w = info.size >> 16; + h = info.size & 0xFFFF; + } else { + valid = false; + } + hist.str = 0; + hist.max = 12; +} + +Term::~Term() { + //TODO : free readline history +} + +void Term::print(char *s) { + libc::fprint(fd, s); +} + +void Term::printf(char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + libc::vfprintf(fd, fmt, ap); + va_end(ap); +} + +void Term::vprintf(char* fmt, va_list ap) { + libc::vfprintf(fd, fmt, ap); +} + +char* Term::readln() { + return libc::freadln(fd); +} + +char *Term::readline() { + return libc::freadline(fd, &hist); +} diff --git a/src/user/lib/fwik/main.cpp b/src/user/lib/fwik/main.cpp new file mode 100644 index 0000000..8e2e289 --- /dev/null +++ b/src/user/lib/fwik/main.cpp @@ -0,0 +1,30 @@ +#include <IO/IOStream.h> +#include <stdio.h> +#include <cpp.h> + +IOStream stdio; + +int Main(char **args); // FWIK app main + +extern "C" int main(char **args) { + stdio.term = 0; + + Node zero(libc::term); + if (zero.info.type & FT_TERMINAL) { + stdio.term = new Term(zero); + } + Main(args); +} + + +// C++ support + +//Enables pure virtual functions +extern "C" void __cxa_pure_virtual() { + //do nothing +} +//Enables global objects +void *__dso_handle; +extern "C" int __cxa_atexit(void (*f)(void*), void *p, void *d) { return 0; } + + diff --git a/src/user/lib/Makefile b/src/user/lib/libc/Makefile index c2630c4..14a5421 100644 --- a/src/user/lib/Makefile +++ b/src/user/lib/libc/Makefile @@ -1,11 +1,11 @@ -Out = _user.o +Out = _libc.o Obj = tce/syscall.o std/_dlmalloc.o \ std/stdio.o std/stdlib.o std/string.o std/sched.o std/readline.o \ start.o include $(SrcPath)/common.make -CFLAGS += -I$(SrcPath)/include -I$(SrcPath)/user/lib/include +CFLAGS += -I$(SrcPath)/include -I$(SrcPath)/user/lib/libc/include -Wno-error=unused-variable LDFLAGS += -r diff --git a/src/user/lib/include/_dlmalloc.h b/src/user/lib/libc/include/_dlmalloc.h index d896e22..d896e22 100644 --- a/src/user/lib/include/_dlmalloc.h +++ b/src/user/lib/libc/include/_dlmalloc.h diff --git a/src/user/lib/include/readline.h b/src/user/lib/libc/include/readline.h index 53193d7..910ad9a 100644 --- a/src/user/lib/include/readline.h +++ b/src/user/lib/libc/include/readline.h @@ -3,14 +3,21 @@ #include <stdio.h> -char* freadln(FILE f); // minimal line-reading function. user must free the returned value. typedef struct _rdln_hist { int max; int n; char **str; } readline_history; -char* readline(FILE f, readline_history *h); + +#ifdef __cplusplus +extern "C" { namespace libc { +#endif +char* freadln(FILE f); // minimal line-reading function. user must free the returned value. +char* freadline(FILE f, readline_history *h); +#ifdef __cplusplus +} } +#endif #endif diff --git a/src/user/lib/include/sched.h b/src/user/lib/libc/include/sched.h index 1fe148d..451a114 100644 --- a/src/user/lib/include/sched.h +++ b/src/user/lib/libc/include/sched.h @@ -10,7 +10,7 @@ typedef uint32_t mutex_t; #ifdef __cplusplus -extern "C" { +extern "C" { namespace libc { #endif int mutex_lock(mutex_t* mutex); //wait for mutex to be free @@ -18,7 +18,7 @@ int mutex_lockE(mutex_t* mutex); //lock mutex only if free, returns !0 if locked void mutex_unlock(mutex_t* mutex); #ifdef __cplusplus -} +} } #endif #endif diff --git a/src/user/lib/include/stdarg.h b/src/user/lib/libc/include/stdarg.h index 5cd74ff..5cd74ff 100644 --- a/src/user/lib/include/stdarg.h +++ b/src/user/lib/libc/include/stdarg.h diff --git a/src/user/lib/include/stdio.h b/src/user/lib/libc/include/stdio.h index 099746e..d93ebf0 100644 --- a/src/user/lib/include/stdio.h +++ b/src/user/lib/libc/include/stdio.h @@ -4,6 +4,10 @@ #include <stdarg.h> #include <tce/syscall.h> +#ifdef __cplusplus +extern "C" { namespace libc { +#endif + extern FILE term; void print(char *s); @@ -17,5 +21,9 @@ void fprintf(FILE f, char *s, ...); void vfprintf(FILE f, char *s, va_list arg); +#ifdef __cplusplus +} } +#endif + #endif diff --git a/src/user/lib/include/stdlib.h b/src/user/lib/libc/include/stdlib.h index 594f5c5..1cc5d16 100644 --- a/src/user/lib/include/stdlib.h +++ b/src/user/lib/libc/include/stdlib.h @@ -4,8 +4,15 @@ #include <types.h> #include <tce/syscall.h> +#ifdef __cplusplus +extern "C" { namespace libc { +#endif + void abort(); extern volatile int errno; +#ifdef __cplusplus +} } +#endif #include <_dlmalloc.h> diff --git a/src/user/lib/include/string.h b/src/user/lib/libc/include/string.h index 87d8da2..fbe3ade 100644 --- a/src/user/lib/include/string.h +++ b/src/user/lib/libc/include/string.h @@ -4,7 +4,7 @@ #include <types.h> #ifdef __cplusplus -extern "C" { +extern "C" { namespace libc { #endif void *memcpy(void *dest, const void *src, int count); @@ -19,7 +19,7 @@ char *strcat(char *dest, const char *src); int strcmp(const char *s1, const char *s2); #ifdef __cplusplus -} +} } #endif diff --git a/src/user/lib/include/tce/syscall.h b/src/user/lib/libc/include/tce/syscall.h index c182f40..6ad37f1 100644 --- a/src/user/lib/include/tce/syscall.h +++ b/src/user/lib/libc/include/tce/syscall.h @@ -7,6 +7,10 @@ #define NEW_STACK_SIZE 0x8000 +#ifdef __cplusplus +extern "C" { namespace libc { +#endif + void thread_exit(); void schedule(); void thread_sleep(int time); @@ -16,7 +20,7 @@ void thread_new(void (*entry)(void*), void *data); void irq_wait(int number); int proc_priv(); -void* sbrk(size_t size); +void* sbrk(ptrdiff_t size); void brk(void* ptr); int run(char* file, char** args, FILE zero_fd); @@ -32,4 +36,8 @@ int read(FILE file, size_t offset, size_t len, char *buffer); int write(FILE file, size_t offset, size_t len, char *buffer); int link(char* from, char* to, int mode); +#ifdef __cplusplus +} } +#endif + #endif diff --git a/src/user/lib/start.c b/src/user/lib/libc/start.c index 2521f97..2521f97 100644 --- a/src/user/lib/start.c +++ b/src/user/lib/libc/start.c diff --git a/src/user/lib/std/_dlmalloc.c b/src/user/lib/libc/std/_dlmalloc.c index aae76f8..aae76f8 100644 --- a/src/user/lib/std/_dlmalloc.c +++ b/src/user/lib/libc/std/_dlmalloc.c diff --git a/src/user/lib/std/readline.c b/src/user/lib/libc/std/readline.c index a3beefa..c2237b6 100644 --- a/src/user/lib/std/readline.c +++ b/src/user/lib/libc/std/readline.c @@ -38,7 +38,7 @@ char* freadln(FILE f) { // ** READLINE -char *readline(FILE f, readline_history *h) { +char *freadline(FILE f, readline_history *h) { int i; fprint(f, "\x1b[h"); // disable keyboard echo diff --git a/src/user/lib/std/sched.c b/src/user/lib/libc/std/sched.c index 29ea8a8..84e9c3a 100644 --- a/src/user/lib/std/sched.c +++ b/src/user/lib/libc/std/sched.c @@ -1,4 +1,5 @@ #include <sched.h> +#include <tce/syscall.h> /* Internal use only. This function is atomic, meaning it cannot be interrupted by a system task switch. */ static uint32_t atomic_exchange(uint32_t* ptr, uint32_t newval) { diff --git a/src/user/lib/std/stdio.c b/src/user/lib/libc/std/stdio.c index b1778d6..3595622 100644 --- a/src/user/lib/std/stdio.c +++ b/src/user/lib/libc/std/stdio.c @@ -1,6 +1,7 @@ #include <stdlib.h> #include <stdio.h> #include <tce/syscall.h> +#include <readline.h> FILE term = 0; void print(char *s) { fprint(term, s); } diff --git a/src/user/lib/std/stdlib.c b/src/user/lib/libc/std/stdlib.c index 9d46b5c..9d46b5c 100644 --- a/src/user/lib/std/stdlib.c +++ b/src/user/lib/libc/std/stdlib.c diff --git a/src/user/lib/std/string.c b/src/user/lib/libc/std/string.c index 21dbd03..5a41b7e 100644 --- a/src/user/lib/std/string.c +++ b/src/user/lib/libc/std/string.c @@ -1,4 +1,5 @@ #include <string.h> +#include <stdlib.h> int strlen(const char *str) { int i = 0; diff --git a/src/user/lib/tce/syscall.c b/src/user/lib/libc/tce/syscall.c index 4304840..80e0e0e 100644 --- a/src/user/lib/tce/syscall.c +++ b/src/user/lib/libc/tce/syscall.c @@ -65,12 +65,12 @@ int proc_priv() { // ******** memory -void* sbrk(size_t s) { +void* sbrk(ptrdiff_t s) { return (void*)call(SC_SBRK, s, 0, 0, 0, 0); } void brk(void* ptr) { - return call (SC_BRK, ptr, 0, 0, 0, 0); + call (SC_BRK, (size_t)ptr, 0, 0, 0, 0); } // ********** proc diff --git a/src/user/yosh/Makefile b/src/user/yosh/Makefile deleted file mode 100644 index 9879a73..0000000 --- a/src/user/yosh/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -Obj = main.o -Out = yosh.elf - -include $(SrcPath)/user/app_common.make - -LDFLAGS += -Map test.map |