diff --git a/42sh/includes/.readscript.h.swp b/42sh/includes/.readscript.h.swp index 69ba5059..747c167a 100644 Binary files a/42sh/includes/.readscript.h.swp and b/42sh/includes/.readscript.h.swp differ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index e9f22dc9..279d7450 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/01/19 21:03:13 by ariard ### ########.fr */ +/* Updated: 2017/01/22 22:42:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 47191403..e0a4a1f4 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/01/22 00:37:20 by ariard ### ########.fr */ +/* Updated: 2017/01/22 22:50:22 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,8 +41,9 @@ struct s_comp # define SHELL_OPTS_LC (1 << 1) # define SHELL_MODE_INPUT (1 << 2) # define SHELL_MODE_EXEC (1 << 3) +# define SHELL_MODE_SCRIPT (1 << 4) -# define SHELL_MODE_MASK (SHELL_MODE_INPUT | SHELL_MODE_EXEC) +# define SHELL_MODE_MASK (SHELL_MODE_INPUT | SHELL_MODE_EXEC | SHELL_MODE_SCRIPT) # define SHELL_HAS_JOBC(b) (b & SHELL_OPTS_JOBC) # define SHELL_MSG_NOJOBC "no job-control" diff --git a/42sh/includes/readscript.h b/42sh/includes/readscript.h index 11228a50..d12f2cc3 100644 --- a/42sh/includes/readscript.h +++ b/42sh/includes/readscript.h @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/21 22:50:22 by ariard #+# #+# */ -/* Updated: 2017/01/22 21:24:47 by ariard ### ########.fr */ +/* Updated: 2017/01/22 22:19:32 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,4 +37,6 @@ typedef struct s_script t_script; int ft_read_script(char *file); +int rs_shebang_check(char *file, t_script *script); + #endif diff --git a/42sh/script.sh b/42sh/script.sh index 8fb9fff0..4065c7cb 100755 --- a/42sh/script.sh +++ b/42sh/script.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/yolosh while [ 1 ]; do sleep 1 echo "a" diff --git a/42sh/src/exec/exec_ampersand.c b/42sh/src/exec/exec_ampersand.c index ad4123d8..a3ab0fa9 100644 --- a/42sh/src/exec/exec_ampersand.c +++ b/42sh/src/exec/exec_ampersand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:01:30 by jhalford #+# #+# */ -/* Updated: 2017/01/11 14:41:54 by jhalford ### ########.fr */ +/* Updated: 2017/01/22 22:08:34 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 6b2c2562..17da9096 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2017/01/12 13:14:35 by jhalford ### ########.fr */ +/* Updated: 2017/01/22 22:16:06 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index b656db94..462674d4 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ -/* Updated: 2017/01/21 22:56:51 by ariard ### ########.fr */ +/* Updated: 2017/01/22 22:08:22 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index 9857891f..ca0b0bbe 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/01/20 00:12:56 by ariard ### ########.fr */ +/* Updated: 2017/01/22 22:11:35 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_setexec.c b/42sh/src/exec/process_setexec.c index 2ce1d0a3..db7afc51 100644 --- a/42sh/src/exec/process_setexec.c +++ b/42sh/src/exec/process_setexec.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ -/* Updated: 2017/01/11 18:01:36 by jhalford ### ########.fr */ +/* Updated: 2017/01/22 22:16:52 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/ft_readline.c b/42sh/src/line-editing/ft_readline.c index e7b4a105..90f36e9e 100644 --- a/42sh/src/line-editing/ft_readline.c +++ b/42sh/src/line-editing/ft_readline.c @@ -6,7 +6,7 @@ /* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/08 18:03:48 by sbenning #+# #+# */ -/* Updated: 2017/01/22 21:06:24 by ariard ### ########.fr */ +/* Updated: 2017/01/22 21:43:48 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/rl_merge_line.c b/42sh/src/line-editing/rl_merge_line.c index 50ee04af..91a3f8ff 100644 --- a/42sh/src/line-editing/rl_merge_line.c +++ b/42sh/src/line-editing/rl_merge_line.c @@ -6,7 +6,7 @@ /* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/09 01:25:03 by sbenning #+# #+# */ -/* Updated: 2017/01/21 22:34:25 by ariard ### ########.fr */ +/* Updated: 2017/01/22 21:56:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/line-editing/rl_setup.c b/42sh/src/line-editing/rl_setup.c index c505c991..4870c0a7 100644 --- a/42sh/src/line-editing/rl_setup.c +++ b/42sh/src/line-editing/rl_setup.c @@ -6,7 +6,7 @@ /* By: sbenning +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/08 18:07:47 by sbenning #+# #+# */ -/* Updated: 2017/01/22 21:25:10 by ariard ### ########.fr */ +/* Updated: 2017/01/22 22:46:09 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/.main.c.swo b/42sh/src/main/.main.c.swo index 457e93eb..2ec104e9 100644 Binary files a/42sh/src/main/.main.c.swo and b/42sh/src/main/.main.c.swo differ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 98bf0967..e64ce801 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/01/22 21:18:37 by ariard ### ########.fr */ +/* Updated: 2017/01/22 22:52:41 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,7 +50,10 @@ int main(int ac, char **av) return (0); } if (ft_read_script(av[1])) - return (0); + { + shell_single_command(data_singleton()->line.input); + return (0); + } while (1) { if (ft_readline()) diff --git a/42sh/src/main/shell_get_opts.c b/42sh/src/main/shell_get_opts.c index 04fb5a22..9096ee5e 100644 --- a/42sh/src/main/shell_get_opts.c +++ b/42sh/src/main/shell_get_opts.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/11 14:04:48 by jhalford #+# #+# */ -/* Updated: 2017/01/19 21:23:17 by ariard ### ########.fr */ +/* Updated: 2017/01/22 22:18:56 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index eca82051..c6f549e0 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/01/22 20:56:35 by ariard ### ########.fr */ +/* Updated: 2017/01/22 21:58:53 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/read-script/.ft_read_script.c.swp b/42sh/src/read-script/.ft_read_script.c.swp index cfd7b8e5..48f63c7a 100644 Binary files a/42sh/src/read-script/.ft_read_script.c.swp and b/42sh/src/read-script/.ft_read_script.c.swp differ diff --git a/42sh/src/line-editing/.ft_readline.c.swp b/42sh/src/read-script/.rs_shebang.c.swp similarity index 69% rename from 42sh/src/line-editing/.ft_readline.c.swp rename to 42sh/src/read-script/.rs_shebang.c.swp index b02632a5..7e751dc1 100644 Binary files a/42sh/src/line-editing/.ft_readline.c.swp and b/42sh/src/read-script/.rs_shebang.c.swp differ diff --git a/42sh/src/read-script/ft_read_script.c b/42sh/src/read-script/ft_read_script.c index 4e674620..92a11f9d 100644 --- a/42sh/src/read-script/ft_read_script.c +++ b/42sh/src/read-script/ft_read_script.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/21 22:49:31 by ariard #+# #+# */ -/* Updated: 2017/01/22 21:39:49 by ariard ### ########.fr */ +/* Updated: 2017/01/22 22:19:42 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,8 @@ int ft_read_script(char *file) return (-1); if (rs_loop(file, script) < 0) return (-1); + if (rs_shebang_check(file, script) < 0) + return (-1); script->buffer = NULL; DG("{inv}{bol}{gre}read of script{eoc} script detected"); return (1); diff --git a/42sh/src/read-script/rs_shebang.c b/42sh/src/read-script/rs_shebang.c new file mode 100644 index 00000000..090cb834 --- /dev/null +++ b/42sh/src/read-script/rs_shebang.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* rs_shebang.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/22 21:42:27 by ariard #+# #+# */ +/* Updated: 2017/01/22 22:53:34 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static char *rs_shebang_parse_interpreter(char *shebang) +{ + int i; + + i = ft_strlen(shebang) - 1; + while (shebang[i]) + { + if (shebang[i] == '/') + return (&shebang[++i]); + i--; + } + return (NULL); +} + +static int rs_valid_interpreter(char *interpreter) +{ + if (ft_strncmp(interpreter, "bash", ft_strlen("bash")) == 0) + return (1); + if (ft_strncmp(interpreter, "csh", ft_strlen("csh")) == 0) + return (1); + if (ft_strncmp(interpreter, "ksh", ft_strlen("bash")) == 0) + return (1); + if (ft_strncmp(interpreter, "sh", ft_strlen("bash")) == 0) + return (1); + if (ft_strncmp(interpreter, "tcsh", ft_strlen("bash")) == 0) + return (1); + if (ft_strncmp(interpreter, "tcsh", ft_strlen("bash")) == 0) + return (1); + if (ft_strncmp(interpreter, "minishell", ft_strlen("minishell")) == 0) + return (1); + return (0); +} + +static void rs_set_line_input(char *interpreter, char *file) +{ + char *buffer; + + buffer = ft_strnew(1024); + ft_strncpy(buffer, interpreter, ft_strlen(interpreter)); + ft_strncat(buffer, " ", 1); + ft_strncat(buffer, file, ft_strlen(file)); + data_singleton()->line.input = buffer; + data_singleton()->opts &= ~SHELL_MODE_MASK; + data_singleton()->opts |= SHELL_MODE_INPUT; +} + +int rs_shebang_check(char *file, t_script *script) +{ + char *interpreter; + + if (!script || !script->queue || !script->queue->next) + return (0); + interpreter = rs_shebang_parse_interpreter(script->queue->next->content); + if (interpreter == NULL || rs_valid_interpreter(interpreter) == 0) + rs_set_line_input("sh", file); + else if (ft_strncmp(interpreter, "minishell", ft_strlen("minishell")) != 0) + rs_set_line_input(interpreter, file); + else if (ft_strncmp(interpreter, "minishell", ft_strlen("minishell")) == 0) + { + data_singleton()->opts &= ~SHELL_MODE_MASK; + data_singleton()->opts |= SHELL_MODE_EXEC; + } + return (0); +}