1 /* jobs.h -- structures and stuff used by the jobs.c file. */
3 /* Copyright (C) 1993 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #if !defined (_JOBS_H_)
29 /* Defines controlling the fashion in which jobs are listed. */
30 #define JLIST_STANDARD 0
32 #define JLIST_PID_ONLY 2
33 #define JLIST_CHANGED_ONLY 3
34 #define JLIST_NONINTERACTIVE 4
36 /* If _POSIX_VERSION is not defined, we assume that <sys/wait.h> defines
37 a `union wait' and various macros used to manipulate it. Look in
38 bashwait.h for the things we expect to find. */
39 #if defined (HAVE_SYS_WAIT_H)
40 # include <sys/wait.h>
41 #else /* !HAVE_SYS_WAIT_H */
42 # if !defined (_POSIX_VERSION)
43 # include "bashwait.h"
45 #endif /* !HAVE_SYS_WAIT_H */
47 /* How to get the status of a job. For Posix, this is just an
48 int, but for other systems we have to crack the union wait. */
49 #if !defined (_POSIX_VERSION)
50 typedef union wait WAIT;
51 # define WSTATUS(t) (t.w_status)
52 #else /* _POSIX_VERSION */
54 # define WSTATUS(t) (t)
55 #endif /* _POSIX_VERSION */
57 /* Make sure that parameters to wait3 are defined. */
58 #if !defined (WNOHANG)
63 /* More Posix P1003.1 definitions. In the POSIX versions, the parameter is
64 passed as an `int', in the non-POSIX version, as `union wait'. */
65 #if defined (_POSIX_VERSION)
67 # if !defined (WSTOPSIG)
68 # define WSTOPSIG(s) ((s) >> 8)
69 # endif /* !WSTOPSIG */
71 # if !defined (WTERMSIG)
72 # define WTERMSIG(s) ((s) & 0177)
73 # endif /* !WTERMSIG */
75 # if !defined (WEXITSTATUS)
76 # define WEXITSTATUS(s) ((s) >> 8)
77 # endif /* !WEXITSTATUS */
79 # if !defined (WIFSTOPPED)
80 # define WIFSTOPPED(s) (((s) & 0177) == 0177)
81 # endif /* !WIFSTOPPED */
83 # if !defined (WIFEXITED)
84 # define WIFEXITED(s) (((s) & 0377) == 0)
85 # endif /* !WIFEXITED */
87 # if !defined (WIFSIGNALED)
88 # define WIFSIGNALED(s) (!WIFSTOPPED(s) && !WIFEXITED(s))
89 # endif /* !WIFSIGNALED */
91 # if !defined (WIFCORED)
92 # define WIFCORED(s) ((s) & 0200)
93 # endif /* !WIFCORED */
95 #else /* !_POSIX_VERSION */
97 # if !defined (WSTOPSIG)
98 # define WSTOPSIG(s) ((s).w_stopsig)
99 # endif /* !WSTOPSIG */
101 # if !defined (WTERMSIG)
102 # define WTERMSIG(s) ((s).w_termsig)
103 # endif /* !WTERMSIG */
105 # if !defined (WEXITSTATUS)
106 # define WEXITSTATUS(s) ((s).w_retcode)
107 # endif /* !WEXITSTATUS */
109 # if !defined (WIFCORED)
110 # define WIFCORED(s) ((s).w_coredump)
111 # endif /* !WIFCORED */
113 #endif /* !_POSIX_VERSION */
115 /* I looked it up. For pretty_print_job (). The real answer is 24. */
116 #define LONGEST_SIGNAL_DESC 24
118 /* We keep an array of jobs. Each entry in the array is a linked list
119 of processes that are piped together. The first process encountered is
122 /* Each child of the shell is remembered in a STRUCT PROCESS. A chain of
123 such structures is a pipeline. The chain is circular. */
124 typedef struct process {
125 struct process *next; /* Next process in the pipeline. A circular chain. */
126 pid_t pid; /* Process ID. */
127 WAIT status; /* The status of this command as returned by wait. */
128 int running; /* Non-zero if this process is running. */
129 char *command; /* The particular program that is running. */
132 /* A description of a pipeline's state. */
133 typedef enum { JRUNNING, JSTOPPED, JDEAD, JMIXED } JOB_STATE;
134 #define JOBSTATE(job) (jobs[(job)]->state)
136 #define STOPPED(j) (jobs[(j)]->state == JSTOPPED)
137 #define RUNNING(j) (jobs[(j)]->state == JRUNNING)
138 #define DEADJOB(j) (jobs[(j)]->state == JDEAD)
140 /* Values for the FLAGS field in the JOB struct below. */
141 #define J_FOREGROUND 0x01 /* Non-zero if this is running in the foreground. */
142 #define J_NOTIFIED 0x02 /* Non-zero if already notified about job state. */
143 #define J_JOBCONTROL 0x04 /* Non-zero if this job started under job control. */
144 #define J_NOHUP 0x08 /* Don't send SIGHUP to job if shell gets SIGHUP. */
146 #define IS_FOREGROUND(j) ((jobs[j]->flags & J_FOREGROUND) != 0)
147 #define IS_NOTIFIED(j) ((jobs[j]->flags & J_NOTIFIED) != 0)
148 #define IS_JOBCONTROL(j) ((jobs[j]->flags & J_JOBCONTROL) != 0)
151 char *wd; /* The working directory at time of invocation. */
152 PROCESS *pipe; /* The pipeline of processes that make up this job. */
153 pid_t pgrp; /* The process ID of the process group (necessary). */
154 JOB_STATE state; /* The state that this job is in. */
155 int flags; /* Flags word: J_NOTIFIED, J_FOREGROUND, or J_JOBCONTROL. */
156 #if defined (JOB_CONTROL)
157 COMMAND *deferred; /* Commands that will execute when this job is done. */
158 VFunction *j_cleanup; /* Cleanup function to call when job marked JDEAD */
159 PTR_T cleanarg; /* Argument passed to (*j_cleanup)() */
160 #endif /* JOB_CONTROL */
163 #define NO_JOB -1 /* An impossible job array index. */
164 #define DUP_JOB -2 /* A possible return value for get_job_spec (). */
166 /* A value which cannot be a process ID. */
167 #define NO_PID (pid_t)-1
170 #if !defined (HAVE_UNISTD_H)
171 extern pid_t fork (), getpid (), getpgrp ();
172 #endif /* !HAVE_UNISTD_H */
174 /* Stuff from the jobs.c file. */
175 extern pid_t original_pgrp, shell_pgrp, pipeline_pgrp;
176 extern pid_t last_made_pid, last_asynchronous_pid;
177 extern int current_job, previous_job;
178 extern int asynchronous_notification;
180 extern int job_slots;
182 extern void making_children __P((void));
183 extern void stop_making_children __P((void));
184 extern void cleanup_the_pipeline __P((void));
185 extern void save_pipeline __P((int));
186 extern void restore_pipeline __P((int));
187 extern void start_pipeline __P((void));
188 extern int stop_pipeline __P((int, COMMAND *));
189 extern void delete_job __P((int));
190 extern void nohup_job __P((int));
192 extern void terminate_current_pipeline __P((void));
193 extern void terminate_stopped_jobs __P((void));
194 extern void hangup_all_jobs __P((void));
195 extern void kill_current_pipeline __P((void));
197 #if defined (__STDC__) && defined (pid_t)
198 extern void describe_pid __P((int));
200 extern void describe_pid __P((pid_t));
203 extern void list_one_job __P((JOB *, int, int, int));
204 extern void list_all_jobs __P((int));
205 extern void list_stopped_jobs __P((int));
206 extern void list_running_jobs __P((int));
208 extern pid_t make_child __P((char *, int));
209 extern int get_tty_state __P((void));
210 extern int set_tty_state __P((void));
212 extern int wait_for_single_pid __P((pid_t));
213 extern void wait_for_background_pids __P((void));
214 extern int wait_for __P((pid_t));
215 extern int wait_for_job __P((int));
217 extern void notify_and_cleanup __P((void));
218 extern void reap_dead_jobs __P((void));
219 extern int start_job __P((int, int));
220 extern int kill_pid __P((pid_t, int, int));
221 extern int initialize_jobs __P((void));
222 extern void initialize_job_signals __P((void));
223 extern int give_terminal_to __P((pid_t));
225 extern void set_sigwinch_handler __P((void));
226 extern void unset_sigwinch_handler __P((void));
228 extern void unfreeze_jobs_list __P((void));
229 extern int set_job_control __P((int));
230 extern void without_job_control __P((void));
231 extern void end_job_control __P((void));
232 extern void restart_job_control __P((void));
233 extern void set_sigchld_handler __P((void));
234 extern void ignore_tty_job_signals __P((void));
235 extern void default_tty_job_signals __P((void));
237 #if defined (JOB_CONTROL)
238 extern int job_control;
241 #endif /* _JOBS_H_ */