Commit 160b7731 authored by pêle-mêle's avatar pêle-mêle
Browse files

some adjustments, mainly in main

parent 3ebe0beb
......@@ -21,7 +21,7 @@ SRC += ft_export.c
SRC += ft_unset.c
SRC += ft_env.c
SRC += ft_exit.c
SRC += ft_error.c
vpath %.c $(SRC_DIR)
......
......@@ -6,7 +6,7 @@
# By: mvidal-a <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/12/23 13:17:34 by mvidal-a #+# #+# #
# Updated: 2021/01/10 14:50:54 by mvidal-a ### ########.fr #
# Updated: 2021/01/26 12:41:03 by mvidal-a ### ########.fr #
# #
# **************************************************************************** #
......@@ -73,6 +73,7 @@ SRCS_CUSTOM += ft_lstshift.c
SRCS_CUSTOM += ft_swap.c
SRCS_CUSTOM += strjoin_free.c
SRCS_CUSTOM += ft_varnamelen.c
SRCS_CUSTOM += blank_str.c
VPATH = part_1:part_2:bonus:custom
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* blank_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/26 12:18:10 by mvidal-a #+# #+# */
/* Updated: 2021/01/26 12:18:20 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int blank_str(char *line)
{
while (ft_isspace(*line))
line++;
if (*line == '\0')
return (TRUE);
return (FALSE);
}
......@@ -6,7 +6,7 @@
/* By: mvidal-a <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/04 12:29:06 by mvidal-a #+# #+# */
/* Updated: 2021/01/10 14:50:24 by mvidal-a ### ########.fr */
/* Updated: 2021/01/26 12:18:43 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
......@@ -99,6 +99,7 @@ ssize_t ft_index(char *haystack, char needle);
void ft_swap(void **a, void **b);
int ft_isspace(int c);
int blank_str(char *line);
int ft_isset(char c, char *set);
int ft_strcmp(const char *s1, const char *s2);
char *strjoin_free(char *s1, char const *s2);
......
......@@ -97,8 +97,8 @@ int ft_error(int ret, t_xe *xe)
return (exec_error(ret, xe));
else if (ret > CHILD_EXIT)
err_output(ret);
else
putstr_stderr("ERROR CODE ERROR");// temp
else if (ret != SUCCESS) // temp
putstr_stderr("ERROR CODE ERROR (printed for debug)");// temp
return (SUCCESS);
}
......
......@@ -56,15 +56,10 @@ static int main_loop(t_xe *xe)
{
xe->line = line;
ret = check_syntax(line);
if (ret != SUCCESS)
{
free(line);
return (ft_error(ret, xe));
}
ret = handle_execution(xe, STDIN_FILENO, 0);
if (ret == SUCCESS)
ret = handle_execution(xe, STDIN_FILENO, 0);
free(line);
if (ret != SUCCESS)
return (ft_error(ret, xe));
ret = ft_error(ret, xe);
}
return (ret);
}
......@@ -74,12 +69,11 @@ int exec_env_init(t_xe *xe, char **env_source)
char *shlvl;
char *val;
int tmp;
int ret;
xe->env = dup_str_array(env_source);
xe->child = 0;
if (xe->env == NULL)
ret = MALLOC_ERR;
return (MALLOC_ERR);
shlvl = get_var_value(xe->env, SHLVL_STR, ft_strlen(SHLVL_STR));
// printf("shlvl = %s\n", shlvl);
tmp = ft_atoi(shlvl);
......@@ -91,15 +85,15 @@ int exec_env_init(t_xe *xe, char **env_source)
return (SUCCESS);
}
int read_from_file(t_xe *xe, char **argv)
int read_from_file(t_xe *xe, char *file)
{
int fd;
int flags;
flags = O_RDONLY;
fd = open(argv[1], flags);
if (fd < 0)
return (-1);
fd = open(file, flags);
if (fd == ERROR)
return (-1); // another error code
dup2(fd, STDIN_FILENO);
xe->backup_stdin = dup(STDIN_FILENO);
return (SUCCESS);
......@@ -116,16 +110,15 @@ int main(int argc, char **argv, char **env_source)
if (xe == NULL)
return (ft_exit(MALLOC_ERR, xe));
ft_bzero(xe, sizeof(t_xe));
if (xe == NULL)
ret = MALLOC_ERR;
else if (argc > 2)
if (argc > 2)
ret = ARG_ERR;
else if (argc == 2)
{
//signal_handler(); ?
ret = exec_env_init(xe, env_source);
ret = read_from_file(xe, argv);
if (ret != SUCCESS)
return (ft_exit(ret, xe));
ret = read_from_file(xe, argv[1]);
if (ret != SUCCESS) // needed?
return (ft_exit(ret, xe)); // needed?
while (ret == SUCCESS)
ret = main_loop(xe);
}
......@@ -133,9 +126,9 @@ int main(int argc, char **argv, char **env_source)
{
signal_handler(); // err?
ret = exec_env_init(xe, env_source);
if (ret != SUCCESS)
return (ft_exit(ret, xe));
while (ret != CLEAN_EXIT && ret != FAILURE && ret != CHILD_EXIT)
if (ret != SUCCESS) // needed?
return (ft_exit(ret, xe)); // needed?
while (ret != CLEAN_EXIT && ret != FAILURE && ret != CHILD_EXIT) // why not while (ret == SUCCESS) ?
ret = main_loop(xe);
}
return (ft_exit(ret, xe));
......
......@@ -173,7 +173,7 @@ int parse_input(char **line, char **env, int stat_loc, t_command *command)
if (*line == NULL)
break ;
}
free(machine.cur_arg); //Double free, remove this one ?
free(machine.cur_arg);
command->pipe_flag = machine.pipe_flag;
command->redir_paths = machine.redir_paths;
command->redir_types = machine.redir_types;
......@@ -190,15 +190,6 @@ int parse_input(char **line, char **env, int stat_loc, t_command *command)
return (SUCCESS);
}
int empty_command(char *line)
{
while (ft_isspace(*line))
line++;
if (*line == '\0')
return (TRUE);
return (FALSE);
}
t_command *parse_one_command(t_xe *xe)
{
t_command *cur_command;
......
......@@ -77,15 +77,6 @@ char *parse_quoted_variable(t_state_machine *machine, char *line)
return (line);
}
int blank_str(char *line) // duplicate of empty_command
{
while (ft_isspace(*line))
line++;
if (*line == '\0')
return (TRUE);
return (FALSE);
}
char *parse_variable(t_state_machine *machine, char *line)
{
size_t var_name_len;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment