maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
execute.h File Reference
#include "shell.h"
#include "tokens.h"
#include "env.h"
Include dependency graph for execute.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int execute_ast (t_ast *node, t_ms_data *data)
 execute Abstract Syntax Tree
 
int execute (t_ms_data *data)
 execute distribution function
 
char * ft_find_path (char *cmd, t_env *envp)
 function find the full path of the executed command
 
void ft_free_2d_arr (char **arr)
 
int ft_perror (char *str)
 
void close_fds (int in, int out)
 close two file descriptors
 
void handle_io_fd (t_ms_data *data)
 close temporary input/output file descriptors and reset them to -1
 
void handle_std_io (int *std_io, int std_fileno)
 
int handle_shell_variable (t_ast *node, t_ms_data *data)
 
int handle_get_shell_variable (t_ms_data *data, const char *key)
 
void shell_variable_update (t_ms_data *data, int status)
 

Function Documentation

◆ close_fds()

void close_fds ( int  in,
int  out 
)

close two file descriptors

--

  • Parameters
    fdsfile descriptors

Definition at line 28 of file utils_0.c.

29{
30 close(in);
31 close(out);
32}

Referenced by builtin_pipe(), child_process(), execute_child(), and new_process().

Here is the caller graph for this function:

◆ execute()

int execute ( t_ms_data data)

execute distribution function

--

  • Parameters
    dataminishell data struct
  • Returns
    int return status:
  • - 0: success
  • - 1: error

Definition at line 29 of file execute_child.c.

30{
31 size_t i;
32 char *builtin_commands[7];
33 int (*builtin_functions[7])(t_ms_data *);
34
35 builtin_commands[0] = "cd";
36 builtin_commands[1] = "echo";
37 builtin_commands[2] = "env";
38 builtin_commands[3] = "exit";
39 builtin_commands[4] = "export";
40 builtin_commands[5] = "pwd";
41 builtin_commands[6] = "unset";
42 builtin_functions[0] = &builtin_cd;
43 builtin_functions[1] = &builtin_echo;
44 builtin_functions[2] = &builtin_env;
45 builtin_functions[3] = &builtin_exit;
46 builtin_functions[4] = &builtin_export;
47 builtin_functions[5] = &builtin_pwd;
48 builtin_functions[6] = &builtin_unset;
49 if (data->args[0] == NULL)
50 ft_perror("minishel");
51 i = -1;
52 while (++i < sizeof(builtin_commands) / sizeof(char *))
53 if (ft_strcmp(data->args[0], builtin_commands[i]) == 0)
54 return ((*builtin_functions[i])(data));
55 return (new_process(data));
56}
int builtin_exit(t_ms_data *data)
Definition exit.c:62
int builtin_pwd(t_ms_data *data)
Definition pwd.c:24
int builtin_unset(t_ms_data *data)
Definition unset.c:29
int builtin_cd(t_ms_data *data)
Definition cd.c:28
int builtin_echo(t_ms_data *data)
Definition echo.c:22
int builtin_env(t_ms_data *data)
Definition env.c:35
int builtin_export(t_ms_data *data)
Definition export.c:30
int ft_perror(char *str)
static int new_process(t_ms_data *data)
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
char ** args
Definition shell.h:23

References s_ms_data::args, builtin_cd(), builtin_echo(), builtin_env(), builtin_exit(), builtin_export(), builtin_pwd(), builtin_unset(), ft_perror(), ft_strcmp(), and new_process().

Referenced by execute_ast().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute_ast()

int execute_ast ( t_ast node,
t_ms_data data 
)

execute Abstract Syntax Tree

--

  • Parameters
    nodeAbstract Syntax Tree Node
  • Parameters
    dataminishell data struct
  • Returns
    int return status:
  • - 0: success
  • - 1: error

Definition at line 38 of file execute.c.

39{
40 if (!node)
41 return (EXIT_FAILURE);
42 if (node->type == PIPE)
43 return (builtin_pipe(node, data));
44 else if (node->type == REDIR_IN)
45 return (redirect_in(node, data));
46 else if (node->type == REDIR_OUT)
47 return (redirect_out(node, data));
48 else if (node->type == REDIR_APPEND)
49 return (redirect_append(node, data));
50 else if (node->type == REDIR_HEREDOC)
51 return (redirect_here_doc(node, data));
52 else if (node->type == PHRASE)
53 {
54 data->args = node->args;
55 return (execute(data));
56 }
57 return (EXIT_SUCCESS);
58}
int execute(t_ms_data *data)
execute distribution function
#define EXIT_SUCCESS
Definition exit_status.h:16
#define EXIT_FAILURE
Definition exit_status.h:17
int builtin_pipe(t_ast *node, t_ms_data *data)
execute pipe when | is found in the command
Definition pipe.c:35
int redirect_out(t_ast *node, t_ms_data *data)
int redirect_in(t_ast *node, t_ms_data *data)
redirection input in context of executing AST
Definition redirect_in.c:38
int redirect_append(t_ast *node, t_ms_data *data)
redirect append ">>" to the end of the file output
int redirect_here_doc(t_ast *node, t_ms_data *data)
char ** args
Definition tokens.h:52
t_token_type type
Definition tokens.h:51
@ REDIR_IN
Definition tokens.h:34
@ PHRASE
Definition tokens.h:31
@ REDIR_HEREDOC
Definition tokens.h:37
@ PIPE
Definition tokens.h:32
@ REDIR_OUT
Definition tokens.h:35
@ REDIR_APPEND
Definition tokens.h:36

