1 This file is jobs.def, from which is created jobs.c.
2 It implements the builtins "jobs" and "disown" 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.
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
41 #if defined (JOB_CONTROL)
42 #include "../bashtypes.h"
44 #if defined (HAVE_UNISTD_H)
48 #include "../bashansi.h"
49 #include "../bashintl.h"
53 #include "../execute_cmd.h"
54 #include "bashgetopt.h"
57 #define JSTATE_ANY 0x0
58 #define JSTATE_RUNNING 0x1
59 #define JSTATE_STOPPED 0x2
61 static int execute_list_with_replacements __P((WORD_LIST *));
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. */
75 int form, execute, state, opt, any_failed, job;
78 execute = any_failed = 0;
79 form = JLIST_STANDARD;
82 reset_internal_getopt ();
83 while ((opt = internal_getopt (list, "lpnxrs")) != -1)
91 form = JLIST_PID_ONLY;
94 form = JLIST_CHANGED_ONLY;
97 if (form != JLIST_STANDARD)
99 builtin_error (_("no other options allowed with `-x'"));
100 return (EXECUTION_FAILURE);
105 state = JSTATE_RUNNING;
108 state = JSTATE_STOPPED;
120 return (execute_list_with_replacements (list));
127 list_all_jobs (form);
130 list_running_jobs (form);
133 list_stopped_jobs (form);
136 return (EXECUTION_SUCCESS);
141 BLOCK_CHILD (set, oset);
142 job = get_job_spec (list);
144 if ((job == NO_JOB) || jobs == 0 || get_job_by_jid (job) == 0)
146 sh_badjob (list->word->word);
149 else if (job != DUP_JOB)
150 list_one_job ((JOB *)NULL, form, 0, job);
152 UNBLOCK_CHILD (oset);
155 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
159 execute_list_with_replacements (list)
162 register WORD_LIST *l;
167 /* First do the replacement of job specifications with pids. */
168 for (l = list; l; l = l->next)
170 if (l->word->word[0] == '%') /* we have a winner */
172 job = get_job_spec (l);
174 /* A bad job spec is not really a job spec! Pass it through. */
175 if (INVALID_JOB (job))
178 j = get_job_by_jid (job);
179 free (l->word->word);
180 l->word->word = itos (j->pgrp);
184 /* Next make a new simple command and execute it. */
185 begin_unwind_frame ("jobs_builtin");
187 command = make_bare_simple_command ();
188 command->value.Simple->words = copy_word_list (list);
189 command->value.Simple->redirects = (REDIRECT *)NULL;
190 command->flags |= CMD_INHIBIT_EXPANSION;
191 command->value.Simple->flags |= CMD_INHIBIT_EXPANSION;
193 add_unwind_protect (dispose_command, command);
194 result = execute_command (command);
195 dispose_command (command);
197 discard_unwind_frame ("jobs_builtin");
200 #endif /* JOB_CONTROL */
203 $FUNCTION disown_builtin
204 $DEPENDS_ON JOB_CONTROL
205 $SHORT_DOC disown [-h] [-ar] [jobspec ...]
206 By default, removes each JOBSPEC argument from the table of active jobs.
207 If the -h option is given, the job is not removed from the table, but is
208 marked so that SIGHUP is not sent to the job if the shell receives a
209 SIGHUP. The -a option, when JOBSPEC is not supplied, means to remove all
210 jobs from the job table; the -r option means to remove only running jobs.
213 #if defined (JOB_CONTROL)
215 disown_builtin (list)
218 int opt, job, retval, nohup_only, running_jobs, all_jobs;
222 nohup_only = running_jobs = all_jobs = 0;
223 reset_internal_getopt ();
224 while ((opt = internal_getopt (list, "ahr")) != -1)
243 retval = EXECUTION_SUCCESS;
245 /* `disown -a' or `disown -r' */
246 if (list == 0 && (all_jobs || running_jobs))
249 nohup_all_jobs (running_jobs);
251 delete_all_jobs (running_jobs);
252 return (EXECUTION_SUCCESS);
257 BLOCK_CHILD (set, oset);
258 job = (list && legal_number (list->word->word, &pid_value) && pid_value == (pid_t) pid_value)
259 ? get_job_by_pid ((pid_t) pid_value, 0)
260 : get_job_spec (list);
262 if (job == NO_JOB || jobs == 0 || INVALID_JOB (job))
264 sh_badjob (list ? list->word->word : "current");
265 retval = EXECUTION_FAILURE;
271 UNBLOCK_CHILD (oset);
280 #endif /* JOB_CONTROL */