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-2005 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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 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
35 #include "../bashtypes.h"
38 #if defined (HAVE_UNISTD_H)
42 #include "../bashintl.h"
47 #include "bashgetopt.h"
49 #if defined (JOB_CONTROL)
50 extern char *this_command_name;
52 static int fg_bg __P((WORD_LIST *, int));
54 /* How to bring a job into the foreground. */
60 register WORD_LIST *t;
64 sh_nojobs ((char *)NULL);
65 return (EXECUTION_FAILURE);
68 if (no_options (list))
72 /* If the last arg on the line is '&', then start this job in the
73 background. Else, fg the job. */
74 for (t = list; t && t->next; t = t->next)
76 fg_bit = (t && t->word->word[0] == '&' && t->word->word[1] == '\0') == 0;
78 return (fg_bg (list, fg_bit));
80 #endif /* JOB_CONTROL */
84 $DEPENDS_ON JOB_CONTROL
85 $SHORT_DOC bg [job_spec ...]
86 Place each JOB_SPEC in the background, as if it had been started with
87 `&'. If JOB_SPEC is not present, the shell's notion of the current
91 #if defined (JOB_CONTROL)
92 /* How to put a job into the background. */
101 sh_nojobs ((char *)NULL);
102 return (EXECUTION_FAILURE);
105 if (no_options (list))
109 /* This relies on the fact that fg_bg() takes a WORD_LIST *, but only acts
110 on the first member (if any) of that list. */
111 r = EXECUTION_SUCCESS;
114 if (fg_bg (list, 0) == EXECUTION_FAILURE)
115 r = EXECUTION_FAILURE;
124 /* How to put a job into the foreground/background. */
126 fg_bg (list, foreground)
131 int job, status, old_async_pid;
134 BLOCK_CHILD (set, oset);
135 job = get_job_spec (list);
137 if (INVALID_JOB (job))
140 sh_badjob (list ? list->word->word : "current");
145 j = get_job_by_jid (job);
146 /* Or if j->pgrp == shell_pgrp. */
147 if (IS_JOBCONTROL (job) == 0)
149 builtin_error (_("job %d started without job control"), job + 1);
155 old_async_pid = last_asynchronous_pid;
156 last_asynchronous_pid = j->pgrp; /* As per Posix.2 5.4.2 */
159 status = start_job (job, foreground);
164 UNBLOCK_CHILD (oset);
165 return (foreground ? status : EXECUTION_SUCCESS);
170 last_asynchronous_pid = old_async_pid;
173 UNBLOCK_CHILD (oset);
174 return (EXECUTION_FAILURE);
177 #endif /* JOB_CONTROL */