aboutsummaryrefslogtreecommitdiff
path: root/src/lexer/utils.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-16 16:40:22 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-16 16:40:22 +0200
commit3bb997212b3a0d140a34932f9deff52793673d49 (patch)
tree2df7ed965a0a29a1c47442831218cdf553633dfb /src/lexer/utils.c
parente439b71d807529734f04ce9d78b98c12022e7c72 (diff)
downloadminishell-3bb997212b3a0d140a34932f9deff52793673d49.tar.gz
minishell-3bb997212b3a0d140a34932f9deff52793673d49.tar.bz2
minishell-3bb997212b3a0d140a34932f9deff52793673d49.zip
Added g_state to store global variables, Refactoring tok_assign_str
Diffstat (limited to 'src/lexer/utils.c')
-rw-r--r--src/lexer/utils.c87
1 files changed, 36 insertions, 51 deletions
diff --git a/src/lexer/utils.c b/src/lexer/utils.c
index b4846d6..66b2af7 100644
--- a/src/lexer/utils.c
+++ b/src/lexer/utils.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/16 08:18:15 by nahaddac #+# #+# */
-/* Updated: 2020/09/14 15:16:35 by charles ### ########.fr */
+/* Updated: 2020/09/16 16:37:20 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,25 +15,25 @@
// return token tag corresponding to string id
enum e_tok tok_assign_tag(char *content)
{
- if (content[0] == ';')
- return (TAG_END);
- if (ft_strncmp(content, "&&", 2) == 0)
- return (TAG_AND);
- if (ft_strncmp(content, "||", 2) == 0)
- return (TAG_OR);
- if(content[0] == '|')
- return (TAG_PIPE);
- if (content[0] == '>')
- return (TAG_REDIR_OUT);
- if (content[0] == '<')
- return (TAG_REDIR_IN);
- if (ft_strncmp(content, ">>", 2) == 0)
- return (TAG_REDIR_APPEND);
- if (content[0] == '(')
- return (TAG_PARENT_OPEN);
- if (content[0] == ')')
- return (TAG_PARENT_CLOSE);
- return (0);
+ if (content[0] == ';')
+ return (TAG_END);
+ if (ft_strncmp(content, "&&", 2) == 0)
+ return (TAG_AND);
+ if (ft_strncmp(content, "||", 2) == 0)
+ return (TAG_OR);
+ if(content[0] == '|')
+ return (TAG_PIPE);
+ if (content[0] == '>')
+ return (TAG_REDIR_OUT);
+ if (content[0] == '<')
+ return (TAG_REDIR_IN);
+ if (ft_strncmp(content, ">>", 2) == 0)
+ return (TAG_REDIR_APPEND);
+ if (content[0] == '(')
+ return (TAG_PARENT_OPEN);
+ if (content[0] == ')')
+ return (TAG_PARENT_CLOSE);
+ return (0);
}
enum e_tok tok_assign_stick(t_tok_lst *tok)
@@ -49,35 +49,20 @@ enum e_tok tok_assign_stick(t_tok_lst *tok)
enum e_tok tok_assign_str(t_tok_lst *tok)
{
- int i;
+ char *found;
- // could use strchr to search ' or "
- i = 0;
- while (tok->content[i] != '\0')
- {
- if (tok->content[i] == '\'')
- {
- tok->tag = TAG_STR_SINGLE;
- return (tok_assign_stick(tok));
- }
- if (tok->content[i] == '"')
- {
- tok->tag = TAG_STR_DOUBLE;
- return (tok_assign_stick(tok));
- }
- else
- {
- tok->tag = TAG_STR;
- return (tok_assign_stick(tok));
- }
- i++;
- }
- return (0);
+ found = ft_strpbrk(tok->content, "'\"");
+ if (found == NULL)
+ tok->tag = TAG_STR;
+ else if (*found == '\'')
+ tok->tag = TAG_STR_SINGLE;
+ else if (*found == '"')
+ tok->tag = TAG_STR_DOUBLE;
+ return (tok_assign_stick(tok));
}
// check is char is separator
-// & alone could be considered a separator
int lexer_sep(char c)
{
return (ft_strchr(";&|><()", c) != NULL);
@@ -94,14 +79,14 @@ int quote_len(char *input, int i)
char quote_type;
quote_type = input[i];
- i++;
- while (input[i] != quote_type && input[i] != '\0')
- {
- if (quote_type == '"' && input[i] == '\\')
+ i++;
+ while (input[i] != quote_type && input[i] != '\0')
+ {
+ if (quote_type == '"' && input[i] == '\\')
i++;
- i++;
- }
+ i++;
+ }
while (ft_isblank(input[i + 1]))
i++;
- return (i + 1);
+ return (i + 1);
}