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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,22 +14,22 @@
int ft_echo(t_cmd *args) int ft_echo(t_cmd *args)
{ {
int put_nl; int put_nl;
put_nl = 1; put_nl = 1;
while(args && !strcmp(args->token,"-n")) while (args && !strcmp(args->token, "-n"))
{ {
put_nl = 0; put_nl = 0;
args = args->next; args = args->next;
} }
while(args) while (args)
{ {
ft_putstr_fd(args->token, STDOUT_FILENO); ft_putstr_fd(args->token, STDOUT_FILENO);
if(args->next) if (args->next)
ft_putchar_fd(' ',STDOUT_FILENO); ft_putchar_fd(' ', STDOUT_FILENO);
args = args->next; args = args->next;
} }
if(put_nl) if (put_nl)
ft_putchar_fd('\n',STDOUT_FILENO); ft_putchar_fd('\n', STDOUT_FILENO);
return(0); return (0);
} }

View File

@ -6,32 +6,32 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int exec_builtin(t_cmd *parsed_cmd, t_env *env) int exec_builtin(t_cmd *parsed_cmd, t_env *env)
{ {
if(!strcmp(parsed_cmd->token, "echo")) if (!strcmp(parsed_cmd->token, "echo"))
g_return_code = ft_echo(parsed_cmd->next); g_return_code = ft_echo(parsed_cmd->next);
else if (!strcmp(parsed_cmd->token,"ret")) else if (!strcmp(parsed_cmd->token, "ret"))
g_return_code = ft_atoi(parsed_cmd->next->token); g_return_code = ft_atoi(parsed_cmd->next->token);
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);
} }
void ft_exec_command(t_cmd *parsed_cmd, t_env *env) void ft_exec_command(t_cmd *parsed_cmd, t_env *env)
{ {
if(!parsed_cmd) if (!parsed_cmd)
return; return ;
if(exec_builtin(parsed_cmd, env)) if (exec_builtin(parsed_cmd, env))
return ; return ;
ft_printf("not a builtin\n"); ft_printf("not a builtin\n");
} }

View File

@ -6,26 +6,30 @@
/* 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;
ft_printf("exit\n"); ft_printf("exit\n");
if(args && args->next) if (args && args->next)
ft_printf("minishell: exit: too many arguments\n"); ft_printf("minishell: exit: too many arguments\n");
else else
{ {
if (args)
exit_code = (unsigned char)ft_atoi(args->token);
else
exit_code = g_return_code;
ft_free_cmd(start); ft_free_cmd(start);
if(args) ft_free_env(env);
exit((unsigned char)ft_atoi(args->token));
exit(g_return_code); exit(g_return_code);
} }
} }

View File

@ -6,16 +6,16 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_cmd *ft_cmd_add_back(t_cmd *cmd, char *token) t_cmd *ft_cmd_add_back(t_cmd *cmd, char *token)
{ {
t_cmd *res; t_cmd *res;
t_cmd *current; t_cmd *current;
res = ft_calloc(1, sizeof(t_cmd)); res = ft_calloc(1, sizeof(t_cmd));
if (!res) if (!res)
@ -26,15 +26,15 @@ t_cmd *ft_cmd_add_back(t_cmd *cmd, char *token)
current = cmd; current = cmd;
while (current->next) while (current->next)
current = current->next; current = current->next;
current->next = res; current->next = res;
return (cmd); return (cmd);
} }
void ft_free_cmd(t_cmd *cmd) void ft_free_cmd(t_cmd *cmd)
{ {
if(cmd && cmd->next) if (cmd && cmd->next)
ft_free_cmd(cmd->next); ft_free_cmd(cmd->next);
if(cmd) if (cmd)
free(cmd->token); free(cmd->token);
free(cmd); free(cmd);
} }

View File

@ -6,16 +6,16 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
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)
{ {
t_env *res; t_env *res;
t_env *current; t_env *current;
res = ft_calloc(1, sizeof(t_env)); res = ft_calloc(1, sizeof(t_env));
if (!res) if (!res)
@ -27,15 +27,15 @@ t_env *ft_env_add_back(t_env *env, char *name, char *value)
current = env; current = env;
while (current->next) while (current->next)
current = current->next; current = current->next;
current->next = res; current->next = res;
return (env); return (env);
} }
void ft_free_env(t_env *env) void ft_free_env(t_env *env)
{ {
if(env && env->next) if (env && env->next)
ft_free_env(env->next); ft_free_env(env->next);
if(env) if (env)
{ {
free(env->name); free(env->name);
free(env->value); free(env->value);
@ -45,10 +45,33 @@ void ft_free_env(t_env *env)
int ft_print_env(t_env *env) 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,12 +6,12 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
int ft_isspace(int c) int ft_isspace(int c)
{ {
return (c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v' return (c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v'
|| c == ' '); || c == ' ');
} }

199
main.c
View File

@ -6,194 +6,33 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int g_return_code = 0; int g_return_code = 0;
char *get_prompt(void) char *get_prompt(void)
{ {
char *res; char *res;
res = ft_strjoin_free("\001",ft_get_color(0,255,0),2); res = ft_strjoin_free("\001", ft_get_color(0, 255, 0), 2);
res = ft_strjoin_free(res,"\002", 1); res = ft_strjoin_free(res, "\002", 1);
res = ft_strjoin_free(res, getenv("USER"),1); res = ft_strjoin_free(res, getenv("USER"), 1);
res = ft_strjoin_free(res, "@",1); res = ft_strjoin_free(res, "@", 1);
res = ft_strjoin_free(res, "minishell \001\033[0m\002$>",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);
}
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); return (res);
} }
t_env *ft_get_env(char **envp) t_env *ft_get_env(char **envp)
{ {
t_env *env; t_env *env;
char *name; char *name;
char *value; char *value;
int i; int i;
int j; int j;
env = 0; env = 0;
while (*envp) while (*envp)
@ -216,12 +55,12 @@ t_env *ft_get_env(char **envp)
return (env); return (env);
} }
int main(int argc, char **argv, char **envp) int main(int argc, char **argv, char **envp)
{ {
char *command; char *command;
t_cmd *parsed_cmd; t_cmd *parsed_cmd;
t_env *env; t_env *env;
char *prompt; char *prompt;
command = (char *)STDOUT_FILENO; command = (char *)STDOUT_FILENO;
(void)argc; (void)argc;

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,10 +17,9 @@
# 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;
struct s_cmd *next; struct s_cmd *next;
} t_cmd; } t_cmd;
@ -31,13 +30,13 @@ typedef struct s_env
struct s_env *next; struct s_env *next;
} t_env; } t_env;
extern int g_return_code; extern int g_return_code;
t_cmd *ft_cmd_add_back(t_cmd *res, char *token); 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,20 +6,22 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i = 0; int i;
while(i < argc)
i = 0;
while (i < argc)
{ {
printf("\"%s\"\n",argv[i]); printf("\"%s\"\n", argv[i]);
i++; i++;
} }
return(0); return (0);
} }

View File

@ -6,14 +6,15 @@
/* 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)
return(atoi(argv[1])); return (atoi(argv[1]));
return(0); return (0);
} }