did some slight norming (200 lines of norminette and i'm not even done yet... :c)

This commit is contained in:
mdev9
2024-04-22 19:56:22 +02:00
parent 642a9ab232
commit e4df4a5655
15 changed files with 98 additions and 112 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:20:21 by marde-vr #+# #+# */
/* Updated: 2024/04/22 13:40:49 by marde-vr ### ########.fr */
/* Updated: 2024/04/22 19:39:03 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,7 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token)
if (!cmd_token)
return (0);
else if ((msh->in_type == PIPE || msh->out_type == PIPE)
&& cmd_is_forkable_builtin(cmd_token))
&& cmd_is_forkable_builtin(cmd_token))
return (1);
else if (!ft_strcmp(cmd_token, "cd"))
g_return_code = cd(msh->tokens);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */
/* Updated: 2024/04/19 14:42:50 by tomoron ### ########.fr */
/* Updated: 2024/04/22 19:32:11 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -66,7 +66,7 @@ void remove_command_from_msh(t_msh *msh)
while (msh->cmds && !is_cmd_type(msh->cmds))
msh->cmds = msh->cmds->next;
while (msh->cmds && (is_cmd_type(msh->cmds) || is_input_type(msh->cmds)
|| is_output_type(msh->cmds)))
|| is_output_type(msh->cmds)))
msh->cmds = msh->cmds->next;
tmp = msh->cmds;
while (tmp && !is_cmd_type(tmp))

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
/* Updated: 2024/04/22 19:18:52 by marde-vr ### ########.fr */
/* Updated: 2024/04/22 19:55:41 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,28 +26,22 @@ void get_redirections(t_msh *msh, t_cmd *cmds)
if (!get_out_type(msh, cmds))
get_in_type(msh, cmds);
}
/*
printf("in_type:");
print_cmd_type(msh->in_type, 0);
printf("\nout_type:");
print_cmd_type(msh->out_type, 0);
printf("\n");*/
}
t_cmd *get_next_command(t_cmd *cmd)
{
while(cmd)
while (cmd)
{
while(cmd && !is_operand_type(cmd))
while (cmd && !is_operand_type(cmd))
cmd = cmd->next;
if(cmd && cmd->cmd_type == AND && !g_return_code)
return(cmd->next);
if(cmd && cmd->cmd_type == OR && g_return_code)
return(cmd->next);
if(cmd)
if (cmd && cmd->cmd_type == AND && !g_return_code)
return (cmd->next);
if (cmd && cmd->cmd_type == OR && g_return_code)
return (cmd->next);
if (cmd)
cmd = cmd->next;
}
return(0);
return (0);
}
void exec_command_bonus(t_msh *msh, char *cmd_str)
@ -55,26 +49,21 @@ void exec_command_bonus(t_msh *msh, char *cmd_str)
t_cmd *cmds;
t_cmd *tmp;
if(!cmd_str)
if (!cmd_str)
return ;
cmds = parsing_bonus(cmd_str);
//print_parsed_cmd(cmds); // debug
tmp = check_cmds_syntax(cmds);
if (tmp)
{
print_syntax_error_bonus(tmp);
//printf("error\n"); // debug
free_cmd(cmds);
return ;
}
msh->cmds_head = cmds;
while (cmds)
{
//print_parsed_cmd(cmds); // debug
msh->tokens = parse_cmds_to_token(cmds, msh->env);
msh->cmds = cmds;
//print_msh_struct(msh); // debug
//print_parsed_token(msh->tokens); // debug
exec_commands(msh);
msh->in_fd = 0;
msh->out_fd = 0;
@ -89,14 +78,12 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
if (i != cmd_count - 1)
{
//fprintf(stderr, "piping %d", i);
if (pipe(msh->fds[i]) == -1)
{
perror("minishell: pipe");
free(cmd_args);
ft_exit(msh, 1);
}
//fprintf(stderr, "pipe: msh->fds[%d][0]: %d, msh->fds[%d][1]: %d\n", i, msh->fds[i][0], i, msh->fds[i][1]);
}
pid = fork();
if (pid == -1)
@ -136,13 +123,14 @@ int get_cmd_count(t_cmd *cmds)
nb = 0;
while (cmds && !is_operand_type(cmds))
{
while(cmds && (is_output_type(cmds) || is_input_type(cmds)))
cmds=cmds->next;
while (cmds && (is_output_type(cmds) || is_input_type(cmds)))
cmds = cmds->next;
if (is_cmd_type(cmds))
nb++;
while(cmds && (is_output_type(cmds) || is_input_type(cmds) || is_cmd_type(cmds)))
cmds=cmds->next;
if(cmds && cmds->cmd_type == PIPE)
while (cmds && (is_output_type(cmds) || is_input_type(cmds)
|| is_cmd_type(cmds)))
cmds = cmds->next;
if (cmds && cmds->cmd_type == PIPE)
cmds = cmds->next;
}
return (nb);
@ -150,31 +138,31 @@ int get_cmd_count(t_cmd *cmds)
void print_signaled(int status)
{
int signal;
static const char *sigmsg[] = {0, "Hangup", 0, "Quit", \
"Illegal instruction", "Trace/breakpoint trap", "Aborted", "Bus error", \
"Floating point exception", "Killed", "User defined signal 1", \
"Segmentation fault (skill issue)", "User defined signal 2", 0,\
"Alarm clock", "Terminated", "Stack fault" ,0 ,0, "Stopped", "Stopped",\
"Stopped", "Stopped", 0, "CPU time limit exceeded",\
"File size limit exceeded", "Virtual time expired",\
"Profiling timer expired","I/O possible", "Power failure",\
"Bad system call"};
int signal;
static const char *sigmsg[] = {0, "Hangup", 0, "Quit", "Illegal \
instruction", "Trace/breakpoint trap", "Aborted", "Bus error",
"Floating point exception", "Killed", "User defined signal 1",
"Segmentation fault (skill issue)", "User defined signal 2", 0,
"Alarm clock", "Terminated", "Stack fault", 0, 0, "Stopped", "Stopped",
"Stopped", "Stopped", 0, "CPU time limit exceeded",
"File size limit exceeded", "Virtual time expired",
"Profiling timer expired", "I/O possible", "Power failure",
"Bad system call"};
signal = WTERMSIG(status);
if(signal < 31 && sigmsg[signal])
if (signal < 31 && sigmsg[signal])
{
ft_putstr_fd((char *)sigmsg[signal], 2);
}
if(signal >= 34 && signal <= 64)
if (signal >= 34 && signal <= 64)
{
ft_putstr_fd("Real-time signal ", 2);
ft_putnbr_fd(signal - 34, 2);
}
if(WCOREDUMP(status))
if (WCOREDUMP(status))
ft_putstr_fd(" (core dumped)", 2);
ft_putstr_fd("\n", 2);
g_return_code = signal + 128;
g_return_code = signal + 128;
}
void end_execution(t_msh *msh, int cmd_count)
@ -185,7 +173,7 @@ void end_execution(t_msh *msh, int cmd_count)
i = 0;
status = 0;
while (i < cmd_count)
waitpid(msh->pids[i++], &status, 0);
waitpid(msh->pids[i++], &status, 0);
if (!g_return_code && WIFEXITED(status))
g_return_code = WEXITSTATUS(status);
if (WIFSIGNALED(status))
@ -194,17 +182,17 @@ void end_execution(t_msh *msh, int cmd_count)
free_fds(msh);
msh->pids = 0;
free(msh->fds);
signal(SIGINT, signal_handler_interactive); //enables ctrl-C
signal(SIGINT, signal_handler_interactive);
signal(SIGQUIT, signal_handler_interactive);
set_echoctl(0);
}
int handle_parenthesis(t_msh *msh)
int handle_parenthesis(t_msh *msh)
{
if(!(msh->cmds->cmd_type == PAREN || (msh->cmds->cmd_type == PIPE && msh->cmds->next->cmd_type == PAREN)))
return(0);
return(1);
if (!(msh->cmds->cmd_type == PAREN || (msh->cmds->cmd_type == PIPE
&& msh->cmds->next->cmd_type == PAREN)))
return (0);
return (1);
}
void exec_commands(t_msh *msh)
@ -226,5 +214,5 @@ void exec_commands(t_msh *msh)
exec_command(msh, i, cmd_count);
i++;
}
end_execution(msh, cmd_count);
end_execution(msh, cmd_count);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
/* Updated: 2024/04/21 23:46:43 by tomoron ### ########.fr */
/* Updated: 2024/04/22 19:31:12 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,38 +36,38 @@ int export_invalid_identifier(char *arg, char *name)
return (1);
}
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)
{
t_env *tmp;
t_env *tmp;
tmp = env;
if(!value)
if (!value)
{
free(name);
ft_printf_fd(2, "minishell: malloc failed");
return(env);
return (env);
}
while(tmp)
while (tmp)
{
if(!ft_strcmp(name, tmp->name))
if (!ft_strcmp(name, tmp->name))
{
free(name);
if(!*value)
if (!*value)
{
free(value);
return(env);
return (env);
}
if(append)
if (append)
value = ft_strjoin_free(tmp->value, value, 2);
if(!value)
return(env);
if (!value)
return (env);
free(tmp->value);
tmp->value = value;
return(env);
return (env);
}
tmp = tmp->next;
}
return(env_add_back(env, name, value));
return (env_add_back(env, name, value));
}
int ft_export(t_token *cmd, t_env *env)

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */
/* Updated: 2024/04/22 19:12:50 by marde-vr ### ########.fr */
/* Updated: 2024/04/22 19:50:00 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,6 @@ void redirect_input(t_msh *msh, int i, char **cmd_args)
{
if (dup2(msh->fds[i - 1][0], 0) < 0)
{
perror("dup2"); //debug
free(cmd_args);
ft_exit(msh, 1);
}
@ -82,7 +81,8 @@ int get_in_type(t_msh *msh, t_cmd *tokens)
if (open_input_file(msh, &cur_token))
return (1);
}
if (cur_token && cur_token->next && !is_operand_type(cur_token->next) && cur_token->cmd_type != PIPE && cur_token->next->cmd_type != PIPE)
if (cur_token && cur_token->next && !is_operand_type(cur_token->next)
&& cur_token->cmd_type != PIPE && cur_token->next->cmd_type != PIPE)
return (get_in_type(msh, cur_token->next));
return (0);
}

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/22 19:13:12 by marde-vr ### ########.fr */
/* Updated: 2024/04/22 19:30:03 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -46,7 +46,6 @@ typedef struct s_cmd
struct s_cmd *next;
} t_cmd;
typedef struct s_token
{
char *value;
@ -83,7 +82,7 @@ void *here_doc_variables(int write, int index, void *data);
int add_var_to_str(char *res, char **command, t_env *env);
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);
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);
@ -137,13 +136,13 @@ void exec_commands(t_msh *msh);
char *get_var_name(char *str);
int exec_builtin(t_msh *msh);
void get_cmd_path(t_msh *msh);
int is_operand_type(t_cmd *cmd);
int is_operand_type(t_cmd *cmd);
int set_echoctl(int value);
int is_output_type(t_cmd *cmd);
int is_output_type(t_cmd *cmd);
int print_env(t_env *env);
t_cmd *free_cmd(t_cmd *cmd);
int ft_export(t_token *cmd, t_env *env);
int is_input_type(t_cmd *cmd);
int is_input_type(t_cmd *cmd);
void free_env(t_env *env);
int ft_unset(t_msh *msh);
void free_fds(t_msh *msh);
@ -151,7 +150,7 @@ void free_msh(t_msh *msh);
void free_msh(t_msh *msh);
int echo(t_token *args);
int exit_bt(t_msh *msh);
int is_cmd_type(t_cmd *cmd);
int is_cmd_type(t_cmd *cmd);
int is_cmd_char(char c);
int cd(t_token *args);
int pwd(void);

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */
/* Updated: 2024/04/22 19:12:32 by marde-vr ### ########.fr */
/* Updated: 2024/04/22 19:42:05 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,6 @@ void redirect_output(t_msh *msh, int i, char **cmd_args)
free(cmd_args);
ft_exit(msh, 1);
}
}
else
{
@ -66,7 +65,8 @@ int get_out_type(t_msh *msh, t_cmd *cmds)
msh->out_fd = 0;
ret = 0;
cur_cmd = cmds;
while (cur_cmd && cur_cmd->next && (!is_cmd_type(cur_cmd) && !is_output_type(cur_cmd)))
while (cur_cmd && cur_cmd->next && (!is_cmd_type(cur_cmd)
&& !is_output_type(cur_cmd)))
cur_cmd = cur_cmd->next;
while (cur_cmd && cur_cmd->next && !is_output_type(cur_cmd)
&& !is_operand_type(cur_cmd) && cur_cmd->cmd_type != PIPE)
@ -74,7 +74,7 @@ int get_out_type(t_msh *msh, t_cmd *cmds)
if (cur_cmd && (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN))
msh->out_type = 0;
else if (cur_cmd && is_output_type(cur_cmd) && !is_operand_type(cur_cmd)
&& cur_cmd->cmd_type != PIPE)
&& cur_cmd->cmd_type != PIPE)
{
msh->out_type = cur_cmd->cmd_type;
filename = parse_tokens(cur_cmd->value, msh->env);
@ -85,5 +85,5 @@ int get_out_type(t_msh *msh, t_cmd *cmds)
}
else if (cur_cmd && cur_cmd->cmd_type == PIPE)
msh->out_type = PIPE;
return(ret);
return (ret);
}

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/19 10:43:47 by tomoron ### ########.fr */
/* Updated: 2024/04/22 19:37:26 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
@ -51,7 +51,7 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
else if (**cmd == '~' && !*in_quote && !*in_dquote)
i += add_home_to_str(res + i);
else if (((**cmd == '\'' && *in_dquote) || (**cmd == '"' && *in_quote))
|| (**cmd != '\'' && **cmd != '"'))
|| (**cmd != '\'' && **cmd != '"'))
res[i++] = **cmd;
(*cmd)++;
}
@ -82,20 +82,21 @@ t_token *parse_tokens(char *command, t_env *env)
return (res);
}
t_token *parse_cmds_to_token(t_cmd *command, t_env *env)
t_token *parse_cmds_to_token(t_cmd *command, t_env *env)
{
t_token *res;
t_token *new;
t_token *res;
t_token *new;
res = 0;
while(command && (is_cmd_type(command) || is_output_type(command) || is_input_type(command)))
while (command && (is_cmd_type(command) || is_output_type(command)
|| is_input_type(command)))
{
if(is_cmd_type(command))
if (is_cmd_type(command))
{
new = parse_tokens(command->value, env);
new = parse_tokens(command->value, env);
res = add_token_back(res, new);
}
command = command->next;
}
return(res);
return (res);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */
/* Updated: 2024/04/19 10:40:51 by tomoron ### ########.fr */
/* Updated: 2024/04/22 19:38:45 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -126,6 +126,7 @@ char *get_cmd_value(char **cmd, t_cmd_type type)
(*cmd)++;
return (res);
}
void print_syntax_error_bonus(t_cmd *cmd)
{
if (cmd->cmd_type == CMD || cmd->cmd_type == PAREN)
@ -204,6 +205,7 @@ int get_next_arg_len(char *cmd)
}
return (len);
}
char *get_next_arg(char **cmd)
{
int len;
@ -236,7 +238,7 @@ t_cmd *parsing_bonus(char *cmd)
if (type == CMD || type == PAREN)
value = get_cmd_value(&cmd, type);
else if (type == RED_O || type == RED_O_APP || type == RED_I
|| type == HERE_DOC)
|| type == HERE_DOC)
value = get_next_arg(&cmd);
else
value = 0;

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 18:31:00 by tomoron ### ########.fr */
/* Updated: 2024/04/22 19:28:28 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -65,7 +65,7 @@ int get_token_len(char *command, t_env *env)
else if (*command != '\'' && *command != '"')
res++;
else if ((*command == '\'' && in_dquote) || (*command == '"'
&& in_quote))
&& in_quote))
res++;
command++;
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
/* Updated: 2024/04/22 19:18:39 by marde-vr ### ########.fr */
/* Updated: 2024/04/22 19:49:36 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,7 +31,6 @@ void execute_command(t_msh *msh, char **cmd_args)
{
char **env;
//if (cmd_is_forkable_builtin(msh->tokens->value))
if (exec_builtin(msh))
{
free(cmd_args);
@ -43,7 +42,7 @@ void execute_command(t_msh *msh, char **cmd_args)
env = env_to_char_tab(msh->env);
if (env)
{
if(execve(msh->tokens->value, cmd_args, env))
if (execve(msh->tokens->value, cmd_args, env))
perror("execve");
}
ft_free_str_arr(env);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 14:31:13 by tomoron #+# #+# */
/* Updated: 2024/04/21 20:22:09 by tomoron ### ########.fr */
/* Updated: 2024/04/22 19:53:34 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -57,14 +57,12 @@ int set_echoctl(int value)
{
struct termios t_p;
//ft_printf("echoctl value : %d\n",value);
if(!isatty(1))
return(0);
if (!isatty(1))
return (0);
if (tcgetattr(1, &t_p))
return (1);
if (((t_p.c_lflag & ECHOCTL) != 0) == value)
return (0);
//ft_printf("change\n");
if (value)
t_p.c_lflag = t_p.c_lflag | ECHOCTL;
else

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/17 09:12:33 by tomoron #+# #+# */
/* Updated: 2024/04/18 20:49:00 by marde-vr ### ########.fr */
/* Updated: 2024/04/22 19:30:43 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -42,8 +42,8 @@ void sort_wildcards_token(t_token *list)
list = start;
while (list->next)
{
if (wildcard_cmp(list->value, list->next->value,
"zZyYxXwWvVuUtTsSrRqQpPoOnNmMlLkKjJiIhHgGfFeEdDcCbBaA9876543210") > 0)
if (wildcard_cmp(list->value, list->next->value, \
"zZyYxXwWvVuUtTsSrRqQpPoOnNmMlLkKjJiIhHgGfFeEdDcCbBaA9876543210") > 0)
{
swap = list->value;
list->value = list->next->value;

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
/* Updated: 2024/04/22 16:43:31 by tomoron ### ########.fr */
/* Updated: 2024/04/22 19:53:16 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
void free_fds(t_msh *msh)
{
int i;
int i;
if (msh->fds)
{
@ -45,7 +45,6 @@ void free_msh(t_msh *msh)
void ft_exit(t_msh *msh, int exit_code)
{
//ft_printf("exiting");
set_echoctl(msh->echoctl);
free_msh(msh);
exit(exit_code);

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 12:53:29 by tomoron #+# #+# */
/* Updated: 2024/04/19 10:55:20 by tomoron ### ########.fr */
/* Updated: 2024/04/22 19:28:13 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
@ -51,7 +51,7 @@ t_token *get_all_files(DIR *dir, char *wildcard)
t_token *add_token_back(t_token *res, t_token *next)
{
t_token *start;
t_token *start;
if (!res)
return (next);