From 9f06dea3392236765eaf873b1815eb193e7ecd7f Mon Sep 17 00:00:00 2001 From: narnaud Date: Fri, 27 May 2022 09:08:01 +0200 Subject: [PATCH] new: remove file_error fct, fix: unknow or invalid variables wrongly display --- built-in.c | 18 +++++++++--------- caller.c | 4 ++-- env.c | 4 ++-- errors.c | 29 +++++++++-------------------- lexer.c | 7 ++----- minishell.c | 2 +- minishell.h | 7 +++---- 7 files changed, 28 insertions(+), 43 deletions(-) diff --git a/built-in.c b/built-in.c index 30267c5..ed52846 100755 --- a/built-in.c +++ b/built-in.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */ -/* Updated: 2022/05/18 16:26:51 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/27 08:46:51 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,15 +79,15 @@ int ft_cd(t_datas *datas, t_command *cmd) char *home; if (cmd->argc > 2) - return (cmd_error("cd", "too many arguments")); + return (cmd_error("cd", "too many arguments\n", NULL)); if (cmd->argv[1]) { if (access(cmd->argv[1], X_OK)) - return (file_error(cmd->argv[0], cmd->argv[1], - "Permission denied\n")); + return (cmd_error(cmd->argv[0], cmd->argv[1], + ": Permission denied\n")); else if (access(cmd->argv[1], F_OK)) - return (file_error(cmd->argv[0], cmd->argv[1], - "Not a directory\n")); + return (cmd_error(cmd->argv[0], cmd->argv[1], + ": Not a directory\n")); chdir(cmd->argv[1]); return (0); } @@ -97,7 +97,7 @@ int ft_cd(t_datas *datas, t_command *cmd) if (*home) chdir(home); else - return (cmd_error("cd", "HOME not set")); + return (cmd_error("cd", "HOME not set\n", NULL)); } return (0); } @@ -110,11 +110,11 @@ int ft_exit(t_datas *datas, t_command *cmd) { if (!ft_isnumber(cmd->argv[1])) { - cmd_error("exit", "numeric argument required"); + cmd_error("exit", "numeric argument required\n", NULL); halt(datas, 2, 0); } halt(datas, ft_atoi(cmd->argv[1]) % 255, 0); } - cmd_error("exit", "too many arguments"); + cmd_error("exit", "too many arguments\n", NULL); return (EXIT_FAILURE); } diff --git a/caller.c b/caller.c index eb0006c..3af2c55 100755 --- a/caller.c +++ b/caller.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ -/* Updated: 2022/05/17 17:26:25 by narnaud ### ########.fr */ +/* Updated: 2022/05/27 08:48:24 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,7 +55,7 @@ static void call_binary(t_datas *datas, t_command *cmd) path_dirs++; } } - cmd_error(cmd->argv[0], "command not found"); + cmd_error(cmd->argv[0], "command not found\n", NULL); } static pid_t call(t_datas *datas, t_command *cmd) diff --git a/env.c b/env.c index c01fea3..835b660 100755 --- a/env.c +++ b/env.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ -/* Updated: 2022/05/18 15:54:39 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/27 08:51:55 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,7 +72,7 @@ int save_var(t_datas *datas, char *argv) var = ft_split(argv, '='); name_len = ft_strlen(var[0]); - if (!is_valid_identifier(var[0])) + if (!is_valid_identifier(var[0], 0)) return (1); i = 0; while (datas->envp[i]) diff --git a/errors.c b/errors.c index 2c9132c..e22ff59 100644 --- a/errors.c +++ b/errors.c @@ -6,35 +6,24 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */ -/* Updated: 2022/05/18 14:45:53 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/27 08:47:01 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int file_error(char *cmd, char *file, char *msg) +int cmd_error(char *cmd, char *msg, char *msg2) { ft_putstr_fd("Minishell: ", 2); ft_putstr_fd(cmd, 2); ft_putstr_fd(": ", 2); - ft_putstr_fd(file, 2); - ft_putstr_fd(": ", 2); ft_putstr_fd(msg, 2); - ft_putchar_fd('\n', 2); - return (1); -} - -int cmd_error(char *cmd, char *msg) -{ - ft_putstr_fd("Minishell: ", 2); - ft_putstr_fd(cmd, 2); - ft_putstr_fd(": ", 2); - ft_putstr_fd(msg, 2); - ft_putchar_fd('\n', 2); + if (msg2) + ft_putstr_fd(msg2, 2); return (1); } -int is_valid_identifier(char *ident) +int is_valid_identifier(char *ident, int silent) { int i; @@ -46,8 +35,8 @@ int is_valid_identifier(char *ident) if (!ft_isalpha(ident[i]) && !ft_isdigit(ident[i]) && ident[i] != '_') { - ft_putstr_fd("export: not an identifier: ", 2); - ft_putstr_fd(ident, 2); + if (!silent) + cmd_error("export", "not an identifier: ", ident); ft_putchar_fd('\n', 2); return (0); } @@ -55,8 +44,8 @@ int is_valid_identifier(char *ident) } return (1); } - ft_putstr_fd("export: not an identifier: ", 2); - ft_putstr_fd(ident, 2); + if (!silent) + cmd_error("export", "not an identifier: ", ident); ft_putchar_fd('\n', 2); return (0); } diff --git a/lexer.c b/lexer.c index 2f109c2..25f3e14 100755 --- a/lexer.c +++ b/lexer.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ -/* Updated: 2022/05/17 15:58:09 by mea ### ########.fr */ +/* Updated: 2022/05/27 08:59:20 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,11 +24,8 @@ void tmp_dealer(t_datas *datas, t_lexer *lex, char *line, char *tmp) if (check_state(lex, &line)) continue ; if (lex->state != S_QUOTE_ST && lex->deep == 0 && *line == '$' - && (line[1] != '\0' && !ft_isspace(line[1]))) - { + && (ft_isalpha(line[1]) || line[1] == '_' || line[1] == '?')) tmp_i = replace_var(datas, &line, tmp, tmp_i); - lex->empty = 1; - } else if (check_register(lex, &line, tmp)) tmp_i = (ft_bzero(tmp, STR_MAX_SIZE), 0); else if (!(lex->state == ROOT_ST && lex->deep == 0 diff --git a/minishell.c b/minishell.c index 09383ba..674c9cd 100755 --- a/minishell.c +++ b/minishell.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ -/* Updated: 2022/05/24 08:23:21 by narnaud ### ########.fr */ +/* Updated: 2022/05/27 08:50:47 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/minishell.h b/minishell.h index 7f5096f..91df26e 100755 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ -/* Updated: 2022/05/17 17:25:53 by narnaud ### ########.fr */ +/* Updated: 2022/05/27 08:51:16 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,9 +60,8 @@ void sigs_handler(int sig_num); void halt(t_datas *datas, int ret_code, int silent); // ---------------------------------- Errors.c -int file_error(char *cmd, char *file, char *msg); -int cmd_error(char *cmd, char *msg); -int is_valid_identifier(char *ident); +int cmd_error(char *cmd, char *msg, char *msg2); +int is_valid_identifier(char *ident, int silent); // ----------------------------------Builtins.c int is_builtin(char *cmd);