i-bash/builtins/exit.def

139 lines
3.4 KiB
Modula-2
Raw Normal View History

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
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 1, 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, 675 Mass Ave, Cambridge, MA 02139, 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
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
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 */
extern int interactive, login_shell;
extern int last_command_exit_value;
1997-06-05 14:59:13 +00:00
extern Function *this_shell_builtin;
extern Function *last_shell_builtin;
1996-08-26 18:22:31 +00:00
static int exit_or_logout ();
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;
{
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;
{
1996-12-23 17:02:34 +00:00
if (login_shell == 0 && interactive)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +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)
int exit_immediate_okay;
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. */
if (!exit_immediate_okay)
{
register int i;
for (i = 0; i < job_slots; i++)
1996-12-23 17:02:34 +00:00
if (jobs[i] && STOPPED (i))
1996-08-26 18:22:31 +00:00
{
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. */
1997-06-05 14:59:13 +00:00
exit_value = list ? get_numeric_arg (list, 1) : last_command_exit_value;
1996-08-26 18:22:31 +00:00
/* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
if (login_shell && sourced_logout++ == 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
last_command_exit_value = exit_value;
/* Exit the program. */
1996-12-23 17:02:34 +00:00
jump_to_top_level (EXITPROG);
/*NOTREACHED*/
1996-08-26 18:22:31 +00:00
}