Added libshell for TCC

git-svn-id: svn://kolibrios.org@9207 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat 2021-09-26 19:56:07 +00:00
parent 8918a376d9
commit d38b3890d2
17 changed files with 226 additions and 3 deletions

Binary file not shown.

View File

@ -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)

View File

@ -0,0 +1 @@
-I../../libc.obj/include

View File

@ -0,0 +1,8 @@
#include <shell_api.h>
void shell_cls()
{
__shell_init();
*__shell_shm = SHELL_CLS;
__SHELL_WAIT();
}

View File

@ -0,0 +1,11 @@
#include <shell_api.h>
#include <sys/ksys.h>
void shell_exit()
{
if(__shell_is_init){
*__shell_shm = SHELL_EXIT;
__SHELL_WAIT();
_ksys_shm_close(__shell_shm_name);
}
}

View File

@ -0,0 +1,12 @@
#include <shell_api.h>
#include <string.h>
unsigned shell_get_pid()
{
unsigned pid;
__shell_init();
*__shell_shm = SHELL_PID;
__SHELL_WAIT();
memcpy(&pid, __shell_shm+1, sizeof(unsigned));
return pid;
}

View File

@ -0,0 +1,9 @@
#include "shell_api.h"
char shell_getc()
{
__shell_init();
*__shell_shm = SHELL_GETC;
__SHELL_WAIT();
return *(__shell_shm+1);
}

View File

@ -0,0 +1,10 @@
#include <shell_api.h>
#include <string.h>
void shell_gets(char *str, int n)
{
__shell_init();
*__shell_shm = SHELL_GETS;
__SHELL_WAIT();
strncpy(str, __shell_shm+1, n);
}

View File

@ -0,0 +1,44 @@
#include <sys/ksys.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <shell_api.h>
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();
}
}
}

View File

@ -0,0 +1,13 @@
#include <shell_api.h>
#include <sys/ksys.h>
int shell_ping()
{
__shell_init();
*__shell_shm = SHELL_PING;
_ksys_delay(10);
if(*__shell_shm==SHELL_OK){
return 1;
}
return 0;
}

View File

@ -0,0 +1,12 @@
#include <shell_api.h>
#include <stdio.h>
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();
}

View File

@ -0,0 +1,9 @@
#include <shell_api.h>
void shell_putc(char c)
{
__shell_init();
*__shell_shm = SHELL_PUTC;
*(__shell_shm+1) = c;
__SHELL_WAIT();
}

View File

@ -0,0 +1,10 @@
#include <shell_api.h>
#include <string.h>
void shell_puts(const char *str)
{
__shell_init();
*__shell_shm = SHELL_PUTS;
strcpy(__shell_shm+1, str);
__SHELL_WAIT();
}

View File

@ -0,0 +1,37 @@
#ifndef _SHELL_API_H_
#define _SHELL_API_H_
#include <sys/ksys.h>
#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

View File

@ -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)

View File

@ -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

View File

@ -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;
}