8a293da507aace05e7cbb2474b0e73af6c9f7b65
[platform/upstream/bash.git] / builtins / jobs.def
1 This file is jobs.def, from which is created jobs.c.
2 It implements the builtin "jobs" 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 $PRODUCES jobs.c
23
24 $BUILTIN jobs
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
34 leader.
35 $END
36
37 #include "../shell.h"
38
39 #if defined (JOB_CONTROL)
40 #include <sys/types.h>
41 #include <signal.h>
42 #include "../jobs.h"
43
44 #include "bashgetopt.h"
45
46 extern int job_control, interactive_shell;
47 static int execute_list_with_replacements ();
48
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. */
56 int
57 jobs_builtin (list)
58      WORD_LIST *list;
59 {
60   int form = JLIST_STANDARD, execute = 0;
61   int opt;
62   int any_failed = 0;
63
64   if (!job_control && !interactive_shell)
65     return (EXECUTION_SUCCESS);
66
67   reset_internal_getopt ();
68   while ((opt = internal_getopt (list, "lpnx")) != -1)
69     {
70       switch (opt)
71         {
72         case 'l':
73           form = JLIST_LONG;
74           break;
75         case 'p':
76           form = JLIST_PID_ONLY;
77           break;
78         case 'n':
79           form = JLIST_CHANGED_ONLY;
80           break;
81         case 'x':
82           if (form != JLIST_STANDARD)
83             {
84               builtin_error ("Other options not allowed with `-x'");
85               return (EXECUTION_FAILURE);
86             }
87           execute++;
88           break;
89
90         default:
91           builtin_error ("usage: jobs [-lpn [jobspec]] [-x command [args]]");
92           return (EX_USAGE);
93         }
94     }
95
96   list = loptend;
97
98   if (execute)
99     return (execute_list_with_replacements (list));
100
101   if (!list)
102     {
103       list_jobs (form);
104       return (EXECUTION_SUCCESS);
105     }
106
107   while (list)
108     {
109       int job;
110       sigset_t set, oset;
111
112       BLOCK_CHILD (set, oset);
113       job = get_job_spec (list);
114
115       if ((job == NO_JOB) || !jobs || !jobs[job])
116         {
117           builtin_error ("No such job %s", list->word->word);
118           any_failed++;
119         }
120       else if (job != DUP_JOB)
121         list_one_job ((JOB *)NULL, form, 0, job);
122
123       UNBLOCK_CHILD (oset);
124       list = list->next;
125     }
126   return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
127 }
128
129 static int
130 execute_list_with_replacements (list)
131      WORD_LIST *list;
132 {
133   register WORD_LIST *l;
134   int job, result;
135
136   /* First do the replacement of job specifications with pids. */
137   for (l = list; l; l = l->next)
138     {
139       if (l->word->word[0] == '%')      /* we have a winner */
140         {
141           job = get_job_spec (l);
142
143           /* A bad job spec is not really a job spec! Pass it through. */
144           if (job < 0 || job >= job_slots || !jobs[job])
145             continue;
146
147           free (l->word->word);
148           l->word->word = itos (jobs[job]->pgrp);
149         }
150     }
151
152   /* Next make a new simple command and execute it. */
153   begin_unwind_frame ("jobs_builtin");
154   {
155     COMMAND *command = (COMMAND *)NULL;
156
157     add_unwind_protect (dispose_command, command);
158
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;
164
165     result = execute_command (command);
166   }
167
168   run_unwind_frame ("jobs_builtin");
169   return (result);
170 }
171 #endif /* JOB_CONTROL */