main.c revision 1.14 1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. 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 static char copyright[] =
39 "@(#) Copyright (c) 1991, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 /*static char sccsid[] = "from: @(#)main.c 8.1 (Berkeley) 5/31/93";*/
45 static char *rcsid = "$Id: main.c,v 1.14 1994/08/03 16:25:26 jtc Exp $";
46 #endif /* not lint */
47
48 #include <stdio.h>
49 #include <signal.h>
50 #include <sys/stat.h>
51 #include <unistd.h>
52 #include <fcntl.h>
53 #include "shell.h"
54 #include "main.h"
55 #include "mail.h"
56 #include "options.h"
57 #include "output.h"
58 #include "parser.h"
59 #include "nodes.h"
60 #include "eval.h"
61 #include "jobs.h"
62 #include "input.h"
63 #include "trap.h"
64 #include "var.h"
65 #include "memalloc.h"
66 #include "error.h"
67 #include "init.h"
68 #include "mystring.h"
69 #include "exec.h"
70
71 #define PROFILE 0
72
73 int rootpid;
74 int rootshell;
75 STATIC union node *curcmd;
76 STATIC union node *prevcmd;
77 extern int errno;
78 #if PROFILE
79 short profile_buf[16384];
80 extern int etext();
81 #endif
82
83 #ifdef __STDC__
84 STATIC void read_profile(char *);
85 char *getenv(char *);
86 #else
87 STATIC void read_profile();
88 char *getenv();
89 #endif
90
91
92 /*
93 * Main routine. We initialize things, parse the arguments, execute
94 * profiles if we're a login shell, and then call cmdloop to execute
95 * commands. The setjmp call sets up the location to jump to when an
96 * exception occurs. When an exception occurs the variable "state"
97 * is used to figure out how far we had gotten.
98 */
99
100 main(argc, argv) char **argv; {
101 struct jmploc jmploc;
102 struct stackmark smark;
103 volatile int state;
104 char *shinit;
105
106 #if PROFILE
107 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
108 #endif
109 state = 0;
110 if (setjmp(jmploc.loc)) {
111 /*
112 * When a shell procedure is executed, we raise the
113 * exception EXSHELLPROC to clean up before executing
114 * the shell procedure.
115 */
116 if (exception == EXSHELLPROC) {
117 rootpid = getpid();
118 rootshell = 1;
119 minusc = NULL;
120 state = 3;
121 } else if (state == 0 || iflag == 0 || ! rootshell)
122 exitshell(2);
123 reset();
124 if (exception == EXINT
125 #if ATTY
126 && (! attyset() || equal(termval(), "emacs"))
127 #endif
128 ) {
129 out2c('\n');
130 flushout(&errout);
131 }
132 popstackmark(&smark);
133 FORCEINTON; /* enable interrupts */
134 if (state == 1)
135 goto state1;
136 else if (state == 2)
137 goto state2;
138 else if (state == 3)
139 goto state3;
140 else
141 goto state4;
142 }
143 handler = &jmploc;
144 #ifdef DEBUG
145 opentrace();
146 trputs("Shell args: "); trargs(argv);
147 #endif
148 rootpid = getpid();
149 rootshell = 1;
150 init();
151 setstackmark(&smark);
152 procargs(argc, argv);
153 if (argv[0] && argv[0][0] == '-') {
154 state = 1;
155 read_profile("/etc/profile");
156 state1:
157 state = 2;
158 read_profile(".profile");
159 }
160 state2:
161 state = 3;
162 if (getuid() == geteuid() && getgid() == getegid()) {
163 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
164 state = 3;
165 read_profile(shinit);
166 }
167 }
168 state3:
169 state = 4;
170 if (minusc) {
171 evalstring(minusc);
172 }
173 if (sflag || minusc == NULL) {
174 state4: /* XXX ??? - why isn't this before the "if" statement */
175 cmdloop(1);
176 }
177 #if PROFILE
178 monitor(0);
179 #endif
180 exitshell(exitstatus);
181 }
182
183
184 /*
185 * Read and execute commands. "Top" is nonzero for the top level command
186 * loop; it turns on prompting if the shell is interactive.
187 */
188
189 void
190 cmdloop(top) {
191 union node *n;
192 struct stackmark smark;
193 int inter;
194 int numeof = 0;
195
196 TRACE(("cmdloop(%d) called\n", top));
197 setstackmark(&smark);
198 for (;;) {
199 if (pendingsigs)
200 dotrap();
201 inter = 0;
202 if (iflag && top) {
203 inter++;
204 showjobs(1);
205 chkmail(0);
206 flushout(&output);
207 }
208 n = parsecmd(inter);
209 /* showtree(n); DEBUG */
210 if (n == NEOF) {
211 if (!top || numeof >= 50)
212 break;
213 if (!stoppedjobs()) {
214 if (!Iflag)
215 break;
216 out2str("\nUse \"exit\" to leave shell.\n");
217 }
218 numeof++;
219 } else if (n != NULL && nflag == 0) {
220 job_warning = (job_warning == 2) ? 1 : 0;
221 numeof = 0;
222 evaltree(n, 0);
223 }
224 popstackmark(&smark);
225 }
226 popstackmark(&smark); /* unnecessary */
227 }
228
229
230
231 /*
232 * Read /etc/profile or .profile. Return on error.
233 */
234
235 STATIC void
236 read_profile(name)
237 char *name;
238 {
239 int fd;
240
241 INTOFF;
242 if ((fd = open(name, O_RDONLY)) >= 0)
243 setinputfd(fd, 1);
244 INTON;
245 if (fd < 0)
246 return;
247 cmdloop(0);
248 popfile();
249 }
250
251
252
253 /*
254 * Read a file containing shell functions.
255 */
256
257 void
258 readcmdfile(name)
259 char *name;
260 {
261 int fd;
262
263 INTOFF;
264 if ((fd = open(name, O_RDONLY)) >= 0)
265 setinputfd(fd, 1);
266 else
267 error("Can't open %s", name);
268 INTON;
269 cmdloop(0);
270 popfile();
271 }
272
273
274
275 /*
276 * Take commands from a file. To be compatable we should do a path
277 * search for the file, which is necessary to find sub-commands.
278 */
279
280
281 static char *
282 find_dot_file(basename) char *basename; {
283 static char localname[FILENAME_MAX+1];
284 char *fullname;
285 char *path = pathval();
286 struct stat statb;
287
288 /* don't try this for absolute or relative paths */
289 if( strchr(basename, '/'))
290 return basename;
291
292 while ((fullname = padvance(&path, basename)) != NULL) {
293 strcpy(localname, fullname);
294 stunalloc(fullname);
295 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
296 return localname;
297 }
298 return basename;
299 }
300
301 dotcmd(argc, argv) char **argv; {
302 exitstatus = 0;
303 if (argc >= 2) { /* That's what SVR2 does */
304 char *fullname = find_dot_file(argv[1]);
305 setinputfile(fullname, 1);
306 commandname = fullname;
307 cmdloop(0);
308 popfile();
309 }
310 return exitstatus;
311 }
312
313
314 exitcmd(argc, argv) char **argv; {
315 if (stoppedjobs())
316 return;
317 if (argc > 1)
318 exitstatus = number(argv[1]);
319 exitshell(exitstatus);
320 }
321
322
323 #ifdef notdef
324 /*
325 * Should never be called.
326 */
327
328 void
329 exit(exitstatus) {
330 _exit(exitstatus);
331 }
332 #endif
333