improve of cmpl.
[bush.git] / builtins / fg_bg.def
blobd53eb74197b2b891b03565a5cc75a726919d08ca
1 This file is fg_bg.def, from which is created fg_bg.c.
2 It implements the builtins "bg" and "fg" in Bush.
4 Copyright (C) 1987-2020 Free Software Foundation, Inc.
6 This file is part of GNU Bush, the Bourne Again SHell.
8 Bush 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 Bush 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 Bush. If not, see <http://www.gnu.org/licenses/>.
21 $PRODUCES fg_bg.c
23 $BUILTIN fg
24 $FUNCTION fg_builtin
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
31 current job is used.
33 Exit Status:
34 Status of command placed in foreground, or failure if an error occurs.
35 $END
37 #include <config.h>
39 #include "../src/bushtypes.h"
40 #include <signal.h>
42 #if defined (HAVE_UNISTD_H)
43 # include <unistd.h>
44 #endif
46 #include "../src/bushintl.h"
48 #include "../src/shell.h"
49 #include "../src/runner/execute_cmd.h"
50 #include "../src/jobs.h"
51 #include "common.h"
52 #include "bushgetopt.h"
54 #if defined (JOB_CONTROL)
55 static int fg_bg PARAMS((WORD_LIST *, int));
57 /* How to bring a job into the foreground. */
58 int
59 fg_builtin (list)
60 WORD_LIST *list;
62 int fg_bit;
63 register WORD_LIST *t;
65 CHECK_HELPOPT (list);
67 if (job_control == 0)
69 sh_nojobs ((char *)NULL);
70 return (EXECUTION_FAILURE);
73 if (no_options (list))
74 return (EX_USAGE);
75 list = loptend;
77 /* If the last arg on the line is '&', then start this job in the
78 background. Else, fg the job. */
79 for (t = list; t && t->next; t = t->next)
81 fg_bit = (t && t->word->word[0] == '&' && t->word->word[1] == '\0') == 0;
83 return (fg_bg (list, fg_bit));
85 #endif /* JOB_CONTROL */
87 $BUILTIN bg
88 $FUNCTION bg_builtin
89 $DEPENDS_ON JOB_CONTROL
90 $SHORT_DOC bg [job_spec ...]
91 Move jobs to the background.
93 Place the jobs identified by each JOB_SPEC in the background, as if they
94 had been started with `&'. If JOB_SPEC is not present, the shell's notion
95 of the current job is used.
97 Exit Status:
98 Returns success unless job control is not enabled or an error occurs.
99 $END
101 #if defined (JOB_CONTROL)
102 /* How to put a job into the background. */
104 bg_builtin (list)
105 WORD_LIST *list;
107 int r;
109 CHECK_HELPOPT (list);
111 if (job_control == 0)
113 sh_nojobs ((char *)NULL);
114 return (EXECUTION_FAILURE);
117 if (no_options (list))
118 return (EX_USAGE);
119 list = loptend;
121 /* This relies on the fact that fg_bg() takes a WORD_LIST *, but only acts
122 on the first member (if any) of that list. */
123 r = EXECUTION_SUCCESS;
126 if (fg_bg (list, 0) == EXECUTION_FAILURE)
127 r = EXECUTION_FAILURE;
128 if (list)
129 list = list->next;
131 while (list);
133 return r;
136 /* How to put a job into the foreground/background. */
137 static int
138 fg_bg (list, foreground)
139 WORD_LIST *list;
140 int foreground;
142 sigset_t set, oset;
143 int job, status, old_async_pid;
144 JOB *j;
146 BLOCK_CHILD (set, oset);
147 job = get_job_spec (list);
149 if (INVALID_JOB (job))
151 if (job != DUP_JOB)
152 sh_badjob (list ? list->word->word : _("current"));
154 goto failure;
157 j = get_job_by_jid (job);
158 /* Or if j->pgrp == shell_pgrp. */
159 if (IS_JOBCONTROL (job) == 0)
161 builtin_error (_("job %d started without job control"), job + 1);
162 goto failure;
165 if (foreground == 0)
167 old_async_pid = last_asynchronous_pid;
168 last_asynchronous_pid = j->pgrp; /* As per Posix.2 5.4.2 */
171 status = start_job (job, foreground);
173 if (status >= 0)
175 /* win: */
176 UNBLOCK_CHILD (oset);
177 return (foreground ? status : EXECUTION_SUCCESS);
179 else
181 if (foreground == 0)
182 last_asynchronous_pid = old_async_pid;
184 failure:
185 UNBLOCK_CHILD (oset);
186 return (EXECUTION_FAILURE);
189 #endif /* JOB_CONTROL */