Browse Source

[enh] add basic shell script execution as parameter.

Moul 3 years ago
parent
commit
5230434daa
3 changed files with 24 additions and 2 deletions
  1. 3 0
      shell_script.sh
  2. 20 2
      src/main.c
  3. 1 0
      src/sh.h

+ 3 - 0
shell_script.sh

@@ -0,0 +1,3 @@
+ls
+env
+ls | cat | wc -l

+ 20 - 2
src/main.c

@@ -8,12 +8,14 @@ int		main(int ac, char **av, char **env)
 {
 	g_tenv = env;
 	signal(SIGINT, sighandler);
-	if (ac != 1)
+	if (ac > 1)
 	{
 		if (ft_strcmp(av[1], "-c") == 0 && ac == 3 && ft_strlen(av[2]) > 0)
 			comma_split(av[2]);
+		if (ac == 2 && ft_strcmp(av[1], "-c") != 0)
+			shell_script(av[1]);
 		else
-			ft_putendl("./21sh -c <command>");
+			ft_putendl("./21sh -c <command>\n./21sh <shell_script>");
 		return (0);
 	}
 	open_shell();
@@ -21,6 +23,22 @@ int		main(int ac, char **av, char **env)
 }
 
 /*
+** Run shell script
+** Read lines of the file
+** Run the line
+*/
+
+void	shell_script(char *script_name)
+{
+	int		fd;
+	char	*line;
+
+	fd = open(script_name, O_RDONLY);
+	while (get_next_line(fd, &line) != 0)
+		comma_split(line);
+}
+
+/*
 ** Display prompt.
 ** Read on standard stream.
 */

+ 1 - 0
src/sh.h

@@ -20,6 +20,7 @@ char	**g_path;
 /*
 ** main.c
 */
+void	shell_script(char *script_name);
 void	open_shell();
 void	comma_split(char *cmd);
 void	split_cmd_and_his_options(char *cmd);