init version.
[bush.git] / builtins / exit.def
blob0f62dad2a701527d352c1f0de94f9c6b582842b5
1 This file is exit.def, from which is created exit.c.
2 It implements the builtins "exit", and "logout" in Bush.
4 Copyright (C) 1987-2020 Free Software Foundation, Inc.
6 This file is part of GNU Bush, the Bourne Again SHell.
8 Bush is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bush is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bush. If not, see <http://www.gnu.org/licenses/>.
21 $PRODUCES exit.c
23 $BUILTIN exit
24 $FUNCTION exit_builtin
25 $SHORT_DOC exit [n]
26 Exit the shell.
28 Exits the shell with a status of N. If N is omitted, the exit status
29 is that of the last command executed.
30 $END
32 #include <config.h>
34 #include "../src/bushtypes.h"
35 #include <stdio.h>
37 #if defined (HAVE_UNISTD_H)
38 # include <unistd.h>
39 #endif
41 #include "../src/bushintl.h"
43 #include "../src/shell.h"
44 #include "../src/execute_cmd.h"
45 #include "../src/jobs.h"
46 #include "../src/trap.h"
48 #include "common.h"
49 #include "builtext.h" /* for jobs_builtin */
51 extern int check_jobs_at_exit;
53 static int exit_or_logout PARAMS((WORD_LIST *));
54 static int sourced_logout;
56 int
57 exit_builtin (list)
58 WORD_LIST *list;
60 CHECK_HELPOPT (list);
62 if (interactive)
64 fprintf (stderr, login_shell ? _("logout\n") : "exit\n");
65 fflush (stderr);
68 return (exit_or_logout (list));
71 $BUILTIN logout
72 $FUNCTION logout_builtin
73 $SHORT_DOC logout [n]
74 Exit a login shell.
76 Exits a login shell with exit status N. Returns an error if not executed
77 in a login shell.
78 $END
80 /* How to logout. */
81 int
82 logout_builtin (list)
83 WORD_LIST *list;
85 CHECK_HELPOPT (list);
87 if (login_shell == 0 /* && interactive */)
89 builtin_error (_("not login shell: use `exit'"));
90 return (EXECUTION_FAILURE);
92 else
93 return (exit_or_logout (list));
96 static int
97 exit_or_logout (list)
98 WORD_LIST *list;
100 int exit_value;
102 #if defined (JOB_CONTROL)
103 int exit_immediate_okay, stopmsg;
105 exit_immediate_okay = (interactive == 0 ||
106 last_shell_builtin == exit_builtin ||
107 last_shell_builtin == logout_builtin ||
108 last_shell_builtin == jobs_builtin);
110 /* Check for stopped jobs if the user wants to. */
111 if (exit_immediate_okay == 0)
113 register int i;
114 for (i = stopmsg = 0; i < js.j_jobslots; i++)
115 if (jobs[i] && STOPPED (i))
116 stopmsg = JSTOPPED;
117 else if (check_jobs_at_exit && stopmsg == 0 && jobs[i] && RUNNING (i))
118 stopmsg = JRUNNING;
120 if (stopmsg == JSTOPPED)
121 fprintf (stderr, _("There are stopped jobs.\n"));
122 else if (stopmsg == JRUNNING)
123 fprintf (stderr, _("There are running jobs.\n"));
125 if (stopmsg && check_jobs_at_exit)
126 list_all_jobs (JLIST_STANDARD);
128 if (stopmsg)
130 /* This is NOT superfluous because EOF can get here without
131 going through the command parser. Set both last and this
132 so that either `exit', `logout', or ^D will work to exit
133 immediately if nothing intervenes. */
134 this_shell_builtin = last_shell_builtin = exit_builtin;
135 return (EXECUTION_FAILURE);
138 #endif /* JOB_CONTROL */
140 /* Get return value if present. This means that you can type
141 `logout 5' to a shell, and it returns 5. */
143 /* If we're running the exit trap (running_trap == 1, since running_trap
144 gets set to SIG+1), and we don't have a argument given to `exit'
145 (list == 0), use the exit status we saved before running the trap
146 commands (trap_saved_exit_value). */
147 exit_value = (running_trap == 1 && list == 0) ? trap_saved_exit_value : get_exitstat (list);
149 bush_logout ();
151 last_command_exit_value = exit_value;
153 /* Exit the program. */
154 jump_to_top_level (EXITPROG);
155 /*NOTREACHED*/
158 void
159 bush_logout ()
161 /* Run our `~/.bush_logout' file if it exists, and this is a login shell. */
162 if (login_shell && sourced_logout++ == 0 && subshell_environment == 0)
164 maybe_execute_file ("~/.bush_logout", 1);
165 #ifdef SYS_BUSH_LOGOUT
166 maybe_execute_file (SYS_BUSH_LOGOUT, 1);
167 #endif