Skip to content

Commit

Permalink
no easter egg sadge + builtin handler + echo
Browse files Browse the repository at this point in the history
  • Loading branch information
zakissimo committed Jan 3, 2023
1 parent 09e0364 commit 9c74366
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 87 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: zhabri <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/04 08:44:16 by zhabri #+# #+# #
# Updated: 2023/01/02 15:00:09 by zhabri ### ########.fr #
# Updated: 2023/01/03 11:40:34 by zhabri ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -41,7 +41,6 @@ SRCS = minishell.c \
open.c \
add_cmd.c \
heredocs.c \
parse_init_input.c \
pipex.c \
pipex_children.c \
utils_pipex.c \
Expand All @@ -54,7 +53,9 @@ SRCS = minishell.c \
exit.c \
utils_exit.c \
ft_atoll.c \
ft_split_sep.c
ft_split_sep.c \
builtin.c \
echo.c

OBJS = $(SRCS:.c=.o)

Expand Down
46 changes: 46 additions & 0 deletions builtin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/03 10:49:26 by zhabri #+# #+# */
/* Updated: 2023/01/03 11:47:01 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int builtin(char *cmd)
{
int i;
char **cmd_split;
static const char *builtins[8] = \
{"echo", "cd", "export", "env", "unset", "exit", "pwd", NULL};

i = 0;
cmd_split = ft_split_sep(cmd, " \t");
while (cmd_split[0] && builtins[i])
{
if (!ft_strncmp(builtins[i], cmd_split[0], ft_strlen(builtins[i]) + 1))
{
free_tab(cmd_split);
return (i);
}
i++;
}
free_tab(cmd_split);
return (-1);
}

void call_builtin(int built_in, t_cmd *cmd)
{
t_builtin *tab[7];

tab[0] = echo;
tab[5] = exit_child;
tab[6] = pwd;
if (cmd->fd_out != -3)
tab[built_in](cmd->str);
}
46 changes: 46 additions & 0 deletions echo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/03 11:24:58 by zhabri #+# #+# */
/* Updated: 2023/01/03 11:40:06 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft/includes/libft.h"
#include "minishell.h"

static bool is_echo_with_newline(char *arg)
{
int i;

if (arg && arg[0] != '-')
return (true);
i = 1;
while (arg[i] == 'n')
i++;
if (!arg[i])
return (false);
return (true);
}

void echo(char *cmd)
{
int i;
char **cmd_split;
bool newline;

i = 1;
cmd_split = ft_split_sep(cmd, " \t");
newline = is_echo_with_newline(cmd_split[1]);
if (!newline)
i++;
while (cmd_split[i])
ft_putstr_fd(cmd_split[i++], 1);
if (newline)
ft_putchar_fd('\n', 1);
free_tab(cmd_split);
}
24 changes: 6 additions & 18 deletions exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/30 11:56:08 by zhabri #+# #+# */
/* Updated: 2023/01/02 15:02:43 by zhabri ### ########.fr */
/* Updated: 2023/01/03 11:30:29 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -58,14 +58,17 @@ bool exit_parent(void)
return (false);
}

bool exit_child_arg(char **cmd_split)
void exit_child(char *cmd)
{
long long ret;
char **cmd_split;

cmd_split = ft_split_sep(cmd, " \t");
g_glob->exit_ret = 0;
if (cmd_split[1])
{
if (check_exit_error(cmd_split))
return (true);
return ;
if (!is_valid_exit_arg(cmd_split[1]))
{
free_tab(cmd_split);
Expand All @@ -75,19 +78,4 @@ bool exit_child_arg(char **cmd_split)
g_glob->exit_ret = ret;
}
free_tab(cmd_split);
return (false);
}

bool exit_child(char *cmd)
{
char **cmd_split;

cmd_split = ft_split_sep(cmd, " \t");
if (!ft_strncmp(cmd_split[0], "exit", 5))
{
g_glob->exit_ret = 0;
exit_child_arg(cmd_split);
return (true);
}
return (false);
}
20 changes: 19 additions & 1 deletion main_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/23 10:40:36 by zhabri #+# #+# */
/* Updated: 2022/12/29 13:57:23 by zhabri ### ########.fr */
/* Updated: 2023/01/03 11:15:41 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -49,3 +49,21 @@ void init_g_glob(void)
g_glob->sig_int = false;
g_glob->here_doc = false;
}

void clean_exit(int *children_pid)
{
free_null(children_pid);
if (g_glob->cmds)
clear_cmds();
free_null(g_glob->cmds);
if (g_glob->head)
free_op_list();
if (g_glob->envp)
ft_lstclear(g_glob->envp, free);
free_null(g_glob->envp);
free_null(g_glob->head);
free_null(g_glob->input);
free_null(g_glob->minishell_sum);
free_null(g_glob);
rl_clear_history();
}
5 changes: 2 additions & 3 deletions minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/01 11:49:37 by zhabri #+# #+# */
/* Updated: 2022/12/30 12:16:26 by zhabri ### ########.fr */
/* Updated: 2023/01/03 10:47:50 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -81,10 +81,9 @@ void event_loop(void)

int main(int argc, char **argv, char **envp)
{
(void)argc;
init_sig_callbacks(0);
init_g_glob();
if (argc > 1)
init_input(argv[1]);
get_envp(envp);
get_sum(argv[0], &(g_glob->minishell_sum), NULL);
event_loop();
Expand Down
13 changes: 9 additions & 4 deletions minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/27 20:30:45 by zhabri #+# #+# */
/* Updated: 2023/01/02 15:04:44 by zhabri ### ########.fr */
/* Updated: 2023/01/03 11:47:48 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -73,11 +73,16 @@ typedef struct s_glob
char *minishell_sum;
} t_glob;

typedef void t_builtin(char *cmd);

extern t_glob *g_glob;

void echo(char *cmd);
void call_builtin(int built_in, t_cmd *cmd);
int builtin(char *cmd);
bool last_cmd_is_exit(void);
char **ft_split_sep(char const *s, char *sep);
bool exit_child(char *cmd);
void exit_child(char *cmd);
bool exit_parent(void);
bool check_exit_error(char **cmd_split);
void print_exit_num_error(char *cmd);
Expand All @@ -94,7 +99,6 @@ void init_g_glob(void);
void ignore_sig(int sig);
void final_clean_up(void);
void free_null(void *var);
void init_input(char *str);
char *get_first(void);
bool ft_open_out(t_cmd *node, t_token *token);
bool ft_open_in(t_cmd *node, t_token *token);
Expand Down Expand Up @@ -150,6 +154,7 @@ void free_tab_bis(void *t);
void get_sum(char *cmd, char **ret, int *pipes);
char *get_path(char *cmd);
void change_sig_handling(char *cmd, int *pipes);
void pwd(void);
void pwd(char *cmd);
void init_children_pid(int **children_pid, int size);

#endif
33 changes: 0 additions & 33 deletions parse_init_input.c

This file was deleted.

15 changes: 8 additions & 7 deletions pipex.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/21 12:02:33 by brenaudo #+# #+# */
/* Updated: 2023/01/02 15:27:42 by zhabri ### ########.fr */
/* Updated: 2023/01/03 11:47:55 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -31,23 +31,24 @@ int *pipex_loop(void)
{
int pipes[4];
int *children_pid;
bool exit_ret;
int built_in;
t_list *curr;

curr = *g_glob->cmds;
children_pid = NULL;
while (!children_pid)
children_pid = ft_calloc(ft_lstsize(curr) + 1, sizeof(int));
init_children_pid(&children_pid, ft_lstsize(curr) + 1);
create_pipes(pipes);
while (curr)
{
if (curr->content)
{
exit_ret = exit_child(((t_cmd *)curr->content)->str);
if (!exit_ret)
built_in = builtin(((t_cmd *)curr->content)->str);
if (built_in == -1)
pipex_loop_exit(children_pid, curr, pipes);
else
{
children_pid[((t_cmd *)curr->content)->cmd_idx] = -1;
call_builtin(built_in, ((t_cmd *)curr->content));
}
}
curr = curr->next;
}
Expand Down
7 changes: 5 additions & 2 deletions pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/02 11:16:11 by brenaudo #+# #+# */
/* Updated: 2023/01/02 13:33:21 by zhabri ### ########.fr */
/* Updated: 2023/01/03 11:47:31 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

void pwd(void)
void pwd(char *cmd)
{
int size;
char *path;
char *ret;

(void)cmd;
size = 16;
path = NULL;
ret = NULL;
Expand All @@ -32,4 +33,6 @@ void pwd(void)
}
ft_putstr_fd(path, 1);
ft_putchar_fd('\n', 1);
free_null(path);
free_null(ret);
}
Loading

0 comments on commit 9c74366

Please sign in to comment.