]> git.dkaiser.de - 42/minishell.git/commitdiff
Some changes
authorDominik Kaiser <dkaiser@3-E-1.42heilbronn.de>
Tue, 14 Jan 2025 17:20:33 +0000 (18:20 +0100)
committerDominik Kaiser <dkaiser@3-E-1.42heilbronn.de>
Tue, 14 Jan 2025 17:20:33 +0000 (18:20 +0100)
include/minishell.h
src/builtins_part_one.c
src/env_to_strlst.c
src/format_string.c
src/main.c
src/repl.c

index 028a4fcc30612e7026dfdf966e1b14d214664bda..f0f458b6a3a33a8be18556518d0a026c67868bfe 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/22 17:14:49 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/11 16:05:11 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/14 14:52:29 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -40,6 +40,6 @@ int                           eval(t_node *node, t_env **env);
 char                   *get_cmd_path(char *cmd, t_env *env);
 int                            execute_cmd(t_cmd *cmd, t_env **env);
 char                   *format_string(char *str, t_env *env);
-
+int set_return_code(int return_code, t_env **env);
 
 #endif
index d64bb549d453f6a5db6a85d943cf5cfa9230c9a2..b6e52615afe6e4b53dafb995450b33097d8b583a 100644 (file)
@@ -6,11 +6,12 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/09 17:01:16 by chuhlig           #+#    #+#             */
-/*   Updated: 2025/01/10 14:36:55 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/14 15:20:42 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "env.h"
+#include <stdio.h>
 
 int    echo(char **av)
 {
@@ -22,7 +23,7 @@ int   echo(char **av)
        if (av[1] == NULL || av[1][0] == '\0')
        {
                write(1, "\n", 1);
-               return (1);
+               return (0);
        }
        if (ft_strncmp(av[1], "-n", 3) == 0)
        {
@@ -59,6 +60,11 @@ int  ft_env(t_env *env)
 {
        while (env != NULL)
        {
+               if (strchr(env->name, '?'))
+               {
+                       env = env->next;
+                       continue;
+               }
                printf("%s", env->name);
                printf("=%s\n", env->value);
                env = env->next;
@@ -89,6 +95,8 @@ int   unset(char **av, t_env **env)
        i = 0;
        while (av[++i])
        {
+               if (ft_strchr(av[i], '?'))
+                       return (1);
                current = *env;
                prev = NULL;
                while (current)
@@ -145,6 +153,8 @@ int export(char **av, t_env **env)
                {
                        tmp = ft_strchr(av[i], '=');
                        *tmp = '\0';
+                       if (ft_strchr(av[i], '?'))
+                               return (1);
                        current = check_existing(*env, av[i]);
                        if (current)
                                free(current->value);
@@ -158,4 +168,20 @@ int        export(char **av, t_env **env)
                }
        }
        return (0);
-}
\ No newline at end of file
+}
+
+void set_return_code(int return_code, t_env **env)
+{
+       t_env *cur;
+
+       cur = check_existing(*env, "?");
+       if (cur)
+               free(cur->value);
+       else
+       {
+               cur = env_new(ft_strdup("?"));
+               cur->next = *env;
+               *env = cur;
+       }
+       cur->value = ft_itoa(return_code);
+}
index c4c98c32e6323454d7114e17aaca1f27f0e25ddc..e0536c025fa7ca72de841b1f8537d993b71b7e95 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/12/17 19:22:28 by chuhlig           #+#    #+#             */
-/*   Updated: 2024/12/17 19:22:36 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/14 14:41:39 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -26,6 +26,11 @@ char **env_to_strlst(t_env *env)
        cur = env;
        while (cur != NULL)
        {
+               if (ft_strchr(cur->name, '?'))
+               {
+                       cur = cur->next;
+                       continue;
+               }
                size++;
                cur = cur->next;
        }
@@ -36,6 +41,8 @@ char  **env_to_strlst(t_env *env)
        cur = env;
        while (i < size)
        {
+               if (ft_strchr(cur->name, '?'))
+                cur = cur->next;
                result[i] = get_var_assign(cur);
                cur = cur->next;
                i++;
@@ -55,4 +62,4 @@ static char   *get_var_assign(t_env *cur)
        result = ft_strjoin(left_side, cur->value);
        free(left_side);
        return (result);
-}
\ No newline at end of file
+}
index bd7f703d8192744f18e456b16b3c33ed849820be..1fe028c52a41687d5a5c3c9a10903c72d1e8e2fe 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/12/17 19:30:11 by chuhlig           #+#    #+#             */
-/*   Updated: 2024/12/17 19:31:54 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/14 18:06:17 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 static void    append_slice(char **dst, char *src, int start, int end);
 static void    append_var(char **dst, char *src, int *pos, t_env *env);
 
-enum           e_format_mode
-{
-       LITERAL = 1,
-       VARIABLE = 2,
-};
-
 char   *format_string(char *str, t_env *env)
 {
        char    *result;
        int             pos;
        int             start;
-       int             mode;
+       int             is_literal;
 
        pos = 0;
        start = 0;
-       mode = 0;
+       is_literal = 0;
        result = NULL;
        if (str == NULL)
                return (NULL);
@@ -42,14 +36,14 @@ char        *format_string(char *str, t_env *env)
                {
                        append_slice(&result, str, start, pos);
                        start = pos + 1;
-                       mode ^= LITERAL;
+                       is_literal = !is_literal;
                }
-               if (str[pos] == '"' && !(mode & LITERAL))
+               if (str[pos] == '"' && !is_literal)
                {
                        append_slice(&result, str, start, pos);
                        start = pos + 1;
                }
-               if (str[pos] == '$' && !(mode & LITERAL))
+               if (str[pos] == '$' && !is_literal)
                {
                        append_slice(&result, str, start, pos);
                        append_var(&result, str, &pos, env);
index a53760c8471d6fed9793a5e58b4c5c5c6f89878b..9e5d4eca30ff527104ecdb9ec09725aec6e1f664 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/22 17:14:03 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/12/17 19:26:42 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/14 15:22:40 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -22,5 +22,6 @@ int   main(int argc, char *argv[], char *envp[])
        if (init())
                return (1);
        getenvlst(&env, envp);
+       set_return_code(0, &env);
        repl("Minishell $ ", &env);
 }
index 7ff80a8adfdb12f7573c17131b287e767fff1a2a..4292197a8ae18af6fbef605863f53e972345ebba 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/24 16:07:04 by dkaiser           #+#    #+#             */
-/*   Updated: 2025/01/11 16:01:44 by chuhlig          ###   ########.fr       */
+/*   Updated: 2025/01/14 14:53:29 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -54,7 +54,7 @@ void  repl(const char *prompt, t_env **env)
                if (lines)
                {
                        print_ast(lines->content);
-                       eval(lines->content, env);
+                       set_return_code(eval(lines->content, env), env);
                }
                free(input);
        }