From faa69a40b8c5d124885502aa8cd616c487e24cb5 Mon Sep 17 00:00:00 2001
From: Anonymus Raccoon
Date: Wed, 6 May 2020 14:15:41 +0200
Subject: [PATCH] Creating a self insert function
---
Makefile | 8 +++--
include/key_functions.h | 20 ++++++++++++
include/shell.h | 16 ++++++----
src/key_bindings/basic_typing_functions.c | 39 +++++++++++++++++++++++
src/key_bindings/default_bindings.c | 19 +++++++++++
src/shell.c | 16 ++++++----
6 files changed, 102 insertions(+), 16 deletions(-)
create mode 100644 include/key_functions.h
create mode 100644 src/key_bindings/basic_typing_functions.c
create mode 100644 src/key_bindings/default_bindings.c
diff --git a/Makefile b/Makefile
index d05eeeb..9f3e0f7 100644
--- a/Makefile
+++ b/Makefile
@@ -28,7 +28,9 @@ SRC = src/shell.c \
src/utility/split_str.c \
src/utility/fusion.c \
src/utility/split_commands.c \
- src/utility/get_return.c
+ src/utility/get_return.c \
+ src/key_bindings/basic_typing_functions.c \
+ src/key_bindings/default_bindings.c
OBJ = $(SRC:%.c=%.o)
OBJ += src/main.o
@@ -48,9 +50,9 @@ CC = gcc
INCLUDE = -I ./include
-CFLAGS = $(INCLUDE) -Wall -Wextra -Wshadow
+CFLAGS = $(INCLUDE) -Wall -Wextra -Wshadow -Wno-unused-parameter
-LDFLAGS =
+LDFLAGS = -lncurses
all: $(NAME)
diff --git a/include/key_functions.h b/include/key_functions.h
new file mode 100644
index 0000000..4edd969
--- /dev/null
+++ b/include/key_functions.h
@@ -0,0 +1,20 @@
+/*
+** EPITECH PROJECT, 2020
+** ash
+** File description:
+** bindings
+*/
+
+#include "shell.h"
+
+#pragma once
+
+typedef struct key_function
+{
+ const char *name;
+ int (*run)(int key, buffer_t *command_buffer, env_t *env);
+} key_function_t;
+
+extern const key_function_t key_functions[];
+
+int self_insert_command(int key, buffer_t *command_buffer, env_t *env);
\ No newline at end of file
diff --git a/include/shell.h b/include/shell.h
index c5bdbd9..bf40ba4 100644
--- a/include/shell.h
+++ b/include/shell.h
@@ -6,6 +6,7 @@
*/
typedef struct redirection redirection;
+typedef struct env_s env_t;
#pragma once
#include
@@ -20,16 +21,17 @@ typedef struct history_s
struct history_s *next;
} history_t;
-typedef struct key_function
+typedef struct buffer
{
- const char *name;
- int (*run)(int key, char *command_buffer, env_t *env);
-} key_function_t;
+ char *buffer;
+ int size;
+ int pos;
+} buffer_t;
typedef struct binding
{
int key;
- const key_function_t *func;
+ int (*func)(int key, buffer_t *command_buffer, env_t *env);
} binding_t;
typedef struct env_s
@@ -63,4 +65,6 @@ bool envvar_is_valid(const char *str);
#define INVALID_ENV_VAR \
"setenv: Variable name must contain alphanumeric characters.\n"
-#define ERROR 84
\ No newline at end of file
+#define ERROR 84
+
+extern const binding_t emacs_bindings[];
diff --git a/src/key_bindings/basic_typing_functions.c b/src/key_bindings/basic_typing_functions.c
new file mode 100644
index 0000000..7437fb9
--- /dev/null
+++ b/src/key_bindings/basic_typing_functions.c
@@ -0,0 +1,39 @@
+/*
+** EPITECH PROJECT, 2020
+** ash
+** File description:
+** basic_typing_functions
+*/
+
+#include "shell.h"
+#include "key_functions.h"
+#include
+#include
+#include
+#include
+
+int self_insert_command(int key, buffer_t *buffer, env_t *env)
+{
+ int len;
+ const char *chars = unctrl(key);
+ const int charlens = strlen(chars);
+
+ if (buffer->buffer == NULL) {
+ buffer->buffer = calloc(sizeof(char *), 100);
+ buffer->size = 100;
+ }
+ if (!buffer->buffer)
+ return (-1);
+ len = strlen(buffer->buffer) + charlens;
+ if (len > buffer->size) {
+ buffer->buffer = realloc(buffer->buffer, buffer->size + 100);
+ buffer->size += 100;
+ }
+ if (!buffer->buffer)
+ return (-1);
+ for (int i = len - 1; i >= buffer->pos; i--)
+ buffer->buffer[i] = buffer->buffer[i - charlens];
+ strcpy(buffer->buffer + buffer->pos, chars);
+ buffer->pos++;
+ return (0);
+}
\ No newline at end of file
diff --git a/src/key_bindings/default_bindings.c b/src/key_bindings/default_bindings.c
new file mode 100644
index 0000000..0a95255
--- /dev/null
+++ b/src/key_bindings/default_bindings.c
@@ -0,0 +1,19 @@
+/*
+** EPITECH PROJECT, 2020
+** ash
+** File description:
+** default_bindings
+*/
+
+#include "shell.h"
+#include "key_functions.h"
+#include
+
+const key_function_t key_functions[] = {
+ {"self-insert-command", &self_insert_command},
+ {NULL, NULL}
+};
+
+const binding_t emacs_bindings[] = {
+ {0, NULL}
+};
\ No newline at end of file
diff --git a/src/shell.c b/src/shell.c
index d363d7d..2f5e3ae 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -5,11 +5,12 @@
** shell
*/
+#include "shell.h"
+#include "builtin.h"
+#include "key_functions.h"
#include
#include
#include
-#include "shell.h"
-#include "builtin.h"
#include
#include
#include
@@ -39,27 +40,28 @@ void test()
// free(cmd);
}
-int process_key(int key, char *buffer, env_t *env)
+int process_key(int key, buffer_t *buffer, env_t *env)
{
for (int i = 0; env->bindings[i].func; i++)
if (key == env->bindings[i].key)
- return (env->bindings[i].func->run(key, buffer, env));
- return (0);
+ return (env->bindings[i].func(key, buffer, env));
+ return (self_insert_command(key, buffer, env));
}
void start_shell(env_t *env)
{
WINDOW *window = initscr();
int key;
- char *buffer = calloc(100, sizeof(char));
+ buffer_t buffer = {.size = 0, .buffer = NULL, .pos = 0};
noecho();
prompt_prepare(env);
do {
+ printf("%s \n", buffer.buffer);
key = getch();
if (key == ERR)
break;
- } while (process_key(key, buffer, env) > 0);
+ } while (process_key(key, &buffer, env) >= 0);
delwin(window);
endwin();
}
\ No newline at end of file