"close #141"
This commit is contained in:
Antoine Riard 2017-03-22 15:25:29 +01:00
commit 5765d495c6
41 changed files with 250 additions and 336 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/21 17:18:57 by ariard ### ########.fr # # Updated: 2017/03/22 14:59:33 by gwojda ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
NAME = 42sh NAME = 42sh
CC = gcc CC = gcc
FLAGS = -Wall -Wextra -Werror -fvisibility=hidden #-fsanitize=address FLAGS = -Wall -Wextra -Werror #-fvisibility=hidden# -fsanitize=address
D_FLAGS = -g D_FLAGS = -g
DELTA = $$(echo "$$(tput cols)-47"|bc) DELTA = $$(echo "$$(tput cols)-47"|bc)
@ -40,7 +40,6 @@ 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\

View file

@ -19,6 +19,18 @@
# define BT_EXPORT_LP (1 << 0) # define BT_EXPORT_LP (1 << 0)
# define BT_ENV_LI (1 << 0)
# define BT_ENV_LU (1 << 1)
struct s_env_data
{
t_flag flag;
char **av_data;
char **custom_env;
};
typedef struct s_env_data t_env_data;
t_execf *is_builtin(t_process *p); t_execf *is_builtin(t_process *p);
int builtin_return_status(int ret, int status); 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[]);
@ -38,6 +50,7 @@ int builtin_history(const char *path, char *const av[], char *const envp[]);
int builtin_hash(const char *path, char *const av[], char *const envp[]); int builtin_hash(const char *path, char *const av[], char *const envp[]);
int builtin_math(const char *path, char *const av[], char *const envp[]); int builtin_math(const char *path, char *const av[], char *const envp[]);
int bt_env_geti(char ***av, t_env_data *data);
int error_msg(char *msg); int error_msg(char *msg);
#endif #endif

View file

