VirtualBox

source: kBuild/trunk/src/kash/shinstance.h@ 1211

Last change on this file since 1211 was 1211, checked in by bird, 17 years ago

reorg.

File size: 14.0 KB
Line 
1/* $Id: $ */
2/** @file
3 *
4 * The shell instance and it's methods.
5 *
6 * Copyright (c) 2007 knut st. osmundsen <[email protected]>
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 /* alias.c */
117#define ATABSIZE 39
118 struct alias *atab[ATABSIZE];
119
120 /* cd.c */
121 char *curdir; /**< current working directory */
122 char *prevdir; /**< previous working directory */
123 char *cdcomppath;
124 int getpwd_first; /**< static in getpwd. (initialized to 1!) */
125
126 /* error.h */
127 struct jmploc *handler;
128 int exception;
129 int exerrno/* = 0 */; /**< Last exec error */
130 int volatile suppressint;
131 int volatile intpending;
132
133 /* error.c */
134 char errmsg_buf[16]; /**< static in errmsg. (bss) */
135
136 /* eval.h */
137 char *commandname; /**< currently executing command */
138 int exitstatus; /**< exit status of last command */
139 int back_exitstatus;/**< exit status of backquoted command */
140 struct strlist *cmdenviron; /**< environment for builtin command */
141 int funcnest; /**< depth of function calls */
142 int evalskip; /**< set if we are skipping commands */
143 int skipcount; /**< number of levels to skip */
144 int loopnest; /**< current loop nesting level */
145
146 /* eval.c */
147 int vforked;
148
149 /* expand.c */
150 char *expdest; /**< output of current string */
151 struct nodelist *argbackq; /**< list of back quote expressions */
152 struct ifsregion ifsfirst; /**< first struct in list of ifs regions */
153 struct ifsregion *ifslastp; /**< last struct in list */
154 struct arglist exparg; /**< holds expanded arg list */
155 char *expdir; /**< Used by expandmeta. */
156
157 /* exec.h */
158 const char *pathopt; /**< set by padvance */
159
160 /* exec.c */
161 struct tblentry *cmdtable[CMDTABLESIZE];
162 int builtinloc/* = -1*/; /**< index in path of %builtin, or -1 */
163
164 /* input.h */
165 int plinno/* = 1 */;/**< input line number */
166 int parsenleft; /**< number of characters left in input buffer */
167 char *parsenextc; /**< next character in input buffer */
168 int init_editline/* = 0 */; /**< 0 == not setup, 1 == OK, -1 == failed */
169
170 /* input.c */
171 int parselleft; /**< copy of parsefile->lleft */
172 struct parsefile basepf; /**< top level input file */
173 char basebuf[BUFSIZ];/**< buffer for top level input file */
174 struct parsefile *parsefile/* = &basepf*/; /**< current input file */
175#ifndef SMALL
176 EditLine *el; /**< cookie for editline package */
177#endif
178
179 /* jobs.h */
180 pid_t backgndpid/* = -1 */; /**< pid of last background process */
181 int job_warning; /**< user was warned about stopped jobs */
182
183 /* jobs.c */
184 struct job *jobtab; /**< array of jobs */
185 int njobs; /**< size of array */
186 int jobs_invalid; /**< set in child */
187#if JOBS
188 int initialpgrp; /**< pgrp of shell on invocation */
189 int curjob/* = -1*/;/**< current job */
190#endif
191 int ttyfd/* = -1*/;
192 int jobctl; /**< job control enabled / disabled */
193 char *cmdnextc;
194 int cmdnleft;
195
196 /* mail.c */
197#define MAXMBOXES 10
198 int nmboxes; /**< number of mailboxes */
199 time_t mailtime[MAXMBOXES]; /**< times of mailboxes */
200
201 /* main.h */
202 int rootpid; /**< pid of main shell. */
203 int rootshell; /**< true if we aren't a child of the main shell. */
204 struct shinstance *psh_rootshell; /**< The root shell pointer. (!rootshell) */
205
206 /* memalloc.h */
207 char *stacknxt/* = stackbase.space*/;
208 int stacknleft/* = MINSIZE*/;
209 int sstrnleft;
210 int herefd/* = -1 */;
211
212 /* memalloc.c */
213 struct stack_block stackbase;
214 struct stack_block *stackp/* = &stackbase*/;
215 struct stackmark *markp;
216
217 /* myhistedit.h */
218 int displayhist;
219#ifndef SMALL
220 History *hist;
221 EditLine *el;
222#endif
223
224 /* output.h */
225 struct output output;
226 struct output errout;
227 struct output memout;
228 struct output *out1;
229 struct output *out2;
230
231 /* output.c */
232#define OUTBUFSIZ BUFSIZ
233#define MEM_OUT -3 /**< output to dynamically allocated memory */
234
235 /* options.h */
236 struct optent optlist[NOPTS];
237 char *minusc; /**< argument to -c option */
238 char *arg0; /**< $0 */
239 struct shparam shellparam; /**< $@ */
240 char **argptr; /**< argument list for builtin commands */
241 char *optionarg; /**< set by nextopt */
242 char *optptr; /**< used by nextopt */
243
244 /* parse.h */
245 int tokpushback;
246 int whichprompt; /**< 1 == PS1, 2 == PS2 */
247
248 /* parser.c */
249 int noalias/* = 0*/;/**< when set, don't handle aliases */
250 struct heredoc *heredoclist; /**< list of here documents to read */
251 int parsebackquote; /**< nonzero if we are inside backquotes */
252 int doprompt; /**< if set, prompt the user */
253 int needprompt; /**< true if interactive and at start of line */
254 int lasttoken; /**< last token read */
255 char *wordtext; /**< text of last word returned by readtoken */
256 int checkkwd; /**< 1 == check for kwds, 2 == also eat newlines */
257 struct nodelist *backquotelist;
258 union node *redirnode;
259 struct heredoc *heredoc;
260 int quoteflag; /**< set if (part of) last token was quoted */
261 int startlinno; /**< line # where last token started */
262
263 /* redir.c */
264 struct redirtab *redirlist;
265 int fd0_redirected/* = 0*/;
266
267 /* trap.h */
268 int pendingsigs; /**< indicates some signal received */
269
270 /* trap.c */
271 char gotsig[NSIG]; /**< indicates specified signal received */
272 char *trap[NSIG+1]; /**< trap handler commands */
273 char sigmode[NSIG]; /**< current value of signal */
274
275 /* var.h */
276 struct localvar *localvars;
277#if ATTY
278 struct var vatty;
279#endif
280 struct var vifs;
281 struct var vmail;
282 struct var vmpath;
283 struct var vpath;
284#ifdef _MSC_VER
285 struct var vpath2;
286#endif
287 struct var vps1;
288 struct var vps2;
289 struct var vps4;
290#ifndef SMALL
291 struct var vterm;
292 struct var vhistsize;
293#endif
294 struct var voptind;
295#ifdef PC_OS2_LIBPATHS
296 struct var libpath_vars[4];
297#endif
298#ifdef SMALL
299# define VTABSIZE 39
300#else
301# define VTABSIZE 517
302#endif
303 struct var *vartab[VTABSIZE];
304
305 /* builtins.h */
306
307 /* bltin/test.c */
308 char **t_wp;
309 struct t_op const *t_wp_op;
310
311} shinstance;
312
313
314extern shinstance *sh_create_root_shell(shinstance *, int, char **);
315char *sh_getenv(shinstance *, const char *);
316
317/* signals */
318typedef void (*sh_sig_t)(shinstance *, int);
319#ifdef _MSC_VER
320 typedef uint32_t sh_sigset_t;
321#else
322 typedef sigset_t sh_sigset_t;
323#endif
324struct sh_sigaction
325{
326 sh_sig_t sh_handler;
327 sh_sigset_t sh_mask;
328 int sh_flags;
329};
330#define SH_SIG_DFL ((sh_sig_t)SIG_DFL)
331#define SH_SIG_IGN ((sh_sig_t)SIG_IGN)
332
333int sh_sigaction(int, const struct sh_sigaction *, struct sh_sigaction *);
334sh_sig_t sh_signal(shinstance *, int, sh_sig_t);
335void sh_raise_sigint(shinstance *);
336void sh_sigemptyset(sh_sigset_t *);
337int sh_sigprocmask(shinstance *, int, sh_sigset_t const *, sh_sigset_t *);
338void sh_abort(shinstance *);
339
340/* times */
341#include <time.h>
342#ifdef _MSC_VER
343 typedef struct sh_tms
344 {
345 clock_t tms_utime;
346 clock_t tms_stime;
347 clock_t tms_cutime;
348 clock_t tms_cstime;
349 } sh_tms;
350#else
351# include <times.h>
352 typedef struct tms sh_tms;
353#endif
354clock_t sh_times(sh_tms *);
355int sh_sysconf_clk_tck(void);
356
357/* wait / process */
358#ifdef _MSC_VER
359# include <process.h>
360# define WNOHANG 1 /* Don't hang in wait. */
361# define WUNTRACED 2 /* Tell about stopped, untraced children. */
362# define WCONTINUED 4 /* Report a job control continued process. */
363# define _W_INT(w) (*(int *)&(w)) /* Convert union wait to int. */
364# define WCOREFLAG 0200
365# define _WSTATUS(x) (_W_INT(x) & 0177)
366# define _WSTOPPED 0177 /* _WSTATUS if process is stopped */
367# define WIFSTOPPED(x) (_WSTATUS(x) == _WSTOPPED)
368# define WSTOPSIG(x) (_W_INT(x) >> 8)
369# define WIFSIGNALED(x) (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
370# define WTERMSIG(x) (_WSTATUS(x))
371# define WIFEXITED(x) (_WSTATUS(x) == 0)
372# define WEXITSTATUS(x) (_W_INT(x) >> 8)
373# define WIFCONTINUED(x) (x == 0x13) /* 0x13 == SIGCONT */
374# define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
375# define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
376# define W_STOPCODE(sig) ((sig) << 8 | _WSTOPPED)
377#else
378# include <sys/wait.h>
379#endif
380pid_t sh_waitpid(shinstance *, pid_t, int *, int);
381void sh__exit(shinstance *, int);
382int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
383uid_t sh_getuid(shinstance *);
384uid_t sh_geteuid(shinstance *);
385gid_t sh_getgid(shinstance *);
386gid_t sh_getegid(shinstance *);
387pid_t sh_getpid(shinstance *);
388pid_t sh_getpgrp(shinstance *);
389pid_t sh_getpgid(shinstance *, pid_t);
390int sh_setpgid(shinstance *, pid_t, pid_t);
391int sh_killpg(shinstance *, pid_t, int);
392
393/* tc* */
394pid_t sh_tcgetpgrp(shinstance *, int);
395int sh_tcsetpgrp(shinstance *, int, pid_t);
396
397#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette