diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-01-17 22:49:31 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-01-17 22:49:31 +0100 |
| commit | 1a66f5d069e4ccb600186918f716323aa7bae052 (patch) | |
| tree | 9010251014a766f29d1c2fb6c30c9abdb1766962 | |
| parent | b2146cd4d6e6f5fda36d59c054e0634a2098a03c (diff) | |
add tctl and list
| -rw-r--r-- | core/Makefile | 2 | ||||
| -rw-r--r-- | core/read.c | 96 | ||||
| -rw-r--r-- | core/tctl.c | 44 | ||||
| -rw-r--r-- | lib/list/list.c | 14 | ||||
| -rw-r--r-- | lib/list/list.h | 2 | ||||
| -rw-r--r-- | lib/sys/ioctl.h | 11 | ||||
| -rw-r--r-- | lib/tctl/tctl.c | 18 | ||||
| -rw-r--r-- | lib/tctl/tctl.h | 14 | ||||
| -rw-r--r-- | smash/Makefile | 10 |
9 files changed, 204 insertions, 7 deletions
diff --git a/core/Makefile b/core/Makefile index 0dc6d12..d8335b0 100644 --- a/core/Makefile +++ b/core/Makefile @@ -9,7 +9,7 @@ options: objects/%: %.c -mkdir -p $$(dirname $@) - gcc $< -o $@ ../lib/slib.a -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -fno-builtin -g + gcc $< -o $@ ../lib/slib.a -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -fno-builtin -Wno-int-conversion -g clean: rm ${OBJ} diff --git a/core/read.c b/core/read.c new file mode 100644 index 0000000..82b74be --- /dev/null +++ b/core/read.c @@ -0,0 +1,96 @@ +#include "../lib/io/io.h" +#include "../lib/arg/arg.h" +#include "../lib/list/list.h" + +#define BUF_SIZE 1024 + +void mode_all(int fd); +void mode_singleline(int fd); + +enum Modes { + MODE_ALL, + MODE_SINGLELINE, + MODE_SIZE +} mode; + +char buf[BUF_SIZE] = ""; +list_t *files; +void(*mode_funcs[MODE_SIZE])(int fd) = { + mode_all, + mode_singleline +}; + +void add_file(const char *path) +{ + list_append(files, (void*)path); +} + + +void add_stdin() +{ + list_append(files, 0); +} + + +void set_singleline_mode() +{ + mode = MODE_SINGLELINE; +} + + +void mode_all(int fd) +{ + u64 rs; + while ((rs = read(fd, buf, BUF_SIZE - 1))) { + buf[rs] = 0; + wstd(buf); + } +} + + +void mode_singleline(int fd) +{ + u64 rs; + u64 has_newline_char = 0; + char *p; + while (!has_newline_char) { + rs = read(fd, buf, BUF_SIZE - 1); + for (p = buf; p < buf + rs; ++p) { + if (*p == '\n') { + has_newline_char = 1; + *(p + 1) = 0; + break; + } + } + wstd(buf); + } +} + + +int main(int argc, const char **argv) +{ + list_node_t *file; + int fd; + + files = new_list(); + + arg_register_default(&add_file); + arg_register_flag("-", &add_stdin); + arg_register_flag("-l", &set_singleline_mode); + arg_parse_arg(argc, argv); + + if (files->size == 0) { + list_append(files, 0); + } + + for (file = files->first; file; file = file->next) { + if (file->value) + fd = open(file->value, OPEN_READ_ONLY, 0); + else + fd = STDIN_FD; + + mode_funcs[mode](fd); + } + + free_list(files); +} diff --git a/core/tctl.c b/core/tctl.c new file mode 100644 index 0000000..e57c7cd --- /dev/null +++ b/core/tctl.c @@ -0,0 +1,44 @@ +#include "../lib/io/io.h" +#include "../lib/tctl/tctl.h" +#include "../lib/arg/arg.h" +#include "../lib/cstr/cstr.h" + +window_size_t size; + +void set_height(const char *h) +{ + size.height = cstr_to_u64(h); +} + + +void set_width(const char *h) +{ + size.width = cstr_to_u64(h); +} + + +int main(int argc, const char **argv) +{ + size.width = 0; + size.height = 0; + + arg_register_arg("-w", &set_width); + arg_register_arg("-h", &set_height); + arg_parse_arg(argc, argv); + + if (size.width == 0 && size.height == 0) { + size = tctl_get_window_size(); + wstdf("width: %u, height: %u\n", size.width, size.height); + } else { + window_size_t new_size = tctl_get_window_size(); + + if (size.width != 0) + new_size.width = size.width; + if (size.height != 0) + new_size.height = size.height; + + tctl_set_window_size(new_size); + } + + return 0; +} diff --git a/lib/list/list.c b/lib/list/list.c index 753fdb1..8064da4 100644 --- a/lib/list/list.c +++ b/lib/list/list.c @@ -28,6 +28,20 @@ list_t *from_array(void **array, u64 size) } +void free_list(list_t *list) +{ + list_node_t *node = list->first; + list_node_t *next; + free(list); + + while (node) { + next = node->next; + free(node); + node = next; + } +} + + void list_append(list_t *list, void *value) { list_node_t *node = __new_list_node(value); diff --git a/lib/list/list.h b/lib/list/list.h index 9acecac..2021b2a 100644 --- a/lib/list/list.h +++ b/lib/list/list.h @@ -21,6 +21,6 @@ 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(); +void free_list(list_t *list); #endif diff --git a/lib/sys/ioctl.h b/lib/sys/ioctl.h new file mode 100644 index 0000000..4bf73f7 --- /dev/null +++ b/lib/sys/ioctl.h @@ -0,0 +1,11 @@ +#ifndef SYS_IOCTL_H +#define SYS_IOCTL_H + +#include "syscalls.h" + +int ioctl(unsigned int fd, unsigned int cmd, void *arg) +{ + return syscall(IOCTL, fd, cmd, arg); +} + +#endif diff --git a/lib/tctl/tctl.c b/lib/tctl/tctl.c new file mode 100644 index 0000000..54034f4 --- /dev/null +++ b/lib/tctl/tctl.c @@ -0,0 +1,18 @@ +#include "tctl.h" +#include "../sys/ioctl.h" + +#define GET_WIN_SIZE 0x5413 +#define SET_WIN_SIZE 0x5414 + +window_size_t tctl_get_window_size() +{ + window_size_t ws; + ioctl(0, GET_WIN_SIZE, &ws); + return ws; +} + + +void tctl_set_window_size(window_size_t size) +{ + ioctl(0, SET_WIN_SIZE, &size); +} diff --git a/lib/tctl/tctl.h b/lib/tctl/tctl.h new file mode 100644 index 0000000..d106818 --- /dev/null +++ b/lib/tctl/tctl.h @@ -0,0 +1,14 @@ +#ifndef TCTL_H +#define TCTL_H + +typedef struct { + unsigned short int height; + unsigned short int width; + unsigned short int ws_xpixel; + unsigned short int ws_ypixel; +} window_size_t; + +window_size_t tctl_get_window_size(); +void tctl_set_window_size(window_size_t size); + +#endif diff --git a/smash/Makefile b/smash/Makefile index 9612edf..8bae278 100644 --- a/smash/Makefile +++ b/smash/Makefile @@ -1,20 +1,20 @@ default_target: all unit_test_parser: - gcc parser.c ../lib/cstr/cstr.c ../lib/malloc/malloc.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o test -DPARSER_UNIT_TEST -g + gcc parser.c ../lib/slib.a -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o test -DPARSER_UNIT_TEST -g ./test unit_test_variables: - gcc variables.c ../lib/avl_tree/avl_tree.c ../lib/cstr/cstr.c ../lib/malloc/malloc.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -o test -DVARIABLES_UNIT_TEST -g + gcc variables.c ../lib/slib.a -static -nostdlib -fno-stack-protector -o test -DVARIABLES_UNIT_TEST -g ./test unit_test_builtin: - gcc builtin.c ../lib/cstr/cstr.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o test -DBUILTIN_UNIT_TEST -g + gcc builtin.c ../lib/slib.a -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o test -DBUILTIN_UNIT_TEST -g ./test unit_test_exec: - gcc exec.c builtin.c parser.c ../lib/cstr/cstr.c ../lib/io/io.c ../lib/env/env.c ../lib/malloc/malloc.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o test -DEXEC_UNIT_TEST -g + gcc exec.c builtin.c parser.c ../lib/slib.a -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -o test -DEXEC_UNIT_TEST -g ./test all: - gcc main.c exec.c parser.c builtin.c ../lib/env/env.c ../lib/io/io.c ../lib/avl_tree/avl_tree.c ../lib/cstr/cstr.c ../lib/malloc/malloc.c ../lib/sys/start.S -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -fno-builtin -o smash -DVARIABLES_UNIT_TEST -g + gcc main.c exec.c parser.c builtin.c ../lib/slib.a -static -nostdlib -fno-stack-protector -Wno-implicit-function-declaration -fno-builtin -o smash -DVARIABLES_UNIT_TEST -g |