@ -131,7 +131,7 @@ struct s_job
struct s_exec struct s_exec
{ {
t_job job; t_job job;
int fd_save[3]; int fd_save[10];
t_flag attrs; t_flag attrs;
int fdin; int fdin;
t_list *op_stack; t_list *op_stack;
@ -140,7 +140,7 @@ struct s_exec
}; };
int exec_reset(void); int exec_reset(void);
int do_the_muther_forker(t_process *p); int process_fork(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

@ -67,7 +67,7 @@ int data_init(int ac, char **av);
void data_exit(void); void data_exit(void);
int get_c_arg(char ***av, t_data *data); int get_c_arg(char ***av, t_data *data);
void content_free(void *data, size_t content_size); /* void content_free(void *data, size_t content_size); */
char *ft_putast(void *node); char *ft_putast(void *node);
void ft_putast2(void *node); void ft_putast2(void *node);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/03 18:04:07 by jhalford #+# #+# */ /* Created: 2016/11/03 18:04:07 by jhalford #+# #+# */
/* Updated: 2017/03/14 21:09:17 by jhalford ### ########.fr */ /* Updated: 2017/03/21 17:52:10 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,4 +22,5 @@ void ft_sstrdel(char **sstr, int index)
sstr[i] = sstr[i + 1]; sstr[i] = sstr[i + 1];
i++; i++;
} }
ft_strdel(sstr + index);
} }

View file

@ -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 (builtin_return_status(0, 1)); return (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))
{ {
SH_ERR(CDERR_1, target); SH_ERR(CDERR_1, target);
return (builtin_return_status(0, 1)); return (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 (builtin_return_status(0, 0)); return (0);
} }

View file

@ -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 (builtin_return_status(0, 0)); return (0);
} }

View file

@ -1,124 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
/* Updated: 2017/03/21 01:14:41 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) #define ENV_USAGE "env [-i] [name=value]... [utility [argument...]]"
#define ENV_NOFILE "env: %s: No such file or directory"
#define ENV_NOPERM "env: %s: Permission denied"
t_cliopts g_env_opts[] =
{ {
if (arg_miss) {'i', NULL, BT_ENV_LI, 0, bt_env_geti},
ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n"); {0, 0, 0, 0, 0},
else if (c) };
ft_dprintf(2, "{red}env: illegal option -- %c{eoc}\n", c);
ft_dprintf(2, "usage: %s\n", US_ENV); int bt_env_geti(char ***av, t_env_data *data)
{
if (!av || !*av || !data)
return (1); return (1);
} while (**av && ft_strchr(**av, '='))
static void env_freeone(char **env, char *arg)
{
int i;
char *tmp;
while (env && *env && (i = -1))
{ {
if (ft_strcmp(*env, arg) == '=' data->custom_env = ft_sstradd(data->custom_env, **av);
&& ft_strlen(arg) == ft_strlenchr(*env, '=')) ++(*av);
{
tmp = *env;
while (*env)
{
*env = *(env + 1);
++env;
} }
ft_strdel(&tmp); --(*av);
return ;
}
++env;
}
}
static void env_replace(char ***custom_env, char *arg)
{
char **arg_split;
if ((arg_split = ft_strsplit(arg, '=')))
{
env_freeone(*custom_env, *arg_split);
ft_tabdel(&arg_split);
}
*custom_env = ft_sstradd(*custom_env, arg);
}
static int env_treat_flag(char ***custom_env, char *const *arg[])
{
char *tmp;
while (*(++*arg))
{
if (!ft_strcmp(**arg, "-i"))
{
tmp = ft_strjoin("PATH=", ft_getenv(*custom_env, "PATH"));
ft_tabdel(custom_env);
*custom_env = ft_sstradd(NULL, tmp);
ft_strdel(&tmp);
}
else if (!ft_strcmp(**arg, "-u"))
{
++*arg;
if (**arg)
env_freeone(*custom_env, **arg);
else
return (env_usage(1, 0));
}
else if (ft_strchr(**arg, '='))
env_replace(custom_env, **arg);
else if (!ft_strcmp(**arg, "--"))
{
++*arg;
return (0); return (0);
} }
else if ((**arg)[0] == '-')
return (env_usage(0, (**arg)[1])); static int bt_env_parse(t_env_data *data, char **av)
else {
return (0); data->flag = 0;
} data->av_data = NULL;
data->custom_env = NULL;
if (cliopts_get(av, g_env_opts, data))
return (1);
return (0); return (0);
} }
int builtin_env(const char *path, int builtin_env(const char *path,
char *const argv[], char *const envp[]) char *const argv[], char *const envp[])
{ {
char **env; t_env_data data;
int status;
pid_t pid; pid_t pid;
struct stat buf;
(void)path; (void)envp;
pid = 0; if (bt_env_parse(&data, (char**)argv))
if (!argv || ft_strcmp(*argv, "env")) return (ft_perror() && SH_ERR("usage: %s", ENV_USAGE) ? 0 : 0);
return (builtin_return_status(0, env_usage(0, 0))); else if (!*data.av_data)
env = ft_sstrdup((char **)envp); return (builtin_setenv(NULL, (char*[]){"setenv", 0}, NULL));
if (env_treat_flag(&env, &argv)) else if ((pid = fork()) == 0)
{ {
ft_sstrfree(env); if (!(path = ft_strchr(data.av_data[0], '/') ?
return (builtin_return_status(0, 1)); ft_strdup(data.av_data[0]) : ft_hash(data.av_data[0]))
|| access(path, F_OK) != 0)
exit (SH_ERR(ENV_NOFILE, data.av_data[0]));
stat(path, &buf);
if (S_ISDIR(buf.st_mode) || access(path, X_OK) != 0)
exit (SH_ERR(ENV_NOPERM, data.av_data[0]));
execve(path, data.av_data, data.custom_env);
} }
if (!*argv) waitpid(pid, &status, 0);
{ ft_sstrfree(data.custom_env);
ft_sstrprint(env, '\n'); tcsetpgrp(STDIN, data_singleton()->jobc.shell_pgid);
if (env) return (0);
ft_putchar('\n');
}
else
pid = command_setoutput(argv, env);
ft_tabdel(&env);
DG("%d", pid);
return (builtin_return_status(pid, 0));
} }

View file

@ -47,7 +47,7 @@ int builtin_export(
if (cliopts_get((char**)av, g_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 (builtin_return_status(0, bt_export_print())); return (bt_export_print());
av = data.av_data; av = data.av_data;
while (*av) while (*av)
{ {
@ -57,9 +57,9 @@ int builtin_export(
else else
equal = ft_getenv(data_singleton()->local_var, *av); equal = ft_getenv(data_singleton()->local_var, *av);
equal ? equal++ : equal; equal ? equal++ : equal;
builtin_setenv("internal", (char*[]){"global", *av, equal}, NULL); builtin_setenv("internal", (char*[]){"export", *av, equal}, NULL);
builtin_unsetenv("internal", (char*[]){"local", *av, NULL}, NULL); builtin_unsetenv("internal", (char*[]){"local", *av, NULL}, NULL);
av++; av++;
} }
return (builtin_return_status(0, 0)); return (0);
} }

View file

@ -44,7 +44,7 @@ int builtin_hash(const char *path, char *const av[], char *const envp[])
(void)path; (void)path;
(void)envp; (void)envp;
if (!av || ft_hash_opt(av)) if (!av || ft_hash_opt(av))
return (builtin_return_status(0, 1)); return (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 (builtin_return_status(0, 0)); return (0);
} }

View file

@ -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 (builtin_return_status(0, 0)); return (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 (builtin_return_status(0, 0)); return (0);
} }

View file

@ -90,19 +90,19 @@ int builtin_math(const char *path, char *const av[], char *const envp[])
(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 (builtin_return_status(0, SH_ERR(MATHERR_0))); return (SH_ERR(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)) if (get_value(var, &value))
return (builtin_return_status(0, 1)); return (1);
operator = av[2]; operator = av[2];
if (ft_strlen(operator) != 1 || !(ft_strchr("+-/*%", operator[0]))) if (ft_strlen(operator) != 1 || !(ft_strchr("+-/*%", operator[0])))
return (builtin_return_status(0, SH_ERR(MATHERR_2))); return (SH_ERR(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, SH_ERR(MATHERR_3))); return (SH_ERR(MATHERR_3));
if (do_math(&value, operator, operand)) if (do_math(&value, operator, operand))
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}, NULL);
return (builtin_return_status(0, 0)); return (0);
} }

View file

@ -11,6 +11,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#define US_READ "read [-ers] [-u fd] [-t timeout] [-p prompt]" #define US_READ "read [-ers] [-u fd] [-t timeout] [-p prompt]"
#define US_READ_1 "[-n nchars] [-d delim] [name ...]" #define US_READ_1 "[-n nchars] [-d delim] [name ...]"
@ -26,11 +27,6 @@ t_cliopts g_read_opts[] =
{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0},
}; };
void bt_read_usage(void)
{
SH_ERR("usage: read %s %s\n", US_READ, US_READ_1);
}
int bt_read_init(t_read *data, char **av) int bt_read_init(t_read *data, char **av)
{ {
data->opts = 0; data->opts = 0;
@ -115,8 +111,8 @@ int builtin_read(const char *path, char *const av[], char *const envp[])
if (ret == -1) if (ret == -1)
exit(1); exit(1);
if (ret != 0) if (ret != 0)
bt_read_usage(); SH_ERR("usage: read %s %s\n", US_READ, US_READ_1);
if (ret != 2) if (ret != 2)
bt_read_exit(&data); bt_read_exit(&data);
return (builtin_return_status(0, ret)); return (ret);
} }

View file

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

View file

@ -12,7 +12,7 @@
#include "minishell.h" #include "minishell.h"
#define SETERR_0 "setenv: invalid variable name" #define SETERR_0 "%s: %s: invalid variable name"
static int assign_var(char *const av[], char ***env) static int assign_var(char *const av[], char ***env)
{ {
@ -44,8 +44,9 @@ int builtin_setenv(const char *path,
int ret; int ret;
(void)path; (void)path;
(void)envp;
if (!av || !av[0]) if (!av || !av[0])
return (builtin_return_status(0, 1)); return (1);
env = (ft_strcmp(av[0], "local") == 0) ? &data_singleton()->local_var : env = (ft_strcmp(av[0], "local") == 0) ? &data_singleton()->local_var :
&data_singleton()->env; &data_singleton()->env;
if (!av[1]) if (!av[1])
@ -59,8 +60,8 @@ int builtin_setenv(const char *path,
ret = word_is_assignment((char *[]){av[1], (esc + 1)}); ret = word_is_assignment((char *[]){av[1], (esc + 1)});
ft_strdel(&esc); ft_strdel(&esc);
if (!ret && ft_strcmp(av[1], "?")) if (!ret && ft_strcmp(av[1], "?"))
return (SH_ERR(SETERR_0)); return (SH_ERR(SETERR_0, av[0], av[1]));
assign_var(av, env); assign_var(av, env);
} }
return (envp ? builtin_return_status(0, 0) : 0); return (0);
} }

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/21 17:40:31 by jhalford ### ########.fr */ /* Updated: 2017/03/21 17:51:37 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,5 +35,5 @@ int builtin_unsetenv(const char *path, char *const av[], char *const envp[])
} }
i++; i++;
} }
return (builtin_return_status(0, 0)); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/09 16:54:59 by gwojda #+# #+# */ /* Created: 2017/03/09 16:54:59 by gwojda #+# #+# */
/* Updated: 2017/03/21 14:47:18 by gwojda ### ########.fr */ /* Updated: 2017/03/22 15:14:41 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ void c_seek_abs_path(t_comp *c, char *current_word)
char *tmp; char *tmp;
int len; int len;
len = ft_strrchr(c->rcmd, '/') - current_word + 1; len = ft_strrchr(current_word, '/') - current_word + 1;
if (len < 0) if (len < 0)
return ; return ;
if (c->cpath) if (c->cpath)

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/09 15:50:24 by gwojda #+# #+# */ /* Created: 2017/03/09 15:50:24 by gwojda #+# #+# */
/* Updated: 2017/03/16 08:28:19 by alao ### ########.fr */ /* Updated: 2017/03/22 12:29:40 by alao ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,14 +38,15 @@ int c_seek_env(t_comp *c, char *current_word)
{ {
char **env; char **env;
int i; int i;
int m_len;
i = 0; i = 0;
env = data_singleton()->env; env = data_singleton()->env;
c->match = ft_strdupi_w(current_word + 1); c->match = ft_strdupi_w(current_word + 1);
m_len = ft_strlen(c->match);
while (env[i]) while (env[i])
{ {
if (!ft_strncmp(c->match, env[i], ft_strlen(c->match)) && if (!ft_strncmp(c->match, env[i], m_len) && env[i][m_len] != '=')
env[i][ft_strlen(c->match)] != '=')
c_addnode(c, ft_strndup(env[i], ft_strchr(env[i], '=') - env[i])); c_addnode(c, ft_strndup(env[i], ft_strchr(env[i], '=') - env[i]));
++i; ++i;
} }

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */ /* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 13:31:21 by alao #+# #+# */ /* Created: 2017/01/09 13:31:21 by alao #+# #+# */
/* Updated: 2017/03/21 14:53:21 by gwojda ### ########.fr */ /* Updated: 2017/03/22 12:57:02 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -88,7 +88,7 @@ int c_seek_files(t_data *s, t_comp *c, char *current_word)
if (c->rcmd[0] == '.') if (c->rcmd[0] == '.')
{ {
c->cpath = ft_strdup("./"); c->cpath = ft_strdup("./");
c->match = ft_strdup("."); c->match = ft_strdup(current_word);
} }
if (c->cpath == NULL) if (c->cpath == NULL)
{ {

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */ /* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 11:21:16 by alao #+# #+# */ /* Created: 2017/01/09 11:21:16 by alao #+# #+# */
/* Updated: 2017/03/21 11:39:22 by gwojda ### ########.fr */ /* Updated: 2017/03/22 15:12:03 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,35 +6,12 @@
/* 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/20 18:14:35 by gwojda ### ########.fr */ /* Updated: 2017/03/22 15:13:12 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
/*
** Failsafe by checking if the nearby char are not a < or > for aggregation.
*/
static int c_chevron(t_comp *c)
{
size_t pos;
pos = c->ircmd;
if (pos >= ft_strlen(c->rcmd))
pos = ft_strlen(c->rcmd) - (ft_strlen(data_singleton()->line.input)
- pos);
while (pos)
{
if (c->rcmd[pos] == '<' || c->rcmd[pos] == '>')
return (1);
--pos;
}
if (c->rcmd[pos] == '<' || c->rcmd[pos] == '>')
return (1);
return (0);
}
/* /*
** Seek the current word. ** Seek the current word.
*/ */
@ -42,16 +19,20 @@ static int c_chevron(t_comp *c)
static char *c_current_words(t_comp *c) static char *c_current_words(t_comp *c)
{ {
size_t pos; size_t pos;
char *str;
pos = c->ircmd; (void)c;
if (pos >= ft_strlen(c->rcmd)) pos = data_singleton()->line.pos;
pos = ft_strlen(c->rcmd) - str = data_singleton()->line.input;
(ft_strlen(data_singleton()->line.input) - pos + 1); DG("pos = \"%zu\"", pos);
while (pos && c->rcmd[pos] != ' ') while (pos && str[pos] != ' ' && str[pos] != '<'
&& str[pos] != '>' && str[pos] != '\n' && str[pos] != ';')
--pos; --pos;
if (c->rcmd[pos] == ' ') if (str[pos] == ' ' || str[pos] == '<'
|| str[pos] == '>' || str[pos] == '\n' || str[pos] == ';')
++pos; ++pos;
return (c->rcmd + pos); DG("pos = \"%zu\"", pos);
return (str + pos);
} }
/* /*
@ -62,12 +43,14 @@ int c_matching(t_data *s, t_comp *c)
{ {
char *current_word; char *current_word;
DG("c->rcmd = \"%s\"", c->rcmd);
current_word = c_current_words(c); current_word = c_current_words(c);
DG("current_word = \"%s\"", current_word);
if (ft_strchr(c->rcmd, '/')) if (ft_strchr(c->rcmd, '/'))
c_seek_abs_path(c, current_word); c_seek_abs_path(c, current_word);
else if (ft_strchr(c->rcmd, '$')) else if (ft_strchr(c->rcmd, '$'))
c_seek_env(c, current_word); c_seek_env(c, current_word);
else if (c->rcmd[0] != '.' && !(ft_strchr(c->rcmd, ' ')) && !c_chevron(c)) else if (c->rcmd[0] != '.' && !(ft_strchr(c->rcmd, ' ')))
c_seek_binary(s, c); c_seek_binary(s, c);
else else
c_seek_files(s, c, current_word); c_seek_files(s, c, current_word);

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 17:20:01 by gwojda #+# #+# */ /* Created: 2017/03/08 17:20:01 by gwojda #+# #+# */
/* Updated: 2017/03/21 13:40:42 by gwojda ### ########.fr */ /* Updated: 2017/03/22 14:51:44 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,16 +16,14 @@
** Recreate the command from the globbing module responds. ** Recreate the command from the globbing module responds.
*/ */
static void c_replace_globbing(char **glob, size_t start) static void c_replace_globbing(char **glob, size_t start, size_t pos)
{ {
char *ref_next; char *ref_next;
char *ref_mid; char *ref_mid;
char *ref_prev; char *ref_prev;
char *str; char *str;
size_t pos;
str = data_singleton()->line.input; str = data_singleton()->line.input;
pos = data_singleton()->line.pos;
while (str[pos] && str[pos] != ' ') while (str[pos] && str[pos] != ' ')
++pos; ++pos;
while (str[pos] && str[pos] == ' ') while (str[pos] && str[pos] == ' ')
@ -103,7 +101,7 @@ int c_glob_matching(void)
if ((pos = c_glob_pos(NULL, 0)) == -1) if ((pos = c_glob_pos(NULL, 0)) == -1)
return (0); return (0);
current_word = ft_strdupi_w(str + pos); current_word = ft_strdupi_w(str + pos);
if (current_word[0] == '$') if (current_word[0] == '$' || current_word[0] == '~')
{ {
free(current_word); free(current_word);
return (0); return (0);
@ -113,6 +111,6 @@ int c_glob_matching(void)
ss_glob = glob(current_word, glob_echap, glob_echap, 1); ss_glob = glob(current_word, glob_echap, glob_echap, 1);
if (c_check_glob(ss_glob, current_word, glob_echap, pos)) if (c_check_glob(ss_glob, current_word, glob_echap, pos))
return (0); return (0);
c_replace_globbing(ss_glob, pos); c_replace_globbing(ss_glob, pos, data_singleton()->line.pos);
return (1); return (1);
} }

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */ /* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 12:03:30 by alao #+# #+# */ /* Created: 2017/02/15 12:03:30 by alao #+# #+# */
/* Updated: 2017/03/21 15:03:37 by gwojda ### ########.fr */ /* Updated: 2017/03/22 12:25:26 by alao ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -48,25 +48,27 @@ static int c_refresh_match(t_comp *c, long int keypress)
int c_rematch(t_comp *c, long int keypress) int c_rematch(t_comp *c, long int keypress)
{ {
t_data *s;
s = data_singleton();
if (ft_isascii(keypress)) if (ft_isascii(keypress))
{ {
c->isrematch = 1; c->isrematch = 1;
c_term_clear(c); c_term_clear(c);
c_refresh_match(c, keypress); c_refresh_match(c, keypress);
c_clear_lst(c); c_clear_lst(c);
c_matching(data_singleton(), c); c_matching(s, c);
if (data_singleton()->comp && c->lst == NULL) if (s->comp && c->lst == NULL)
c_clear(data_singleton()); c_clear(s);
else if (data_singleton()->comp && c->lst == c->lst->next) else if (s->comp && c->lst == c->lst->next)
return (1); return (1);
ft_print(keypress, &data_singleton()->line.input, ft_print(keypress, &s->line.input, &s->line.pos);
&data_singleton()->line.pos);
return (1); return (1);
} }
else else
{ {
c_term_clear(c); c_term_clear(c);
c_clear(data_singleton()); c_clear(s);
} }
return (0); return (0);
} }

View file

@ -6,28 +6,31 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */ /* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/10/11 10:44:40 by alao #+# #+# */ /* Created: 2016/10/11 10:44:40 by alao #+# #+# */
/* Updated: 2017/03/21 11:22:03 by gwojda ### ########.fr */ /* Updated: 2017/03/22 12:14:53 by alao ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "completion.h" #include "completion.h"
/* /*
** Clear the previous list from the screen and restore the same position. ** Clear the screen and restore cursor position.
**
** First thing is the move the cursor down (do), clear the terminal (cd), back
** up (up) and then move back in position.
*/ */
void c_term_clear(t_comp *c) void c_term_clear(t_comp *c)
{ {
int i; int i;
int lcmd; int lcmd;
t_data *s;
s = data_singleton();
ft_putstr(tgetstr("do", NULL)); ft_putstr(tgetstr("do", NULL));
ft_putstr(tgetstr("cd", NULL)); ft_putstr(tgetstr("cd", NULL));
ft_putstr(tgetstr("up", NULL)); ft_putstr(tgetstr("up", NULL));
i = 0; i = 0;
lcmd = 0; lcmd = (c->rcmd) ? ft_nb_last_line(s->line.input, s->line.pos) : 0;
lcmd = (c->rcmd) ? ft_nb_last_line(data_singleton()->line.input,
data_singleton()->line.pos) : 0;
while (i < lcmd) while (i < lcmd)
{ {
ft_putstr(tgetstr("nd", NULL)); ft_putstr(tgetstr("nd", NULL));
@ -36,8 +39,12 @@ void c_term_clear(t_comp *c)
} }
/* /*
** Move the terminal up by the number of line needed and move it back up to ** Restore cursor position.
** the original position. **
** Move the cursor up as needed (up) then to the beginning of the line (cr).
** The value check if the current print is a rolling list (terminal too small to
** print the whole list at once). If so, m_size is used or c_line.
** The cursor is then moved to the right (nd) by restore the position.
*/ */
void c_term_mv_back(t_comp *c) void c_term_mv_back(t_comp *c)
@ -45,7 +52,9 @@ void c_term_mv_back(t_comp *c)
int i; int i;
int lcmd; int lcmd;
int value; int value;
t_data *s;
s = data_singleton();
i = 0; i = 0;
if (c->c_line > c->win_y) if (c->c_line > c->win_y)
value = c->m_size; value = c->m_size;
@ -58,8 +67,7 @@ void c_term_mv_back(t_comp *c)
} }
ft_putstr(tgetstr("cr", NULL)); ft_putstr(tgetstr("cr", NULL));
i = 0; i = 0;
lcmd = (c->rcmd) ? ft_nb_last_line(data_singleton()->line.input, lcmd = (c->rcmd) ? ft_nb_last_line(s->line.input, s->line.pos) : 0;
data_singleton()->line.pos) : 0;
while (i < lcmd) while (i < lcmd)
{ {
ft_putstr(tgetstr("nd", NULL)); ft_putstr(tgetstr("nd", NULL));
@ -68,8 +76,13 @@ void c_term_mv_back(t_comp *c)
} }
/* /*
** Move the terminal down by the number of line needed and move it back up to ** Make room for the list.
** the first line under the prompt **
** Move the cursor down as needed (do) and clear them (cd).
** The value check if the current print is a rolling list (terminal too small to
** print the whole list at once). If so, m_size is used or c_line.
** The cursor is then moved back up to the line just below where it was to begin
** printing the list.
*/ */
void c_term_mv_down(t_comp *c) void c_term_mv_down(t_comp *c)
@ -101,6 +114,8 @@ void c_term_mv_down(t_comp *c)
} }
/* /*
** Retrieve new terminal size.
**
** If the terminal has changed in size, the function will refresh these values ** If the terminal has changed in size, the function will refresh these values
** and clear the previous print list. ** and clear the previous print list.
*/ */

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */ /* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/09/20 14:50:33 by alao #+# #+# */ /* Created: 2016/09/20 14:50:33 by alao #+# #+# */
/* Updated: 2017/03/16 09:17:11 by alao ### ########.fr */ /* Updated: 2017/03/22 12:00:27 by alao ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
/* /*
** Function to select the next item in the list if it has already been created ** Function to select the next item in the list if it has already been created
** and if the keypressed is tab. ** and if the key pressed is tab.
*/ */
static void c_next_item(t_comp *c) static void c_next_item(t_comp *c)
@ -29,11 +29,20 @@ static void c_next_item(t_comp *c)
} }
/* /*
** If the list is empty after the parsing, all memory is cleared. ** Once the completion has been processed, this is the return point.
** if it detect a single item list, the command is directly updated. ** The three condition are as follow:
** If none of the above behavior is right, the terminal is dropped down by ** - If the completion list is empty (NULL), the screen is cleared and (1) is
** comp->c_line then the list is printed. Once that done, the previous position ** returned.
** of the cursor is restored. ** - If the list contain only one element, c_updater() is called to update the
** string in the struct data then (1) is returned.
** - If the list contain multiple element that mean the completion is not yet
** finished so the c_term_mv_down() drop the terminal to make room for the
** list to be printed using c_printer() and the position is restored by
** calling c_term_mv_back(). (0) s returned because the command doesn't need
** an update to be done.
**
** RETURN VALUE :
** If the function return (1), the line edition module will update the command.
*/ */
int c_dispatcher(t_data *s) int c_dispatcher(t_data *s)
@ -60,17 +69,29 @@ int c_dispatcher(t_data *s)
/* /*
** Autocompletion feature. ** Autocompletion feature.
** **
** If the structure of the completion (later called comp) doesn't exist, the ** The autocompletion module work like the one found in ZSH. Meaning you can
** select the element in a list and complet the command. You can also move in
** it using arrows. It can rematch and update the list of choice with a new
** input from the user. The globbing is also supported. It will also complete
** path (adding /) and solve dots path.
** The completion module work for relative, absolute path and binary from the
** PATH. The env completion is also supported ($[tab]).
**
** The module start by checking if a globbing should be done using
** c_glob_matching(). If so, (1) is returned which trigger an update.
** If the completion structure (later called comp) doesn't exist, the
** function will check for a few things. If the current position is zero, it ** function will check for a few things. If the current position is zero, it
** will return immediately. Same thing apply if the current position of the ** will return immediately. Same thing apply if the current position of the
** cursor is not a space. If those condition are not met the comp struct is ** cursor is not a space. If those condition are not met the comp struct is
** created using c_init(). ** created using c_init().
** **
** If the comp struct already exist at the call of the function, it will check ** If the comp struct already exist at the call of the function, the first
** which key has been pressed. If the tab key has been used, that mean an other ** thing done is checking if the terminal have been resized between call to
** item should be selected. This is done using c_next_item(). If the keypress ** update the corresponding var. Then it will check which key has been pressed.
** is not tab, c_gtfo() is called to know if the command should be updated or **
** simply delete all the memory. ** If the tab key has been used, that mean an other item should be selected.
** This is done using c_next_item(). If the keypress is not tab, c_keypress()
** is called to process the new key.
** **
** The c_dispatcher() is called which will print or update the command ** The c_dispatcher() is called which will print or update the command
** depending on their respective condition. ** depending on their respective condition.
@ -79,14 +100,15 @@ int c_dispatcher(t_data *s)
int completion(long int keypress) int completion(long int keypress)
{ {
t_data *s; t_data *s;
int pos;
s = data_singleton(); s = data_singleton();
pos = s->line.pos;
if (c_glob_matching()) if (c_glob_matching())
return (1); return (1);
if (s->comp == NULL) if (s->comp == NULL)
{ {
if ((s->line.pos == 0) || (s->line.input[s->line.pos] != ' ' && if (!pos || (s->line.input[pos] != ' ' && s->line.input[pos] != '\0'))
s->line.input[s->line.pos] != '\0'))
return (0); return (0);
c_init(s, keypress); c_init(s, keypress);
if (s->comp == NULL) if (s->comp == NULL)

View file

@ -38,9 +38,8 @@ int exec_leaf(t_btree **ast)
if (p.type >= PROCESS_MAX) if (p.type >= PROCESS_MAX)
return (1); return (1);
p.map = g_process_map[p.type]; p.map = g_process_map[p.type];
if (!(process_launch(&p))) if (process_launch(&p))
{ {
DG("check");
job_addprocess(&p); job_addprocess(&p);
if (IS_PIPEEND(p)) if (IS_PIPEEND(p))
{ {
@ -51,7 +50,5 @@ int exec_leaf(t_btree **ast)
job->pgid = 0; job->pgid = 0;
} }
} }
else
DG("WHY HERE?");
return (0); return (0);
} }

View file

@ -12,13 +12,6 @@
#include "minishell.h" #include "minishell.h"
static int print_error(char *std)
{
ft_dprintf(2, "{red}%s: internal fcntl %s error errno=%i{eoc}\n",
SHELL_NAME, std, errno);
return (errno);
}
int exec_reset_job(t_job *job) int exec_reset_job(t_job *job)
{ {
job->id = 0; job->id = 0;
@ -32,21 +25,13 @@ int exec_reset(void)
{ {
t_exec *exec; t_exec *exec;
t_jobc *jobc; t_jobc *jobc;
int i;
exec = &data_singleton()->exec; exec = &data_singleton()->exec;
jobc = &data_singleton()->jobc; jobc = &data_singleton()->jobc;
if (errno != EBADF) i = -1;
{ while (++i < 10)
if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1 exec->fd_save[i] = fcntl(i, F_DUPFD_CLOEXEC, 10);
&& errno != EBADF)
return (print_error("STDIN"));
if ((exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC, 10)) == -1
&& errno != EBADF)
return (print_error("STDOUT"));
if ((exec->fd_save[2] = fcntl(STDERR, F_DUPFD_CLOEXEC, 10)) == -1
&& errno != EBADF)
return (print_error("STDERR"));
}
exec->op_stack = NULL; exec->op_stack = NULL;
exec->fdin = STDIN; exec->fdin = STDIN;
exec->attrs = 0; exec->attrs = 0;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */ /* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */
/* Updated: 2017/03/20 15:50:12 by gwojda ### ########.fr */ /* Updated: 2017/03/21 18:10:21 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,8 +14,8 @@
int plaunch_builtin(t_process *p) int plaunch_builtin(t_process *p)
{ {
return ((*p->data.cmd.execf)( return (*p->data.cmd.execf)(
p->data.cmd.path, p->data.cmd.path,
p->data.cmd.av, p->data.cmd.av,
data_singleton()->env)); data_singleton()->env);
} }

View file

@ -14,7 +14,7 @@
static void error_launch(char *error_ori, char *error_type, int error_code) static void error_launch(char *error_ori, char *error_type, int error_code)
{ {
ft_dprintf(2, "{red}%s: %s%s{eoc}\n", SHELL_NAME, error_ori, error_type); SH_ERR("%s: %s%s", SHELL_NAME, error_ori, error_type);
exit(error_code); exit(error_code);
} }

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 17:26:53 by wescande #+# #+# */ /* Created: 2017/03/07 17:26:53 by wescande #+# #+# */
/* Updated: 2017/03/21 00:53:01 by wescande ### ########.fr */ /* Updated: 2017/03/21 18:10:08 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,13 +6,13 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */
/* Updated: 2017/03/21 20:53:04 by ariard ### ########.fr */ /* Updated: 2017/03/21 18:11:33 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int do_the_muther_forker(t_process *p) int process_fork(t_process *p)
{ {
pid_t pid; pid_t pid;
@ -35,40 +35,25 @@ 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) int process_launch(t_process *p)
{ {
p->state = PROCESS_RUNNING;
if (IS_PIPESINGLE(*p) if (IS_PIPESINGLE(*p)
&& p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL) && p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL)
{ {
if (process_redirect(p)) if (process_redirect(p))
{
set_exitstatus(1, 1); set_exitstatus(1, 1);
return (0); else
} set_exitstatus(p->map.launch(p), 1);
return (p->map.launch(p));
/* set_exitstatus(p->map.launch(p), 1); */
/* return (0); */
}
return (do_the_muther_forker(p));
}
int process_launch(t_process *p)
{
pid_t pid;
p->state = PROCESS_RUNNING;
if (!(pid = do_the_fork_if_i_have_to(p)))
{
process_resetfds(p); process_resetfds(p);
process_free(p, 0); process_free(p, 0);
return (1); return (0);
} }
DG("FORK"); p->pid = process_fork(p);
p->pid = pid; process_setgroup(p, p->pid);
process_setgroup(p, pid);
if (p->fdin != STDIN) if (p->fdin != STDIN)
close(p->fdin); close(p->fdin);
if (p->fdout != STDOUT) if (p->fdout != STDOUT)
close(p->fdout); close(p->fdout);
return (0); return (1);
} }

