Imported from ../bash-3.0.tar.gz.
[platform/upstream/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-2003 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 "../bashintl.h"
41
42 #include "../shell.h"
43 #include "../jobs.h"
44
45 #include "common.h"
46 #include "builtext.h"   /* for jobs_builtin */
47
48 extern int last_command_exit_value;
49 extern int running_trap, trap_saved_exit_value;
50 extern int subshell_environment;
51 extern sh_builtin_func_t *this_shell_builtin;
52 extern sh_builtin_func_t *last_shell_builtin;
53
54 static int exit_or_logout __P((WORD_LIST *));
55 static int sourced_logout;
56
57 int
58 exit_builtin (list)
59      WORD_LIST *list;
60 {
61   if (interactive)
62     {
63       fprintf (stderr, login_shell ? "logout\n" : "exit\n");
64       fflush (stderr);
65     }
66
67   return (exit_or_logout (list));
68 }
69
70 $BUILTIN logout
71 $FUNCTION logout_builtin
72 $SHORT_DOC logout
73 Logout of a login shell.
74 $END
75
76 /* How to logout. */
77 int
78 logout_builtin (list)
79      WORD_LIST *list;
80 {
81   if (login_shell == 0 /* && interactive */)
82     {
83       builtin_error (_("not login shell: use `exit'"));
84       return (EXECUTION_FAILURE);
85     }
86   else
87     return (exit_or_logout (list));
88 }
89
90 static int
91 exit_or_logout (list)
92      WORD_LIST *list;
93 {
94   int exit_value;
95
96 #if defined (JOB_CONTROL)
97   int exit_immediate_okay;
98
99   exit_immediate_okay = (interactive  == 0 ||
100                          last_shell_builtin == exit_builtin ||
101                          last_shell_builtin == logout_builtin ||
102                          last_shell_builtin == jobs_builtin);
103
104   /* Check for stopped jobs if the user wants to. */
105   if (!exit_immediate_okay)
106     {
107       register int i;
108       for (i = 0; i < job_slots; i++)
109         if (jobs[i] && STOPPED (i))
110           {
111             fprintf (stderr, _("There are stopped jobs.\n"));
112
113             /* This is NOT superfluous because EOF can get here without
114                going through the command parser.  Set both last and this
115                so that either `exit', `logout', or ^D will work to exit
116                immediately if nothing intervenes. */
117             this_shell_builtin = last_shell_builtin = exit_builtin;
118             return (EXECUTION_FAILURE);
119           }
120     }
121 #endif /* JOB_CONTROL */
122
123   /* Get return value if present.  This means that you can type
124      `logout 5' to a shell, and it returns 5. */
125
126   /* If we're running the exit trap (running_trap == 1, since running_trap
127      gets set to SIG+1), and we don't have a argument given to `exit'
128      (list == 0), use the exit status we saved before running the trap
129      commands (trap_saved_exit_value). */
130   exit_value = (running_trap == 1 && list == 0) ? trap_saved_exit_value : get_exitstat (list);
131
132   bash_logout ();
133
134   last_command_exit_value = exit_value;
135
136   /* Exit the program. */
137   jump_to_top_level (EXITPROG);
138   /*NOTREACHED*/
139 }
140
141 void
142 bash_logout ()
143 {
144   /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
145   if (login_shell && sourced_logout++ == 0 && subshell_environment == 0)
146     {
147       maybe_execute_file ("~/.bash_logout", 1);
148 #ifdef SYS_BASH_LOGOUT
149       maybe_execute_file (SYS_BASH_LOGOUT, 1);
150 #endif
151     }
152 }