]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/jobs.def
Imported from ../bash-2.04.tar.gz.
[thirdparty/bash.git] / builtins / jobs.def
1 This file is jobs.def, from which is created jobs.c.
2 It implements the builtins "jobs" and "disown" in Bash.
3
4 Copyright (C) 1987, 1989, 1991, 1992 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 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
11 version.
12
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
16 for more details.
17
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.
21
22 $PRODUCES jobs.c
23
24 $BUILTIN jobs
25 $FUNCTION jobs_builtin
26 $DEPENDS_ON JOB_CONTROL
27 $SHORT_DOC jobs [-lnprs] [jobspec ...] or jobs -x command [args]
28 Lists the active jobs. The -l option lists process id's in addition
29 to the normal information; the -p option lists process id's only.
30 If -n is given, only processes that have changed status since the last
31 notification are printed. JOBSPEC restricts output to that job. The
32 -r and -s options restrict output to running and stopped jobs only,
33 respectively. Without options, the status of all active jobs is
34 printed. If -x is given, COMMAND is run after all job specifications
35 that appear in ARGS have been replaced with the process ID of that job's
36 process group leader.
37 $END
38
39 #include <config.h>
40
41 #if defined (JOB_CONTROL)
42 #include "../bashtypes.h"
43 #include <signal.h>
44 #if defined (HAVE_UNISTD_H)
45 # include <unistd.h>
46 #endif
47
48 #include "../bashansi.h"
49
50 #include "../shell.h"
51 #include "../jobs.h"
52 #include "../execute_cmd.h"
53 #include "bashgetopt.h"
54 #include "common.h"
55
56 #define JSTATE_ANY 0x0
57 #define JSTATE_RUNNING 0x1
58 #define JSTATE_STOPPED 0x2
59
60 extern int job_control, interactive_shell;
61 static int execute_list_with_replacements ();
62
63 /* The `jobs' command. Prints outs a list of active jobs. If the
64 argument `-l' is given, then the process id's are printed also.
65 If the argument `-p' is given, print the process group leader's
66 pid only. If `-n' is given, only processes that have changed
67 status since the last notification are printed. If -x is given,
68 replace all job specs with the pid of the appropriate process
69 group leader and execute the command. The -r and -s options mean
70 to print info about running and stopped jobs only, respectively. */
71 int
72 jobs_builtin (list)
73 WORD_LIST *list;
74 {
75 int form, execute, state, opt, any_failed, job;
76 sigset_t set, oset;
77
78 if (job_control == 0 && interactive_shell == 0)
79 return (EXECUTION_SUCCESS);
80
81 execute = any_failed = 0;
82 form = JLIST_STANDARD;
83 state = JSTATE_ANY;
84
85 reset_internal_getopt ();
86 while ((opt = internal_getopt (list, "lpnxrs")) != -1)
87 {
88 switch (opt)
89 {
90 case 'l':
91 form = JLIST_LONG;
92 break;
93 case 'p':
94 form = JLIST_PID_ONLY;
95 break;
96 case 'n':
97 form = JLIST_CHANGED_ONLY;
98 break;
99 case 'x':
100 if (form != JLIST_STANDARD)
101 {
102 builtin_error ("Other options not allowed with `-x'");
103 return (EXECUTION_FAILURE);
104 }
105 execute++;
106 break;
107 case 'r':
108 state = JSTATE_RUNNING;
109 break;
110 case 's':
111 state = JSTATE_STOPPED;
112 break;
113
114 default:
115 builtin_usage ();
116 return (EX_USAGE);
117 }
118 }
119
120 list = loptend;
121
122 if (execute)
123 return (execute_list_with_replacements (list));
124
125 if (!list)
126 {
127 switch (state)
128 {
129 case JSTATE_ANY:
130 list_all_jobs (form);
131 break;
132 case JSTATE_RUNNING:
133 list_running_jobs (form);
134 break;
135 case JSTATE_STOPPED:
136 list_stopped_jobs (form);
137 break;
138 }
139 return (EXECUTION_SUCCESS);
140 }
141
142 while (list)
143 {
144 BLOCK_CHILD (set, oset);
145 job = get_job_spec (list);
146
147 if ((job == NO_JOB) || !jobs || !jobs[job])
148 {
149 builtin_error ("no such job %s", list->word->word);
150 any_failed++;
151 }
152 else if (job != DUP_JOB)
153 list_one_job ((JOB *)NULL, form, 0, job);
154
155 UNBLOCK_CHILD (oset);
156 list = list->next;
157 }
158 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
159 }
160
161 static int
162 execute_list_with_replacements (list)
163 WORD_LIST *list;
164 {
165 register WORD_LIST *l;
166 int job, result;
167
168 /* First do the replacement of job specifications with pids. */
169 for (l = list; l; l = l->next)
170 {
171 if (l->word->word[0] == '%') /* we have a winner */
172 {
173 job = get_job_spec (l);
174
175 /* A bad job spec is not really a job spec! Pass it through. */
176 if (job < 0 || job >= job_slots || !jobs[job])
177 continue;
178
179 free (l->word->word);
180 l->word->word = itos (jobs[job]->pgrp);
181 }
182 }
183
184 /* Next make a new simple command and execute it. */
185 begin_unwind_frame ("jobs_builtin");
186 {
187 COMMAND *command = (COMMAND *)NULL;
188
189 add_unwind_protect (dispose_command, command);
190
191 command = make_bare_simple_command ();
192 command->value.Simple->words = copy_word_list (list);
193 command->value.Simple->redirects = (REDIRECT *)NULL;
194 command->flags |= CMD_INHIBIT_EXPANSION;
195 command->value.Simple->flags |= CMD_INHIBIT_EXPANSION;
196
197 result = execute_command (command);
198 }
199
200 run_unwind_frame ("jobs_builtin");
201 return (result);
202 }
203 #endif /* JOB_CONTROL */
204
205 $BUILTIN disown
206 $FUNCTION disown_builtin
207 $DEPENDS_ON JOB_CONTROL
208 $SHORT_DOC disown [-h] [-ar] [jobspec ...]
209 By default, removes each JOBSPEC argument from the table of active jobs.
210 If the -h option is given, the job is not removed from the table, but is
211 marked so that SIGHUP is not sent to the job if the shell receives a
212 SIGHUP. The -a option, when JOBSPEC is not supplied, means to remove all
213 jobs from the job table; the -r option means to remove only running jobs.
214 $END
215
216 #if defined (JOB_CONTROL)
217 int
218 disown_builtin (list)
219 WORD_LIST *list;
220 {
221 int opt, job, retval, nohup_only, running_jobs, all_jobs;
222 sigset_t set, oset;
223
224 nohup_only = running_jobs = all_jobs = 0;
225 reset_internal_getopt ();
226 while ((opt = internal_getopt (list, "ahr")) != -1)
227 {
228 switch (opt)
229 {
230 case 'a':
231 all_jobs = 1;
232 break;
233 case 'h':
234 nohup_only = 1;
235 break;
236 case 'r':
237 running_jobs = 1;
238 break;
239 default:
240 builtin_usage ();
241 return (EX_USAGE);
242 }
243 }
244 list = loptend;
245 retval = EXECUTION_SUCCESS;
246
247 /* `disown -a' or `disown -r' */
248 if (list == 0 && (all_jobs || running_jobs))
249 {
250 if (nohup_only)
251 nohup_all_jobs (running_jobs);
252 else
253 delete_all_jobs (running_jobs);
254 return (EXECUTION_SUCCESS);
255 }
256
257 do
258 {
259 BLOCK_CHILD (set, oset);
260 job = (list && all_digits(list->word->word))
261 ? get_job_by_pid (atoi(list->word->word), 0)
262 : get_job_spec (list);
263
264 if (job == NO_JOB || jobs == 0 || job < 0 || job >= job_slots || jobs[job] == 0)
265 {
266 builtin_error ("%s: no such job", list ? list->word->word : "current");
267 retval = EXECUTION_FAILURE;
268 }
269 else if (nohup_only)
270 nohup_job (job);
271 else
272 delete_job (job, 1);
273 UNBLOCK_CHILD (oset);
274
275 if (list)
276 list = list->next;
277 }
278 while (list);
279
280 return (retval);
281 }
282 #endif /* JOB_CONTROL */