From d38b3890d2c9c58aa94ff019ab0decd82e0a5ff1 Mon Sep 17 00:00:00 2001 From: turbocat Date: Sun, 26 Sep 2021 19:56:07 +0000 Subject: [PATCH] Added libshell for TCC git-svn-id: svn://kolibrios.org@9207 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../develop/ktcc/trunk/bin/lib/libshell.a | Bin 0 -> 9436 bytes .../develop/ktcc/trunk/lib/libshell/Makefile | 26 +++++++++++ .../ktcc/trunk/lib/libshell/compile_flags.txt | 1 + .../ktcc/trunk/lib/libshell/shell_cls.c | 8 ++++ .../ktcc/trunk/lib/libshell/shell_exit.c | 11 +++++ .../ktcc/trunk/lib/libshell/shell_get_pid.c | 12 +++++ .../ktcc/trunk/lib/libshell/shell_getc.c | 9 ++++ .../ktcc/trunk/lib/libshell/shell_gets.c | 10 ++++ .../ktcc/trunk/lib/libshell/shell_init.c | 44 ++++++++++++++++++ .../ktcc/trunk/lib/libshell/shell_ping.c | 13 ++++++ .../ktcc/trunk/lib/libshell/shell_printf.c | 12 +++++ .../ktcc/trunk/lib/libshell/shell_putc.c | 9 ++++ .../ktcc/trunk/lib/libshell/shell_puts.c | 10 ++++ .../ktcc/trunk/libc.obj/include/shell_api.h | 37 +++++++++++++++ .../ktcc/trunk/libc.obj/samples/Makefile | 6 +-- .../ktcc/trunk/libc.obj/samples/build_all.sh | 1 + .../ktcc/trunk/libc.obj/samples/shell_test.c | 20 ++++++++ 17 files changed, 226 insertions(+), 3 deletions(-) create mode 100644 programs/develop/ktcc/trunk/bin/lib/libshell.a create mode 100644 programs/develop/ktcc/trunk/lib/libshell/Makefile create mode 100644 programs/develop/ktcc/trunk/lib/libshell/compile_flags.txt create mode 100644 programs/develop/ktcc/trunk/lib/libshell/shell_cls.c create mode 100644 programs/develop/ktcc/trunk/lib/libshell/shell_exit.c create mode 100644 programs/develop/ktcc/trunk/lib/libshell/shell_get_pid.c create mode 100644 programs/develop/ktcc/trunk/lib/libshell/shell_getc.c create mode 100644 programs/develop/ktcc/trunk/lib/libshell/shell_gets.c create mode 100644 programs/develop/ktcc/trunk/lib/libshell/shell_init.c create mode 100644 programs/develop/ktcc/trunk/lib/libshell/shell_ping.c create mode 100644 programs/develop/ktcc/trunk/lib/libshell/shell_printf.c create mode 100644 programs/develop/ktcc/trunk/lib/libshell/shell_putc.c create mode 100644 programs/develop/ktcc/trunk/lib/libshell/shell_puts.c create mode 100644 programs/develop/ktcc/trunk/libc.obj/include/shell_api.h create mode 100644 programs/develop/ktcc/trunk/libc.obj/samples/shell_test.c diff --git a/programs/develop/ktcc/trunk/bin/lib/libshell.a b/programs/develop/ktcc/trunk/bin/lib/libshell.a new file mode 100644 index 0000000000000000000000000000000000000000..582aa71f6f8275ce5bbdbace3815770535121f5b GIT binary patch literal 9436 zcmc&)O>7%Q6ds2_-4I9$<-Zh{-vDh0A#DOGV4y}tB#!x-YRk&2 z(R1-v>eRb%!#6kJQW-OC?4h3gWA9Xdt!+p1izvLS18?>Ocphbwm9GXT(dHVo-=rmT z*#LI0BTqQ8fAYl_v=_;;Hd$tse7$t2l+1d$^dVcPr(|-4QWM{SC*zqBOy14pS15Il z<&S}PV10H|UZ>P^lrO`jzw8|>tNv`doL2o)rIP9|9hxntr@+f|n$LMTYgC%HI@}kM zIL9m)iYEHThqg}EY3mr0w&RdUr}edV5xc9P0$|~JhE7{9Ozf_K!n&hwI*sdL6#5ly zSm@uWROV0u2MGvLD)zb6i_d{Qdoi#vRZJNq@DCBODHg zhim&+D;%D&4{3@+jVS}TWe!x(7Y9xz#!bW8`GqqfKv{=3POQLCUe?Fuy{RJB9tPiw zB3Kh_QRiZdYk4<{5V1BW0yyQ`CRgegZ0T*eiY;gVRB+`?Q7~ojg>D!eI|FfNu86{ArrWT;X875Ng-)PN zNY>rO&ptGS!Os!c1>?s?TR^B)92*`n@hnUnJ8uWwfFHiJA3vOQ!Oz8_mqAs0nfFhO zA5E-EWb3XZRxfa>LPJUBlQ$7ZBj9Ni!I8#-)e%Rxp)SNV<^Ci@+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fu4cPoN0!7I7E>gi}?;Y z9AJ@ry;2{Oc1ez0)0geNBrLE&w=xd>^I7I-rtn5m8^pg@i|wK1CTrm zid+jn1Lybs9poFaw^9!~=IK0(eu^A+V{C8JIe?s*;#IWYlDqM31><{?4VLKNBw_)t z17sN5i8ttf*#y8rHqfbngzMpnnL-$Pj2s-`=dLi7R>2_SPd{xS#gR_v>4{gXf zuB|9CHdNJCJTN%EAzQb}UCT4I;qc!*XYHUH;u{B~w{a5*mN^+=ayw&=J+~XD)`_Su zGcV{g_;YYy9;hc(5}NSCzGJtL64qw%aD z=)357jFaco?Z^?|z+;8zdn6Y_pBr`cDt0c0g6v!j(T@8X-G)BS{+eOvMlAHxXcLlk zcQKSg!vKuf4{gYsVdx_?24l!<*US=IVrXb2^x45lJLm=sagaw7-An~J56b;vXLW)< z?kuY~vpr+NpLlvr^Jp`Uc5gc(%NYoKW^j{(31?#&<7)qi#FuVa8gF&MR0=uB!v5x8XgRflVf(!4LHFV z>tYSpUt&$X=hRqKg#QH+dS3WQlEju#)CCg~Tc`^YTjac%lU79p|J5Pk;}XxCC5GIG z=Mx)GfVT@189ZA^eBH&wF)^@Q>|9I)*}0gYT?(ZYCRSkMkL%XG8qOC+&=3X_DcCi` F#6M1AWE=nh literal 0 HcmV?d00001 diff --git a/programs/develop/ktcc/trunk/lib/libshell/Makefile b/programs/develop/ktcc/trunk/lib/libshell/Makefile new file mode 100644 index 0000000000..54d6dedbfc --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/Makefile @@ -0,0 +1,26 @@ +CC = kos32-tcc +AR = ar +CFLAGS = -c -I../../libc.obj/include + +LIB = libshell.a + +OBJS = \ + shell_exit.o \ + shell_puts.o \ + shell_get_pid.o \ + shell_ping.o \ + shell_getc.o \ + shell_cls.o \ + shell_init.o \ + shell_gets.o \ + shell_printf.o \ + shell_putc.o + +$(LIB): $(OBJS) + $(AR) -crs $@ $(OBJS) + +%.o : %.c Makefile + $(CC) $(CFLAGS) -o $@ $< + +clean: + rm -rf $(OBJS) $(LIB) \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/libshell/compile_flags.txt b/programs/develop/ktcc/trunk/lib/libshell/compile_flags.txt new file mode 100644 index 0000000000..8f44bce8ff --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/compile_flags.txt @@ -0,0 +1 @@ +-I../../libc.obj/include \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/libshell/shell_cls.c b/programs/develop/ktcc/trunk/lib/libshell/shell_cls.c new file mode 100644 index 0000000000..e85af9b77c --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/shell_cls.c @@ -0,0 +1,8 @@ +#include + +void shell_cls() +{ + __shell_init(); + *__shell_shm = SHELL_CLS; + __SHELL_WAIT(); +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/libshell/shell_exit.c b/programs/develop/ktcc/trunk/lib/libshell/shell_exit.c new file mode 100644 index 0000000000..1c34e9f656 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/shell_exit.c @@ -0,0 +1,11 @@ +#include +#include + +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/programs/develop/ktcc/trunk/lib/libshell/shell_get_pid.c b/programs/develop/ktcc/trunk/lib/libshell/shell_get_pid.c new file mode 100644 index 0000000000..d54cba03e6 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/shell_get_pid.c @@ -0,0 +1,12 @@ +#include +#include + +unsigned shell_get_pid() +{ + unsigned pid; + __shell_init(); + *__shell_shm = SHELL_PID; + __SHELL_WAIT(); + memcpy(&pid, __shell_shm+1, sizeof(unsigned)); + return pid; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/libshell/shell_getc.c b/programs/develop/ktcc/trunk/lib/libshell/shell_getc.c new file mode 100644 index 0000000000..84c9efd1c9 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/shell_getc.c @@ -0,0 +1,9 @@ +#include "shell_api.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/programs/develop/ktcc/trunk/lib/libshell/shell_gets.c b/programs/develop/ktcc/trunk/lib/libshell/shell_gets.c new file mode 100644 index 0000000000..41ff708fa3 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/shell_gets.c @@ -0,0 +1,10 @@ +#include +#include + +void shell_gets(char *str, int n) +{ + __shell_init(); + *__shell_shm = SHELL_GETS; + __SHELL_WAIT(); + strncpy(str, __shell_shm+1, n); +} diff --git a/programs/develop/ktcc/trunk/lib/libshell/shell_init.c b/programs/develop/ktcc/trunk/lib/libshell/shell_init.c new file mode 100644 index 0000000000..c98ffe3d59 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/shell_init.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +char app_name[13]; +char __shell_shm_name[32]; +char*__shell_shm=NULL; +int __shell_is_init=0; + +int __shell_shm_init() +{ + __shell_is_init=1; + ksys_thread_t *proc_info = (ksys_thread_t*)malloc(sizeof(ksys_thread_t)); + if(proc_info == NULL){ + return -1; + } + unsigned PID; + + _ksys_thread_info(proc_info, -1); + PID = proc_info->pid; + strncpy(app_name, proc_info->name, 12); + 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, SHELL_SHM_MAX, &__shell_shm); +} + +void __shell_init() +{ + if(!__shell_is_init){ + if(__shell_shm_init()){ + debug_printf("%s: shell problems detected!\n", app_name); + _ksys_exit(); + } + + if(!shell_ping()){ + debug_printf("%s: no shell found!\n", app_name); + _ksys_exit(); + } + } +} diff --git a/programs/develop/ktcc/trunk/lib/libshell/shell_ping.c b/programs/develop/ktcc/trunk/lib/libshell/shell_ping.c new file mode 100644 index 0000000000..87c1169fe8 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/shell_ping.c @@ -0,0 +1,13 @@ +#include +#include + +int shell_ping() +{ + __shell_init(); + *__shell_shm = SHELL_PING; + _ksys_delay(10); + if(*__shell_shm==SHELL_OK){ + return 1; + } + return 0; +} \ No newline at end of file diff --git a/programs/develop/ktcc/trunk/lib/libshell/shell_printf.c b/programs/develop/ktcc/trunk/lib/libshell/shell_printf.c new file mode 100644 index 0000000000..c61628e47f --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/shell_printf.c @@ -0,0 +1,12 @@ +#include +#include + +void shell_printf(const char *format,...) +{ + va_list ap; + va_start (ap, format); + *__shell_shm=SHELL_PUTS; + vsnprintf(__shell_shm+1, SHELL_SHM_MAX, format, ap); + va_end(ap); + __SHELL_WAIT(); +} diff --git a/programs/develop/ktcc/trunk/lib/libshell/shell_putc.c b/programs/develop/ktcc/trunk/lib/libshell/shell_putc.c new file mode 100644 index 0000000000..fa4d7b9284 --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/shell_putc.c @@ -0,0 +1,9 @@ +#include + +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/programs/develop/ktcc/trunk/lib/libshell/shell_puts.c b/programs/develop/ktcc/trunk/lib/libshell/shell_puts.c new file mode 100644 index 0000000000..58ddf8734c --- /dev/null +++ b/programs/develop/ktcc/trunk/lib/libshell/shell_puts.c @@ -0,0 +1,10 @@ +#include +#include + +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/programs/develop/ktcc/trunk/libc.obj/include/shell_api.h b/programs/develop/ktcc/trunk/libc.obj/include/shell_api.h new file mode 100644 index 0000000000..3a68b2d4b6 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc.obj/include/shell_api.h @@ -0,0 +1,37 @@ +#ifndef _SHELL_API_H_ +#define _SHELL_API_H_ + +#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 +#define SHELL_PID 7 +#define SHELL_PING 8 + +#define SHELL_SHM_MAX 1024*16 + +extern char __shell_shm_name[32]; +extern char *__shell_shm; +extern int __shell_is_init; +extern void __shell_init(); + +#define __SHELL_WAIT() while (*__shell_shm) _ksys_delay(5) + +extern int shell_ping(); +extern unsigned shell_get_pid(); +extern void shell_exit(); + +extern char shell_getc(); +extern void shell_gets(char *str, int n); + +extern void shell_putc(char c); +extern void shell_puts(const char *str); +extern void shell_printf(const char *format,...); + +extern void shell_cls(); +#endif diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/Makefile b/programs/develop/ktcc/trunk/libc.obj/samples/Makefile index b0ad39d36e..2e72c0d2ac 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/Makefile +++ b/programs/develop/ktcc/trunk/libc.obj/samples/Makefile @@ -1,4 +1,3 @@ - KTCC=kos32-tcc FASM= fasm KPACK = kpack @@ -22,9 +21,10 @@ clayer/dialog.kex \ clayer/msgbox.kex \ clayer/boxlib.kex \ thread_work.kex \ -sdltest.kex +sdltest.kex \ +shell_test.kex -LIBS= -lSDL -ltcc -lsound -ldialog -lrasterworks -limg -lbox -lmsgbox -lnetwork -lc.obj +LIBS= -lSDL -lshell -ltcc -lsound -ldialog -lrasterworks -limg -lbox -lmsgbox -lnetwork -lc.obj all: $(BIN) diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh b/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh index a452e4ff34..108d7dae5b 100644 --- a/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh +++ b/programs/develop/ktcc/trunk/libc.obj/samples/build_all.sh @@ -18,3 +18,4 @@ cp /kolibrios/develop/tcc/samples/clayer/logo.png /tmp0/1/tcc_samples/logo.png /kolibrios/develop/tcc/tcc clayer/rasterworks.c -o /tmp0/1/tcc_samples/rasterworks -ltcc -lrasterworks -lc.obj /kolibrios/develop/tcc/tcc thread_work.c -o /tmp0/1/tcc_samples/thread_work -ltcc -lc.obj /kolibrios/develop/tcc/tcc -I/kolibrios/develop/tcc/include/SDL sdltest.c -o /tmp0/1/tcc_samples/sdltest -lSDL -lsound -ltcc -lc.obj +/kolibrios/develop/tcc/tcc shell_test.c -o /tmp0/1/tcc_samples/shell_test -lshell -ltcc -lc.obj diff --git a/programs/develop/ktcc/trunk/libc.obj/samples/shell_test.c b/programs/develop/ktcc/trunk/libc.obj/samples/shell_test.c new file mode 100644 index 0000000000..c56a5869ba --- /dev/null +++ b/programs/develop/ktcc/trunk/libc.obj/samples/shell_test.c @@ -0,0 +1,20 @@ + #include "../include/shell_api.h" + +char string[256]; + +int main(){ + shell_cls(); + shell_printf("SHELL PID=%d\n\r", shell_get_pid()); + + shell_puts("This is a test console application for Shell\n\r"); + shell_puts("Type a string (255 symbols max): "); + + shell_gets(string, 255); + shell_printf("You typed: %s\n\r", string); + + shell_puts("Press any key: "); + string[0] = shell_getc(); + shell_printf("\n\rYou pressed: %c", string[0]); + shell_exit(); + return 0; +} \ No newline at end of file