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, 1989, 1991 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 it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 1, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 $DEPENDS_ON JOB_CONTROL
27 $SHORT_DOC fg [job_spec]
28 Place JOB_SPEC in the foreground, and make it the current job. If
29 JOB_SPEC is not present, the shell's notion of the current job is
33 #include <sys/types.h>
38 #if defined (JOB_CONTROL)
39 extern char *this_command_name;
43 /* How to bring a job into the foreground. */
49 register WORD_LIST *t = list;
53 builtin_error ("no job control");
54 return (EXECUTION_FAILURE);
57 /* If the last arg on the line is '&', then start this job in the
58 background. Else, fg the job. */
63 if (t && t->word->word[0] == '&' && !t->word->word[1])
66 return (fg_bg (list, fg_bit));
68 #endif /* JOB_CONTROL */
72 $DEPENDS_ON JOB_CONTROL
73 $SHORT_DOC bg [job_spec]
74 Place JOB_SPEC in the background, as if it had been started with
75 `&'. If JOB_SPEC is not present, the shell's notion of the current
79 #if defined (JOB_CONTROL)
80 /* How to put a job into the background. */
87 builtin_error ("no job control");
88 return (EXECUTION_FAILURE);
91 return (fg_bg (list, 0));
94 /* How to put a job into the foreground/background. */
96 fg_bg (list, foreground)
101 int job, status = EXECUTION_SUCCESS, old_async_pid;
103 BLOCK_CHILD (set, oset);
104 job = get_job_spec (list);
106 if (job < 0 || job >= job_slots || !jobs[job])
109 builtin_error ("No such job %s", list ? list->word->word : "");
114 /* Or if jobs[job]->pgrp == shell_pgrp. */
115 if (!(jobs[job]->flags & J_JOBCONTROL))
117 builtin_error ("job %%%d started without job control", job + 1);
123 old_async_pid = last_asynchronous_pid;
124 last_asynchronous_pid = jobs[job]->pgrp; /* As per Posix.2 5.4.2 */
127 status = start_job (job, foreground);
132 UNBLOCK_CHILD (oset);
138 last_asynchronous_pid = old_async_pid;
141 UNBLOCK_CHILD (oset);
142 return (EXECUTION_FAILURE);
145 #endif /* JOB_CONTROL */