kolibri-libc:

- Added shell apis(shell_api.h)
- Added functions  for working with a named shared memory area(ksys.h).
- Fixed itoa

git-svn-id: svn://kolibrios.org@8629 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat 2021-03-03 14:56:26 +00:00
parent 7d923337f0
commit 781f3f0fab
15 changed files with 238 additions and 36 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,13 @@
#ifndef _SHELL_API_H_
#define _SHELL_API_H_
#include <stddef.h>
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

View File

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

View File

@ -0,0 +1,21 @@
#include <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
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)

View File

@ -0,0 +1,8 @@
#include "shell.h"
void shell_cls()
{
__shell_init();
*__shell_shm = SHELL_CLS;
SHELL_WAIT();
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,37 @@
#include <ksys.h>
#include <string.h>
#include <stdlib.h>
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;
}

View File

@ -0,0 +1,9 @@
#include "shell.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.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

@ -1,20 +1,42 @@
#include <string.h>
/*
char* itoa(int n, char* s)
#include <ksys.h>
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;
}
*/
/* 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;
}