78 lines
2.2 KiB
C
78 lines
2.2 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* fill_token.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2021/10/19 08:38:55 by lperrey #+# #+# */
|
|
/* Updated: 2021/10/30 22:35:01 by lperrey ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "minishell.h"
|
|
int check_operators(t_token *t, char *input, int *i, int *t_i);
|
|
|
|
enum e_in_quote_state
|
|
{
|
|
NOT_IN = 0,
|
|
IN_QUOTES = '\'',
|
|
IN_DQUOTES = '\"'
|
|
};
|
|
|
|
int fill_token(t_token *t, char *input, int *i, int *t_i)
|
|
{
|
|
static int in_quotes = 0;
|
|
|
|
// operators
|
|
if (!in_quotes)
|
|
{
|
|
if (check_operators(t, input, i, t_i) == DELIMITE_TOKEN)
|
|
return (DELIMITE_TOKEN);
|
|
}
|
|
// quoting
|
|
if (input[*i] == '\'' && in_quotes != IN_DQUOTES)
|
|
{
|
|
t->content[(*t_i)++] = input[(*i)++];
|
|
if (in_quotes == IN_QUOTES)
|
|
in_quotes = 0;
|
|
else if (ft_strchr(&input[*i], '\'')) // if closed quotes
|
|
in_quotes = IN_QUOTES;
|
|
return (CONTINUE_TOKEN);
|
|
}
|
|
else if (input[*i] == '\"' && in_quotes != IN_QUOTES)
|
|
{
|
|
t->content[(*t_i)++] = input[(*i)++];
|
|
if (in_quotes == IN_DQUOTES)
|
|
in_quotes = 0;
|
|
else if (ft_strchr(&input[*i], '\"')) // if closed dquotes
|
|
in_quotes = IN_DQUOTES;
|
|
return (CONTINUE_TOKEN);
|
|
}
|
|
// blanks
|
|
if (!in_quotes && (input[*i] == ' ' || input[*i] == '\t'))
|
|
{
|
|
while (input[*i] == ' ' || input[*i] == '\t')
|
|
(*i)++;
|
|
return (DELIMITE_TOKEN);
|
|
}
|
|
else
|
|
t->content[(*t_i)++] = input[(*i)++];
|
|
return (CONTINUE_TOKEN);
|
|
}
|
|
|
|
/*
|
|
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03
|
|
1 - OK
|
|
2 - OK
|
|
3 - OK
|
|
4 - OK
|
|
5 - OK / SEMI-OSEF
|
|
6 - OK
|
|
7 - OK
|
|
8 - OK
|
|
9 - OSEF
|
|
10 - OK
|
|
|
|
*/
|