mirror of
https://github.com/Savapitech/42sh.git
synced 2026-03-18 21:50:35 +01:00
[FIX] fanalyser problems plus the rest of the basic history such as double exclamation marks
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
|
||||
#include <string.h>
|
||||
#include "utils.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -58,12 +58,12 @@ static char *concat_cmd_arg(char *dest, char *src)
|
||||
char *his_last_command(char *line,
|
||||
his_variable_t *his_variable, his_command_t *his_command)
|
||||
{
|
||||
char *new_line;
|
||||
char *new_line = NULL;
|
||||
char *new_str = NULL;
|
||||
|
||||
if (his_command->sz == 0){
|
||||
printf("%d: Event not found\n", his_command->sz);
|
||||
return new_str;
|
||||
return NULL;
|
||||
}
|
||||
new_line = concat_cmd_arg(his_command[his_command->sz - 1].command,
|
||||
his_command[his_command->sz - 1].arg);
|
||||
@@ -77,16 +77,25 @@ char *his_last_command(char *line,
|
||||
char *his_last_same_command(char *line,
|
||||
his_variable_t *his_variable, his_command_t *his_command)
|
||||
{
|
||||
char *new_line = malloc(sizeof(char) * 10);
|
||||
char *new_line = &line[1];
|
||||
char *new_str = NULL;
|
||||
|
||||
if (new_line == NULL)
|
||||
return NULL;
|
||||
new_line[0] = 'l';
|
||||
new_line[1] = 's';
|
||||
new_line[2] = '\0';
|
||||
new_line[3] = '\0';
|
||||
for (int i = his_command->sz - 1; i >= 0; i--) {
|
||||
if (his_command[i].command == NULL) {
|
||||
printf("%s: Event not found\n", new_line);
|
||||
return NULL;
|
||||
}
|
||||
if (strncmp(his_command[i].command, new_line, strlen(new_line)) == 0) {
|
||||
new_line = concat_cmd_arg(his_command[i].command,
|
||||
his_command[i].arg);
|
||||
new_str = cat_in_str(his_variable, line, new_line);
|
||||
free(line);
|
||||
return new_str;
|
||||
}
|
||||
}
|
||||
printf("%s: Event not found\n", new_line);
|
||||
free(line);
|
||||
return new_line;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *his_id_command(char *line,
|
||||
@@ -108,19 +117,44 @@ char *his_id_command(char *line,
|
||||
return new_str;
|
||||
}
|
||||
|
||||
static char *get_last_word(char *str)
|
||||
{
|
||||
char *last_word = NULL;
|
||||
int last_space = 0;
|
||||
int x = 0;
|
||||
|
||||
if (!str)
|
||||
return NULL;
|
||||
while (str[x] != '\0') {
|
||||
if(isblank(str[x]))
|
||||
last_space = x + 1;
|
||||
x++;
|
||||
}
|
||||
last_word = malloc(sizeof(char) * (x - last_space) + 1);
|
||||
if (last_word != NULL) {
|
||||
last_word = strncpy(last_word, &str[last_space], x - last_space);
|
||||
last_word[x - last_space] = '\0';
|
||||
}
|
||||
return last_word;
|
||||
}
|
||||
|
||||
char *his_last_word(char *line,
|
||||
his_variable_t *his_variable, his_command_t *his_command)
|
||||
{
|
||||
char *new_line = malloc(sizeof(char) * 10);
|
||||
char *new_line = NULL;
|
||||
char *new_str = NULL;
|
||||
|
||||
if (new_line == NULL)
|
||||
if (his_command[his_command->sz - 1].arg == NULL){
|
||||
new_line = get_last_word(his_command[his_command->sz - 1].command);
|
||||
} else
|
||||
new_line = get_last_word(his_command[his_command->sz - 1].arg);
|
||||
if (!new_line)
|
||||
return NULL;
|
||||
new_line[0] = 'l';
|
||||
new_line[1] = 's';
|
||||
new_line[2] = '\0';
|
||||
new_line[3] = '\0';
|
||||
new_str = cat_in_str(his_variable, line, new_line);
|
||||
printf("%s\n", new_line);
|
||||
free(new_line);
|
||||
free(line);
|
||||
return new_line;
|
||||
return new_str;
|
||||
}
|
||||
|
||||
char *his_last_arg(char *line,
|
||||
@@ -23,6 +23,5 @@ int builtins_exit(ef_t *ef, char **args __attribute__((unused)))
|
||||
}
|
||||
free_env(ef->env);
|
||||
free(ef->buffer);
|
||||
save_cmd_history(ef->builtin_handler->history_command);
|
||||
exit(ef->builtin_handler->history->last_exit_code);
|
||||
}
|
||||
|
||||
@@ -13,93 +13,6 @@
|
||||
#include "history.h"
|
||||
#include "ctype.h"
|
||||
|
||||
static long long file_size(char *file)
|
||||
{
|
||||
int fd = 0;
|
||||
char buffer[4096];
|
||||
long long size = 0;
|
||||
int somme = 0;
|
||||
|
||||
fd = open(file, O_RDONLY);
|
||||
if (fd == -1)
|
||||
return -1;
|
||||
for (somme = 1; somme > 0; size += somme)
|
||||
somme = read(fd, buffer, 4095);
|
||||
if (somme == -1){
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
return size;
|
||||
}
|
||||
|
||||
char *my_catchar(char *file)
|
||||
{
|
||||
int fd = open(file, O_RDONLY);
|
||||
long long int size = file_size(file);
|
||||
char *str = malloc(sizeof(char) * (size + 1));
|
||||
int err = 0;
|
||||
|
||||
if (str == NULL || fd == -1)
|
||||
return NULL;
|
||||
err = read(fd, str, size);
|
||||
if (err == -1)
|
||||
return NULL;
|
||||
close(fd);
|
||||
str[size] = '\0';
|
||||
return str;
|
||||
}
|
||||
|
||||
static int check_delim(char *str, int x, char *delim, int before)
|
||||
{
|
||||
for (int i = 0; delim[i] != '\0'; i++) {
|
||||
if (str[x] == delim[i] && before != 1)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int get_len(char *str, char *delim)
|
||||
{
|
||||
int before = 0;
|
||||
int len = 1;
|
||||
|
||||
if (!str)
|
||||
return -1;
|
||||
for (int x = 0; str[x] != '\0'; x++){
|
||||
if (check_delim(str, x, delim, before) == 1) {
|
||||
len++;
|
||||
before = 1;
|
||||
} else
|
||||
before = 0;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
char **word_array_tok(char *str, char *delim)
|
||||
{
|
||||
char *tmp;
|
||||
char *strToken;
|
||||
char **tab = NULL;
|
||||
int x = 0;
|
||||
|
||||
if (!str || !delim)
|
||||
return tab;
|
||||
tmp = strdup(str);
|
||||
tab = malloc(sizeof(char *) * (get_len(tmp, delim) + 1));
|
||||
if (tab != NULL) {
|
||||
strToken = strtok(tmp, delim);
|
||||
while (strToken != NULL) {
|
||||
tab[x] = strdup(strToken);
|
||||
x++;
|
||||
strToken = strtok(NULL, delim);
|
||||
}
|
||||
tab[x] = NULL;
|
||||
}
|
||||
free(tmp);
|
||||
return tab;
|
||||
}
|
||||
|
||||
static char *get_arg(char *line, int x, int end_cmd)
|
||||
{
|
||||
char *tmp = malloc(sizeof(char) * (x - end_cmd) + 1);
|
||||
@@ -131,46 +44,3 @@ his_command_t set_cmd(char *line, his_command_t cmd_struct)
|
||||
cmd_struct.arg = get_arg(line, x, end_cmd);
|
||||
return cmd_struct;
|
||||
}
|
||||
|
||||
his_command_t *fill_cmd_history(his_command_t *cmd_history)
|
||||
{
|
||||
char *buffer = my_catchar(".historique");
|
||||
char **saved;
|
||||
int i = 0;
|
||||
|
||||
saved = word_array_tok(buffer, "\n");
|
||||
if (!saved)
|
||||
return cmd_history;
|
||||
for (; saved[i] != NULL || i >= 100; i++) {
|
||||
cmd_history[i] = set_cmd(saved[i], cmd_history[i]);
|
||||
}
|
||||
cmd_history->sz = i;
|
||||
free(buffer);
|
||||
for (int x = 0; saved[x]; x++)
|
||||
free(saved[x]);
|
||||
free(saved);
|
||||
return cmd_history;
|
||||
}
|
||||
|
||||
int save_cmd_history(his_command_t *cmd_history)
|
||||
{
|
||||
int fd = open(".historique", O_CREAT | O_TRUNC | O_WRONLY, 0644);
|
||||
|
||||
if (fd == -1){
|
||||
return 84;
|
||||
}
|
||||
printf("passed here\n");
|
||||
for (int i = 0; i < cmd_history->sz; i++){
|
||||
if (cmd_history[i].command != NULL)
|
||||
write(fd, cmd_history[i].command, strlen(cmd_history[i].command));
|
||||
if (cmd_history[i].arg != NULL) {
|
||||
write(fd, " ", 1);
|
||||
write(fd, cmd_history[i].arg, strlen(cmd_history[i].arg));
|
||||
}
|
||||
free(cmd_history[i].command);
|
||||
free(cmd_history[i].arg);
|
||||
write(fd, "\n", 1);
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -97,7 +97,6 @@ his_command_t *init_cmd_history(void)
|
||||
cmd_history[i].id = i;
|
||||
}
|
||||
cmd_history->sz = 0;
|
||||
cmd_history = fill_cmd_history(cmd_history);
|
||||
return cmd_history;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,10 @@ his_command_t *save_command(char *cmd, his_command_t *cmd_history)
|
||||
{
|
||||
if (!cmd)
|
||||
return cmd_history;
|
||||
cmd_history[cmd_history->sz] = set_cmd(cmd, cmd_history[cmd_history->sz]);
|
||||
if (cmd_history->sz < 100) {
|
||||
cmd_history[cmd_history->sz] = set_cmd(cmd,
|
||||
cmd_history[cmd_history->sz]);
|
||||
}
|
||||
cmd_history->sz++;
|
||||
return cmd_history;
|
||||
}
|
||||
@@ -24,7 +27,6 @@ size_t update_command(char **buffer,
|
||||
|
||||
buffer_len = u_strlen(*buffer);
|
||||
(*buffer)[buffer_len - 1] = '\0';
|
||||
//parse_alias
|
||||
parse_history(buffer, &buffer_len,
|
||||
buffer_sz, &builtin_handler->history_command);
|
||||
builtin_handler->history_command = save_command(*buffer,
|
||||
|
||||
Reference in New Issue
Block a user