Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development - Textmode Texteditor #114

Merged
merged 23 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ boot.iso
boot2.iso
boot.img
*.dat
*.a

log.txt
filesystemv2.img
Expand All @@ -56,6 +57,9 @@ rootfs/bin/*

RetrOS-32-debug.img

rootfs/symbols.map
rootfs/sysutil/symbols.map

# Executables
*.exe
*.out
Expand Down
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ASFLAGS=
LDFLAGS=
MAKEFLAGS += --no-print-directory

QEMU_OPS = -device e1000,netdev=net0 -serial stdio -netdev user,id=net0,hostfwd=tcp::8080-:8080 -object filter-dump,id=net0,netdev=net0,file=dump.dat -m 32m
QEMU_OPS = -display sdl -device e1000,netdev=net0 -serial stdio -netdev user,id=net0,hostfwd=tcp::8080-:8080 -object filter-dump,id=net0,netdev=net0,file=dump.dat -m 32m

# ---------------- For counting how many files to compile ----------------
ifneq ($(words $(MAKECMDGOALS)),1)
Expand Down Expand Up @@ -61,7 +61,7 @@ KERNELOBJ = bin/kernel.o bin/terminal.o bin/helpers.o bin/pci.o bin/virtualdisk.
bin/diskdev.o bin/scheduler.o bin/work.o bin/rbuffer.o bin/errors.o bin/kclock.o bin/tar.o bin/color.o bin/loopback.o \
bin/serial.o bin/io.o bin/syscalls.o bin/list.o bin/hashmap.o bin/vbe.o bin/ksyms.o bin/windowserver.o bin/encoding.o\
bin/mouse.o bin/ipc.o bin/sysinf.o ${PROGRAMOBJ} ${GFXOBJ} bin/font8.o bin/net.o bin/fs.o bin/ext.o bin/fat16.o bin/partition.o\
bin/admin.o bin/usermanager.o bin/user.o bin/group.o bin/snake.o bin/msgbox.o bin/kevents.o
bin/admin.o bin/usermanager.o bin/user.o bin/group.o bin/snake.o bin/msgbox.o bin/kevents.o bin/textmode.o

BOOTOBJ = bin/bootloader.o

Expand All @@ -80,16 +80,19 @@ bootblock: $(BOOTOBJ)
@$(LD) $(LDFLAGS) -o bin/bootblock $^ -Ttext 0x7C00 --oformat=binary

multiboot_kernel: bin/multiboot.o $(KERNELOBJ)
@echo "[KERNEL] Linking kernel..."
@$(LD) -o bin/kernelout $^ $(LDFLAGS) -T ./boot/multiboot.ld
@echo "[KERNEL] Finished compiling kernel."

# Idea taken from SerenityOS
symbols: bin/multiboot.o $(KERNELOBJ)
@echo "[KERNEL] Creating symbols..."
@$(LD) -o bin/symbols $^ $(LDFLAGS) -T ./kernel/linkersym.ld
@nm -C -n bin/symbols | grep ' [Tt] ' | sed 's/ [Tt] / /' > rootfs/symbols.map
@nm -C -n bin/symbols | grep ' [Tt] ' | sed 's/ [Tt] / /' > rootfs/sysutil/symbols.map


kernel: bin/kcrt0.o $(KERNELOBJ)
@echo "[KERNEL] Linking kernel..."
@$(LD) -o bin/kernelout $^ $(LDFLAGS) -T ./kernel/linker.ld

.depend: **/*.[cSh]
Expand All @@ -103,7 +106,7 @@ bin/%.o: */%.c
@$(ECHO) [KERNEL] Compiling $<
@$(CC) -o $@ -c $< $(CCFLAGS)

bin/%.o: */*/%.c
bin/%.o: kernel/*/%.c
@$(ECHO) [PROGRAM] Compiling $<
@$(CC) -o $@ -c $< $(CCFLAGS)

Expand Down Expand Up @@ -156,7 +159,7 @@ create_fs:

bare: compile create_fs

img: grub_fix tools compile symbols tests create_fs sync
img: grub_fix tools compile symbols create_fs sync
@echo "Finished creating the image."
$(TIME-END)

Expand Down
4 changes: 3 additions & 1 deletion admin/usermanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static int __list_users(struct usermanager* usrman);
static int __load_users(struct usermanager* usrman);
static int __save_users(struct usermanager* usrman);
static struct user* __authenticate_user(struct usermanager* usrman, const char* username, const char* password);
static struct user* __find_user(struct usermanager* usrman, const char* username);

