Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is fg_bg.def, from which is created fg_bg.c. |
| 2 | It implements the builtins "bg" and "fg" 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 fg_bg.c |
| 22 | |
| 23 | $BUILTIN fg |
| 24 | $FUNCTION fg_builtin |
| 25 | $DEPENDS_ON JOB_CONTROL |
| 26 | $SHORT_DOC fg [job_spec] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 27 | Move job to the foreground. |
| 28 | |
| 29 | Place the job identified by JOB_SPEC in the foreground, making it the |
| 30 | current job. If JOB_SPEC is not present, the shell's notion of the |
| 31 | current job is used. |
| 32 | |
| 33 | Exit Status: |
| 34 | Status of command placed in foreground, or failure if an error occurs. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 35 | $END |
| 36 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 37 | #include <config.h> |
| 38 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 39 | #include "../bashtypes.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 40 | #include <signal.h> |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 41 | |
| 42 | #if defined (HAVE_UNISTD_H) |
| 43 | # include <unistd.h> |
| 44 | #endif |
| 45 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 46 | #include "../bashintl.h" |
| 47 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 48 | #include "../shell.h" |
| 49 | #include "../jobs.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 50 | #include "common.h" |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 51 | #include "bashgetopt.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 52 | |
| 53 | #if defined (JOB_CONTROL) |
| 54 | extern char *this_command_name; |
| 55 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 56 | static int fg_bg __P((WORD_LIST *, int)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 57 | |
| 58 | /* How to bring a job into the foreground. */ |
| 59 | int |
| 60 | fg_builtin (list) |
| 61 | WORD_LIST *list; |
| 62 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 63 | int fg_bit; |
| 64 | register WORD_LIST *t; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 65 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 66 | if (job_control == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 67 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 68 | sh_nojobs ((char *)NULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 69 | return (EXECUTION_FAILURE); |
| 70 | } |
| 71 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 72 | if (no_options (list)) |
| 73 | return (EX_USAGE); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 74 | list = loptend; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 75 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 76 | /* If the last arg on the line is '&', then start this job in the |
| 77 | background. Else, fg the job. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 78 | for (t = list; t && t->next; t = t->next) |
| 79 | ; |
| 80 | fg_bit = (t && t->word->word[0] == '&' && t->word->word[1] == '\0') == 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 81 | |
| 82 | return (fg_bg (list, fg_bit)); |
| 83 | } |
| 84 | #endif /* JOB_CONTROL */ |
| 85 | |
| 86 | $BUILTIN bg |
| 87 | $FUNCTION bg_builtin |
| 88 | $DEPENDS_ON JOB_CONTROL |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 89 | $SHORT_DOC bg [job_spec ...] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 90 | Move jobs to the background. |
| 91 | |
| 92 | Place the jobs identified by each JOB_SPEC in the background, as if they |
| 93 | had been started with `&'. If JOB_SPEC is not present, the shell's notion |
| 94 | of the current job is used. |
| 95 | |
| 96 | Exit Status: |
| 97 | Returns success unless job control is not enabled or an error occurs. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 98 | $END |
| 99 | |
| 100 | #if defined (JOB_CONTROL) |
| 101 | /* How to put a job into the background. */ |
| 102 | int |
| 103 | bg_builtin (list) |
| 104 | WORD_LIST *list; |
| 105 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 106 | int r; |
| 107 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 108 | if (job_control == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 109 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 110 | sh_nojobs ((char *)NULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 111 | return (EXECUTION_FAILURE); |
| 112 | } |
| 113 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 114 | if (no_options (list)) |
| 115 | return (EX_USAGE); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 116 | list = loptend; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 117 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 118 | /* This relies on the fact that fg_bg() takes a WORD_LIST *, but only acts |
| 119 | on the first member (if any) of that list. */ |
| 120 | r = EXECUTION_SUCCESS; |
| 121 | do |
| 122 | { |
| 123 | if (fg_bg (list, 0) == EXECUTION_FAILURE) |
| 124 | r = EXECUTION_FAILURE; |
| 125 | if (list) |
| 126 | list = list->next; |
| 127 | } |
| 128 | while (list); |
| 129 | |
| 130 | return r; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* How to put a job into the foreground/background. */ |
| 134 | static int |
| 135 | fg_bg (list, foreground) |
| 136 | WORD_LIST *list; |
| 137 | int foreground; |
| 138 | { |
| 139 | sigset_t set, oset; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 140 | int job, status, old_async_pid; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 141 | JOB *j; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 142 | |
| 143 | BLOCK_CHILD (set, oset); |
| 144 | job = get_job_spec (list); |
| 145 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 146 | if (INVALID_JOB (job)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 147 | { |
| 148 | if (job != DUP_JOB) |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 149 | sh_badjob (list ? list->word->word : _("current")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 150 | |
| 151 | goto failure; |
| 152 | } |
| 153 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 154 | j = get_job_by_jid (job); |
| 155 | /* Or if j->pgrp == shell_pgrp. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 156 | if (IS_JOBCONTROL (job) == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 157 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 158 | builtin_error (_("job %d started without job control"), job + 1); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 159 | goto failure; |
| 160 | } |
| 161 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 162 | if (foreground == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 163 | { |
| 164 | old_async_pid = last_asynchronous_pid; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 165 | last_asynchronous_pid = j->pgrp; /* As per Posix.2 5.4.2 */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | status = start_job (job, foreground); |
| 169 | |
| 170 | if (status >= 0) |
| 171 | { |
| 172 | /* win: */ |
| 173 | UNBLOCK_CHILD (oset); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 174 | return (foreground ? status : EXECUTION_SUCCESS); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 175 | } |
| 176 | else |
| 177 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 178 | if (foreground == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 179 | last_asynchronous_pid = old_async_pid; |
| 180 | |
| 181 | failure: |
| 182 | UNBLOCK_CHILD (oset); |
| 183 | return (EXECUTION_FAILURE); |
| 184 | } |
| 185 | } |
| 186 | #endif /* JOB_CONTROL */ |