Merge branch 'master' of github.com:AnonymusRaccoon/ash into prompt

This commit is contained in:
Melefo
2020-05-24 19:04:29 +02:00
12 changed files with 112 additions and 18 deletions
+2 -1
View File
@@ -54,7 +54,8 @@ SRC = src/shell.c \
src/key_bindings/autocompletion.c \
src/builtin/builtin_bindkey.c \
src/key_bindings/other_bindings.c \
src/builtin/builtin_vars.c
src/builtin/builtin_vars.c \
src/key_bindings/signals.c
OBJ = $(SRC:%.c=%.o)
OBJ += src/main.o
+3 -1
View File
@@ -46,4 +46,6 @@ int down_history_command(int key, buffer_t *buffer, env_t *env);
int quoted_insert_command(int key, buffer_t *buffer, env_t *env);
int clear_screen_command(int key, buffer_t *buffer, env_t *env);
int complete_command(int key, buffer_t *buffer, env_t *env);
int complete_command(int key, buffer_t *buffer, env_t *env);
int tty_sigintr_command(int key, buffer_t *buffer, env_t *env);
+4 -1
View File
@@ -21,6 +21,7 @@
#define KEY_DC CSI(0x7e33)
#include <termios.h>
#include <stdio.h>
typedef struct
{
@@ -80,4 +81,6 @@ my_addstr(window, str))
#define my_clrtobot() (printf("\x1B[J"))
void my_npause(my_window *window);
void my_nresume(my_window *window);
void my_nresume(my_window *window);
extern my_window *stdwin;
+1
View File
@@ -85,5 +85,6 @@ char *get_alias(char *cmd, alias_t *alias);
char *add_separator(char *cmd, int *return_values, int index);
char *get_alias_command(char *cmd, alias_t *alias);
void setup_sigint(void);
#define ERROR 84
+27 -4
View File
@@ -8,11 +8,13 @@
#include "shell.h"
#include "builtin.h"
#include "redirections.h"
#include <unistd.h>
#include <malloc.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
int run_builtin(const builtin *cmd, char **a, redirection *inout[2], env_t *env)
{
@@ -63,9 +65,30 @@ int builtin_cd(char **argv, env_t *env)
return (0);
}
bool my_strisnum(char *str)
{
for (int i = 0; str[i]; i++) {
if (!isdigit(str[i]))
return (false);
}
return (true);
}
int builtin_exit(char **argv, env_t *env)
{
if (argv[1])
env->vars = my_setenv(env->vars, "?", "1");
char *ptr = argv[1];
if (!ptr) {
env->vars = my_setenv(env->vars, "?", "0");
return (-1);
}
if (argv[1][0] == '-') {
ptr = &ptr[1];
}
if (!my_strisnum(ptr)) {
dprintf(2, "exit: Expression Syntax.\n");
return (0);
}
env->vars = my_setenv(env->vars, "?", argv[1]);
return (-1);
}
}
+1 -1
View File
@@ -101,7 +101,7 @@ bool handle_parent_inout(redirection *inout[2], env_t *env, bool builtin)
void run_cmd(char **argv, redirection *inout[2], env_t *env)
{
pid_t pid;
int status;
int status = 0;
char *path;
if (strlen(argv[0]) == 0)
+16 -10
View File
@@ -34,18 +34,32 @@ int eof_command(int key, buffer_t *buffer, env_t *env)
return (0);
}
int history_size(history_t *hist)
{
int i = 1;
if (!hist)
return (0);
while (hist->next) {
hist = hist->next;
i++;
}
return (i);
}
bool set_buffer_to_history(buffer_t *buffer, env_t *env)
{
history_t *hist = env->history;
char *cmd = NULL;
int len;
int hist_index = history_size(env->history) - buffer->history_index;
if (buffer->history_index == 0)
cmd = buffer->saved_buffer != NULL ? buffer->saved_buffer : "";
else
for (int i = 1; i < buffer->history_index && hist; i++)
for (int i = 0; i < hist_index && hist; i++)
hist = hist->next;
if (!cmd && (!hist || !(cmd = hist->command)))
if (hist_index < 0 || (!cmd && (!hist || !(cmd = hist->command))))
return (false);
if (!buffer->buffer || buffer->size < (len = strlen(cmd))) {
buffer->buffer = realloc(buffer->buffer, buffer->size + len);
@@ -75,12 +89,4 @@ int down_history_command(int key, buffer_t *buffer, env_t *env)
buffer->history_index--;
set_buffer_to_history(buffer, env);
return (0);
}
int clear_screen_command(int key, buffer_t *buffer, env_t *env)
{
my_move(env->window, 0, 0);
my_clrtobot();
prompt_prepare(buffer, env);
return (0);
}
+2
View File
@@ -25,6 +25,7 @@ const key_function_t key_functions[] = {
{"complete-command", &complete_command},
{"clear-screen", &clear_screen_command},
{"quoted-insert", &quoted_insert_command},
{"tty-sigintr", &tty_sigintr_command},
{NULL, NULL}
};
@@ -43,6 +44,7 @@ const binding_t emacs_bindings[] = {
{'\t', &complete_command},
{CTRL('l'), &clear_screen_command},
{CTRL('v'), &quoted_insert_command},
{CTRL('c'), &tty_sigintr_command},
{0, NULL}
};
+8
View File
@@ -13,4 +13,12 @@ int quoted_insert_command(int key, buffer_t *buffer, env_t *env)
{
buffer->quoted_insert = true;
return (0);
}
int clear_screen_command(int key, buffer_t *buffer, env_t *env)
{
my_move(env->window, 0, 0);
my_clrtobot();
prompt_prepare(buffer, env);
return (0);
}
+42
View File
@@ -0,0 +1,42 @@
/*
** EPITECH PROJECT, 2020
** ash
** File description:
** signals
*/
#include <signal.h>
#include <stddef.h>
#include <unistd.h>
#include "shell.h"
#include "key_functions.h"
#include "my_ncurses.h"
void on_sigint(int sig, siginfo_t *info, void *context)
{
my_addstr(stdwin, "\n");
}
void setup_sigint(void)
{
struct sigaction sa;
sa.sa_sigaction = &on_sigint;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sigaction(SIGINT, &sa, NULL);
}
int tty_sigintr_command(int key, buffer_t *buffer, env_t *env)
{
if (buffer->buffer) {
buffer->buffer[0] = '\0';
buffer->pos = 0;
env->vars = my_setenv(env->vars, "?", "130");
}
if (env->window) {
my_addstr(env->window, "\n");
prompt_prepare(buffer, env);
}
return (0);
}
+1
View File
@@ -74,6 +74,7 @@ void start_shell(env_t *env)
buffer_t buffer = {NULL, 0, 0, 0, 0, NULL, false};
int key;
setup_sigint();
if (isatty(0)) {
env->window = my_initwin();
prompt_prepare(&buffer, env);
+5
View File
@@ -0,0 +1,5 @@
int main()
{
while (1);
return (0);
}