25a3b6332eae67bcfce2921e22770731a9344dfc
[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, 1989, 1991 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, 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 <stdio.h>
32 #include <sys/types.h>
33 #include "../shell.h"
34 #include "../jobs.h"
35
36 #include "builtext.h"   /* for jobs_builtin */
37
38 extern int interactive, login_shell;
39 extern int last_command_exit_value;
40
41 static int exit_or_logout ();
42 static int sourced_logout = 0;
43
44 int
45 exit_builtin (list)
46      WORD_LIST *list;
47 {
48   if (interactive)
49     {
50       fprintf (stderr, login_shell ? "logout\n" : "exit\n");
51       fflush (stderr);
52     }
53
54   return (exit_or_logout (list));
55 }
56
57 $BUILTIN logout
58 $FUNCTION logout_builtin
59 $SHORT_DOC logout
60 Logout of a login shell.
61 $END
62
63 /* How to logout. */
64 int
65 logout_builtin (list)
66      WORD_LIST *list;
67 {
68   if (!login_shell && interactive)
69     {
70       builtin_error ("Not login shell: use `exit'");
71       return (EXECUTION_FAILURE);
72     }
73   else
74     return (exit_or_logout (list));
75 }
76
77 /* Clean up work for exiting or logging out. */
78 Function *last_shell_builtin = (Function *)NULL;
79 Function *this_shell_builtin = (Function *)NULL;
80
81 static int
82 exit_or_logout (list)
83      WORD_LIST *list;
84 {
85   int exit_value;
86
87 #if defined (JOB_CONTROL)
88   int exit_immediate_okay;
89
90   exit_immediate_okay = (!interactive ||
91                          last_shell_builtin == exit_builtin ||
92                          last_shell_builtin == logout_builtin ||
93                          last_shell_builtin == jobs_builtin);
94
95   /* Check for stopped jobs if the user wants to. */
96   if (!exit_immediate_okay)
97     {
98       register int i;
99       for (i = 0; i < job_slots; i++)
100         if (jobs[i] && (jobs[i]->state == JSTOPPED))
101           {
102             fprintf (stderr, "There are stopped jobs.\n");
103
104             /* This is NOT superfluous because EOF can get here without
105                going through the command parser.  Set both last and this
106                so that either `exit', `logout', or ^D will work to exit
107                immediately if nothing intervenes. */
108             this_shell_builtin = last_shell_builtin = exit_builtin;
109             return (EXECUTION_FAILURE);
110           }
111     }
112 #endif /* JOB_CONTROL */
113
114   /* Get return value if present.  This means that you can type
115      `logout 5' to a shell, and it returns 5. */
116   if (list)
117     exit_value = get_numeric_arg (list);
118   else
119     exit_value = last_command_exit_value;
120
121   /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
122   if (login_shell && sourced_logout++ == 0)
123     maybe_execute_file ("~/.bash_logout", 1);
124
125   last_command_exit_value = exit_value;
126
127   /* Exit the program. */
128   longjmp (top_level, EXITPROG);
129 }