modif env, modif command get ouput, modif hash (just a str is needed)

This commit is contained in:
wescande 2017-03-19 17:45:29 +01:00
parent c8c9049661
commit 60f150a4b4
18 changed files with 189 additions and 114 deletions

View file

@ -209,6 +209,7 @@ job_control/sigint_handler.c\
job_control/sigtstp_handler.c\
job_control/sigttin_handler.c\
job_control/sigttou_handler.c\
lexer/do_lexer_routine.c\
lexer/get_lexer_stack.c\
lexer/get_reserved_words.c\
lexer/get_state_global.c\
@ -279,6 +280,7 @@ parser/add_sep.c\
parser/add_subshell.c\
parser/aggregate_sym.c\
parser/build_tree.c\
parser/do_parser_routine.c\
parser/error_syntax.c\
parser/eval_sym.c\
parser/ft_parse.c\

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 11:10:14 by gwojda #+# #+# */
/* Updated: 2017/03/15 18:54:26 by jhalford ### ########.fr */
/* Updated: 2017/03/19 16:37:44 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,9 +23,9 @@ typedef struct s_hash
char *path;
} t_hash;
int ft_add_hash(t_process *p);
int ft_hash(t_process *p);
int ft_is_hash(t_process *p);
char *ft_add_hash(char *cmd);
char *ft_hash(char *cmd);
char *ft_is_hash(char *cmd);
int ft_hash_str(char *str);
void ft_hash_free(void *ptr, size_t size);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/03/18 02:58:19 by wescande ### ########.fr */
/* Updated: 2017/03/19 17:13:05 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -82,7 +82,7 @@ void token_print(t_list *lst);
int reduce_parens(t_list **alst, char *str);
int bquotes_expand(t_list **alst);
char *command_getoutput(char *command, char *const av_cmd[], char **env);
char *command_getoutput(char *command, char *const av[], char **env, int pipe_mode);
int ft_is_delim(char c);
int ft_is_delim_list(char c);
@ -94,6 +94,7 @@ int get_lexer_stack(t_lexer lexer);
int get_reserved_words(t_list *temp);
int insert_newline(t_list **alst);
int do_lexer_routine(t_list **token, char *stream);
void lexer_init(t_lexer *lexer);
void lexer_destroy(t_lexer *lexer);
int lexer_lex(t_list **alst, t_lexer *lexer);

View file

@ -60,6 +60,7 @@ struct s_errormatch
char *error;
};
int do_parser_routine(t_list **token, t_btree **ast);
void parser_init(t_parser *parser);
void parser_destroy(t_parser *parser);
int stack_init(t_parser *parser);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
/* Updated: 2017/03/18 04:13:57 by wescande ### ########.fr */
/* Updated: 2017/03/19 17:43:24 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -106,7 +106,7 @@ int builtin_env(const char *path, char *const argv[], char *const envp[])
ft_putchar('\n');
}
else
ft_putstr(command_getoutput(NULL, argv, env));
command_getoutput(NULL, argv, env, 0);
ft_tabdel(&env);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */
/* Updated: 2017/03/16 21:42:38 by jhalford ### ########.fr */
/* Updated: 2017/03/19 16:00:16 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -67,6 +67,8 @@ int process_set(t_process *p, t_btree *ast)
p->to_close = fds[PIPE_READ];
p->fdout = fds[PIPE_WRITE];
exec->fdin = fds[PIPE_READ];
if (!ast)
return (0);
p->redirs = ft_lstmap(((t_astnode *)ast->item)->data.cmd.redir, &redir_copy);
return (process_set_spec(p, ast));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:48:10 by jhalford #+# #+# */
/* Updated: 2017/03/18 00:23:57 by wescande ### ########.fr */
/* Updated: 2017/03/19 14:04:57 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,7 @@ int process_setgroup(t_process *p, pid_t pid)
if (setpgid(pid, j->pgid) == -1)
ft_dprintf(2, "{red}%s: internal setpgid() errno=%i{eoc}\n", SHELL_NAME, errno);
/* if (JOB_IS_FG(j->attrs)) */
if (pid ==0 && JOB_IS_FG(j->attrs))
if (pid == 0 && JOB_IS_FG(j->attrs))
{
DG("tcsetpgrp[%i]", j->pgid);
tcsetpgrp(STDIN, j->pgid);

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 15:06:05 by wescande #+# #+# */
/* Updated: 2017/03/15 18:48:13 by ariard ### ########.fr */
/* Updated: 2017/03/19 16:42:10 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,7 @@ int pset_cmd(t_process *p, t_btree *ast)
{
t_btree *func;
if (!(p->data.cmd.av = token_to_argv(((t_astnode *)ast->item)->data.cmd.token, 1)))
if (ast && !(p->data.cmd.av = token_to_argv(((t_astnode *)ast->item)->data.cmd.token, 1)))
{
p->type = PROCESS_EMPTY;
return (0);
@ -40,7 +40,7 @@ int pset_cmd(t_process *p, t_btree *ast)
if (stat(p->data.cmd.path, p->data.cmd.stat) == -1)
ft_memdel((void**)&p->data.cmd.stat);
}
else if (ft_hash(p))
else if ((p->data.cmd.path = ft_hash(p->data.cmd.av[0])))
{
p->data.cmd.execf = &execve;
if (stat(p->data.cmd.path, p->data.cmd.stat) == -1)

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 19:44:25 by wescande #+# #+# */
/* Updated: 2017/03/18 04:11:20 by wescande ### ########.fr */
/* Updated: 2017/03/19 17:42:45 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -38,35 +38,67 @@ static char *manage_command(char *const av_cmd[])
if (!av_cmd)
return (NULL);
i = -1;
command = ft_strdup(av_cmd[++i]);
command = NULL;
while (av_cmd[++i])
ft_strjoin(command, av_cmd[i]);
{
if (!command)
command = ft_str3join("\"", av_cmd[i], "\"");
else
command = ft_strjoinf(command, ft_str3join("\"", av_cmd[i], "\""), 3);
}
return (command);
}
char *command_getoutput(char *command, char *const av_cmd[], char **env)
static void execute_command(char *command, char *const av[], char **env)
{
t_list *token;
t_btree *ast;
t_data *data;
token = NULL;
ast = NULL;
data = data_singleton();
data->env = env;
data->opts &= ~SH_INTERACTIVE;
data->opts &= ~SH_OPTS_JOBC;
command = command ? command : manage_command(av);
DG("command is %s", command);
if (do_lexer_routine(&token, command))
{
ft_dprintf(2, "{red}%s: syntax error in command substitution{eoc}\n",
SHELL_NAME);
exit(1);
}
if (do_parser_routine(&token, &ast) <= 0)
{
ft_dprintf(2, "{red}%s: parse error in command substitution{eoc}\n",
SHELL_NAME);
exit(1);
}
exit(data_singleton()->parser.state == SUCCESS && ft_exec(&ast) < 0);
}
char *command_getoutput(char *command, char *const av[], char **env, int pipe_mode)
{
char *output;
int ret;
int pid;
int fds[2];
char **av;
pipe(fds);
if (!command && !av)
return (NULL);
if (pipe_mode)
pipe(fds);
if (!(pid = fork()))
{
close(fds[PIPE_READ]);
dup2_close(fds[PIPE_WRITE], STDOUT);
av = ft_sstradd(NULL, data_singleton()->argv[0]);
av = ft_sstradd(av, "-c");
if (!(command = command ? command : manage_command(av_cmd)))
exit(1);
DG("command is %s", command);
av = ft_sstradd(av, command);
execve(data_singleton()->argv[0], av, env);
exit(1);
if (pipe_mode)
{
close(fds[PIPE_READ]);
dup2_close(fds[PIPE_WRITE], STDOUT);
}
execute_command(command, av, env);
}
waitpid(pid, &ret, WUNTRACED);
output = manage_output(fds);
return (output);
if (pipe_mode)
return (manage_output(fds));
return (NULL);
}

View file

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

View file

@ -6,23 +6,22 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 11:20:11 by gwojda #+# #+# */
/* Updated: 2017/03/16 10:39:34 by gwojda ### ########.fr */
/* Updated: 2017/03/19 16:40:25 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_add_hash(t_process *p)
char *ft_add_hash(char *cmd)
{
int id;
t_hash hash;
if (!(hash.path = ft_findexec(ft_getenv(
data_singleton()->env, "PATH"), p->data.cmd.av[0])))
return (0);
hash.key = ft_strdup(p->data.cmd.av[0]);
id = ft_hash_str(p->data.cmd.av[0]);
data_singleton()->env, "PATH"), cmd)))
return (NULL);
hash.key = ft_strdup(cmd);
id = ft_hash_str(cmd);
ft_lsteadd(&(g_hash[id]), ft_lstnew(&hash, sizeof(t_hash)));
p->data.cmd.path = ft_strdup(hash.path);
return (1);
return (ft_strdup(hash.path));
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 11:06:19 by gwojda #+# #+# */
/* Updated: 2017/03/07 16:02:41 by jhalford ### ########.fr */
/* Updated: 2017/03/19 16:38:12 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,10 +14,11 @@
t_list *g_hash[MAX_HASH] = {NULL};
int ft_hash(t_process *p)
char *ft_hash(char *cmd)
{
if (!ft_is_hash(p))
if (!ft_add_hash(p))
return (0);
return (1);
char *path;
if (!(path = ft_is_hash(cmd)))
path = ft_add_hash(cmd);
return (path);
}

View file

@ -6,36 +6,35 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 11:08:40 by gwojda #+# #+# */
/* Updated: 2017/03/07 14:45:20 by wescande ### ########.fr */
/* Updated: 2017/03/19 16:39:18 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_is_hash(t_process *p)
char *ft_is_hash(char *cmd)
{
t_list *list;
t_list *ref;
int id;
id = ft_hash_str(p->data.cmd.av[0]);
id = ft_hash_str(cmd);
list = g_hash[id];
ref = list;
while (list)
{
if (!ft_strcmp(((t_hash *)list->content)->key, p->data.cmd.av[0]))
if (!ft_strcmp(((t_hash *)list->content)->key, cmd))
{
if (access(((t_hash *)list->content)->path, X_OK))
{
ref->next = list->next;
ft_lstdelone(&list, &ft_hash_free);
return (0);
return (NULL);
}
p->data.cmd.path = ft_strdup(((t_hash *)list->content)->path);
return (1);
return (ft_strdup(((t_hash *)list->content)->path));
}
ref = list;
list = list->next;
}
return (0);
return (NULL);
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* do_lexer_routine.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/19 14:24:38 by wescande #+# #+# */
/* Updated: 2017/03/19 14:31:39 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int do_lexer_routine(t_list **token, char *stream)
{
t_list *ltoken;
t_data *data;
data = data_singleton();
if (data->lexer.state == HEREDOC || data->parser.state == UNDEFINED)
{
ft_strappend(&data->lexer.str, (char[]){'\n', 0});
data->lexer.pos++;
}
ft_strappend(&data->lexer.str, stream);
if (get_lexer_stack(data->lexer) == BACKSLASH)
pop(&data->lexer.stack);
ltoken = ft_lstlast(*token);
if (lexer_lex(*token ? &ltoken : token, &data->lexer) < 0)
exit(1);
if (get_lexer_stack(data->lexer) > 2)
return (1);
data->lexer.state = DEFAULT;
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */
/* Updated: 2017/03/17 21:18:44 by jhalford ### ########.fr */
/* Updated: 2017/03/19 16:49:29 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,11 +29,11 @@ int data_init(int ac, char **av)
set_exitstatus(0, 1);
shlvl = ft_getenv(data->env, "SHLVL");
if (shlvl)
{
shlvl = ft_itoa(ft_atoi(shlvl) + 1);
builtin_setenv(NULL, (char *[]){"setenv", "SHLVL", shlvl, 0}, NULL);
ft_strdel(&shlvl);
}
else
shlvl = ft_strdup("1");
builtin_setenv(NULL, (char *[]){"setenv", "SHLVL", shlvl, 0}, NULL);
ft_strdel(&shlvl);
data->comp = NULL;
data->opts = 0;
exec_reset();

View file

@ -28,54 +28,6 @@ static int do_readline_routine(char **stream)
return (ret);
}
static int do_lexer_routine(t_list **token, char *stream)
{
t_list *ltoken;
t_data *data;
data = data_singleton();
if (data->lexer.state == HEREDOC || data->parser.state == UNDEFINED)
{
ft_strappend(&data->lexer.str, (char[]){'\n', 0});
data->lexer.pos++;
}
ft_strappend(&data->lexer.str, stream);
if (get_lexer_stack(data->lexer) == BACKSLASH)
pop(&data->lexer.stack);
ltoken = ft_lstlast(*token);
if (lexer_lex(*token ? &ltoken : token, &data->lexer) < 0)
exit(1);
if (get_lexer_stack(data->lexer) > 2)
return (1);
data->lexer.state = DEFAULT;
return (0);
}
static int do_parser_routine(t_list **token, t_btree **ast)
{
t_data *data;
data = data_singleton();
if (get_reserved_words(*token))
return (1);
if (insert_newline(token))
return (1);
if (data->parser.state == SUCCESS && stack_init(&data->parser))
exit(1);
if (ft_parse(ast, token, &data->parser))
exit(1);
if ((data->lexer.state = data->parser.heredoc_queue ? HEREDOC : DEFAULT))
return (0);
if (data->parser.state == ERROR)
{
error_syntax(token);
return (1);
}
else if (data->parser.state == SUCCESS)
return (1);
return (0);
}
static int handle_instruction(t_list **token, t_btree **ast)
{
int ret;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */
/* Updated: 2017/03/18 00:04:33 by wescande ### ########.fr */
/* Updated: 2017/03/19 16:50:42 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -71,7 +71,8 @@ static int interactive_settings(void)
*shell_pgid = getpid();
if (setpgid(*shell_pgid, *shell_pgid))
{
ft_dprintf(2, "Couldnt put the shell in it's own process group");
ft_dprintf(2,
"{red}Couldnt put the shell in it's own process group{eoc}\n");
exit(1);
}
tcsetpgrp(STDIN, *shell_pgid);

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* do_parser_routine.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/19 14:24:14 by wescande #+# #+# */
/* Updated: 2017/03/19 14:24:22 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int do_parser_routine(t_list **token, t_btree **ast)
{
t_data *data;
data = data_singleton();
if (get_reserved_words(*token))
return (1);
if (insert_newline(token))
return (1);
if (data->parser.state == SUCCESS && stack_init(&data->parser))
exit(1);
if (ft_parse(ast, token, &data->parser))
exit(1);
if ((data->lexer.state = data->parser.heredoc_queue ? HEREDOC : DEFAULT))
return (0);
if (data->parser.state == ERROR)
{
error_syntax(token);
return (1);
}
else if (data->parser.state == SUCCESS)
return (1);
return (0);
}