source: trunk/essentials/app-shells/bash/builtins/fg_bg.def

Last change on this file was 3228, checked in by bird, 18 years ago

bash 3.1

File size: 3.9 KB
Line 
1This file is fg_bg.def, from which is created fg_bg.c.
2It implements the builtins "bg" and "fg" in Bash.
3
4Copyright (C) 1987-2005 Free Software Foundation, Inc.
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
8Bash is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
12
13Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License along
19with Bash; see the file COPYING. If not, write to the Free Software
20Foundation, 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]
28Place JOB_SPEC in the foreground, and make it the current job. If
29JOB_SPEC is not present, the shell's notion of the current job is
30used.
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 "../bashintl.h"
43
44#include "../shell.h"
45#include "../jobs.h"
46#include "common.h"
47#include "bashgetopt.h"
48
49#if defined (JOB_CONTROL)
50extern char *this_command_name;
51
52static int fg_bg __P((WORD_LIST *, int));
53
54/* How to bring a job into the foreground. */
55int
56fg_builtin (list)
57 WORD_LIST *list;
58{
59 int fg_bit;
60 register WORD_LIST *t;
61
62 if (job_control == 0)
63 {
64 sh_nojobs ((char *)NULL);
65 return (EXECUTION_FAILURE);
66 }
67
68 if (no_options (list))
69 return (EX_USAGE);
70 list = loptend;
71
72 /* If the last arg on the line is '&', then start this job in the
73 background. Else, fg the job. */
74 for (t = list; t && t->next; t = t->next)
75 ;
76 fg_bit = (t && t->word->word[0] == '&' && t->word->word[1] == '\0') == 0;
77
78 return (fg_bg (list, fg_bit));
79}
80#endif /* JOB_CONTROL */
81
82$BUILTIN bg
83$FUNCTION bg_builtin
84$DEPENDS_ON JOB_CONTROL
85$SHORT_DOC bg [job_spec ...]
86Place each JOB_SPEC in the background, as if it had been started with
87`&'. If JOB_SPEC is not present, the shell's notion of the current
88job is used.
89$END
90
91#if defined (JOB_CONTROL)
92/* How to put a job into the background. */
93int
94bg_builtin (list)
95 WORD_LIST *list;
96{
97 int r;
98
99 if (job_control == 0)
100 {
101 sh_nojobs ((char *)NULL);
102 return (EXECUTION_FAILURE);
103 }
104
105 if (no_options (list))
106 return (EX_USAGE);
107 list = loptend;
108
109 /* This relies on the fact that fg_bg() takes a WORD_LIST *, but only acts
110 on the first member (if any) of that list. */
111 r = EXECUTION_SUCCESS;
112 do
113 {
114 if (fg_bg (list, 0) == EXECUTION_FAILURE)
115 r = EXECUTION_FAILURE;
116 if (list)
117 list = list->next;
118 }
119 while (list);
120
121 return r;
122}
123
124/* How to put a job into the foreground/background. */
125static int
126fg_bg (list, foreground)
127 WORD_LIST *list;
128 int foreground;
129{
130 sigset_t set, oset;
131 int job, status, old_async_pid;
132 JOB *j;
133
134 BLOCK_CHILD (set, oset);
135 job = get_job_spec (list);
136
137 if (INVALID_JOB (job))
138 {
139 if (job != DUP_JOB)
140 sh_badjob (list ? list->word->word : "current");
141
142 goto failure;
143 }
144
145 j = get_job_by_jid (job);
146 /* Or if j->pgrp == shell_pgrp. */
147 if (IS_JOBCONTROL (job) == 0)
148 {
149 builtin_error (_("job %d started without job control"), job + 1);
150 goto failure;
151 }
152
153 if (foreground == 0)
154 {
155 old_async_pid = last_asynchronous_pid;
156 last_asynchronous_pid = j->pgrp; /* As per Posix.2 5.4.2 */
157 }
158
159 status = start_job (job, foreground);
160
161 if (status >= 0)
162 {
163 /* win: */
164 UNBLOCK_CHILD (oset);
165 return (foreground ? status : EXECUTION_SUCCESS);
166 }
167 else
168 {
169 if (foreground == 0)
170 last_asynchronous_pid = old_async_pid;
171
172 failure:
173 UNBLOCK_CHILD (oset);
174 return (EXECUTION_FAILURE);
175 }
176}
177#endif /* JOB_CONTROL */
Note: See TracBrowser for help on using the repository browser.