Add exit codes, make exit a "normal" built-in
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 27 Aug 2024 13:09:13 +0000 (15:09 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 27 Aug 2024 13:09:13 +0000 (15:09 +0200)
Makefile
inc/builtins.h
inc/minishell_structs.h
src/builtins/exit.c [new file with mode: 0644]
src/env.c
src/execution.c
src/main.c

index 71683a06ba46c40b96f4fe619d64deb76f3c2359..b100a973bfc0efec75ed28d3afa1939282bc5c66 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -40,6 +40,7 @@ SOURCES :=    main.c                          \
                        builtins/env.c          \
                        builtins/export.c       \
                        builtins/unset.c        \
+                       builtins/exit.c         \
 
 SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
 
index 0afbd1d2d4ad6c45325b3459372a7deafcd3d374..30ee6b6b8305736b46eabac91038899e6736fa8f 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/26 11:39:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/26 11:51:32 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 15:02:28 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -21,6 +21,7 @@ int           pwd(void);
 int            ft_env(int argc, t_execution_env *env);
 int            export(int argc, char **argv, t_execution_env *env);
 int            unset(int argc, char **argv, t_execution_env *env);
+int            execute_exit(int argc, char **argv, t_execution_env *env);
 
 void   unset_single(const char *var_name, t_execution_env *env);
 
index 4b8b98071dfcf2ade53e372408c6b2f362402dc2..20b2085c07351f3880a8a231e03a5c3f134c11d6 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/26 09:08:46 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/27 13:37:07 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 15:03:52 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -31,6 +31,8 @@ typedef struct s_execution_env
        int             stdin_fd;
        int             stdout_fd;
        int             ret_val;
+       int             may_exit;
+       int             exit;
        int             last_was_builtin;
        int             last_builtin_ret_val;
        t_vars  *vars;
diff --git a/src/builtins/exit.c b/src/builtins/exit.c
new file mode 100644 (file)
index 0000000..7a978a2
--- /dev/null
@@ -0,0 +1,50 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   exit.c                                             :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/08/27 14:49:17 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/08/27 15:05:25 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "builtins.h"
+#include "minishell_structs.h"
+#include <unistd.h>
+
+int    is_numeric(const char *str)
+{
+       size_t  i;
+
+       i = 0;
+       if (str[0] == '+' || str[0] == '-')
+               ++i;
+       while (str[i])
+       {
+               if (!ft_isdigit(str[i]))
+                       return (0);
+               ++i;
+       }
+       return (1);
+}
+
+int    execute_exit(int argc, char **argv, t_execution_env *env)
+{
+       if (env->may_exit)
+               env->exit = 1;
+       if (argc == 1)
+               return (0);
+       if (argc > 2)
+       {
+               ft_dprintf(STDERR_FILENO, "exit: too many arguments\n");
+               return (1);
+       }
+       if (!is_numeric(argv[1]))
+       {
+               ft_dprintf(STDERR_FILENO, "exit: %s: numeric argument required\n", argv[1]);
+               return (2);
+       }
+       return (ft_atoi(argv[1]));
+}
index 936d7ba31d3e2defb7f5d2d1a3ccba98c0222d83..dca713f44e93d5bb6676c1628be3661947ba9df9 100644 (file)
--- a/src/env.c
+++ b/src/env.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 21:24:52 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/27 10:34:14 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 15:02:02 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -21,6 +21,7 @@ int   init_env(t_execution_env *env, char **envp)
        env->stdin_fd = STDIN_FILENO;
        env->stdout_fd = STDOUT_FILENO;
        env->ret_val = 0;
+       env->exit = 0;
        env->vars = malloc(sizeof(t_vars));
        if (!env->vars)
                return (1);
index 94df96df3d4984ec210b861fff05f9ce7617a226..a60991ea3f0d23858e66781e22d1829bd75d1e89 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/27 11:37:08 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 15:04:43 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -728,6 +728,8 @@ int is_builtin(const char *str)
                return (1);
        if (!ft_strcmp(str, "unset"))
                return (1);
+       if (!ft_strcmp(str, "exit"))
+               return (1);
        return (0);
 }
 
@@ -795,6 +797,8 @@ int ex_builtin(char **fields, __attribute__((unused)) t_vec *assignments, __attr
                env->last_builtin_ret_val = export(count_fields(fields), fields, env);
        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);
        else
                return (1);
        env->last_was_builtin = 1;
@@ -985,8 +989,10 @@ int        ex_pipeline(t_parse_tree_node *pipeline, t_execution_env *env, int depth)
 
        if (pipeline->children.size == 1)
        {
+               env->may_exit = (depth == 0);
                if (ex_command(ft_vec_access(&pipeline->children, 0), env))
                        return (1);
+               env->may_exit = 0;
        }
        else
        {
index 8d7e2dbf654a5b24d97aa4e6f97784748820bd4b..72319287e158fbaccf1a5d06e100b105910a3532 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/26 13:11:47 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/27 14:31:19 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 15:08:06 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -31,6 +31,7 @@ int   main(int argc, char **argv)
 
 int    main(int argc, __attribute__((unused)) char **argv, char **envp)
 {
+       int                     res;
        char            *line;
        t_execution_env env;
 
@@ -44,18 +45,21 @@ int main(int argc, __attribute__((unused)) char **argv, char **envp)
                clean_env(&env);
                return (2);
        }
-       while (1)
+       while (env.exit == 0)
        {
                line = get_line();
-               if (!line || !ft_strncmp(line, "exit", 4))
+               if (!line)
+               {
+                       env.ret_val = 0;
                        break ;
+               }
                handle_input(&line, &env);
                free(line);
        }
+       res = env.ret_val;
        clean_env(&env);
        parse(NULL, NULL);
        terminate_input();
-       free(line);
        ft_printf("exit\n");
-       return (0);
+       return (res);
 }