exit.def   [plain text]

This file is exit.def, from which is created exit.c.
It implements the builtins "exit", and "logout" in Bash.

Copyright (C) 1987-2005 Free Software Foundation, Inc.

This file is part of GNU Bash, the Bourne Again SHell.

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 2, or (at your option) any later

Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
for more details.

You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING.  If not, write to the Free Software
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.

$PRODUCES exit.c

$FUNCTION exit_builtin
$SHORT_DOC exit [n]
Exit the shell with a status of N.  If N is omitted, the exit status
is that of the last command executed.

#include <config.h>

#include "../bashtypes.h"
#include <stdio.h>

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>

#include "../bashintl.h"

#include "../shell.h"
#include "../jobs.h"

#include "common.h"
#include "builtext.h"	/* for jobs_builtin */

extern int last_command_exit_value;
extern int running_trap, trap_saved_exit_value;
extern int subshell_environment;
extern sh_builtin_func_t *this_shell_builtin;
extern sh_builtin_func_t *last_shell_builtin;

static int exit_or_logout __P((WORD_LIST *));
static int sourced_logout;

exit_builtin (list)
     WORD_LIST *list;
  if (interactive)
      fprintf (stderr, login_shell ? "logout\n" : "exit\n");
      fflush (stderr);

  return (exit_or_logout (list));

$BUILTIN logout
$FUNCTION logout_builtin
$SHORT_DOC logout
Logout of a login shell.

/* How to logout. */
logout_builtin (list)
     WORD_LIST *list;
  if (login_shell == 0 /* && interactive */)
      builtin_error (_("not login shell: use `exit'"));
      return (EXECUTION_FAILURE);
    return (exit_or_logout (list));

static int
exit_or_logout (list)
     WORD_LIST *list;
  int exit_value;

#if defined (JOB_CONTROL)
  int exit_immediate_okay;

  exit_immediate_okay = (interactive  == 0 ||
			 last_shell_builtin == exit_builtin ||
			 last_shell_builtin == logout_builtin ||
			 last_shell_builtin == jobs_builtin);

  /* Check for stopped jobs if the user wants to. */
  if (!exit_immediate_okay)
      register int i;
      for (i = 0; i < js.j_jobslots; i++)
	if (jobs[i] && STOPPED (i))
	    fprintf (stderr, _("There are stopped jobs.\n"));

	    /* 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);
#endif /* JOB_CONTROL */

  /* Get return value if present.  This means that you can type
     `logout 5' to a shell, and it returns 5. */

  /* 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);

bash_logout ()
  /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
  if (login_shell && sourced_logout++ == 0 && subshell_environment == 0)
      maybe_execute_file ("~/.bash_logout", 1);
      maybe_execute_file (SYS_BASH_LOGOUT, 1);