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

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

kill.c ++

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