main.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. e = (t_e*)malloc(sizeof(t_e));
  9. e->env = env;
  10. e->path = ft_strsplit(get_tenv(e, "PATH"), ':');
  11. e->par_open = "\'\"`[({";
  12. e->par_close = "\'\"`])}";
  13. signal(SIGINT, sighandler);
  14. if (ac > 1)
  15. {
  16. if (ft_strcmp(av[1], "-c") == 0 && ac == 3)
  17. {
  18. init_detect_blocks(e, av[2]);
  19. comma_split(e, av[2]);
  20. }
  21. else if (ac == 2 && ft_strcmp(av[1], "-c") != 0)
  22. shell_script(e, av[1]);
  23. else
  24. ft_putendl("./21sh -c <command>\n./21sh <shell_script>");
  25. return (0);
  26. }
  27. e->linep = 0;
  28. prompt_get_cmd_keys(e);
  29. return (0);
  30. }
  31. /*
  32. ** Run shell script
  33. ** Read lines of the file
  34. ** Run the line
  35. */
  36. void shell_script(t_e *e, char *script_name)
  37. {
  38. int fd;
  39. char *line;
  40. if ((fd = open(script_name, O_RDONLY)) < 0)
  41. error_message("File do not exist", script_name, "exit");
  42. while (get_next_line(fd, &line) != 0)
  43. {
  44. init_detect_blocks(e, line);
  45. comma_split(e, line);
  46. }
  47. }
  48. /*
  49. ** Split at commas ';'.
  50. ** If string not empty:
  51. ** - Launch redirections
  52. ** Calculate len on the string to compare coted block detection
  53. */
  54. void comma_split(t_e *e, char *line)
  55. {
  56. char **cmds;
  57. int i;
  58. i = -1;
  59. e->slen = 0;
  60. if ((cmds = cmd_splitting(e, line, ';')) != NULL)
  61. while (cmds[++i] != NULL)
  62. {
  63. if (ft_strcmp(cmds[i], "") != 0)
  64. pipe_init(e, cmds[i]);
  65. e->slen += ft_strlen(cmds[i]);
  66. }
  67. free(cmds);
  68. }