From 3f79b7bd553a52fca7a098f5195b406ff9970491 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sun, 15 Jan 2023 01:13:51 +0100 Subject: add list and static library builder --- lib/Makefile | 28 ++++++++++++ lib/arg/Makefile | 3 ++ lib/arg/arg.c | 85 ++++++++++++++++++++++++++++++++++ lib/arg/arg.h | 9 ++++ lib/avl_tree/avl_tree.c | 34 ++++++-------- lib/config.mk | 3 ++ lib/io/io.c | 14 ++++++ lib/io/test | Bin 26504 -> 0 bytes lib/list/list.c | 98 ++++++++++++++++++++++++++++++++++++++++ lib/list/list.h | 26 +++++++++++ lib/objects/aec/aec.o | Bin 0 -> 15552 bytes lib/objects/arg/arg.o | Bin 0 -> 19176 bytes lib/objects/avl_tree/avl_tree.o | Bin 0 -> 11240 bytes lib/objects/cstr/cstr.o | Bin 0 -> 8208 bytes lib/objects/env/env.o | Bin 0 -> 5208 bytes lib/objects/io/io.o | Bin 0 -> 16936 bytes lib/objects/list/list.o | Bin 0 -> 6424 bytes lib/objects/malloc/malloc.o | Bin 0 -> 15648 bytes lib/objects/sys/start.o | Bin 0 -> 2424 bytes lib/slib.a | Bin 0 -> 102482 bytes lib/sys/getdents.h | 18 ++++++++ lib/sys/io.h | 16 +++++-- lib/sys/open.h | 2 + 23 files changed, 314 insertions(+), 22 deletions(-) create mode 100644 lib/Makefile create mode 100644 lib/arg/Makefile create mode 100644 lib/arg/arg.c create mode 100644 lib/arg/arg.h create mode 100644 lib/config.mk delete mode 100755 lib/io/test create mode 100644 lib/list/list.c create mode 100644 lib/list/list.h create mode 100644 lib/objects/aec/aec.o create mode 100644 lib/objects/arg/arg.o create mode 100644 lib/objects/avl_tree/avl_tree.o create mode 100644 lib/objects/cstr/cstr.o create mode 100644 lib/objects/env/env.o create mode 100644 lib/objects/io/io.o create mode 100644 lib/objects/list/list.o create mode 100644 lib/objects/malloc/malloc.o create mode 100644 lib/objects/sys/start.o create mode 100644 lib/slib.a create mode 100644 lib/sys/getdents.h (limited to 'lib') diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..1e8c5b7 --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,28 @@ +C_SRC=$(shell find . -name "*.c") +ASM_SRC=$(shell find . -name "*.S") +C_OBJ=$(C_SRC:./%.c=objects/%.o) +ASM_OBJ=$(ASM_SRC:./%.S=objects/%.o) +SRC=${C_SRC} ${ASM_SRC} +OBJ=${C_OBJ} ${ASM_OBJ} + +include config.mk + +default: slib.a + +options: + @echo SRC: ${SRC} + @echo OBJ: ${OBJ} + +objects/%.o: %.c + -mkdir -p "$$(dirname $@)" + gcc -c $< -o $@ ${CFLAGS} + +objects/%.o: %.S + -mkdir -p "$$(dirname $@)" + gcc -c $< -o $@ ${CFLAGS} + +slib.a: ${OBJ} + ar rcs slib.a ${OBJ} + +clean: + rm -r objects slib.a diff --git a/lib/arg/Makefile b/lib/arg/Makefile new file mode 100644 index 0000000..384f797 --- /dev/null +++ b/lib/arg/Makefile @@ -0,0 +1,3 @@ +unit_test: + gcc test.c arg.c ../io/io.c ../aec/aec.c ../malloc/malloc.c ../avl_tree/avl_tree.c ../cstr/cstr.c ../env/env.c ../sys/start.S -o test -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -g + ./test diff --git a/lib/arg/arg.c b/lib/arg/arg.c new file mode 100644 index 0000000..271b535 --- /dev/null +++ b/lib/arg/arg.c @@ -0,0 +1,85 @@ +#include "arg.h" + +#include "../sys/sizes.h" +#include "../cstr/cstr.h" + +#include "../avl_tree/avl_tree.h" +#include "../malloc/malloc.h" +#include "../sys/exit.h" +#include "../io/io.h" +#include "../aec/aec.h" + +typedef struct { + const char *name; + union { + void (*flag_func)(); + void (*arg_func)(const char *); + }; +} arg_func_t; + +avl_tree_t *flags = 0; +avl_tree_t *args = 0; +void (*default_arg)(const char *) = 0; + + +i8 __arg_func_compare(void *a, void *b) { + return cstr_compare(((arg_func_t *)a)->name, ((arg_func_t *)b)->name); +} + + +void arg_parse_arg(int argc, const char **argv) +{ + arg_func_t *func; + arg_func_t q; + for (int i = 1; i < argc; ++i) { + q.name = argv[i]; + if (args && + i < argc - 1 && + argv[i + 1][0] != '-' && + (func = avl_tree_search(args, &q)) != 0) { + func->arg_func(argv[i + 1]); + ++i; + } else if (flags && (func = avl_tree_search(flags, &q)) != 0) { + func->flag_func(); + + } else if (argv[i][0] != '-' && default_arg) { + default_arg(argv[i]); + + } else { + wstdf("%S%Ferror:%S unknown flag '%s'\n", SGR_BOLD, COLOR_RED, SGR_RESET, argv[i]); + exit(-1); + } + } +} + + +void arg_register_arg(const char *flag, void(*func)(const char *)) +{ + if (!args) { + args = new_avl_tree(&__arg_func_compare, 1); + } + + arg_func_t *f = malloc(sizeof(arg_func_t)); + f->name = flag; + f->arg_func = func; + avl_tree_insert(args, f); +} + + +void arg_register_flag(const char *flag, void(*func)(const char *)) +{ + if (!flags) { + flags = new_avl_tree(&__arg_func_compare, 1); + } + + arg_func_t *f = malloc(sizeof(arg_func_t)); + f->name = flag; + f->flag_func = func; + avl_tree_insert(flags, f); +} + + +void arg_register_default(void (*func)(const char *)) +{ + default_arg = func; +} diff --git a/lib/arg/arg.h b/lib/arg/arg.h new file mode 100644 index 0000000..5bffb33 --- /dev/null +++ b/lib/arg/arg.h @@ -0,0 +1,9 @@ +#ifndef ARG_H +#define ARG_H + +void arg_parse_arg(int argc, const char **argv); +void arg_register_flag(const char *flag, void(*func)()); +void arg_register_arg(const char *flag, void(*func)(const char *)); +void arg_register_default(void (*func)(const char *)); + +#endif diff --git a/lib/avl_tree/avl_tree.c b/lib/avl_tree/avl_tree.c index db3e44e..1e901d1 100644 --- a/lib/avl_tree/avl_tree.c +++ b/lib/avl_tree/avl_tree.c @@ -63,22 +63,18 @@ void *avl_tree_search(avl_tree_t *tree, void *value) return 0; avl_tree_node_t *current = tree->root; - u8 factor = tree->compare(current->value, value); + i8 factor = 0; - while (current->left || current->right) { + while (current) { factor = tree->compare(current->value, value); switch (factor) { - case (u8)1: current = current->right; break; - case (u8)0: return current->value; break; - case (u8)-1: current = current->left; break; + case 1: current = current->right; break; + case 0: return current->value; break; + case -1: current = current->left; break; } } - factor = tree->compare(current->value, value); - if (!factor) - return current->value; - return 0; } @@ -99,7 +95,7 @@ void avl_tree_insert(avl_tree_t *tree, void *value) { avl_tree_node_t *current = tree->root; avl_tree_node_t *last = current; - u8 factor = 0; + i8 factor = 0; if (current) factor = tree->compare(current->value, value); @@ -110,13 +106,13 @@ void avl_tree_insert(avl_tree_t *tree, void *value) last = current; switch (factor) { - case (u8)1: current = current->right; break; - case (u8)0: + case 1: current = current->right; break; + case 0: free(current->value); current->value = value; current = 0; break; - case (u8)-1: current = current->left; break; + case -1: current = current->left; break; } } @@ -125,8 +121,8 @@ void avl_tree_insert(avl_tree_t *tree, void *value) current = __new_avl_tree_node(last, value); switch (factor) { - case (u8)1: last->right = current; break; - case (u8)-1: last->left = current; break; + case 1: last->right = current; break; + case -1: last->left = current; break; } } else if (!last) { ++tree->size; @@ -159,7 +155,7 @@ void avl_tree_remove(avl_tree_t *tree, void *value) { avl_tree_node_t *current = tree->root; avl_tree_node_t *last = current; - u8 factor = 0; + i8 factor = 0; if (current) factor = tree->compare(current->value, value); @@ -170,9 +166,9 @@ void avl_tree_remove(avl_tree_t *tree, void *value) last = current; switch (factor) { - case (u8)1: current = current->right; break; - case (u8)0: current = 0; break; - case (u8)-1: current = current->left; break; + case 1: current = current->right; break; + case 0: current = 0; break; + case -1: current = current->left; break; } } diff --git a/lib/config.mk b/lib/config.mk new file mode 100644 index 0000000..642bc35 --- /dev/null +++ b/lib/config.mk @@ -0,0 +1,3 @@ +VERSION=0.0.1 +CFLAGS=-fno-stack-protector -Wno-implicit-function-declaration -fno-builtin -g +CC=cc diff --git a/lib/io/io.c b/lib/io/io.c index 3b0ea96..43e05b4 100644 --- a/lib/io/io.c +++ b/lib/io/io.c @@ -23,6 +23,8 @@ void rstd(char *buf, unsigned long count) void wstdf__(const char *buf, const void **args) { const char *start = buf; + char stoi_buf[32] = ""; + int i; for (; *buf; ++buf) { if (*buf == '%') { if (buf - start > 0) @@ -36,6 +38,18 @@ void wstdf__(const char *buf, const void **args) __wstdn(((const char **)args)[0], cstr_length(((const char**)args)[0])); ++args; break; + case 'i': + for (i = 0; i < 32; ++i) + stoi_buf[i] = 0; + i64_to_cstr(((u64 *)args)[0], stoi_buf, 32); + __wstdn(stoi_buf, cstr_length(stoi_buf)); + break; + case 'u': + for (i = 0; i < 32; ++i) + stoi_buf[i] = 0; + u64_to_cstr(((u64 *)args)[0], stoi_buf, 32); + __wstdn(stoi_buf, cstr_length(stoi_buf)); + break; case 'S': sgr(((const char **)args)[0]); ++args; diff --git a/lib/io/test b/lib/io/test deleted file mode 100755 index b2a3047..0000000 Binary files a/lib/io/test and /dev/null differ diff --git a/lib/list/list.c b/lib/list/list.c new file mode 100644 index 0000000..753fdb1 --- /dev/null +++ b/lib/list/list.c @@ -0,0 +1,98 @@ +#include "list.h" +#include "../malloc/malloc.h" + + +list_node_t *__new_list_node(void *value); + + +list_t *new_list() +{ + list_t *list = malloc(sizeof(list_t)); + list->first = 0; + list->last = 0; + list->size = 0; + return list; +} + + +list_t *from_array(void **array, u64 size) +{ + int i; + list_t *list = new_list(); + list->size = size; + + for (i = 0; i < size; ++i) + list_append(list, array[i]); + + return list; +} + + +void list_append(list_t *list, void *value) +{ + list_node_t *node = __new_list_node(value); + + if (list->last == 0) { + list->first = node; + list->last = list->first; + } else { + list->last->next = node; + node->previous = list->last; + list->last = node; + } + + ++list->size; +} + + +void list_prepend(list_t *list, void *value) +{ + list_node_t *node = __new_list_node(value); + + if (list->last == 0) { + list->first = node; + list->last = list->first; + } else { + list->first->previous = node; + node->next = list->first; + list->first = node; + } + + ++list->size; +} + + +void *pop_first(list_t *list) +{ + void *value = list->first->value; + list_node_t *node = list->first; + + list->first = node->next; + free(node); + + --list->size; + return value; +} + + +void *pop_last(list_t *list) +{ + void *value = list->last->value; + list_node_t *node = list->last; + + list->last = node->previous; + free(node); + + --list->size; + return value; +} + + +list_node_t *__new_list_node(void *value) +{ + list_node_t *node = malloc(sizeof(list_node_t)); + node->value = value; + node->next = 0; + node->previous = 0; + return node; +} diff --git a/lib/list/list.h b/lib/list/list.h new file mode 100644 index 0000000..9acecac --- /dev/null +++ b/lib/list/list.h @@ -0,0 +1,26 @@ +#ifndef LIST_H +#define LIST_H + +#include "../sys/sizes.h" + +typedef struct list_node_t__ { + struct list_node_t__ *next; + struct list_node_t__ *previous; + void *value; +} list_node_t; + +typedef struct { + list_node_t *first; + list_node_t *last; + u64 size; +} list_t; + +list_t *new_list(); +list_t *from_array(void **array, u64 size); +void list_append(list_t *list, void *value); +void list_prepend(list_t *list, void *value); +void *pop_first(list_t *list); +void *pop_last(list_t *list); +void free_list(); + +#endif diff --git a/lib/objects/aec/aec.o b/lib/objects/aec/aec.o new file mode 100644 index 0000000..140ff1e Binary files /dev/null and b/lib/objects/aec/aec.o differ diff --git a/lib/objects/arg/arg.o b/lib/objects/arg/arg.o new file mode 100644 index 0000000..cd32ac8 Binary files /dev/null and b/lib/objects/arg/arg.o differ diff --git a/lib/objects/avl_tree/avl_tree.o b/lib/objects/avl_tree/avl_tree.o new file mode 100644 index 0000000..26c68bb Binary files /dev/null and b/lib/objects/avl_tree/avl_tree.o differ diff --git a/lib/objects/cstr/cstr.o b/lib/objects/cstr/cstr.o new file mode 100644 index 0000000..ee94098 Binary files /dev/null and b/lib/objects/cstr/cstr.o differ diff --git a/lib/objects/env/env.o b/lib/objects/env/env.o new file mode 100644 index 0000000..fbe41b0 Binary files /dev/null and b/lib/objects/env/env.o differ diff --git a/lib/objects/io/io.o b/lib/objects/io/io.o new file mode 100644 index 0000000..a7d55ad Binary files /dev/null and b/lib/objects/io/io.o differ diff --git a/lib/objects/list/list.o b/lib/objects/list/list.o new file mode 100644 index 0000000..7ec2d6b Binary files /dev/null and b/lib/objects/list/list.o differ diff --git a/lib/objects/malloc/malloc.o b/lib/objects/malloc/malloc.o new file mode 100644 index 0000000..ad940d6 Binary files /dev/null and b/lib/objects/malloc/malloc.o differ diff --git a/lib/objects/sys/start.o b/lib/objects/sys/start.o new file mode 100644 index 0000000..f9ede7d Binary files /dev/null and b/lib/objects/sys/start.o differ diff --git a/lib/slib.a b/lib/slib.a new file mode 100644 index 0000000..17f8a97 Binary files /dev/null and b/lib/slib.a differ diff --git a/lib/sys/getdents.h b/lib/sys/getdents.h new file mode 100644 index 0000000..b5a0c74 --- /dev/null +++ b/lib/sys/getdents.h @@ -0,0 +1,18 @@ +#ifndef GETDENTS_H +#define GETDENTS_H + +#include "syscalls.h" + +struct dirent { + unsigned long d_ino; + unsigned long d_off; + unsigned short d_reclen; + char d_name[]; +}; + +static int getdents(unsigned int fd, char *buf, unsigned int count) +{ + return syscall(GETDENTS, fd, buf, count); +} + +#endif diff --git a/lib/sys/io.h b/lib/sys/io.h index 4222c4d..c518fcb 100644 --- a/lib/sys/io.h +++ b/lib/sys/io.h @@ -10,8 +10,18 @@ #define STDOUT_FD 1 #define STDERR_FD 2 -#define FILE_READ_ONLY 0 -#define FILE_WRITE_ONLY 1 -#define FILE_READ_WRITE 2 +#define OPEN_READ_ONLY 0 +#define OPEN_WRITE_ONLY 1 +#define OPEN_READ_WRITE 2 +#define OPEN_DIRECTORY 0200000 + +#define TYPE_UNKNOWN 0 +#define TYPE_FIFO 1 +#define TYPE_CHAR_DEVICE 2 +#define TYPE_DIRECTORY 4 +#define TYPE_BLOCK_DEVICE 6 +#define TYPE_REGULAR_FILE 8 +#define TYPE_SYM_LINK 10 +#define TYPE_SOCKET 12 #endif diff --git a/lib/sys/open.h b/lib/sys/open.h index 5aeab6d..e01da63 100644 --- a/lib/sys/open.h +++ b/lib/sys/open.h @@ -3,6 +3,8 @@ #include "syscalls.h" + + static int open(const char *filename, int flags, int mode) { return syscall(OPEN, filename, flags, mode); -- cgit v1.2.3-70-g09d2