From 1b9d0540da6b26410966a15acf6e2190b8462e3b Mon Sep 17 00:00:00 2001 From: wescande Date: Fri, 24 Mar 2017 14:49:46 +0100 Subject: [PATCH] rectif bquote quand on change de dossier + ajout option -v pour le script + je sais plus trop quoi mais cest good --- 42sh/includes/minishell.h | 3 +- 42sh/scriptheader.sh | 83 +++++++++++++++++++++---------- 42sh/src/glob/command_getoutput.c | 6 +-- 42sh/src/main/data_init.c | 33 ++++++++++-- 42sh/src/main/main.c | 8 +-- 5 files changed, 93 insertions(+), 40 deletions(-) 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/scriptheader.sh b/42sh/scriptheader.sh index 9dd5cec2..e2c1f881 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 @@ -59,43 +62,69 @@ do_checkout () echo "$cya $i was checked out. A copy still exist in $HOME/Documents/.$i_tmp.back$res\n" } -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/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/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 2b2f966c..2777064d 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/23 16:58:43 by ariard ### ########.fr */ +/* Updated: 2017/03/24 14:47:30 by wescande ### ########.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); @@ -44,8 +44,8 @@ static int handle_instruction(t_list **token, t_btree **ast) if (do_lexer_routine(token, stream) > 0) continue ; 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 ; }