This commit is contained in:
Loic Deridder
2025-02-11 14:25:40 +01:00
parent 5149604867
commit ebc62388a2
13 changed files with 375 additions and 174 deletions

View File

@@ -6,14 +6,14 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 11:25:25 by lderidde #+# #+# */
/* Updated: 2025/02/11 08:41:31 by lderidde ### ########.fr */
/* Updated: 2025/02/11 14:20:44 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_H
# define MINISHELL_H
# define DEBUG 1
# define DEBUG 0
typedef struct s_ast_n t_ast_n;
typedef struct s_node t_node;

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/10 10:33:28 by nalebrun #+# #+# */
/* Updated: 2024/11/27 12:20:56 by nalebrun ### ########.fr */
/* Created: 2024/10/10 10:33:28 by lderidde #+# #+# */
/* Updated: 2025/02/11 11:57:07 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -89,7 +89,7 @@ char *ft_sprintf(const char *str, ...);
int ft_debug(const char *fstr, ...);
char *rep_c(char c, int count);
char *get_next_line(int fd);
char *get_next_line(int fd, int del);
void ft_free(char **p);
void ft_free_v(void **p);

View File

@@ -3,121 +3,237 @@
/* ::: :::::::: */
/* gnl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/27 11:45:42 by nalebrun #+# #+# */
/* Updated: 2024/11/27 12:50:04 by nalebrun ### ########.fr */
/* Created: 2024/11/27 11:45:42 by lderidde #+# #+# */
/* Updated: 2025/02/11 12:24:09 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../libft.h"
#include "gnl.h"
void free_buf(char **tab)
static char *f_free(char *res, char *buffer, int ret)
{
if (tab && *tab)
{
free(*tab);
*tab = NULL;
}
char *tmp;
buffer[ret] = '\0';
tmp = ft_strjoin_gnl(res, buffer);
free(res);
return (tmp);
}
char *build_tab(char *tab, char *buffer, int i)
{
char *ret;
size_t t_len;
size_t b_len;
b_len = ft_gstrlen(buffer, -2);
t_len = ft_gstrlen(tab, i);
ret = malloc(b_len + t_len + 1);
if (!ret)
{
free(tab);
return (NULL);
}
if (tab)
{
ft_strlcpy(ret, tab, t_len + 1);
ft_strlcat(&ret[t_len], buffer, b_len + t_len + 1);
free(tab);
}
else
ft_strlcpy(ret, buffer, b_len + 1);
return (ret);
}
char *get_the_line(char **tab, int i)
static char *ft_line(char *buffer)
{
char *line;
char *new_tab;
size_t len;
char *nl_pos;
int i;
nl_pos = ft_strichr(*tab, '\n', i);
if (!nl_pos)
return (NULL);
len = nl_pos - *tab + 1;
line = malloc(len + 1);
i = 0;
while (buffer[i] && buffer[i] != '\n')
i++;
line = f_calloc(sizeof(char), (i + 2));
if (!line)
return (NULL);
ft_strlcpy(line, *tab, len + 1);
line[len] = '\0';
new_tab = ft_strdup(*tab + len);
if (!new_tab)
i = 0;
while (buffer[i] && buffer[i] != '\n')
{
line[i] = buffer[i];
i++;
}
if (buffer[i] == '\n')
line[i] = buffer[i];
i++;
line[i] = '\0';
return (line);
}
static char *ft_next(char *buffer)
{
char *buf;
int start;
int i;
start = 0;
buf = NULL;
while (buffer[start] && buffer[start] != '\n')
start++;
if (!buffer[start++])
{
free(buffer);
return (NULL);
}
if ((ft_strlen(buffer) - start) != 0)
{
buf = f_calloc(sizeof(char), ft_strlen(buffer) - start + 1);
if (!buf)
return (NULL);
}
i = 0;
ft_strlcat(buf, buffer, -1);
if (buf)
buf[i] = '\0';
free(buffer);
return (buf);
}
static char *ft_readfile(int fd, char *res, int ret)
{
char *buf;
if (!res)
res = f_calloc(1, sizeof(char));
if (!res)
return (NULL);
buf = malloc(BUFFER_SIZE + 1);
if (!buf)
return (NULL);
while (ret)
{
ret = read(fd, buf, BUFFER_SIZE);
if (ret == -1)
{
free(res);
free(buf);
return (NULL);
}
res = f_free(res, buf, ret);
if (ft_strchr_index(buf, '\n') != -1)
break ;
}
free(buf);
return (res);
}
char *get_next_line(int fd, int del)
{
static char *buffer[256];
char *line;
if (del == 1)
{
free(buffer[fd]);
buffer[fd] = NULL;
return (NULL);
}
if (fd < 0 || BUFFER_SIZE < 1 || fd > 255)
return (NULL);
buffer[fd] = ft_readfile(fd, buffer[fd], 1);
if (!buffer[fd])
return (NULL);
line = ft_line(buffer[fd]);
buffer[fd] = ft_next(buffer[fd]);
if (buffer[fd] == NULL && line[0] == '\0')
{
free(line);
return (NULL);
}
free(*tab);
*tab = new_tab;
return (line);
}
int read_file(int fd, char **tab, int i)
{
char buffer[BUFFER_SIZE + 1];
ssize_t bytes_read;
bytes_read = read(fd, buffer, BUFFER_SIZE);
if (bytes_read <= 0)
{
if (bytes_read == -1)
free_buf(&tab[fd]);
return (bytes_read);
}
buffer[bytes_read] = '\0';
tab[fd] = build_tab(tab[fd], buffer, i);
if (!tab[fd])
return (-1);
return (bytes_read);
}
char *get_next_line(int fd)
{
static char *tab[256];
char *next_line;
int i;
i = -1;
next_line = NULL;
if (fd < 0 || fd >= 256 || BUFFER_SIZE == 0)
return (NULL);
while (++i || 1)
{
next_line = get_the_line(&tab[fd], i);
if (next_line)
return (next_line);
if (read_file(fd, tab, i) <= 0)
break ;
}
if (tab[fd] && *tab[fd])
{
next_line = tab[fd];
tab[fd] = NULL;
return (next_line);
}
free(tab[fd]);
tab[fd] = NULL;
return (NULL);
}
// void free_buf(char **tab)
// {
// if (tab && *tab)
// {
// free(*tab);
// *tab = NULL;
// }
// }
//
// char *build_tab(char *tab, char *buffer, int i)
// {
// char *ret;
// size_t t_len;
// size_t b_len;
//
// b_len = ft_gstrlen(buffer, -2);
// t_len = ft_gstrlen(tab, i);
// ret = malloc(b_len + t_len + 1);
// if (!ret)
// {
// free(tab);
// return (NULL);
// }
// if (tab)
// {
// ft_strlcpy(ret, tab, t_len + 1);
// ft_strlcat(&ret[t_len], buffer, b_len + t_len + 1);
// free(tab);
// }
// else
// ft_strlcpy(ret, buffer, b_len + 1);
// return (ret);
// }
//
// char *get_the_line(char **tab, int i)
// {
// char *line;
// char *new_tab;
// size_t len;
// char *nl_pos;
//
// nl_pos = ft_strichr(*tab, '\n', i);
// if (!nl_pos)
// return (NULL);
// len = nl_pos - *tab + 1;
// line = malloc(len + 1);
// if (!line)
// return (NULL);
// ft_strlcpy(line, *tab, len + 1);
// line[len] = '\0';
// new_tab = ft_strdup(*tab + len);
// if (!new_tab)
// {
// free(line);
// return (NULL);
// }
// free(*tab);
// *tab = new_tab;
// return (line);
// }
//
// int read_file(int fd, char **tab, int i)
// {
// char buffer[BUFFER_SIZE + 1];
// ssize_t bytes_read;
//
// bytes_read = read(fd, buffer, BUFFER_SIZE);
// if (bytes_read <= 0)
// {
// if (bytes_read == -1)
// free_buf(&tab[fd]);
// return (bytes_read);
// }
// buffer[bytes_read] = '\0';
// tab[fd] = build_tab(tab[fd], buffer, i);
// if (!tab[fd])
// return (-1);
// return (bytes_read);
// }
//
// char *get_next_line(int fd)
// {
// static char *tab[256];
// char *next_line;
// int i;
//
// i = -1;
// next_line = NULL;
// if (fd < 0 || fd >= 256 || BUFFER_SIZE == 0)
// return (NULL);
// while (++i || 1)
// {
// next_line = get_the_line(&tab[fd], i);
// if (next_line)
// return (next_line);
// if (read_file(fd, tab, i) <= 0)
// break ;
// }
// if (tab[fd] && *tab[fd])
// {
// next_line = tab[fd];
// tab[fd] = NULL;
// return (next_line);
// }
// free(tab[fd]);
// tab[fd] = NULL;
// return (NULL);
// }

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* gnl.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/27 12:46:20 by nalebrun #+# #+# */
/* Updated: 2024/11/27 12:53:23 by nalebrun ### ########.fr */
/* Created: 2024/11/27 12:46:20 by lderidde #+# #+# */
/* Updated: 2025/02/11 11:56:11 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,7 +15,11 @@
# include "../../libft.h"
size_t ft_gstrlen(const char *str, int j);
char *ft_strichr(const char *s, int c, int i);
char *ft_strjoin_gnl(char *s1, char *s2);
void *f_calloc(int size, int nb);
int ft_strchr_index(char *s, int c);
char *get_next_line(int fd, int del);
// size_t ft_gstrlen(const char *str, int j);
// char *ft_strichr(const char *s, int c, int i);
#endif

View File

@@ -3,61 +3,122 @@
/* ::: :::::::: */
/* gnl_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/27 12:48:02 by nalebrun #+# #+# */
/* Updated: 2024/11/27 12:53:32 by nalebrun ### ########.fr */
/* Created: 2024/11/27 12:48:02 by lderidde #+# #+# */
/* Updated: 2025/02/11 11:56:38 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../libft.h"
#include "gnl.h"
size_t ft_gstrlen(const char *str, int j)
int ft_strchr_index(char *s, int c)
{
size_t i;
if (!s)
return (-1);
i = 0;
if (!str)
return (0);
if (j == 0 || j == -2)
while (s[i])
{
while (str[i])
i++;
return (i);
if (s[i] == (unsigned char)c)
return (i);
i++;
}
else
{
if (j > 0)
j--;
if (j != 0)
i += (BUFFER_SIZE * j) - 1;
while (str[i])
i++;
if (s[i] == (unsigned char)c)
return (i);
}
return (-1);
}
char *ft_strichr(const char *s, int c, int i)
void *f_calloc(int size, int nb)
{
void *ptr;
int i;
i = 0;
ptr = malloc(size * nb);
if (!ptr)
return (NULL);
while (i < size * nb)
{
((char *)ptr)[i] = 0;
i++;
}
return (ptr);
}
char *ft_strjoin_gnl(char *s1, char *s2)
{
char *str;
char ch;
int i;
int j;
if (!s)
return (0);
if (i > 0)
i--;
ch = c;
str = (char *)s;
if (i != 0)
str += (BUFFER_SIZE * i) - 1;
while (*str)
str = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
if (!str)
return (NULL);
j = 0;
i = -1;
while (s1 && s1[++i])
{
if (*str == ch)
return (str);
str++;
str[j] = s1[i];
j++;
}
if ((ch == 0) && (*str == 0))
return (str);
return (0);
i = 0;
while (s2 && s2[i])
{
str[j] = s2[i];
i++;
j++;
}
str[j] = '\0';
return (str);
}
// size_t ft_gstrlen(const char *str, int j)
// {
// size_t i;
//
// i = 0;
// if (!str)
// return (0);
// if (j == 0 || j == -2)
// {
// while (str[i])
// i++;
// return (i);
// }
// else
// {
// if (j > 0)
// j--;
// if (j != 0)
// i += (BUFFER_SIZE * j) - 1;
// while (str[i])
// i++;
// return (i);
// }
// }
//
// char *ft_strichr(const char *s, int c, int i)
// {
// char *str;
// char ch;
//
// if (!s)
// return (0);
// if (i > 0)
// i--;
// ch = c;
// str = (char *)s;
// if (i != 0)
// str += (BUFFER_SIZE * i) - 1;
// while (*str)
// {
// if (*str == ch)
// return (str);
// str++;
// }
// if ((ch == 0) && (*str == 0))
// return (str);
// return (0);
// }

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:13 by lderidde #+# #+# */
/* Updated: 2025/02/06 13:27:33 by lderidde ### ########.fr */
/* Updated: 2025/02/11 13:01:12 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,16 +29,19 @@ int ft_isnumeric(char *str)
return (1);
}
void bash_exit(int code)
void bash_exit(int code, int check, t_ast_n *node)
{
ft_putendl_fd("exit", 2);
if (check)
ft_putendl_fd("exit", 2);
free_child(node->msh);
exit(code);
}
void bash_exit_errornum(char *arg)
void bash_exit_errornum(char *arg, t_ast_n *node)
{
ft_putendl_fd("exit", 2);
err_msg_cmd("exit", arg, "numeric argument required", 2);
free_child(node->msh);
exit(2);
}
@@ -52,6 +55,7 @@ int bash_exiterrorcount(void)
int builtin_exit(char **arg, bool depth, t_ast_n *node)
{
long res;
int ret;
res = node->msh->ex_code;
if (arg[1] && ft_isnumeric(arg[1]))
@@ -61,14 +65,18 @@ int builtin_exit(char **arg, bool depth, t_ast_n *node)
if (count_args(arg) > 2 && ft_isnumeric(arg[1]))
return (err_msg_cmd("exit", NULL, "too many arguments", 1));
else if (arg[1] && (!ft_isnumeric(arg[1]) || errno == ERANGE))
exit (err_msg_cmd("exit", arg[1], "numeric argument required", 2));
exit (res % 256);
{
ret = err_msg_cmd("exit", arg[1], "numeric argument required", 2);
free_child(node->msh);
exit (ret);
}
bash_exit (res % 256, 0, node);
}
if (count_args(arg) > 2 && ft_isnumeric(arg[1]))
return (bash_exiterrorcount());
else if (arg[1] && (!ft_isnumeric(arg[1]) || errno == ERANGE))
bash_exit_errornum(arg[1]);
bash_exit_errornum(arg[1], node);
else
bash_exit(res % 256);
bash_exit(res % 256, 1, node);
return (1);
}

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:20 by lderidde #+# #+# */
/* Updated: 2025/02/08 14:57:34 by lderidde ### ########.fr */
/* Updated: 2025/02/11 14:25:21 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */
/* Updated: 2025/02/11 11:06:48 by lderidde ### ########.fr */
/* Updated: 2025/02/11 12:30:00 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -186,15 +186,17 @@ void read_input(t_ast_n *node, int j)
check = ifhere_remove_quote(node, j);
len = ft_strlen(node->files[j]);
str = get_next_line(node->msh->here_fd);
str = get_next_line(node->msh->here_fd, 0);
while (str && ft_strncmp(str, node->files[j], len) != 0)
{
if (!check)
expander_here(&str, node);
write(1, str, ft_strlen(str));
ft_free(&str);
str = get_next_line(node->msh->here_fd);
str = get_next_line(node->msh->here_fd, 0);
}
if (!str)
get_next_line(node->msh->here_fd, 1);
ft_free(&str);
}
@@ -210,6 +212,7 @@ void here_doc(t_ast_n *node, int i)
close(node->fds[0]);
close(node->fds[1]);
read_input(node, i);
free_child(node->msh);
exit(EXIT_SUCCESS);
}
else if (pid > 0)
@@ -334,8 +337,10 @@ char *find_path(char *cmd, char **env)
if (access(cmd, F_OK) == 0 && access(cmd, X_OK) == 0)
return (cmd);
i = 0;
while (ft_strnstr(env[i], "PATH=", 5) == NULL)
while (env[i] && ft_strnstr(env[i], "PATH=", 5) == NULL)
i++;
if (!env[i])
return (NULL);
paths = ft_split(env[i] + 5, ":");
i = -1;
while (paths[++i])

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/10 09:09:37 by lderidde #+# #+# */
/* Updated: 2025/02/10 13:29:09 by lderidde ### ########.fr */
/* Updated: 2025/02/11 13:04:05 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -100,7 +100,7 @@ int expand_star(t_ast_n *node, int j)
int i;
i = 0;
while (node->args[j][i])
while (node->args[j] && node->args[j][i])
{
if (node->args[j][i] == '*' && !in_quote(node->args[j], &node->args[j][i]))
return (expander_star(node, j, node->args[j]), 1);

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/07 10:23:02 by lderidde #+# #+# */
/* Updated: 2025/02/10 10:40:34 by lderidde ### ########.fr */
/* Updated: 2025/02/11 13:10:18 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -106,7 +106,7 @@ t_ast_n *expand_node(t_ast_n *node)
int check;
i = -1;
while (node->args[++i])
while (node->args && node->args[++i])
{
check = 0;
if (expand_tilde(node, i))
@@ -116,6 +116,8 @@ t_ast_n *expand_node(t_ast_n *node)
if (!ifremove_quote(node, i) && check)
{
split_tab(node, i);
if (!node->args[i])
i--;
check = 0;
}
check = expand_star(node, i);

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 14:16:52 by nalebrun #+# #+# */
/* Updated: 2025/02/10 12:31:46 by nalebrun ### ########.fr */
/* Created: 2025/01/27 14:16:52 by lderidde #+# #+# */
/* Updated: 2025/02/11 13:22:49 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,14 +37,14 @@ static void add_prevhistory(t_msh *msh)
char *str;
char *tmp;
str = get_next_line(msh->hist);
str = get_next_line(msh->hist, 0);
while (str)
{
tmp = ft_substr(str, 0, ft_strlen(str) - 1);
add_history(tmp);
free(tmp);
free(str);
str = get_next_line(msh->hist);
str = get_next_line(msh->hist, 0);
}
}
@@ -58,7 +58,9 @@ static void interpret_cmd(char **input, t_msh *msh)
}
msh->here_fd = open(".heredoc", O_RDONLY);
msh->ex_code = execute_command(msh->head);
close(msh->here_fd);
get_next_line(msh->here_fd, 1);
if (msh->here_fd != -1)
close(msh->here_fd);
unlink(".heredoc");
free_ast(msh->head);
msh->head = NULL;

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 15:32:03 by lderidde #+# #+# */
/* Updated: 2025/02/10 13:31:17 by lderidde ### ########.fr */
/* Updated: 2025/02/11 13:18:58 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -50,7 +50,8 @@ t_msh *init_msh(char **envp)
void free_msh(t_msh *msh)
{
free_tab(msh->env);
close(msh->hist);
if (msh->hist != -1)
close(msh->hist);
free(msh->input);
free(msh);
}

View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* powerline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/10 12:19:10 by nalebrun #+# #+# */
/* Updated: 2025/02/10 12:19:10 by nalebrun ### ########.fr */
/* Created: 2025/02/10 12:19:10 by lderidde #+# #+# */
/* Updated: 2025/02/11 14:19:32 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,12 +29,14 @@ char *get_pwd()
char *out;
pwd = getcwd(NULL, 0);
if (!pwd)
return (ft_strdup(""));
pwd_base = pwd;
cpy = pwd;
if (ft_strncmp(pwd, "/home", 5) == 0)
{
pwd += 6;
while (*pwd && (*pwd) != '/')
while (pwd && *pwd && (*pwd) != '/')
pwd++;
out = ft_strjoin("~", pwd);
}