main.c 955 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "sh.h"
  2. /*
  3. ** Handle -c command
  4. */
  5. int main(int ac, char **av, char **env)
  6. {
  7. t_e *e;
  8. g_tenv = env;
  9. e = (t_e*)malloc(sizeof(t_e));
  10. signal(SIGINT, sighandler);
  11. if (ac > 1)
  12. {
  13. if (ft_strcmp(av[1], "-c") == 0 && ac == 3)
  14. comma_split(e, av[2]);
  15. else if (ac == 2 && ft_strcmp(av[1], "-c") != 0)
  16. shell_script(e, av[1]);
  17. else
  18. ft_putendl("./21sh -c <command>\n./21sh <shell_script>");
  19. return (0);
  20. }
  21. e->linep = 0;
  22. term(e);
  23. return (0);
  24. }
  25. /*
  26. ** Run shell script
  27. ** Read lines of the file
  28. ** Run the line
  29. */
  30. void shell_script(t_e *e, char *script_name)
  31. {
  32. int fd;
  33. char *line;
  34. fd = open(script_name, O_RDONLY);
  35. while (get_next_line(fd, &line) != 0)
  36. comma_split(e, line);
  37. }
  38. /*
  39. ** Split at commas ';'.
  40. ** Launch redirections
  41. */
  42. void comma_split(t_e *e, char *line)
  43. {
  44. char **cmds;
  45. int i;
  46. i = -1;
  47. if ((cmds = ft_strsplit(line, ';')) != NULL)
  48. while (cmds[++i] != NULL)
  49. parse_redirection(e, cmds[i]);
  50. }