1 This file is fg_bg.def, from which is created fg_bg.c.
2 It implements the builtins "bg" and "fg" in Bash.
4 Copyright (C) 1987-2009 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
25 $DEPENDS_ON JOB_CONTROL
26 $SHORT_DOC fg [job_spec]
27 Move job to the foreground.
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
34 Status of command placed in foreground, or failure if an error occurs.
39 #include "../bashtypes.h"
42 #if defined (HAVE_UNISTD_H)
46 #include "../bashintl.h"
51 #include "bashgetopt.h"
53 #if defined (JOB_CONTROL)
54 extern char *this_command_name;
56 static int fg_bg __P((WORD_LIST *, int));
58 /* How to bring a job into the foreground. */
64 register WORD_LIST *t;
68 sh_nojobs ((char *)NULL);
69 return (EXECUTION_FAILURE);
72 if (no_options (list))
76 /* If the last arg on the line is '&', then start this job in the
77 background. Else, fg the job. */
78 for (t = list; t && t->next; t = t->next)
80 fg_bit = (t && t->word->word[0] == '&' && t->word->word[1] == '\0') == 0;
82 return (fg_bg (list, fg_bit));
84 #endif /* JOB_CONTROL */
88 $DEPENDS_ON JOB_CONTROL
89 $SHORT_DOC bg [job_spec ...]
90 Move jobs to the background.
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.
97 Returns success unless job control is not enabled or an error occurs.
100 #if defined (JOB_CONTROL)
101 /* How to put a job into the background. */
108 if (job_control == 0)
110 sh_nojobs ((char *)NULL);
111 return (EXECUTION_FAILURE);
114 if (no_options (list))
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;
123 if (fg_bg (list, 0) == EXECUTION_FAILURE)
124 r = EXECUTION_FAILURE;
133 /* How to put a job into the foreground/background. */
135 fg_bg (list, foreground)
140 int job, status, old_async_pid;
143 BLOCK_CHILD (set, oset);
144 job = get_job_spec (list);
146 if (INVALID_JOB (job))
149 sh_badjob (list ? list->word->word : _("current"));
154 j = get_job_by_jid (job);
155 /* Or if j->pgrp == shell_pgrp. */
156 if (IS_JOBCONTROL (job) == 0)
158 builtin_error (_("job %d started without job control"), job + 1);
164 old_async_pid = last_asynchronous_pid;
165 last_asynchronous_pid = j->pgrp; /* As per Posix.2 5.4.2 */
168 status = start_job (job, foreground);
173 UNBLOCK_CHILD (oset);
174 return (foreground ? status : EXECUTION_SUCCESS);
179 last_asynchronous_pid = old_async_pid;
182 UNBLOCK_CHILD (oset);
183 return (EXECUTION_FAILURE);
186 #endif /* JOB_CONTROL */