1 /* $NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
38 The Regents of the University of California. All rights reserved.\n");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95";
44 #else
45 __RCSID("$NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <errno.h>
50 #include <stdio.h>
51 #include <signal.h>
52 #include <sys/stat.h>
53 #include <unistd.h>
54 #include <fcntl.h>
55
56
57 #include "shell.h"
58 #include "main.h"
59 #include "options.h"
60 #include "output.h"
61 #include "parser.h"
62 #include "nodes.h"
63 #include "expand.h"
64 #include "eval.h"
65 #include "jobs.h"
66 #include "input.h"
67 #include "trap.h"
68 #include "var.h"
69 #include "show.h"
70 #include "memalloc.h"
71 #include "error.h"
72 #include "init.h"
73 #include "mystring.h"
74 #include "exec.h"
75 #include "cd.h"
76
77 #define PROFILE 0
78
79 int rootpid;
80 int rootshell;
81 STATIC union node *curcmd;
82 STATIC union node *prevcmd;
83 #if PROFILE
84 short profile_buf[16384];
85 extern int etext();
86 #endif
87
88 STATIC void read_profile(const char *);
89 STATIC char *find_dot_file(char *);
90 int main(int, char **);
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 int
main(int argc,char ** argv)101 main(int argc, char **argv)
102 {
103 struct jmploc jmploc;
104 struct stackmark smark;
105 volatile int state;
106 char *shinit;
107
108 #if PROFILE
109 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
110 #endif
111 state = 0;
112 if (setjmp(jmploc.loc)) {
113 /*
114 * When a shell procedure is executed, we raise the
115 * exception EXSHELLPROC to clean up before executing
116 * the shell procedure.
117 */
118 switch (exception) {
119 case EXSHELLPROC:
120 rootpid = getpid();
121 rootshell = 1;
122 minusc = NULL;
123 state = 3;
124 break;
125
126 case EXEXEC:
127 exitstatus = exerrno;
128 break;
129
130 case EXERROR:
131 exitstatus = 2;
132 break;
133
134 default:
135 break;
136 }
137
138 if (exception != EXSHELLPROC) {
139 if (state == 0 || iflag == 0 || ! rootshell)
140 exitshell(exitstatus);
141 }
142 reset();
143 if (exception == EXINT
144 #if ATTY
145 && (! attyset() || equal(termval(), "emacs"))
146 #endif
147 ) {
148 out2c('\n');
149 flushout(&errout);
150 }
151 popstackmark(&smark);
152 FORCEINTON; /* enable interrupts */
153 if (state == 1)
154 goto state1;
155 else if (state == 2)
156 goto state2;
157 else if (state == 3)
158 goto state3;
159 else
160 goto state4;
161 }
162 handler = &jmploc;
163 #ifdef DEBUG
164 #if DEBUG == 2
165 debug = 1;
166 #endif
167 opentrace();
168 trputs("Shell args: "); trargs(argv);
169 #endif
170 rootpid = getpid();
171 rootshell = 1;
172 init();
173 setstackmark(&smark);
174 procargs(argc, argv);
175 if (argv[0] && argv[0][0] == '-') {
176 state = 1;
177 read_profile("/etc/profile");
178 state1:
179 state = 2;
180 read_profile(".profile");
181 }
182 state2:
183 state = 3;
184 if (getuid() == geteuid() && getgid() == getegid()) {
185 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
186 state = 3;
187 read_profile(shinit);
188 }
189 }
190 state3:
191 state = 4;
192 if (sflag == 0 || minusc) {
193 static int sigs[] = {
194 SIGINT, SIGQUIT, SIGHUP,
195 #ifdef SIGTSTP
196 SIGTSTP,
197 #endif
198 SIGPIPE
199 };
200 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
201 int i;
202
203 for (i = 0; i < SIGSSIZE; i++)
204 setsignal(sigs[i], 0);
205 }
206
207 if (minusc)
208 evalstring(minusc, 0);
209
210 if (sflag || minusc == NULL) {
211 state4: /* XXX ??? - why isn't this before the "if" statement */
212 cmdloop(1);
213 }
214 #if PROFILE
215 monitor(0);
216 #endif
217 exitshell(exitstatus);
218 /* NOTREACHED */
219 }
220
221
222 /*
223 * Read and execute commands. "Top" is nonzero for the top level command
224 * loop; it turns on prompting if the shell is interactive.
225 */
226
227 void
cmdloop(int top)228 cmdloop(int top)
229 {
230 union node *n;
231 struct stackmark smark;
232 int inter;
233 int numeof = 0;
234
235 TRACE(("cmdloop(%d) called\n", top));
236 setstackmark(&smark);
237 for (;;) {
238 if (pendingsigs)
239 dotrap();
240 inter = 0;
241 if (iflag && top) {
242 inter = 1;
243 showjobs(out2, SHOW_CHANGED);
244 flushout(&errout);
245 }
246 n = parsecmd(inter);
247 /* showtree(n); DEBUG */
248 if (n == NEOF) {
249 if (!top || numeof >= 50)
250 break;
251 if (!stoppedjobs()) {
252 if (!Iflag)
253 break;
254 out2str("\nUse \"exit\" to leave shell.\n");
255 }
256 numeof++;
257 } else if (n != NULL && nflag == 0) {
258 job_warning = (job_warning == 2) ? 1 : 0;
259 numeof = 0;
260 evaltree(n, 0);
261 }
262 popstackmark(&smark);
263 setstackmark(&smark);
264 if (evalskip == SKIPFILE) {
265 evalskip = 0;
266 break;
267 }
268 }
269 popstackmark(&smark);
270 }
271
272
273
274 /*
275 * Read /etc/profile or .profile. Return on error.
276 */
277
278 STATIC void
read_profile(const char * name)279 read_profile(const char *name)
280 {
281 int fd;
282 int xflag_set = 0;
283 int vflag_set = 0;
284
285 INTOFF;
286 if ((fd = open(name, O_RDONLY)) >= 0)
287 setinputfd(fd, 1);
288 INTON;
289 if (fd < 0)
290 return;
291 /* -q turns off -x and -v just when executing init files */
292 if (qflag) {
293 if (xflag)
294 xflag = 0, xflag_set = 1;
295 if (vflag)
296 vflag = 0, vflag_set = 1;
297 }
298 cmdloop(0);
299 if (qflag) {
300 if (xflag_set)
301 xflag = 1;
302 if (vflag_set)
303 vflag = 1;
304 }
305 popfile();
306 }
307
308
309
310 /*
311 * Read a file containing shell functions.
312 */
313
314 void
readcmdfile(char * name)315 readcmdfile(char *name)
316 {
317 int fd;
318
319 INTOFF;
320 if ((fd = open(name, O_RDONLY)) >= 0)
321 setinputfd(fd, 1);
322 else
323 error("Can't open %s", name);
324 INTON;
325 cmdloop(0);
326 popfile();
327 }
328
329
330
331 /*
332 * Take commands from a file. To be compatible we should do a path
333 * search for the file, which is necessary to find sub-commands.
334 */
335
336
337 STATIC char *
find_dot_file(char * basename)338 find_dot_file(char *basename)
339 {
340 char *fullname;
341 const char *path = pathval();
342 struct stat statb;
343
344 /* don't try this for absolute or relative paths */
345 if (strchr(basename, '/'))
346 return basename;
347
348 while ((fullname = padvance(&path, basename)) != NULL) {
349 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
350 /*
351 * Don't bother freeing here, since it will
352 * be freed by the caller.
353 */
354 return fullname;
355 }
356 stunalloc(fullname);
357 }
358
359 /* not found in the PATH */
360 error("%s: not found", basename);
361 /* NOTREACHED */
362 }
363
364 int
dotcmd(int argc,char ** argv)365 dotcmd(int argc, char **argv)
366 {
367 exitstatus = 0;
368
369 if (argc >= 2) { /* That's what SVR2 does */
370 char *fullname;
371 struct stackmark smark;
372
373 setstackmark(&smark);
374 fullname = find_dot_file(argv[1]);
375 setinputfile(fullname, 1);
376 commandname = fullname;
377 cmdloop(0);
378 popfile();
379 popstackmark(&smark);
380 }
381 return exitstatus;
382 }
383
384
385 int
exitcmd(int argc,char ** argv)386 exitcmd(int argc, char **argv)
387 {
388 if (stoppedjobs())
389 return 0;
390 if (argc > 1)
391 exitstatus = number(argv[1]);
392 exitshell(exitstatus);
393 /* NOTREACHED */
394 }
395