e48af38671fcac2cd5aa677e00b2ef4f8b2f97e1
[platform/upstream/bash.git] / builtins / fg_bg.def
1 This file is fg_bg.def, from which is created fg_bg.c.
2 It implements the builtins "bg" and "fg" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 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 fg_bg.c
23
24 $BUILTIN fg
25 $FUNCTION fg_builtin
26 $DEPENDS_ON JOB_CONTROL
27 $SHORT_DOC fg [job_spec]
28 Place JOB_SPEC in the foreground, and make it the current job.  If
29 JOB_SPEC is not present, the shell's notion of the current job is
30 used.
31 $END
32
33 #include <sys/types.h>
34 #include <signal.h>
35 #include "../shell.h"
36 #include "../jobs.h"
37
38 #if defined (JOB_CONTROL)
39 extern char *this_command_name;
40
41 static int fg_bg ();
42
43 /* How to bring a job into the foreground. */
44 int
45 fg_builtin (list)
46      WORD_LIST *list;
47 {
48   int fg_bit = 1;
49   register WORD_LIST *t = list;
50
51   if (!job_control)
52     {
53       builtin_error ("no job control");
54       return (EXECUTION_FAILURE);
55     }
56
57   /* If the last arg on the line is '&', then start this job in the
58      background.  Else, fg the job. */
59
60   while (t && t->next)
61     t = t->next;
62
63   if (t && t->word->word[0] == '&' && !t->word->word[1])
64     fg_bit = 0;
65
66   return (fg_bg (list, fg_bit));
67 }
68 #endif /* JOB_CONTROL */
69
70 $BUILTIN bg
71 $FUNCTION bg_builtin
72 $DEPENDS_ON JOB_CONTROL
73 $SHORT_DOC bg [job_spec]
74 Place JOB_SPEC in the background, as if it had been started with
75 `&'.  If JOB_SPEC is not present, the shell's notion of the current
76 job is used.
77 $END
78
79 #if defined (JOB_CONTROL)
80 /* How to put a job into the background. */
81 int
82 bg_builtin (list)
83      WORD_LIST *list;
84 {
85   if (!job_control)
86     {
87       builtin_error ("no job control");
88       return (EXECUTION_FAILURE);
89     }
90
91   return (fg_bg (list, 0));
92 }
93
94 /* How to put a job into the foreground/background. */
95 static int
96 fg_bg (list, foreground)
97      WORD_LIST *list;
98      int foreground;
99 {
100   sigset_t set, oset;
101   int job, status = EXECUTION_SUCCESS, old_async_pid;
102
103   BLOCK_CHILD (set, oset);
104   job = get_job_spec (list);
105
106   if (job < 0 || job >= job_slots || !jobs[job])
107     {
108       if (job != DUP_JOB)
109         builtin_error ("No such job %s", list ? list->word->word : "");
110
111       goto failure;
112     }
113
114   /* Or if jobs[job]->pgrp == shell_pgrp. */
115   if (!(jobs[job]->flags & J_JOBCONTROL))
116     {
117       builtin_error ("job %%%d started without job control", job + 1);
118       goto failure;
119     }
120
121   if (!foreground)
122     {
123       old_async_pid = last_asynchronous_pid;
124       last_asynchronous_pid = jobs[job]->pgrp;  /* As per Posix.2 5.4.2 */
125     }
126
127   status = start_job (job, foreground);
128
129   if (status >= 0)
130     {
131     /* win: */
132       UNBLOCK_CHILD (oset);
133       return (status);
134     }
135   else
136     {
137       if (!foreground)
138         last_asynchronous_pid = old_async_pid;
139
140     failure:
141       UNBLOCK_CHILD (oset);
142       return (EXECUTION_FAILURE);
143     }
144 }
145 #endif /* JOB_CONTROL */