exit sur la sortie d'erreure et parsing des variables $ mieux
This commit is contained in:
0
.gitignore
vendored
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
3
Makefile
3
Makefile
@ -6,7 +6,7 @@
|
||||
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
||||
# Updated: 2024/02/09 18:13:02 by tomoron ### ########.fr #
|
||||
# Updated: 2024/02/09 18:32:31 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -18,6 +18,7 @@ SRCS = main.c\
|
||||
ft_exec.c\
|
||||
ft_exit.c\
|
||||
ft_echo.c\
|
||||
ft_pwd.c\
|
||||
ft_parsing.c\
|
||||
ft_parsing_var.c
|
||||
|
||||
|
16
ft_exec.c
Normal file → Executable file
16
ft_exec.c
Normal file → Executable file
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/09 14:59:22 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/02/12 14:46:37 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,14 +14,16 @@
|
||||
|
||||
int exec_builtin(t_cmd *parsed_cmd, t_env *env)
|
||||
{
|
||||
if (!strcmp(parsed_cmd->token, "echo"))
|
||||
if (!ft_strcmp(parsed_cmd->token, "echo"))
|
||||
g_return_code = ft_echo(parsed_cmd->next);
|
||||
else if (!strcmp(parsed_cmd->token, "ret"))
|
||||
else if (!ft_strcmp(parsed_cmd->token, "ret"))
|
||||
g_return_code = ft_atoi(parsed_cmd->next->token);
|
||||
else if (!strcmp(parsed_cmd->token, "env"))
|
||||
else if (!ft_strcmp(parsed_cmd->token, "env"))
|
||||
g_return_code = ft_print_env(env);
|
||||
else if (!strcmp(parsed_cmd->token, "exit"))
|
||||
else if (!ft_strcmp(parsed_cmd->token, "exit"))
|
||||
ft_exit(parsed_cmd, env);
|
||||
else if (!ft_strcmp(parsed_cmd->token, "pwd"))
|
||||
g_return_code = ft_pwd();
|
||||
else
|
||||
return (STDIN_FILENO);
|
||||
return (STDOUT_FILENO);
|
||||
@ -29,9 +31,7 @@ int exec_builtin(t_cmd *parsed_cmd, t_env *env)
|
||||
|
||||
void ft_exec_command(t_cmd *parsed_cmd, t_env *env)
|
||||
{
|
||||
if (!parsed_cmd)
|
||||
return ;
|
||||
if (exec_builtin(parsed_cmd, env))
|
||||
if (!parsed_cmd || exec_builtin(parsed_cmd, env))
|
||||
return ;
|
||||
ft_printf("not a builtin\n");
|
||||
}
|
||||
|
19
ft_exit.c
Normal file → Executable file
19
ft_exit.c
Normal file → Executable file
@ -6,12 +6,19 @@
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/09 15:08:01 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/02/12 20:11:51 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void ft_print_numeric_arg_err(char *arg)
|
||||
{
|
||||
ft_putstr_fd("minishell: exit: ", 2);
|
||||
ft_putstr_fd(arg, 2);
|
||||
ft_putstr_fd(": numeric argument required\n", 2);
|
||||
}
|
||||
|
||||
void ft_exit(t_cmd *args, t_env *env)
|
||||
{
|
||||
t_cmd *start;
|
||||
@ -20,16 +27,20 @@ void ft_exit(t_cmd *args, t_env *env)
|
||||
start = args;
|
||||
args = args->next;
|
||||
ft_printf("exit\n");
|
||||
if (args && args->next)
|
||||
ft_printf("minishell: exit: too many arguments\n");
|
||||
if (args && args->next && ft_strisnbr(args->token))
|
||||
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
|
||||
else
|
||||
{
|
||||
if (args && !ft_strisnbr(args->token))
|
||||
ft_print_numeric_arg_err(args->token);
|
||||
if (args)
|
||||
exit_code = (unsigned char)ft_atoi(args->token);
|
||||
else if (args && !ft_strisnbr(args->token))
|
||||
exit_code = 2;
|
||||
else
|
||||
exit_code = g_return_code;
|
||||
ft_free_cmd(start);
|
||||
ft_free_env(env);
|
||||
exit(g_return_code);
|
||||
exit(exit_code);
|
||||
}
|
||||
}
|
||||
|
5
ft_lst_cmd.c
Normal file → Executable file
5
ft_lst_cmd.c
Normal file → Executable file
@ -6,13 +6,13 @@
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/09 15:09:02 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/02/11 22:54:28 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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_token_type type)
|
||||
{
|
||||
t_cmd *res;
|
||||
t_cmd *current;
|
||||
@ -21,6 +21,7 @@ t_cmd *ft_cmd_add_back(t_cmd *cmd, char *token)
|
||||
if (!res)
|
||||
return (cmd);
|
||||
res->token = token;
|
||||
res->type = type;
|
||||
if (!cmd)
|
||||
return (res);
|
||||
current = cmd;
|
||||
|
0
ft_lst_env.c
Normal file → Executable file
0
ft_lst_env.c
Normal file → Executable file
29
ft_parsing.c
Normal file → Executable file
29
ft_parsing.c
Normal file → Executable file
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/09 18:15:45 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/02/12 14:52:13 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "minishell.h"
|
||||
@ -28,7 +28,7 @@ char *ft_get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
|
||||
*in_quote = !*in_quote;
|
||||
if (**cmd == '$' && !*in_quote)
|
||||
{
|
||||
(*cmd)++;
|
||||
(*cmd) += EXIT_FAILURE;
|
||||
i += ft_add_var_to_str(res + i, cmd, env);
|
||||
}
|
||||
else if (((**cmd == '\'' && *in_dquote) || (**cmd == '"' && *in_quote))
|
||||
@ -39,24 +39,33 @@ char *ft_get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
|
||||
return (res);
|
||||
}
|
||||
|
||||
t_token_type ft_get_token_type(char *command)
|
||||
{
|
||||
(void)command;
|
||||
return (ARG);
|
||||
}
|
||||
|
||||
t_cmd *ft_parse_command(char *command, t_env *env)
|
||||
{
|
||||
int in_quote;
|
||||
int in_dquote;
|
||||
t_cmd *res;
|
||||
char *token;
|
||||
t_token_type type;
|
||||
|
||||
in_quote = 0;
|
||||
in_dquote = 0;
|
||||
res = STDIN_FILENO;
|
||||
if (!command)
|
||||
return (STDIN_FILENO);
|
||||
while (*command)
|
||||
in_quote = EXIT_SUCCESS;
|
||||
in_dquote = STDIN_FILENO;
|
||||
res = 0;
|
||||
while (command && *command)
|
||||
{
|
||||
type = ft_get_token_type(command);
|
||||
if (type == ARG)
|
||||
token = ft_get_token(&command, &in_quote, &in_dquote, env);
|
||||
res = ft_cmd_add_back(res, token);
|
||||
else
|
||||
token = 0;
|
||||
res = ft_cmd_add_back(res, token, type);
|
||||
}
|
||||
if (in_quote || in_dquote)
|
||||
if (command && (in_quote || in_dquote))
|
||||
{
|
||||
ft_free_cmd(res);
|
||||
ft_putstr_fd("minishell: syntax error\n", 2);
|
||||
|
44
ft_parsing_var.c
Normal file → Executable file
44
ft_parsing_var.c
Normal file → Executable file
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/09 18:22:16 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/02/12 20:11:18 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -29,7 +29,7 @@ int ft_get_var_len(char **command, t_env *env)
|
||||
|
||||
(*command)++;
|
||||
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
||||
return (1);
|
||||
return (2);
|
||||
if (**command == '?')
|
||||
return (get_number_len(g_return_code));
|
||||
var_name = get_var_name(*command);
|
||||
@ -68,31 +68,45 @@ int ft_get_token_len(char *command, t_env *env)
|
||||
return (res);
|
||||
}
|
||||
|
||||
int ft_add_return_code_to_str(char *res)
|
||||
{
|
||||
char *var;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
var = ft_itoa(g_return_code);
|
||||
while (var && var[i])
|
||||
{
|
||||
res[i] = var[i];
|
||||
i++;
|
||||
}
|
||||
free(var);
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_add_var_to_str(char *res, char **command, t_env *env)
|
||||
{
|
||||
char *var_name;
|
||||
char *var;
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
i = 0;
|
||||
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
||||
{
|
||||
*res = '$';
|
||||
return (1);
|
||||
res[0] = '$';
|
||||
res[1] = **command;
|
||||
return (2);
|
||||
}
|
||||
if (**command == '?')
|
||||
{
|
||||
var = ft_itoa(g_return_code);
|
||||
while (var && var[++i])
|
||||
res[i] = var[i];
|
||||
free(var);
|
||||
return (i + 1);
|
||||
}
|
||||
return (ft_add_return_code_to_str(res));
|
||||
var_name = get_var_name(*command);
|
||||
var = ft_getenv(env, var_name);
|
||||
free(var_name);
|
||||
while (var && var[++i])
|
||||
while (var && var[i])
|
||||
{
|
||||
res[i] = var[i];
|
||||
*command += get_var_name_len(*command) - 1;
|
||||
return (i + !var);
|
||||
i++;
|
||||
}
|
||||
*command += get_var_name_len(*command) - 1;
|
||||
return (i);
|
||||
}
|
||||
|
25
ft_pwd.c
Executable file
25
ft_pwd.c
Executable file
@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_pwd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/09 18:31:21 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/12 14:53:05 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "minishell.h"
|
||||
|
||||
int ft_pwd(void)
|
||||
{
|
||||
char *buffer;
|
||||
|
||||
buffer = malloc(1024 * 1024);
|
||||
if (!buffer)
|
||||
return (1);
|
||||
getcwd(buffer, 1024 * 1024);
|
||||
ft_printf("%s\n", buffer);
|
||||
free(buffer);
|
||||
return (0);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
||||
# Updated: 2024/02/05 01:10:13 by tomoron ### ########.fr #
|
||||
# Updated: 2024/02/11 17:41:50 by tomoron ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -44,6 +44,7 @@ SRCS = ft_atoi.c\
|
||||
ft_strcmp.c\
|
||||
ft_strnstr.c\
|
||||
ft_strrchr.c\
|
||||
ft_strisnbr.c\
|
||||
ft_strtrim.c\
|
||||
ft_substr.c\
|
||||
ft_tolower.c\
|
||||
|
0
libft/ft_isspace.c
Normal file → Executable file
0
libft/ft_isspace.c
Normal file → Executable file
24
libft/ft_strisnbr.c
Executable file
24
libft/ft_strisnbr.c
Executable file
@ -0,0 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strisnbr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/11 17:14:58 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/12 03:21:56 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_strisnbr(char *str)
|
||||
{
|
||||
if(!str)
|
||||
return(0);
|
||||
while(*str)
|
||||
{
|
||||
if(*str < '0' || *str > '9')
|
||||
return(0);
|
||||
str++;
|
||||
}
|
||||
return(1);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/10/30 16:55:48 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/08 18:44:22 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/02/11 17:41:32 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -67,6 +67,7 @@ t_list *ft_lstnew(void *content);
|
||||
int ft_atoi(const char *str);
|
||||
t_list *ft_lstlast(t_list *lst);
|
||||
int ft_lstsize(t_list *lst);
|
||||
int ft_strisnbr(char *str);
|
||||
void ft_reset_color(void);
|
||||
int ft_isspace(char c);
|
||||
int ft_isalnum(int c);
|
||||
|
11
main.c
Normal file → Executable file
11
main.c
Normal file → Executable file
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/09 16:28:28 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/02/12 14:53:26 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,12 +17,17 @@ int g_return_code = 0;
|
||||
char *get_prompt(void)
|
||||
{
|
||||
char *res;
|
||||
char cwd_buffer[100];
|
||||
|
||||
res = ft_strjoin_free("\001", ft_get_color(0, 255, 0), 2);
|
||||
res = ft_strjoin_free("\001", ft_get_color(10, 255, 80), 2);
|
||||
res = ft_strjoin_free(res, "\002", 1);
|
||||
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);
|
||||
res = ft_strjoin_free(res, "minishell\001\033[0m\002:\001", 1);
|
||||
res = ft_strjoin_free(res, ft_get_color(80, 80, 255), 3);
|
||||
res = ft_strjoin_free(res, "\002", 1);
|
||||
res = ft_strjoin_free(res, getcwd(cwd_buffer, 99), 1);
|
||||
res = ft_strjoin_free(res, "\001\033[0m\002$ ", 1);
|
||||
return (res);
|
||||
}
|
||||
|
||||
|
15
minishell.h
Normal file → Executable file
15
minishell.h
Normal file → Executable file
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/09 18:17:30 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/02/11 22:54:49 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -14,11 +14,21 @@
|
||||
# define MINISHELL_H
|
||||
# include <readline/readline.h>
|
||||
# include <readline/history.h>
|
||||
# include <limits.h>
|
||||
# include <stdio.h>//debug
|
||||
# include "libft/libft.h"
|
||||
|
||||
typedef enum e_token_type
|
||||
{
|
||||
ARG,
|
||||
PIPE,
|
||||
OR,
|
||||
AND
|
||||
} t_token_type;
|
||||
|
||||
typedef struct s_cmd
|
||||
{
|
||||
t_token_type type;
|
||||
char *token;
|
||||
struct s_cmd *next;
|
||||
} t_cmd;
|
||||
@ -32,7 +42,7 @@ typedef struct s_env
|
||||
|
||||
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, t_token_type type);
|
||||
void ft_free_cmd(t_cmd *cmd);
|
||||
void ft_exec_command(t_cmd *cmd, t_env *env);
|
||||
int ft_echo(t_cmd *args);
|
||||
@ -45,4 +55,5 @@ int ft_get_token_len(char *cmd, t_env *env);
|
||||
int ft_add_var_to_str(char *res, char **command, t_env *env);
|
||||
int get_var_name_len(char *command);
|
||||
char *ft_getenv(t_env *env, char *var_name);
|
||||
int ft_pwd(void);
|
||||
#endif
|
||||
|
0
other/test.c
Normal file → Executable file
0
other/test.c
Normal file → Executable file
0
other/test2.c
Normal file → Executable file
0
other/test2.c
Normal file → Executable file
0
valgrind.supp
Normal file → Executable file
0
valgrind.supp
Normal file → Executable file
Reference in New Issue
Block a user