mirror of
https://github.com/Ladebeze66/minishell.git
synced 2026-11-09 13:47:17 +01:00
minishell
This commit is contained in:
83
command/command.c
Executable file
83
command/command.c
Executable file
@@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* command.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/29 21:21:23 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/09 18:55:48 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../minishell.h"
|
||||
|
||||
int execute_builtin_and_free(char **args, t_minishell *shell, int fd)
|
||||
{
|
||||
int status;
|
||||
|
||||
if (ft_strcmp(args[0], "echo") == 0)
|
||||
{
|
||||
status = builtin_echo(args, shell, fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
status = execute_builtin(args, shell);
|
||||
}
|
||||
free_args_array(args);
|
||||
shell->last_exit_status = status;
|
||||
return (status);
|
||||
}
|
||||
|
||||
static int execute_external_and_free(char **args, t_minishell *shell)
|
||||
{
|
||||
pid_t pid;
|
||||
int status;
|
||||
|
||||
status = 0;
|
||||
pid = execute_external_command(args, shell->environ);
|
||||
free_args_array(args);
|
||||
if (pid > 0)
|
||||
{
|
||||
waitpid(pid, &status, WUNTRACED);
|
||||
if (WIFEXITED(status))
|
||||
{
|
||||
shell->last_exit_status = WEXITSTATUS(status);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
shell->last_exit_status = 127;
|
||||
}
|
||||
return (status);
|
||||
}
|
||||
|
||||
static int process_command(t_command *command, t_minishell *shell, int fd)
|
||||
{
|
||||
char **args;
|
||||
int status;
|
||||
|
||||
status = 0;
|
||||
args = build_args_array(command);
|
||||
if (args != NULL)
|
||||
{
|
||||
if (is_builtin_command(command->command, shell))
|
||||
{
|
||||
status = execute_builtin_and_free(args, shell, fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
status = execute_external_and_free(args, shell);
|
||||
}
|
||||
}
|
||||
return (status);
|
||||
}
|
||||
|
||||
void execute_command(t_command *command, t_minishell *shell, int fd)
|
||||
{
|
||||
if (command == NULL || command->command == NULL)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
process_command(command, shell, fd);
|
||||
}
|
||||
80
command/command_array.c
Executable file
80
command/command_array.c
Executable file
@@ -0,0 +1,80 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* command_array.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 11:26:25 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/09 18:56:21 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../minishell.h"
|
||||
|
||||
static char **allocate_args_array(t_command *command)
|
||||
{
|
||||
char **args;
|
||||
|
||||
args = malloc(sizeof(char *) * (command->arg_count + 2));
|
||||
if (!args)
|
||||
{
|
||||
return (NULL);
|
||||
}
|
||||
return (args);
|
||||
}
|
||||
|
||||
static int duplicate_command_arg(char **args, t_command *command, int i)
|
||||
{
|
||||
args[i + 1] = ft_strdup(command->args[i]);
|
||||
if (!args[i + 1])
|
||||
{
|
||||
while (--i >= 0)
|
||||
free(args[i + 1]);
|
||||
free(args[0]);
|
||||
free(args);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
char **build_args_array(t_command *command)
|
||||
{
|
||||
char **args;
|
||||
int i;
|
||||
|
||||
args = allocate_args_array(command);
|
||||
if (!args)
|
||||
return (NULL);
|
||||
args[0] = ft_strdup(command->command);
|
||||
if (!args[0])
|
||||
{
|
||||
free(args);
|
||||
return (NULL);
|
||||
}
|
||||
i = 0;
|
||||
while (i < command->arg_count)
|
||||
{
|
||||
if (!duplicate_command_arg(args, command, i))
|
||||
return (NULL);
|
||||
i++;
|
||||
}
|
||||
args[command->arg_count + 1] = NULL;
|
||||
return (args);
|
||||
}
|
||||
|
||||
void free_args_array(char **args)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (args)
|
||||
{
|
||||
while (args[i] != NULL)
|
||||
{
|
||||
free(args[i]);
|
||||
i++;
|
||||
}
|
||||
free(args);
|
||||
}
|
||||
}
|
||||
83
command/command_utils.c
Executable file
83
command/command_utils.c
Executable file
@@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* command_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 11:03:59 by fgras-ca #+# #+# */
|
||||
/* Updated: 2023/12/10 12:21:17 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../minishell.h"
|
||||
|
||||
void free_command(t_command *command)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!command)
|
||||
return ;
|
||||
free(command->command);
|
||||
if (command->args)
|
||||
{
|
||||
i = 0;
|
||||
while (i < command->arg_count)
|
||||
{
|
||||
free(command->args[i]);
|
||||
i++;
|
||||
}
|
||||
free(command->args);
|
||||
}
|
||||
}
|
||||
|
||||
void reset_command(t_command *command)
|
||||
{
|
||||
if (!command)
|
||||
return ;
|
||||
free_command(command);
|
||||
command->command = NULL;
|
||||
command->args = NULL;
|
||||
command->arg_count = 0;
|
||||
}
|
||||
|
||||
t_command *create_new_command(void)
|
||||
{
|
||||
t_command *new_command;
|
||||
|
||||
new_command = malloc(sizeof(t_command));
|
||||
if (new_command == NULL)
|
||||
return (NULL);
|
||||
new_command->command = NULL;
|
||||
new_command->args = NULL;
|
||||
new_command->arg_count = 0;
|
||||
return (new_command);
|
||||
}
|
||||
|
||||
void add_token_to_command(t_command *command, char *token)
|
||||
{
|
||||
char **new_args;
|
||||
char *new_token;
|
||||
|
||||
if (!token || *token == '\0' || !command)
|
||||
return ;
|
||||
new_token = ft_strdup(token);
|
||||
if (!new_token)
|
||||
return ;
|
||||
if (command->command == NULL)
|
||||
command->command = new_token;
|
||||
else
|
||||
{
|
||||
new_args = ft_realloc(command->args,
|
||||
sizeof(char *) * (command->arg_count + 2),
|
||||
sizeof(char *) * command->arg_count);
|
||||
if (!new_args)
|
||||
{
|
||||
free(new_token);
|
||||
return ;
|
||||
}
|
||||
command->args = new_args;
|
||||
command->args[command->arg_count++] = new_token;
|
||||
command->args[command->arg_count] = NULL;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user