View file

@ -19,20 +19,10 @@ void process_resetfds(t_process *p)
(void)p; (void)p;
exec = &data_singleton()->exec; exec = &data_singleton()->exec;
i = 0; i = -1;
while (i < 10) while (++i < 10)
{ {
/* if (i!=3) //JACK SOME PB HERE on close la sortie debug en forcant le close sur tous les fd... ne risque-t-on pas autre chose ??!!! */ if (exec->fd_save[i] != -1)
/* { */ dup2(exec->fd_save[i], i);
close(i++);
/* } */
/* else */
/* i++; */
} }
if (exec->fd_save[0] != -1)
dup2(exec->fd_save[0], STDIN);
if (exec->fd_save[1] != -1)
dup2(exec->fd_save[1], STDOUT);
if (exec->fd_save[2] != -1)
dup2(exec->fd_save[2], STDERR);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */ /* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */
/* Updated: 2017/03/21 17:40:36 by jhalford ### ########.fr */ /* Updated: 2017/03/21 17:41:36 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/21 01:21:38 by wescande ### ########.fr */ /* Updated: 2017/03/21 18:15:53 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,8 +21,7 @@ static char *manage_output(int *fds)
output = NULL; output = NULL;
close(fds[PIPE_WRITE]); close(fds[PIPE_WRITE]);
while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)) > 0) while ((ret = read(fds[PIPE_READ], buf, BUF_SIZE)) > 0) {
{
buf[ret] = 0; buf[ret] = 0;
ft_strappend(&output, buf); ft_strappend(&output, buf);
} }
@ -89,7 +88,7 @@ char *command_getoutput(char *command)
if (!command) if (!command)
return (NULL); return (NULL);
pipe(fds); pipe(fds);
if (!(pid = do_the_muther_forker(NULL))) if (!(pid = process_fork(NULL)))
{ {
close(fds[PIPE_READ]); close(fds[PIPE_READ]);
dup2_close(fds[PIPE_WRITE], STDOUT); dup2_close(fds[PIPE_WRITE], STDOUT);
@ -110,7 +109,7 @@ int command_setoutput(char *const av[], char **env)
if (!av) if (!av)
return (0); return (0);
pid = 0; pid = 0;
if (!(pid = do_the_muther_forker(NULL))) if (!(pid = process_fork(NULL)))
execute_command(av, env); execute_command(av, env);
return (pid); return (pid);
} }

View file

@ -6,13 +6,13 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */ /* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */
/* Updated: 2017/03/18 15:04:16 by gwojda ### ########.fr */ /* Updated: 2017/03/22 12:09:59 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static int ft_history_parsing_4(char *str, int *i) static int history_parsing_nb_and_previous(char *str, int *i)
{ {
int tmp; int tmp;
@ -34,11 +34,11 @@ static int ft_history_parsing_4(char *str, int *i)
return (0); return (0);
} }
static int ft_history_parsing_3(char *str, int *i) static int history_parsing_nb_and_name(char *str, int *i)
{ {
int tmp; int tmp;
if (ft_history_parsing_4(str, i)) if (history_parsing_nb_and_previous(str, i))
return (1); return (1);
else if (ft_isdigit(str[(*i) + 1]) && (size_t)ft_atoi(str + (*i) + 1) < else if (ft_isdigit(str[(*i) + 1]) && (size_t)ft_atoi(str + (*i) + 1) <
ft_hist_len()) ft_hist_len())
@ -60,18 +60,20 @@ static int ft_history_parsing_3(char *str, int *i)
return (1); return (1);
} }
static int ft_history_parsing_2(int has_prompt, char **input) static int rematch_history_parsing(int has_prompt, char **input)
{ {
int ret;
data_singleton()->line.pos = 0; data_singleton()->line.pos = 0;
has_prompt ? ft_prompt() : ft_putstr("> "); has_prompt ? ft_prompt() : ft_putstr("> ");
if (ft_read_stdin(input) < 0) if ((ret = ft_read_stdin(input)) < 0)
return (-1); return (-1);
if (data_singleton()->line.input) if (data_singleton()->line.input)
ft_current_str(data_singleton()->line.input, ft_current_str(data_singleton()->line.input,
data_singleton()->line.pos); data_singleton()->line.pos);
ft_putchar('\n'); ft_putchar('\n');
ft_history_parsing(has_prompt, input); ft_history_parsing(has_prompt, input);
return (0); return (ret);
} }
int ft_history_parsing(int has_prompt, char **input) int ft_history_parsing(int has_prompt, char **input)
@ -88,14 +90,14 @@ int ft_history_parsing(int has_prompt, char **input)
if (data_singleton()->line.input[i] == '!') if (data_singleton()->line.input[i] == '!')
{ {
boolean = 1; boolean = 1;
if (!ft_history_parsing_3(data_singleton()->line.input, &i)) if (!history_parsing_nb_and_name(data_singleton()->line.input, &i))
boolean = 0; boolean = 0;
else else
break ; break ;
} }
++i; ++i;
} }
if (boolean && ft_history_parsing_2(has_prompt, input)) if (boolean)
return (-1); return (rematch_history_parsing(has_prompt, input));
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/25 20:04:16 by gwojda #+# #+# */ /* Created: 2017/01/25 20:04:16 by gwojda #+# #+# */
/* Updated: 2017/01/26 11:33:22 by gwojda ### ########.fr */ /* Updated: 2017/03/22 11:56:47 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -52,13 +52,11 @@ void ft_realloc_str_history_3(char **str, size_t pos, char *s)
if (new_str3) if (new_str3)
{ {
new_str2 = ft_strjoin(new_str, new_str3); new_str2 = ft_strjoin(new_str, new_str3);
free(new_str);
new_str3 = ft_strjoin(new_str2, (*str) + pos + ft_strlen(s) + 2); new_str3 = ft_strjoin(new_str2, (*str) + pos + ft_strlen(s) + 2);
free(new_str2); free(new_str2);
} }
else free(new_str);
new_str3 = ft_strjoin(new_str, (*str) + pos + ft_strlen(s) + 2); ft_strdel(str);
free(s);
free(*str);
*str = new_str3; *str = new_str3;
free(s);
} }

View file

@ -30,6 +30,7 @@ int job_wait(int id)
&& errno != ECHILD) && errno != ECHILD)
ft_dprintf(2, "{red}%s: waitpid error errno=%i{eoc}\n", ft_dprintf(2, "{red}%s: waitpid error errno=%i{eoc}\n",
SHELL_NAME, errno); SHELL_NAME, errno);
DG("wait trigger pid=%i", pid);
if (pid <= 1 || mark_process_status(pid, status) if (pid <= 1 || mark_process_status(pid, status)
|| job_is_stopped(j) || job_is_stopped(j)
|| job_is_completed(j)) || job_is_completed(j))

View file

@ -19,8 +19,9 @@ int lexer_word(t_list **alst, t_lexer *lexer)
token = (*alst)->content; token = (*alst)->content;
token->type = token->type ? token->type : TK_WORD; token->type = token->type ? token->type : TK_WORD;
if ((state = get_state_global(lexer)) if (lexer->str[lexer->pos] != '!'
|| (state = get_state_redir(lexer))) && ((state = get_state_global(lexer))
|| (state = get_state_redir(lexer))))
lexer->state = state; lexer->state = state;
else if (lexer->str[lexer->pos] == '=' else if (lexer->str[lexer->pos] == '='
&& word_is_assignment((char *[]){token->data, (char *)token->esc})) && word_is_assignment((char *[]){token->data, (char *)token->esc}))

View file

@ -58,7 +58,7 @@ static int read_stdin(int *ret, int *j)
{ {
*j = 0; *j = 0;
*ret = 0; *ret = 0;
if (read(0, ret, sizeof(int)) < 0) if (read(STDIN, ret, sizeof(int)) < 0)
return (-1); return (-1);
return (1); return (1);
} }

View file

@ -17,11 +17,14 @@ extern char **environ;
static int localenv_init(void) static int localenv_init(void)
{ {
t_data *data; t_data *data;
char *pid;
pid = ft_itoa(getpid());
data = data_singleton(); data = data_singleton();
data->local_var = NULL; data->local_var = NULL;
builtin_setenv(NULL, (char *[]){"local", "IFS", " \t\n", 0}, NULL); builtin_setenv(NULL, (char *[]){"local", "IFS", " \t\n", 0}, NULL);
builtin_setenv(NULL, (char *[]){"local", "PS2", " >", 0}, NULL); builtin_setenv(NULL, (char *[]){"local", "PS2", " >", 0}, NULL);
ft_strdel(&pid);
return (0); return (0);
} }

View file

@ -43,6 +43,7 @@ static int handle_instruction(t_list **token, t_btree **ast)
return (ret); return (ret);
if (do_lexer_routine(token, stream) > 0) if (do_lexer_routine(token, stream) > 0)
continue ; continue ;
/* token_print(*token); */
if ((ret = do_parser_routine(token, ast)) == 1 if ((ret = do_parser_routine(token, ast)) == 1
&& SH_NO_INTERACTIVE(data->opts)) && SH_NO_INTERACTIVE(data->opts))
return (ret); return (ret);