This commit is contained in:
2024-04-24 19:20:42 +02:00
12 changed files with 50 additions and 55 deletions

BIN
srcs/.minishell.h.swp Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 14:54:53 by tomoron #+# #+# */ /* Created: 2024/04/24 14:54:53 by tomoron #+# #+# */
/* Updated: 2024/04/24 14:55:56 by tomoron ### ########.fr */ /* Updated: 2024/04/24 18:57:48 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,7 +38,7 @@ int check_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
return (0); return (0);
} }
token = parse_cmds_to_token(cmd, env); token = parse_cmds_to_token(cmd, env);
if (!token) if (token == (void *)1)
return (0); return (0);
free_token(token); free_token(token);
return (1); return (1);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */ /* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */
/* Updated: 2024/04/18 20:48:52 by marde-vr ### ########.fr */ /* Updated: 2024/04/24 14:52:21 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -39,7 +39,7 @@ int echo(t_token *args)
{ {
while (args->value[i] == 'n') while (args->value[i] == 'n')
i++; i++;
if (!args->value[i]) if (!args->value[i] && i > 1)
put_nl = 0; put_nl = 0;
else else
ft_printf("%s ", args->value); ft_printf("%s ", args->value);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
/* Updated: 2024/04/24 19:15:13 by tomoron ### ########.fr */ /* Updated: 2024/04/24 19:18:07 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -117,7 +117,10 @@ void exec_commands(t_msh *msh)
int i; int i;
if (!msh->tokens && !is_parenthesis(msh->cmds)) if (!msh->tokens && !is_parenthesis(msh->cmds))
{
g_return_code = 0;
return ; return ;
}
cmd_count = get_cmd_count(msh->cmds); cmd_count = get_cmd_count(msh->cmds);
msh->fds = ft_calloc(cmd_count + 1, sizeof(int **)); msh->fds = ft_calloc(cmd_count + 1, sizeof(int **));
msh->pids = ft_calloc(cmd_count, sizeof(int *)); msh->pids = ft_calloc(cmd_count, sizeof(int *));

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 19:11:36 by tomoron ### ########.fr */ /* Updated: 2024/04/24 19:20:23 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -82,16 +82,16 @@ 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 set_echoctl(int value); int set_echoctl(int value);
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 <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 17:19:27 by tomoron #+# #+# */ /* Created: 2024/03/27 17:19:27 by tomoron #+# #+# */
/* Updated: 2024/04/18 20:49:00 by marde-vr ### ########.fr */ /* Updated: 2024/04/24 18:59:26 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,7 @@ t_token *parsing_syntax_error(t_token *res)
{ {
free_token(res); free_token(res);
ft_putstr_fd("minishell: syntax error\n", 2); ft_putstr_fd("minishell: syntax error\n", 2);
return (0); return ((void *)1);
} }
int is_input_type(t_cmd *cmd) int is_input_type(t_cmd *cmd)

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

@ -1,17 +1,3 @@
cat <minishell.h <<HERE <missing <<DOC | echo oi
#invalid command, followed by empty variable, should clear the exit code
doesntexist
$EMPTY
echo $?
# Should skip the empty argument, and print hello after spaces
echo - "" " " hello
NORME :c
To test: To test:
test signals test signals