Imported from ../bash-2.05b.tar.gz.
[platform/upstream/bash.git] / jobs.h
1 /* jobs.h -- structures and stuff used by the jobs.c file. */
2
3 /* Copyright (C) 1993 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6
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
10    version.
11
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
15    for more details.
16
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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
21 #if !defined (_JOBS_H_)
22 #  define _JOBS_H_
23
24 #include "quit.h"
25 #include "siglist.h"
26
27 #include "stdc.h"
28
29 #include "posixwait.h"
30
31 /* Defines controlling the fashion in which jobs are listed. */
32 #define JLIST_STANDARD       0
33 #define JLIST_LONG           1
34 #define JLIST_PID_ONLY       2
35 #define JLIST_CHANGED_ONLY   3
36 #define JLIST_NONINTERACTIVE 4
37
38 /* I looked it up.  For pretty_print_job ().  The real answer is 24. */
39 #define LONGEST_SIGNAL_DESC 24
40
41 /* We keep an array of jobs.  Each entry in the array is a linked list
42    of processes that are piped together.  The first process encountered is
43    the group leader. */
44
45 /* Values for the `running' field of a struct process. */
46 #define PS_DONE         0
47 #define PS_RUNNING      1
48 #define PS_STOPPED      2
49
50 /* Each child of the shell is remembered in a STRUCT PROCESS.  A chain of
51    such structures is a pipeline.  The chain is circular. */
52 typedef struct process {
53   struct process *next; /* Next process in the pipeline.  A circular chain. */
54   pid_t pid;            /* Process ID. */
55   WAIT status;          /* The status of this command as returned by wait. */
56   int running;          /* Non-zero if this process is running. */
57   char *command;        /* The particular program that is running. */
58 } PROCESS;
59
60 /* PRUNNING really means `not exited' */
61 #define PRUNNING(p)     ((p)->running || WIFSTOPPED((p)->status))
62 #define PSTOPPED(p)     (WIFSTOPPED((p)->status))
63 #define PDEADPROC(p)    ((p)->running == PS_DONE)
64
65 /* A description of a pipeline's state. */
66 typedef enum { JRUNNING, JSTOPPED, JDEAD, JMIXED } JOB_STATE;
67 #define JOBSTATE(job) (jobs[(job)]->state)
68
69 #define STOPPED(j)      (jobs[(j)]->state == JSTOPPED)
70 #define RUNNING(j)      (jobs[(j)]->state == JRUNNING)
71 #define DEADJOB(j)      (jobs[(j)]->state == JDEAD)
72
73 /* Values for the FLAGS field in the JOB struct below. */
74 #define J_FOREGROUND 0x01 /* Non-zero if this is running in the foreground.  */
75 #define J_NOTIFIED   0x02 /* Non-zero if already notified about job state.   */
76 #define J_JOBCONTROL 0x04 /* Non-zero if this job started under job control. */
77 #define J_NOHUP      0x08 /* Don't send SIGHUP to job if shell gets SIGHUP. */
78
79 #define IS_FOREGROUND(j)        ((jobs[j]->flags & J_FOREGROUND) != 0)
80 #define IS_NOTIFIED(j)          ((jobs[j]->flags & J_NOTIFIED) != 0)
81 #define IS_JOBCONTROL(j)        ((jobs[j]->flags & J_JOBCONTROL) != 0)
82
83 typedef struct job {
84   char *wd;        /* The working directory at time of invocation. */
85   PROCESS *pipe;   /* The pipeline of processes that make up this job. */
86   pid_t pgrp;      /* The process ID of the process group (necessary). */
87   JOB_STATE state; /* The state that this job is in. */
88   int flags;       /* Flags word: J_NOTIFIED, J_FOREGROUND, or J_JOBCONTROL. */
89 #if defined (JOB_CONTROL)
90   COMMAND *deferred;    /* Commands that will execute when this job is done. */
91   sh_vptrfunc_t *j_cleanup; /* Cleanup function to call when job marked JDEAD */
92   PTR_T cleanarg;       /* Argument passed to (*j_cleanup)() */
93 #endif /* JOB_CONTROL */
94 } JOB;
95
96 #define NO_JOB  -1      /* An impossible job array index. */
97 #define DUP_JOB -2      /* A possible return value for get_job_spec (). */
98
99 /* A value which cannot be a process ID. */
100 #define NO_PID (pid_t)-1
101
102 /* System calls. */
103 #if !defined (HAVE_UNISTD_H)
104 extern pid_t fork (), getpid (), getpgrp ();
105 #endif /* !HAVE_UNISTD_H */
106
107 /* Stuff from the jobs.c file. */
108 extern pid_t original_pgrp, shell_pgrp, pipeline_pgrp;
109 extern pid_t last_made_pid, last_asynchronous_pid;
110 extern int current_job, previous_job;
111 extern int asynchronous_notification;
112 extern JOB **jobs;
113 extern int job_slots;
114
115 extern void making_children __P((void));
116 extern void stop_making_children __P((void));
117 extern void cleanup_the_pipeline __P((void));
118 extern void save_pipeline __P((int));
119 extern void restore_pipeline __P((int));
120 extern void start_pipeline __P((void));
121 extern int stop_pipeline __P((int, COMMAND *));
122
123 extern void delete_job __P((int, int));
124 extern void nohup_job __P((int));
125 extern void delete_all_jobs __P((int));
126 extern void nohup_all_jobs __P((int));
127
128 extern int count_all_jobs __P((void));
129
130 extern void terminate_current_pipeline __P((void));
131 extern void terminate_stopped_jobs __P((void));
132 extern void hangup_all_jobs __P((void));
133 extern void kill_current_pipeline __P((void));
134
135 #if defined (__STDC__) && defined (pid_t)
136 extern int get_job_by_pid __P((int, int));
137 extern void describe_pid __P((int));
138 #else
139 extern int get_job_by_pid __P((pid_t, int));
140 extern void describe_pid __P((pid_t));
141 #endif
142
143 extern void list_one_job __P((JOB *, int, int, int));
144 extern void list_all_jobs __P((int));
145 extern void list_stopped_jobs __P((int));
146 extern void list_running_jobs __P((int));
147
148 extern pid_t make_child __P((char *, int));
149
150 extern int get_tty_state __P((void));
151 extern int set_tty_state __P((void));
152
153 extern int wait_for_single_pid __P((pid_t));
154 extern void wait_for_background_pids __P((void));
155 extern int wait_for __P((pid_t));
156 extern int wait_for_job __P((int));
157
158 extern void notify_and_cleanup __P((void));
159 extern void reap_dead_jobs __P((void));
160 extern int start_job __P((int, int));
161 extern int kill_pid __P((pid_t, int, int));
162 extern int initialize_job_control __P((int));
163 extern void initialize_job_signals __P((void));
164 extern int give_terminal_to __P((pid_t, int));
165
166 extern void set_sigwinch_handler __P((void));
167 extern void unset_sigwinch_handler __P((void));
168
169 extern void unfreeze_jobs_list __P((void));
170 extern int set_job_control __P((int));
171 extern void without_job_control __P((void));
172 extern void end_job_control __P((void));
173 extern void restart_job_control __P((void));
174 extern void set_sigchld_handler __P((void));
175 extern void ignore_tty_job_signals __P((void));
176 extern void default_tty_job_signals __P((void));
177
178 #if defined (JOB_CONTROL)
179 extern int job_control;
180 #endif
181
182 #endif /* _JOBS_H_ */