/* consts */
static const char* userdb = "/sysutil/users.db";
Expand All @@ -37,7 +38,8 @@ static struct user_manager_ops default_ops = {
.list = __list_users,
.load = __load_users,
.save = __save_users,
.authenticate = __authenticate_user
.authenticate = __authenticate_user,
.get = __find_user
};
static const struct userdb default_db = {
.magic = USRMAN_MAGIC,
Expand Down
2 changes: 2 additions & 0 deletions apps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ programs: staticlibs bin/crt0.o ${USROBJS}
@make -C graphs/
@make -C cube/
@make -C users/
@make -C texed/
@echo [USR] All user programs created and linked!

bin/%.o: utils/%.cpp
Expand Down Expand Up @@ -85,4 +86,5 @@ clean:
@make -C graphs/ clean
@make -C cube/ clean
@make -C users/ clean
@make -C texed/ clean
rm -f .depend
Binary file removed apps/libcore.a
Binary file not shown.
Binary file removed apps/libgraphic.a
Binary file not shown.
Binary file removed apps/libnet.a
Binary file not shown.
53 changes: 53 additions & 0 deletions apps/texed/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
### RetrOS-32 v0.0.1 Makefile ###
# This makefile only works in the
# RetrOS-32 Environment. It depends
# on static libraries.
##################################
ROOT = ../../

CCFLAGS=-m32 -O2 -Wall -Wextra -Wpedantic -Wstrict-aliasing \
-Wno-pointer-arith -Wno-unused-parameter -nostdlib \
-nostdinc -ffreestanding -fno-pie -fno-stack-protector \
-fno-builtin-function -fno-builtin -I $(ROOT)include/ -I $(ROOT)apps/
MAKEFLAGS += --no-print-directory
UNAME := $(shell uname)
ifeq ($(UNAME),Linux)
CC=gcc
CPP=g++
AS=as
LD=ld

CCFLAGS += -elf_i386
ASFLAGS += --32
LDFLAGS += -m elf_i386
else
CC=i386-elf-gcc
CPP=i386-elf-g++
AS=i386-elf-as
LD=i386-elf-ld
endif

#### ONLY EDIT THIS ####
OUTPUT = texed.o
LIBS = -lcore
EXTENDED =
########################

SRC_FILES = $(wildcard *.c)
OBJ_FILES = $(SRC_FILES:%.c=$(OUTPUTDIR)%.o)
OUTPUTDIR = ./bin/

all: $(OUTPUT) install

install: $(OUTPUT)
@cp $(OUTPUT) $(ROOT)rootfs/bin

$(OUTPUT): $(OBJ_FILES)
$(LD) -o $@ $(LDFLAGS) $^ -L$(ROOT)apps $(LIBS) $(EXTENDED) -T $(ROOT)apps/utils/linker.ld

$(OUTPUTDIR)%.o: %.c
@mkdir -p $(OUTPUTDIR)
$(CC) $(CCFLAGS) -c $< -o $@

clean:
rm -rf $(OUTPUTDIR)* *.o *.d $(OUTPUT) .depend
34 changes: 34 additions & 0 deletions apps/texed/Makefile.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
### Standard Unix Environment Makefile ###
# This makefile is adapted to compile with ncurses
# in a standard Unix-like environment.
##############################################
CC=gcc
CPP=g++
AS=as
LD=ld
UNAME := $(shell uname)
ifeq ($(UNAME),Linux)
CCFLAGS=-O2 -Wall -Wextra -Wpedantic -Iinclude/ -Iapps/
LDFLAGS=-lncurses
else
# For macOS, adjust as needed (e.g., clang, ncurses path)
CCFLAGS=-O2 -Iinclude/ -I/usr/local/opt/ncurses/include -DNCURSES
LDFLAGS=-L/usr/local/opt/ncurses/lib -lncurses
endif

#### ONLY EDIT THIS ####
OUTPUT = texed
SRC_FILES = $(wildcard *.c)
OBJ_FILES = $(SRC_FILES:%.c=%.o)
########################

all: $(OUTPUT)

$(OUTPUT): $(OBJ_FILES)
$(CC) -o $@ $^ $(LDFLAGS)

%.o: %.c
$(CC) $(CCFLAGS) -c $< -o $@

clean:
rm -f *.o $(OUTPUT)
90 changes: 90 additions & 0 deletions apps/texed/cmd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

#ifndef NCURSES
#include <libc.h>
#include <lib/syscall.h>
#else
#include <ncurses.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#endif // !NCURSES

#include "include/screen.h"
#include "include/textbuffer.h"

#define MAX_CMD 100

static int textbuffer_replace(struct textbuffer *buffer, char *search, char *replace) {
for (size_t i = 0; i < buffer->line_count; i++) {
int index;
while ((index = strstr(buffer->lines[i]->text, search)) >= 0) {
size_t replace_len = strlen(replace);
size_t search_len = strlen(search);
size_t new_length = buffer->lines[i]->length - search_len + replace_len;

if (new_length > buffer->lines[i]->capacity) {
return -1;
}

memmove(buffer->lines[i]->text + index + replace_len,
buffer->lines[i]->text + index + search_len,
buffer->lines[i]->length - index - search_len);
memcpy(buffer->lines[i]->text + index, replace, replace_len);

buffer->lines[i]->length = new_length;
}
}
return 0;
}

static int textbuffer_parse_replace(struct textbuffer *buffer, char *command) {
char search[MAX_CMD];
char replace[MAX_CMD];
int i = 0;
int j = 0;
int k = 0;
while(command[i] != ' ') {
i++;
}
i++;
while (command[i] != ' ') {
search[j] = command[i];
i++;
j++;
}
search[j] = '\0';
i++;
while (command[i] != '\0') {
replace[k] = command[i];
i++;
k++;
}
replace[k] = '\0';

textbuffer_replace(buffer, search, replace);
}

static int textbuffer_command(struct textbuffer* buffer, char* command) {
if (strcmp(command, "search") == 0) {
textbuffer_search_main(buffer);
}

if (strcmp(command, "exit") == 0) {
return -117;

}

// replace word with word without strtok
if (strncmp(command, "replace", 7) == 0) {
textbuffer_parse_replace(buffer, command);
}

return 0;
}


int textbuffer_command_main(struct textbuffer* buffer) {
textbuffer_get_input(buffer, 'C', "Command: ", textbuffer_command);
return 0;
}
75 changes: 75 additions & 0 deletions apps/texed/helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef NCURSES
#include <libc.h>
#include <lib/syscall.h>
#else
#include <ncurses.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#endif // !NCURSES

#include "include/screen.h"
#include "include/textbuffer.h"

#define MAX_INPUT 100

int textbuffer_get_input(struct textbuffer* buffer, char tag, char* message, int (*callback)(struct textbuffer*, char*))
{
char input[MAX_INPUT];
int input_len = 0;

int box_width = 50;
int box_height = 15;
int screen_width = 80;
int screen_height = 23;

int start_x = (screen_width - box_width) / 2;
int start_y = (screen_height - box_height) / 2;

for (int x = start_x; x < start_x + box_width; x++) {
for (int y = start_y; y < start_y + box_height; y++) {
screen_put_char(x, y, ' ', COLOR(VGA_COLOR_WHITE, VGA_COLOR_LIGHT_GREY));
}
}

screen_draw_box(start_x, start_y, box_width, box_height, COLOR(VGA_COLOR_WHITE, VGA_COLOR_LIGHT_GREY));


screen_put_char(start_x+3, start_y, '[', COLOR(VGA_COLOR_WHITE, VGA_COLOR_LIGHT_GREY));
screen_put_char(start_x+4, start_y, tag, COLOR(VGA_COLOR_GREEN, VGA_COLOR_LIGHT_GREY));
screen_put_char(start_x+5, start_y, ']', COLOR(VGA_COLOR_WHITE, VGA_COLOR_LIGHT_GREY));

screen_write(start_x+3, start_y+1, message, COLOR(VGA_COLOR_WHITE, VGA_COLOR_LIGHT_GREY));
screen_write(start_x+3, start_y+2, " ", COLOR(VGA_COLOR_WHITE, VGA_COLOR_BLUE));

screen_write(start_x+3, start_y+4, "Press [Enter] to input", COLOR(VGA_COLOR_WHITE, VGA_COLOR_LIGHT_GREY));

while(1) {
screen_set_cursor(start_x+3 + input_len, start_y+2);
screen_write(start_x+3, start_y+2, input, COLOR(VGA_COLOR_WHITE, VGA_COLOR_BLUE));
int c = screen_get_char();
if (c == 0)
continue;
if (c == CTRLC)
break;
if (c == BACKSPACE){
if (input_len > 0){
input_len--;
input[input_len] = 0;
}
continue;
}
if (c == ENTER){
return callback(buffer, input);
}

if(input_len >= MAX_INPUT) {
continue;
}

input[input_len] = c;
input_len++;
}
return -1;
}
Loading
Loading