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

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

kash: forking on windows (almost there).

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 16.2 KB
Line 
1/* $Id: shinstance.h 2293 2009-02-28 07:25:12Z 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 * 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 /* trap.h */
301 int pendingsigs; /**< indicates some signal received */
302
303 /* trap.c */
304 char gotsig[NSIG]; /**< indicates specified signal received */
305 char *trap[NSIG+1]; /**< trap handler commands */
306 char sigmode[NSIG]; /**< current value of signal */
307
308 /* var.h */
309 struct localvar *localvars;
310 struct var vatty;
311 struct var vifs;
312 struct var vmail;
313 struct var vmpath;
314 struct var vpath;
315#ifdef _MSC_VER
316 struct var vpath2;
317#endif
318 struct var vps1;
319 struct var vps2;
320 struct var vps4;
321#ifndef SMALL
322 struct var vterm;
323 struct var vhistsize;
324#endif
325 struct var voptind;
326#ifdef PC_OS2_LIBPATHS
327 struct var libpath_vars[4];
328#endif
329#ifdef SMALL
330# define VTABSIZE 39
331#else
332# define VTABSIZE 517
333#endif
334 struct var *vartab[VTABSIZE];
335
336 /* builtins.h */
337
338 /* bltin/test.c */
339 char **t_wp;
340 struct t_op const *t_wp_op;
341
342};
343
344
345extern shinstance *sh_create_root_shell(shinstance *, int, char **, char **);
346
347/* environment & pwd.h */
348char *sh_getenv(shinstance *, const char *);
349char **sh_environ(shinstance *);
350const char *sh_gethomedir(shinstance *, const char *);
351
352/* signals */
353#define SH_SIG_UNK ((shsig_t)(intptr_t)-199)
354#define SH_SIG_DFL ((shsig_t)SIG_DFL)
355#define SH_SIG_IGN ((shsig_t)SIG_IGN)
356#define SH_SIG_ERR ((shsig_t)SIG_ERR)
357#ifdef _MSC_VER
358# define SA_RESTART 0x02
359# define SIG_BLOCK 1
360# define SIG_UNBLOCK 2
361# define SIG_SETMASK 3
362# define SIGHUP 5
363# define SIGQUIT 9
364# define SIGPIPE 12
365# define SIGTTOU 17
366# define SIGTSTP 18
367# define SIGTTIN 19
368# define SIGCONT 20
369# define sys_siglist sys_signame
370#endif /* _MSC_VER */
371#ifdef __sun__
372# define sys_siglist _sys_siglist
373#endif
374#ifndef HAVE_SYS_SIGNAME
375extern char sys_signame[NSIG][16];
376#endif
377
378int sh_sigaction(shinstance *, int, const struct shsigaction *, struct shsigaction *);
379shsig_t sh_signal(shinstance *, int, shsig_t);
380int sh_siginterrupt(shinstance *, int, int);
381void sh_sigemptyset(shsigset_t *);
382void sh_sigfillset(shsigset_t *);
383void sh_sigaddset(shsigset_t *, int);
384void sh_sigdelset(shsigset_t *, int);
385int sh_sigismember(shsigset_t const *, int);
386int sh_sigprocmask(shinstance *, int, shsigset_t const *, shsigset_t *);
387void sh_abort(shinstance *) __attribute__((__noreturn__));
388void sh_raise_sigint(shinstance *);
389int sh_kill(shinstance *, pid_t, int);
390int sh_killpg(shinstance *, pid_t, int);
391
392/* times */
393#include <time.h>
394#ifdef _MSC_VER
395 typedef struct shtms
396 {
397 clock_t tms_utime;
398 clock_t tms_stime;
399 clock_t tms_cutime;
400 clock_t tms_cstime;
401 } shtms;
402#else
403# include <sys/times.h>
404 typedef struct tms shtms;
405#endif
406clock_t sh_times(shinstance *, shtms *);
407int sh_sysconf_clk_tck(void);
408
409/* wait / process */
410int sh_add_child(shinstance *psh, pid_t pid, void *hChild);
411#ifdef _MSC_VER
412# include <process.h>
413# define WNOHANG 1 /* Don't hang in wait. */
414# define WUNTRACED 2 /* Tell about stopped, untraced children. */
415# define WCONTINUED 4 /* Report a job control continued process. */
416# define _W_INT(w) (*(int *)&(w)) /* Convert union wait to int. */
417# define WCOREFLAG 0200
418# define _WSTATUS(x) (_W_INT(x) & 0177)
419# define _WSTOPPED 0177 /* _WSTATUS if process is stopped */
420# define WIFSTOPPED(x) (_WSTATUS(x) == _WSTOPPED)
421# define WSTOPSIG(x) (_W_INT(x) >> 8)
422# define WIFSIGNALED(x) (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
423# define WTERMSIG(x) (_WSTATUS(x))
424# define WIFEXITED(x) (_WSTATUS(x) == 0)
425# define WEXITSTATUS(x) (_W_INT(x) >> 8)
426# define WIFCONTINUED(x) (x == 0x13) /* 0x13 == SIGCONT */
427# define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
428# define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
429# define W_STOPCODE(sig) ((sig) << 8 | _WSTOPPED)
430#else
431# include <sys/wait.h>
432#endif
433pid_t sh_fork(shinstance *);
434pid_t sh_waitpid(shinstance *, pid_t, int *, int);
435void sh__exit(shinstance *, int) __attribute__((__noreturn__));
436int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
437uid_t sh_getuid(shinstance *);
438uid_t sh_geteuid(shinstance *);
439gid_t sh_getgid(shinstance *);
440gid_t sh_getegid(shinstance *);
441pid_t sh_getpid(shinstance *);
442pid_t sh_getpgrp(shinstance *);
443pid_t sh_getpgid(shinstance *, pid_t);
444int sh_setpgid(shinstance *, pid_t, pid_t);
445
446/* tc* */
447pid_t sh_tcgetpgrp(shinstance *, int);
448int sh_tcsetpgrp(shinstance *, int, pid_t);
449
450/* sys/resource.h */
451#ifdef _MSC_VER
452 typedef int64_t shrlim_t;
453 typedef struct shrlimit
454 {
455 shrlim_t rlim_cur;
456 shrlim_t rlim_max;
457 } shrlimit;
458# define RLIMIT_CPU 0
459# define RLIMIT_FSIZE 1
460# define RLIMIT_DATA 2
461# define RLIMIT_STACK 3
462# define RLIMIT_CORE 4
463# define RLIMIT_RSS 5
464# define RLIMIT_MEMLOCK 6
465# define RLIMIT_NPROC 7
466# define RLIMIT_NOFILE 8
467# define RLIMIT_SBSIZE 9
468# define RLIMIT_VMEM 10
469# define RLIM_NLIMITS 11
470# define RLIM_INFINITY (0x7fffffffffffffffLL)
471#else
472 typedef rlim_t shrlim_t;
473 typedef struct rlimit shrlimit;
474#endif
475int sh_getrlimit(shinstance *, int, shrlimit *);
476int sh_setrlimit(shinstance *, int, const shrlimit *);
477
478
479#ifdef DEBUG
480# define TRACE2(param) trace param
481# define TRACE2V(param) tracev param
482#else
483# define TRACE2(param)
484# define TRACE2V(param)
485#endif
486
487#endif
Note: See TracBrowser for help on using the repository browser.