herething.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "sh.h"
  2. /*
  3. ** Check if there is only one parenthesis and not an other
  4. ** If not found quit herething
  5. ** return boolean value if herething have been executed
  6. */
  7. int herething(t_e *e)
  8. {
  9. char *fc;
  10. int pos;
  11. pos = -1;
  12. while (e->par_open[++pos] != '\0')
  13. if ((fc = ft_strchr(e->cmd, e->par_open[pos])) != NULL \
  14. && ft_strchr(&fc[1], e->par_close[pos]) == NULL)
  15. {
  16. exec_herething(e, pos, fc);
  17. return (1);
  18. }
  19. return (0);
  20. }
  21. /*
  22. ** Store command in a tab
  23. ** Store in cmd the command to be saved in history
  24. ** Remove cotes
  25. ** Fork and exec command
  26. ** Wait son fork ends
  27. ** Free memory
  28. */
  29. void exec_herething(t_e *e, int pos, char *fc)
  30. {
  31. char **cmd;
  32. cmd = (char**)malloc(sizeof(char*) * 3);
  33. cmd[0] = ft_strdup_till(e->cmd, fc[0]);
  34. cmd[0] = ft_assign_free_str(cmd[0], ft_strtrim);
  35. cmd[1] = prompt_herething(e, pos, fc);
  36. cmd[2] = NULL;
  37. ft_strdel(&e->cmd);
  38. e->cmd = ft_strjoinf(cmd[0], ft_strjoin(" ", cmd[1]), 2);
  39. remove_cotes(cmd);
  40. if (manage_cmd_1(e, cmd) == 0 && fork() == 0)
  41. manage_cmd_2(e, cmd);
  42. wait(NULL);
  43. ft_strdel(&cmd[0]);
  44. }
  45. /*
  46. ** Prompt and buffer
  47. */
  48. char *prompt_herething(t_e *e, int pos, char *fc)
  49. {
  50. t_e *h;
  51. char *txt;
  52. h = (t_e*)malloc(sizeof(t_e));
  53. h->cmd = ft_strnew(0);
  54. txt = ft_strdup(fc);
  55. while (ft_strchr(h->cmd, e->par_close[pos]) == NULL)
  56. {
  57. txt = ft_strjoinf(txt, ft_strjoinf(h->cmd, "\n", 1), 3);
  58. h->cmd = ft_strnew(0);
  59. ft_putstr("\n>");
  60. here_edition(h, 0);
  61. }
  62. ft_putchar('\n');
  63. txt = ft_strjoinf(txt, h->cmd, 3);
  64. free(h);
  65. return (txt);
  66. }