Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is suspend.def, from which is created suspend.c. |
| 2 | It implements the builtin "suspend" in Bash. |
| 3 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 4 | Copyright (C) 1987-2009 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES suspend.c |
| 22 | |
| 23 | $BUILTIN suspend |
| 24 | $DEPENDS_ON JOB_CONTROL |
| 25 | $FUNCTION suspend_builtin |
| 26 | $SHORT_DOC suspend [-f] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 27 | Suspend shell execution. |
| 28 | |
| 29 | Suspend the execution of this shell until it receives a SIGCONT signal. |
| 30 | Unless forced, login shells cannot be suspended. |
| 31 | |
| 32 | Options: |
| 33 | -f force the suspend, even if the shell is a login shell |
| 34 | |
| 35 | Exit Status: |
| 36 | Returns success unless job control is not enabled or an error occurs. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 37 | $END |
| 38 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 39 | #include <config.h> |
| 40 | |
| 41 | #if defined (JOB_CONTROL) |
| 42 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 43 | # ifdef _MINIX |
| 44 | # include <sys/types.h> |
| 45 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 46 | # include <unistd.h> |
| 47 | #endif |
| 48 | |
| 49 | #include "../bashtypes.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 50 | #include <signal.h> |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 51 | #include "../bashintl.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 52 | #include "../shell.h" |
| 53 | #include "../jobs.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 54 | #include "common.h" |
| 55 | #include "bashgetopt.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 56 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 57 | static sighandler suspend_continue __P((int)); |
| 58 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 59 | static SigHandler *old_cont; |
| 60 | #if 0 |
| 61 | static SigHandler *old_stop; |
| 62 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 63 | |
| 64 | /* Continue handler. */ |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 65 | static sighandler |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 66 | suspend_continue (sig) |
| 67 | int sig; |
| 68 | { |
| 69 | set_signal_handler (SIGCONT, old_cont); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 70 | #if 0 |
| 71 | set_signal_handler (SIGSTOP, old_stop); |
| 72 | #endif |
| 73 | SIGRETURN (0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* Suspending the shell. If -f is the arg, then do the suspend |
| 77 | no matter what. Otherwise, complain if a login shell. */ |
| 78 | int |
| 79 | suspend_builtin (list) |
| 80 | WORD_LIST *list; |
| 81 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 82 | int opt, force; |
| 83 | |
| 84 | reset_internal_getopt (); |
| 85 | force = 0; |
| 86 | while ((opt = internal_getopt (list, "f")) != -1) |
| 87 | switch (opt) |
| 88 | { |
| 89 | case 'f': |
| 90 | force++; |
| 91 | break; |
| 92 | default: |
| 93 | builtin_usage (); |
| 94 | return (EX_USAGE); |
| 95 | } |
| 96 | |
| 97 | list = loptend; |
| 98 | |
| 99 | if (job_control == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 100 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 101 | sh_nojobs (_("cannot suspend")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 102 | return (EXECUTION_FAILURE); |
| 103 | } |
| 104 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 105 | if (force == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 106 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 107 | no_args (list); |
| 108 | |
| 109 | if (login_shell) |
| 110 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 111 | builtin_error (_("cannot suspend a login shell")); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 112 | return (EXECUTION_FAILURE); |
| 113 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 116 | /* XXX - should we put ourselves back into the original pgrp now? If so, |
| 117 | call end_job_control() here and do the right thing in suspend_continue |
| 118 | (that is, call restart_job_control()). */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 119 | old_cont = (SigHandler *)set_signal_handler (SIGCONT, suspend_continue); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 120 | #if 0 |
| 121 | old_stop = (SigHandler *)set_signal_handler (SIGSTOP, SIG_DFL); |
| 122 | #endif |
| 123 | killpg (shell_pgrp, SIGSTOP); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 124 | return (EXECUTION_SUCCESS); |
| 125 | } |
| 126 | |
| 127 | #endif /* JOB_CONTROL */ |