mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-07-17 18:21:43 +00:00
Merge remote-tracking branch 'Userspace/master'
This commit is contained in:
66
Userspace/apps/base/fsh/Makefile
Normal file
66
Userspace/apps/base/fsh/Makefile
Normal file
@@ -0,0 +1,66 @@
|
||||
WORKSPACE := ../../../
|
||||
|
||||
# Config file
|
||||
include ../$(WORKSPACE)Makefile.conf
|
||||
|
||||
CC = ../$(WORKSPACE)$(COMPILER_PATH)/$(COMPILER_ARCH)gcc
|
||||
CPP = ../$(WORKSPACE)$(COMPILER_PATH)/$(COMPILER_ARCH)g++
|
||||
LD = ../$(WORKSPACE)$(COMPILER_PATH)/$(COMPILER_ARCH)ld
|
||||
AS = ../$(WORKSPACE)$(COMPILER_PATH)/$(COMPILER_ARCH)as
|
||||
OBJDUMP = ../$(WORKSPACE)$(COMPILER_PATH)/$(COMPILER_ARCH)objdump
|
||||
|
||||
GIT_COMMIT = $(shell git rev-parse HEAD)
|
||||
GIT_COMMIT_SHORT = $(shell git rev-parse --short HEAD)
|
||||
|
||||
ifeq ($(OSARCH), amd64)
|
||||
S_SOURCES = $(shell find ./ -type f -name '*.S' -not -path "./arch/i386/*" -not -path "./arch/aarch64/*")
|
||||
C_SOURCES = $(shell find ./ -type f -name '*.c' -not -path "./arch/i386/*" -not -path "./arch/aarch64/*")
|
||||
CPP_SOURCES = $(shell find ./ -type f -name '*.cpp' -not -path "./arch/i386/*" -not -path "./arch/aarch64/*")
|
||||
else ifeq ($(OSARCH), i386)
|
||||
S_SOURCES = $(shell find ./ -type f -name '*.S' -not -path "./arch/amd64/*" -not -path "./arch/aarch64/*")
|
||||
C_SOURCES = $(shell find ./ -type f -name '*.c' -not -path "./arch/amd64/*" -not -path "./arch/aarch64/*")
|
||||
CPP_SOURCES = $(shell find ./ -type f -name '*.cpp' -not -path "./arch/amd64/*" -not -path "./arch/aarch64/*")
|
||||
else ifeq ($(OSARCH), aarch64)
|
||||
S_SOURCES = $(shell find ./ -type f -name '*.S' -not -path "./arch/amd64/*" -not -path "./arch/i386/*")
|
||||
C_SOURCES = $(shell find ./ -type f -name '*.c' -not -path "./arch/amd64/*" -not -path "./arch/i386/*")
|
||||
CPP_SOURCES = $(shell find ./ -type f -name '*.cpp' -not -path "./arch/amd64/*" -not -path "./arch/i386/*")
|
||||
endif
|
||||
OBJ = $(C_SOURCES:.c=.o) $(CPP_SOURCES:.cpp=.o) $(ASM_SOURCES:.asm=.o) $(S_SOURCES:.S=.o) $(PSF_SOURCES:.psf=.o) $(BMP_SOURCES:.bmp=.o)
|
||||
|
||||
SYSROOT = --sysroot=$(WORKSPACE)out/
|
||||
FILENAME = sh
|
||||
|
||||
HEADERS = $(sort $(dir $(wildcard $(WORKSPACE)out/include/*)))
|
||||
|
||||
LDFLAGS =
|
||||
CFLAGS = -I$(WORKSPACE)out/include \
|
||||
-DGIT_COMMIT='"$(GIT_COMMIT)"' \
|
||||
-DGIT_COMMIT_SHORT='"$(GIT_COMMIT_SHORT)"'
|
||||
WARNCFLAG = -Wall -Wextra
|
||||
|
||||
ifeq ($(DEBUG), 1)
|
||||
CFLAGS += -DDEBUG -ggdb3 -O0 -fdiagnostics-color=always -fverbose-asm
|
||||
LDFLAGS += -ggdb3 -O0
|
||||
endif
|
||||
|
||||
build: $(FILENAME)
|
||||
mv $(FILENAME) $(WORKSPACE)out/bin/$(FILENAME)
|
||||
|
||||
$(FILENAME): $(OBJ)
|
||||
$(info Linking $@)
|
||||
$(CC) $(LDFLAGS) $(SYSROOT) $(OBJ) -o $@
|
||||
|
||||
%.o: %.c $(HEADERS)
|
||||
$(info Compiling $<)
|
||||
$(CC) $(CFLAGS) $(WARNCFLAG) -std=c17 -c $< -o $@
|
||||
|
||||
%.o: %.cpp $(HEADERS)
|
||||
$(info Compiling $<)
|
||||
$(CPP) $(CFLAGS) $(WARNCFLAG) -std=c++20 -fexceptions -c $< -o $@ -fno-rtti
|
||||
|
||||
%.o: %.S
|
||||
$(info Compiling $<)
|
||||
$(AS) -o $@ $<
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ)
|
160
Userspace/apps/base/fsh/fsh.c
Normal file
160
Userspace/apps/base/fsh/fsh.c
Normal file
@@ -0,0 +1,160 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#define _GNU_SOURCE
|
||||
#include <signal.h>
|
||||
#include <termios.h>
|
||||
#define _POSIX_SOURCE
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_COMMAND_LENGTH 1024
|
||||
#define MAX_ARGS 10
|
||||
|
||||
void DisableInputBuffering()
|
||||
{
|
||||
struct termios tty_attr;
|
||||
tcgetattr(STDIN_FILENO, &tty_attr);
|
||||
tty_attr.c_lflag &= ~(ICANON | ECHO);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &tty_attr);
|
||||
}
|
||||
|
||||
void EnableInputBuffering()
|
||||
{
|
||||
struct termios tty_attr;
|
||||
tcgetattr(STDIN_FILENO, &tty_attr);
|
||||
tty_attr.c_lflag |= ICANON | ECHO;
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &tty_attr);
|
||||
}
|
||||
|
||||
void ReadLine(char *Buffer, size_t BufferSize)
|
||||
{
|
||||
size_t index = 0;
|
||||
int c;
|
||||
while (1)
|
||||
{
|
||||
c = getchar();
|
||||
if (c == EOF || c == '\n')
|
||||
{
|
||||
Buffer[index] = '\0';
|
||||
break;
|
||||
}
|
||||
else if (c == 127 || c == 8) // Backspace
|
||||
{
|
||||
if (index > 0)
|
||||
{
|
||||
printf("\b \b");
|
||||
index--;
|
||||
}
|
||||
}
|
||||
else if (c == 4) // Ctrl + D
|
||||
{
|
||||
if (index == 0)
|
||||
kill(getpid(), SIGQUIT);
|
||||
else
|
||||
putchar('\a');
|
||||
}
|
||||
else
|
||||
{
|
||||
if (index < BufferSize - 1)
|
||||
{
|
||||
putchar(c);
|
||||
Buffer[index] = (char)c;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ExecuteCommand(char *command)
|
||||
{
|
||||
char *args[MAX_ARGS];
|
||||
int i = 0;
|
||||
|
||||
char *token = strtok(command, " ");
|
||||
while (token != NULL && i < MAX_ARGS - 1)
|
||||
{
|
||||
args[i++] = token;
|
||||
token = strtok(NULL, " ");
|
||||
}
|
||||
args[i] = NULL;
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
execvp(args[0], args);
|
||||
perror("execvp");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else if (pid > 0)
|
||||
wait(NULL);
|
||||
else
|
||||
{
|
||||
perror("fork");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void HandleSignal(int signal)
|
||||
{
|
||||
if (signal == SIGQUIT)
|
||||
{
|
||||
EnableInputBuffering();
|
||||
printf("\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else if (signal == SIGINT)
|
||||
{
|
||||
putchar('\n');
|
||||
}
|
||||
else if (signal == SIGHUP)
|
||||
{
|
||||
EnableInputBuffering();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else if (signal == SIGCHLD)
|
||||
wait(NULL);
|
||||
else if (signal == SIGSEGV)
|
||||
{
|
||||
printf("Segmentation fault\n");
|
||||
while (1)
|
||||
sleep(1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Signal %s(%d) received\n",
|
||||
strsignal(signal), signal);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char command[MAX_COMMAND_LENGTH];
|
||||
for (int i = 0; i < NSIG; ++i)
|
||||
signal(i, HandleSignal);
|
||||
|
||||
char hostname[256];
|
||||
gethostname(hostname, sizeof(hostname));
|
||||
|
||||
while (1)
|
||||
{
|
||||
printf("\033[1;32m");
|
||||
printf("┌──(%s@%s)-[%s]\n", getenv("USER"), hostname, getenv("PWD"));
|
||||
printf("└$ ");
|
||||
printf("\033[0m");
|
||||
|
||||
DisableInputBuffering();
|
||||
ReadLine(command, sizeof(command));
|
||||
EnableInputBuffering();
|
||||
putchar('\n');
|
||||
|
||||
if (strcmp(command, "exit") == 0)
|
||||
break;
|
||||
|
||||
ExecuteCommand(command);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user