VirtualBox

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

Last change on this file since 1784 was 1784, checked in by bird, 16 years ago

kash: hacking.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 15.4 KB
Line 
1/* $Id: shinstance.h 1784 2008-09-13 03:27:59Z bird $ */
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#ifndef _MSC_VER
33# include <termios.h>
34# include <sys/ioctl.h>
35# include <sys/resource.h>
36#endif
37#include <errno.h>
38#ifdef _MSC_VER
39# define EWOULDBLOCK 512
40#endif
41
42#include "shtypes.h"
43#include "shthread.h"
44#include "shfile.h"
45#include "shell.h"
46#include "output.h"
47#include "options.h"
48
49#include "expand.h"
50#include "exec.h"
51#include "var.h"
52
53#ifdef _MSC_VER
54# define strcasecmp stricmp
55# define strncasecmp strnicmp
56#endif
57
58
59/* memalloc.c */
60#define MINSIZE 504 /* minimum size of a block */
61struct stack_block {
62 struct stack_block *prev;
63 char space[MINSIZE];
64};
65
66/* input.c */
67struct strpush {
68 struct strpush *prev; /* preceding string on stack */
69 char *prevstring;
70 int prevnleft;
71 int prevlleft;
72 struct alias *ap; /* if push was associated with an alias */
73};
74
75/*
76 * The parsefile structure pointed to by the global variable parsefile
77 * contains information about the current file being read.
78 */
79struct parsefile {
80 struct parsefile *prev; /* preceding file on stack */
81 int linno; /* current line */
82 int fd; /* file descriptor (or -1 if string) */
83 int nleft; /* number of chars left in this line */
84 int lleft; /* number of chars left in this buffer */
85 char *nextc; /* next char in buffer */
86 char *buf; /* input buffer */
87 struct strpush *strpush; /* for pushing strings at this level */
88 struct strpush basestrpush; /* so pushing one is fast */
89};
90
91/* exec.c */
92#define CMDTABLESIZE 31 /* should be prime */
93#define ARB 1 /* actual size determined at run time */
94
95struct tblentry {
96 struct tblentry *next; /* next entry in hash chain */
97 union param param; /* definition of builtin function */
98 short cmdtype; /* index identifying command */
99 char rehash; /* if set, cd done since entry created */
100 char cmdname[ARB]; /* name of command */
101};
102
103/* expand.c */
104/*
105 * Structure specifying which parts of the string should be searched
106 * for IFS characters.
107 */
108struct ifsregion {
109 struct ifsregion *next; /* next region in list */
110 int begoff; /* offset of start of region */
111 int endoff; /* offset of end of region */
112 int inquotes; /* search for nul bytes only */
113};
114
115
116/**
117 * A shell instance.
118 *
119 * This is the core structure of the shell, it contains all
120 * the data associated with a shell process except that it's
121 * running in a thread and not a separate process.
122 */
123struct shinstance
124{
125 struct shinstance *next; /**< The next shell instance. */
126 struct shinstance *prev; /**< The previous shell instance. */
127 struct shinstance *parent; /**< The parent shell instance. */
128 pid_t pid; /**< The (fake) process id of this shell instance. */
129 shtid tid; /**< The thread identifier of the thread for this shell. */
130 shfdtab fdtab; /**< The file descriptor table. */
131 shsigaction_t sigactions[NSIG]; /**< The signal actions registered with this shell instance. */
132
133 /* alias.c */
134#define ATABSIZE 39
135 struct alias *atab[ATABSIZE];
136
137 /* cd.c */
138 char *curdir; /**< current working directory */
139 char *prevdir; /**< previous working directory */
140 char *cdcomppath;
141 int getpwd_first; /**< static in getpwd. (initialized to 1!) */
142
143 /* error.h */
144 struct jmploc *handler;
145 int exception;
146 int exerrno/* = 0 */; /**< Last exec error */
147 int volatile suppressint;
148 int volatile intpending;
149
150 /* error.c */
151 char errmsg_buf[16]; /**< static in errmsg. (bss) */
152
153 /* eval.h */
154 char *commandname; /**< currently executing command */
155 int exitstatus; /**< exit status of last command */
156 int back_exitstatus;/**< exit status of backquoted command */
157 struct strlist *cmdenviron; /**< environment for builtin command */
158 int funcnest; /**< depth of function calls */
159 int evalskip; /**< set if we are skipping commands */
160 int skipcount; /**< number of levels to skip */
161 int loopnest; /**< current loop nesting level */
162
163 /* eval.c */
164 int vforked;
165
166 /* expand.c */
167 char *expdest; /**< output of current string */
168 struct nodelist *argbackq; /**< list of back quote expressions */
169 struct ifsregion ifsfirst; /**< first struct in list of ifs regions */
170 struct ifsregion *ifslastp; /**< last struct in list */
171 struct arglist exparg; /**< holds expanded arg list */
172 char *expdir; /**< Used by expandmeta. */
173
174 /* exec.h */
175 const char *pathopt; /**< set by padvance */
176
177 /* exec.c */
178 struct tblentry *cmdtable[CMDTABLESIZE];
179 int builtinloc/* = -1*/; /**< index in path of %builtin, or -1 */
180
181 /* input.h */
182 int plinno/* = 1 */;/**< input line number */
183 int parsenleft; /**< number of characters left in input buffer */
184 char *parsenextc; /**< next character in input buffer */
185 int init_editline/* = 0 */; /**< 0 == not setup, 1 == OK, -1 == failed */
186
187 /* input.c */
188 int parselleft; /**< copy of parsefile->lleft */
189 struct parsefile basepf; /**< top level input file */
190 char basebuf[BUFSIZ];/**< buffer for top level input file */
191 struct parsefile *parsefile/* = &basepf*/; /**< current input file */
192#ifndef SMALL
193 EditLine *el; /**< cookie for editline package */
194#endif
195
196 /* jobs.h */
197 pid_t backgndpid/* = -1 */; /**< pid of last background process */
198 int job_warning; /**< user was warned about stopped jobs */
199
200 /* jobs.c */
201 struct job *jobtab; /**< array of jobs */
202 int njobs; /**< size of array */
203 int jobs_invalid; /**< set in child */
204 int initialpgrp; /**< pgrp of shell on invocation */
205 int curjob/* = -1*/;/**< current job */
206 int ttyfd/* = -1*/;
207 int jobctl; /**< job control enabled / disabled */
208 char *cmdnextc;
209 int cmdnleft;
210
211
212 /* mail.c */
213#define MAXMBOXES 10
214 int nmboxes; /**< number of mailboxes */
215 time_t mailtime[MAXMBOXES]; /**< times of mailboxes */
216
217 /* main.h */
218 int rootpid; /**< pid of main shell. */
219 int rootshell; /**< true if we aren't a child of the main shell. */
220 struct shinstance *psh_rootshell; /**< The root shell pointer. (!rootshell) */
221
222 /* memalloc.h */
223 char *stacknxt/* = stackbase.space*/;
224 int stacknleft/* = MINSIZE*/;
225 int sstrnleft;
226 int herefd/* = -1 */;
227
228 /* memalloc.c */
229 struct stack_block stackbase;
230 struct stack_block *stackp/* = &stackbase*/;
231 struct stackmark *markp;
232
233 /* myhistedit.h */
234 int displayhist;
235#ifndef SMALL
236 History *hist;
237 EditLine *el;
238#endif
239
240 /* output.h */
241 struct output output;
242 struct output errout;
243 struct output memout;
244 struct output *out1;
245 struct output *out2;
246
247 /* output.c */
248#define OUTBUFSIZ BUFSIZ
249#define MEM_OUT -3 /**< output to dynamically allocated memory */
250
251 /* options.h */
252 struct optent optlist[NOPTS];
253 char *minusc; /**< argument to -c option */
254 char *arg0; /**< $0 */
255 struct shparam shellparam; /**< $@ */
256 char **argptr; /**< argument list for builtin commands */
257 char *optionarg; /**< set by nextopt */
258 char *optptr; /**< used by nextopt */
259
260 /* parse.h */
261 int tokpushback;
262 int whichprompt; /**< 1 == PS1, 2 == PS2 */
263
264 /* parser.c */
265 int noalias/* = 0*/;/**< when set, don't handle aliases */
266 struct heredoc *heredoclist; /**< list of here documents to read */
267 int parsebackquote; /**< nonzero if we are inside backquotes */
268 int doprompt; /**< if set, prompt the user */
269 int needprompt; /**< true if interactive and at start of line */
270 int lasttoken; /**< last token read */
271 char *wordtext; /**< text of last word returned by readtoken */
272 int checkkwd; /**< 1 == check for kwds, 2 == also eat newlines */
273 struct nodelist *backquotelist;
274 union node *redirnode;
275 struct heredoc *heredoc;
276 int quoteflag; /**< set if (part of) last token was quoted */
277 int startlinno; /**< line # where last token started */
278
279 /* redir.c */
280 struct redirtab *redirlist;
281 int fd0_redirected/* = 0*/;
282
283 /* trap.h */
284 int pendingsigs; /**< indicates some signal received */
285
286 /* trap.c */
287 char gotsig[NSIG]; /**< indicates specified signal received */
288 char *trap[NSIG+1]; /**< trap handler commands */
289 char sigmode[NSIG]; /**< current value of signal */
290
291 /* var.h */
292 struct localvar *localvars;
293 struct var vatty;
294 struct var vifs;
295 struct var vmail;
296 struct var vmpath;
297 struct var vpath;
298#ifdef _MSC_VER
299 struct var vpath2;
300#endif
301 struct var vps1;
302 struct var vps2;
303 struct var vps4;
304#ifndef SMALL
305 struct var vterm;
306 struct var vhistsize;
307#endif
308 struct var voptind;
309#ifdef PC_OS2_LIBPATHS
310 struct var libpath_vars[4];
311#endif
312#ifdef SMALL
313# define VTABSIZE 39
314#else
315# define VTABSIZE 517
316#endif
317 struct var *vartab[VTABSIZE];
318
319 /* builtins.h */
320
321 /* bltin/test.c */
322 char **t_wp;
323 struct t_op const *t_wp_op;
324
325};
326
327
328extern shinstance *sh_create_root_shell(shinstance *, int, char **);
329
330/* environment & pwd.h */
331char *sh_getenv(shinstance *, const char *);
332char **sh_environ(shinstance *);
333const char *sh_gethomedir(shinstance *, const char *);
334
335/* signals */
336#define SH_SIG_UNK ((shsig_t)(intptr_t)-199)
337#define SH_SIG_DFL ((shsig_t)SIG_DFL)
338#define SH_SIG_IGN ((shsig_t)SIG_IGN)
339#define SH_SIG_ERR ((shsig_t)SIG_ERR)
340#ifdef _MSC_VER
341# define SIG_BLOCK 1
342# define SIG_UNBLOCK 2
343# define SIG_SETMASK 3
344# define SIGHUP 5
345# define SIGQUIT 9
346# define SIGPIPE 12
347# define SIGTTOU 17
348# define SIGTSTP 18
349# define SIGTTIN 19
350# define SIGCONT 20
351# define sys_siglist sys_signame
352#endif /* _MSC_VER */
353#ifdef __sun__
354# define sys_siglist _sys_siglist
355#endif
356#ifndef HAVE_SYS_SIGNAME
357extern char sys_signame[NSIG][16];
358#endif
359
360int sh_sigaction(shinstance *, int, const struct shsigaction *, struct shsigaction *);
361shsig_t sh_signal(shinstance *, int, shsig_t);
362int sh_siginterrupt(shinstance *, int, int);
363void sh_sigemptyset(shsigset_t *);
364int sh_sigfillset(shsigset_t *);
365void sh_sigaddset(shsigset_t *, int);
366void sh_sigdelset(shsigset_t *, int);
367int sh_sigismember(shsigset_t *, int);
368int sh_sigprocmask(shinstance *, int, shsigset_t const *, shsigset_t *);
369void sh_abort(shinstance *) __attribute__((__noreturn__));
370void sh_raise_sigint(shinstance *);
371int sh_kill(shinstance *, pid_t, int);
372int sh_killpg(shinstance *, pid_t, int);
373
374/* times */
375#include <time.h>
376#ifdef _MSC_VER
377 typedef struct shtms
378 {
379 clock_t tms_utime;
380 clock_t tms_stime;
381 clock_t tms_cutime;
382 clock_t tms_cstime;
383 } shtms;
384#else
385# include <sys/times.h>
386 typedef struct tms shtms;
387#endif
388clock_t sh_times(shinstance *, shtms *);
389int sh_sysconf_clk_tck(void);
390
391/* wait / process */
392#ifdef _MSC_VER
393# include <process.h>
394# define WNOHANG 1 /* Don't hang in wait. */
395# define WUNTRACED 2 /* Tell about stopped, untraced children. */
396# define WCONTINUED 4 /* Report a job control continued process. */
397# define _W_INT(w) (*(int *)&(w)) /* Convert union wait to int. */
398# define WCOREFLAG 0200
399# define _WSTATUS(x) (_W_INT(x) & 0177)
400# define _WSTOPPED 0177 /* _WSTATUS if process is stopped */
401# define WIFSTOPPED(x) (_WSTATUS(x) == _WSTOPPED)
402# define WSTOPSIG(x) (_W_INT(x) >> 8)
403# define WIFSIGNALED(x) (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
404# define WTERMSIG(x) (_WSTATUS(x))
405# define WIFEXITED(x) (_WSTATUS(x) == 0)
406# define WEXITSTATUS(x) (_W_INT(x) >> 8)
407# define WIFCONTINUED(x) (x == 0x13) /* 0x13 == SIGCONT */
408# define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
409# define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
410# define W_STOPCODE(sig) ((sig) << 8 | _WSTOPPED)
411#else
412# include <sys/wait.h>
413#endif
414pid_t sh_fork(shinstance *);
415pid_t sh_waitpid(shinstance *, pid_t, int *, int);
416void sh__exit(shinstance *, int) __attribute__((__noreturn__));
417int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
418uid_t sh_getuid(shinstance *);
419uid_t sh_geteuid(shinstance *);
420gid_t sh_getgid(shinstance *);
421gid_t sh_getegid(shinstance *);
422pid_t sh_getpid(shinstance *);
423pid_t sh_getpgrp(shinstance *);
424pid_t sh_getpgid(shinstance *, pid_t);
425int sh_setpgid(shinstance *, pid_t, pid_t);
426
427/* tc* */
428pid_t sh_tcgetpgrp(shinstance *, int);
429int sh_tcsetpgrp(shinstance *, int, pid_t);
430
431/* sys/resource.h */
432#ifdef _MSC_VER
433 typedef int64_t shrlim_t;
434 typedef struct shrlimit
435 {
436 shrlim_t rlim_cur;
437 shrlim_t rlim_max;
438 } shrlimit;
439# define RLIMIT_CPU 0
440# define RLIMIT_FSIZE 1
441# define RLIMIT_DATA 2
442# define RLIMIT_STACK 3
443# define RLIMIT_CORE 4
444# define RLIMIT_RSS 5
445# define RLIMIT_MEMLOCK 6
446# define RLIMIT_NPROC 7
447# define RLIMIT_NOFILE 8
448# define RLIMIT_SBSIZE 9
449# define RLIMIT_VMEM 10
450# define RLIM_NLIMITS 11
451# define RLIM_INFINITY (0x7fffffffffffffffLL)
452#else
453 typedef rlim_t shrlim_t;
454 typedef struct rlimit shrlimit;
455#endif
456int sh_getrlimit(shinstance *, int, shrlimit *);
457int sh_setrlimit(shinstance *, int, const shrlimit *);
458
459#endif
Note: See TracBrowser for help on using the repository browser.

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