]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/fg_bg.def
Bash-4.4 distribution sources and documentation
[thirdparty/bash.git] / builtins / fg_bg.def
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
4 Copyright (C) 1987-2009 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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.
12
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.
17
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/>.
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]
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.
35 $END
36
37 #include <config.h>
38
39 #include "../bashtypes.h"
40 #include <signal.h>
41
42 #if defined (HAVE_UNISTD_H)
43 # include <unistd.h>
44 #endif
45
46 #include "../bashintl.h"
47
48 #include "../shell.h"
49 #include "../jobs.h"
50 #include "common.h"
51 #include "bashgetopt.h"
52
53 #if defined (JOB_CONTROL)
54 extern char *this_command_name;
55
56 static int fg_bg __P((WORD_LIST *, int));
57
58 /* How to bring a job into the foreground. */
59 int
60 fg_builtin (list)
61 WORD_LIST *list;
62 {
63 int fg_bit;
64 register WORD_LIST *t;
65
66 CHECK_HELPOPT (list);
67
68 if (job_control == 0)
69 {
70 sh_nojobs ((char *)NULL);
71 return (EXECUTION_FAILURE);
72 }
73
74 if (no_options (list))
75 return (EX_USAGE);
76 list = loptend;
77
78 /* If the last arg on the line is '&', then start this job in the
79 background. Else, fg the job. */
80 for (t = list; t && t->next; t = t->next)
81 ;
82 fg_bit = (t && t->word->word[0] == '&' && t->word->word[1] == '\0') == 0;
83
84 return (fg_bg (list, fg_bit));
85 }
86 #endif /* JOB_CONTROL */
87
88 $BUILTIN bg
89 $FUNCTION bg_builtin
90 $DEPENDS_ON JOB_CONTROL
91 $SHORT_DOC bg [job_spec ...]
92 Move jobs to the background.
93
94 Place the jobs identified by each JOB_SPEC in the background, as if they
95 had been started with `&'. If JOB_SPEC is not present, the shell's notion
96 of the current job is used.
97
98 Exit Status:
99 Returns success unless job control is not enabled or an error occurs.
100 $END
101
102 #if defined (JOB_CONTROL)
103 /* How to put a job into the background. */
104 int
105 bg_builtin (list)
106 WORD_LIST *list;
107 {
108 int r;
109
110 CHECK_HELPOPT (list);
111
112 if (job_control == 0)
113 {
114 sh_nojobs ((char *)NULL);
115 return (EXECUTION_FAILURE);
116 }
117
118 if (no_options (list))
119 return (EX_USAGE);
120 list = loptend;
121
122 /* This relies on the fact that fg_bg() takes a WORD_LIST *, but only acts
123 on the first member (if any) of that list. */
124 r = EXECUTION_SUCCESS;
125 do
126 {
127 if (fg_bg (list, 0) == EXECUTION_FAILURE)
128 r = EXECUTION_FAILURE;
129 if (list)
130 list = list->next;
131 }
132 while (list);
133
134 return r;
135 }
136
137 /* How to put a job into the foreground/background. */
138 static int
139 fg_bg (list, foreground)
140 WORD_LIST *list;
141 int foreground;
142 {
143 sigset_t set, oset;
144 int job, status, old_async_pid;
145 JOB *j;
146
147 BLOCK_CHILD (set, oset);
148 job = get_job_spec (list);
149
150 if (INVALID_JOB (job))
151 {
152 if (job != DUP_JOB)
153 sh_badjob (list ? list->word->word : _("current"));
154
155 goto failure;
156 }
157
158 j = get_job_by_jid (job);
159 /* Or if j->pgrp == shell_pgrp. */
160 if (IS_JOBCONTROL (job) == 0)
161 {
162 builtin_error (_("job %d started without job control"), job + 1);
163 goto failure;
164 }
165
166 if (foreground == 0)
167 {
168 old_async_pid = last_asynchronous_pid;
169 last_asynchronous_pid = j->pgrp; /* As per Posix.2 5.4.2 */
170 }
171
172 status = start_job (job, foreground);
173
174 if (status >= 0)
175 {
176 /* win: */
177 UNBLOCK_CHILD (oset);
178 return (foreground ? status : EXECUTION_SUCCESS);
179 }
180 else
181 {
182 if (foreground == 0)
183 last_asynchronous_pid = old_async_pid;
184
185 failure:
186 UNBLOCK_CHILD (oset);
187 return (EXECUTION_FAILURE);
188 }
189 }
190 #endif /* JOB_CONTROL */