Browse Source

[mod] add non used code to parse cotes.
- cote parsing must be done before comma, redirections, and pipe parsing
- update readme accordingly.

Moul 3 years ago
parent
commit
193e0e7341
2 changed files with 167 additions and 4 deletions
  1. 4 4
      README.md
  2. 163 0
      non_use_src/cmd_parsing_cotes.c

+ 4 - 4
README.md

@@ -29,10 +29,10 @@ Three entries:
 - Script as sub-cmd
 - Open Shell
  - Termcaps
-  - History
-  - Herething
-   - Handle semicolons
-    - Handle line parsing
+  - Handle line parsing
+   - History
+   - Herething
+    - Handle semicolons
      - Launch redirections
      - Launch pipes
       - Launch executions

+ 163 - 0
non_use_src/cmd_parsing_cotes.c

@@ -0,0 +1,163 @@
+#include "sh.h"
+
+# À mettre dans sh.h
+
+/*
+** cmd_parsing_cotes.c
+*/
+char	**parse_cotes(char *s);
+void	detect_types(char *s, t_cotes *c);
+void	handle_non_coted_parsing(char *s, t_cotes *c);
+void	parse_non_coted(char *s, t_cotes *c, int len);
+
+cmd_opt[i] = parse_cotes(cmd_pipe[i]);
+	à mettre dans pipe.c à la place de
+cmd_opt[i] = cmd_split(cmd_pipe[i], ' ', '\t');
+
+————————————————————————————————————————————————
+
+/*
+** len: arbitrary value which limit to 50 blocks
+** Handle variables
+** Copy pointer c->cmd and free struct c
+*/
+
+char	**parse_cotes(char *s)
+{
+	int		len;
+	t_cotes *c;
+	char	**cmd;
+
+	len = 50;
+	c = (t_cotes*)malloc(sizeof(t_cotes));
+	c->cmd = (char**)malloc(sizeof(char*) * (len + 1));
+	c->param = "\'\"`";
+	c->pos = 0;
+	c->j = 0;
+	detect_types(s, c);
+	c->cmd[c->j] = NULL;
+	cmd = c->cmd;
+	free(c);
+//ft_puttab(cmd); // debug
+	return (cmd);
+}
+
+/*
+** Till string check is not arrived to the end:
+** - Check three cotes types:
+**  - if spaces: increment position and stop at the end
+**  - If, non-coted blocks: launch f()
+**  - Else if, found first and second cotes:
+**   - retrieve part between cotes
+**   - change new position cursor at the end of block coted, after the cote
+**   - break first loop to restart checking for blocks for all cotes types
+*/
+
+void	detect_types(char *s, t_cotes *c)
+{
+	int		first;
+	int		snd;
+
+	while (s[c->pos] != '\0')
+	{
+//ft_putendl("DEBUG 2"); //debug
+		c->i = -1;
+		while (c->param[++c->i] != '\0')
+		{
+//ft_putendl("DEBUG 1"); //debug
+			while ((s[c->pos] == ' ' || s[c->pos] == '\t') && s[c->pos] != '\0')
+			{
+				c->pos++;
+//ft_putendl("c->pos++"); // debug
+			}
+//ft_putnbrn(ft_search_pos_char(&s[c->pos], c->param[c->i])); // debug
+//ft_printf("%s: %d\n", "position sur la string", c->pos); // debug
+//ft_printf("%s\n", &s[c->pos]); //debug
+			if (ft_isvisible(s[c->pos]) && s[c->pos] != '\'' \
+					&& s[c->pos] != '\"' && s[c->pos] != '`')
+			{
+//ft_putendl("DEBUG 3: non-coted"); //debug
+				handle_non_coted_parsing(s, c);
+			}
+			else if ((first = ft_search_pos_char(&s[c->pos], c->param[c->i]))\
+				!= -1 && (snd = \
+				ft_search_pos_char(&s[c->pos + first + 1], c->param[c->i])) != -1)
+			{
+				first += c->pos;
+				snd += first + 1;
+//ft_putendl("DEBUG 4: coted"); //debug
+//ft_printf("Cut from position: %d for an lengh of %d\n", first + 1, snd - first - 1); // debug
+				c->cmd[c->j++] = ft_strsub(s, first + 1, snd - first - 1);
+//ft_printf("c->pos: %d|  first: %d|  snd: %d\n", c->pos, first, snd);
+				c->pos = snd + 1;
+//ft_printf("c->pos: %d\n", c->pos); // debug
+				break ;
+			}
+//ft_printf("%s: %d\n", "longueur de la string", ft_strlen(s)); // debug
+//ft_printf("|%s|\n", c->cmd[c->j - 1]); // debug
+		}
+	}
+}
+
+/*
+** for every cotes type:
+**  search position of len between current pos and next cote
+** launch parsing: if there is a cote or till string end if there is no cote
+*/
+
+void	handle_non_coted_parsing(char *s, t_cotes *c)
+{
+	int		i;
+	int		len;
+
+	i = -1;
+	while (c->param[++i] != '\0')
+		if ((len = ft_search_pos_char(&s[c->pos], c->param[i])) != -1)
+		{
+			parse_non_coted(s, c, len);
+			return ;
+		}
+	parse_non_coted(s, c, ft_strlen(s));
+}
+
+/*
+** retrieve this part of the string from actual pos and len cote pos
+** split it at white chars
+** distribute cmd splited on general array
+** change position after block retrieved
+*/
+
+void	parse_non_coted(char *s, t_cotes *c, int len)
+{
+	int		k;
+	char	**cmds;
+
+	k = -1;
+	cmds = cmd_split(ft_strsub(s, c->pos, len), ' ', '\t');
+	while (cmds[++k] != NULL)
+		c->cmd[c->j++] = cmds[k];
+	c->pos += len;
+}
+
+—————————————————————————————————————————
+
+# À mettre dans struct.h
+
+/*
+** coted parsing struct
+** pos: current parsing position on checking string
+** cmd: array containing splitted cmd and subcmd at spaces and cotes 
+** j: position in cmd
+** param: str containing cotes
+** i: position in param list
+*/
+
+typedef struct			s_cotes {
+	int					pos;
+	char				**cmd;
+	int					j;
+	char				*param;
+	int					i;
+}						t_cotes;
+
+#endif