Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is exit.def, from which is created exit.c. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2 | It implements the builtins "exit", and "logout" in Bash. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4 | Copyright (C) 1987-2009 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash 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. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES exit.c |
| 22 | |
| 23 | $BUILTIN exit |
| 24 | $FUNCTION exit_builtin |
| 25 | $SHORT_DOC exit [n] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | Exit the shell. |
| 27 | |
| 28 | Exits the shell with a status of N. If N is omitted, the exit status |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 29 | is that of the last command executed. |
| 30 | $END |
| 31 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 32 | #include <config.h> |
| 33 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 34 | #include "../bashtypes.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | |
| 37 | #if defined (HAVE_UNISTD_H) |
| 38 | # include <unistd.h> |
| 39 | #endif |
| 40 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 41 | #include "../bashintl.h" |
| 42 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 43 | #include "../shell.h" |
| 44 | #include "../jobs.h" |
| 45 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 46 | #include "common.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 47 | #include "builtext.h" /* for jobs_builtin */ |
| 48 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 49 | extern int check_jobs_at_exit; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 50 | extern int last_command_exit_value; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 51 | extern int running_trap, trap_saved_exit_value; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 52 | extern int subshell_environment; |
| 53 | extern sh_builtin_func_t *this_shell_builtin; |
| 54 | extern sh_builtin_func_t *last_shell_builtin; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 55 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 56 | static int exit_or_logout __P((WORD_LIST *)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 57 | static int sourced_logout; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 58 | |
| 59 | int |
| 60 | exit_builtin (list) |
| 61 | WORD_LIST *list; |
| 62 | { |
| 63 | if (interactive) |
| 64 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 65 | fprintf (stderr, login_shell ? _("logout\n") : "exit\n"); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 66 | fflush (stderr); |
| 67 | } |
| 68 | |
| 69 | return (exit_or_logout (list)); |
| 70 | } |
| 71 | |
| 72 | $BUILTIN logout |
| 73 | $FUNCTION logout_builtin |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 74 | $SHORT_DOC logout [n] |
| 75 | Exit a login shell. |
| 76 | |
| 77 | Exits a login shell with exit status N. Returns an error if not executed |
| 78 | in a login shell. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 79 | $END |
| 80 | |
| 81 | /* How to logout. */ |
| 82 | int |
| 83 | logout_builtin (list) |
| 84 | WORD_LIST *list; |
| 85 | { |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 86 | if (login_shell == 0 /* && interactive */) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 87 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 88 | builtin_error (_("not login shell: use `exit'")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 89 | return (EXECUTION_FAILURE); |
| 90 | } |
| 91 | else |
| 92 | return (exit_or_logout (list)); |
| 93 | } |
| 94 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 95 | static int |
| 96 | exit_or_logout (list) |
| 97 | WORD_LIST *list; |
| 98 | { |
| 99 | int exit_value; |
| 100 | |
| 101 | #if defined (JOB_CONTROL) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 102 | int exit_immediate_okay, stopmsg; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 103 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 104 | exit_immediate_okay = (interactive == 0 || |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 105 | last_shell_builtin == exit_builtin || |
| 106 | last_shell_builtin == logout_builtin || |
| 107 | last_shell_builtin == jobs_builtin); |
| 108 | |
| 109 | /* Check for stopped jobs if the user wants to. */ |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 110 | if (exit_immediate_okay == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 111 | { |
| 112 | register int i; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 113 | for (i = stopmsg = 0; i < js.j_jobslots; i++) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 114 | if (jobs[i] && STOPPED (i)) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 115 | stopmsg = JSTOPPED; |
Chet Ramey | 89a9286 | 2011-11-21 20:49:12 -0500 | [diff] [blame] | 116 | else if (check_jobs_at_exit && stopmsg == 0 && jobs[i] && RUNNING (i)) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 117 | stopmsg = JRUNNING; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 118 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 119 | if (stopmsg == JSTOPPED) |
| 120 | fprintf (stderr, _("There are stopped jobs.\n")); |
| 121 | else if (stopmsg == JRUNNING) |
| 122 | fprintf (stderr, _("There are running jobs.\n")); |
| 123 | |
| 124 | if (stopmsg && check_jobs_at_exit) |
| 125 | list_all_jobs (JLIST_STANDARD); |
| 126 | |
| 127 | if (stopmsg) |
| 128 | { |
| 129 | /* This is NOT superfluous because EOF can get here without |
| 130 | going through the command parser. Set both last and this |
| 131 | so that either `exit', `logout', or ^D will work to exit |
| 132 | immediately if nothing intervenes. */ |
| 133 | this_shell_builtin = last_shell_builtin = exit_builtin; |
| 134 | return (EXECUTION_FAILURE); |
| 135 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 136 | } |
| 137 | #endif /* JOB_CONTROL */ |
| 138 | |
| 139 | /* Get return value if present. This means that you can type |
| 140 | `logout 5' to a shell, and it returns 5. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 141 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 142 | /* If we're running the exit trap (running_trap == 1, since running_trap |
| 143 | gets set to SIG+1), and we don't have a argument given to `exit' |
| 144 | (list == 0), use the exit status we saved before running the trap |
| 145 | commands (trap_saved_exit_value). */ |
| 146 | exit_value = (running_trap == 1 && list == 0) ? trap_saved_exit_value : get_exitstat (list); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 147 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 148 | bash_logout (); |
| 149 | |
| 150 | last_command_exit_value = exit_value; |
| 151 | |
| 152 | /* Exit the program. */ |
| 153 | jump_to_top_level (EXITPROG); |
| 154 | /*NOTREACHED*/ |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | bash_logout () |
| 159 | { |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 160 | /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 161 | if (login_shell && sourced_logout++ == 0 && subshell_environment == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 162 | { |
| 163 | maybe_execute_file ("~/.bash_logout", 1); |
| 164 | #ifdef SYS_BASH_LOGOUT |
| 165 | maybe_execute_file (SYS_BASH_LOGOUT, 1); |
| 166 | #endif |
| 167 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 168 | } |