]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/fg_bg.def
9d379e8453f78960dcd38d0bea513b5d97d23fce
[thirdparty/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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 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 <config.h>
34
35 #include "../bashtypes.h"
36 #include <signal.h>
37
38 #if defined (HAVE_UNISTD_H)
39 # include <unistd.h>
40 #endif
41
42 #include "../shell.h"
43 #include "../jobs.h"
44 #include "common.h"
45
46 #if defined (JOB_CONTROL)
47 extern char *this_command_name;
48
49 static int fg_bg __P((WORD_LIST *, int));
50
51 /* How to bring a job into the foreground. */
52 int
53 fg_builtin (list)
54 WORD_LIST *list;
55 {
56 int fg_bit;
57 register WORD_LIST *t;
58
59 if (job_control == 0)
60 {
61 builtin_error ("no job control");
62 return (EXECUTION_FAILURE);
63 }
64
65 if (no_options (list))
66 return (EX_USAGE);
67
68 /* If the last arg on the line is '&', then start this job in the
69 background. Else, fg the job. */
70 for (t = list; t && t->next; t = t->next)
71 ;
72 fg_bit = (t && t->word->word[0] == '&' && t->word->word[1] == '\0') == 0;
73
74 return (fg_bg (list, fg_bit));
75 }
76 #endif /* JOB_CONTROL */
77
78 $BUILTIN bg
79 $FUNCTION bg_builtin
80 $DEPENDS_ON JOB_CONTROL
81 $SHORT_DOC bg [job_spec]
82 Place JOB_SPEC in the background, as if it had been started with
83 `&'. If JOB_SPEC is not present, the shell's notion of the current
84 job is used.
85 $END
86
87 #if defined (JOB_CONTROL)
88 /* How to put a job into the background. */
89 int
90 bg_builtin (list)
91 WORD_LIST *list;
92 {
93 if (job_control == 0)
94 {
95 builtin_error ("no job control");
96 return (EXECUTION_FAILURE);
97 }
98
99 if (no_options (list))
100 return (EX_USAGE);
101
102 return (fg_bg (list, 0));
103 }
104
105 /* How to put a job into the foreground/background. */
106 static int
107 fg_bg (list, foreground)
108 WORD_LIST *list;
109 int foreground;
110 {
111 sigset_t set, oset;
112 int job, status, old_async_pid;
113
114 BLOCK_CHILD (set, oset);
115 job = get_job_spec (list);
116
117 if (job < 0 || job >= job_slots || jobs[job] == 0)
118 {
119 if (job != DUP_JOB)
120 builtin_error ("%s: no such job", list ? list->word->word : "current");
121
122 goto failure;
123 }
124
125 /* Or if jobs[job]->pgrp == shell_pgrp. */
126 if (IS_JOBCONTROL (job) == 0)
127 {
128 builtin_error ("job %%%d started without job control", job + 1);
129 goto failure;
130 }
131
132 if (foreground == 0)
133 {
134 old_async_pid = last_asynchronous_pid;
135 last_asynchronous_pid = jobs[job]->pgrp; /* As per Posix.2 5.4.2 */
136 }
137
138 status = start_job (job, foreground);
139
140 if (status >= 0)
141 {
142 /* win: */
143 UNBLOCK_CHILD (oset);
144 return (status);
145 }
146 else
147 {
148 if (foreground == 0)
149 last_asynchronous_pid = old_async_pid;
150
151 failure:
152 UNBLOCK_CHILD (oset);
153 return (EXECUTION_FAILURE);
154 }
155 }
156 #endif /* JOB_CONTROL */