VirtualBox

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

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

kash: avoid file steams in the trace code.

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