1 This file is jobs.def, from which is created jobs.c.
2 It implements the builtin "jobs" in Bash.
4 Copyright (C) 1987, 1989, 1991, 1992 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
25 $FUNCTION jobs_builtin
26 $DEPENDS_ON JOB_CONTROL
27 $SHORT_DOC jobs [-lnp] [jobspec ...] | 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.
32 If -x is given, COMMAND is run after all job specifications that appear
33 in ARGS have been replaced with the process ID of that job's process group
39 #if defined (JOB_CONTROL)
40 #include <sys/types.h>
44 #include "bashgetopt.h"
46 extern int job_control, interactive_shell;
47 static int execute_list_with_replacements ();
49 /* The `jobs' command. Prints outs a list of active jobs. If the
50 argument `-l' is given, then the process id's are printed also.
51 If the argument `-p' is given, print the process group leader's
52 pid only. If `-n' is given, only processes that have changed
53 status since the last notification are printed. If -x is given,
54 replace all job specs with the pid of the appropriate process
55 group leader and execute the command. */
60 int form = JLIST_STANDARD, execute = 0;
64 if (!job_control && !interactive_shell)
65 return (EXECUTION_SUCCESS);
67 reset_internal_getopt ();
68 while ((opt = internal_getopt (list, "lpnx")) != -1)
76 form = JLIST_PID_ONLY;
79 form = JLIST_CHANGED_ONLY;
82 if (form != JLIST_STANDARD)
84 builtin_error ("Other options not allowed with `-x'");
85 return (EXECUTION_FAILURE);
91 builtin_error ("usage: jobs [-lpn [jobspec]] [-x command [args]]");
99 return (execute_list_with_replacements (list));
104 return (EXECUTION_SUCCESS);
112 BLOCK_CHILD (set, oset);
113 job = get_job_spec (list);
115 if ((job == NO_JOB) || !jobs || !jobs[job])
117 builtin_error ("No such job %s", list->word->word);
120 else if (job != DUP_JOB)
121 list_one_job ((JOB *)NULL, form, 0, job);
123 UNBLOCK_CHILD (oset);
126 return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
130 execute_list_with_replacements (list)
133 register WORD_LIST *l;
136 /* First do the replacement of job specifications with pids. */
137 for (l = list; l; l = l->next)
139 if (l->word->word[0] == '%') /* we have a winner */
141 job = get_job_spec (l);
143 /* A bad job spec is not really a job spec! Pass it through. */
144 if (job < 0 || job >= job_slots || !jobs[job])
147 free (l->word->word);
148 l->word->word = itos (jobs[job]->pgrp);
152 /* Next make a new simple command and execute it. */
153 begin_unwind_frame ("jobs_builtin");
155 COMMAND *command = (COMMAND *)NULL;
157 add_unwind_protect (dispose_command, command);
159 command = make_bare_simple_command ();
160 command->value.Simple->words = copy_word_list (list);
161 command->value.Simple->redirects = (REDIRECT *)NULL;
162 command->flags |= CMD_INHIBIT_EXPANSION;
163 command->value.Simple->flags |= CMD_INHIBIT_EXPANSION;
165 result = execute_command (command);
168 run_unwind_frame ("jobs_builtin");
171 #endif /* JOB_CONTROL */