source: trunk/src/kash/shinstance.h@ 2291

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

hash: cooked our own heap on windows (for forking).

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