VirtualBox

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

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

kash: malloc/free/friends gets a psh.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 16.0 KB
Line 
1/* $Id: shinstance.h 2290 2009-02-27 04:08:07Z 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/* heap */
339void *sh_malloc(shinstance *, size_t);
340void *sh_calloc(shinstance *, size_t, size_t);
341void *sh_realloc(shinstance *, void *, size_t);
342char *sh_strdup(shinstance *, const char *);
343void sh_free(shinstance *, void *);
344
345/* signals */
346#define SH_SIG_UNK ((shsig_t)(intptr_t)-199)
347#define SH_SIG_DFL ((shsig_t)SIG_DFL)
348#define SH_SIG_IGN ((shsig_t)SIG_IGN)
349#define SH_SIG_ERR ((shsig_t)SIG_ERR)
350#ifdef _MSC_VER
351# define SA_RESTART 0x02
352# define SIG_BLOCK 1
353# define SIG_UNBLOCK 2
354# define SIG_SETMASK 3
355# define SIGHUP 5
356# define SIGQUIT 9
357# define SIGPIPE 12
358# define SIGTTOU 17
359# define SIGTSTP 18
360# define SIGTTIN 19
361# define SIGCONT 20
362# define sys_siglist sys_signame
363#endif /* _MSC_VER */
364#ifdef __sun__
365# define sys_siglist _sys_siglist
366#endif
367#ifndef HAVE_SYS_SIGNAME
368extern char sys_signame[NSIG][16];
369#endif
370
371int sh_sigaction(shinstance *, int, const struct shsigaction *, struct shsigaction *);
372shsig_t sh_signal(shinstance *, int, shsig_t);
373int sh_siginterrupt(shinstance *, int, int);
374void sh_sigemptyset(shsigset_t *);
375void sh_sigfillset(shsigset_t *);
376void sh_sigaddset(shsigset_t *, int);
377void sh_sigdelset(shsigset_t *, int);
378int sh_sigismember(shsigset_t const *, int);
379int sh_sigprocmask(shinstance *, int, shsigset_t const *, shsigset_t *);
380void sh_abort(shinstance *) __attribute__((__noreturn__));
381void sh_raise_sigint(shinstance *);
382int sh_kill(shinstance *, pid_t, int);
383int sh_killpg(shinstance *, pid_t, int);
384
385/* times */
386#include <time.h>
387#ifdef _MSC_VER
388 typedef struct shtms
389 {
390 clock_t tms_utime;
391 clock_t tms_stime;
392 clock_t tms_cutime;
393 clock_t tms_cstime;
394 } shtms;
395#else
396# include <sys/times.h>
397 typedef struct tms shtms;
398#endif
399clock_t sh_times(shinstance *, shtms *);
400int sh_sysconf_clk_tck(void);
401
402/* wait / process */
403#ifdef _MSC_VER
404# include <process.h>
405# define WNOHANG 1 /* Don't hang in wait. */
406# define WUNTRACED 2 /* Tell about stopped, untraced children. */
407# define WCONTINUED 4 /* Report a job control continued process. */
408# define _W_INT(w) (*(int *)&(w)) /* Convert union wait to int. */
409# define WCOREFLAG 0200
410# define _WSTATUS(x) (_W_INT(x) & 0177)
411# define _WSTOPPED 0177 /* _WSTATUS if process is stopped */
412# define WIFSTOPPED(x) (_WSTATUS(x) == _WSTOPPED)
413# define WSTOPSIG(x) (_W_INT(x) >> 8)
414# define WIFSIGNALED(x) (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
415# define WTERMSIG(x) (_WSTATUS(x))
416# define WIFEXITED(x) (_WSTATUS(x) == 0)
417# define WEXITSTATUS(x) (_W_INT(x) >> 8)
418# define WIFCONTINUED(x) (x == 0x13) /* 0x13 == SIGCONT */
419# define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
420# define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
421# define W_STOPCODE(sig) ((sig) << 8 | _WSTOPPED)
422#else
423# include <sys/wait.h>
424#endif
425pid_t sh_fork(shinstance *);
426pid_t sh_waitpid(shinstance *, pid_t, int *, int);
427void sh__exit(shinstance *, int) __attribute__((__noreturn__));
428int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
429uid_t sh_getuid(shinstance *);
430uid_t sh_geteuid(shinstance *);
431gid_t sh_getgid(shinstance *);
432gid_t sh_getegid(shinstance *);
433pid_t sh_getpid(shinstance *);
434pid_t sh_getpgrp(shinstance *);
435pid_t sh_getpgid(shinstance *, pid_t);
436int sh_setpgid(shinstance *, pid_t, pid_t);
437
438/* tc* */
439pid_t sh_tcgetpgrp(shinstance *, int);
440int sh_tcsetpgrp(shinstance *, int, pid_t);
441
442/* sys/resource.h */
443#ifdef _MSC_VER
444 typedef int64_t shrlim_t;
445 typedef struct shrlimit
446 {
447 shrlim_t rlim_cur;
448 shrlim_t rlim_max;
449 } shrlimit;
450# define RLIMIT_CPU 0
451# define RLIMIT_FSIZE 1
452# define RLIMIT_DATA 2
453# define RLIMIT_STACK 3
454# define RLIMIT_CORE 4
455# define RLIMIT_RSS 5
456# define RLIMIT_MEMLOCK 6
457# define RLIMIT_NPROC 7
458# define RLIMIT_NOFILE 8
459# define RLIMIT_SBSIZE 9
460# define RLIMIT_VMEM 10
461# define RLIM_NLIMITS 11
462# define RLIM_INFINITY (0x7fffffffffffffffLL)
463#else
464 typedef rlim_t shrlim_t;
465 typedef struct rlimit shrlimit;
466#endif
467int sh_getrlimit(shinstance *, int, shrlimit *);
468int sh_setrlimit(shinstance *, int, const shrlimit *);
469
470
471#ifdef DEBUG
472# define TRACE2(param) trace param
473# define TRACE2V(param) tracev param
474#else
475# define TRACE2(param)
476# define TRACE2V(param)
477#endif
478
479#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