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