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 */
|
---|
45 | struct stack_block {
|
---|
46 | struct stack_block *prev;
|
---|
47 | char space[MINSIZE];
|
---|
48 | };
|
---|
49 |
|
---|
50 | /* input.c */
|
---|
51 | struct 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 | */
|
---|
63 | struct 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 |
|
---|
79 | struct 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 | */
|
---|
92 | struct 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 | */
|
---|
107 | typedef 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 vtermcap;
|
---|
177 | struct var vhistsize;
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | /* myhistedit.h */
|
---|
181 | int displayhist;
|
---|
182 | #ifndef SMALL
|
---|
183 | History *hist;
|
---|
184 | EditLine *el;
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | /* memalloc.h */
|
---|
188 | char *stacknxt/* = stackbase.space*/;
|
---|
189 | int stacknleft/* = MINSIZE*/;
|
---|
190 | int sstrnleft;
|
---|
191 | int herefd/* = -1 */;
|
---|
192 |
|
---|
193 | /* memalloc.c */
|
---|
194 | struct stack_block stackbase;
|
---|
195 | struct stack_block *stackp/* = &stackbase*/;
|
---|
196 | struct stackmark *markp;
|
---|
197 |
|
---|
198 | /* jobs.h */
|
---|
199 | pid_t backgndpid/* = -1 */; /**< pid of last background process */
|
---|
200 | int job_warning; /**< user was warned about stopped jobs */
|
---|
201 |
|
---|
202 | /* jobs.c */
|
---|
203 | struct job *jobtab; /**< array of jobs */
|
---|
204 | int njobs; /**< size of array */
|
---|
205 | int jobs_invalid; /**< set in child */
|
---|
206 | #if JOBS
|
---|
207 | int initialpgrp; /**< pgrp of shell on invocation */
|
---|
208 | int curjob/* = -1*/;/**< current job */
|
---|
209 | #endif
|
---|
210 | int ttyfd/* = -1*/;
|
---|
211 | int jobctl; /**< job control enabled / disabled */
|
---|
212 | char *cmdnextc;
|
---|
213 | int cmdnleft;
|
---|
214 |
|
---|
215 | /* input.h */
|
---|
216 | int plinno/* = 1 */;/**< input line number */
|
---|
217 | int parsenleft; /**< number of characters left in input buffer */
|
---|
218 | char *parsenextc; /**< next character in input buffer */
|
---|
219 | int init_editline/* = 0 */; /**< 0 == not setup, 1 == OK, -1 == failed */
|
---|
220 |
|
---|
221 | /* input.c */
|
---|
222 | int parselleft; /**< copy of parsefile->lleft */
|
---|
223 | struct parsefile basepf; /**< top level input file */
|
---|
224 | char basebuf[BUFSIZ];/**< buffer for top level input file */
|
---|
225 | struct parsefile *parsefile/* = &basepf*/; /**< current input file */
|
---|
226 | #ifndef SMALL
|
---|
227 | EditLine *el; /**< cookie for editline package */
|
---|
228 | #endif
|
---|
229 |
|
---|
230 |
|
---|
231 | /* exec.h */
|
---|
232 | const char *pathopt; /**< set by padvance */
|
---|
233 |
|
---|
234 | /* exec.c */
|
---|
235 | struct tblentry *cmdtable[CMDTABLESIZE];
|
---|
236 | int builtinloc/* = -1*/; /**< index in path of %builtin, or -1 */
|
---|
237 |
|
---|
238 |
|
---|
239 | /* eval.h */
|
---|
240 | char *commandname; /**< currently executing command */
|
---|
241 | int exitstatus; /**< exit status of last command */
|
---|
242 | int back_exitstatus;/**< exit status of backquoted command */
|
---|
243 | struct strlist *cmdenviron; /**< environment for builtin command */
|
---|
244 | int funcnest; /**< depth of function calls */
|
---|
245 | int evalskip; /**< set if we are skipping commands */
|
---|
246 | int skipcount; /**< number of levels to skip */
|
---|
247 | int loopnest; /**< current loop nesting level */
|
---|
248 |
|
---|
249 | /* builtins.h */
|
---|
250 |
|
---|
251 | /* alias.c */
|
---|
252 | #define ATABSIZE 39
|
---|
253 | struct alias *atab[ATABSIZE];
|
---|
254 |
|
---|
255 | /* cd.c */
|
---|
256 | char *curdir; /**< current working directory */
|
---|
257 | char *prevdir; /**< previous working directory */
|
---|
258 | char *cdcomppath;
|
---|
259 | int getpwd_first; /**< static in getpwd. (initialized to 1!) */
|
---|
260 |
|
---|
261 | /* error.c */
|
---|
262 | char errmsg_buf[16]; /**< static in errmsg. (bss) */
|
---|
263 |
|
---|
264 | /* eval.c */
|
---|
265 | int vforked;
|
---|
266 |
|
---|
267 | /* expand.c */
|
---|
268 | char *expdest; /**< output of current string */
|
---|
269 | struct nodelist *argbackq; /**< list of back quote expressions */
|
---|
270 | struct ifsregion ifsfirst; /**< first struct in list of ifs regions */
|
---|
271 | struct ifsregion *ifslastp; /**< last struct in list */
|
---|
272 | struct arglist exparg; /**< holds expanded arg list */
|
---|
273 | char *expdir; /**< Used by expandmeta. */
|
---|
274 |
|
---|
275 | /* mail.c */
|
---|
276 | #define MAXMBOXES 10
|
---|
277 | int nmboxes; /**< number of mailboxes */
|
---|
278 | time_t mailtime[MAXMBOXES]; /**< times of mailboxes */
|
---|
279 |
|
---|
280 | /* bltin/test.c */
|
---|
281 | char **t_wp;
|
---|
282 | struct t_op const *t_wp_op;
|
---|
283 |
|
---|
284 | } shinstance;
|
---|
285 |
|
---|
286 |
|
---|
287 | extern shinstance *sh_create_root_shell(shinstance *, int, char **);
|
---|
288 | char *sh_getenv(shinstance *, const char *);
|
---|
289 |
|
---|
290 | /* signals */
|
---|
291 | typedef void (*sh_sig_t)(shinstance *, int);
|
---|
292 | #ifdef _MSC_VER
|
---|
293 | typedef uint32_t sh_sigset_t;
|
---|
294 | #else
|
---|
295 | typedef sigset_t sh_sigset_t;
|
---|
296 | #endif
|
---|
297 | struct sh_sigaction
|
---|
298 | {
|
---|
299 | sh_sig_t sh_handler;
|
---|
300 | sh_sigset_t sh_mask;
|
---|
301 | int sh_flags;
|
---|
302 | };
|
---|
303 | #define SH_SIG_DFL ((sh_sig_t)SIG_DFL)
|
---|
304 | #define SH_SIG_IGN ((sh_sig_t)SIG_IGN)
|
---|
305 |
|
---|
306 | int sh_sigaction(int, const struct sh_sigaction *, struct sh_sigaction *);
|
---|
307 | sh_sig_t sh_signal(shinstance *, int, sh_sig_t);
|
---|
308 | void sh_raise_sigint(shinstance *);
|
---|
309 | void sh_sigemptyset(sh_sigset_t *);
|
---|
310 | int sh_sigprocmask(shinstance *, int, sh_sigset_t const *, sh_sigset_t *);
|
---|
311 | void sh_abort(shinstance *);
|
---|
312 |
|
---|
313 | /* times */
|
---|
314 | #include <time.h>
|
---|
315 | #ifdef _MSC_VER
|
---|
316 | typedef struct sh_tms
|
---|
317 | {
|
---|
318 | clock_t tms_utime;
|
---|
319 | clock_t tms_stime;
|
---|
320 | clock_t tms_cutime;
|
---|
321 | clock_t tms_cstime;
|
---|
322 | } sh_tms;
|
---|
323 | #else
|
---|
324 | # include <times.h>
|
---|
325 | typedef struct tms sh_tms;
|
---|
326 | #endif
|
---|
327 | clock_t sh_times(sh_tms *);
|
---|
328 | int sh_sysconf_clk_tck(void);
|
---|
329 |
|
---|
330 | /* wait / process */
|
---|
331 | #ifdef _MSC_VER
|
---|
332 | # include <process.h>
|
---|
333 | # define WNOHANG 1 /* Don't hang in wait. */
|
---|
334 | # define WUNTRACED 2 /* Tell about stopped, untraced children. */
|
---|
335 | # define WCONTINUED 4 /* Report a job control continued process. */
|
---|
336 | # define _W_INT(w) (*(int *)&(w)) /* Convert union wait to int. */
|
---|
337 | # define WCOREFLAG 0200
|
---|
338 | # define _WSTATUS(x) (_W_INT(x) & 0177)
|
---|
339 | # define _WSTOPPED 0177 /* _WSTATUS if process is stopped */
|
---|
340 | # define WIFSTOPPED(x) (_WSTATUS(x) == _WSTOPPED)
|
---|
341 | # define WSTOPSIG(x) (_W_INT(x) >> 8)
|
---|
342 | # define WIFSIGNALED(x) (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
|
---|
343 | # define WTERMSIG(x) (_WSTATUS(x))
|
---|
344 | # define WIFEXITED(x) (_WSTATUS(x) == 0)
|
---|
345 | # define WEXITSTATUS(x) (_W_INT(x) >> 8)
|
---|
346 | # define WIFCONTINUED(x) (x == 0x13) /* 0x13 == SIGCONT */
|
---|
347 | # define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
|
---|
348 | # define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
|
---|
349 | # define W_STOPCODE(sig) ((sig) << 8 | _WSTOPPED)
|
---|
350 | #else
|
---|
351 | # include <sys/wait.h>
|
---|
352 | #endif
|
---|
353 | pid_t sh_waitpid(shinstance *, pid_t, int *, int);
|
---|
354 | void sh__exit(shinstance *, int);
|
---|
355 | int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
|
---|
356 | uid_t sh_getuid(shinstance *);
|
---|
357 | uid_t sh_geteuid(shinstance *);
|
---|
358 | gid_t sh_getgid(shinstance *);
|
---|
359 | gid_t sh_getegid(shinstance *);
|
---|
360 | pid_t sh_getpid(shinstance *);
|
---|
361 | pid_t sh_getpgrp(shinstance *);
|
---|
362 | pid_t sh_getpgid(shinstance *, pid_t);
|
---|
363 | int sh_setpgid(shinstance *, pid_t, pid_t);
|
---|
364 | int sh_killpg(shinstance *, pid_t, int);
|
---|
365 |
|
---|
366 | /* tc* */
|
---|
367 | pid_t sh_tcgetpgrp(shinstance *, int);
|
---|
368 | int sh_tcsetpgrp(shinstance *, int, pid_t);
|
---|
369 |
|
---|
370 | #endif
|
---|