Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /*- |
2 | * Copyright (c) 1991 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Kenneth Almquist. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. All advertising materials mentioning features or use of this software | |
17 | * must display the following acknowledgement: | |
18 | * This product includes software developed by the University of | |
19 | * California, Berkeley and its contributors. | |
20 | * 4. Neither the name of the University nor the names of its contributors | |
21 | * may be used to endorse or promote products derived from this software | |
22 | * without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | * SUCH DAMAGE. | |
35 | */ | |
36 | ||
37 | #ifndef lint | |
38 | char copyright[] = | |
39 | "@(#) Copyright (c) 1991 The Regents of the University of California.\n\ | |
40 | All rights reserved.\n"; | |
41 | #endif /* not lint */ | |
42 | ||
43 | #ifndef lint | |
68de1ca3 AM |
44 | /*static char sccsid[] = "from: @(#)main.c 5.2 (Berkeley) 3/13/91";*/ |
45 | static char rcsid[] = "main.c,v 1.4 1993/08/01 18:58:12 mycroft Exp"; | |
15637ed4 RG |
46 | #endif /* not lint */ |
47 | ||
48 | #include <signal.h> | |
49 | #include <fcntl.h> | |
50 | #include "shell.h" | |
51 | #include "main.h" | |
52 | #include "mail.h" | |
53 | #include "options.h" | |
54 | #include "output.h" | |
55 | #include "parser.h" | |
56 | #include "nodes.h" | |
57 | #include "eval.h" | |
58 | #include "jobs.h" | |
59 | #include "input.h" | |
60 | #include "trap.h" | |
61 | #if ATTY | |
62 | #include "var.h" | |
63 | #endif | |
64 | #include "memalloc.h" | |
65 | #include "error.h" | |
66 | #include "init.h" | |
67 | #include "mystring.h" | |
68 | ||
69 | #define PROFILE 0 | |
70 | ||
71 | int rootpid; | |
72 | int rootshell; | |
73 | STATIC union node *curcmd; | |
74 | STATIC union node *prevcmd; | |
75 | extern int errno; | |
76 | #if PROFILE | |
77 | short profile_buf[16384]; | |
78 | extern int etext(); | |
79 | #endif | |
80 | ||
81 | #ifdef __STDC__ | |
82 | STATIC void read_profile(char *); | |
83 | char *getenv(char *); | |
84 | #else | |
85 | STATIC void read_profile(); | |
86 | char *getenv(); | |
87 | #endif | |
88 | ||
89 | ||
90 | /* | |
91 | * Main routine. We initialize things, parse the arguments, execute | |
92 | * profiles if we're a login shell, and then call cmdloop to execute | |
93 | * commands. The setjmp call sets up the location to jump to when an | |
94 | * exception occurs. When an exception occurs the variable "state" | |
95 | * is used to figure out how far we had gotten. | |
96 | */ | |
97 | ||
98 | main(argc, argv) char **argv; { | |
99 | struct jmploc jmploc; | |
100 | struct stackmark smark; | |
101 | volatile int state; | |
102 | char *shinit; | |
103 | ||
104 | #if PROFILE | |
105 | monitor(4, etext, profile_buf, sizeof profile_buf, 50); | |
106 | #endif | |
107 | state = 0; | |
108 | if (setjmp(jmploc.loc)) { | |
109 | /* | |
110 | * When a shell procedure is executed, we raise the | |
111 | * exception EXSHELLPROC to clean up before executing | |
112 | * the shell procedure. | |
113 | */ | |
114 | if (exception == EXSHELLPROC) { | |
115 | rootpid = getpid(); | |
116 | rootshell = 1; | |
117 | minusc = NULL; | |
118 | state = 3; | |
119 | } else if (state == 0 || iflag == 0 || ! rootshell) | |
120 | exitshell(2); | |
121 | reset(); | |
122 | #if ATTY | |
123 | if (exception == EXINT | |
124 | && (! attyset() || equal(termval(), "emacs"))) { | |
125 | #else | |
126 | if (exception == EXINT) { | |
127 | #endif | |
128 | out2c('\n'); | |
129 | flushout(&errout); | |
130 | } | |
131 | popstackmark(&smark); | |
132 | FORCEINTON; /* enable interrupts */ | |
133 | if (state == 1) | |
134 | goto state1; | |
135 | else if (state == 2) | |
136 | goto state2; | |
137 | else | |
138 | goto state3; | |
139 | } | |
140 | handler = &jmploc; | |
141 | #ifdef DEBUG | |
142 | opentrace(); | |
143 | trputs("Shell args: "); trargs(argv); | |
144 | #endif | |
145 | rootpid = getpid(); | |
146 | rootshell = 1; | |
147 | init(); | |
148 | setstackmark(&smark); | |
149 | procargs(argc, argv); | |
150 | if (argv[0] && argv[0][0] == '-') { | |
151 | state = 1; | |
152 | read_profile("/etc/profile"); | |
153 | state1: | |
154 | state = 2; | |
155 | read_profile(".profile"); | |
156 | } else if ((sflag || minusc) && (shinit = getenv("SHINIT")) != NULL) { | |
157 | state = 2; | |
158 | evalstring(shinit); | |
159 | } | |
160 | state2: | |
161 | state = 3; | |
162 | if (minusc) { | |
163 | evalstring(minusc); | |
164 | } | |
165 | if (sflag || minusc == NULL) { | |
166 | state3: | |
167 | cmdloop(1); | |
168 | } | |
169 | #if PROFILE | |
170 | monitor(0); | |
171 | #endif | |
172 | exitshell(exitstatus); | |
173 | } | |
174 | ||
175 | ||
176 | /* | |
177 | * Read and execute commands. "Top" is nonzero for the top level command | |
178 | * loop; it turns on prompting if the shell is interactive. | |
179 | */ | |
180 | ||
181 | void | |
182 | cmdloop(top) { | |
183 | union node *n; | |
184 | struct stackmark smark; | |
185 | int inter; | |
186 | int numeof; | |
187 | ||
188 | TRACE(("cmdloop(%d) called\n", top)); | |
189 | setstackmark(&smark); | |
190 | numeof = 0; | |
191 | for (;;) { | |
192 | if (pendingsigs) | |
193 | dotrap(); | |
194 | inter = 0; | |
195 | if (iflag && top) { | |
196 | inter++; | |
197 | showjobs(1); | |
198 | chkmail(0); | |
199 | flushout(&output); | |
200 | } | |
201 | n = parsecmd(inter); | |
202 | #ifdef DEBUG | |
203 | /* showtree(n); */ | |
204 | #endif | |
205 | if (n == NEOF) { | |
206 | if (Iflag == 0 || numeof >= 50) | |
207 | break; | |
208 | out2str("\nUse \"exit\" to leave shell.\n"); | |
209 | numeof++; | |
210 | } else if (n != NULL && nflag == 0) { | |
211 | if (inter) { | |
212 | INTOFF; | |
213 | if (prevcmd) | |
214 | freefunc(prevcmd); | |
215 | prevcmd = curcmd; | |
216 | curcmd = copyfunc(n); | |
217 | INTON; | |
218 | } | |
219 | evaltree(n, 0); | |
220 | #ifdef notdef | |
221 | if (exitstatus) /*DEBUG*/ | |
222 | outfmt(&errout, "Exit status 0x%X\n", exitstatus); | |
223 | #endif | |
224 | } | |
225 | popstackmark(&smark); | |
226 | } | |
227 | popstackmark(&smark); /* unnecessary */ | |
228 | } | |
229 | ||
230 | ||
231 | ||
232 | /* | |
233 | * Read /etc/profile or .profile. Return on error. | |
234 | */ | |
235 | ||
236 | STATIC void | |
237 | read_profile(name) | |
238 | char *name; | |
239 | { | |
240 | int fd; | |
241 | ||
242 | INTOFF; | |
243 | if ((fd = open(name, O_RDONLY)) >= 0) | |
244 | setinputfd(fd, 1); | |
245 | INTON; | |
246 | if (fd < 0) | |
247 | return; | |
248 | cmdloop(0); | |
249 | popfile(); | |
250 | } | |
251 | ||
252 | ||
253 | ||
254 | /* | |
255 | * Read a file containing shell functions. | |
256 | */ | |
257 | ||
258 | void | |
259 | readcmdfile(name) | |
260 | char *name; | |
261 | { | |
262 | int fd; | |
263 | ||
264 | INTOFF; | |
265 | if ((fd = open(name, O_RDONLY)) >= 0) | |
266 | setinputfd(fd, 1); | |
267 | else | |
268 | error("Can't open %s", name); | |
269 | INTON; | |
270 | cmdloop(0); | |
271 | popfile(); | |
272 | } | |
273 | ||
274 | ||
275 | ||
276 | /* | |
277 | * Take commands from a file. To be compatable we should do a path | |
278 | * search for the file, but a path search doesn't make any sense. | |
279 | */ | |
280 | ||
281 | dotcmd(argc, argv) char **argv; { | |
282 | exitstatus = 0; | |
283 | if (argc >= 2) { /* That's what SVR2 does */ | |
284 | setinputfile(argv[1], 1); | |
285 | commandname = argv[1]; | |
286 | cmdloop(0); | |
287 | popfile(); | |
288 | } | |
289 | return exitstatus; | |
290 | } | |
291 | ||
292 | ||
293 | exitcmd(argc, argv) char **argv; { | |
294 | if (argc > 1) | |
295 | exitstatus = number(argv[1]); | |
296 | exitshell(exitstatus); | |
297 | } | |
298 | ||
299 | ||
300 | lccmd(argc, argv) char **argv; { | |
301 | if (argc > 1) { | |
302 | defun(argv[1], prevcmd); | |
303 | return 0; | |
304 | } else { | |
305 | INTOFF; | |
306 | freefunc(curcmd); | |
307 | curcmd = prevcmd; | |
308 | prevcmd = NULL; | |
309 | INTON; | |
310 | evaltree(curcmd, 0); | |
311 | return exitstatus; | |
312 | } | |
313 | } | |
314 | ||
315 | ||
316 | ||
317 | #ifdef notdef | |
318 | /* | |
319 | * Should never be called. | |
320 | */ | |
321 | ||
322 | void | |
323 | exit(exitstatus) { | |
324 | _exit(exitstatus); | |
325 | } | |
326 | #endif |