Browse Source

new: remove file_error fct, fix: unknow or invalid variables wrongly display

master
narnaud 3 years ago
parent
commit
9f06dea339
  1. 18
      built-in.c
  2. 4
      caller.c
  3. 4
      env.c
  4. 29
      errors.c
  5. 7
      lexer.c
  6. 2
      minishell.c
  7. 7
      minishell.h

18
built-in.c

@ -6,7 +6,7 @@
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */ /* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */ /* 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; char *home;
if (cmd->argc > 2) 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 (cmd->argv[1])
{ {
if (access(cmd->argv[1], X_OK)) if (access(cmd->argv[1], X_OK))
return (file_error(cmd->argv[0], cmd->argv[1], return (cmd_error(cmd->argv[0], cmd->argv[1],
"Permission denied\n")); ": Permission denied\n"));
else if (access(cmd->argv[1], F_OK)) else if (access(cmd->argv[1], F_OK))
return (file_error(cmd->argv[0], cmd->argv[1], return (cmd_error(cmd->argv[0], cmd->argv[1],
"Not a directory\n")); ": Not a directory\n"));
chdir(cmd->argv[1]); chdir(cmd->argv[1]);
return (0); return (0);
} }
@ -97,7 +97,7 @@ int ft_cd(t_datas *datas, t_command *cmd)
if (*home) if (*home)
chdir(home); chdir(home);
else else
return (cmd_error("cd", "HOME not set")); return (cmd_error("cd", "HOME not set\n", NULL));
} }
return (0); return (0);
} }
@ -110,11 +110,11 @@ int ft_exit(t_datas *datas, t_command *cmd)
{ {
if (!ft_isnumber(cmd->argv[1])) 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, 2, 0);
} }
halt(datas, ft_atoi(cmd->argv[1]) % 255, 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); return (EXIT_FAILURE);
} }

4
caller.c

@ -6,7 +6,7 @@
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */ /* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ /* 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++; 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) static pid_t call(t_datas *datas, t_command *cmd)

4
env.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ /* 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, '='); var = ft_split(argv, '=');
name_len = ft_strlen(var[0]); name_len = ft_strlen(var[0]);
if (!is_valid_identifier(var[0])) if (!is_valid_identifier(var[0], 0))
return (1); return (1);
i = 0; i = 0;
while (datas->envp[i]) while (datas->envp[i])

29
errors.c

@ -6,35 +6,24 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */ /* 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" #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("Minishell: ", 2);
ft_putstr_fd(cmd, 2); ft_putstr_fd(cmd, 2);
ft_putstr_fd(": ", 2); ft_putstr_fd(": ", 2);
ft_putstr_fd(file, 2);
ft_putstr_fd(": ", 2);
ft_putstr_fd(msg, 2); ft_putstr_fd(msg, 2);
ft_putchar_fd('\n', 2); if (msg2)
return (1); ft_putstr_fd(msg2, 2);
}
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);
return (1); return (1);
} }
int is_valid_identifier(char *ident) int is_valid_identifier(char *ident, int silent)
{ {
int i; int i;
@ -46,8 +35,8 @@ int is_valid_identifier(char *ident)
if (!ft_isalpha(ident[i]) && !ft_isdigit(ident[i]) if (!ft_isalpha(ident[i]) && !ft_isdigit(ident[i])
&& ident[i] != '_') && ident[i] != '_')
{ {
ft_putstr_fd("export: not an identifier: ", 2); if (!silent)
ft_putstr_fd(ident, 2); cmd_error("export", "not an identifier: ", ident);
ft_putchar_fd('\n', 2); ft_putchar_fd('\n', 2);
return (0); return (0);
} }
@ -55,8 +44,8 @@ int is_valid_identifier(char *ident)
} }
return (1); return (1);
} }
ft_putstr_fd("export: not an identifier: ", 2); if (!silent)
ft_putstr_fd(ident, 2); cmd_error("export", "not an identifier: ", ident);
ft_putchar_fd('\n', 2); ft_putchar_fd('\n', 2);
return (0); return (0);
} }

7
lexer.c

@ -6,7 +6,7 @@
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */ /* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ /* 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)) if (check_state(lex, &line))
continue ; continue ;
if (lex->state != S_QUOTE_ST && lex->deep == 0 && *line == '$' 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); tmp_i = replace_var(datas, &line, tmp, tmp_i);
lex->empty = 1;
}
else if (check_register(lex, &line, tmp)) else if (check_register(lex, &line, tmp))
tmp_i = (ft_bzero(tmp, STR_MAX_SIZE), 0); tmp_i = (ft_bzero(tmp, STR_MAX_SIZE), 0);
else if (!(lex->state == ROOT_ST && lex->deep == 0 else if (!(lex->state == ROOT_ST && lex->deep == 0

2
minishell.c

@ -6,7 +6,7 @@
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */ /* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ /* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

7
minishell.h

@ -6,7 +6,7 @@
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */ /* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ /* 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); void halt(t_datas *datas, int ret_code, int silent);
// ---------------------------------- Errors.c // ---------------------------------- Errors.c
int file_error(char *cmd, char *file, char *msg); int cmd_error(char *cmd, char *msg, char *msg2);
int cmd_error(char *cmd, char *msg); int is_valid_identifier(char *ident, int silent);
int is_valid_identifier(char *ident);
// ----------------------------------Builtins.c // ----------------------------------Builtins.c
int is_builtin(char *cmd); int is_builtin(char *cmd);

Loading…
Cancel
Save