VirtualBox

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

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

kash: windows build fixes.

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