"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> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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
CC = gcc
FLAGS = -Wall -Wextra -Werror -fvisibility=hidden #-fsanitize=address
FLAGS = -Wall -Wextra -Werror #-fvisibility=hidden# -fsanitize=address
D_FLAGS = -g
DELTA = $$(echo "$$(tput cols)-47"|bc)
@ -40,7 +40,6 @@ builtin/builtin_hash.c\
builtin/builtin_history.c\
builtin/builtin_math.c\
builtin/builtin_read.c\
builtin/builtin_return_status.c\
builtin/builtin_setenv.c\
builtin/builtin_unset.c\
builtin/builtin_unsetenv.c\

View file

@ -19,6 +19,18 @@
# 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);
int builtin_return_status(int ret, int status);
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_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);
#endif

View file

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

View file

@ -67,7 +67,7 @@ int data_init(int ac, char **av);
void data_exit(void);
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);
void ft_putast2(void *node);

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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];
i++;
}
ft_strdel(sstr + index);
}

View file

@ -81,14 +81,14 @@ int builtin_cd(const char *path,
opts = CDOPT_L;
i = builtin_cd_opts(av, &opts);
if (!(target = builtin_cd_special(av + i, envp)))
return (builtin_return_status(0, 1));
return (1);
cwd = getcwd(NULL, 0);
builtin_setenv(path, (char*[4]){"setenv", "OLDPWD", cwd, NULL}, envp);
free(cwd);
if (chdir(target))
{
SH_ERR(CDERR_1, target);
return (builtin_return_status(0, 1));
return (1);
}
else if (target != av[i])
ft_printf("%s\n", target);
@ -97,5 +97,5 @@ int builtin_cd(const char *path,
free(cwd);
if (!ft_strcmp(*(av + i), "-"))
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('\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"
#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)
ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n");
else if (c)
ft_dprintf(2, "{red}env: illegal option -- %c{eoc}\n", c);
ft_dprintf(2, "usage: %s\n", US_ENV);
{'i', NULL, BT_ENV_LI, 0, bt_env_geti},
{0, 0, 0, 0, 0},
};
int bt_env_geti(char ***av, t_env_data *data)
{
if (!av || !*av || !data)
return (1);
while (**av && ft_strchr(**av, '='))
{
data->custom_env = ft_sstradd(data->custom_env, **av);
++(*av);
}
static void env_freeone(char **env, char *arg)
{
int i;
char *tmp;
while (env && *env && (i = -1))
{
if (ft_strcmp(*env, arg) == '='
&& ft_strlen(arg) == ft_strlenchr(*env, '='))
{
tmp = *env;
while (*env)
{
*env = *(env + 1);
++env;
}
ft_strdel(&tmp);
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);
}
else if ((**arg)[0] == '-')
return (env_usage(0, (**arg)[1]));
else
--(*av);
return (0);
}
static int bt_env_parse(t_env_data *data, char **av)
{
data->flag = 0;
data->av_data = NULL;
data->custom_env = NULL;
if (cliopts_get(av, g_env_opts, data))
return (1);
return (0);
}
int builtin_env(const char *path,
char *const argv[], char *const envp[])
{
char **env;
t_env_data data;
int status;
pid_t pid;
struct stat buf;
(void)path;
pid = 0;
if (!argv || ft_strcmp(*argv, "env"))
return (builtin_return_status(0, env_usage(0, 0)));
env = ft_sstrdup((char **)envp);
if (env_treat_flag(&env, &argv))
(void)envp;
if (bt_env_parse(&data, (char**)argv))
return (ft_perror() && SH_ERR("usage: %s", ENV_USAGE) ? 0 : 0);
else if (!*data.av_data)
return (builtin_setenv(NULL, (char*[]){"setenv", 0}, NULL));
else if ((pid = fork()) == 0)
{
ft_sstrfree(env);
return (builtin_return_status(0, 1));
if (!(path = ft_strchr(data.av_data[0], '/') ?
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)
{
ft_sstrprint(env, '\n');
if (env)
ft_putchar('\n');
}
else
pid = command_setoutput(argv, env);
ft_tabdel(&env);
DG("%d", pid);
return (builtin_return_status(pid, 0));
waitpid(pid, &status, 0);
ft_sstrfree(data.custom_env);
tcsetpgrp(STDIN, data_singleton()->jobc.shell_pgid);
return (0);
}

View file

@ -47,7 +47,7 @@ int builtin_export(
if (cliopts_get((char**)av, g_export_opts, &data))
ft_perror();
if (data.flag & BT_EXPORT_LP)
return (builtin_return_status(0, bt_export_print()));
return (bt_export_print());
av = data.av_data;
while (*av)
{
@ -57,9 +57,9 @@ int builtin_export(
else
equal = ft_getenv(data_singleton()->local_var, *av);
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);
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)envp;
if (!av || ft_hash_opt(av))
return (builtin_return_status(0, 1));
return (1);
i = 0;
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]);
++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;
len = 1;
if (!head)
return (builtin_return_status(0, 0));
return (0);
if (head && !head->str)
head = head->next;
while (head && head->str)
@ -34,5 +34,5 @@ int builtin_history(const char *path, char *const av[], char *const envp[])
++len;
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)envp;
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);
var = av[1];
if (get_value(var, &value))
return (builtin_return_status(0, 1));
return (1);
operator = av[2];
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];
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))
return (builtin_return_status(0, 1));
return (1);
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"
#define US_READ "read [-ers] [-u fd] [-t timeout] [-p prompt]"
#define US_READ_1 "[-n nchars] [-d delim] [name ...]"
@ -26,11 +27,6 @@ t_cliopts g_read_opts[] =
{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)
{
data->opts = 0;
@ -115,8 +111,8 @@ int builtin_read(const char *path, char *const av[], char *const envp[])
if (ret == -1)
exit(1);
if (ret != 0)
bt_read_usage();
SH_ERR("usage: read %s %s\n", US_READ, US_READ_1);
if (ret != 2)
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"
#define SETERR_0 "setenv: invalid variable name"
#define SETERR_0 "%s: %s: invalid variable name"
static int assign_var(char *const av[], char ***env)
{
@ -44,8 +44,9 @@ int builtin_setenv(const char *path,
int ret;
(void)path;
(void)envp;
if (!av || !av[0])
return (builtin_return_status(0, 1));
return (1);
env = (ft_strcmp(av[0], "local") == 0) ? &data_singleton()->local_var :
&data_singleton()->env;
if (!av[1])
@ -59,8 +60,8 @@ int builtin_setenv(const char *path,
ret = word_is_assignment((char *[]){av[1], (esc + 1)});
ft_strdel(&esc);
if (!ret && ft_strcmp(av[1], "?"))
return (SH_ERR(SETERR_0));
return (SH_ERR(SETERR_0, av[0], av[1]));
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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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++;
}
return (builtin_return_status(0, 0));
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
int len;
len = ft_strrchr(c->rcmd, '/') - current_word + 1;
len = ft_strrchr(current_word, '/') - current_word + 1;
if (len < 0)
return ;
if (c->cpath)

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
int i;
int m_len;
i = 0;
env = data_singleton()->env;
c->match = ft_strdupi_w(current_word + 1);
m_len = ft_strlen(c->match);
while (env[i])
{
if (!ft_strncmp(c->match, env[i], ft_strlen(c->match)) &&
env[i][ft_strlen(c->match)] != '=')
if (!ft_strncmp(c->match, env[i], m_len) && env[i][m_len] != '=')
c_addnode(c, ft_strndup(env[i], ft_strchr(env[i], '=') - env[i]));
++i;
}

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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] == '.')
{
c->cpath = ft_strdup("./");
c->match = ft_strdup(".");
c->match = ft_strdup(current_word);
}
if (c->cpath == NULL)
{

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
/*
** 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.
*/
@ -42,16 +19,20 @@ static int c_chevron(t_comp *c)
static char *c_current_words(t_comp *c)
{
size_t pos;
char *str;
pos = c->ircmd;
if (pos >= ft_strlen(c->rcmd))
pos = ft_strlen(c->rcmd) -
(ft_strlen(data_singleton()->line.input) - pos + 1);
while (pos && c->rcmd[pos] != ' ')
(void)c;
pos = data_singleton()->line.pos;
str = data_singleton()->line.input;
DG("pos = \"%zu\"", pos);
while (pos && str[pos] != ' ' && str[pos] != '<'
&& str[pos] != '>' && str[pos] != '\n' && str[pos] != ';')
--pos;
if (c->rcmd[pos] == ' ')
if (str[pos] == ' ' || str[pos] == '<'
|| str[pos] == '>' || str[pos] == '\n' || str[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;
DG("c->rcmd = \"%s\"", c->rcmd);
current_word = c_current_words(c);
DG("current_word = \"%s\"", current_word);
if (ft_strchr(c->rcmd, '/'))
c_seek_abs_path(c, current_word);
else if (ft_strchr(c->rcmd, '$'))
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);
else
c_seek_files(s, c, current_word);

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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.
*/
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_mid;
char *ref_prev;
char *str;
size_t pos;
str = data_singleton()->line.input;
pos = data_singleton()->line.pos;
while (str[pos] && str[pos] != ' ')
++pos;
while (str[pos] && str[pos] == ' ')
@ -103,7 +101,7 @@ int c_glob_matching(void)
if ((pos = c_glob_pos(NULL, 0)) == -1)
return (0);
current_word = ft_strdupi_w(str + pos);
if (current_word[0] == '$')
if (current_word[0] == '$' || current_word[0] == '~')
{
free(current_word);
return (0);
@ -113,6 +111,6 @@ int c_glob_matching(void)
ss_glob = glob(current_word, glob_echap, glob_echap, 1);
if (c_check_glob(ss_glob, current_word, glob_echap, pos))
return (0);
c_replace_globbing(ss_glob, pos);
c_replace_globbing(ss_glob, pos, data_singleton()->line.pos);
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
t_data *s;
s = data_singleton();
if (ft_isascii(keypress))
{
c->isrematch = 1;
c_term_clear(c);
c_refresh_match(c, keypress);
c_clear_lst(c);
c_matching(data_singleton(), c);
if (data_singleton()->comp && c->lst == NULL)
c_clear(data_singleton());
else if (data_singleton()->comp && c->lst == c->lst->next)
c_matching(s, c);
if (s->comp && c->lst == NULL)
c_clear(s);
else if (s->comp && c->lst == c->lst->next)
return (1);
ft_print(keypress, &data_singleton()->line.input,
&data_singleton()->line.pos);
ft_print(keypress, &s->line.input, &s->line.pos);
return (1);
}
else
{
c_term_clear(c);
c_clear(data_singleton());
c_clear(s);
}
return (0);
}

View file

@ -6,28 +6,31 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
/*
** 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)
{
int i;
int lcmd;
t_data *s;
s = data_singleton();
ft_putstr(tgetstr("do", NULL));
ft_putstr(tgetstr("cd", NULL));
ft_putstr(tgetstr("up", NULL));
i = 0;
lcmd = 0;
lcmd = (c->rcmd) ? ft_nb_last_line(data_singleton()->line.input,
data_singleton()->line.pos) : 0;
lcmd = (c->rcmd) ? ft_nb_last_line(s->line.input, s->line.pos) : 0;
while (i < lcmd)
{
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
** the original position.
** Restore cursor 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)
@ -45,7 +52,9 @@ void c_term_mv_back(t_comp *c)
int i;
int lcmd;
int value;
t_data *s;
s = data_singleton();
i = 0;
if (c->c_line > c->win_y)
value = c->m_size;
@ -58,8 +67,7 @@ void c_term_mv_back(t_comp *c)
}
ft_putstr(tgetstr("cr", NULL));
i = 0;
lcmd = (c->rcmd) ? ft_nb_last_line(data_singleton()->line.input,
data_singleton()->line.pos) : 0;
lcmd = (c->rcmd) ? ft_nb_last_line(s->line.input, s->line.pos) : 0;
while (i < lcmd)
{
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
** the first line under the prompt
** Make room for the list.
**
** 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)
@ -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
** and clear the previous print list.
*/

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
@ -29,11 +29,20 @@ static void c_next_item(t_comp *c)
}
/*
** If the list is empty after the parsing, all memory is cleared.
** if it detect a single item list, the command is directly updated.
** If none of the above behavior is right, the terminal is dropped down by
** comp->c_line then the list is printed. Once that done, the previous position
** of the cursor is restored.
** Once the completion has been processed, this is the return point.
** The three condition are as follow:
** - If the completion list is empty (NULL), the screen is cleared and (1) is
** returned.
** - 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)
@ -60,17 +69,29 @@ int c_dispatcher(t_data *s)
/*
** 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
** 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
** created using c_init().
**
** If the comp struct already exist at the call of the function, it will check
** which key has been pressed. 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_gtfo() is called to know if the command should be updated or
** simply delete all the memory.
** If the comp struct already exist at the call of the function, the first
** thing done is checking if the terminal have been resized between call to
** update the corresponding var. Then it will check which key has been pressed.
**
** 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
** depending on their respective condition.
@ -79,14 +100,15 @@ int c_dispatcher(t_data *s)
int completion(long int keypress)
{
t_data *s;
int pos;
s = data_singleton();
pos = s->line.pos;
if (c_glob_matching())
return (1);
if (s->comp == NULL)
{
if ((s->line.pos == 0) || (s->line.input[s->line.pos] != ' ' &&
s->line.input[s->line.pos] != '\0'))
if (!pos || (s->line.input[pos] != ' ' && s->line.input[pos] != '\0'))
return (0);
c_init(s, keypress);
if (s->comp == NULL)

View file

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

View file

@ -12,13 +12,6 @@
#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)
{
job->id = 0;
@ -32,21 +25,13 @@ int exec_reset(void)
{
t_exec *exec;
t_jobc *jobc;
int i;
exec = &data_singleton()->exec;
jobc = &data_singleton()->jobc;
if (errno != EBADF)
{
if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1
&& 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"));
}
i = -1;
while (++i < 10)
exec->fd_save[i] = fcntl(i, F_DUPFD_CLOEXEC, 10);
exec->op_stack = NULL;
exec->fdin = STDIN;
exec->attrs = 0;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
return ((*p->data.cmd.execf)(
return (*p->data.cmd.execf)(
p->data.cmd.path,
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)
{
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);
}

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
int do_the_muther_forker(t_process *p)
int process_fork(t_process *p)
{
pid_t pid;
@ -35,40 +35,25 @@ int do_the_muther_forker(t_process *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)
&& p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL)
{
if (process_redirect(p))
{
set_exitstatus(1, 1);
return (0);
}
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)))
{
else
set_exitstatus(p->map.launch(p), 1);
process_resetfds(p);
process_free(p, 0);
return (1);
return (0);
}
DG("FORK");
p->pid = pid;
process_setgroup(p, pid);
p->pid = process_fork(p);
process_setgroup(p, p->pid);
if (p->fdin != STDIN)
close(p->fdin);
if (p->fdout != STDOUT)
close(p->fdout);
return (0);
return (1);
}

View file

@ -19,20 +19,10 @@ void process_resetfds(t_process *p)
(void)p;
exec = &data_singleton()->exec;
i = 0;
while (i < 10)
i = -1;
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 ??!!! */
/* { */
close(i++);
/* } */
/* else */
/* i++; */
if (exec->fd_save[i] != -1)
dup2(exec->fd_save[i], 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
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;
ft_strappend(&output, buf);
}
@ -89,7 +88,7 @@ char *command_getoutput(char *command)
if (!command)
return (NULL);
pipe(fds);
if (!(pid = do_the_muther_forker(NULL)))
if (!(pid = process_fork(NULL)))
{
close(fds[PIPE_READ]);
dup2_close(fds[PIPE_WRITE], STDOUT);
@ -110,7 +109,7 @@ int command_setoutput(char *const av[], char **env)
if (!av)
return (0);
pid = 0;
if (!(pid = do_the_muther_forker(NULL)))
if (!(pid = process_fork(NULL)))
execute_command(av, env);
return (pid);
}

View file

@ -6,13 +6,13 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
static int ft_history_parsing_4(char *str, int *i)
static int history_parsing_nb_and_previous(char *str, int *i)
{
int tmp;
@ -34,11 +34,11 @@ static int ft_history_parsing_4(char *str, int *i)
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;
if (ft_history_parsing_4(str, i))
if (history_parsing_nb_and_previous(str, i))
return (1);
else if (ft_isdigit(str[(*i) + 1]) && (size_t)ft_atoi(str + (*i) + 1) <
ft_hist_len())
@ -60,18 +60,20 @@ static int ft_history_parsing_3(char *str, int *i)
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;
has_prompt ? ft_prompt() : ft_putstr("> ");
if (ft_read_stdin(input) < 0)
if ((ret = ft_read_stdin(input)) < 0)
return (-1);
if (data_singleton()->line.input)
ft_current_str(data_singleton()->line.input,
data_singleton()->line.pos);
ft_putchar('\n');
ft_history_parsing(has_prompt, input);
return (0);
return (ret);
}
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] == '!')
{
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;
else
break ;
}
++i;
}
if (boolean && ft_history_parsing_2(has_prompt, input))
return (-1);
if (boolean)
return (rematch_history_parsing(has_prompt, input));
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
new_str2 = ft_strjoin(new_str, new_str3);
free(new_str);
new_str3 = ft_strjoin(new_str2, (*str) + pos + ft_strlen(s) + 2);
free(new_str2);
}
else
new_str3 = ft_strjoin(new_str, (*str) + pos + ft_strlen(s) + 2);
free(s);
free(*str);
free(new_str);
ft_strdel(str);
*str = new_str3;
free(s);
}

View file

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

View file

@ -19,8 +19,9 @@ int lexer_word(t_list **alst, t_lexer *lexer)
token = (*alst)->content;
token->type = token->type ? token->type : TK_WORD;
if ((state = get_state_global(lexer))
|| (state = get_state_redir(lexer)))
if (lexer->str[lexer->pos] != '!'
&& ((state = get_state_global(lexer))
|| (state = get_state_redir(lexer))))
lexer->state = state;
else if (lexer->str[lexer->pos] == '='
&& 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;
*ret = 0;
if (read(0, ret, sizeof(int)) < 0)
if (read(STDIN, ret, sizeof(int)) < 0)
return (-1);
return (1);
}

View file

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

View file

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