Browse Source

Norminage

master
narnaud 3 years ago
parent
commit
5664ba48fd
  1. 1
      README.md
  2. 8
      built-in.c
  3. 23
      caller.c
  4. 52
      env.c
  5. 12
      lexer.c
  6. 15
      libft/nbr/ft_max.c
  7. 4
      minishell.c
  8. 12
      parser.c
  9. 5
      utils_2.c

1
README.md

@ -3,7 +3,6 @@
## TODO : ## TODO :
- Protect mallocs, - Protect mallocs,
- [WIP]Norminette
- verify exits codes - verify exits codes
https://wiki.bash-hackers.org/scripting/basics?s[]=variables#exit_codes https://wiki.bash-hackers.org/scripting/basics?s[]=variables#exit_codes

8
built-in.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */ /* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */
/* Updated: 2022/05/06 13:37:47 by narnaud ### ########.fr */ /* Updated: 2022/05/07 23:17:15 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -62,9 +62,11 @@ int ft_cd(t_command *cmd)
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], "Permission denied\n")); return (file_error(cmd->argv[0], cmd->argv[1], \
"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], "Not a directory\n")); return (file_error(cmd->argv[0], cmd->argv[1], \
"Not a directory\n"));
chdir(cmd->argv[1]); chdir(cmd->argv[1]);
return (1); return (1);
} }

23
caller.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */
/* Updated: 2022/05/06 13:32:33 by narnaud ### ########.fr */ /* Updated: 2022/05/07 23:16:47 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,13 +17,13 @@ static pid_t caller(t_datas *datas, t_command *cmd);
int is_builtin(char *cmd) int is_builtin(char *cmd)
{ {
if (!ft_strncmp(cmd, "echo", 5) || if (!ft_strncmp(cmd, "echo", 5) \
!ft_strncmp(cmd, "pwd", 4) || || !ft_strncmp(cmd, "pwd", 4) \
!ft_strncmp(cmd, "cd", 3) || || !ft_strncmp(cmd, "cd", 3) \
!ft_strncmp(cmd, "export", 7) || || !ft_strncmp(cmd, "export", 7) \
!ft_strncmp(cmd, "unset", 6) || || !ft_strncmp(cmd, "unset", 6) \
!ft_strncmp(cmd, "env", 4) || || !ft_strncmp(cmd, "env", 4) \
!ft_strncmp(cmd, "exit", 5)) || !ft_strncmp(cmd, "exit", 5))
return (1); return (1);
else else
return (0); return (0);
@ -90,8 +90,9 @@ static void exe(t_datas *datas, t_command *cmd)
path_length = ft_strlen(*path_dirs) + ft_strlen(cmd->argv[0]) + 2; path_length = ft_strlen(*path_dirs) + ft_strlen(cmd->argv[0]) + 2;
path = ft_calloc(path_length, sizeof(char)); path = ft_calloc(path_length, sizeof(char));
ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs));
ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); ft_memcpy(path + ft_strlen(*path_dirs), "/", 1);
ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0])); ft_memcpy(path + ft_strlen(*path_dirs) + 1, \
cmd->argv[0], ft_strlen(cmd->argv[0]));
execve(path, cmd->argv, datas->envp); execve(path, cmd->argv, datas->envp);
free(path); free(path);
path_dirs++; path_dirs++;
@ -116,5 +117,5 @@ static pid_t caller(t_datas *datas, t_command *cmd)
} }
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
return pid; return (pid);
} }

52
env.c

@ -6,13 +6,14 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ /* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */
/* Updated: 2022/05/06 13:39:03 by narnaud ### ########.fr */ /* Updated: 2022/05/07 23:38:40 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static void clean_env(t_datas *datas); static void clean_env(t_datas *datas, int i);
int ft_unset(t_datas *datas, t_command *cmd) int ft_unset(t_datas *datas, t_command *cmd)
{ {
char **env; char **env;
@ -32,14 +33,9 @@ int ft_unset(t_datas *datas, t_command *cmd)
{ {
env = ft_split(datas->envp[j], '='); env = ft_split(datas->envp[j], '=');
if (!ft_strncmp(cmd->argv[i], env[0], name_len + 1)) if (!ft_strncmp(cmd->argv[i], env[0], name_len + 1))
{ clean_env(datas, j);
free(datas->envp[j]); else if (++j)
datas->envp[j] = NULL;
clean_env(datas);
continue;
}
ft_free_split(env); ft_free_split(env);
j++;
} }
i++; i++;
} }
@ -66,6 +62,7 @@ int ft_env(t_datas *datas, t_command *cmd)
} }
static int is_valid_identifier(char *ident); static int is_valid_identifier(char *ident);
int ft_export(t_datas *datas, t_command *cmd) int ft_export(t_datas *datas, t_command *cmd)
{ {
char **new; char **new;
@ -79,32 +76,27 @@ int ft_export(t_datas *datas, t_command *cmd)
new = ft_split(cmd->argv[1], '='); new = ft_split(cmd->argv[1], '=');
name_len = ft_strlen(new[0]); name_len = ft_strlen(new[0]);
if (!is_valid_identifier(new[0])) if (!is_valid_identifier(new[0]))
{
ft_putstr_fd("export: not an identifier: ", 2);
ft_putstr_fd(new[0], 2);
ft_putchar_fd('\n', 2);
return (0); return (0);
}
while (datas->envp[i]) while (datas->envp[i])
{ {
env = ft_split(datas->envp[i], '='); env = ft_split(datas->envp[i], '=');
if (!ft_strncmp(new[0], env[0], name_len + 1)) if (!ft_strncmp(new[0], env[0], name_len + 1))
{ datas->envp[i] = (free(datas->envp[i]), NULL);
datas->envp[i] = ft_strdup(cmd->argv[1]); else
return (1);
}
ft_free_split(env);
i++; i++;
ft_free_split(env);
} }
ft_free_split(new);
datas->envp[i] = ft_strdup(cmd->argv[1]); datas->envp[i] = ft_strdup(cmd->argv[1]);
return (1); return (1);
} }
static void clean_env(t_datas *datas) static void clean_env(t_datas *datas, int i)
{ {
int i;
int j; int j;
free(datas->envp[i]);
datas->envp[i] = NULL;
i = 0; i = 0;
j = 0; j = 0;
while (i < ENVP_MAX_SIZE) while (i < ENVP_MAX_SIZE)
@ -125,15 +117,27 @@ static void clean_env(t_datas *datas)
static int is_valid_identifier(char *ident) static int is_valid_identifier(char *ident)
{ {
int i;
i = 0;
if (ft_isalpha(ident[0]) || ident[0] == '_') if (ft_isalpha(ident[0]) || ident[0] == '_')
{ {
while (*ident) while (ident[i])
{
if (!ft_isalpha(ident[i]) && !ft_isdigit(ident[i]) \
&& ident[i] != '_')
{ {
if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_') ft_putstr_fd("export: not an identifier: ", 2);
ft_putstr_fd(ident, 2);
ft_putchar_fd('\n', 2);
return (0); return (0);
ident++; }
i++;
} }
return (1); return (1);
} }
ft_putstr_fd("export: not an identifier: ", 2);
ft_putstr_fd(ident, 2);
ft_putchar_fd('\n', 2);
return (0); return (0);
} }

12
lexer.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */
/* Updated: 2022/05/06 11:19:26 by narnaud ### ########.fr */ /* Updated: 2022/05/07 23:12:25 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,14 +23,14 @@ t_token *lexer(t_datas *datas, char *line)
char *tmp; char *tmp;
int tmp_i; int tmp_i;
lex = ft_calloc(1, sizeof *lex); lex = ft_calloc(1, sizeof(*lex));
lex->state = ROOT_ST; lex->state = ROOT_ST;
tmp = ft_calloc(STR_MAX_SIZE, sizeof *tmp); tmp = ft_calloc(STR_MAX_SIZE, sizeof(*tmp));
tmp_i = 0; tmp_i = 0;
while (*line) while (*line)
{ {
if (check_state(lex, &line)) if (check_state(lex, &line))
continue; continue ;
if (lex->state != S_QUOTE_ST && *line == '$') if (lex->state != S_QUOTE_ST && *line == '$')
tmp_i = replace_var(datas, &line, tmp, tmp_i); tmp_i = replace_var(datas, &line, tmp, tmp_i);
else if (check_register(lex, &line, tmp)) else if (check_register(lex, &line, tmp))
@ -87,7 +87,7 @@ static int create_token(t_lexer *lex, char str[])
lex->next_type = WORD; lex->next_type = WORD;
if (!str[0]) if (!str[0])
return (0); return (0);
tok = ft_calloc(1, sizeof *tok); tok = ft_calloc(1, sizeof(*tok));
tok->type = type; tok->type = type;
tok->value = ft_strdup(str); tok->value = ft_strdup(str);
tmp = lex->tokens; tmp = lex->tokens;
@ -115,7 +115,7 @@ static int check_register(t_lexer *lex, char **line, char *tmp)
(*line)++; (*line)++;
spaces = 1; spaces = 1;
} }
if (set_redir(lex, line, '>') if (set_redir(lex, line, '>') \
|| set_redir(lex, line, '<')) || set_redir(lex, line, '<'))
return (create_token(lex, tmp)); return (create_token(lex, tmp));
else if (**line == '|') else if (**line == '|')

15
libft/nbr/ft_max.c

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_max.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud@student.42nice.fr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/07 22:56:56 by narnaud@stude #+# #+# */
/* Updated: 2022/05/07 22:57:21 by narnaud@stude ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h" #include "../libft.h"
int ft_max(int a, int b) int ft_max(int a, int b)
@ -5,6 +17,5 @@ int ft_max(int a, int b)
if (a > b) if (a > b)
return (a); return (a);
else else
return(b); return (b);
} }

4
minishell.c

@ -6,11 +6,11 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/06 12:03:13 by narnaud ### ########.fr */ /* Updated: 2022/05/07 23:08:05 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
# include "minishell.h" #include "minishell.h"
void termios(int ctl) void termios(int ctl)
{ {

12
parser.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */ /* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */
/* Updated: 2022/05/06 11:21:18 by narnaud ### ########.fr */ /* Updated: 2022/05/07 23:07:11 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,12 +21,12 @@ t_command *parser(t_datas *datas, t_token *tok, t_command *prev)
if (!tok) if (!tok)
return (NULL); return (NULL);
cmd = ft_calloc(1, sizeof *cmd); cmd = ft_calloc(1, sizeof(*cmd));
if (prev) if (prev)
cmd->prev = prev; cmd->prev = prev;
cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *));
cmd->argc = 0; cmd->argc = 0;
while(tok && tok->type != PIPE) while (tok && tok->type != PIPE)
tok = parse_cmd(datas, tok, cmd); tok = parse_cmd(datas, tok, cmd);
if (tok) if (tok)
{ {
@ -34,7 +34,7 @@ t_command *parser(t_datas *datas, t_token *tok, t_command *prev)
free(tok->value); free(tok->value);
free(tok); free(tok);
} }
return(cmd); return (cmd);
} }
static size_t count_arguments(t_token *tok) static size_t count_arguments(t_token *tok)
@ -74,11 +74,11 @@ static int push_heredoc(t_datas *datas, char *str)
int pip[2]; int pip[2];
pipe(pip); pipe(pip);
while(1) while (1)
{ {
line = readline(">"); line = readline(">");
if (!ft_strncmp(line, str, ft_strlen(str) + 1)) if (!ft_strncmp(line, str, ft_strlen(str) + 1))
break; break ;
ft_putstr_fd(expend_str(datas, line), pip[1]); ft_putstr_fd(expend_str(datas, line), pip[1]);
write(pip[1], "\n", 1); write(pip[1], "\n", 1);
} }

5
utils_2.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */ /* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */
/* Updated: 2022/05/06 13:25:32 by narnaud ### ########.fr */ /* Updated: 2022/05/07 23:04:44 by narnaud@stude ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,6 +25,7 @@ int file_error(char *cmd, char *file, char *msg)
} }
static char *get_var_value(t_datas *datas, char **line, int name_len); static char *get_var_value(t_datas *datas, char **line, int name_len);
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i) int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
{ {
int name_len; int name_len;
@ -58,7 +59,7 @@ char *expend_str(t_datas *datas, char *line)
char *tmp; char *tmp;
int tmp_i; int tmp_i;
tmp = ft_calloc(STR_MAX_SIZE, sizeof *tmp); tmp = ft_calloc(STR_MAX_SIZE, sizeof(*tmp));
if (!tmp) if (!tmp)
free(tmp); free(tmp);
tmp_i = 0; tmp_i = 0;

Loading…
Cancel
Save