This commit is contained in:
Jack Halford 2017-03-20 15:35:39 +01:00
commit 88598b049a
37 changed files with 282 additions and 247 deletions

View file

@ -6,14 +6,14 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ # # By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 by wescande #+# #+# # # Created: 2016/08/29 21:32:58 by wescande #+# #+# #
# Updated: 2017/03/19 15:14:11 by gwojda ### ########.fr # # Updated: 2017/03/20 12:04:31 by wescande ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
NAME = 42sh NAME = 42sh
CC = gcc CC = gcc
FLAGS = -Wall -Wextra -Werror -g FLAGS = -Wall -Wextra -Werror -fvisibility=hidden
D_FLAGS = -g D_FLAGS = -g
DELTA = $$(echo "$$(tput cols)-47"|bc) DELTA = $$(echo "$$(tput cols)-47"|bc)
@ -40,6 +40,7 @@ builtin/builtin_hash.c\
builtin/builtin_history.c\ builtin/builtin_history.c\
builtin/builtin_math.c\ builtin/builtin_math.c\
builtin/builtin_read.c\ builtin/builtin_read.c\
builtin/builtin_return_status.c\
builtin/builtin_setenv.c\ builtin/builtin_setenv.c\
builtin/builtin_unset.c\ builtin/builtin_unset.c\
builtin/builtin_unsetenv.c\ builtin/builtin_unsetenv.c\
@ -302,7 +303,8 @@ NB = $(words $(SRC_BASE))
INDEX = 0 INDEX = 0
all : all :
@make $(NAME) @make -C $(LIBFT_DIR)
@make -j $(NAME)
$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS)
@$(CC) $(OBJS) -o $(NAME) \ @$(CC) $(OBJS) -o $(NAME) \

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */ /* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */
/* Updated: 2017/03/17 18:59:55 by ariard ### ########.fr */ /* Updated: 2017/03/20 12:51:23 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,6 +20,7 @@
# define BT_EXPORT_LP (1 << 0) # define BT_EXPORT_LP (1 << 0)
t_execf *is_builtin(t_process *p); t_execf *is_builtin(t_process *p);
int builtin_return_status(int ret, int status);
int builtin_export(const char *path, char *const av[], char *const envp[]); int builtin_export(const char *path, char *const av[], char *const envp[]);
int builtin_unset(const char *path, char *const av[], char *const envp[]); int builtin_unset(const char *path, char *const av[], char *const envp[]);
int builtin_env(const char *path, char *const argv[], char *const envp[]); int builtin_env(const char *path, char *const argv[], char *const envp[]);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* builtin_read.h :+: :+: :+: */ /* builtin_read.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */ /* Created: 2017/01/20 15:02:39 by jhalford #+# #+# */
/* Updated: 2017/03/15 19:13:26 by jhalford ### ########.fr */ /* Updated: 2017/03/20 15:34:47 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,17 +30,18 @@ typedef struct s_readopt t_readopt;
struct s_read struct s_read
{ {
t_flag opts; t_flag opts;
char **names; char **names;
char delim; char delim;
int nchars; int nchars;
char *prompt; char *prompt;
int timeout; int timeout;
int fd; int fd;
char *input; char *input;
}; };
int builtin_read(const char *path, char *const av[], char *const envp[]); int builtin_read(const char *path, char *const av[],
char *const envp[]);
int bt_read_init(t_read *data, char **av); int bt_read_init(t_read *data, char **av);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */
/* Updated: 2017/03/20 14:05:18 by jhalford ### ########.fr */ /* Updated: 2017/03/20 15:34:20 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -140,6 +140,7 @@ struct s_exec
}; };
int exec_reset(void); int exec_reset(void);
int do_the_muther_forker(t_process *p);
int process_setgroup(t_process *p, pid_t pid); int process_setgroup(t_process *p, pid_t pid);
void process_setsig(void); void process_setsig(void);
void process_reset(t_process *p); void process_reset(t_process *p);

View file

@ -6,7 +6,7 @@
/* By: sbenning <sbenning@student.42.fr> +#+ +:+ +#+ */ /* By: sbenning <sbenning@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/08 16:34:51 by sbenning #+# #+# */ /* Created: 2016/12/08 16:34:51 by sbenning #+# #+# */
/* Updated: 2017/03/15 18:47:56 by jhalford ### ########.fr */ /* Updated: 2017/03/20 12:33:06 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,7 +27,7 @@ typedef struct s_curs t_curs;
** Cursor data : cursor's coordonate and windows size ** Cursor data : cursor's coordonate and windows size
*/ */
struct s_curs struct s_curs
{ {
int co; int co;
int li; int li;
@ -79,6 +79,6 @@ void curs_write(t_curs *curs, char *str, size_t len);
void curs_write_static(t_curs *curs, char *str, size_t len); void curs_write_static(t_curs *curs, char *str, size_t len);
void curs_video_write(t_curs *curs, char *str, size_t len); void curs_video_write(t_curs *curs, char *str, size_t len);
void curs_video_write_static( void curs_video_write_static(
t_curs *curs, char *str, size_t len); t_curs *curs, char *str, size_t len);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */
/* Updated: 2017/03/17 17:24:03 by gwojda ### ########.fr */ /* Updated: 2017/03/20 12:32:15 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -57,9 +57,6 @@
# define CORRUPT 1 # define CORRUPT 1
//# define STR data_singleton()->line.input
//# define POS data_singleton()->line.pos
# define ERROR_CNTL_R 1 # define ERROR_CNTL_R 1
typedef struct s_list_history typedef struct s_list_history

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/03/20 15:10:24 by jhalford ### ########.fr */ /* Updated: 2017/03/20 15:34:41 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -61,9 +61,6 @@ struct s_rvwords
int type; int type;
}; };
/* extern t_rvwords g_rvwords[]; */
/* extern int (*g_lexer[])(t_list **alst, t_lexer *lexer); */
int ft_post_tokenize(t_list **alst, char **str); int ft_post_tokenize(t_list **alst, char **str);
t_token *token_init(); t_token *token_init();
@ -79,7 +76,8 @@ void token_print(t_list *lst);
int reduce_parens(t_list **alst, char *str); int reduce_parens(t_list **alst, char *str);
int bquotes_expand(t_list **alst); int bquotes_expand(t_list **alst);
char *command_getoutput(char *command, char *const av[], char **env, int pipe_mode); char *command_getoutput(char *command);
int command_setoutput(char *const av[], char **env);
int ft_is_delim(char c); int ft_is_delim(char c);
int ft_is_delim_list(char c); int ft_is_delim_list(char c);

View file

@ -6,7 +6,7 @@
# By: jhalford <jack@crans.org> +#+ +:+ +#+ # # By: jhalford <jack@crans.org> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2017/02/07 16:09:36 by jhalford #+# #+# # # Created: 2017/02/07 16:09:36 by jhalford #+# #+# #
# Updated: 2017/03/18 14:15:35 by gwojda ### ########.fr # # Updated: 2017/03/20 12:05:06 by wescande ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -207,7 +207,7 @@ all:
$(NAME): $(OBJ_DIR) $(OBJS) $(NAME): $(OBJ_DIR) $(OBJS)
@$(AR) $(NAME) $(OBJS) @$(AR) $(NAME) $(OBJS)
@ranlib $(NAME) @ranlib $(NAME)
@strip -x $(NAME) @# @strip -x $(NAME)
@printf "\r\033[48;5;15;38;5;25m✅ MAKE $(NAME)\033[0m\033[K\n" @printf "\r\033[48;5;15;38;5;25m✅ MAKE $(NAME)\033[0m\033[K\n"
$(OBJ_DIR)%.o: $(SRC_DIR)%.c | $(OBJ_DIR) $(OBJ_DIR)%.o: $(SRC_DIR)%.c | $(OBJ_DIR)

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* bt_read_term.c :+: :+: :+: */ /* bt_read_term.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */ /* Created: 2017/01/25 16:02:05 by jhalford #+# #+# */
/* Updated: 2017/03/15 21:23:41 by jhalford ### ########.fr */ /* Updated: 2017/03/20 14:23:46 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ struct termios bt_read_term(int init)
return (term); return (term);
} }
int bt_read_terminit(t_read *data) int bt_read_terminit(t_read *data)
{ {
struct termios term; struct termios term;
@ -39,7 +39,7 @@ int bt_read_terminit(t_read *data)
return (0); return (0);
} }
int bt_read_exit(t_read *data) int bt_read_exit(t_read *data)
{ {
struct termios term; struct termios term;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */
/* Updated: 2017/03/16 14:17:57 by jhalford ### ########.fr */ /* Updated: 2017/03/20 12:54:02 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -81,14 +81,14 @@ int builtin_cd(const char *path,
opts = CDOPT_L; opts = CDOPT_L;
i = builtin_cd_opts(av, &opts); i = builtin_cd_opts(av, &opts);
if (!(target = builtin_cd_special(av + i, envp))) if (!(target = builtin_cd_special(av + i, envp)))
return (1); return (builtin_return_status(0, 1));
cwd = getcwd(NULL, 0); cwd = getcwd(NULL, 0);
builtin_setenv(path, (char*[4]){"setenv", "OLDPWD", cwd, NULL}, envp); builtin_setenv(path, (char*[4]){"setenv", "OLDPWD", cwd, NULL}, envp);
free(cwd); free(cwd);
if (chdir(target)) if (chdir(target))
{ {
ft_dprintf(2, CDERR_1, target); ft_dprintf(2, CDERR_1, target);
return (1); return (builtin_return_status(0, 1));
} }
else if (target != av[i]) else if (target != av[i])
ft_printf("%s\n", target); ft_printf("%s\n", target);
@ -97,5 +97,5 @@ int builtin_cd(const char *path,
free(cwd); free(cwd);
if (!ft_strcmp(*(av + i), "-")) if (!ft_strcmp(*(av + i), "-"))
free(target); free(target);
return (0); return (builtin_return_status(0, 0));
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:21:41 by jhalford #+# #+# */ /* Created: 2016/11/28 14:21:41 by jhalford #+# #+# */
/* Updated: 2017/01/20 00:10:31 by ariard ### ########.fr */ /* Updated: 2017/03/20 14:40:00 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,5 +25,5 @@ int builtin_echo(const char *path, char *const av[], char *const envp[])
ft_putchar(' '); ft_putchar(' ');
} }
ft_putchar('\n'); ft_putchar('\n');
return (0); return (builtin_return_status(0, 0));
} }

View file

@ -6,23 +6,24 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
/* Updated: 2017/03/19 17:43:24 by wescande ### ########.fr */ /* Updated: 2017/03/20 15:02:31 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#define US_ENV "env [-i] [-u name] [name=value]... [utility [argument...]]"
static int env_usage(int arg_miss, char c) static int env_usage(int arg_miss, char c)
{ {
if (arg_miss) if (arg_miss)
ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n"); ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n");
else if (c) else if (c)
ft_dprintf(2, "{red}env: illegal option -- %c{eoc}\n", c); ft_dprintf(2, "{red}env: illegal option -- %c{eoc}\n", c);
ft_dprintf(2, "usage: env [-i] [-u name] ... [name=value] ... cmd\n"); ft_dprintf(2, "usage: %s\n", US_ENV);
return (1); return (1);
} }
static void env_freeone(char **env, char *arg) static void env_freeone(char **env, char *arg)
{ {
int i; int i;
char *tmp; char *tmp;
@ -45,7 +46,7 @@ static void env_freeone(char **env, char *arg)
} }
} }
static void env_replace(char ***custom_env, char *arg) static void env_replace(char ***custom_env, char *arg)
{ {
char **arg_split; char **arg_split;
@ -57,7 +58,7 @@ static void env_replace(char ***custom_env, char *arg)
*custom_env = ft_sstradd(*custom_env, arg); *custom_env = ft_sstradd(*custom_env, arg);
} }
static int env_treat_flag(char ***custom_env, char *const *arg[]) static int env_treat_flag(char ***custom_env, char *const *arg[])
{ {
while (*(++*arg)) while (*(++*arg))
{ {
@ -86,18 +87,21 @@ static int env_treat_flag(char ***custom_env, char *const *arg[])
return (0); return (0);
} }
int builtin_env(const char *path, char *const argv[], char *const envp[]) int builtin_env(const char *path,
char *const argv[], char *const envp[])
{ {
char **env; char **env;
pid_t pid;
(void)path; (void)path;
pid = 0;
if (!argv || ft_strcmp(*argv, "env")) if (!argv || ft_strcmp(*argv, "env"))
return (env_usage(0, 0)); return (builtin_return_status(0, env_usage(0, 0)));
env = ft_sstrdup((char **)envp); env = ft_sstrdup((char **)envp);
if (env_treat_flag(&env, &argv)) if (env_treat_flag(&env, &argv))
{ {
ft_sstrfree(env); ft_sstrfree(env);
return (1); return (builtin_return_status(0, 1));
} }
if (!*argv) if (!*argv)
{ {
@ -106,7 +110,7 @@ int builtin_env(const char *path, char *const argv[], char *const envp[])
ft_putchar('\n'); ft_putchar('\n');
} }
else else
command_getoutput(NULL, argv, env, 0); pid = command_setoutput(argv, env);
ft_tabdel(&env); ft_tabdel(&env);
return (0); return (builtin_return_status(pid, 0));
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */
/* Updated: 2017/03/20 11:37:51 by jhalford ### ########.fr */ /* Updated: 2017/03/20 14:17:40 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,10 +30,8 @@ int builtin_exit(const char *path, char *const av[], char *const envp[])
data_singleton()->argv[0]); data_singleton()->argv[0]);
return (0); return (0);
} }
if (av && av[1]) status = (av && av[1]) ?
status = ft_atoi(av[1]); ft_atoi(av[1]) : ft_atoi(ft_getenv(data_singleton()->env, "?"));
else
status = ft_atoi(ft_getenv(data_singleton()->env, "?"));
if (SH_IS_INTERACTIVE(data_singleton()->opts)) if (SH_IS_INTERACTIVE(data_singleton()->opts))
tcsetattr(STDIN, TCSANOW, &data_singleton()->jobc.shell_tmodes); tcsetattr(STDIN, TCSANOW, &data_singleton()->jobc.shell_tmodes);
job_hup_all(); job_hup_all();

View file

@ -6,13 +6,13 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */ /* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */
/* Updated: 2017/03/18 11:46:32 by gwojda ### ########.fr */ /* Updated: 2017/03/20 14:44:02 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static t_cliopts export_opts[] = static t_cliopts g_export_opts[] =
{ {
{'p', NULL, BT_EXPORT_LP, 0, NULL}, {'p', NULL, BT_EXPORT_LP, 0, NULL},
{0, NULL, 0, 0, NULL}, {0, NULL, 0, 0, NULL},
@ -44,25 +44,22 @@ int builtin_export(
(void)envp; (void)envp;
(void)path; (void)path;
data.flag = 0; data.flag = 0;
if (cliopts_get((char**)av, export_opts, &data)) if (cliopts_get((char**)av, g_export_opts, &data))
ft_perror(); ft_perror();
if (data.flag & BT_EXPORT_LP) if (data.flag & BT_EXPORT_LP)
return (bt_export_print()); return (builtin_return_status(0, bt_export_print()));
av = data.av_data; av = data.av_data;
while (*av) while (*av)
{ {
if ((equal = ft_strchr(*av, '='))) equal = ft_strchr(*av, '=');
{ if (equal)
*equal = 0; *equal = 0;
builtin_setenv("internal", (char*[]){"global", *av, equal + 1}, NULL);
}
else else
{
equal = ft_getenv(data_singleton()->local_var, *av); equal = ft_getenv(data_singleton()->local_var, *av);
builtin_setenv("internal", (char*[]){"global", *av, equal}, NULL); equal ? equal++ : equal;
} builtin_setenv("internal", (char*[]){"global", *av, equal}, NULL);
builtin_unsetenv("internal", (char*[]){"local", *av, NULL}, NULL); builtin_unsetenv("internal", (char*[]){"local", *av, NULL}, NULL);
av++; av++;
} }
return (0); return (builtin_return_status(0, 0));
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 16:37:43 by gwojda #+# #+# */ /* Created: 2017/02/18 16:37:43 by gwojda #+# #+# */
/* Updated: 2017/02/18 16:51:43 by gwojda ### ########.fr */ /* Updated: 2017/03/20 14:03:30 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -43,8 +43,8 @@ int builtin_hash(const char *path, char *const av[], char *const envp[])
(void)path; (void)path;
(void)envp; (void)envp;
if (ft_hash_opt(av)) if (!av || ft_hash_opt(av))
return (0); return (builtin_return_status(0, 1));
i = 0; i = 0;
while (i < MAX_HASH) while (i < MAX_HASH)
{ {
@ -52,5 +52,5 @@ int builtin_hash(const char *path, char *const av[], char *const envp[])
ft_hash_lst_print(g_hash[i]); ft_hash_lst_print(g_hash[i]);
++i; ++i;
} }
return (0); return (builtin_return_status(0, 0));
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/24 14:54:53 by gwojda #+# #+# */ /* Created: 2017/01/24 14:54:53 by gwojda #+# #+# */
/* Updated: 2017/02/20 20:30:18 by ariard ### ########.fr */ /* Updated: 2017/03/20 14:04:02 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,7 @@ int builtin_history(const char *path, char *const av[], char *const envp[])
head = data_singleton()->line.list_end; head = data_singleton()->line.list_end;
len = 1; len = 1;
if (!head) if (!head)
return (0); return (builtin_return_status(0, 0));
if (head && !head->str) if (head && !head->str)
head = head->next; head = head->next;
while (head && head->str) while (head && head->str)
@ -34,5 +34,5 @@ int builtin_history(const char *path, char *const av[], char *const envp[])
++len; ++len;
head = head->next; head = head->next;
} }
return (0); return (builtin_return_status(0, 0));
} }

View file

@ -6,26 +6,26 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/17 18:54:00 by ariard #+# #+# */ /* Created: 2017/03/17 18:54:00 by ariard #+# #+# */
/* Updated: 2017/03/17 19:59:42 by ariard ### ########.fr */ /* Updated: 2017/03/20 14:45:09 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
# define MATHERR_0 "math : invalid number of arguments\n" #define MATHERR_0 "math : invalid number of arguments\n"
# define MATHERR_1 "math : invalid variable name\n" #define MATHERR_1 "math : invalid variable name\n"
# define MATHERR_2 "math : invalid operator\n" #define MATHERR_2 "math : invalid operator\n"
# define MATHERR_3 "math : invalid operand\n" #define MATHERR_3 "math : invalid operand\n"
# define MATHERR_4 "math : division by 0\n" #define MATHERR_4 "math : division by 0\n"
static int error_msg(char *msg) static int error_msg(char *msg)
{ {
ft_dprintf(2, "{red}%s{eoc}", msg); ft_dprintf(2, "{red}%s{eoc}", msg);
return (-1); return (1);
} }
static int init_math(char **var, char **value, char **operator, static int init_math(char **var, char **value, char **operator,
char **operand) char **operand)
{ {
*var = NULL; *var = NULL;
*value = NULL; *value = NULL;
@ -38,78 +38,77 @@ static int get_value(char *var, char **value)
{ {
char *temp; char *temp;
char *esc; char *esc;
int ret; int ret;
esc = ft_strnew((ft_strlen(var) >> 3) + 1); esc = ft_strnew((ft_strlen(var) >> 3) + 1);
ret = word_is_assignment((char *[]) {var, (esc + 1)}); ret = word_is_assignment((char *[]) {var, (esc + 1)});
ft_strdel(&esc); ft_strdel(&esc);
if (!ret) if (!ret)
return (error_msg(MATHERR_1)); return (error_msg(MATHERR_1));
temp = ft_sstrstr(data_singleton()->local_var, var); temp = ft_sstrstr(data_singleton()->local_var, var);
if (temp) if (temp)
{ {
temp += ft_strlenchr(temp, '=') + 1; temp += ft_strlenchr(temp, '=') + 1;
*value = ft_strdup(temp); *value = ft_strdup(temp);
if (!(ft_stris(*value, &ft_isdigit))) if (!(ft_stris(*value, &ft_isdigit)))
{ {
ft_strdel(value); ft_strdel(value);
*value = ft_itoa(0); *value = ft_itoa(0);
} }
} }
else else
*value = ft_itoa(0); *value = ft_itoa(0);
return (0); return (0);
} }
static int do_math(char **value, char *operator, char *operand) static int do_math(char **value, char *operator, char *operand)
{ {
long ope1; long ope1;
long ope2; long ope2;
ope1 = ft_atoi(*value); ope1 = ft_atoi(*value);
if (operand) if (operand)
ope2 = ft_atoi(operand); ope2 = ft_atoi(operand);
else else
ope2 = 0; ope2 = 0;
if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0) if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
return (error_msg(MATHERR_4)); return (error_msg(MATHERR_4));
else else
{ {
ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1; ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1;
ope1 = (operator[0] == '-') ? ope1 - ope2 : ope1; ope1 = (operator[0] == '-') ? ope1 - ope2 : ope1;
ope1 = (operator[0] == '/') ? ope1 / ope2 : ope1; ope1 = (operator[0] == '/') ? ope1 / ope2 : ope1;
ope1 = (operator[0] == '*') ? ope1 * ope2 : ope1; ope1 = (operator[0] == '*') ? ope1 * ope2 : ope1;
ope1 = (operator[0] == '%') ? ope1 % ope2 : ope1; ope1 = (operator[0] == '%') ? ope1 % ope2 : ope1;
} }
ft_strdel(value); ft_strdel(value);
*value = ft_itoa(ope1); *value = ft_itoa(ope1);
return (0); return (0);
} }
int builtin_math(const char *path, char *const av[], char *const envp[]) int builtin_math(const char *path, char *const av[], char *const envp[])
{ {
char *var; char *var;
char *value; char *value;
char *operator; char *operator;
char *operand; char *operand;
(void)path; (void)path;
(void)envp; (void)envp;
if (!av || !av[1] || !av[2] || !av[3] || av[4]) if (!av || !av[1] || !av[2] || !av[3] || av[4])
return (error_msg(MATHERR_0)); return (builtin_return_status(0, error_msg(MATHERR_0)));
init_math(&var, &value, &operator, &operand); init_math(&var, &value, &operator, &operand);
var = av[1]; var = av[1];
if (get_value(var, &value) == -1) if (get_value(var, &value))
return (-1); return (builtin_return_status(0, 1));
operator = av[2]; operator = av[2];
if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-' if (ft_strlen(operator) != 1 || !(ft_strchr("+-/*%", operator[0])))
|| operator[0] == '/' || operator[0] == '*' || operator[0] == '%'))) return (builtin_return_status(0, error_msg(MATHERR_2)));
return (error_msg(MATHERR_2)); operand = av[3];
operand = av[3]; if (!ft_stris(operand, &ft_isdigit))
if (!ft_stris(operand, &ft_isdigit)) return (builtin_return_status(0, error_msg(MATHERR_3)));
return (error_msg(MATHERR_3)); if (do_math(&value, operator, operand))
if (do_math(&value, operator, operand) == -1) return (builtin_return_status(0, 1));
return (-1); builtin_setenv("setenv", (char *[]){"local", var, value, 0}, NULL);
builtin_setenv("setenv", (char *[]){"local", var, value, 0}, data_singleton()->local_var); return (builtin_return_status(0, 0));
return (0);
} }

View file

@ -3,14 +3,16 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* builtin_read.c :+: :+: :+: */ /* builtin_read.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */
/* Updated: 2017/03/20 12:41:43 by jhalford ### ########.fr */ /* Updated: 2017/03/20 15:34:56 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#define US_READ "read [-ers] [-u fd] [-t timeout] [-p prompt]"
#define US_READ_1 "[-n nchars] [-d delim] [name ...]"
t_cliopts g_read_opts[] = t_cliopts g_read_opts[] =
{ {
@ -26,8 +28,7 @@ t_cliopts g_read_opts[] =
void bt_read_usage(void) void bt_read_usage(void)
{ {
ft_dprintf(2, "{red}read: usage: read [-ers] [-u fd] [-t timeout]"); ft_dprintf(2, "usage: read %s %s\n", US_READ, US_READ_1);
ft_dprintf(2, " [-p prompt] [-n nchars] [-d delim] [name ...]{eoc}\n");
} }
int bt_read_init(t_read *data, char **av) int bt_read_init(t_read *data, char **av)
@ -40,13 +41,13 @@ int bt_read_init(t_read *data, char **av)
data->timeout = 0; data->timeout = 0;
data->input = NULL; data->input = NULL;
if ((cliopts_get(av, g_read_opts, data))) if ((cliopts_get(av, g_read_opts, data)))
return(ft_perror() ? 2 : 2); return (ft_perror() ? 2 : 2);
if (bt_read_terminit(data) < 0) if (bt_read_terminit(data) < 0)
return (-1); return (-1);
return (0); return (0);
} }
int bt_read_loop(t_read *data) int bt_read_loop(t_read *data)
{ {
int i; int i;
int esc; int esc;
@ -74,7 +75,7 @@ int bt_read_loop(t_read *data)
return (0); return (0);
} }
int bt_read_assign(t_read *data) int bt_read_assign(t_read *data)
{ {
char *input; char *input;
char **names; char **names;
@ -97,7 +98,7 @@ int bt_read_assign(t_read *data)
return (0); return (0);
} }
int builtin_read(const char *path, char *const av[], char *const envp[]) int builtin_read(const char *path, char *const av[], char *const envp[])
{ {
t_read data; t_read data;
int ret; int ret;
@ -117,5 +118,5 @@ int builtin_read(const char *path, char *const av[], char *const envp[])
bt_read_usage(); bt_read_usage();
if (ret != 2) if (ret != 2)
bt_read_exit(&data); bt_read_exit(&data);
return (ret); return (builtin_return_status(0, ret));
} }

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_return_status.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 12:48:04 by wescande #+# #+# */
/* Updated: 2017/03/20 12:49:18 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_return_status(int ret, int status)
{
set_exitstatus(status, 1);
return (ret);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
/* Updated: 2017/03/20 12:57:59 by jhalford ### ########.fr */ /* Updated: 2017/03/20 15:35:09 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -39,10 +39,9 @@ int builtin_setenv(const char *path,
{ {
char ***env; char ***env;
(void)envp;
(void)path; (void)path;
if (!av || !av[0]) if (!av || !av[0])
return (1); return (builtin_return_status(0, 1));
if (ft_strcmp(av[0], "local") == 0) if (ft_strcmp(av[0], "local") == 0)
env = &data_singleton()->local_var; env = &data_singleton()->local_var;
else else
@ -53,6 +52,6 @@ int builtin_setenv(const char *path,
ft_putchar('\n'); ft_putchar('\n');
} }
else else
return (assign_var(av, env)); assign_var(av, env);
return (0); return (envp ? builtin_return_status(0, 0) : 0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 11:43:34 by gwojda #+# #+# */ /* Created: 2017/02/15 11:43:34 by gwojda #+# #+# */
/* Updated: 2017/02/15 11:58:22 by gwojda ### ########.fr */ /* Updated: 2017/03/20 14:27:43 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,5 +14,13 @@
int builtin_unset(const char *path, char *const av[], char *const envp[]) int builtin_unset(const char *path, char *const av[], char *const envp[])
{ {
char *tmp;
char **av_tmp;
tmp = av[0];
av_tmp = (char **)av;
av_tmp[0] = "local";
builtin_unsetenv(path, av, envp);
av_tmp[0] = tmp;
return (builtin_unsetenv(path, av, envp)); return (builtin_unsetenv(path, av, envp));
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */ /* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */
/* Updated: 2017/03/18 11:47:53 by gwojda ### ########.fr */ /* Updated: 2017/03/20 14:40:05 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,5 +35,5 @@ int builtin_unsetenv(const char *path, char *const av[], char *const envp[])
} }
i++; i++;
} }
return (0); return (builtin_return_status(0, 0));
} }

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */ /* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/10/15 13:27:14 by alao #+# #+# */ /* Created: 2016/10/15 13:27:14 by alao #+# #+# */
/* Updated: 2017/03/19 15:34:38 by gwojda ### ########.fr */ /* Updated: 2017/03/20 14:41:04 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,8 @@ static int c_chevron(t_comp *c)
pos = c->ircmd; pos = c->ircmd;
if (pos >= ft_strlen(c->rcmd)) if (pos >= ft_strlen(c->rcmd))
pos = ft_strlen(c->rcmd) - (ft_strlen(data_singleton()->line.input) - pos); pos = ft_strlen(c->rcmd) - (ft_strlen(data_singleton()->line.input)
- pos);
while (pos) while (pos)
{ {
if (c->rcmd[pos] == '<' || c->rcmd[pos] == '>') if (c->rcmd[pos] == '<' || c->rcmd[pos] == '>')

View file

@ -6,27 +6,16 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */
/* Updated: 2017/03/20 10:35:14 by jhalford ### ########.fr */ /* Updated: 2017/03/20 14:57:26 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int do_the_muther_forker(t_process *p) int do_the_muther_forker(t_process *p)
{ {
pid_t pid; pid_t pid;
if (IS_PIPESINGLE(*p) && p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL)
{
if (process_redirect(p))
{
set_exitstatus(1, 1);
return (0);
}
set_exitstatus(p->map.launch(p), 1);
return (0);
}
if ((pid = fork()) == -1) if ((pid = fork()) == -1)
{ {
ft_dprintf(3, "{red}%s: internal fork error{eoc}\n", SHELL_NAME); ft_dprintf(3, "{red}%s: internal fork error{eoc}\n", SHELL_NAME);
@ -34,6 +23,8 @@ int do_the_muther_forker(t_process *p)
} }
else if (pid) else if (pid)
return (pid); return (pid);
if (!p)
return (pid);
DG("START OF FORK"); DG("START OF FORK");
if (process_redirect(p)) if (process_redirect(p))
exit (1); exit (1);
@ -45,13 +36,28 @@ int do_the_muther_forker(t_process *p)
exit(p->map.launch(p)); exit(p->map.launch(p));
} }
static int do_the_fork_if_i_have_to(t_process *p)
{
if (IS_PIPESINGLE(*p)
&& p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL)
{
if (process_redirect(p))
{
set_exitstatus(1, 1);
return (0);
}
return (p->map.launch(p));
}
return (do_the_muther_forker(p));
}
int process_launch(t_process *p) int process_launch(t_process *p)
{ {
pid_t pid; pid_t pid;
DG("p->type=%i", p->type); DG("p->type=%i", p->type);
p->state = PROCESS_RUNNING; p->state = PROCESS_RUNNING;
if (!(pid = do_the_muther_forker(p))) if (!(pid = do_the_fork_if_i_have_to(p)))
{ {
DG("launcher did not fork!"); DG("launcher did not fork!");
process_resetfds(p); process_resetfds(p);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
/* Updated: 2017/03/20 12:58:31 by jhalford ### ########.fr */ /* Updated: 2017/03/20 15:35:33 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,7 +32,6 @@ void set_exitstatus(int status, int override)
} }
} }
astatus = ft_itoa(exitval); astatus = ft_itoa(exitval);
builtin_setenv("setenv", (char*[]){"setenv", "?", astatus, 0}, builtin_setenv("setenv", (char*[]){"setenv", "?", astatus, 0}, NULL);
data_singleton()->env);
ft_strdel(&astatus); ft_strdel(&astatus);
} }

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 19:44:25 by wescande #+# #+# */ /* Created: 2017/03/14 19:44:25 by wescande #+# #+# */
/* Updated: 2017/03/19 17:42:45 by wescande ### ########.fr */ /* Updated: 2017/03/20 15:10:40 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,11 +49,12 @@ static char *manage_command(char *const av_cmd[])
return (command); return (command);
} }
static void execute_command(char *command, char *const av[], char **env) static void execute_command(char *const av[], char **env)
{ {
t_list *token; t_list *token;
t_btree *ast; t_btree *ast;
t_data *data; t_data *data;
char *command;
token = NULL; token = NULL;
ast = NULL; ast = NULL;
@ -61,7 +62,7 @@ static void execute_command(char *command, char *const av[], char **env)
data->env = env; data->env = env;
data->opts &= ~SH_INTERACTIVE; data->opts &= ~SH_INTERACTIVE;
data->opts &= ~SH_OPTS_JOBC; data->opts &= ~SH_OPTS_JOBC;
command = command ? command : manage_command(av); command = manage_command(av);
DG("command is %s", command); DG("command is %s", command);
if (do_lexer_routine(&token, command)) if (do_lexer_routine(&token, command))
{ {
@ -78,27 +79,37 @@ static void execute_command(char *command, char *const av[], char **env)
exit(data_singleton()->parser.state == SUCCESS && ft_exec(&ast) < 0); exit(data_singleton()->parser.state == SUCCESS && ft_exec(&ast) < 0);
} }
char *command_getoutput(char *command, char *const av[], char **env, int pipe_mode) char *command_getoutput(char *command)
{ {
int ret; int ret;
int pid; int pid;
int fds[2]; int fds[2];
char **av;
if (!command && !av) if (!command)
return (NULL); return (NULL);
if (pipe_mode) pipe(fds);
pipe(fds); if (!(pid = do_the_muther_forker(NULL)))
if (!(pid = fork()))
{ {
if (pipe_mode) close(fds[PIPE_READ]);
{ dup2_close(fds[PIPE_WRITE], STDOUT);
close(fds[PIPE_READ]); av = ft_sstradd(NULL, data_singleton()->argv[0]);
dup2_close(fds[PIPE_WRITE], STDOUT); av = ft_sstradd(av, "-c");
} av = ft_sstradd(av, command);
execute_command(command, av, env); execve(data_singleton()->argv[0], av, data_singleton()->env);
exit(1);
} }
waitpid(pid, &ret, WUNTRACED); waitpid(pid, &ret, WUNTRACED);
if (pipe_mode) return (manage_output(fds));
return (manage_output(fds)); }
return (NULL);
int command_setoutput(char *const av[], char **env)
{
int pid;
if (!av)
return (0);
if (!(pid = do_the_muther_forker(NULL)))
execute_command(av, env);
return (pid);
} }

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 17:47:53 by wescande #+# #+# */ /* Created: 2017/02/17 17:47:53 by wescande #+# #+# */
/* Updated: 2017/03/19 17:33:11 by wescande ### ########.fr */ /* Updated: 2017/03/20 15:10:32 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -72,7 +72,7 @@ static char *get_output(char *command)
char *output; char *output;
int len; int len;
if (!(output = command_getoutput(command, NULL, data_singleton()->env, 1))) if (!(output = command_getoutput(command)))
return (NULL); return (NULL);
len = ft_strlen(output); len = ft_strlen(output);
while (output[--len] == '\n') while (output[--len] == '\n')
@ -80,18 +80,6 @@ static char *get_output(char *command)
return (output); return (output);
} }
static char *treat_str(t_bquote *me, char *sta)
{
if (sta && *me->str == '\n'
&& !is_char_esc(me->esc, CH(*me->wk)[0], me->str))
*me->str = ';';
if (sta && *me->str == '\n'
&& is_char_esc(me->esc, CH(*me->wk)[0], me->str))
*me->str = ' ';
return (*me->str == '`' && !sta
&& !is_char_esc(me->esc2, CH(*me->wk)[0], me->str) ? me->str : sta);
}
static int search_bquote(t_bquote *me) static int search_bquote(t_bquote *me)
{ {
char *sta; char *sta;
@ -100,7 +88,8 @@ static int search_bquote(t_bquote *me)
sta = NULL; sta = NULL;
while (*(++me->str)) while (*(++me->str))
{ {
sta = treat_str(me, sta); sta = *me->str == '`' && !sta
&& !is_char_esc(me->esc2, CH(*me->wk)[0], me->str) ? me->str : sta;
if (sta && *me->str == '`' && me->str != sta if (sta && *me->str == '`' && me->str != sta
&& !is_char_esc(me->esc2, CH(*me->wk)[0], me->str)) && !is_char_esc(me->esc2, CH(*me->wk)[0], me->str))
{ {

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 10:43:16 by gwojda #+# #+# */ /* Created: 2017/01/26 10:43:16 by gwojda #+# #+# */
/* Updated: 2017/03/18 14:29:15 by gwojda ### ########.fr */ /* Updated: 2017/03/20 14:09:01 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,7 @@ static void ft_clear_prompt(size_t *pos, size_t srch_pos)
ft_puttermcaps("cd"); ft_puttermcaps("cd");
} }
static int ft_surch_and_realloc(char **str, char **str_srch, static int search(char **str, char **str_srch,
int ret, size_t *srch_pos) int ret, size_t *srch_pos)
{ {
if (!(*str_srch = ft_realloc_imput(*str_srch, ret, *srch_pos))) if (!(*str_srch = ft_realloc_imput(*str_srch, ret, *srch_pos)))
@ -51,7 +51,7 @@ static void ft_give_new_prompt(char *str_srch, size_t srch_pos)
} }
} }
static void ft_modify_str(char *str_srch, size_t srch_pos, char **str, static int ft_modify_str(char *str_srch, size_t srch_pos, char **str,
size_t *pos) size_t *pos)
{ {
ft_clear_prompt(pos, srch_pos); ft_clear_prompt(pos, srch_pos);
@ -63,6 +63,7 @@ static void ft_modify_str(char *str_srch, size_t srch_pos, char **str,
ft_get_next_str(*str, pos); ft_get_next_str(*str, pos);
} }
free(str_srch); free(str_srch);
return (0);
} }
int ft_surch_in_history(char **str, size_t *pos) int ft_surch_in_history(char **str, size_t *pos)
@ -79,9 +80,8 @@ int ft_surch_in_history(char **str, size_t *pos)
ft_give_new_prompt(str_srch, srch_pos); ft_give_new_prompt(str_srch, srch_pos);
ret = 0; ret = 0;
read(0, &ret, sizeof(int)); read(0, &ret, sizeof(int));
if (ft_isprint(ret) && if (ft_isprint(ret) && search(str, &str_srch, ret, &srch_pos) < 0)
ft_surch_and_realloc(str, &str_srch, ret, &srch_pos) < 0) return (-1);
return (-1);
else if (!ft_isprint(ret) && ret == 127 && srch_pos) else if (!ft_isprint(ret) && ret == 127 && srch_pos)
{ {
--srch_pos; --srch_pos;
@ -92,6 +92,5 @@ int ft_surch_in_history(char **str, size_t *pos)
else if (ret != 127 && !ft_isprint(ret)) else if (ret != 127 && !ft_isprint(ret))
break ; break ;
} }
ft_modify_str(str_srch, srch_pos, str, pos); return (ft_modify_str(str_srch, srch_pos, str, pos));
return (0);
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* builtin_fg.c :+: :+: :+: */ /* builtin_fg.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/08 14:30:07 by jhalford #+# #+# */ /* Created: 2017/01/08 14:30:07 by jhalford #+# #+# */
/* Updated: 2017/03/20 12:12:16 by jhalford ### ########.fr */ /* Updated: 2017/03/20 14:42:22 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,7 +27,7 @@ int builtin_fg(const char *path, char *const av[], char *const envp[])
return (-1); return (-1);
} }
jobc = &data_singleton()->jobc; jobc = &data_singleton()->jobc;
job_getrank(&rank); job_getrank(&rank);
if (av[1] ? (id = *av[1]) : 0) if (av[1] ? (id = *av[1]) : 0)
{ {
if ((jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id))) if ((jlist = ft_lst_find(jobc->first_job, &id, job_cmp_id)))

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
/* Updated: 2017/03/20 09:57:09 by jhalford ### ########.fr */ /* Updated: 2017/03/20 14:49:03 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,11 @@ int readline(int has_prompt, char **input)
if (!has_prompt) if (!has_prompt)
data_singleton()->line.prompt_size = 1; data_singleton()->line.prompt_size = 1;
if (!SH_IS_INTERACTIVE(data_singleton()->opts)) if (!SH_IS_INTERACTIVE(data_singleton()->opts))
return ((ret = get_next_line(data_singleton()->fd, input)) >= 0 ? !ret : ret); {
if ((ret = get_next_line(data_singleton()->fd, input)) >= 0)
return (!ret);
return (ret);
}
readline_init(has_prompt); readline_init(has_prompt);
ret = ft_read_stdin(input); ret = ft_read_stdin(input);
if (ret < 0) if (ret < 0)
@ -30,6 +34,7 @@ int readline(int has_prompt, char **input)
ft_putchar('\n'); ft_putchar('\n');
if (has_prompt) if (has_prompt)
ret = ft_history_parsing(has_prompt, input); ret = ft_history_parsing(has_prompt, input);
ft_strdel(&data_singleton()->line.copy_tmp);
if (tcsetattr(0, TCSANOW, ft_save_termios(0)) == -1) if (tcsetattr(0, TCSANOW, ft_save_termios(0)) == -1)
return (-1); return (-1);
return (ret); return (ret);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */ /* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */
/* Updated: 2017/03/18 15:43:07 by gwojda ### ########.fr */ /* Updated: 2017/03/20 14:44:51 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,6 @@ void data_exit(void)
t_data *data; t_data *data;
data = data_singleton(); data = data_singleton();
/* ft_strdel(&data->line.input); */
ft_sstrfree(data->env); ft_sstrfree(data->env);
ft_sstrfree(data->local_var); ft_sstrfree(data->local_var);
ft_sstrfree(data->argv); ft_sstrfree(data->argv);

View file

@ -6,13 +6,13 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/10 11:36:39 by jhalford #+# #+# */ /* Created: 2017/01/10 11:36:39 by jhalford #+# #+# */
/* Updated: 2017/03/16 15:12:53 by jhalford ### ########.fr */ /* Updated: 2017/03/20 14:45:01 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_data *data_singleton() t_data *data_singleton(void)
{ {
static t_data *data = NULL; static t_data *data = NULL;

View file

@ -3,15 +3,16 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* main.c :+: :+: :+: */ /* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Updated: 2017/03/17 10:51:23 by gwojda ### ########.fr */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */
/* Updated: 2017/03/20 14:46:44 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static int do_readline_routine(char **stream) static int do_readline_routine(char **stream)
{ {
int ret; int ret;
int has_prompt; int has_prompt;
@ -55,7 +56,7 @@ static int handle_instruction(t_list **token, t_btree **ast)
return (0); return (0);
} }
int main(int ac, char **av) int main(int ac, char **av)
{ {
int ret; int ret;
t_data *data; t_data *data;
@ -67,7 +68,7 @@ int main(int ac, char **av)
DG("{inv}{bol}{gre}start of shell{eoc}"); DG("{inv}{bol}{gre}start of shell{eoc}");
if (shell_init(ac, av) != 0) if (shell_init(ac, av) != 0)
return (1); return (1);
DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts) ? "ON" : "OFF");
token = NULL; token = NULL;
ast = NULL; ast = NULL;
data = data_singleton(); data = data_singleton();

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
/* Updated: 2017/03/20 11:30:42 by jhalford ### ########.fr */ /* Updated: 2017/03/20 14:54:28 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,7 @@ static t_cliopts g_opts[] =
{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0},
}; };
int get_c_arg(char ***av, t_data *data) int get_c_arg(char ***av, t_data *data)
{ {
if (!av || !*av) if (!av || !*av)
return (1); return (1);
@ -28,7 +28,7 @@ int get_c_arg(char ***av, t_data *data)
return (0); return (0);
} }
static int get_input_fd(t_data *data) static int get_input_fd(t_data *data)
{ {
static int fds[2] = {-1, STDIN}; static int fds[2] = {-1, STDIN};
char *file; char *file;
@ -47,7 +47,8 @@ static int get_input_fd(t_data *data)
ft_printf("{red}%s: %s: No such file or directory\n{eoc}", ft_printf("{red}%s: %s: No such file or directory\n{eoc}",
data->argv[0], file); data->argv[0], file);
else if (S_ISDIR(buf.st_mode)) else if (S_ISDIR(buf.st_mode))
ft_printf("{red}%s: %s: is a directory\n{eoc}", data->argv[0], file); ft_printf("{red}%s: %s: is a directory\n{eoc}", data->argv[0],
file);
else if ((fds[PIPE_READ] = open(file, O_RDONLY | O_CLOEXEC)) < 0) else if ((fds[PIPE_READ] = open(file, O_RDONLY | O_CLOEXEC)) < 0)
ft_printf("{red}%s: %s: No such file or directory\n{eoc}", ft_printf("{red}%s: %s: No such file or directory\n{eoc}",
data->argv[0], file); data->argv[0], file);
@ -115,7 +116,7 @@ int shell_init(int ac, char **av)
if ((data->fd = get_input_fd(data)) < 0) if ((data->fd = get_input_fd(data)) < 0)
return (-1); return (-1);
DG(); DG();
if (SH_IS_INTERACTIVE(data->opts) && interactive_settings() < 0) if (SH_IS_INTERACTIVE(data->opts) && interactive_settings() < 0)
return (-1); return (-1);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/24 18:41:50 by ariard #+# #+# */ /* Created: 2017/02/24 18:41:50 by ariard #+# #+# */
/* Updated: 2017/03/18 15:50:28 by ariard ### ########.fr */ /* Updated: 2017/03/20 14:57:34 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -41,16 +41,16 @@ int add_subshell_cmd(t_btree **ast, t_list **lst)
token = (*lst)->content; token = (*lst)->content;
node = (*ast)->item; node = (*ast)->item;
if ((token->type == SUBSHELL && node->type == SUBSHELL) if ((token->type == SUBSHELL && node->type == SUBSHELL)
|| (token->type == TK_LBRACE && node->type == TK_LBRACE)) || (token->type == TK_LBRACE && node->type == TK_LBRACE))
node->nest++; node->nest++;
if (((token->type == TK_PAREN_CLOSE && node->type == SUBSHELL) if (((token->type == TK_PAREN_CLOSE && node->type == SUBSHELL)
|| (token->type == TK_RBRACE && node->type == TK_LBRACE)) && node->nest > 0) || (token->type == TK_RBRACE && node->type == TK_LBRACE)) && node->nest > 0)
node->nest--; node->nest--;
else if (token->type == TK_PAREN_CLOSE && node->type == SUBSHELL else if (token->type == TK_PAREN_CLOSE && node->type == SUBSHELL
&& node->nest == 0) && node->nest == 0)
return ((node->full = 1)); return ((node->full = 1));
else if (token->type == TK_RBRACE && node->type == TK_LBRACE else if (token->type == TK_RBRACE && node->type == TK_LBRACE
&& node->nest == 0) && node->nest == 0)
return ((node->full = 1)); return ((node->full = 1));
return (add_cmd(&(*ast)->right, lst)); return (add_cmd(&(*ast)->right, lst));
} }

View file

@ -6,13 +6,13 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */
/* Updated: 2017/03/15 16:33:57 by ariard ### ########.fr */ /* Updated: 2017/03/20 14:59:23 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
char *read_state(t_sym current) char *read_state(t_sym current)
{ {
if (current == FNAME) if (current == FNAME)
return ("FNAME"); return ("FNAME");
@ -131,7 +131,7 @@ char *read_state(t_sym current)
if (current == TK_DONE) if (current == TK_DONE)
return ("TK_DONE"); return ("TK_DONE");
if (current == AND_OR_MINOR) if (current == AND_OR_MINOR)
return("AND_OR_MINOR"); return ("AND_OR_MINOR");
if (current == AND_OR_MAJOR) if (current == AND_OR_MAJOR)
return ("AND_OR_MAJOR"); return ("AND_OR_MAJOR");
if (current == TK_COMMAND) if (current == TK_COMMAND)
@ -227,7 +227,6 @@ int ft_read_stack(t_sym *stack)
return (0); return (0);
} }
int ft_show_heredoc_data(t_astnode *node) int ft_show_heredoc_data(t_astnode *node)
{ {
t_redir *redir; t_redir *redir;

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/18 17:13:31 by ariard #+# #+# */ /* Created: 2017/03/18 17:13:31 by ariard #+# #+# */
/* Updated: 2017/03/18 17:15:11 by ariard ### ########.fr */ /* Updated: 2017/03/20 14:57:58 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,4 +19,4 @@ void sym_free(void *data, size_t size)
(void)size; (void)size;
sym = data; sym = data;
free(sym); free(sym);
} }