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