source: trunk/src/kash/shinstance.h@ 1207

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

jobs and other stuff.

File size: 12.4 KB
Line 
1/* $Id: $ */
2/** @file
3 *
4 * The shell instance and it's methods.
5 *
6 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
7 *
8 *
9 * This file is part of kBuild.
10 *
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#ifndef ___shinstance_h___
28#define ___shinstance_h___
29
30#include <stdio.h> /* BUFSIZ */
31#include <signal.h> /* NSIG */
32
33#include "shtypes.h"
34#include "shthread.h"
35#include "shfile.h"
36#include "output.h"
37#include "options.h"
38
39#include "expand.h"
40#include "exec.h"
41#include "var.h"
42
43/* memalloc.c */
44#define MINSIZE 504 /* minimum size of a block */
45struct stack_block {
46 struct stack_block *prev;
47 char space[MINSIZE];
48};
49
50/* input.c */
51struct strpush {
52 struct strpush *prev; /* preceding string on stack */
53 char *prevstring;
54 int prevnleft;
55 int prevlleft;
56 struct alias *ap; /* if push was associated with an alias */
57};
58
59/*
60 * The parsefile structure pointed to by the global variable parsefile
61 * contains information about the current file being read.
62 */
63struct parsefile {
64 struct parsefile *prev; /* preceding file on stack */
65 int linno; /* current line */
66 int fd; /* file descriptor (or -1 if string) */
67 int nleft; /* number of chars left in this line */
68 int lleft; /* number of chars left in this buffer */
69 char *nextc; /* next char in buffer */
70 char *buf; /* input buffer */
71 struct strpush *strpush; /* for pushing strings at this level */
72 struct strpush basestrpush; /* so pushing one is fast */
73};
74
75/* exec.c */
76#define CMDTABLESIZE 31 /* should be prime */
77#define ARB 1 /* actual size determined at run time */
78
79struct tblentry {
80 struct tblentry *next; /* next entry in hash chain */
81 union param param; /* definition of builtin function */
82 short cmdtype; /* index identifying command */
83 char rehash; /* if set, cd done since entry created */
84 char cmdname[ARB]; /* name of command */
85};
86
87/* expand.c */
88/*
89 * Structure specifying which parts of the string should be searched
90 * for IFS characters.
91 */
92struct ifsregion {
93 struct ifsregion *next; /* next region in list */
94 int begoff; /* offset of start of region */
95 int endoff; /* offset of end of region */
96 int inquotes; /* search for nul bytes only */
97};
98
99
100/**
101 * A shell instance.
102 *
103 * This is the core structure of the shell, it contains all
104 * the data associated with a shell process except that it's
105 * running in a thread and not a separate process.
106 */
107typedef struct shinstance
108{
109 struct shinstance *next; /**< The next shell instance. */
110 struct shinstance *prev; /**< The previous shell instance. */
111 struct shinstance *parent; /**< The parent shell instance. */
112 pid_t pid; /**< The (fake) process id of this shell instance. */
113 shtid tid; /**< The thread identifier of the thread for this shell. */
114 shfdtab fdtab; /**< The file descriptor table. */
115
116 /* error.h */
117 struct jmploc *handler;
118 int exception;
119 int exerrno/* = 0 */; /**< Last exec error */
120 int volatile suppressint;
121 int volatile intpending;
122
123 /* main.h */
124 int rootpid; /**< pid of main shell. */
125 int rootshell; /**< true if we aren't a child of the main shell. */
126 struct shinstance *psh_rootshell; /**< The root shell pointer. (!rootshell) */
127
128 /* trap.h */
129 int pendingsigs;
130
131 /* parse.h */
132 int tokpushback;
133 int whichprompt; /**< 1 == PS1, 2 == PS2 */
134
135 /* output.h */
136 struct output output;
137 struct output errout;
138 struct output memout;
139 struct output *out1;
140 struct output *out2;
141 /* output.c */
142#define OUTBUFSIZ BUFSIZ
143#define MEM_OUT -3 /**< output to dynamically allocated memory */
144
145 /* options.h */
146 struct optent optlist[NOPTS];
147 char *minusc; /**< argument to -c option */
148 char *arg0; /**< $0 */
149 struct shparam shellparam; /**< $@ */
150 char **argptr; /**< argument list for builtin commands */
151 char *optionarg; /**< set by nextopt */
152 char *optptr; /**< used by nextopt */
153
154 /* var.h */
155 struct localvar *localvars;
156#if ATTY
157 struct var vatty;
158#endif
159 struct var vifs;
160 struct var vmail;
161 struct var vmpath;
162 struct var vpath;
163#ifdef _MSC_VER
164 struct var vpath2;
165#endif
166 struct var vps1;
167 struct var vps2;
168 struct var vps4;
169#ifndef SMALL
170 struct var vterm;
171 struct var vtermcap;
172 struct var vhistsize;
173#endif
174
175 /* myhistedit.h */
176 int displayhist;
177#ifndef SMALL
178 History *hist;
179 EditLine *el;
180#endif
181
182 /* memalloc.h */
183 char *stacknxt/* = stackbase.space*/;
184 int stacknleft/* = MINSIZE*/;
185 int sstrnleft;
186 int herefd/* = -1 */;
187
188 /* memalloc.c */
189 struct stack_block stackbase;
190 struct stack_block *stackp/* = &stackbase*/;
191 struct stackmark *markp;
192
193 /* jobs.h */
194 pid_t backgndpid/* = -1 */; /**< pid of last background process */
195 int job_warning; /**< user was warned about stopped jobs */
196
197 /* jobs.c */
198 struct job *jobtab; /**< array of jobs */
199 int njobs; /**< size of array */
200 int jobs_invalid; /**< set in child */
201#if JOBS
202 int initialpgrp; /**< pgrp of shell on invocation */
203 int curjob/* = -1*/;/**< current job */
204#endif
205 int ttyfd/* = -1*/;
206 int jobctl; /**< job control enabled / disabled */
207 char *cmdnextc;
208 int cmdnleft;
209
210 /* input.h */
211 int plinno/* = 1 */;/**< input line number */
212 int parsenleft; /**< number of characters left in input buffer */
213 char *parsenextc; /**< next character in input buffer */
214 int init_editline/* = 0 */; /**< 0 == not setup, 1 == OK, -1 == failed */
215
216 /* input.c */
217 int parselleft; /**< copy of parsefile->lleft */
218 struct parsefile basepf; /**< top level input file */
219 char basebuf[BUFSIZ];/**< buffer for top level input file */
220 struct parsefile *parsefile/* = &basepf*/; /**< current input file */
221#ifndef SMALL
222 EditLine *el; /**< cookie for editline package */
223#endif
224
225
226 /* exec.h */
227 const char *pathopt; /**< set by padvance */
228
229 /* exec.c */
230 struct tblentry *cmdtable[CMDTABLESIZE];
231 int builtinloc/* = -1*/; /**< index in path of %builtin, or -1 */
232
233
234 /* eval.h */
235 char *commandname; /**< currently executing command */
236 int exitstatus; /**< exit status of last command */
237 int back_exitstatus;/**< exit status of backquoted command */
238 struct strlist *cmdenviron; /**< environment for builtin command */
239 int funcnest; /**< depth of function calls */
240 int evalskip; /**< set if we are skipping commands */
241 int skipcount; /**< number of levels to skip */
242 int loopnest; /**< current loop nesting level */
243
244 /* builtins.h */
245
246 /* alias.c */
247#define ATABSIZE 39
248 struct alias *atab[ATABSIZE];
249
250 /* cd.c */
251 char *curdir; /**< current working directory */
252 char *prevdir; /**< previous working directory */
253 char *cdcomppath;
254 int getpwd_first; /**< static in getpwd. (initialized to 1!) */
255
256 /* error.c */
257 char errmsg_buf[16]; /**< static in errmsg. (bss) */
258
259 /* eval.c */
260 int vforked;
261
262 /* expand.c */
263 char *expdest; /**< output of current string */
264 struct nodelist *argbackq; /**< list of back quote expressions */
265 struct ifsregion ifsfirst; /**< first struct in list of ifs regions */
266 struct ifsregion *ifslastp; /**< last struct in list */
267 struct arglist exparg; /**< holds expanded arg list */
268 char *expdir; /**< Used by expandmeta. */
269
270 /* mail.c */
271#define MAXMBOXES 10
272 int nmboxes; /**< number of mailboxes */
273 time_t mailtime[MAXMBOXES]; /**< times of mailboxes */
274
275 /* bltin/test.c */
276 char **t_wp;
277 struct t_op const *t_wp_op;
278
279 /* trap.c */
280 char gotsig[NSIG]; /**< indicates specified signal received */
281
282} shinstance;
283
284
285extern shinstance *sh_create_root_shell(shinstance *inherit, int argc, char **argv);
286char *sh_getenv(shinstance *, const char *);
287
288/* signals */
289#include <signal.h>
290#ifdef _MSC_VER
291 typedef uint32_t sh_sigset_t;
292#else
293 typedef sigset_t sh_sigset_t;
294#endif
295
296typedef void (*sh_handler)(int);
297sh_handler sh_signal(shinstance *, int, sh_handler handler);
298void sh_raise_sigint(shinstance *);
299void sh_sigemptyset(sh_sigset_t *set);
300int sh_sigprocmask(shinstance *, int op, sh_sigset_t const *new, sh_sigset_t *old);
301void sh_abort(shinstance *);
302
303/* times */
304#include <time.h>
305#ifdef _MSC_VER
306 typedef struct sh_tms
307 {
308 clock_t tms_utime;
309 clock_t tms_stime;
310 clock_t tms_cutime;
311 clock_t tms_cstime;
312 } sh_tms;
313#else
314# include <times.h>
315 typedef struct tms sh_tms;
316#endif
317clock_t sh_times(sh_tms *);
318int sh_sysconf_clk_tck(void);
319
320/* wait / process */
321#ifdef _MSC_VER
322# include <process.h>
323# define WNOHANG 1 /* Don't hang in wait. */
324# define WUNTRACED 2 /* Tell about stopped, untraced children. */
325# define WCONTINUED 4 /* Report a job control continued process. */
326# define _W_INT(w) (*(int *)&(w)) /* Convert union wait to int. */
327# define WCOREFLAG 0200
328# define _WSTATUS(x) (_W_INT(x) & 0177)
329# define _WSTOPPED 0177 /* _WSTATUS if process is stopped */
330# define WIFSTOPPED(x) (_WSTATUS(x) == _WSTOPPED)
331# define WSTOPSIG(x) (_W_INT(x) >> 8)
332# define WIFSIGNALED(x) (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
333# define WTERMSIG(x) (_WSTATUS(x))
334# define WIFEXITED(x) (_WSTATUS(x) == 0)
335# define WEXITSTATUS(x) (_W_INT(x) >> 8)
336# define WIFCONTINUED(x) (x == 0x13) /* 0x13 == SIGCONT */
337# define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
338# define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
339# define W_STOPCODE(sig) ((sig) << 8 | _WSTOPPED)
340#else
341# include <sys/wait.h>
342#endif
343pid_t sh_waitpid(shinstance *, pid_t, int *, int);
344void sh__exit(shinstance *, int);
345int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
346uid_t sh_getuid(shinstance *);
347uid_t sh_geteuid(shinstance *);
348gid_t sh_getgid(shinstance *);
349gid_t sh_getegid(shinstance *);
350pid_t sh_getpid(shinstance *);
351pid_t sh_getpgrp(shinstance *);
352pid_t sh_getpgid(shinstance *, pid_t);
353int sh_setpgid(shinstance *, pid_t, pid_t);
354int sh_killpg(shinstance *, pid_t, int);
355
356/* tc* */
357pid_t sh_tcgetpgrp(shinstance *, int);
358int sh_tcsetpgrp(shinstance *, int, pid_t);
359
360#endif
Note: See TracBrowser for help on using the repository browser.