This commit is contained in:
Tom Moron
2024-02-09 16:43:35 +01:00
parent 22403b127e
commit ce72dde43e
12 changed files with 242 additions and 244 deletions

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */ /* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */
/* Updated: 2024/02/07 23:15:04 by tomoron ### ########.fr */ /* Updated: 2024/02/09 14:56:57 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
/* Updated: 2024/02/08 16:57:26 by tomoron ### ########.fr */ /* Updated: 2024/02/09 14:59:22 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ int exec_builtin(t_cmd *parsed_cmd, t_env *env)
else if (!strcmp(parsed_cmd->token, "env")) else if (!strcmp(parsed_cmd->token, "env"))
g_return_code = ft_print_env(env); g_return_code = ft_print_env(env);
else if (!strcmp(parsed_cmd->token, "exit")) else if (!strcmp(parsed_cmd->token, "exit"))
ft_exit(parsed_cmd); ft_exit(parsed_cmd, env);
else else
return (STDIN_FILENO); return (STDIN_FILENO);
return (STDOUT_FILENO); return (STDOUT_FILENO);

View File

@ -6,15 +6,16 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */ /* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
/* Updated: 2024/02/07 16:48:07 by tomoron ### ########.fr */ /* Updated: 2024/02/09 15:08:01 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void ft_exit(t_cmd *args) void ft_exit(t_cmd *args, t_env *env)
{ {
t_cmd *start; t_cmd *start;
int exit_code;
start = args; start = args;
args = args->next; args = args->next;
@ -23,9 +24,12 @@ void ft_exit(t_cmd *args)
ft_printf("minishell: exit: too many arguments\n"); ft_printf("minishell: exit: too many arguments\n");
else else
{ {
ft_free_cmd(start);
if (args) if (args)
exit((unsigned char)ft_atoi(args->token)); exit_code = (unsigned char)ft_atoi(args->token);
else
exit_code = g_return_code;
ft_free_cmd(start);
ft_free_env(env);
exit(g_return_code); exit(g_return_code);
} }
} }

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ /* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
/* Updated: 2024/02/08 12:42:12 by tomoron ### ########.fr */ /* Updated: 2024/02/09 15:09:02 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ /* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
/* Updated: 2024/02/08 16:58:48 by tomoron ### ########.fr */ /* Updated: 2024/02/09 15:24:59 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,8 +47,31 @@ int ft_print_env(t_env *env)
{ {
while (env) while (env)
{ {
printf("%s=%s\n",env->name, env->value); ft_printf("%s=%s\n", env->name, env->value);
env = env->next; env = env->next;
} }
return (0); return (0);
} }
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);
}

66
ft_parsing.c Normal file
View File

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */
/* Updated: 2024/02/09 16:40:07 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *ft_get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
{
char *res;
int i;
i = 0;
while (ft_isspace(**cmd))
(*cmd)++;
res = ft_calloc(ft_get_token_len(*cmd, env) + 1, 1);
while (res && **cmd && (!ft_isspace(**cmd) || *in_quote || *in_dquote))
{
if (**cmd == '"' && !*in_quote)
*in_dquote = !*in_dquote;
if (**cmd == '\'' && !*in_dquote)
*in_quote = !*in_quote;
if (**cmd == '$' && !*in_quote)
{
(*cmd)++;
i += ft_add_var_to_str(res + i, cmd, env);
}
else if (((**cmd == '\'' && *in_dquote) || (**cmd == '"' && *in_quote))
|| (**cmd != '\'' && **command != '"'))
res[i++] = **cmd;
(*cmd)++;
}
return (res);
}
t_cmd *ft_parse_command(char *command, t_env *env)
{
int in_quote;
int in_dquote;
t_cmd *res;
char *token;
in_quote = 0;
in_dquote = 0;
res = STDIN_FILENO;
if (!command)
return (STDIN_FILENO);
while (*command)
{
token = ft_get_token(&command, &in_quote, &in_dquote, env);
res = ft_cmd_add_back(res, token);
}
if (in_quote || in_dquote)
{
ft_free_cmd(res);
ft_putstr_fd("minishell: syntax error\n", 2);
return (0);
}
return (res);
}

64
ft_parsing_var.c Normal file
View File

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parsing_var.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */
/* Updated: 2024/02/09 15:24:43 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
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;
int res;
in_quote = 0;
in_dquote = 0;
res = 0;
while (*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_quote)
res += ft_get_var_len(&command, env);
else if (*command != '\'' && *command != '"')
res++;
else if ((*command == '\'' && in_dquote)
|| (*command == '"' && in_quote))

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/05 00:58:08 by tomoron #+# #+# */ /* Created: 2024/02/05 00:58:08 by tomoron #+# #+# */
/* Updated: 2024/02/06 22:17:28 by tomoron ### ########.fr */ /* Updated: 2024/02/09 16:40:35 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

163
main.c
View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */ /* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
/* Updated: 2024/02/09 14:37:48 by tomoron ### ########.fr */ /* Updated: 2024/02/09 16:28:28 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,167 +26,6 @@ char *get_prompt(void)
return (res); 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);
}
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;
int res;
in_quote = 0;
in_dquote = 0;
res = 0;
while (*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_quote)
res += ft_get_var_len(&command, env);
else if (*command != '\'' && *command != '"')
res++;
else if ((*command == '\'' && in_dquote) || (*command == '"' && in_quote))
res++;
command++;
}
return (res);
}
int ft_add_var_to_str(char *res, char **command, t_env *env)
{
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, int *in_quote, int *in_dquote, t_env *env)
{
char *res;
int i;
i = 0;
while (ft_isspace(**command))
(*command)++;
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_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);
}
t_cmd *ft_parse_command(char *command, t_env *env)
{
int in_quote;
int in_dquote;
t_cmd *res;
char *token;
in_quote = 0;
in_dquote = 0;
res = STDIN_FILENO;
if (!command)
return (STDIN_FILENO);
while (*command)
{
token = ft_get_token(&command, &in_quote, &in_dquote, env);
res = ft_cmd_add_back(res, token);
}
if (in_quote || in_dquote)
{
ft_free_cmd(res);
ft_putstr_fd("minishell: syntax error\n", 2);
return (0);
}
return (res);
}
t_env *ft_get_env(char **envp) t_env *ft_get_env(char **envp)
{ {
t_env *env; t_env *env;

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/02/08 16:58:15 by tomoron ### ########.fr */ /* Updated: 2024/02/09 14:58:21 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,6 @@
# include <stdio.h>//debug # include <stdio.h>//debug
# include "libft/libft.h" # include "libft/libft.h"
typedef struct s_cmd typedef struct s_cmd
{ {
char *token; char *token;
@ -37,7 +36,7 @@ t_cmd *ft_cmd_add_back(t_cmd *res, char *token);
void ft_free_cmd(t_cmd *cmd); void ft_free_cmd(t_cmd *cmd);
void ft_exec_command(t_cmd *cmd, t_env *env); void ft_exec_command(t_cmd *cmd, t_env *env);
int ft_echo(t_cmd *args); int ft_echo(t_cmd *args);
void ft_exit(t_cmd *args); void ft_exit(t_cmd *args, t_env *env);
t_env *ft_env_add_back(t_env *env, char *name, char *value); t_env *ft_env_add_back(t_env *env, char *name, char *value);
void ft_free_env(t_env *env); void ft_free_env(t_env *env);
int ft_print_env(t_env *env); int ft_print_env(t_env *env);

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:30:08 by tomoron #+# #+# */ /* Created: 2024/02/04 17:30:08 by tomoron #+# #+# */
/* Updated: 2024/02/07 14:29:12 by tomoron ### ########.fr */ /* Updated: 2024/02/09 16:41:52 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,9 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i = 0; int i;
i = 0;
while (i < argc) while (i < argc)
{ {
printf("\"%s\"\n", argv[i]); printf("\"%s\"\n", argv[i]);

View File

@ -6,11 +6,12 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 13:30:04 by tomoron #+# #+# */ /* Created: 2024/02/07 13:30:04 by tomoron #+# #+# */
/* Updated: 2024/02/07 13:34:55 by tomoron ### ########.fr */ /* Updated: 2024/02/09 16:42:59 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc == 2) if (argc == 2)