From 37fb05269485370afad1e1dc799aa933d966da7c Mon Sep 17 00:00:00 2001
From: Anonymus Raccoon
Date: Wed, 6 May 2020 20:50:14 +0200
Subject: [PATCH] Handling ctrl d as eof
---
Makefile | 1 +
include/key_functions.h | 5 ++++-
src/execute.c | 2 +-
src/key_bindings/control_commands.c | 13 +++++++++++++
src/key_bindings/default_bindings.c | 2 ++
src/redirections/pty_pipe.c | 9 +++++++--
6 files changed, 28 insertions(+), 4 deletions(-)
create mode 100644 src/key_bindings/control_commands.c
diff --git a/Makefile b/Makefile
index f23177c..ac8a6b2 100644
--- a/Makefile
+++ b/Makefile
@@ -31,6 +31,7 @@ SRC = src/shell.c \
src/utility/get_return.c \
src/key_bindings/basic_typing_functions.c \
src/key_bindings/default_bindings.c \
+ src/key_bindings/control_commands.c \
src/redirections/pty_pipe.c
OBJ = $(SRC:%.c=%.o)
diff --git a/include/key_functions.h b/include/key_functions.h
index 2b9c255..c994760 100644
--- a/include/key_functions.h
+++ b/include/key_functions.h
@@ -9,6 +9,8 @@
#include "shell.h"
+#define CTRL(c) ((c) & 0x1F)
+
typedef struct key_function
{
const char *name;
@@ -25,4 +27,5 @@ extern const key_function_t key_functions[];
extern const binding_t emacs_bindings[];
int self_insert_command(int key, buffer_t *buffer, env_t *env);
-int newline_command(int key, buffer_t *buffer, env_t *env);
\ No newline at end of file
+int newline_command(int key, buffer_t *buffer, env_t *env);
+int eof_command(int key, buffer_t *buffer, env_t *env);
\ No newline at end of file
diff --git a/src/execute.c b/src/execute.c
index a23bfa2..1ff9039 100644
--- a/src/execute.c
+++ b/src/execute.c
@@ -107,8 +107,8 @@ void run_cmd(char **argv, redirection *inout[2], env_t *env)
path = eval(argv[0], argv, env);
return (exec_error(path, argv[0]));
}
+ waitpid(pid, &status, 0);
if (handle_parent_inout(inout, env, false))
return;
- waitpid(pid, &status, 0);
handle_signal(status, env);
}
\ No newline at end of file
diff --git a/src/key_bindings/control_commands.c b/src/key_bindings/control_commands.c
new file mode 100644
index 0000000..9a6095d
--- /dev/null
+++ b/src/key_bindings/control_commands.c
@@ -0,0 +1,13 @@
+/*
+** EPITECH PROJECT, 2020
+** ash
+** File description:
+** control_commands
+*/
+
+#include "shell.h"
+
+int eof_command(int key, buffer_t *buffer, env_t *env)
+{
+ return (-1);
+}
\ No newline at end of file
diff --git a/src/key_bindings/default_bindings.c b/src/key_bindings/default_bindings.c
index 76b4deb..3b90897 100644
--- a/src/key_bindings/default_bindings.c
+++ b/src/key_bindings/default_bindings.c
@@ -13,10 +13,12 @@
const key_function_t key_functions[] = {
{"self-insert-command", &self_insert_command},
{"newline", &newline_command},
+ {"enf-of-file", &eof_command},
{NULL, NULL}
};
const binding_t emacs_bindings[] = {
{'\n', &newline_command},
+ {CTRL('d'), &eof_command},
{0, NULL}
};
\ No newline at end of file
diff --git a/src/redirections/pty_pipe.c b/src/redirections/pty_pipe.c
index b413d02..7009848 100644
--- a/src/redirections/pty_pipe.c
+++ b/src/redirections/pty_pipe.c
@@ -84,8 +84,13 @@ void pty_get_output(redirection *pty, env_t *env)
FILE *file = fdopen(pty->extra_data, "r");
int y = getcury(env->window);
- while (getline(&line, &size, file) > 0)
- mvaddstr(y++, 0, line);
+ close(pty->fd);
+ while (getline(&line, &size, file) > 0) {
+ dprintf(2, "Writing at %d: %s\n", y, line);
+ mvaddstr(y, 0, line);
+ y++;
+ }
if (line)
free(line);
+ fclose(file);
}
\ No newline at end of file