]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - builtins/jobs.def
Bash-4.3 distribution sources and documentation
[thirdparty/bash.git] / builtins / jobs.def
index 4cc9b77bdf38757e4b6486afb9cb4e8b98573428..47da58e495a0dcc7b6f13a3371b4f4e42f9c51ca 100644 (file)
@@ -1,23 +1,22 @@
 This file is jobs.def, from which is created jobs.c.
 It implements the builtins "jobs" and "disown" in Bash.
 
-Copyright (C) 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
+Copyright (C) 1987-2009 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
-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 1, or (at your option) any later
-version.
+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.
 
-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.
+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.
 
-You should have received a copy of the GNU General Public License along
-with Bash; see the file COPYING.  If not, write to the Free Software
-Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+You should have received a copy of the GNU General Public License
+along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 
 $PRODUCES jobs.c
 
@@ -25,15 +24,26 @@ $BUILTIN jobs
 $FUNCTION jobs_builtin
 $DEPENDS_ON JOB_CONTROL
 $SHORT_DOC jobs [-lnprs] [jobspec ...] or jobs -x command [args]
-Lists the active jobs.  The -l option lists process id's in addition
-to the normal information; the -p option lists process id's only.
-If -n is given, only processes that have changed status since the last
-notification are printed.  JOBSPEC restricts output to that job.  The
--r and -s options restrict output to running and stopped jobs only,
-respectively.  Without options, the status of all active jobs is
-printed.  If -x is given, COMMAND is run after all job specifications
-that appear in ARGS have been replaced with the process ID of that job's
+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
+  -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
 process group leader.
+
+Exit Status:
+Returns success unless an invalid option is given or an error occurs.
+If -x is used, returns the exit status of COMMAND.
 $END
 
 #include <config.h>
@@ -46,6 +56,7 @@ $END
 #endif
 
 #include "../bashansi.h"
+#include "../bashintl.h"
 
 #include "../shell.h"
 #include "../jobs.h"
@@ -57,8 +68,7 @@ $END
 #define JSTATE_RUNNING 0x1
 #define JSTATE_STOPPED 0x2
 
-extern int job_control, interactive_shell;
-static int execute_list_with_replacements ();
+static int execute_list_with_replacements __P((WORD_LIST *));
 
 /* The `jobs' command.  Prints outs a list of active jobs.  If the
    argument `-l' is given, then the process id's are printed also.
@@ -75,9 +85,6 @@ jobs_builtin (list)
   int form, execute, state, opt, any_failed, job;
   sigset_t set, oset;
 
-  if (job_control == 0 && interactive_shell == 0)
-    return (EXECUTION_SUCCESS);
-
   execute = any_failed = 0;
   form = JLIST_STANDARD;
   state = JSTATE_ANY;
@@ -99,7 +106,7 @@ jobs_builtin (list)
        case 'x':
          if (form != JLIST_STANDARD)
            {
-             builtin_error ("Other options not allowed with `-x'");
+             builtin_error (_("no other options allowed with `-x'"));
              return (EXECUTION_FAILURE);
            }
          execute++;
@@ -144,9 +151,9 @@ jobs_builtin (list)
       BLOCK_CHILD (set, oset);
       job = get_job_spec (list);
 
-      if ((job == NO_JOB) || !jobs || !jobs[job])
+      if ((job == NO_JOB) || jobs == 0 || get_job_by_jid (job) == 0)
        {
-         builtin_error ("no such job %s", list->word->word);
+         sh_badjob (list->word->word);
          any_failed++;
        }
       else if (job != DUP_JOB)
@@ -164,6 +171,8 @@ execute_list_with_replacements (list)
 {
   register WORD_LIST *l;
   int job, result;
+  COMMAND *command;
+  JOB *j;
 
   /* First do the replacement of job specifications with pids. */
   for (l = list; l; l = l->next)
@@ -173,31 +182,29 @@ execute_list_with_replacements (list)
          job = get_job_spec (l);
 
          /* A bad job spec is not really a job spec! Pass it through. */
-         if (job < 0 || job >= job_slots || !jobs[job])
+         if (INVALID_JOB (job))
            continue;
 
+         j = get_job_by_jid (job);
          free (l->word->word);
-         l->word->word = itos (jobs[job]->pgrp);
+         l->word->word = itos (j->pgrp);
        }
     }
 
   /* Next make a new simple command and execute it. */
   begin_unwind_frame ("jobs_builtin");
-  {
-    COMMAND *command = (COMMAND *)NULL;
 
-    add_unwind_protect (dispose_command, command);
+  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;
 
-    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;
+  add_unwind_protect (dispose_command, command);
+  result = execute_command (command);
+  dispose_command (command);
 
-    result = execute_command (command);
-  }
-
-  run_unwind_frame ("jobs_builtin");
+  discard_unwind_frame ("jobs_builtin");
   return (result);
 }
 #endif /* JOB_CONTROL */
@@ -206,11 +213,19 @@ $BUILTIN disown
 $FUNCTION disown_builtin
 $DEPENDS_ON JOB_CONTROL
 $SHORT_DOC disown [-h] [-ar] [jobspec ...]
-By default, removes each JOBSPEC argument from the table of active jobs.
-If the -h option is given, the job is not removed from the table, but is
-marked so that SIGHUP is not sent to the job if the shell receives a
-SIGHUP.  The -a option, when JOBSPEC is not supplied, means to remove all
-jobs from the job table; the -r option means to remove only running jobs.
+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
+  -r   remove only running jobs
+
+Exit Status:
+Returns success unless an invalid option or JOBSPEC is given.
 $END
 
 #if defined (JOB_CONTROL)
@@ -220,6 +235,7 @@ disown_builtin (list)
 {
   int opt, job, retval, nohup_only, running_jobs, all_jobs;
   sigset_t set, oset;
+  intmax_t pid_value;
 
   nohup_only = running_jobs = all_jobs = 0;
   reset_internal_getopt ();
@@ -257,13 +273,13 @@ disown_builtin (list)
   do
     {
       BLOCK_CHILD (set, oset);
-      job = (list && all_digits(list->word->word))
-               ? get_job_by_pid (atoi(list->word->word), 0)
+      job = (list && legal_number (list->word->word, &pid_value) && pid_value == (pid_t) pid_value)
+               ? get_job_by_pid ((pid_t) pid_value, 0)
                : get_job_spec (list);
 
-      if (job == NO_JOB || jobs == 0 || job < 0 || job >= job_slots || jobs[job] == 0)
+      if (job == NO_JOB || jobs == 0 || INVALID_JOB (job))
        {
-         builtin_error ("%s: no such job", list ? list->word->word : "current");
+         sh_badjob (list ? list->word->word : _("current"));
          retval = EXECUTION_FAILURE;
        }
       else if (nohup_only)