-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43348f1
commit 848f5f6
Showing
4 changed files
with
169 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,13 @@ | |
/* By: almelo <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/02/11 19:37:10 by almelo #+# #+# */ | ||
/* Updated: 2023/03/04 14:24:46 by almelo ### ########.fr */ | ||
/* Updated: 2023/03/04 16:16:15 by almelo ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "minishell.h" | ||
|
||
static char *get_key(char *env_str) | ||
char *get_key(char *env_str) | ||
{ | ||
char *key; | ||
char *separator; | ||
|
@@ -24,7 +24,7 @@ static char *get_key(char *env_str) | |
return (key); | ||
} | ||
|
||
static char *get_value(char *env_str) | ||
char *get_value(char *env_str) | ||
{ | ||
char *value; | ||
char *separator; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,24 +6,164 @@ | |
/* By: almelo <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/02/25 15:54:04 by almelo #+# #+# */ | ||
/* Updated: 2023/03/04 11:57:22 by almelo ### ########.fr */ | ||
/* Updated: 2023/03/04 18:13:20 by almelo ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "minishell.h" | ||
|
||
//static void deep_free(char **envp) | ||
//{ | ||
// size_t i; | ||
// | ||
// i = 0; | ||
// while (envp[i]) | ||
// { | ||
// free(envp[i]); | ||
// i++; | ||
// } | ||
// free(envp); | ||
//} | ||
int echo(char **argv) | ||
{ | ||
size_t i; | ||
enum e_bool end_nl; | ||
|
||
i = 1; | ||
end_nl = TRUE; | ||
if (ft_strcmp(argv[i], "-n") == 0) | ||
{ | ||
end_nl = FALSE; | ||
i++; | ||
} | ||
while (argv[i]) | ||
{ | ||
ft_putstr_fd(argv[i], STDOUT_FILENO); | ||
ft_putchar_fd(' ', STDOUT_FILENO); | ||
i++; | ||
} | ||
if (end_nl == TRUE) | ||
ft_putchar_fd('\n', STDOUT_FILENO); | ||
return (0); | ||
} | ||
|
||
int cd(char **argv) | ||
{ | ||
if (chdir(argv[1]) == -1) | ||
return (1); | ||
return (0); | ||
} | ||
|
||
int pwd(void) | ||
{ | ||
char *buf; | ||
|
||
buf = getcwd(NULL, 0); | ||
ft_putendl_fd(buf, STDOUT_FILENO); | ||
free(buf); | ||
return (0); | ||
} | ||
|
||
int export(char **argv, t_envl *env_lst) | ||
{ | ||
char *key; | ||
char *value; | ||
|
||
key = get_key(argv[1]); | ||
if (!key) | ||
return (1); | ||
value = get_value(argv[1]); | ||
queue_env(env_lst, new_env(key, value)); | ||
return (0); | ||
} | ||
|
||
static t_env *remove_env(t_envl *env_lst, char *key) | ||
{ | ||
t_env *tmp; | ||
t_env *node; | ||
|
||
printf("%s\n", key); | ||
tmp = env_lst->head; | ||
while (tmp->next) | ||
{ | ||
if (ft_strcmp(tmp->next->key, key) == 0) | ||
{ | ||
node = tmp->next; | ||
tmp->next = tmp->next->next; | ||
return (node); | ||
} | ||
tmp = tmp->next; | ||
} | ||
return (NULL); | ||
} | ||
|
||
int unset(char **argv, t_envl *env_lst) | ||
{ | ||
char *key; | ||
t_env *tmp; | ||
|
||
key = get_key(argv[1]); | ||
tmp = remove_env(env_lst, key); | ||
return (0); | ||
} | ||
|
||
int env(char **envp) | ||
{ | ||
size_t i; | ||
|
||
i = 0; | ||
while (envp[i]) | ||
{ | ||
ft_putendl_fd(envp[i], STDOUT_FILENO); | ||
i++; | ||
} | ||
return (0); | ||
} | ||
|
||
int is_numeric_arg(char *arg) | ||
{ | ||
size_t i; | ||
|
||
i = 0; | ||
while (arg[i]) | ||
{ | ||
if (!ft_isdigit(arg[i])) | ||
return (0); | ||
i++; | ||
} | ||
return (1); | ||
} | ||
|
||
void ft_exit(int argc, char **argv) | ||
{ | ||
int n; | ||
|
||
n = 0; | ||
if (argc == 2 && is_numeric_arg(argv[1])) | ||
n = ft_atoi(argv[1]); | ||
ft_putendl_fd("exit", STDOUT_FILENO); | ||
exit(n); | ||
} | ||
|
||
int get_argc(char **argv) | ||
{ | ||
size_t argc; | ||
|
||
argc = 0; | ||
while (argv[argc]) | ||
argc++; | ||
return (argc); | ||
} | ||
|
||
int handle_builtin(char **argv, char **envp, t_envl *env_lst) | ||
{ | ||
int argc; | ||
|
||
argc = get_argc(argv); | ||
if (ft_strcmp(argv[0], "echo") == 0) | ||
return (echo(argv)); | ||
else if (ft_strcmp(argv[0], "cd") == 0) | ||
return (cd(argv)); | ||
else if (ft_strcmp(argv[0], "pwd") == 0) | ||
return (pwd()); | ||
else if (ft_strcmp(argv[0], "export") == 0) | ||
return (export(argv, env_lst)); | ||
else if (ft_strcmp(argv[0], "unset") == 0) | ||
return (unset(argv, env_lst)); | ||
else if (ft_strcmp(argv[0], "env") == 0) | ||
return (env(envp)); | ||
else if (ft_strcmp(argv[0], "exit") == 0) | ||
ft_exit(argc, argv); | ||
return (-1); | ||
} | ||
|
||
void handle_execution(t_tokenl *token_lst, t_envl *env_lst) | ||
{ | ||
|
@@ -32,22 +172,18 @@ void handle_execution(t_tokenl *token_lst, t_envl *env_lst) | |
char *pathname; | ||
pid_t pid; | ||
|
||
argv = get_next_argv(token_lst); | ||
envp = list_to_envp(env_lst); | ||
pathname = get_pathname(argv, env_lst); | ||
pid = fork(); | ||
if (pid == 0) | ||
argv = get_next_argv(token_lst); | ||
if (handle_builtin(argv, envp, env_lst) == -1) | ||
{ | ||
if (pathname) | ||
pid = fork(); | ||
if (pid == 0) | ||
{ | ||
pathname = get_pathname(argv, env_lst); | ||
if (execve(pathname, argv, envp) == -1) | ||
exit(0); | ||
} | ||
else | ||
exit(0); | ||
wait(&pid); | ||
} | ||
else | ||
wait(&pid); | ||
//free(argv); | ||
//deep_free(envp); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: almelo <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/02/01 23:15:15 by almelo #+# #+# */ | ||
/* Updated: 2023/03/01 22:10:44 by almelo ### ########.fr */ | ||
/* Updated: 2023/03/04 17:53:42 by almelo ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -20,11 +20,11 @@ static void init_lexer_state(t_lexer_state *state, char *input_copy) | |
state->is_quoted = FALSE; | ||
} | ||
|
||
void handle_exit(t_envl *env_lst) | ||
void handle_exit(t_envl *env_lst, int status) | ||
{ | ||
free_env_list(env_lst); | ||
printf("exit\n"); | ||
exit(0); | ||
exit(status); | ||
} | ||
|
||
int main(int argc, char **argv, char **envp) | ||
|
@@ -42,7 +42,7 @@ int main(int argc, char **argv, char **envp) | |
{ | ||
input = readline("minishell$ "); | ||
if (input == NULL) | ||
handle_exit(&env_lst); | ||
handle_exit(&env_lst, 0); | ||
add_history(input); | ||
init_lexer_state(&lexer_state, ft_strdup(input)); | ||
tokenize_input(&token_lst, input, &lexer_state); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: almelo <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/02/01 23:23:55 by almelo #+# #+# */ | ||
/* Updated: 2023/03/04 14:06:02 by almelo ### ########.fr */ | ||
/* Updated: 2023/03/04 16:16:08 by almelo ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -38,6 +38,8 @@ t_env *new_env(void *key, void *value); | |
void queue_env(t_envl *env_lst, t_env *new); | ||
|
||
void create_env_list(t_envl *env_lst, char **envp); | ||
char *get_key(char *env_str); | ||
char *get_value(char *env_str); | ||
|
||
void tokenize_input(t_tokenl *token_lst, char *input, t_lexer_state *state); | ||
|
||
|