1 | /* $Id: $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * The shell instance and it's methods.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
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 | /* parser.c */
|
---|
141 | int noalias/* = 0*/;/**< when set, don't handle aliases */
|
---|
142 | struct heredoc *heredoclist; /**< list of here documents to read */
|
---|
143 | int parsebackquote; /**< nonzero if we are inside backquotes */
|
---|
144 | int doprompt; /**< if set, prompt the user */
|
---|
145 | int needprompt; /**< true if interactive and at start of line */
|
---|
146 | int lasttoken; /**< last token read */
|
---|
147 | char *wordtext; /**< text of last word returned by readtoken */
|
---|
148 | int checkkwd; /**< 1 == check for kwds, 2 == also eat newlines */
|
---|
149 | struct nodelist *backquotelist;
|
---|
150 | union node *redirnode;
|
---|
151 | struct heredoc *heredoc;
|
---|
152 | int quoteflag; /**< set if (part of) last token was quoted */
|
---|
153 | int startlinno; /**< line # where last token started */
|
---|
154 |
|
---|
155 | /* output.h */
|
---|
156 | struct output output;
|
---|
157 | struct output errout;
|
---|
158 | struct output memout;
|
---|
159 | struct output *out1;
|
---|
160 | struct output *out2;
|
---|
161 | /* output.c */
|
---|
162 | #define OUTBUFSIZ BUFSIZ
|
---|
163 | #define MEM_OUT -3 /**< output to dynamically allocated memory */
|
---|
164 |
|
---|
165 | /* options.h */
|
---|
166 | struct optent optlist[NOPTS];
|
---|
167 | char *minusc; /**< argument to -c option */
|
---|
168 | char *arg0; /**< $0 */
|
---|
169 | struct shparam shellparam; /**< $@ */
|
---|
170 | char **argptr; /**< argument list for builtin commands */
|
---|
171 | char *optionarg; /**< set by nextopt */
|
---|
172 | char *optptr; /**< used by nextopt */
|
---|
173 |
|
---|
174 | /* var.h */
|
---|
175 | struct localvar *localvars;
|
---|
176 | #if ATTY
|
---|
177 | struct var vatty;
|
---|
178 | #endif
|
---|
179 | struct var vifs;
|
---|
180 | struct var vmail;
|
---|
181 | struct var vmpath;
|
---|
182 | struct var vpath;
|
---|
183 | #ifdef _MSC_VER
|
---|
184 | struct var vpath2;
|
---|
185 | #endif
|
---|
186 | struct var vps1;
|
---|
187 | struct var vps2;
|
---|
188 | struct var vps4;
|
---|
189 | #ifndef SMALL
|
---|
190 | struct var vterm;
|
---|
191 | struct var vhistsize;
|
---|
192 | #endif
|
---|
193 | struct var voptind;
|
---|
194 | #ifdef PC_OS2_LIBPATHS
|
---|
195 | struct var libpath_vars[4];
|
---|
196 | #endif
|
---|
197 | #ifdef SMALL
|
---|
198 | # define VTABSIZE 39
|
---|
199 | #else
|
---|
200 | # define VTABSIZE 517
|
---|
201 | #endif
|
---|
202 | struct var *vartab[VTABSIZE];
|
---|
203 |
|
---|
204 | /* myhistedit.h */
|
---|
205 | int displayhist;
|
---|
206 | #ifndef SMALL
|
---|
207 | History *hist;
|
---|
208 | EditLine *el;
|
---|
209 | #endif
|
---|
210 |
|
---|
211 | /* memalloc.h */
|
---|
212 | char *stacknxt/* = stackbase.space*/;
|
---|
213 | int stacknleft/* = MINSIZE*/;
|
---|
214 | int sstrnleft;
|
---|
215 | int herefd/* = -1 */;
|
---|
216 |
|
---|
217 | /* memalloc.c */
|
---|
218 | struct stack_block stackbase;
|
---|
219 | struct stack_block *stackp/* = &stackbase*/;
|
---|
220 | struct stackmark *markp;
|
---|
221 |
|
---|
222 | /* jobs.h */
|
---|
223 | pid_t backgndpid/* = -1 */; /**< pid of last background process */
|
---|
224 | int job_warning; /**< user was warned about stopped jobs */
|
---|
225 |
|
---|
226 | /* jobs.c */
|
---|
227 | struct job *jobtab; /**< array of jobs */
|
---|
228 | int njobs; /**< size of array */
|
---|
229 | int jobs_invalid; /**< set in child */
|
---|
230 | #if JOBS
|
---|
231 | int initialpgrp; /**< pgrp of shell on invocation */
|
---|
232 | int curjob/* = -1*/;/**< current job */
|
---|
233 | #endif
|
---|
234 | int ttyfd/* = -1*/;
|
---|
235 | int jobctl; /**< job control enabled / disabled */
|
---|
236 | char *cmdnextc;
|
---|
237 | int cmdnleft;
|
---|
238 |
|
---|
239 | /* input.h */
|
---|
240 | int plinno/* = 1 */;/**< input line number */
|
---|
241 | int parsenleft; /**< number of characters left in input buffer */
|
---|
242 | char *parsenextc; /**< next character in input buffer */
|
---|
243 | int init_editline/* = 0 */; /**< 0 == not setup, 1 == OK, -1 == failed */
|
---|
244 |
|
---|
245 | /* input.c */
|
---|
246 | int parselleft; /**< copy of parsefile->lleft */
|
---|
247 | struct parsefile basepf; /**< top level input file */
|
---|
248 | char basebuf[BUFSIZ];/**< buffer for top level input file */
|
---|
249 | struct parsefile *parsefile/* = &basepf*/; /**< current input file */
|
---|
250 | #ifndef SMALL
|
---|
251 | EditLine *el; /**< cookie for editline package */
|
---|
252 | #endif
|
---|
253 |
|
---|
254 |
|
---|
255 | /* exec.h */
|
---|
256 | const char *pathopt; /**< set by padvance */
|
---|
257 |
|
---|
258 | /* exec.c */
|
---|
259 | struct tblentry *cmdtable[CMDTABLESIZE];
|
---|
260 | int builtinloc/* = -1*/; /**< index in path of %builtin, or -1 */
|
---|
261 |
|
---|
262 |
|
---|
263 | /* eval.h */
|
---|
264 | char *commandname; /**< currently executing command */
|
---|
265 | int exitstatus; /**< exit status of last command */
|
---|
266 | int back_exitstatus;/**< exit status of backquoted command */
|
---|
267 | struct strlist *cmdenviron; /**< environment for builtin command */
|
---|
268 | int funcnest; /**< depth of function calls */
|
---|
269 | int evalskip; /**< set if we are skipping commands */
|
---|
270 | int skipcount; /**< number of levels to skip */
|
---|
271 | int loopnest; /**< current loop nesting level */
|
---|
272 |
|
---|
273 | /* builtins.h */
|
---|
274 |
|
---|
275 | /* alias.c */
|
---|
276 | #define ATABSIZE 39
|
---|
277 | struct alias *atab[ATABSIZE];
|
---|
278 |
|
---|
279 | /* cd.c */
|
---|
280 | char *curdir; /**< current working directory */
|
---|
281 | char *prevdir; /**< previous working directory */
|
---|
282 | char *cdcomppath;
|
---|
283 | int getpwd_first; /**< static in getpwd. (initialized to 1!) */
|
---|
284 |
|
---|
285 | /* error.c */
|
---|
286 | char errmsg_buf[16]; /**< static in errmsg. (bss) */
|
---|
287 |
|
---|
288 | /* eval.c */
|
---|
289 | int vforked;
|
---|
290 |
|
---|
291 | /* expand.c */
|
---|
292 | char *expdest; /**< output of current string */
|
---|
293 | struct nodelist *argbackq; /**< list of back quote expressions */
|
---|
294 | struct ifsregion ifsfirst; /**< first struct in list of ifs regions */
|
---|
295 | struct ifsregion *ifslastp; /**< last struct in list */
|
---|
296 | struct arglist exparg; /**< holds expanded arg list */
|
---|
297 | char *expdir; /**< Used by expandmeta. */
|
---|
298 |
|
---|
299 | /* mail.c */
|
---|
300 | #define MAXMBOXES 10
|
---|
301 | int nmboxes; /**< number of mailboxes */
|
---|
302 | time_t mailtime[MAXMBOXES]; /**< times of mailboxes */
|
---|
303 |
|
---|
304 | /* bltin/test.c */
|
---|
305 | char **t_wp;
|
---|
306 | struct t_op const *t_wp_op;
|
---|
307 |
|
---|
308 | } shinstance;
|
---|
309 |
|
---|
310 |
|
---|
311 | extern shinstance *sh_create_root_shell(shinstance *, int, char **);
|
---|
312 | char *sh_getenv(shinstance *, const char *);
|
---|
313 |
|
---|
314 | /* signals */
|
---|
315 | typedef void (*sh_sig_t)(shinstance *, int);
|
---|
316 | #ifdef _MSC_VER
|
---|
317 | typedef uint32_t sh_sigset_t;
|
---|
318 | #else
|
---|
319 | typedef sigset_t sh_sigset_t;
|
---|
320 | #endif
|
---|
321 | struct sh_sigaction
|
---|
322 | {
|
---|
323 | sh_sig_t sh_handler;
|
---|
324 | sh_sigset_t sh_mask;
|
---|
325 | int sh_flags;
|
---|
326 | };
|
---|
327 | #define SH_SIG_DFL ((sh_sig_t)SIG_DFL)
|
---|
328 | #define SH_SIG_IGN ((sh_sig_t)SIG_IGN)
|
---|
329 |
|
---|
330 | int sh_sigaction(int, const struct sh_sigaction *, struct sh_sigaction *);
|
---|
331 | sh_sig_t sh_signal(shinstance *, int, sh_sig_t);
|
---|
332 | void sh_raise_sigint(shinstance *);
|
---|
333 | void sh_sigemptyset(sh_sigset_t *);
|
---|
334 | int sh_sigprocmask(shinstance *, int, sh_sigset_t const *, sh_sigset_t *);
|
---|
335 | void sh_abort(shinstance *);
|
---|
336 |
|
---|
337 | /* times */
|
---|
338 | #include <time.h>
|
---|
339 | #ifdef _MSC_VER
|
---|
340 | typedef struct sh_tms
|
---|
341 | {
|
---|
342 | clock_t tms_utime;
|
---|
343 | clock_t tms_stime;
|
---|
344 | clock_t tms_cutime;
|
---|
345 | clock_t tms_cstime;
|
---|
346 | } sh_tms;
|
---|
347 | #else
|
---|
348 | # include <times.h>
|
---|
349 | typedef struct tms sh_tms;
|
---|
350 | #endif
|
---|
351 | clock_t sh_times(sh_tms *);
|
---|
352 | int sh_sysconf_clk_tck(void);
|
---|
353 |
|
---|
354 | /* wait / process */
|
---|
355 | #ifdef _MSC_VER
|
---|
356 | # include <process.h>
|
---|
357 | # define WNOHANG 1 /* Don't hang in wait. */
|
---|
358 | # define WUNTRACED 2 /* Tell about stopped, untraced children. */
|
---|
359 | # define WCONTINUED 4 /* Report a job control continued process. */
|
---|
360 | # define _W_INT(w) (*(int *)&(w)) /* Convert union wait to int. */
|
---|
361 | # define WCOREFLAG 0200
|
---|
362 | # define _WSTATUS(x) (_W_INT(x) & 0177)
|
---|
363 | # define _WSTOPPED 0177 /* _WSTATUS if process is stopped */
|
---|
364 | # define WIFSTOPPED(x) (_WSTATUS(x) == _WSTOPPED)
|
---|
365 | # define WSTOPSIG(x) (_W_INT(x) >> 8)
|
---|
366 | # define WIFSIGNALED(x) (_WSTATUS(x) != 0 && !WIFSTOPPED(x) && !WIFCONTINUED(x)) /* bird: made GLIBC tests happy. */
|
---|
367 | # define WTERMSIG(x) (_WSTATUS(x))
|
---|
368 | # define WIFEXITED(x) (_WSTATUS(x) == 0)
|
---|
369 | # define WEXITSTATUS(x) (_W_INT(x) >> 8)
|
---|
370 | # define WIFCONTINUED(x) (x == 0x13) /* 0x13 == SIGCONT */
|
---|
371 | # define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
|
---|
372 | # define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
|
---|
373 | # define W_STOPCODE(sig) ((sig) << 8 | _WSTOPPED)
|
---|
374 | #else
|
---|
375 | # include <sys/wait.h>
|
---|
376 | #endif
|
---|
377 | pid_t sh_waitpid(shinstance *, pid_t, int *, int);
|
---|
378 | void sh__exit(shinstance *, int);
|
---|
379 | int sh_execve(shinstance *, const char *, const char * const*, const char * const *);
|
---|
380 | uid_t sh_getuid(shinstance *);
|
---|
381 | uid_t sh_geteuid(shinstance *);
|
---|
382 | gid_t sh_getgid(shinstance *);
|
---|
383 | gid_t sh_getegid(shinstance *);
|
---|
384 | pid_t sh_getpid(shinstance *);
|
---|
385 | pid_t sh_getpgrp(shinstance *);
|
---|
386 | pid_t sh_getpgid(shinstance *, pid_t);
|
---|
387 | int sh_setpgid(shinstance *, pid_t, pid_t);
|
---|
388 | int sh_killpg(shinstance *, pid_t, int);
|
---|
389 |
|
---|
390 | /* tc* */
|
---|
391 | pid_t sh_tcgetpgrp(shinstance *, int);
|
---|
392 | int sh_tcsetpgrp(shinstance *, int, pid_t);
|
---|
393 |
|
---|
394 | #endif
|
---|