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> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ /* 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" #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 *res;
t_token *current; t_token *current;
if (/*value && !*value*/0) if (value && !*value && is_var)
{ {
free(value); free(value);
return (token); return (token);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ /* 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_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); t_env *export_set_env(t_env *env, char *name, char *value, int append);
void *here_doc_variables(int write, void *data); 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); void find_cmd_path(t_msh *msh, char **paths, int *found);
t_env *env_add_back(t_env *env, char *name, char *value); t_env *env_add_back(t_env *env, char *name, char *value);
t_token *parse_cmds_to_token(t_cmd *command, t_env *env); t_token *parse_cmds_to_token(t_cmd *command, t_env *env);
void exec_command_bonus(t_msh *msh, char *cmd_str); void exec_command_bonus(t_msh *msh, char *cmd_str);
t_token *add_token_back(t_token *res, t_token *next); 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); 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); void child(t_msh *msh, char **cmd_args, int i);
t_token *parse_tokens(char *command, t_env *env); t_token *parse_tokens(char *command, t_env *env);
void parent(t_msh *msh, int i, int cmd_count, char **cmd_args); 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> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */ /* 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" #include "minishell.h"
@ -31,7 +31,7 @@ int add_home_to_str(char *res)
return (i); 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; char *res;
int i; int i;
@ -40,17 +40,17 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
while (ft_isspace(**cmd)) while (ft_isspace(**cmd))
(*cmd)++; (*cmd)++;
res = ft_calloc(get_token_len(*cmd, env) + 1, 1); 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) if (**cmd == '"' && !*(quotes[0]))
*in_dquote = !*in_dquote; *(quotes[1]) = !*(quotes[1]);
if (**cmd == '\'' && !*in_dquote) if (**cmd == '\'' && !*(quotes[1]))
*in_quote = !*in_quote; *(quotes[0]) = !*(quotes[0]);
if (**cmd == '$' && !*in_quote) if (**cmd == '$' && !*(quotes[0]))
i += add_var_to_str(res + i, cmd, env); i += add_var_to_str(res + i, cmd, env, is_var);
else if (**cmd == '~' && !*in_quote && !*in_dquote) else if (**cmd == '~' && !*(quotes[0]) && !*(quotes[1]))
i += add_home_to_str(res + i); 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 != '"')) || (**cmd != '\'' && **cmd != '"'))
res[i++] = **cmd; res[i++] = **cmd;
(*cmd)++; (*cmd)++;
@ -64,16 +64,18 @@ t_token *parse_tokens(char *command, t_env *env)
int in_dquote; int in_dquote;
t_token *res; t_token *res;
char *value; char *value;
int is_var;
in_quote = 0; in_quote = 0;
in_dquote = 0; in_dquote = 0;
res = 0; res = 0;
is_var = 0;
while (command && *command) 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) if (!value)
return (free_token(res)); return (free_token(res));
res = expand_wildcards(res, value); res = expand_wildcards(res, value, is_var);
while (ft_isspace(*command)) while (ft_isspace(*command))
command++; command++;
} }

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */ /* 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); 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_name;
char *var; char *var;
@ -87,6 +87,7 @@ int add_var_to_str(char *res, char **command, t_env *env)
i = 0; i = 0;
(*command)++; (*command)++;
*is_var = 1;
if (**command == '\'' || **command == '"' || !**command) if (**command == '\'' || **command == '"' || !**command)
{ {
*res = '$'; *res = '$';

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */ /* 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; char **paths;
paths = split_paths_from_env(msh->env); paths = split_paths_from_env(msh->env);
if (!paths) if (!paths || !*(msh->tokens->value))
{ {
free_paths(paths); free_paths(paths);
return ; return ;
@ -94,7 +94,10 @@ void get_cmd_path(t_msh *msh)
get_path(msh, &found); get_path(msh, &found);
if (!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); free(msh->tokens->value);
msh->tokens->value = 0; msh->tokens->value = 0;
g_return_code = 127; g_return_code = 127;

View File

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

View File

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