From 5664ba48fdbf84ed0e42b2006d845eace5d2bbc5 Mon Sep 17 00:00:00 2001 From: narnaud Date: Sat, 7 May 2022 23:40:07 +0200 Subject: [PATCH] Norminage --- README.md | 1 - built-in.c | 12 ++++++---- caller.c | 37 ++++++++++++++-------------- env.c | 60 ++++++++++++++++++++++++---------------------- lexer.c | 22 ++++++++--------- libft/nbr/ft_max.c | 15 ++++++++++-- minishell.c | 8 +++---- parser.c | 18 +++++++------- utils_2.c | 57 +++++++++++++++++++++---------------------- 9 files changed, 124 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index ec8fb21..ed0396d 100755 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ ## TODO : - Protect mallocs, -- [WIP]Norminette - verify exits codes https://wiki.bash-hackers.org/scripting/basics?s[]=variables#exit_codes diff --git a/built-in.c b/built-in.c index 5296e0a..e8d82c8 100755 --- a/built-in.c +++ b/built-in.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ int ft_echo(t_command *cmd) if (cmd->fd[1]) fd_out = cmd->fd[1]; - else + else fd_out = 1; no_nl = 0; i = 1; @@ -43,7 +43,7 @@ int ft_echo(t_command *cmd) int ft_pwd(t_command *cmd) { char *dir; - int fd_out; + int fd_out; if (cmd->fd[1]) fd_out = cmd->fd[1]; @@ -62,9 +62,11 @@ int ft_cd(t_command *cmd) if (cmd->argv[1]) { 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)) - 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]); return (1); } diff --git a/caller.c b/caller.c index 7a77074..5560e84 100755 --- a/caller.c +++ b/caller.c @@ -6,24 +6,24 @@ /* 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 */ /* */ /* ************************************************************************** */ #include "minishell.h" -static void exe(t_datas *datas, t_command *cmd); -static pid_t caller(t_datas *datas, t_command *cmd); +static void exe(t_datas *datas, t_command *cmd); +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) || - !ft_strncmp(cmd, "pwd", 4) || - !ft_strncmp(cmd, "cd", 3) || - !ft_strncmp(cmd, "export", 7) || - !ft_strncmp(cmd, "unset", 6) || - !ft_strncmp(cmd, "env", 4) || - !ft_strncmp(cmd, "exit", 5)) + if (!ft_strncmp(cmd, "echo", 5) \ + || !ft_strncmp(cmd, "pwd", 4) \ + || !ft_strncmp(cmd, "cd", 3) \ + || !ft_strncmp(cmd, "export", 7) \ + || !ft_strncmp(cmd, "unset", 6) \ + || !ft_strncmp(cmd, "env", 4) \ + || !ft_strncmp(cmd, "exit", 5)) return (1); else return (0); @@ -52,10 +52,10 @@ int builtin_call(t_datas *datas, t_command *cmd) int piper(t_datas *datas, t_command *cmd) { int pip[2]; - int status; + int status; pid_t pid; - if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) + if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) { pipe(pip); cmd->fd[1] = pip[1]; @@ -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 = ft_calloc(path_length, sizeof(char)); 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, cmd->argv[0], ft_strlen(cmd->argv[0])); + ft_memcpy(path + ft_strlen(*path_dirs), "/", 1); + ft_memcpy(path + ft_strlen(*path_dirs) + 1, \ + cmd->argv[0], ft_strlen(cmd->argv[0])); execve(path, cmd->argv, datas->envp); free(path); path_dirs++; @@ -99,14 +100,14 @@ static void exe(t_datas *datas, t_command *cmd) } } -static pid_t caller(t_datas *datas, t_command *cmd) +static pid_t caller(t_datas *datas, t_command *cmd) { pid_t pid; pid = fork(); if (!pid) { - if (builtin_call(datas, cmd) == -1) + if (builtin_call(datas, cmd) == -1) { if (cmd->fd[0]) dup2(cmd->fd[0], STDIN_FILENO); @@ -116,5 +117,5 @@ static pid_t caller(t_datas *datas, t_command *cmd) } exit(EXIT_SUCCESS); } - return pid; + return (pid); } diff --git a/env.c b/env.c index 40e150e..65e6249 100755 --- a/env.c +++ b/env.c @@ -6,13 +6,14 @@ /* 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" -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) { char **env; @@ -32,14 +33,9 @@ int ft_unset(t_datas *datas, t_command *cmd) { env = ft_split(datas->envp[j], '='); if (!ft_strncmp(cmd->argv[i], env[0], name_len + 1)) - { - free(datas->envp[j]); - datas->envp[j] = NULL; - clean_env(datas); - continue; - } - ft_free_split(env); - j++; + clean_env(datas, j); + else if (++j) + ft_free_split(env); } i++; } @@ -48,7 +44,7 @@ int ft_unset(t_datas *datas, t_command *cmd) int ft_env(t_datas *datas, t_command *cmd) { - int i; + int i; int fd_out; if (cmd->fd[1]) @@ -66,6 +62,7 @@ int ft_env(t_datas *datas, t_command *cmd) } static int is_valid_identifier(char *ident); + int ft_export(t_datas *datas, t_command *cmd) { char **new; @@ -75,36 +72,31 @@ int ft_export(t_datas *datas, t_command *cmd) i = 0; if (cmd->argc < 2) - return (0); + return (0); new = ft_split(cmd->argv[1], '='); name_len = ft_strlen(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); - } while (datas->envp[i]) { env = ft_split(datas->envp[i], '='); if (!ft_strncmp(new[0], env[0], name_len + 1)) - { - datas->envp[i] = ft_strdup(cmd->argv[1]); - return (1); - } + datas->envp[i] = (free(datas->envp[i]), NULL); + else + i++; ft_free_split(env); - i++; } + ft_free_split(new); datas->envp[i] = ft_strdup(cmd->argv[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; j = 0; while (i < ENVP_MAX_SIZE) @@ -125,15 +117,27 @@ static void clean_env(t_datas *datas) static int is_valid_identifier(char *ident) { + int i; + + i = 0; if (ft_isalpha(ident[0]) || ident[0] == '_') { - while (*ident) + while (ident[i]) { - if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *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); + ft_putchar_fd('\n', 2); return (0); - ident++; + } + i++; } return (1); } + ft_putstr_fd("export: not an identifier: ", 2); + ft_putstr_fd(ident, 2); + ft_putchar_fd('\n', 2); return (0); } diff --git a/lexer.c b/lexer.c index c4bd30a..daf3cfd 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/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; int tmp_i; - lex = ft_calloc(1, sizeof *lex); + lex = ft_calloc(1, sizeof(*lex)); lex->state = ROOT_ST; - tmp = ft_calloc(STR_MAX_SIZE, sizeof *tmp); + tmp = ft_calloc(STR_MAX_SIZE, sizeof(*tmp)); tmp_i = 0; while (*line) { if (check_state(lex, &line)) - continue; + continue ; if (lex->state != S_QUOTE_ST && *line == '$') tmp_i = replace_var(datas, &line, tmp, tmp_i); else if (check_register(lex, &line, tmp)) @@ -76,7 +76,7 @@ static int check_state(t_lexer *lex, char **line) static int create_token(t_lexer *lex, char str[]) { t_token *tok; - t_token *tmp; + t_token *tmp; t_type type; type = lex->type; @@ -87,10 +87,10 @@ static int create_token(t_lexer *lex, char str[]) lex->next_type = WORD; if (!str[0]) return (0); - tok = ft_calloc(1, sizeof *tok); + tok = ft_calloc(1, sizeof(*tok)); tok->type = type; tok->value = ft_strdup(str); - tmp = lex->tokens; + tmp = lex->tokens; while (tmp && tmp->next) tmp = tmp->next; if (!tmp) @@ -115,9 +115,9 @@ static int check_register(t_lexer *lex, char **line, char *tmp) (*line)++; spaces = 1; } - if (set_redir(lex, line, '>') + if (set_redir(lex, line, '>') \ || set_redir(lex, line, '<')) - return (create_token(lex, tmp)); + return (create_token(lex, tmp)); else if (**line == '|') { (*line)++; @@ -134,7 +134,7 @@ static int set_redir(t_lexer *lex, char **line, char ch) { static t_type type_out[2] = {OUT, ADD}; static t_type type_in[2] = {IN, HD}; - t_type *type; + t_type *type; if (ch == '>') type = type_out; @@ -148,5 +148,5 @@ static int set_redir(t_lexer *lex, char **line, char ch) lex->next_type = type[0]; return (1); } - return (0); + return (0); } diff --git a/libft/nbr/ft_max.c b/libft/nbr/ft_max.c index 050fa3a..53a7605 100755 --- a/libft/nbr/ft_max.c +++ b/libft/nbr/ft_max.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_max.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud@student.42nice.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" int ft_max(int a, int b) @@ -5,6 +17,5 @@ int ft_max(int a, int b) if (a > b) return (a); else - return(b); + return (b); } - diff --git a/minishell.c b/minishell.c index 6d86adc..c20df95 100755 --- a/minishell.c +++ b/minishell.c @@ -6,11 +6,11 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) { @@ -21,7 +21,7 @@ void termios(int ctl) tcgetattr(tty, &termios_p); if (ctl) { - termios_p.c_lflag |= ECHOCTL; + termios_p.c_lflag |= ECHOCTL; signal(SIGINT, nothing); signal(SIGQUIT, nothing); } @@ -38,7 +38,7 @@ void termios(int ctl) void free_cmd(t_command *cmd) { int i; - + if (!cmd) return ; i = 0; diff --git a/parser.c b/parser.c index 2fcf880..089f150 100755 --- a/parser.c +++ b/parser.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) return (NULL); - cmd = ft_calloc(1, sizeof *cmd); + cmd = ft_calloc(1, sizeof(*cmd)); if (prev) cmd->prev = prev; cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); cmd->argc = 0; - while(tok && tok->type != PIPE) + while (tok && tok->type != PIPE) tok = parse_cmd(datas, tok, cmd); if (tok) { @@ -34,7 +34,7 @@ t_command *parser(t_datas *datas, t_token *tok, t_command *prev) free(tok->value); free(tok); } - return(cmd); + return (cmd); } static size_t count_arguments(t_token *tok) @@ -53,7 +53,7 @@ static size_t count_arguments(t_token *tok) static void update_redir(t_datas *datas, t_command *cmd, t_token *tok); -static t_token *parse_cmd(t_datas *datas, t_token *tok, t_command *cmd) +static t_token *parse_cmd(t_datas *datas, t_token *tok, t_command *cmd) { t_token *prev_tok; @@ -68,17 +68,17 @@ static t_token *parse_cmd(t_datas *datas, t_token *tok, t_command *cmd) return (tok); } -static int push_heredoc(t_datas *datas, char *str) +static int push_heredoc(t_datas *datas, char *str) { char *line; int pip[2]; pipe(pip); - while(1) + while (1) { line = readline(">"); if (!ft_strncmp(line, str, ft_strlen(str) + 1)) - break; + break ; ft_putstr_fd(expend_str(datas, line), pip[1]); write(pip[1], "\n", 1); } @@ -86,7 +86,7 @@ static int push_heredoc(t_datas *datas, char *str) return (pip[0]); } -static void update_redir(t_datas *datas, t_command *cmd, t_token *tok) +static void update_redir(t_datas *datas, t_command *cmd, t_token *tok) { if (tok->type == OUT) { diff --git a/utils_2.c b/utils_2.c index db28e83..13f7442 100644 --- a/utils_2.c +++ b/utils_2.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* utils_2.c :+: :+: :+: */ +/* utils_2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -20,11 +20,12 @@ int file_error(char *cmd, char *file, char *msg) ft_putstr_fd(file, 2); ft_putstr_fd(": ", 2); ft_putstr_fd(msg, 2); - ft_putchar_fd('\n', 2); + ft_putchar_fd('\n', 2); return (0); } -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 name_len; @@ -53,12 +54,12 @@ int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i) return (tmp_i); } -char *expend_str(t_datas *datas, char *line) +char *expend_str(t_datas *datas, char *line) { char *tmp; int tmp_i; - tmp = ft_calloc(STR_MAX_SIZE, sizeof *tmp); + tmp = ft_calloc(STR_MAX_SIZE, sizeof(*tmp)); if (!tmp) free(tmp); tmp_i = 0; @@ -72,28 +73,28 @@ char *expend_str(t_datas *datas, char *line) return (tmp); } -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 i; - char **env; - char *name; - char *value; + int i; + char **env; + char *name; + char *value; - name = ft_substr(*line, 1, name_len - 1); - i = 0; - value = NULL; - while (datas->envp[i] && !value) - { - env = ft_split(datas->envp[i], '='); - if (!ft_strncmp(name, env[0], name_len + 1)) - value = (free(env[0]), env[1]); - else if (++i) - ft_free_split(env); - } - free(name); - if (value) - free(env); - else - value = strdup(""); - return (value); + name = ft_substr(*line, 1, name_len - 1); + i = 0; + value = NULL; + while (datas->envp[i] && !value) + { + env = ft_split(datas->envp[i], '='); + if (!ft_strncmp(name, env[0], name_len + 1)) + value = (free(env[0]), env[1]); + else if (++i) + ft_free_split(env); + } + free(name); + if (value) + free(env); + else + value = strdup(""); + return (value); }