diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 27fa02d0..ae97ac45 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/03/22 17:29:53 by ariard ### ########.fr */ +/* Updated: 2017/03/24 13:35:44 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,6 +53,7 @@ struct s_data t_jobc jobc; char **local_var; t_list *lst_func; + char *binary; }; t_data *data_singleton(); diff --git a/42sh/libft/includes/error.h b/42sh/libft/includes/error.h index c9eb792b..896b5bcb 100644 --- a/42sh/libft/includes/error.h +++ b/42sh/libft/includes/error.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 15:34:21 by jhalford #+# #+# */ -/* Updated: 2017/03/21 14:05:04 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 15:11:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,6 +41,6 @@ extern int g_errnum; extern char **g_argv; int error_set(int n, ...); -int ft_perror(void); +int ft_perror(char *utility); #endif diff --git a/42sh/libft/src/cliopts/cliopts_get.c b/42sh/libft/src/cliopts/cliopts_get.c index e2aa284d..0576d5df 100644 --- a/42sh/libft/src/cliopts/cliopts_get.c +++ b/42sh/libft/src/cliopts/cliopts_get.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 20:04:04 by jhalford #+# #+# */ -/* Updated: 2017/03/20 15:50:08 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 15:02:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -97,7 +97,10 @@ int cliopts_get(char **av, t_cliopts opt_map[], void *data) while (av && *av) { if (ft_strcmp(*av, "--") == 0) - return (0); + { + av++; + break ; + } else if ((*av)[0] == '-' && (*av)[1] == '-') { if (cliopts_parse_long(&av, opt_map, data)) diff --git a/42sh/libft/src/error/error.c b/42sh/libft/src/error/error.c index 2c82901b..3bf7451d 100644 --- a/42sh/libft/src/error/error.c +++ b/42sh/libft/src/error/error.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 16:47:00 by jhalford #+# #+# */ -/* Updated: 2017/03/15 20:45:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 15:10:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,8 +37,8 @@ int error_set(int n, ...) return (g_errnum); } -int ft_perror(void) +int ft_perror(char *utility) { - ft_dprintf(2, "{red}%s: %s{eoc}\n", g_argv[0], g_errmsg); + ft_dprintf(2, "{red}%s: %s{eoc}\n", utility ? utility : g_argv[0], g_errmsg); return (g_errnum); } diff --git a/42sh/scriptheader.sh b/42sh/scriptheader.sh index 9dd5cec2..aec703b0 100755 --- a/42sh/scriptheader.sh +++ b/42sh/scriptheader.sh @@ -1,5 +1,9 @@ #!/bin/sh - +verbose=0 +if [ "$1" = "-v" ] +then + verbose=1 +fi word=$(git status -s | sed 's/.* //') red="\033[38;5;1m" gre="\033[38;5;2m" @@ -37,7 +41,6 @@ array_in_array () confirm () { - # call with a prompt string or use a default echo "$cya${1:-Are you sure? [y/N]}$res" read -r -p " " response case "$response" in @@ -55,47 +58,76 @@ do_checkout () i_tmp=$(echo $i | sed 's/\//_/g') cp $i "$HOME/Documents/.$i_tmp.back" git checkout $i - echo "$gre D - O - N - E $res" - echo "$cya $i was checked out. A copy still exist in $HOME/Documents/.$i_tmp.back$res\n" + if [ $verbose -eq 1 ] + then + echo "$gre D - O - N - E $res" + echo "$cya $i was checked out. A copy still exist in $HOME/Documents/.$i_tmp.back$res\n" + fi } -for i in $word -do - if [ -e $i ] - then - if [ -f $i ] +if [ $verbose -eq 1 ] +then + for i in $word + do + if [ -e $i ] then - diff=$(git diff -U0 --exit-code --color $i) - if [ "$?" -eq 1 ] + if [ -f $i ] then - nb_lines=$(echo "$diff" | wc -l) - if [ "$nb_lines" -eq 7 ] + diff=$(git diff -U0 --exit-code --color $i) + if [ "$?" -eq 1 ] then - match=$(array_in_array "-9 +9 Updated: by ### ########.fr" "$diff") - if [ $match -eq 1 ] + nb_lines=$(echo "$diff" | wc -l) + if [ "$nb_lines" -eq 7 ] then - echo "\n$cya CHANGES on $i :$res" - echo "$diff" - confirm - if [ $? -eq 0 ] + match=$(array_in_array "-9 +9 Updated: by ### ########.fr" "$diff") + if [ $match -eq 1 ] then - do_checkout + echo "\n$cya CHANGES on $i :$res" + echo "$diff" + confirm + if [ $? -eq 0 ] + then + do_checkout + else + echo "$cya Nothing done for $i$res\n" + fi else - echo "$cya Nothing done for $i$res\n" + echo "$gre$i is not concerned (diff on the good lines)$res" fi else - echo "$gre$i is not concerned (diff on the good lines)$res" + echo "$gre$i is not concerned (diff is too big)$res" fi else - echo "$gre$i is not concerned (diff is too big)$res" + echo "$gre$i is not concerned (diff is null)$res" fi else - echo "$gre$i is not concerned (diff is null)$res" + echo "$red$i is not a regular file$res" fi else - echo "$red$i is not a regular file$res" + echo "$red$i doesn't exist$res" fi - else - echo "$red$i doesn't exist$res" - fi -done + done +else + for i in $word + do + if [ -e $i ] + then + if [ -f $i ] + then + diff=$(git diff -U0 --exit-code --color $i) + if [ "$?" -eq 1 ] + then + nb_lines=$(echo "$diff" | wc -l) + if [ "$nb_lines" -eq 7 ] + then + match=$(array_in_array "-9 +9 Updated: by ### ########.fr" "$diff") + if [ $match -eq 1 ] + then + do_checkout + fi + fi + fi + fi + fi + done +fi diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 08c29937..838a9c24 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/22 16:20:31 by gwojda #+# #+# */ -/* Updated: 2017/03/22 17:17:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 15:11:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,7 +55,7 @@ int builtin_env(const char *path, (void)envp; if (bt_env_parse(&data, (char**)argv)) - return (ft_perror() && SH_ERR("usage: %s", ENV_USAGE) ? 0 : 0); + return (ft_perror("env") && 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) diff --git a/42sh/src/builtin/builtin_export.c b/42sh/src/builtin/builtin_export.c index 56e47007..444218b3 100644 --- a/42sh/src/builtin/builtin_export.c +++ b/42sh/src/builtin/builtin_export.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */ -/* Updated: 2017/03/20 14:44:02 by wescande ### ########.fr */ +/* Updated: 2017/03/24 15:11:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ int builtin_export( (void)path; data.flag = 0; if (cliopts_get((char**)av, g_export_opts, &data)) - ft_perror(); + ft_perror("export"); if (data.flag & BT_EXPORT_LP) return (bt_export_print()); av = data.av_data; diff --git a/42sh/src/builtin/builtin_read.c b/42sh/src/builtin/builtin_read.c index ce94d408..abea9035 100644 --- a/42sh/src/builtin/builtin_read.c +++ b/42sh/src/builtin/builtin_read.c @@ -6,14 +6,14 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */ -/* Updated: 2017/03/22 19:21:49 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 15:10:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" #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 ...]" t_cliopts g_read_opts[] = { @@ -36,12 +36,12 @@ int bt_read_init(t_read *data, char **av) data->fd = 0; data->timeout = 0; data->input = NULL; - if ((cliopts_get(av, g_read_opts, data))) - return (ft_perror() ? 2 : 2); if (isatty(STDIN)) data->opts |= BT_READ_INTER; if (bt_read_terminit(data) < 0) - return (-1); + exit (1); + if ((cliopts_get(av, g_read_opts, data))) + return (ft_perror("read")); return (0); } @@ -89,7 +89,8 @@ int bt_read_assign(t_read *data) tok = ft_strtok(input, ifs); while (*names) { - builtin_setenv("setenv", (char*[]){"setenv", *names, tok}, NULL); + if (!(builtin_setenv("setenv", (char*[]){"read", *names, tok}, NULL))) + return (1); ifs = names[1] ? ifs : NULL; tok = ft_strtok(NULL, ifs); names++; @@ -106,16 +107,11 @@ int builtin_read(const char *path, char *const av[], char *const envp[]) (void)envp; ret = 0; if ((ret = bt_read_init(&data, (char **)av)) != 0) - ; + SH_ERR("usage: %s%s", US_READ, US_READ_1); else if ((ret = bt_read_loop(&data))) ; - else if (data.input && bt_read_assign(&data)) - ret = 1; - if (ret == -1) - exit(1); - if (ret != 0) - SH_ERR("usage: read %s %s\n", US_READ, US_READ_1); - if (ret != 2) - bt_read_exit(&data); + else if (data.input && (ret = bt_read_assign(&data))) + ; + bt_read_exit(&data); return (ret); } diff --git a/42sh/src/exec/plaunch_builtin.c b/42sh/src/exec/plaunch_builtin.c index f67b753c..93960d1a 100644 --- a/42sh/src/exec/plaunch_builtin.c +++ b/42sh/src/exec/plaunch_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */ -/* Updated: 2017/03/21 18:10:21 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 14:56:11 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,9 @@ int plaunch_builtin(t_process *p) { - return (*p->data.cmd.execf)( + set_exitstatus((*p->data.cmd.execf)( p->data.cmd.path, p->data.cmd.av, - data_singleton()->env); + data_singleton()->env), 1); + return (0); } diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 5843705c..b044bdbd 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/22 18:26:53 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 14:56:32 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,7 @@ int process_launch(t_process *p) if (process_redirect(p)) set_exitstatus(1, 1); else - set_exitstatus(p->map.launch(p), 1); + p->map.launch(p); shell_resetfds(); shell_resetsig(); process_free(p, 0); diff --git a/42sh/src/glob/command_getoutput.c b/42sh/src/glob/command_getoutput.c index dd024a75..5a96c013 100644 --- a/42sh/src/glob/command_getoutput.c +++ b/42sh/src/glob/command_getoutput.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 19:44:25 by wescande #+# #+# */ -/* Updated: 2017/03/23 03:19:23 by wescande ### ########.fr */ +/* Updated: 2017/03/24 14:47:25 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,10 +44,10 @@ char *command_getoutput(char *command) { close(fds[PIPE_READ]); dup2_close(fds[PIPE_WRITE], STDOUT); - av = ft_sstradd(NULL, data_singleton()->argv[0]); + av = ft_sstradd(NULL, data_singleton()->binary); av = ft_sstradd(av, "-c"); av = ft_sstradd(av, command); - execve(data_singleton()->argv[0], av, data_singleton()->env); + execve(data_singleton()->binary, av, data_singleton()->env); exit(1); } waitpid(pid, &ret, WUNTRACED); diff --git a/42sh/src/job_control/builtin_jobs.c b/42sh/src/job_control/builtin_jobs.c index 15f0e33a..1be75003 100644 --- a/42sh/src/job_control/builtin_jobs.c +++ b/42sh/src/job_control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/03/24 14:08:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 15:12:22 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,7 +74,7 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) } ft_bzero(&data, sizeof(t_data_template)); if (cliopts_get((char**)av, g_jobs_opts, &data)) - return (ft_perror()); + return (ft_perror("jobs")); if (!*data.av_data) bt_jobs_all(data.flag); else if (bt_jobs_spec(data.av_data, data.flag)) diff --git a/42sh/src/lexer/get_state_global.c b/42sh/src/lexer/get_state_global.c index b3b1846c..aa08fe4e 100644 --- a/42sh/src/lexer/get_state_global.c +++ b/42sh/src/lexer/get_state_global.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:39:06 by jhalford #+# #+# */ -/* Updated: 2017/03/24 14:20:30 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 14:51:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,6 @@ t_lexstate get_state_global(t_lexer *lexer) c = lexer->str[lexer->pos]; cn = lexer->str[lexer->pos + 1]; cl = lexer->pos ? lexer->str[lexer->pos - 1] : 0; - DG("%i : '%c'", lexer->pos, cl); ret = 0; if ((ft_is_delim(c) && (ret = DELIM)) || ((c == '&' || c == ';' || c == '|' || c == '!') && (ret = SEP)) diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index cf83e669..46104d14 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/03/23 14:57:19 by ariard ### ########.fr */ +/* Updated: 2017/03/24 13:51:24 by wescande ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,28 @@ extern char **environ; +static int path_binary_save(char *binary) +{ + char *directory; + + if (binary[0] != '/') + { + if (!(directory = getcwd(NULL, 0))) + return (1); + while (binary[0] == '.' && binary[1] == '/') + binary += 2; + binary = ft_strjoin( + directory[ft_strlen(directory) - 1] != '/' ? "/" : "", binary); + binary = ft_strjoinf(directory, binary, 3); + } + else + binary = ft_strdup(binary); + if (access(binary, F_OK | X_OK)) + return (1); + data_singleton()->binary = binary; + return (0); +} + static int localenv_init(void) { t_data *data; @@ -67,15 +89,16 @@ int data_init(int ac, char **av) lexer_init(&data->lexer); parser_init(&data->parser); if ((term_name = ft_getenv(data->env, "TERM")) == NULL) - { term_name = "dumb"; - /* ft_dprintf(2, "{red}TERM not set\n{eoc}"); */ - /* return (-1); */ - } if (tgetent(NULL, term_name) != 1) { ft_dprintf(2, "{red}TERM name is not a tty\n{eoc}"); return (-1); } + if (path_binary_save(av[0])) + { + ft_dprintf(2, "{red}Failed to resolve binary name\n{eoc}"); + return (-1); + } return (0); } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 0d3fa344..b4986179 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ -/* Updated: 2017/03/24 14:55:27 by ariard ### ########.fr */ +/* Updated: 2017/03/24 15:18:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ static int do_readline_routine(char **stream) data = data_singleton(); has_prompt = !(get_lexer_stack(data->lexer) - || data->parser.state == UNDEFINED || data->lexer.state == HEREDOC); + || data->parser.state == UNDEFINED || data->lexer.state == HEREDOC); ret = readline(has_prompt, stream); if (ret == -1) exit(1); @@ -43,10 +43,9 @@ 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); + && SH_NO_INTERACTIVE(data->opts)) + return (ret); else if (ret > 0) break ; } diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 4931fd36..16844a6f 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/03/22 19:36:07 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 15:12:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -89,7 +89,7 @@ int shell_init(int ac, char **av) return (-1); if (cliopts_get(av, g_opts, data)) { - ft_perror(); + ft_perror(NULL); return (SH_ERR("usage: %s", SHELL_USAGE)); } if (!isatty(STDIN) || *data->av_data) diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index a6845ec7..8a3af975 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 16:17:38 by ariard #+# #+# */ -/* Updated: 2017/03/24 15:02:51 by ariard ### ########.fr */ +/* Updated: 2017/03/24 15:18:51 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,7 +49,6 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) parser->state = SUCCESS; else parser->state = UNDEFINED; - btree_print(STDBUG, *ast, &ft_putast); build_tree(ast, token); if ((end_instruction(&parser->stack) && !(*token)->next)) insert_linebreak(token);