minishell/src/minishell.h

125 lines
3.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 15:00/22 by khais #+# #+# */
/* Updated: 2025/04/14 15:00:22 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_H
# define MINISHELL_H
# include "libft.h"
# include "env/env.h"
# include "parser/wordlist/wordlist.h"
# include "parser/wordsplit/wordsplit.h"
# include <sys/stat.h>
# include <fcntl.h>
typedef union u_redirectee
{
int dest; // file descriptor to redirect
t_worddesc *filename; // filename to redirect
} t_redirectee;
typedef enum e_redir_type
{
FT_INVALID_REDIR,
FT_OUTPUT_TRUNC_REDIR,
FT_OUTPUT_APPEND_REDIR,
FT_INPUT_REDIR,
FT_HEREDOC,
} t_redir_type;
typedef struct s_redirect
{
t_redir_type type;
struct s_redirect *next; // next element or NULL.
int source; // fd to be redirected.
int open_flags;
int c_flags; // flags for third arg of open (case O_CREAT).
t_redirectee redirectee; // fd or filename where to redirect source.
char *here_doc_eof; // The here-document limiter if relevant.
} t_redirect;
typedef enum e_cmd_type
{
FT_CONNECTION,
FT_GROUP,
FT_SIMPLE,
} t_cmd_type;
/*
** Fundamentally, this is a tree.
*/
typedef struct s_cmd
{
t_cmd_type type;
/*
** flags will probably be useless to us for now.
*/
int flags;
/*
** Line number the command starts on -> will probably be unused.
*/
int line;
union u_value
{
struct s_connec_cmd *connection;
struct s_group_cmd *group;
struct s_simple_cmd *simple;
} value;
} t_cmd;
typedef enum e_connector
{
FT_INVALID_CONNECTOR,
FT_PIPE,
FT_AND,
FT_OR,
} t_connector;
/*
** This exists to represent pipelines and AND or OR lists.
** does include left associativity.
*/
typedef struct s_connec_cmd
{
t_cmd *first;
t_cmd *second;
t_connector connector;
} t_connec_cmd;
/*
** We do not deal with { list; } grouping. This is therefore roughly
** equivalent to a subshell_com structure in bash. And therefore we
** define this only since it will still be a grouping regarding redirections
** AND require a subshell every time in the case of minishell.
*/
typedef struct s_group_cmd
{
t_cmd *cmd;
t_redirect *redirects; // redirections concerning the whole group.
} t_group_cmd;
typedef struct s_simple_cmd
{
int line; //Probably unused.
t_wordlist *words; //argv in list form
t_redirect *redirections; //redirections to perform
} t_simple_cmd;
typedef struct s_minishell
{
t_env *env;
int lines_read;
int last_return_value;
} t_minishell;
t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back);
#endif