Fuck it, YOLO!

This commit is contained in:
2024-04-24 18:11:37 +02:00
parent 59ce849e26
commit 52174b8737
7 changed files with 42 additions and 34 deletions

View File

@ -6,18 +6,18 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
/* Updated: 2024/04/24 15:36:12 by tomoron ### ########.fr */
/* Updated: 2024/04/24 18:04:15 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_token *token_add_back(t_token *token, char *value)
t_token *token_add_back(t_token *token, char *value, int is_var)
{
t_token *res;
t_token *current;
if (/*value && !*value*/0)
if (value && !*value && is_var)
{
free(value);
return (token);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/04/24 15:17:35 by tomoron ### ########.fr */
/* Updated: 2024/04/24 18:08:01 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -81,15 +81,15 @@ extern int g_return_code;
t_cmd *cmd_add_back(t_cmd *res, char *cmd, t_cmd_type type);
t_env *export_set_env(t_env *env, char *name, char *value, int append);
void *here_doc_variables(int write, void *data);
int add_var_to_str(char *res, char **command, t_env *env);
int add_var_to_str(char *res, char **command, t_env *env, int *is_var);
void find_cmd_path(t_msh *msh, char **paths, int *found);
t_env *env_add_back(t_env *env, char *name, char *value);
t_token *parse_cmds_to_token(t_cmd *command, t_env *env);
void exec_command_bonus(t_msh *msh, char *cmd_str);
t_token *add_token_back(t_token *res, t_token *next);
t_token *expand_wildcards(t_token *res, char *value);
t_token *expand_wildcards(t_token *res, char *value, int is_var);
int cmd_is_builtin(t_msh *msh, char *cmd_token);
t_token *token_add_back(t_token *res, char *token);
t_token *token_add_back(t_token *res, char *token, int is_var);
void child(t_msh *msh, char **cmd_args, int i);
t_token *parse_tokens(char *command, t_env *env);
void parent(t_msh *msh, int i, int cmd_count, char **cmd_args);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */
/* Updated: 2024/04/23 13:32:17 by tomoron ### ########.fr */
/* Updated: 2024/04/24 18:08:42 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
@ -31,7 +31,7 @@ int add_home_to_str(char *res)
return (i);
}
char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
char *get_token(char **cmd, int *quotes[2], t_env *env, int *is_var)
{
char *res;
int i;
@ -40,17 +40,17 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
while (ft_isspace(**cmd))
(*cmd)++;
res = ft_calloc(get_token_len(*cmd, env) + 1, 1);
while (res && **cmd && (is_cmd_char(**cmd) || *in_quote || *in_dquote))
while (res && **cmd && (is_cmd_char(**cmd) || *(quotes[0]) || *(quotes[1])))
{
if (**cmd == '"' && !*in_quote)
*in_dquote = !*in_dquote;
if (**cmd == '\'' && !*in_dquote)
*in_quote = !*in_quote;
if (**cmd == '$' && !*in_quote)
i += add_var_to_str(res + i, cmd, env);
else if (**cmd == '~' && !*in_quote && !*in_dquote)
if (**cmd == '"' && !*(quotes[0]))
*(quotes[1]) = !*(quotes[1]);
if (**cmd == '\'' && !*(quotes[1]))
*(quotes[0]) = !*(quotes[0]);
if (**cmd == '$' && !*(quotes[0]))
i += add_var_to_str(res + i, cmd, env, is_var);
else if (**cmd == '~' && !*(quotes[0]) && !*(quotes[1]))
i += add_home_to_str(res + i);
else if (((**cmd == '\'' && *in_dquote) || (**cmd == '"' && *in_quote))
else if (((**cmd == '\'' && *(quotes[1])) || (**cmd == '"' && *(quotes[0])))
|| (**cmd != '\'' && **cmd != '"'))
res[i++] = **cmd;
(*cmd)++;
@ -64,16 +64,18 @@ t_token *parse_tokens(char *command, t_env *env)
int in_dquote;
t_token *res;
char *value;
int is_var;
in_quote = 0;
in_dquote = 0;
res = 0;
is_var = 0;
while (command && *command)
{
value = get_token(&command, &in_quote, &in_dquote, env);
value = get_token(&command, (int *[2]){&in_quote, &in_dquote}, env, &is_var);
if (!value)
return (free_token(res));
res = expand_wildcards(res, value);
res = expand_wildcards(res, value, is_var);
while (ft_isspace(*command))
command++;
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */
/* Updated: 2024/04/22 19:28:28 by marde-vr ### ########.fr */
/* Updated: 2024/04/24 17:59:47 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -79,7 +79,7 @@ int invalid_variable_char(char *res, char c)
return (2);
}
int add_var_to_str(char *res, char **command, t_env *env)
int add_var_to_str(char *res, char **command, t_env *env, int *is_var)
{
char *var_name;
char *var;
@ -87,6 +87,7 @@ int add_var_to_str(char *res, char **command, t_env *env)
i = 0;
(*command)++;
*is_var = 1;
if (**command == '\'' || **command == '"' || !**command)
{
*res = '$';

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
/* Updated: 2024/04/23 16:41:42 by tomoron ### ########.fr */
/* Updated: 2024/04/24 17:48:14 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -60,7 +60,7 @@ void get_path(t_msh *msh, int *found)
char **paths;
paths = split_paths_from_env(msh->env);
if (!paths)
if (!paths || !*(msh->tokens->value))
{
free_paths(paths);
return ;
@ -94,7 +94,10 @@ void get_cmd_path(t_msh *msh)
get_path(msh, &found);
if (!found)
{
ft_printf_fd(2, "%s: command not found\n", msh->tokens->value);
if(!*(msh->tokens->value))
ft_printf_fd(2, "'': command not found\n");
else
ft_printf_fd(2, "%s: command not found\n", msh->tokens->value);
free(msh->tokens->value);
msh->tokens->value = 0;
g_return_code = 127;

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 12:53:29 by tomoron #+# #+# */
/* Updated: 2024/04/22 19:28:13 by marde-vr ### ########.fr */
/* Updated: 2024/04/24 18:02:49 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
@ -33,7 +33,7 @@ int filename_corresponds(char *wildcard, char *value)
return (!*wildcard && !*value);
}
t_token *get_all_files(DIR *dir, char *wildcard)
t_token *get_all_files(DIR *dir, char *wildcard, int is_var)
{
struct dirent *content;
t_token *res;
@ -43,7 +43,7 @@ t_token *get_all_files(DIR *dir, char *wildcard)
while (content)
{
if (filename_corresponds(wildcard, content->d_name))
res = token_add_back(res, ft_strdup(content->d_name));
res = token_add_back(res, ft_strdup(content->d_name), is_var);
content = readdir(dir);
}
return (res);
@ -62,25 +62,25 @@ t_token *add_token_back(t_token *res, t_token *next)
return (start);
}
t_token *expand_wildcards(t_token *res, char *value)
t_token *expand_wildcards(t_token *res, char *value, int is_var)
{
DIR *dir;
char *cwd;
t_token *new;
if (!ft_strchr(value, '*'))
return (token_add_back(res, value));
return (token_add_back(res, value, is_var));
cwd = getcwd(NULL, 100000);
if (!cwd)
return (token_add_back(res, value));
return (token_add_back(res, value, is_var));
dir = opendir(cwd);
free(cwd);
if (!dir)
return (token_add_back(res, value));
new = get_all_files(dir, value);
return (token_add_back(res, value, is_var));
new = get_all_files(dir, value, is_var);
closedir(dir);
if (!new)
return (token_add_back(res, value));
return (token_add_back(res, value, is_var));
free(value);
sort_wildcards_token(new);
res = add_token_back(res, new);

View File

@ -18,3 +18,5 @@ test signals
test and verify all malocs
verify forbidden functions
check for any hidden or useless files
return code 0 quand liste token est a 0