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