Make lines comply with the 42 Norm
authorLilia-42 <nikolovalilianenkova@gmail.com>
Sat, 31 Aug 2024 11:34:06 +0000 (13:34 +0200)
committerLilia-42 <nikolovalilianenkova@gmail.com>
Sat, 31 Aug 2024 11:34:06 +0000 (13:34 +0200)
src/execution.c
src/main.c

index e367194f1fc6423ffdc7fc0f15f13d0b717ff931..6e184ab5ad1415aac8e5105b143c2a9d23eace0d 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/31 11:30:11 by lnikolov         ###   ########.fr       */
+/*   Updated: 2024/08/31 12:43:50 by lnikolov         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -46,9 +46,11 @@ void clear_old(t_vec *assignments, const char *new)
        return ;
 }
 
-int    add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int quote);
+int    add_word(t_vec *exp_str, const char *word,
+               const t_execution_env *env, int quote);
 
-int    add_assignment(t_vec *assignments, const char *assignment, const t_execution_env *env)
+int    add_assignment(t_vec *assignments, const char *assignment,
+       const t_execution_env *env)
 {
        t_vec   advanced_copy;
        char    *copy;
@@ -67,7 +69,8 @@ int   add_assignment(t_vec *assignments, const char *assignment, const t_execution
        return (1);
 }
 
-int    save_assignments_prefix(t_vec *assignments, t_parse_tree_node *prefix, const t_execution_env *env)
+int    save_assignments_prefix(t_vec *assignments, t_parse_tree_node *prefix,
+       const t_execution_env *env)
 {
        size_t                          i;
        t_parse_tree_node       *subnode;
@@ -89,7 +92,8 @@ int   save_assignments_prefix(t_vec *assignments, t_parse_tree_node *prefix, const
        return (0);
 }
 
-int    save_assignments(t_vec *assignments, t_parse_tree_node *simple_command, const t_execution_env *env)
+int    save_assignments(t_vec *assignments, t_parse_tree_node *simple_command,
+       const t_execution_env *env)
 {
        t_parse_tree_node       *subnode;
 
@@ -128,7 +132,8 @@ int expand_filename(char **filename, const t_execution_env *env)
        return (0);
 }
 
-int    add_redirection_file(t_vec *redirections, t_parse_tree_node *io_file, int fd, t_execution_env *env)
+int    add_redirection_file(t_vec *redirections,
+       t_parse_tree_node *io_file, int fd, t_execution_env *env)
 {
        t_redirection                   redir;
        const t_parse_tree_node *operator;
@@ -158,9 +163,11 @@ int        add_redirection_file(t_vec *redirections, t_parse_tree_node *io_file, int fd
                if (fd < 0)
                        fd = STDOUT_FILENO;
                if (is_token_type(operator, "GREAT"))
-                       sec_fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
+                       sec_fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC,
+                                       S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
                else
-                       sec_fd = open(filename, O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
+                       sec_fd = open(filename, O_CREAT | O_WRONLY | O_APPEND,
+                                       S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
                if (sec_fd == -1)
                {
                        free(filename);
@@ -172,7 +179,8 @@ int add_redirection_file(t_vec *redirections, t_parse_tree_node *io_file, int fd
        return (ft_vec_append(redirections, &redir) != success);
 }
 
-int    write_line_to_pipe(int pipe_fd, const char *line, const t_execution_env *env, int expand)
+int    write_line_to_pipe(int pipe_fd, const char *line,
+       const t_execution_env *env, int expand)
 {
        size_t          i;
        char            *var;
@@ -233,7 +241,8 @@ char        *unquote_delimiter(const char *str)
        return (res);
 }
 
-int    here_file_to_pipe(int pipe_fd, const char *delimiter, const t_execution_env *env)
+int    here_file_to_pipe(int pipe_fd, const char *delimiter,
+       const t_execution_env *env)
 {
        int             expand;
        char    *line;
@@ -259,14 +268,17 @@ int       here_file_to_pipe(int pipe_fd, const char *delimiter, const t_execution_env
                line = get_next_line(STDIN_FILENO);
        }
        if (!line)
-               ft_printf("minishell: warning: here-document delimited by EOF instead of %s!\n", unquoted_delimiter);
+               ft_printf
+               ("minishell: warning: here-document delimited by EOF instead of %s!\n",
+                       unquoted_delimiter);
        get_next_line(-1);
        free(line);
        free(unquoted_delimiter);
        return (0);
 }
 
-int    add_redirection_here(t_vec *redirections, t_parse_tree_node *io_here, int fd, const t_execution_env *env)
+int    add_redirection_here(t_vec *redirections,
+       t_parse_tree_node *io_here, int fd, const t_execution_env *env)
 {
        char                            *delimiter;
        int                                     pipe_fds[2];
@@ -288,13 +300,15 @@ int       add_redirection_here(t_vec *redirections, t_parse_tree_node *io_here, int fd
        free(delimiter);
        if (err)
                return (1);
-       redir = (t_redirection){.from_to_fds[0] = fd, .from_to_fds[1] = pipe_fds[0]};
+       redir = (t_redirection){.from_to_fds[0] = fd,
+               .from_to_fds[1] = pipe_fds[0]};
        return (ft_vec_append(redirections, &redir) != success);
 }
 
-int    add_redirection(t_vec *redirections, t_parse_tree_node *redirect, t_execution_env *env)
+int    add_redirection(t_vec *redirections,
+       t_parse_tree_node *redirect, t_execution_env *env)
 {
-       int                             fd;
+       int                                     fd;
        t_parse_tree_node       *subnode;
 
        fd = -1;
@@ -312,7 +326,8 @@ int add_redirection(t_vec *redirections, t_parse_tree_node *redirect, t_executio
                return (add_redirection_here(redirections, subnode, fd, env));
 }
 
-int    save_redirections_psfix(t_vec *redirections, t_parse_tree_node *node, t_execution_env *env)
+int    save_redirections_psfix(t_vec *redirections,
+       t_parse_tree_node *node, t_execution_env *env)
 {
        size_t                          i;
        t_parse_tree_node       *subnode;
@@ -326,7 +341,8 @@ int save_redirections_psfix(t_vec *redirections, t_parse_tree_node *node, t_exec
                        if (add_redirection(redirections, subnode, env))
                                return (1);
                }
-               else if (is_token_type(subnode, "cmd_suffix") || is_token_type(subnode, "cmd_prefix"))
+               else if (is_token_type(subnode, "cmd_suffix")
+                       || is_token_type(subnode, "cmd_prefix"))
                        if (save_redirections_psfix(redirections, subnode, env))
                                return (1);
                ++i;
@@ -334,7 +350,8 @@ int save_redirections_psfix(t_vec *redirections, t_parse_tree_node *node, t_exec
        return (0);
 }
 
-int    save_redirections(t_vec *redirections, t_parse_tree_node *simple_command, t_execution_env *env)
+int    save_redirections(t_vec *redirections,
+       t_parse_tree_node *simple_command, t_execution_env *env)
 {
        t_parse_tree_node       *subnode;
 
@@ -344,7 +361,8 @@ int save_redirections(t_vec *redirections, t_parse_tree_node *simple_command, t_
        if (is_token_type(subnode, "cmd_prefix"))
                if (save_redirections_psfix(redirections, subnode, env))
                        return (1);
-       subnode = ft_vec_access(&simple_command->children, simple_command->children.size - 1);
+       subnode = ft_vec_access(&simple_command->children,
+                       simple_command->children.size - 1);
        if (is_token_type(subnode, "cmd_suffix"))
                if (save_redirections_psfix(redirections, subnode, env))
                        return (1);
@@ -353,7 +371,8 @@ int save_redirections(t_vec *redirections, t_parse_tree_node *simple_command, t_
 
 static const char      space = ' ';
 
-int    add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int enquote_result)
+int    add_word(t_vec *exp_str, const char *word,
+       const t_execution_env *env, int enquote_result)
 {
        size_t          i;
        char            *var;
@@ -371,7 +390,8 @@ int add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int e
                        quote = '\'' * !quote;
                else if (word[i] == '"' && quote != '\'')
                        quote = '"' * !quote;
-               if (word[i] == '$' && quote != '\'' && (ft_isalnum(word[i + 1]) || word[i + 1] == '_' || word[i + 1] == '?'))
+               if (word[i] == '$' && quote != '\'' && (ft_isalnum(word[i + 1])
+                               || word[i + 1] == '_' || word[i + 1] == '?'))
                {
                        ++i;
                        if (word[i] == '?')
@@ -388,14 +408,15 @@ int       add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int e
                                const_val = get_env_var_value(env, var);
                                free(var);
                                if (!const_val)
-                                       continue;
+                                       continue ;
                                value = ft_strdup(const_val);
                        }
                        if (!value)
                                return (1);
                        if (enquote_result)
                                error = error || ft_vec_append(exp_str, "'") != success;
-                       error = error || ft_vec_append_range(exp_str, value, ft_strlen(value)) != success;
+                       error = error || ft_vec_append_range(exp_str,
+                                       value, ft_strlen(value)) != success;
                        if (enquote_result)
                                error = error || ft_vec_append(exp_str, "'") != success;
                        free(value);
@@ -411,7 +432,8 @@ int add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int e
        return (0);
 }
 
-int    expand_cmd(t_vec *exp_str, const t_parse_tree_node *node, const t_execution_env *env)
+int    expand_cmd(t_vec *exp_str,
+       const t_parse_tree_node *node, const t_execution_env *env)
 {
        size_t                                  i;
        const t_parse_tree_node *subnode;
@@ -553,14 +575,14 @@ char      **expand(t_parse_tree_node *simple_command, const t_execution_env *env)
        while (i < simple_command->children.size)
        {
                subnode = ft_vec_access(&simple_command->children, i);
-               if (is_token_type(subnode, "cmd_name")
+               if ((is_token_type(subnode, "cmd_name")
                                || is_token_type(subnode, "cmd_word")
                                || is_token_type(subnode, "cmd_suffix"))
-                       if (expand_cmd(&expanded_str, subnode, env))
-                       {
-                               ft_vec_free(&expanded_str, NULL);
-                               return (NULL);
-                       }
+                       && expand_cmd(&expanded_str, subnode, env))
+               {
+                       ft_vec_free(&expanded_str, NULL);
+                       return (NULL);
+               }
                ++i;
        }
        if (ft_vec_append(&expanded_str, &null_char) != success)
@@ -587,7 +609,8 @@ int assignments_to_env(const t_vec *assignments, t_execution_env *env)
                var_name = get_var_name(var_line);
                if (!var_name)
                        return (1);
-               if (set_env_var_value(env, var_name, var_line + ft_strlen(var_name) + 1))
+               if (set_env_var_value(env, var_name,
+                               var_line + ft_strlen(var_name) + 1))
                {
                        free(var_name);
                        return (1);
@@ -722,10 +745,11 @@ void      close_redirections(const t_vec *redirections)
        return ;
 }
 
-int    add_exported(__attribute__((unused)) t_vec *assignments, __attribute__((unused)) const t_execution_env *env)
+int    add_exported(__attribute__((unused))
+       t_vec *assignments, __attribute__((unused)) const t_execution_env *env)
 {
-       size_t  i;
-       char    *var;
+       size_t          i;
+       char            *var;
        const t_vec     *exported;
 
        exported = &env->vars->exported;
@@ -733,7 +757,7 @@ int add_exported(__attribute__((unused)) t_vec *assignments, __attribute__((unus
        while (i < exported->size)
        {
                var = ft_strdup(*(char *const *)ft_vec_caccess(exported, i));
-               if (ft_vec_append(assignments, &var) != success)        // Should exported be overwritten?
+               if (ft_vec_append(assignments, &var) != success)
                        return (1);
                ++i;
        }
@@ -804,7 +828,9 @@ int restore_std_filenos(int fds[3])
        return (0);
 }
 
-int    ex_builtin(char **fields, __attribute__((unused)) t_vec *assignments, __attribute__((unused)) const t_vec *redirections, t_execution_env *env)
+int    ex_builtin(char **fields, __attribute__((unused))
+       t_vec *assignments, __attribute__((unused)) const t_vec *redirections,
+               t_execution_env *env)
 {
        int     fds[3];
 
@@ -824,7 +850,8 @@ int ex_builtin(char **fields, __attribute__((unused)) t_vec *assignments, __attr
        else if (!ft_strcmp(fields[0], "unset"))
                env->last_builtin_ret_val = unset(count_fields(fields), fields, env);
        else if (!ft_strcmp(fields[0], "exit"))
-               env->last_builtin_ret_val = execute_exit(count_fields(fields), fields, env);
+               env->last_builtin_ret_val = execute_exit(count_fields(fields),
+                               fields, env);
        else
                return (1);
        env->last_was_builtin = 1;
@@ -849,7 +876,8 @@ void        close_fds(t_vec *fds_to_close)
        return ;
 }
 
-int    ex_fields(char **fields, t_vec *assignments, const t_vec *redirections, t_execution_env *env)
+int    ex_fields(char **fields, t_vec *assignments,
+       const t_vec *redirections, t_execution_env *env)
 {
        pid_t   pid;
        char    *path;
@@ -933,8 +961,8 @@ int ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env)
 
 t_ft_stat      v_strcopy(void *v_dest, const void *v_src)
 {
-       char            **dest;
-       const char      *const *src;
+       char                            **dest;
+       const char *const       *src;
 
        dest = v_dest;
        src = v_src;
@@ -1047,7 +1075,8 @@ void      wait_for_return(t_execution_env *env)
        {
                if (env->child_pids.size == 0)
                        return ;
-               last_pid = *(pid_t *)ft_vec_access(&env->child_pids, env->child_pids.size - 1);
+               last_pid = *(pid_t *)ft_vec_access(&env->child_pids,
+                               env->child_pids.size - 1);
                waitpid(last_pid, &status, 0);
                if (g_last_signal != 0)
                        killall(&env->child_pids, g_last_signal);
index 9902951933d158552cb90d41d004e7328eaeff3d..1516de7649f256520762ccbd21ce97787ddfd2f7 100644 (file)
@@ -3,10 +3,10 @@
 /*                                                        :::      ::::::::   */
 /*   main.c                                             :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
-/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/26 13:11:47 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/31 09:55:10 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/31 12:52:28 by lnikolov         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -21,7 +21,7 @@
 
 volatile sig_atomic_t  g_last_signal;
 
-void   handler(int     sig_num)
+void   handler(int sig_num)
 {
        g_last_signal = sig_num;
        return ;
@@ -76,7 +76,7 @@ void  setup_terminal(void)
 
 void   regenerate_terminal(void)
 {
-       struct termios termios;
+       struct termios  termios;
 
        termios_control(term_load, &termios);
        tcsetattr(STDIN_FILENO, TCSANOW, &termios);
@@ -92,8 +92,8 @@ int   main(int argc, char **argv)
 
 int    main(int argc, __attribute__((unused)) char **argv, char **envp)
 {
-       int                     res;
-       char            *line;
+       int                             res;
+       char                    *line;
        t_execution_env env;
 
        if (argc > 1)