| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | This file is exit.def, from which is created exit.c. | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | It implements the builtins "exit", and "logout" in Bash. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | Copyright (C) 1987-2009 Free Software Foundation, Inc. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | This file is part of GNU Bash, the Bourne Again SHell. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | Bash is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  | the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | (at your option) any later version. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | Bash is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | along with Bash.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | $PRODUCES exit.c | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $BUILTIN exit | 
					
						
							|  |  |  | $FUNCTION exit_builtin | 
					
						
							|  |  |  | $SHORT_DOC exit [n] | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | Exit the shell. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Exits the shell with a status of N.  If N is omitted, the exit status | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | is that of the last command executed. | 
					
						
							|  |  |  | $END | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include <config.h> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | #include "../bashtypes.h" | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include <stdio.h> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_UNISTD_H) | 
					
						
							|  |  |  | #  include <unistd.h> | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #include "../bashintl.h" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #include "../shell.h" | 
					
						
							|  |  |  | #include "../jobs.h" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include "common.h" | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #include "builtext.h"	/* for jobs_builtin */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | extern int check_jobs_at_exit; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | extern int last_command_exit_value; | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | extern int running_trap, trap_saved_exit_value; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | extern int subshell_environment; | 
					
						
							|  |  |  | extern sh_builtin_func_t *this_shell_builtin; | 
					
						
							|  |  |  | extern sh_builtin_func_t *last_shell_builtin; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | static int exit_or_logout __P((WORD_LIST *)); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | static int sourced_logout; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | exit_builtin (list) | 
					
						
							|  |  |  |      WORD_LIST *list; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |   CHECK_HELPOPT (list); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |   if (interactive) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       fprintf (stderr, login_shell ? _("logout\n") : "exit\n"); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |       fflush (stderr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (exit_or_logout (list)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $BUILTIN logout | 
					
						
							|  |  |  | $FUNCTION logout_builtin | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | $SHORT_DOC logout [n] | 
					
						
							|  |  |  | Exit a login shell. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Exits a login shell with exit status N.  Returns an error if not executed | 
					
						
							|  |  |  | in a login shell. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | $END | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* How to logout. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | logout_builtin (list) | 
					
						
							|  |  |  |      WORD_LIST *list; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-15 16:59:08 -04:00
										 |  |  |   CHECK_HELPOPT (list); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   if (login_shell == 0 /* && interactive */) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       builtin_error (_("not login shell: use `exit'")); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |       return (EXECUTION_FAILURE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     return (exit_or_logout (list)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | exit_or_logout (list) | 
					
						
							|  |  |  |      WORD_LIST *list; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int exit_value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (JOB_CONTROL) | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   int exit_immediate_okay, stopmsg; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   exit_immediate_okay = (interactive  == 0 || | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 			 last_shell_builtin == exit_builtin || | 
					
						
							|  |  |  | 			 last_shell_builtin == logout_builtin || | 
					
						
							|  |  |  | 			 last_shell_builtin == jobs_builtin); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Check for stopped jobs if the user wants to. */ | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   if (exit_immediate_okay == 0) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       register int i; | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |       for (i = stopmsg = 0; i < js.j_jobslots; i++) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	if (jobs[i] && STOPPED (i)) | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	  stopmsg = JSTOPPED; | 
					
						
							| 
									
										
										
										
											2011-11-21 20:49:12 -05:00
										 |  |  | 	else if (check_jobs_at_exit && stopmsg == 0 && jobs[i] && RUNNING (i)) | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 	  stopmsg = JRUNNING; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (stopmsg == JSTOPPED) | 
					
						
							|  |  |  | 	fprintf (stderr, _("There are stopped jobs.\n")); | 
					
						
							|  |  |  |       else if (stopmsg == JRUNNING) | 
					
						
							|  |  |  | 	fprintf (stderr, _("There are running jobs.\n")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (stopmsg && check_jobs_at_exit) | 
					
						
							|  |  |  |         list_all_jobs (JLIST_STANDARD); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (stopmsg) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  /* This is NOT superfluous because EOF can get here without | 
					
						
							|  |  |  | 	     going through the command parser.  Set both last and this | 
					
						
							|  |  |  | 	     so that either `exit', `logout', or ^D will work to exit | 
					
						
							|  |  |  | 	     immediately if nothing intervenes. */ | 
					
						
							|  |  |  | 	  this_shell_builtin = last_shell_builtin = exit_builtin; | 
					
						
							|  |  |  | 	  return (EXECUTION_FAILURE); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | #endif /* JOB_CONTROL */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Get return value if present.  This means that you can type | 
					
						
							|  |  |  |      `logout 5' to a shell, and it returns 5. */ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |   /* If we're running the exit trap (running_trap == 1, since running_trap | 
					
						
							|  |  |  |      gets set to SIG+1), and we don't have a argument given to `exit' | 
					
						
							|  |  |  |      (list == 0), use the exit status we saved before running the trap | 
					
						
							|  |  |  |      commands (trap_saved_exit_value). */ | 
					
						
							|  |  |  |   exit_value = (running_trap == 1 && list == 0) ? trap_saved_exit_value : get_exitstat (list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bash_logout (); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   last_command_exit_value = exit_value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Exit the program. */ | 
					
						
							|  |  |  |   jump_to_top_level (EXITPROG); | 
					
						
							|  |  |  |   /*NOTREACHED*/ | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | void | 
					
						
							|  |  |  | bash_logout () | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |   /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   if (login_shell && sourced_logout++ == 0 && subshell_environment == 0) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       maybe_execute_file ("~/.bash_logout", 1); | 
					
						
							|  |  |  | #ifdef SYS_BASH_LOGOUT | 
					
						
							|  |  |  |       maybe_execute_file (SYS_BASH_LOGOUT, 1); | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } |