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