References s_ms_data::args, s_ast::args, builtin_pipe(), execute(), EXIT_FAILURE, EXIT_SUCCESS, PHRASE, PIPE, REDIR_APPEND, REDIR_HEREDOC, REDIR_IN, REDIR_OUT, redirect_append(), redirect_here_doc(), redirect_in(), redirect_out(), and s_ast::type.

Referenced by execute_child(), execute_child(), process_ast_and_io(), redirect_append(), redirect_in(), and redirect_out().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_find_path()

char * ft_find_path ( char *  cmd,
t_env envp 
)

function find the full path of the executed command

--

  • Parameters
    cmdstring from the first argument
  • Parameters
    envpstructure with environment variables
  • Returns
    char* returns the full path of the command TODO: rewrite ft_find_path for working full path when $PATH deleted

Definition at line 32 of file utils_1.c.

33{
34 char **path;
35 char *tmp_full_path;
36 int i;
37
38 path = ft_split(get_env(envp, "PATH"), ':');
39 i = -1;
40 while (path[++i] != NULL)
41 {
42 if (ft_strncmp(cmd, "/", 1) != 0)
43 relative_path_handle(cmd, path[i], &tmp_full_path);
44 else
45 tmp_full_path = ft_strdup(cmd);
46 if (access(tmp_full_path, F_OK) == 0)
47 {
48 ft_free_2d_arr(path);
49 return (tmp_full_path);
50 }
51 free(tmp_full_path);
52 }
53 ft_free_2d_arr(path);
54 return (NULL);
55}
char * get_env(t_env *envp, const char *key)
Definition env.c:59
void ft_free_2d_arr(char **arr)
int ft_strncmp(const char *s1, const char *s2, size_t n)
Definition ft_strncmp.c:24
char ** ft_split(char const *s, char c)
Definition ft_split.c:95
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
static void relative_path_handle(char *cmd, char *path, char **tmp_full_path)
Definition utils_1.c:57

References ft_free_2d_arr(), ft_split(), ft_strdup(), ft_strncmp(), get_env(), and relative_path_handle().

Referenced by child_process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_free_2d_arr()

void ft_free_2d_arr ( char **  arr)

Definition at line 15 of file ft_free_2d_arr.c.

16{
17 int i;
18
19 if (!arr)
20 return ;
21 i = -1;
22 while (arr[++i] != NULL)
23 free(arr[i]);
24 free(arr);
25}

Referenced by ft_find_path(), process_and_reassemble(), and split_loc_vars().

Here is the caller graph for this function:

◆ ft_perror()

int ft_perror ( char *  str)

Definition at line 18 of file exit_status_utils.c.

19{
20 perror (str);
21 exit(EXIT_FAILURE);
22}

References EXIT_FAILURE.

Referenced by builtin_pipe(), execute(), execute_child(), execute_child(), new_process(), and open_tmp_file().

Here is the caller graph for this function:

◆ handle_get_shell_variable()

int handle_get_shell_variable ( t_ms_data data,
const char *  key 
)

Definition at line 47 of file shell_variables.c.

48{
49 char *value;
50 t_env *shell_var;
51
52 shell_var = data->shell_variables;
53 value = get_env(shell_var, key);
54 if (value)
55 {
56 ft_putstr_fd(value, data->std_out);
57 ft_putstr_fd("\n", data->std_out);
58 free(value);
59 return (0);
60 }
61 return (-1);
62}
void ft_putstr_fd(char *s, int fd)
Definition env.h:17
int std_out
Definition shell.h:27
t_env * shell_variables
Definition shell.h:25

References ft_putstr_fd(), get_env(), s_ms_data::shell_variables, and s_ms_data::std_out.

Referenced by handle_shell_variable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_io_fd()

void handle_io_fd ( t_ms_data data)

close temporary input/output file descriptors and reset them to -1

--

Parameters
dataminishell structure

Definition at line 39 of file utils_0.c.

40{
41 if (data->std_in != -1 && data->std_out != -1)
42 {
43 close(data->std_in);
44 close(data->std_out);
45 data->std_in = -1;
46 data->std_out = -1;
47 }
48}
int std_in
Definition shell.h:26

References s_ms_data::std_in, and s_ms_data::std_out.

Referenced by process_ast_and_io().

Here is the caller graph for this function:

◆ handle_shell_variable()

int handle_shell_variable ( t_ast node,
t_ms_data data 
)

Definition at line 64 of file shell_variables.c.

65{
66 if (node->left && node->right == NULL)
67 return (handle_get_shell_variable(data, node->args[0]));
68 return (-1);
69}
int handle_get_shell_variable(t_ms_data *data, const char *key)
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53

References s_ast::args, handle_get_shell_variable(), s_ast::left, and s_ast::right.

Here is the call graph for this function:

◆ handle_std_io()

void handle_std_io ( int *  std_io,
int  std_fileno 
)

Definition at line 50 of file utils_0.c.

51{
52 if (*std_io == -1)
53 *std_io = dup(std_fileno);
54 else
55 dup2(*std_io, std_fileno);
56}

Referenced by child_process(), and new_process().

Here is the caller graph for this function:

◆ shell_variable_update()

void shell_variable_update ( t_ms_data data,
int  status 
)

Definition at line 71 of file shell_variables.c.

72{
73 char *status_str;
74
75 data->exit_status = status;
76 status_str = ft_itoa(status);
77 set_shell_var(&data->shell_variables, "?", status_str);
78 free(status_str);
79}
void set_shell_var(t_env **shell_var, const char *key, const char *value)
char * ft_itoa(int n)
Definition ft_itoa.c:40
int exit_status
Definition shell.h:30

References s_ms_data::exit_status, ft_itoa(), set_shell_var(), and s_ms_data::shell_variables.

Here is the call graph for this function: