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

Last change on this file since 1209 was 1209, checked in by bird, 18 years ago

var.c ++.

File size: 12.9 KB
Line 
1/* $Id: $ */
2/** @file
3 *
4 * The shell instance and it's methods.
5 *
6 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
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
33#include "shtypes.h"
34#include "shthread.h"
35#include "shfile.h"
36#include "output.h"
37#include "options.h"
38
39#include "expand.h"
40#include "exec.h"
41#include "var.h"
42
43/* memalloc.c */
44#define MINSIZE 504 /* minimum size of a block */
45struct stack_block {
46 struct stack_block *prev;
47 char space[MINSIZE];
48};
49
50/* input.c */
51struct strpush {
52 struct strpush *prev; /* preceding string on stack */
53 char *prevstring;
54 int prevnleft;
55 int prevlleft;
56 struct alias *ap; /* if push was associated with an alias */
57};
58
59/*
60 * The parsefile structure pointed to by the global variable parsefile
61 * contains information about the current file being read.
62 */
63struct parsefile {
64 struct parsefile *prev; /* preceding file on stack */
65 int linno; /* current line */
66 int fd; /* file descriptor (or -1 if string) */
67 int nleft; /* number of chars left in this line */
68 int lleft; /* number of chars left in this buffer */
69 char *nextc; /* next char in buffer */
70 char *buf; /* input buffer */
71 struct strpush *strpush; /* for pushing strings at this level */
72 struct strpush basestrpush; /* so pushing one is fast */
73};
74
75/* exec.c */
76#define CMDTABLESIZE 31 /* should be prime */
77#define ARB 1 /* actual size determined at run time */
78
79struct tblentry {
80 struct tblentry *next; /* next entry in hash chain */
81 union param param; /* definition of builtin function */
82 short cmdtype; /* index identifying command */
83 char rehash; /* if set, cd done since entry created */
84 char cmdname[ARB]; /* name of command */
85};
86
87/* expand.c */
88/*
89 * Structure specifying which parts of the string should be searched
90 * for IFS characters.
91 */
92struct ifsregion {
93 struct ifsregion *next; /* next region in list */
94 int begoff; /* offset of start of region */
95 int endoff; /* offset of end of region */
96 int inquotes; /* search for nul bytes only */
97};
98
99
100/**
101 * A shell instance.
102 *
103 * This is the core structure of the shell, it contains all
104 * the data associated with a shell process except that it's
105 * running in a thread and not a separate process.
106 */
107typedef struct shinstance
108{
109 struct shinstance *next; /**< The next shell instance. */
110 struct shinstance *prev; /**< The previous shell instance. */
111 struct shinstance *parent; /**< The parent shell instance. */
112 pid_t pid; /**< The (fake) process id of this shell instance. */
113 shtid tid; /**< The thread identifier of the thread for this shell. */
114 shfdtab fdtab; /**< The file descriptor table. */
115
116 /* error.h */
117 struct jmploc *handler;
118 int exception;
119 int exerrno/* = 0 */; /**< Last exec error */
120 int volatile suppressint;
121 int volatile intpending;
122
123 /* main.h */
124 int rootpid; /**< pid of main shell. */
125 int rootshell; /**< true if we aren't a child of the main shell. */
126 struct shinstance *psh_rootshell; /**< The root shell pointer. (!rootshell) */
127
128 /* trap.h */
129 int pendingsigs; /**< indicates some signal received */
130
131 /* trap.c */
132 char gotsig[NSIG]; /**< indicates specified signal received */
133 char *trap[NSIG+1]; /**< trap handler commands */
134 char sigmode[NSIG]; /**< current value of signal */
135
136 /* parse.h */
137 int tokpushback;
138 int whichprompt; /**< 1 == PS1, 2 == PS2 */
139
140 /* output.h */
141 struct output output;
142 struct output errout;
143 struct output memout;
144 struct output *out1;
145 struct output *out2;
146 /* output.c */
147#define OUTBUFSIZ BUFSIZ
148#define MEM_OUT -3 /**< output to dynamically allocated memory */
149
150 /* options.h */
151 struct optent optlist[NOPTS];
152 char *minusc; /**< argument to -c option */
153 char *arg0; /**< $0 */
154 struct shparam shellparam; /**< $@ */
155 char **argptr; /**< argument list for builtin commands */
156 char *optionarg; /**< set by nextopt */
157 char *optptr; /**< used by nextopt */
158
159 /* var.h */
160 struct localvar *localvars;
161#if ATTY
162 struct var vatty;
163#endif
164 struct var vifs;
165 struct var vmail;
166 struct var vmpath;
167 struct var vpath;
168#ifdef _MSC_VER
169 struct var vpath2;
170#endif
171 struct var vps1;
172 struct var vps2;
173 struct var vps4;
174#ifndef SMALL
175 struct var vterm;
176 struct var vhistsize;
177#endif
178 struct var voptind;
179#ifdef PC_OS2_LIBPATHS
180 struct var libpath_vars[4];
181#endif
182#ifdef SMALL
183# define VTABSIZE 39
184#else
185# define VTABSIZE 517
186#endif
187 struct var *vartab[VTABSIZE];
188
189 /* myhistedit.h */
190 int displayhist;
191#ifndef SMALL
192 History *hist;
193 EditLine *el;
194#endif
195
196 /* memalloc.h */
197 char *stacknxt/* = stackbase.space*/;
198 int stacknleft/* = MINSIZE*/;
199 int sstrnleft;
200 int herefd/* = -1 */;
201
202 /* memalloc.c */
203 struct stack_block stackbase;
204 struct stack_block *stackp/* = &stackbase*/;
205 struct stackmark *markp;
206
207 /* jobs.h */
208 pid_t backgndpid/* = -1 */; /**< pid of last background process */
209 int job_warning; /**< user was warned about stopped jobs */
210
211 /* jobs.c */
212 struct job *jobtab; /**< array of jobs */
213 int njobs; /**< size of array */
214 int jobs_invalid; /**< set in child */
215#if JOBS
216 int initialpgrp; /**< pgrp of shell on invocation */
217 int curjob/* = -1*/;/**< current job */
218#endif
219 int ttyfd/* = -1*/;
220 int jobctl; /**< job control enabled / disabled */
221 char *cmdnextc;
222 int cmdnleft;
223
224 /* input.h */
225 int plinno/* = 1 */;/**< input line number */
226 int parsenleft; /**< number of characters left in input buffer */
227 char *parsenextc; /**< next character in input buffer */
228 int init_editline/* = 0 */; /**< 0 == not setup, 1 == OK, -1 == failed */
229
230 /* input.c */
231 int parselleft; /**< copy of parsefile->lleft */
232 struct parsefile basepf; /**< top level input file */
233 char basebuf[BUFSIZ];/**< buffer for top level input file */
234 struct parsefile *parsefile/* = &basepf*/; /**< current input file */
235#ifndef SMALL
236 EditLine *el; /**< cookie for editline package */
237#endif
238
239
240 /* exec.h */
241 const char *pathopt; /**< set by padvance */
242
243 /* exec.c */
244 struct tblentry *cmdtable[CMDTABLESIZE];
245 int builtinloc/* = -1*/; /**< index in path of %builtin, or -1 */
246
247
248 /* eval.h */
249 char *commandname; /**< currently executing command */
250 int exitstatus; /**< exit status of last command */
251 int back_exitstatus;/**< exit status of backquoted command */
252 struct strlist *cmdenviron; /**< environment for builtin command */
253 int funcnest; /**< depth of function calls */
254 int evalskip; /**< set if we are skipping commands */
255 int skipcount; /**< number of levels to skip */
256 int loopnest; /**< current loop nesting level */
257
258 /* builtins.h */
259
260 /* alias.c */
261#define ATABSIZE 39
262 struct alias *atab[ATABSIZE];
263
264 /* cd.c */
265 char *curdir; /**< current working directory */
266 char *prevdir; /**< previous working directory */
267 char *cdcomppath;
268 int getpwd_first; /**< static in getpwd. (initialized to 1!) */
269
270 /* error.c */
271 char errmsg_buf[16]; /**< static in errmsg. (bss) */
272
273 /* eval.c */
274 int vforked;
275
276 /* expand.c */
277 char *expdest; /**< output of current string */
278 struct nodelist *argbackq; /**< list of back quote expressions */
279 struct ifsregion ifsfirst; /**< first struct in list of ifs regions */
280 struct ifsregion *ifslastp; /**< last struct in list */
281 struct arglist exparg; /**< holds expanded arg list */
282 char *expdir; /**< Used by expandmeta. */
283
284 /* mail.c */
285#define MAXMBOXES 10
286 int nmboxes; /**< number of mailboxes */
287 time_t mailtime[MAXMBOXES]; /**< times of mailboxes */
288
289 /* bltin/test.c */
290 char **t_wp;
291 struct t_op const *t_wp_op;
292
293} shinstance;
294
295
296extern shinstance *sh_create_root_shell(shinstance *, int, char **);
297char *sh_getenv(shinstance *, const char *);
298
299/* signals */
300typedef void (*sh_sig_t)(shinstance *, int);
301#ifdef _MSC_VER
302 typedef uint32_t sh_sigset_t;
303#else
304 typedef sigset_t sh_sigset_t;
305#endif
306struct sh_sigaction
307{
308 sh_sig_t sh_handler;
309 sh_sigset_t sh_mask;
310 int sh_flags;
311};
312#define SH_SIG_DFL ((sh_sig_t)SIG_DFL)
313#define SH_SIG_IGN ((sh_sig_t)SIG_IGN)
314
315int sh_sigaction(int, const struct sh_sigaction *, struct sh_sigaction *);
316sh_sig_t sh_signal(shinstance *, int, sh_sig_t);
317void sh_raise_sigint(shinstance *);
318void sh_sigemptyset(sh_sigset_t *);
319int sh_sigprocmask(shinstance *, int, sh_sigset_t const *, sh_sigset_t *);
320void sh_abort(shinstance *);
321
322/* times */
323#include <time.h>
324#ifdef _MSC_VER
325 typedef struct sh_tms
326 {
327 clock_t tms_utime;
328 clock_t tms_stime;
329 clock_t tms_cutime;
330 clock_t tms_cstime;
331 } sh_tms;
332#else
333# include <times.h>
334 typedef struct tms sh_tms;
335#endif
336clock_t sh_times(sh_tms *);
337int sh_sysconf_clk_tck(void);
338
339/* wait / process */
340#ifdef _MSC_VER
341# include <process.h>
342# define WNOHANG 1 /* Don't hang in wait. */
343# define WUNTRACED 2 /* Tell about stopped, untraced children. */
344# define WCONTINUED 4 /* Report a job control continued process. */
345# define _W_INT(w) (*(int *)&(w)) /* Convert union wait to int. */
346# define WCOREFLAG 0200
347# define _WSTATUS(x) (_W_INT(x) & 0177)
348# define _WSTOPPED 0177 /* _WSTATUS if process is stopped */
349# define WIFSTOPPED(x) (_WSTATUS(x) == _WSTOPPED)
350# define WSTOPSIG(x) (_W_INT(x) >> 8)
351# define WIFSIGNALED(x) (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
352# define WTERMSIG(x) (_WSTATUS(x))
353# define WIFEXITED(x) (_WSTATUS(x) == 0)
354# define WEXITSTATUS(x) (_W_INT(x) >> 8)
355# define WIFCONTINUED(x) (x == 0x13) /* 0x13 == SIGCONT */
356# define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
357# define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
358# define W_STOPCODE(sig) ((sig) << 8 | _WSTOPPED)
359#else
360# include <sys/wait.h>
361#endif
362pid_t sh_waitpid(shinstance *, pid_t, int *, int);
363void sh__exit(shinstance *, int);
364int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
365uid_t sh_getuid(shinstance *);
366uid_t sh_geteuid(shinstance *);
367gid_t sh_getgid(shinstance *);
368gid_t sh_getegid(shinstance *);
369pid_t sh_getpid(shinstance *);
370pid_t sh_getpgrp(shinstance *);
371pid_t sh_getpgid(shinstance *, pid_t);
372int sh_setpgid(shinstance *, pid_t, pid_t);
373int sh_killpg(shinstance *, pid_t, int);
374
375/* tc* */
376pid_t sh_tcgetpgrp(shinstance *, int);
377int sh_tcsetpgrp(shinstance *, int, pid_t);
378
379#endif
Note: See TracBrowser for help on using the repository browser.