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

Copyright (C) 1987, 1989, 1991 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
version.

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.

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

$BUILTIN exit
$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.
$END

#include <config.h>

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

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

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

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

extern int interactive, login_shell;
extern int last_command_exit_value;
extern Function *this_shell_builtin;
extern Function *last_shell_builtin;

#if defined (__MSDOS__)
int subshell_exit_value = 0;
int subshell_exit_builtin = 0;
int subshell_skip_commands = 0;
#endif

static int exit_or_logout ();
static int sourced_logout;

int
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.
$END

/* How to logout. */
int
logout_builtin (list)
     WORD_LIST *list;
{
  if (login_shell == 0 /* && interactive */)
    {
      builtin_error ("not login shell: use `exit'");
      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)
  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 < job_slots; 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. */
  exit_value = list ? get_numeric_arg (list, 1) : last_command_exit_value;

  /* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
  if (login_shell && sourced_logout++ == 0)
    {
#if defined (__MSDOS__)
      if (maybe_execute_file ("~/.bash_logout", 1) == 0)
	  maybe_execute_file ("~/_blogout", 1);
#else
      maybe_execute_file ("~/.bash_logout", 1);
#endif
#ifdef SYS_BASH_LOGOUT
      maybe_execute_file (SYS_BASH_LOGOUT, 1);
#endif
    }

  last_command_exit_value = exit_value;

  /* Exit the program. */
#if defined (__MSDOS__)
  if (subshell_exit_builtin)
    {
      subshell_exit_value = exit_value;
      subshell_exit_builtin = 0;
      subshell_skip_commands = 1;
      return (EXECUTION_SUCCESS);
    }
#endif
  jump_to_top_level (EXITPROG);
  /*NOTREACHED*/
}
