variables d'environnement dans le parsing
This commit is contained in:
212
main.c
212
main.c
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/06 22:21:44 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/02/09 14:37:48 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -23,10 +23,62 @@ char *get_prompt(void)
|
||||
res = ft_strjoin_free(res, getenv("USER"),1);
|
||||
res = ft_strjoin_free(res, "@",1);
|
||||
res = ft_strjoin_free(res, "minishell \001\033[0m\002$>",1);
|
||||
return (res);
|
||||
}
|
||||
|
||||
char *ft_getenv(t_env *env, char *name)
|
||||
{
|
||||
while(env)
|
||||
{
|
||||
if(!ft_strcmp(env->name, name))
|
||||
return(env->value);
|
||||
env = env->next;
|
||||
}
|
||||
return(0);
|
||||
}
|
||||
|
||||
int get_var_name_len(char *command)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = 0;
|
||||
if(command[res] == '?')
|
||||
return(1);
|
||||
while(ft_isalnum(command[res]) || command[res] == '_')
|
||||
res++;
|
||||
return(res);
|
||||
}
|
||||
|
||||
int ft_get_token_len(char *command)
|
||||
char *get_var_name(char *command)
|
||||
{
|
||||
char *res;
|
||||
int len;
|
||||
|
||||
len = get_var_name_len(command);
|
||||
res = ft_substr(command,0, len);
|
||||
return(res);
|
||||
}
|
||||
|
||||
int ft_get_var_len(char **command, t_env *env)
|
||||
{
|
||||
char *var_name;
|
||||
char *env_var;
|
||||
|
||||
(*command)++;
|
||||
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
||||
return (1);
|
||||
if (**command == '?')
|
||||
return (get_number_len(g_return_code));
|
||||
var_name = get_var_name(*command);
|
||||
env_var = ft_getenv(env, var_name);
|
||||
free(var_name);
|
||||
if (!env_var)
|
||||
return (0);
|
||||
*command += get_var_name_len(*command) - 1;
|
||||
return (ft_strlen(env_var));
|
||||
}
|
||||
|
||||
int ft_get_token_len(char *command, t_env *env)
|
||||
{
|
||||
int in_quote;
|
||||
int in_dquote;
|
||||
@ -35,94 +87,160 @@ int ft_get_token_len(char *command)
|
||||
in_quote = 0;
|
||||
in_dquote = 0;
|
||||
res = 0;
|
||||
while(*command && (!isspace(*command) || in_quote || in_dquote))
|
||||
while (*command && (!ft_isspace(*command) || in_quote || in_dquote))
|
||||
{
|
||||
if(*command == '"' && !in_quote)
|
||||
if (*command == '"' && !in_quote)
|
||||
in_dquote = !in_dquote;
|
||||
if(*command == '\'' && !in_dquote)
|
||||
if (*command == '\'' && !in_dquote)
|
||||
in_quote = !in_quote;
|
||||
if(*command != '\'' && *command != '"')
|
||||
if (*command == '$' && !in_quote)
|
||||
res += ft_get_var_len(&command, env);
|
||||
else if (*command != '\'' && *command != '"')
|
||||
res++;
|
||||
if((*command == '\'' && in_dquote) || (*command == '"' && in_quote))
|
||||
else if ((*command == '\'' && in_dquote) || (*command == '"' && in_quote))
|
||||
res++;
|
||||
command++;
|
||||
}
|
||||
return(res);
|
||||
return (res);
|
||||
}
|
||||
|
||||
void ft_skip_spaces(char **command)
|
||||
int ft_add_var_to_str(char *res, char **command, t_env *env)
|
||||
{
|
||||
while (ft_isspace(**command))
|
||||
(*command)++;
|
||||
char *var_name;
|
||||
char *var;
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
||||
{
|
||||
*res = '$';
|
||||
return(1);
|
||||
}
|
||||
if (**command == '?')
|
||||
{
|
||||
var = ft_itoa(g_return_code);
|
||||
while(var && var[++i])
|
||||
res[i] = var[i];
|
||||
free(var);
|
||||
return(i + 1);
|
||||
}
|
||||
var_name = get_var_name(*command);
|
||||
var = ft_getenv(env, var_name);
|
||||
free(var_name);
|
||||
while(var && var[++i])
|
||||
res[i] = var[i];
|
||||
*command += get_var_name_len(*command) - 1;
|
||||
return (i + !var);
|
||||
}
|
||||
|
||||
char *ft_get_token(char **command)
|
||||
char *ft_get_token(char **command, int *in_quote, int *in_dquote, t_env *env)
|
||||
{
|
||||
int in_quote;
|
||||
int in_dquote;
|
||||
char *res;
|
||||
int i;
|
||||
|
||||
in_quote = 0;
|
||||
in_dquote = 0;
|
||||
i = 0;
|
||||
while (ft_isspace(**command))
|
||||
(*command)++;
|
||||
res = malloc(ft_get_token_len(*command));
|
||||
while(res && **command && (!isspace(**command) || in_quote || in_dquote))
|
||||
res = ft_calloc(ft_get_token_len(*command, env) + 1, 1);
|
||||
while (res && **command && (!ft_isspace(**command) || *in_quote || *in_dquote))
|
||||
{
|
||||
if(**command == '"' && !in_quote)
|
||||
in_dquote = !in_dquote;
|
||||
if(**command == '\'' && !in_dquote)
|
||||
in_quote = !in_quote;
|
||||
if(((**command == '\'' && in_dquote) || (**command == '"' && in_quote))
|
||||
if (**command == '"' && !*in_quote)
|
||||
*in_dquote = !*in_dquote;
|
||||
if (**command == '\'' && !*in_dquote)
|
||||
*in_quote = !*in_quote;
|
||||
if (**command == '$' && !*in_quote)
|
||||
{
|
||||
(*command)++;
|
||||
i += ft_add_var_to_str(res + i, command , env);
|
||||
}
|
||||
else if (((**command == '\'' && *in_dquote) || (**command == '"' && *in_quote))
|
||||
|| (**command != '\'' && **command != '"'))
|
||||
res[i++] = **command;
|
||||
(*command)++;
|
||||
}
|
||||
return(res);
|
||||
return (res);
|
||||
}
|
||||
|
||||
t_cmd *ft_parse_command(char *command)
|
||||
t_cmd *ft_parse_command(char *command, t_env *env)
|
||||
{
|
||||
int in_quote;
|
||||
int in_dquote;
|
||||
t_cmd *res;
|
||||
char *token;
|
||||
|
||||
res = 0;
|
||||
while(*command)
|
||||
in_quote = 0;
|
||||
in_dquote = 0;
|
||||
res = STDIN_FILENO;
|
||||
if (!command)
|
||||
return (STDIN_FILENO);
|
||||
while (*command)
|
||||
{
|
||||
token = ft_get_token(&command);
|
||||
token = ft_get_token(&command, &in_quote, &in_dquote, env);
|
||||
res = ft_cmd_add_back(res, token);
|
||||
}
|
||||
return(res);
|
||||
if (in_quote || in_dquote)
|
||||
{
|
||||
ft_free_cmd(res);
|
||||
ft_putstr_fd("minishell: syntax error\n", 2);
|
||||
return (0);
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
t_env *ft_get_env(char **envp)
|
||||
{
|
||||
t_env *env;
|
||||
char *name;
|
||||
char *value;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
env = 0;
|
||||
while (*envp)
|
||||
{
|
||||
i = 0;
|
||||
j = 0;
|
||||
while ((*envp)[i] && (*envp)[i] != '=')
|
||||
i++;
|
||||
while ((*envp)[i + 1 + j])
|
||||
j++;
|
||||
name = ft_substr(*envp, 0, i);
|
||||
value = ft_substr(*envp, i + 1, j);
|
||||
env = ft_env_add_back(env, name, value);
|
||||
if (!name || !value)
|
||||
ft_free_env(env);
|
||||
if (!name || !value)
|
||||
return (0);
|
||||
envp++;
|
||||
}
|
||||
return (env);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
char *command;
|
||||
t_cmd *parsed_cmd;
|
||||
t_env *env;
|
||||
char *prompt;
|
||||
|
||||
command = (char *)1;
|
||||
while(command)
|
||||
command = (char *)STDOUT_FILENO;
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
env = ft_get_env(envp);
|
||||
while (env && command)
|
||||
{
|
||||
prompt = get_prompt();
|
||||
if(!prompt)
|
||||
exit(0);
|
||||
if (!prompt)
|
||||
exit(STDIN_FILENO);
|
||||
command = readline(prompt);
|
||||
add_history(command);
|
||||
free(prompt);
|
||||
t_cmd *test;
|
||||
test = ft_parse_command(command);
|
||||
printf("%s\n",test->token);
|
||||
printf("%s\n",command);
|
||||
if(command && !ft_strcmp(command,"exit"))
|
||||
{
|
||||
rl_clear_history();
|
||||
free(command);
|
||||
printf("au revoir :,(\n");
|
||||
exit(0);
|
||||
}
|
||||
rl_clear_history();
|
||||
add_history(command);
|
||||
parsed_cmd = ft_parse_command(command, env);
|
||||
free(command);
|
||||
ft_exec_command(parsed_cmd, env);
|
||||
ft_free_cmd(parsed_cmd);
|
||||
}
|
||||
rl_clear_history();
|
||||
ft_free_env(env);
|
||||
return (g_return_code);
|
||||
}
|
||||
|
Reference in New Issue
Block a user