]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/exit.def
Imported from ../bash-2.05b.tar.gz.
[thirdparty/bash.git] / builtins / exit.def
1 This file is exit.def, from which is created exit.c.
2 It implements the builtins "exit", and "logout" in Bash.
3
4 Copyright (C) 1987-2002 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21
22 $PRODUCES exit.c
23
24 $BUILTIN exit
25 $FUNCTION exit_builtin
26 $SHORT_DOC exit [n]
27 Exit the shell with a status of N. If N is omitted, the exit status
28 is that of the last command executed.
29 $END
30
31 #include <config.h>
32
33 #include "../bashtypes.h"
34 #include <stdio.h>
35
36 #if defined (HAVE_UNISTD_H)
37 # include <unistd.h>
38 #endif
39
40 #include "../shell.h"
41 #include "../jobs.h"
42
43 #include "common.h"
44 #include "builtext.h" /* for jobs_builtin */
45
46 extern int last_command_exit_value;
47 extern int subshell_environment;
48 extern sh_builtin_func_t *this_shell_builtin;
49 extern sh_builtin_func_t *last_shell_builtin;
50
51 static int exit_or_logout __P((WORD_LIST *));
52 static int sourced_logout;
53
54 int
55 exit_builtin (list)
56 WORD_LIST *list;
57 {
58 if (interactive)
59 {
60 fprintf (stderr, login_shell ? "logout\n" : "exit\n");
61 fflush (stderr);
62 }
63
64 return (exit_or_logout (list));
65 }
66
67 $BUILTIN logout
68 $FUNCTION logout_builtin
69 $SHORT_DOC logout
70 Logout of a login shell.
71 $END
72
73 /* How to logout. */
74 int
75 logout_builtin (list)
76 WORD_LIST *list;
77 {
78 if (login_shell == 0 /* && interactive */)
79 {
80 builtin_error ("not login shell: use `exit'");
81 return (EXECUTION_FAILURE);
82 }
83 else
84 return (exit_or_logout (list));
85 }
86
87 static int
88 exit_or_logout (list)
89 WORD_LIST *list;
90 {
91 int exit_value;
92
93 #if defined (JOB_CONTROL)
94 int exit_immediate_okay;
95
96 exit_immediate_okay = (interactive == 0 ||
97 last_shell_builtin == exit_builtin ||
98 last_shell_builtin == logout_builtin ||
99 last_shell_builtin == jobs_builtin);
100
101 /* Check for stopped jobs if the user wants to. */
102 if (!exit_immediate_okay)
103 {
104 register int i;
105 for (i = 0; i < job_slots; i++)
106 if (jobs[i] && STOPPED (i))
107 {
108 fprintf (stderr, "There are stopped jobs.\n");
109
110 /* This is NOT superfluous because EOF can get here without
111 going through the command parser. Set both last and this
112 so that either `exit', `logout', or ^D will work to exit
113 immediately if nothing intervenes. */
114 this_shell_builtin = last_shell_builtin = exit_builtin;
115 return (EXECUTION_FAILURE);
116 }
117 }
118 #endif /* JOB_CONTROL */
119
120 /* Get return value if present. This means that you can type
121 `logout 5' to a shell, and it returns 5. */
122
123 exit_value = get_exitstat (list);
124
125 /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
126 if (login_shell && sourced_logout++ == 0 && subshell_environment == 0)
127 {
128 maybe_execute_file ("~/.bash_logout", 1);
129 #ifdef SYS_BASH_LOGOUT
130 maybe_execute_file (SYS_BASH_LOGOUT, 1);
131 #endif
132 }
133
134 last_command_exit_value = exit_value;
135
136 /* Exit the program. */
137 jump_to_top_level (EXITPROG);
138 /*NOTREACHED*/
139 }