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