execution

This commit is contained in:
Loic Deridder
2025-01-30 13:39:46 +01:00
parent 615e91f991
commit 2367f6a42d
4 changed files with 78 additions and 27 deletions

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/28 09:01:33 by lderidde #+# #+# */
/* Updated: 2025/01/28 10:35:06 by lderidde ### ########.fr */
/* Updated: 2025/01/30 10:40:49 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,6 +24,7 @@
# include <stdlib.h>
# include <string.h>
# define EXPRT_INV "not a valid identifier"
// void echo(char *msg, int flag);
int builtin_echo(char **arg, char **envp);
int builtin_exit(char **arg, bool depth);

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 08:22:16 by lderidde #+# #+# */
/* Updated: 2025/01/29 14:11:01 by lderidde ### ########.fr */
/* Updated: 2025/01/30 13:19:40 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -84,7 +84,7 @@ t_ast_n *return_hardcode_ast(char **envp)
head = created_ast_n(_AND, NULL, NULL);
head->env = init_env(envp);
head->left = created_ast_n(_CMD, head, head);
setup_cmd(head->left, "ls", "ls");
setup_cmd(head->left, "export", "export test=");
// head->right = created_ast_n(_AND, head, head);
// head->right->left = created_ast_n(_CMD, head->right, head);
// setup_cmd(head->right->left, "echo", "echo $PWD");
@@ -93,17 +93,17 @@ t_ast_n *return_hardcode_ast(char **envp)
head->right = created_ast_n(_PLINE, head, head);
head->right->pline = malloc(sizeof(t_ast_n *) * 5);
head->right->pline[0] = created_ast_n(_CMD, head->right, head);
setup_cmd(head->right->pline[0], "cat", "cat");
setup_cmd(head->right->pline[0], "export", "export");
head->right->pline[1] = created_ast_n(_CMD, head->right, head);
setup_cmd(head->right->pline[1], "cat", "cat -e");
setup_cmd(head->right->pline[1], "grep", "grep test");
head->right->pline[2] = created_ast_n(_CMD, head->right, head);
setup_cmd(head->right->pline[2], "sdd", "");
setup_cmd(head->right->pline[2], "cat", "cat");
head->right->pline[3] = created_ast_n(_CMD, head->right, head);
setup_cmd(head->right->pline[3], "echo", "echo abc");
head->right->pline[0]->redir = _RED_L;
head->right->pline[0]->infile = "Makefile";
setup_cmd(head->right->pline[3], "cat", "cat");
head->right->pline[1]->redir = _RED_R;
head->right->pline[1]->outfile = "test";
head->right->pline[1]->outfile = "file";
// head->right->pline[3]->redir = _RED_R;
// head->right->pline[3]->outfile = "file";
head->right->pline[4] = NULL;
return (head);
}

View File

@@ -6,20 +6,37 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:20 by lderidde #+# #+# */
/* Updated: 2025/01/28 10:19:06 by lderidde ### ########.fr */
/* Updated: 2025/01/30 13:07:49 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/builtins.h"
static void free_tmp(char **tab)
int is_export_valid(char *str)
{
int i;
char *equal;
i = 0;
while (tab[i])
free(tab[i++]);
free(tab);
if (!ft_isalpha(*str) && str[0] != '_')
return (0);
equal = ft_strchr(str, '=');
if(!equal)
{
while (*(++str))
{
if (!ft_isalnum(*str) && *str != '_')
return (0);
}
return (1);
}
else
{
while (++str < equal)
{
if (!ft_isalnum(*str) && *str != '_')
return (0);
}
return (1);
}
}
char **key_value(char *str)
@@ -33,7 +50,7 @@ char **key_value(char *str)
equal = ft_strchr(str, '=');
tmp[0] = ft_substr(str, 0, equal - str);
if (equal - str == 0)
tmp[2] = ft_strdup("");
tmp[1] = ft_strdup("");
else
tmp[1] = ft_substr(equal, 1, ft_strlen(equal));
tmp[2] = NULL;
@@ -55,7 +72,7 @@ void print_arr(char **envp)
ft_printf("declare -x %s=\"%s\"\n", print[0], print[1]);
else
ft_printf("declare -x %s\n", print[0]);
free_tmp(print);
free_tab(print);
}
}
@@ -97,6 +114,8 @@ int builtin_export(char **arg, t_ast_n *head)
if (count_args(arg) == 1)
return (print_export(head->env));
while (++i < count_args(arg))
{
if (is_export_valid(arg[i]))
{
if (ft_strchr(arg[i], '=') != NULL)
{
@@ -104,10 +123,13 @@ int builtin_export(char **arg, t_ast_n *head)
if (!tmp)
return (1);
set_var_env(tmp[0], tmp[1], head);
free_tmp(tmp);
free_tab(tmp);
}
else
set_var_env(arg[i], NULL, head);
}
else
return (err_msg_cmd("export", arg[i], EXPRT_INV, 1));
}
return (0);
}

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */
/* Updated: 2025/01/29 15:07:44 by lderidde ### ########.fr */
/* Updated: 2025/01/30 12:41:55 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
@@ -285,6 +285,32 @@ int exec_pline(t_ast_n **pline)
return (end_pline(last_pid, pline));
}
// int exec_subsh(t_ast_n *node)
// {
// int status;
// pid_t pid;
//
// pid = fork();
// if (pid == 0)
// {
// handle_redir(node);
// return (execute_command(t_node));
// }
// else if (pid > 0)
// {
// waitpid(pid, &status, 0);
// if (WIFEXITED(status))
// return (WEXITSTATUS(status));
// else
// return (1);
// }
// else
// {
// perror("fork");
// return (1);
// }
// }
int execute_command(t_ast_n *node)
{
int status;
@@ -307,5 +333,7 @@ int execute_command(t_ast_n *node)
}
else if (node->state == _PLINE)
return (exec_pline(node->pline));
// else if (node->state == _SUBSH)
// return (exec_subsh(node->subsh));
return (0);
}