Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save #25

Merged
merged 7 commits into from
Feb 17, 2022
Merged

Save #25

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
20220124
  • Loading branch information
Min guk Kang authored and Min guk Kang committed Feb 8, 2022
commit b7b9d27c0437c6113273894d11e867001d9e1e13
60 changes: 46 additions & 14 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,64 @@ typedef struct s_tree {
struct s_tree *right;
} t_tree;

typedef enum s_token {
typedef enum e_token {
BEGIN,
ARG,
CHEVRON_I,
CHEVRON_O,
DOUBLE_CHEVRON_I,
DOUBLE_CHEVRON_O,
INPUT,
OUTPUT_T,
OUTPUT_A,
LIMITER,
PIPE,
OR,
AND,
PARENTHESE_O,
PARENTHESE_C
} t_token;
} e_token;

typedef struct s_redir{
int type;
char *filename;
struct s_redir *in;
struct s_redir *out;
} t_redir;

typedef struct s_cmd {
char **args;
struct s_redir *redir;
} t_cmd;

typedef struct s_pipe {
struct s_cmd *cmd;
struct s_pipe *next;
} t_pipe;

typedef struct s_node {
struct s_cmd *cmd;
struct s_pipe *pipe;
} t_node;

typedef struct s_list {
enum s_token token;
char *content;
struct s_list *next;
struct s_list *prev;
} t_list;
typedef struct s_tree {
int type;
struct s_node *left;
struct s_node *right;
} t_tree;

typedef struct s_cmd
typedef struct s_token {
enum e_token token;
char *content;
struct s_token *next;
struct s_token *prev;
} t_token;

typedef struct s_all {
struct s_redir *redir;
struct s_cmd *cmd;
struct s_pipe *pipe;
struct s_node *node;
struct s_tree *tree;
} t_all;

/*typedef struct s_cmd
{
//char *cmd; je n'ai pas besoin pour l'instant car args[1] peut remplacer celle-ci
char *cmd_path; // command with its path
Expand All @@ -67,7 +99,7 @@ typedef struct s_cmd
int out; // file descriptor en cas de redirection output (!= 1)
char *limiter; // limiter en cas de <<
struct s_cmd *next; // en cas de presence de pipes
} t_cmd;
} t_cmd;*/

/* prototypes */
int minishell(int argc, char *argv[]);
Expand Down
78 changes: 39 additions & 39 deletions srcs/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,100 +76,100 @@ char *put_arg(char *s, int index)
return (result);
}

int redir_input(t_list **lst, char *s, int index)
int redir_input(t_token **tokens, char *s, int index)
{
int i;

i = index;
if (s[i + 1] = '<')
{
(*lst)->token = DOUBLE_CHEVRON_I;
*lst = (*lst)->next;
(*lst)->token = LIMITER;
(*lst)->content = put_arg(s, i + 2);
(*tokens)->token = DOUBLE_CHEVRON_I;
*tokens = (*tokens)->next;
(*tokens)->token = LIMITER;
(*tokens)->content = put_arg(s, i + 2);
i = arg_len(s, i + 2, 0);
}
else
{
(*lst)->token = CHEVRON_I;
*lst = (*lst)->next;
(*lst)->token = INPUT;
(*lst)->content = put_arg(s, i + 1);
(*tokens)->token = CHEVRON_I;
*tokens = (*tokens)->next;
(*tokens)->token = INPUT;
(*tokens)->content = put_arg(s, i + 1);
i = arg_len(s, i + 1, 0);
}
return (i);
}

int redir_output(t_list **lst, char *s, int index)
int redir_output(t_token **tokens, char *s, int index)
{
int i;

i = index;
if (s[i + 1] = '>')
{
(*lst)->token = DOUBLE_CHEVRON_O;
*lst = (*lst)->next;
(*lst)->token = OUTPUT_A;
(*lst)->content = put_arg(s, i + 2);
(*tokens)->token = DOUBLE_CHEVRON_O;
*tokens = (*tokens)->next;
(*tokens)->token = OUTPUT_A;
(*tokens)->content = put_arg(s, i + 2);
i = arg_len(s, i + 2, 0);
}
else
{
(*lst)->token = CHEVRON_O;
*lst = (*lst)->next;
(*lst)->token = OUTPUT_T;
(*lst)->content = put_arg(s, i + 1);
(*tokens)->token = CHEVRON_O;
*tokens = (*tokens)->next;
(*tokens)->token = OUTPUT_T;
(*tokens)->content = put_arg(s, i + 1);
i = arg_len(s, i + 1, 0);
}
return (i);
}

int give_token(t_list **lst, int token, int index)
int give_token(t_token **tokens, int token, int index)
{
(*lst)->token = token;
(*lst)->next = malloc(sizeof(t_list));
*lst = (*lst)->next;
(*tokens)->token = token;
(*tokens)->next = malloc(sizeof(t_token));
*tokens = (*tokens)->next;
return (index);
}

int get_arg(t_list **lst, char *s, int index)
int get_arg(t_token **tokens, char *s, int index)
{
(*lst)->token = ARG;
(*lst)->content = put_arg(s, index);
(*lst)->next = malloc(sizeof(t_list));
*lst = (*lst)->next;
(*tokens)->token = ARG;
(*tokens)->content = put_arg(s, index);
(*tokens)->next = malloc(sizeof(t_token));
*tokens = (*tokens)->next;
return (arg_len(s, index, 0));
}

// a voir = lorsque la commande commence avec un quote

void lexer(char *s, t_list **lst)
void lexer(char *s, t_token **tokens)
{
int i;
t_list *begin;
t_token *begin;

i = 0;
begin = *lst;
begin = *tokens;
while (s[i])
{
while (s[i] == ' ')
i++;
if (is_alpha(s[i]))
i = get_arg(lst, s, i);
i = get_arg(tokens, s, i);
else if (s[i] == '<')
i = redir_input(lst, s, i);
i = redir_input(tokens, s, i);
else if (s[i] == '>')
i = redir_out(lst, s, i);
i = redir_out(tokens, s, i);
else if (s[i] == '|' && s[i + 1] != '|')
i = give_token(lst, PIPE, i);
i = give_token(tokens, PIPE, i);
else if (s[i] == '|' && s[i + 1] == '|')
i = give_token(lst, OR, i);
i = give_token(tokens, OR, i);
else if (s[i] == '&' && s[i + 1] == '&')
i = give_token(lst, AND, i);
i = give_token(tokens, AND, i);
else if (s[i] == '(')
i = give_token(lst, PARENTHESE_O, i);
i = give_token(tokens, PARENTHESE_O, i);
else if (s[i] == ')')
i = give_token(lst, PARENTHESE_C, i);
i = give_token(tokens, PARENTHESE_C, i);
}
*lst = begin;
*tokens = begin;
}
Loading