diff --git a/contrib/kolibri-libc/samples/Makefile b/contrib/kolibri-libc/samples/Makefile index d2604df9a1..045f029d7d 100644 --- a/contrib/kolibri-libc/samples/Makefile +++ b/contrib/kolibri-libc/samples/Makefile @@ -1,18 +1,22 @@ KGCC = ../linuxtools/kgcc KLD = ../linuxtools/kld KPACK = kpack -KTCC=../../../programs/develop/ktcc/trunk/bin/kos32-tcc -KTCC_FLAGS = -nostdlib -I$(KLIBC)/source/include -L$(KLIBC)/lib $(KLIBC)/lib/crt0.o +KTCC=../../../programs/develop/ktcc/trunk/bin/kos32-tcc +KTCC_FLAGS = -nostdlib -stack=1048576 -I$(KLIBC)/source/include -L$(KLIBC)/lib $(KLIBC)/lib/crt0.o + +ifndef NAME +NAME = basic_gui +endif static_kgcc: - $(KGCC) basic_gui.c - $(KLD) basic_gui.o -o basic_gui -lc - $(KPACK) basic_gui + $(KGCC) $(NAME).c + $(KLD) $(NAME).o -o $(NAME) -lc + $(KPACK) $(NAME) static_tcc: - $(KTCC) $(KTCC_FLAGS) basic_gui.c -o basic_gui -lc - $(KPACK) basic_gui + $(KTCC) $(KTCC_FLAGS) $(NAME).c -o $(NAME) -lc + $(KPACK) $(NAME) dynamic_tcc: - $(KTCC) $(KTCC_FLAGS) -D_DYNAMIC basic_gui.c -o basic_gui_dyn -lc.obj - $(KPACK) basic_gui_dyn + $(KTCC) $(KTCC_FLAGS) -D_DYNAMIC $(NAME).c -o $(NAME)_dyn -lc.obj + $(KPACK) $(NAME)_dyn diff --git a/contrib/kolibri-libc/samples/shell_io.c b/contrib/kolibri-libc/samples/shell_io.c new file mode 100644 index 0000000000..f19295772b --- /dev/null +++ b/contrib/kolibri-libc/samples/shell_io.c @@ -0,0 +1,19 @@ +#include +#include +#include + +int main() +{ + char string[256]; + shell_cls(); + shell_puts("This is a test console application for Shell\n\r"); + shell_puts("Type a string (255 symbols max): "); + shell_gets(string); + shell_puts("You typed:\n\r"); + shell_puts(string); + shell_puts("Press any key: "); + string[0] = shell_getc(); + shell_puts("\n\rYou pressed: "); + shell_putc(string[0]); + shell_exit(); +} diff --git a/contrib/kolibri-libc/source/Makefile b/contrib/kolibri-libc/source/Makefile index d9537dd869..cb910d62db 100644 --- a/contrib/kolibri-libc/source/Makefile +++ b/contrib/kolibri-libc/source/Makefile @@ -9,7 +9,7 @@ LIBC.A = $(LIB_DIR)/libc.a LIBC.OBJ = $(LIB_DIR)/libc.obj CFLAGS = -I$(INCLUDE) -m32 -nostdinc -nostdlib -DGNUC -Os -fno-common -fno-builtin -fno-leading-underscore -fno-pie -DIRS := string stdlib stdio sys math ctype exports setjmp +DIRS := string stdlib stdio sys math ctype exports setjmp shell_api cfiles := $(foreach dir,$(DIRS),$(patsubst %.c, %.o, $(wildcard $(dir)/*.c))) asmfiles := $(foreach dir,$(DIRS),$(patsubst %.s, %.o, $(wildcard $(dir)/*.s))) @@ -47,7 +47,7 @@ clean: shared: $(cfiles) $(asmfiles) $(CLINK) $^ - $(KPACK) a.out.obj + #$(KPACK) a.out.obj mv a.out.obj $(LIBC.OBJ) test: diff --git a/contrib/kolibri-libc/source/include/ksys.h b/contrib/kolibri-libc/source/include/ksys.h index 1a5902503e..88149bce2d 100644 --- a/contrib/kolibri-libc/source/include/ksys.h +++ b/contrib/kolibri-libc/source/include/ksys.h @@ -189,15 +189,19 @@ enum KSYS_MOUSE_POS{ KSYS_MOUSE_WINDOW_POS = 1 }; +enum KSYS_SHM_MODE{ + KSYS_SHM_OPEN = 0x00, + KSYS_SHM_OPEN_ALWAYS = 0x04, + KSYS_SHM_CREATE = 0x08, + KSYS_SHM_READ = 0x00, + KSYS_SHM_WRITE = 0x01, +}; + static inline int _ksys_strcmp(const char * s1, const char * s2 ) { - while ((*s1) && (*s1 == *s2)){ - ++s1; - ++s2; - } - - return ( *( unsigned char * )s1 - * ( unsigned char * )s2 ); + while ((*s1) && (*s1 == *s2)){ ++s1; ++s2; } + return(*(unsigned char*)s1 - *(unsigned char *)s2); } // Functions for working with the graphical interface @@ -360,6 +364,7 @@ ksys_pos_t _ksys_get_mouse_pos(int origin) "rol $16, %%eax" :"=a"(val) :"a"(37),"b"(origin) + :"memory" ); return val; } @@ -918,7 +923,8 @@ int not_optimized _ksys_file_rename(const char *name, const char *new_name) static inline -int not_optimized _ksys_exec(char *app_name, char *args){ +int not_optimized _ksys_exec(char *app_name, char *args) +{ ksys70_t file_op; file_op.p00 = 7; file_op.p04dw = 0; @@ -933,4 +939,28 @@ int not_optimized _ksys_exec(char *app_name, char *args){ return val; } +/* Working with a named shared memory area. */ + +static inline +int _ksys_shm_open(char *name, int mode, int size, char **new_shm) +{ + int error; + asm_inline( + "int $0x40" + :"=a"(*new_shm), "=d"(error) + :"a"(68), "b"(22), "c"(name), "d"(size), "S"(mode) + ); + return error; +} + + +static inline +void _ksys_shm_close(char *shm_name) +{ + asm_inline( + "int $0x40": + :"a"(68), "b"(23), "c"(shm_name) + ); +} + #endif // _KSYS_H_ \ No newline at end of file diff --git a/contrib/kolibri-libc/source/include/shell_api.h b/contrib/kolibri-libc/source/include/shell_api.h new file mode 100644 index 0000000000..9e46f58a2b --- /dev/null +++ b/contrib/kolibri-libc/source/include/shell_api.h @@ -0,0 +1,13 @@ +#ifndef _SHELL_API_H_ +#define _SHELL_API_H_ + +#include + +extern void _FUNC(shell_puts)(const char *s); +extern void _FUNC(shell_putc)(char c); +extern char _FUNC(shell_getc)(); +extern void _FUNC(shell_gets)(char *str); +extern void _FUNC(shell_cls)(); +extern void _FUNC(shell_exit)(); + +#endif \ No newline at end of file diff --git a/contrib/kolibri-libc/source/include/stdlib.h b/contrib/kolibri-libc/source/include/stdlib.h index e51039c1ff..d59a1d0f9a 100644 --- a/contrib/kolibri-libc/source/include/stdlib.h +++ b/contrib/kolibri-libc/source/include/stdlib.h @@ -14,7 +14,7 @@ extern int _FUNC(atoi)(const char *s); extern long _FUNC(atol)(const char *); extern long long _FUNC(atoll)(const char *); -//char* itoa)(int n, char* s); +extern void _FUNC(itoa)(int n, char* s); extern int _FUNC(abs)(int); extern long _FUNC(labs)(long); diff --git a/contrib/kolibri-libc/source/shell_api/shell.h b/contrib/kolibri-libc/source/shell_api/shell.h new file mode 100644 index 0000000000..9e2fd0e456 --- /dev/null +++ b/contrib/kolibri-libc/source/shell_api/shell.h @@ -0,0 +1,21 @@ + +#include + +#define SHELL_OK 0 +#define SHELL_EXIT 1 +#define SHELL_PUTC 2 +#define SHELL_PUTS 3 +#define SHELL_GETC 4 +#define SHELL_GETS 5 +#define SHELL_CLS 6 + +extern char __shell_shm_name[32]; +extern char *__shell_shm; +extern int __shell_is_init; +extern int __shell_init(); +extern void __shell_wait(); + +#define SHELL_WAIT() while (*__shell_shm) _ksys_delay(5) + + + diff --git a/contrib/kolibri-libc/source/shell_api/shell_cls.c b/contrib/kolibri-libc/source/shell_api/shell_cls.c new file mode 100644 index 0000000000..c41a137822 --- /dev/null +++ b/contrib/kolibri-libc/source/shell_api/shell_cls.c @@ -0,0 +1,8 @@ +#include "shell.h" + +void shell_cls() +{ + __shell_init(); + *__shell_shm = SHELL_CLS; + SHELL_WAIT(); +} \ No newline at end of file diff --git a/contrib/kolibri-libc/source/shell_api/shell_exit.c b/contrib/kolibri-libc/source/shell_api/shell_exit.c new file mode 100644 index 0000000000..d579798a66 --- /dev/null +++ b/contrib/kolibri-libc/source/shell_api/shell_exit.c @@ -0,0 +1,11 @@ +#include "shell.h" +#include "ksys.h" + +void shell_exit() +{ + if(__shell_is_init){ + *__shell_shm = SHELL_EXIT; + SHELL_WAIT(); + _ksys_shm_close(__shell_shm_name); + } +} \ No newline at end of file diff --git a/contrib/kolibri-libc/source/shell_api/shell_getc.c b/contrib/kolibri-libc/source/shell_api/shell_getc.c new file mode 100644 index 0000000000..0eaf63ca54 --- /dev/null +++ b/contrib/kolibri-libc/source/shell_api/shell_getc.c @@ -0,0 +1,9 @@ +#include "shell.h" + +char shell_getc() +{ + __shell_init(); + *__shell_shm = SHELL_GETC; + SHELL_WAIT(); + return *(__shell_shm+1); +} \ No newline at end of file diff --git a/contrib/kolibri-libc/source/shell_api/shell_gets.c b/contrib/kolibri-libc/source/shell_api/shell_gets.c new file mode 100644 index 0000000000..40b55dba95 --- /dev/null +++ b/contrib/kolibri-libc/source/shell_api/shell_gets.c @@ -0,0 +1,9 @@ +#include "shell.h" + +void shell_gets(char *str) +{ + __shell_init(); + *__shell_shm = SHELL_GETS; + SHELL_WAIT(); + strcpy(str, __shell_shm+1); +} \ No newline at end of file diff --git a/contrib/kolibri-libc/source/shell_api/shell_init.c b/contrib/kolibri-libc/source/shell_api/shell_init.c new file mode 100644 index 0000000000..c0e5b8d0d2 --- /dev/null +++ b/contrib/kolibri-libc/source/shell_api/shell_init.c @@ -0,0 +1,37 @@ +#include +#include +#include + +char __shell_shm_name[32]; +char*__shell_shm=NULL; +int __shell_is_init=0; + +int __shell_shm_init() +{ + __shell_is_init=1; + ksys_proc_table_t *proc_info = (ksys_proc_table_t*)malloc(sizeof(ksys_proc_table_t)); + if(proc_info == NULL){ + return -1; + } + unsigned PID; + + _ksys_process_info(proc_info, -1); + PID = proc_info->pid; + free(proc_info); + + itoa(PID, __shell_shm_name); + strcat(__shell_shm_name, "-SHELL"); + return _ksys_shm_open(__shell_shm_name, KSYS_SHM_OPEN_ALWAYS | KSYS_SHM_WRITE, 1024*16, &__shell_shm); +} + +int __shell_init() +{ + if(__shell_is_init){ + return 0; + } + if(__shell_shm_init()){ + debug_printf("Shell problems detected!\n"); + return -1; + } + return 0; +} \ No newline at end of file diff --git a/contrib/kolibri-libc/source/shell_api/shell_putc.c b/contrib/kolibri-libc/source/shell_api/shell_putc.c new file mode 100644 index 0000000000..9ecc2b1e98 --- /dev/null +++ b/contrib/kolibri-libc/source/shell_api/shell_putc.c @@ -0,0 +1,9 @@ +#include "shell.h" + +void shell_putc(char c) +{ + __shell_init(); + *__shell_shm = SHELL_PUTC; + *(__shell_shm+1) = c; + SHELL_WAIT(); +} \ No newline at end of file diff --git a/contrib/kolibri-libc/source/shell_api/shell_puts.c b/contrib/kolibri-libc/source/shell_api/shell_puts.c new file mode 100644 index 0000000000..a578a6088a --- /dev/null +++ b/contrib/kolibri-libc/source/shell_api/shell_puts.c @@ -0,0 +1,10 @@ +#include "shell.h" +#include "string.h" + +void shell_puts(const char *str) +{ + __shell_init(); + *__shell_shm = SHELL_PUTS; + strcpy(__shell_shm+1, str); + SHELL_WAIT(); +} \ No newline at end of file diff --git a/contrib/kolibri-libc/source/stdlib/itoa.c b/contrib/kolibri-libc/source/stdlib/itoa.c index 711ee31552..27ede3a726 100644 --- a/contrib/kolibri-libc/source/stdlib/itoa.c +++ b/contrib/kolibri-libc/source/stdlib/itoa.c @@ -1,20 +1,42 @@ #include -/* -char* itoa(int n, char* s) +#include + +char *__reverse(char *str) { - int sign; - char *ptr; - ptr = s; - - if(n == (int)0x80000000) - return strcpy(s, "-2147483648"); // overflowed -n - - if ((sign = n) < 0) n = -n; - do { - *ptr++ = n % 10 + '0'; - } while ((n = n / 10) > 0); - if (sign < 0) *ptr++ = '-'; - *ptr = '\0'; - return strrev(s); + char tmp, *src, *dst; + size_t len; + if (str != NULL){ + len = strlen (str); + if (len > 1) { + src = str; + dst = src + len - 1; + while (src < dst) { + tmp = *src; + *src++ = *dst; + *dst-- = tmp; + } + } + } + return str; } -*/ \ No newline at end of file + +/* itoa from K&R */ +void itoa(int n, char s[]) +{ + int i, sign; + + if ((sign = n) < 0) /* record sign */ + n = -n; /* make n positive */ + i = 0; + + do { /* generate digits in reverse order */ + s[i++] = n % 10 + '0'; /* get next digit */ + } while ((n /= 10) > 0); /* delete it */ + + if (sign < 0) + s[i++] = '-'; + + __reverse(s); + s[i] = '\0'; + return; +} \ No newline at end of file