i-bash/builtins/jobs.def

301 lines
7.1 KiB
Modula-2
Raw Permalink Normal View History

1996-08-26 18:22:31 +00:00
This file is jobs.def, from which is created jobs.c.
1996-12-23 17:02:34 +00:00
It implements the builtins "jobs" and "disown" in Bash.
1996-08-26 18:22:31 +00:00
Copyright (C) 1987-2015 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
2009-01-12 13:36:28 +00:00
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
1996-08-26 18:22:31 +00:00
$PRODUCES jobs.c
$BUILTIN jobs
$FUNCTION jobs_builtin
$DEPENDS_ON JOB_CONTROL
1996-12-23 17:02:34 +00:00
$SHORT_DOC jobs [-lnprs] [jobspec ...] or jobs -x command [args]
2009-01-12 13:36:28 +00:00
Display status of jobs.
Lists the active jobs. JOBSPEC restricts output to that job.
Without options, the status of all active jobs is displayed.
Options:
-l lists process IDs in addition to the normal information
-n lists only processes that have changed status since the last
notification
2009-01-12 13:36:28 +00:00
-p lists process IDs only
-r restrict output to running jobs
-s restrict output to stopped jobs
If -x is supplied, COMMAND is run after all job specifications that
appear in ARGS have been replaced with the process ID of that job's
1996-12-23 17:02:34 +00:00
process group leader.
2009-01-12 13:36:28 +00:00
Exit Status:
Returns success unless an invalid option is given or an error occurs.
If -x is used, returns the exit status of COMMAND.
1996-08-26 18:22:31 +00:00
$END
1996-12-23 17:02:34 +00:00
#include <config.h>
1996-08-26 18:22:31 +00:00
#if defined (JOB_CONTROL)
1996-12-23 17:02:34 +00:00
#include "../bashtypes.h"
1996-08-26 18:22:31 +00:00
#include <signal.h>
1996-12-23 17:02:34 +00:00
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "../bashansi.h"
2004-07-27 13:29:18 +00:00
#include "../bashintl.h"
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
#include "../shell.h"
#include "../jobs.h"
#include "../execute_cmd.h"
1996-08-26 18:22:31 +00:00
#include "bashgetopt.h"
1996-12-23 17:02:34 +00:00
#include "common.h"
#define JSTATE_ANY 0x0
#define JSTATE_RUNNING 0x1
#define JSTATE_STOPPED 0x2
1996-08-26 18:22:31 +00:00
2001-11-13 17:56:06 +00:00
static int execute_list_with_replacements __P((WORD_LIST *));
1996-08-26 18:22:31 +00:00
/* The `jobs' command. Prints outs a list of active jobs. If the
argument `-l' is given, then the process id's are printed also.
If the argument `-p' is given, print the process group leader's
pid only. If `-n' is given, only processes that have changed
status since the last notification are printed. If -x is given,
replace all job specs with the pid of the appropriate process
1996-12-23 17:02:34 +00:00
group leader and execute the command. The -r and -s options mean
to print info about running and stopped jobs only, respectively. */
1996-08-26 18:22:31 +00:00
int
jobs_builtin (list)
WORD_LIST *list;
{
1996-12-23 17:02:34 +00:00
int form, execute, state, opt, any_failed, job;
sigset_t set, oset;
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
execute = any_failed = 0;
form = JLIST_STANDARD;
state = JSTATE_ANY;
1996-08-26 18:22:31 +00:00
reset_internal_getopt ();
1996-12-23 17:02:34 +00:00
while ((opt = internal_getopt (list, "lpnxrs")) != -1)
1996-08-26 18:22:31 +00:00
{
switch (opt)
{
case 'l':
form = JLIST_LONG;
break;
case 'p':
form = JLIST_PID_ONLY;
break;
case 'n':
form = JLIST_CHANGED_ONLY;
break;
case 'x':
if (form != JLIST_STANDARD)
{
2004-07-27 13:29:18 +00:00
builtin_error (_("no other options allowed with `-x'"));
1996-08-26 18:22:31 +00:00
return (EXECUTION_FAILURE);
}
execute++;
break;
1996-12-23 17:02:34 +00:00
case 'r':
state = JSTATE_RUNNING;
break;
case 's':
state = JSTATE_STOPPED;
break;
1996-08-26 18:22:31 +00:00
CASE_HELPOPT;
1996-08-26 18:22:31 +00:00
default:
1996-12-23 17:02:34 +00:00
builtin_usage ();
1996-08-26 18:22:31 +00:00
return (EX_USAGE);
}
}
list = loptend;
if (execute)
return (execute_list_with_replacements (list));
if (!list)
{
1996-12-23 17:02:34 +00:00
switch (state)
{
case JSTATE_ANY:
list_all_jobs (form);
break;
case JSTATE_RUNNING:
list_running_jobs (form);
break;
case JSTATE_STOPPED:
list_stopped_jobs (form);
break;
}
1996-08-26 18:22:31 +00:00
return (EXECUTION_SUCCESS);
}
while (list)
{
BLOCK_CHILD (set, oset);
job = get_job_spec (list);
2005-12-07 14:08:12 +00:00
if ((job == NO_JOB) || jobs == 0 || get_job_by_jid (job) == 0)
1996-08-26 18:22:31 +00:00
{
2002-07-17 14:10:11 +00:00
sh_badjob (list->word->word);
1996-08-26 18:22:31 +00:00
any_failed++;
}
else if (job != DUP_JOB)
list_one_job ((JOB *)NULL, form, 0, job);
UNBLOCK_CHILD (oset);
list = list->next;
}
return (any_failed ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
}
static int
execute_list_with_replacements (list)
WORD_LIST *list;
{
register WORD_LIST *l;
int job, result;
2004-07-27 13:29:18 +00:00
COMMAND *command;
2005-12-07 14:08:12 +00:00
JOB *j;
1996-08-26 18:22:31 +00:00
/* First do the replacement of job specifications with pids. */
for (l = list; l; l = l->next)
{
if (l->word->word[0] == '%') /* we have a winner */
{
job = get_job_spec (l);
/* A bad job spec is not really a job spec! Pass it through. */
2005-12-07 14:08:12 +00:00
if (INVALID_JOB (job))
1996-08-26 18:22:31 +00:00
continue;
2005-12-07 14:08:12 +00:00
j = get_job_by_jid (job);
1996-08-26 18:22:31 +00:00
free (l->word->word);
2005-12-07 14:08:12 +00:00
l->word->word = itos (j->pgrp);
1996-08-26 18:22:31 +00:00
}
}
/* Next make a new simple command and execute it. */
begin_unwind_frame ("jobs_builtin");
2004-07-27 13:29:18 +00:00
command = make_bare_simple_command ();
command->value.Simple->words = copy_word_list (list);
command->value.Simple->redirects = (REDIRECT *)NULL;
command->flags |= CMD_INHIBIT_EXPANSION;
command->value.Simple->flags |= CMD_INHIBIT_EXPANSION;
1996-08-26 18:22:31 +00:00
2004-07-27 13:29:18 +00:00
add_unwind_protect (dispose_command, command);
result = execute_command (command);
dispose_command (command);
1996-08-26 18:22:31 +00:00
2004-07-27 13:29:18 +00:00
discard_unwind_frame ("jobs_builtin");
1996-08-26 18:22:31 +00:00
return (result);
}
#endif /* JOB_CONTROL */
1996-12-23 17:02:34 +00:00
$BUILTIN disown
$FUNCTION disown_builtin
$DEPENDS_ON JOB_CONTROL
$SHORT_DOC disown [-h] [-ar] [jobspec ... | pid ...]
2009-01-12 13:36:28 +00:00
Remove jobs from current shell.
Removes each JOBSPEC argument from the table of active jobs. Without
any JOBSPECs, the shell uses its notion of the current job.
Options:
-a remove all jobs if JOBSPEC is not supplied
-h mark each JOBSPEC so that SIGHUP is not sent to the job if the
shell receives a SIGHUP
2009-01-12 13:36:28 +00:00
-r remove only running jobs
Exit Status:
Returns success unless an invalid option or JOBSPEC is given.
1996-12-23 17:02:34 +00:00
$END
#if defined (JOB_CONTROL)
int
disown_builtin (list)
WORD_LIST *list;
{
1998-04-17 19:52:44 +00:00
int opt, job, retval, nohup_only, running_jobs, all_jobs;
1996-12-23 17:02:34 +00:00
sigset_t set, oset;
2002-07-17 14:10:11 +00:00
intmax_t pid_value;
1996-12-23 17:02:34 +00:00
1998-04-17 19:52:44 +00:00
nohup_only = running_jobs = all_jobs = 0;
1996-12-23 17:02:34 +00:00
reset_internal_getopt ();
1998-04-17 19:52:44 +00:00
while ((opt = internal_getopt (list, "ahr")) != -1)
1996-12-23 17:02:34 +00:00
{
switch (opt)
{
1998-04-17 19:52:44 +00:00
case 'a':
all_jobs = 1;
break;
1996-12-23 17:02:34 +00:00
case 'h':
nohup_only = 1;
break;
1998-04-17 19:52:44 +00:00
case 'r':
running_jobs = 1;
break;
CASE_HELPOPT;
1996-12-23 17:02:34 +00:00
default:
builtin_usage ();
return (EX_USAGE);
}
}
list = loptend;
retval = EXECUTION_SUCCESS;
1998-04-17 19:52:44 +00:00
/* `disown -a' or `disown -r' */
if (list == 0 && (all_jobs || running_jobs))
1997-06-05 14:59:13 +00:00
{
if (nohup_only)
1998-04-17 19:52:44 +00:00
nohup_all_jobs (running_jobs);
1997-06-05 14:59:13 +00:00
else
1998-04-17 19:52:44 +00:00
delete_all_jobs (running_jobs);
1997-06-05 14:59:13 +00:00
return (EXECUTION_SUCCESS);
}
1996-12-23 17:02:34 +00:00
do
{
BLOCK_CHILD (set, oset);
2001-11-13 17:56:06 +00:00
job = (list && legal_number (list->word->word, &pid_value) && pid_value == (pid_t) pid_value)
? get_job_by_pid ((pid_t) pid_value, 0)
1997-06-05 14:59:13 +00:00
: get_job_spec (list);
1996-12-23 17:02:34 +00:00
2005-12-07 14:08:12 +00:00
if (job == NO_JOB || jobs == 0 || INVALID_JOB (job))
1996-12-23 17:02:34 +00:00
{
2009-01-12 13:36:28 +00:00
sh_badjob (list ? list->word->word : _("current"));
1996-12-23 17:02:34 +00:00
retval = EXECUTION_FAILURE;
}
else if (nohup_only)
nohup_job (job);
else
1998-04-17 19:52:44 +00:00
delete_job (job, 1);
1996-12-23 17:02:34 +00:00
UNBLOCK_CHILD (oset);
if (list)
list = list->next;
}
while (list);
1997-06-05 14:59:13 +00:00
1996-12-23 17:02:34 +00:00
return (retval);
}
#endif /* JOB_CONTROL */