cmd_parsing_cotes.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "sh.h"
  2. # À mettre dans sh.h
  3. /*
  4. ** cmd_parsing_cotes.c
  5. */
  6. char **parse_cotes(char *s);
  7. void detect_types(char *s, t_cotes *c);
  8. void handle_non_coted_parsing(char *s, t_cotes *c);
  9. void parse_non_coted(char *s, t_cotes *c, int len);
  10. cmd_opt[i] = parse_cotes(cmd_pipe[i]);
  11. à mettre dans pipe.c à la place de
  12. cmd_opt[i] = cmd_split(cmd_pipe[i], ' ', '\t');
  13. ————————————————————————————————————————————————
  14. /*
  15. ** len: arbitrary value which limit to 50 blocks
  16. ** Handle variables
  17. ** Copy pointer c->cmd and free struct c
  18. */
  19. char **parse_cotes(char *s)
  20. {
  21. int len;
  22. t_cotes *c;
  23. char **cmd;
  24. len = 50;
  25. c = (t_cotes*)malloc(sizeof(t_cotes));
  26. c->cmd = (char**)malloc(sizeof(char*) * (len + 1));
  27. c->param = "\'\"`";
  28. c->pos = 0;
  29. c->j = 0;
  30. detect_types(s, c);
  31. c->cmd[c->j] = NULL;
  32. cmd = c->cmd;
  33. free(c);
  34. //ft_puttab(cmd); // debug
  35. return (cmd);
  36. }
  37. /*
  38. ** Till string check is not arrived to the end:
  39. ** - Check three cotes types:
  40. ** - if spaces: increment position and stop at the end
  41. ** - If, non-coted blocks: launch f()
  42. ** - Else if, found first and second cotes:
  43. ** - retrieve part between cotes
  44. ** - change new position cursor at the end of block coted, after the cote
  45. ** - break first loop to restart checking for blocks for all cotes types
  46. */
  47. void detect_types(char *s, t_cotes *c)
  48. {
  49. int first;
  50. int snd;
  51. while (s[c->pos] != '\0')
  52. {
  53. //ft_putendl("DEBUG 2"); //debug
  54. c->i = -1;
  55. while (c->param[++c->i] != '\0')
  56. {
  57. //ft_putendl("DEBUG 1"); //debug
  58. while ((s[c->pos] == ' ' || s[c->pos] == '\t') && s[c->pos] != '\0')
  59. {
  60. c->pos++;
  61. //ft_putendl("c->pos++"); // debug
  62. }
  63. //ft_putnbrn(ft_search_pos_char(&s[c->pos], c->param[c->i])); // debug
  64. //ft_printf("%s: %d\n", "position sur la string", c->pos); // debug
  65. //ft_printf("%s\n", &s[c->pos]); //debug
  66. if (ft_isvisible(s[c->pos]) && s[c->pos] != '\'' \
  67. && s[c->pos] != '\"' && s[c->pos] != '`')
  68. {
  69. //ft_putendl("DEBUG 3: non-coted"); //debug
  70. handle_non_coted_parsing(s, c);
  71. }
  72. else if ((first = ft_search_pos_char(&s[c->pos], c->param[c->i]))\
  73. != -1 && (snd = \
  74. ft_search_pos_char(&s[c->pos + first + 1], c->param[c->i])) != -1)
  75. {
  76. first += c->pos;
  77. snd += first + 1;
  78. //ft_putendl("DEBUG 4: coted"); //debug
  79. //ft_printf("Cut from position: %d for an lengh of %d\n", first + 1, snd - first - 1); // debug
  80. c->cmd[c->j++] = ft_strsub(s, first + 1, snd - first - 1);
  81. //ft_printf("c->pos: %d| first: %d| snd: %d\n", c->pos, first, snd);
  82. c->pos = snd + 1;
  83. //ft_printf("c->pos: %d\n", c->pos); // debug
  84. break ;
  85. }
  86. //ft_printf("%s: %d\n", "longueur de la string", ft_strlen(s)); // debug
  87. //ft_printf("|%s|\n", c->cmd[c->j - 1]); // debug
  88. }
  89. }
  90. }
  91. /*
  92. ** for every cotes type:
  93. ** search position of len between current pos and next cote
  94. ** launch parsing: if there is a cote or till string end if there is no cote
  95. */
  96. void handle_non_coted_parsing(char *s, t_cotes *c)
  97. {
  98. int i;
  99. int len;
  100. i = -1;
  101. while (c->param[++i] != '\0')
  102. if ((len = ft_search_pos_char(&s[c->pos], c->param[i])) != -1)
  103. {
  104. parse_non_coted(s, c, len);
  105. return ;
  106. }
  107. parse_non_coted(s, c, ft_strlen(s));
  108. }
  109. /*
  110. ** retrieve this part of the string from actual pos and len cote pos
  111. ** split it at white chars
  112. ** distribute cmd splited on general array
  113. ** change position after block retrieved
  114. */
  115. void parse_non_coted(char *s, t_cotes *c, int len)
  116. {
  117. int k;
  118. char **cmds;
  119. k = -1;
  120. cmds = cmd_split(ft_strsub(s, c->pos, len), ' ', '\t');
  121. while (cmds[++k] != NULL)
  122. c->cmd[c->j++] = cmds[k];
  123. c->pos += len;
  124. }
  125. —————————————————————————————————————————
  126. # À mettre dans struct.h
  127. /*
  128. ** coted parsing struct
  129. ** pos: current parsing position on checking string
  130. ** cmd: array containing splitted cmd and subcmd at spaces and cotes
  131. ** j: position in cmd
  132. ** param: str containing cotes
  133. ** i: position in param list
  134. */
  135. typedef struct s_cotes {
  136. int pos;
  137. char **cmd;
  138. int j;
  139. char *param;
  140. int i;
  141. } t_cotes;
  142. #endif