source: trunk/src/ash/main.c@ 1077

Last change on this file since 1077 was 1077, checked in by bird, 18 years ago

Drop all the .profile, /etc/profile and ENV stuff as that will screw up sometimes.

  • Property svn:eol-style set to native
File size: 8.4 KB
Line 
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#ifdef HAVE_SYS_CDEFS_H
36#include <sys/cdefs.h>
37#endif
38#ifndef lint
39__COPYRIGHT("@(#) 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#if 0
45static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95";
46#else
47__RCSID("$NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv Exp $");
48#endif
49#endif /* not lint */
50
51#include <errno.h>
52#include <stdio.h>
53#include <signal.h>
54#include <sys/stat.h>
55#include <unistd.h>
56#include <locale.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#include "cd.h"
80
81#define PROFILE 0
82
83int rootpid;
84int rootshell;
85STATIC union node *curcmd;
86STATIC union node *prevcmd;
87#if PROFILE
88short profile_buf[16384];
89extern int etext();
90#endif
91
92#ifdef KMK
93#error "Huh? KMK isn't defined!"
94#else
95STATIC void read_profile(const char *);
96#endif
97STATIC char *find_dot_file(char *);
98int main(int, char **);
99
100/*
101 * Main routine. We initialize things, parse the arguments, execute
102 * profiles if we're a login shell, and then call cmdloop to execute
103 * commands. The setjmp call sets up the location to jump to when an
104 * exception occurs. When an exception occurs the variable "state"
105 * is used to figure out how far we had gotten.
106 */
107
108int
109main(int argc, char **argv)
110{
111 struct jmploc jmploc;
112 struct stackmark smark;
113 volatile int state;
114 char *shinit;
115
116 setlocale(LC_ALL, "");
117
118#if PROFILE
119 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
120#endif
121 state = 0;
122 if (setjmp(jmploc.loc)) {
123 /*
124 * When a shell procedure is executed, we raise the
125 * exception EXSHELLPROC to clean up before executing
126 * the shell procedure.
127 */
128 switch (exception) {
129 case EXSHELLPROC:
130 rootpid = getpid();
131 rootshell = 1;
132 minusc = NULL;
133 state = 3;
134 break;
135
136 case EXEXEC:
137 exitstatus = exerrno;
138 break;
139
140 case EXERROR:
141 exitstatus = 2;
142 break;
143
144 default:
145 break;
146 }
147
148 if (exception != EXSHELLPROC) {
149 if (state == 0 || iflag == 0 || ! rootshell)
150 exitshell(exitstatus);
151 }
152 reset();
153 if (exception == EXINT
154#if ATTY
155 && (! attyset() || equal(termval(), "emacs"))
156#endif
157 ) {
158 out2c('\n');
159 flushout(&errout);
160 }
161 popstackmark(&smark);
162 FORCEINTON; /* enable interrupts */
163 if (state == 1)
164 goto state1;
165 else if (state == 2)
166 goto state2;
167 else if (state == 3)
168 goto state3;
169 else
170 goto state4;
171 }
172 handler = &jmploc;
173#ifdef DEBUG
174#if DEBUG == 2
175 debug = 1;
176#endif
177 opentrace();
178 trputs("Shell args: "); trargs(argv);
179#endif
180 rootpid = getpid();
181 rootshell = 1;
182#ifdef _MSC_VER
183 {
184 extern void init_syntax(void);
185 init_syntax();
186 }
187#endif
188 init();
189 setstackmark(&smark);
190 procargs(argc, argv);
191 if (argv[0] && argv[0][0] == '-') {
192 state = 1;
193#ifndef KMK
194 read_profile("/etc/profile");
195#endif
196state1:
197 state = 2;
198#ifndef KMK
199 read_profile(".profile");
200#endif
201 }
202state2:
203 state = 3;
204#ifndef KMK
205 if (getuid() == geteuid() && getgid() == getegid()) {
206 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
207 state = 3;
208 read_profile(shinit);
209 }
210 }
211#endif
212state3:
213 state = 4;
214 if (sflag == 0 || minusc) {
215 static int sigs[] = {
216 SIGINT, SIGQUIT, SIGHUP,
217#ifdef SIGTSTP
218 SIGTSTP,
219#endif
220 SIGPIPE
221 };
222#define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
223 int i;
224
225 for (i = 0; i < SIGSSIZE; i++)
226 setsignal(sigs[i], 0);
227 }
228
229 if (minusc)
230 evalstring(minusc, 0);
231
232 if (sflag || minusc == NULL) {
233state4: /* XXX ??? - why isn't this before the "if" statement */
234 cmdloop(1);
235 }
236#if PROFILE
237 monitor(0);
238#endif
239 exitshell(exitstatus);
240 /* NOTREACHED */
241}
242
243
244/*
245 * Read and execute commands. "Top" is nonzero for the top level command
246 * loop; it turns on prompting if the shell is interactive.
247 */
248
249void
250cmdloop(int top)
251{
252 union node *n;
253 struct stackmark smark;
254 int inter;
255 int numeof = 0;
256
257 TRACE(("cmdloop(%d) called\n", top));
258 setstackmark(&smark);
259 for (;;) {
260 if (pendingsigs)
261 dotrap();
262 inter = 0;
263 if (iflag && top) {
264 inter = 1;
265 showjobs(out2, SHOW_CHANGED);
266 chkmail(0);
267 flushout(&errout);
268 }
269 n = parsecmd(inter);
270 /* showtree(n); DEBUG */
271 if (n == NEOF) {
272 if (!top || numeof >= 50)
273 break;
274 if (!stoppedjobs()) {
275 if (!Iflag)
276 break;
277 out2str("\nUse \"exit\" to leave shell.\n");
278 }
279 numeof++;
280 } else if (n != NULL && nflag == 0) {
281 job_warning = (job_warning == 2) ? 1 : 0;
282 numeof = 0;
283 evaltree(n, 0);
284 }
285 popstackmark(&smark);
286 setstackmark(&smark);
287 if (evalskip == SKIPFILE) {
288 evalskip = 0;
289 break;
290 }
291 }
292 popstackmark(&smark);
293}
294
295
296
297#ifndef KMK
298/*
299 * Read /etc/profile or .profile. Return on error.
300 */
301
302STATIC void
303read_profile(const char *name)
304{
305 int fd;
306 int xflag_set = 0;
307 int vflag_set = 0;
308
309 INTOFF;
310 if ((fd = open(name, O_RDONLY)) >= 0)
311 setinputfd(fd, 1);
312 INTON;
313 if (fd < 0)
314 return;
315 /* -q turns off -x and -v just when executing init files */
316 if (qflag) {
317 if (xflag)
318 xflag = 0, xflag_set = 1;
319 if (vflag)
320 vflag = 0, vflag_set = 1;
321 }
322 cmdloop(0);
323 if (qflag) {
324 if (xflag_set)
325 xflag = 1;
326 if (vflag_set)
327 vflag = 1;
328 }
329 popfile();
330}
331#endif /* !KMK */
332
333
334
335/*
336 * Read a file containing shell functions.
337 */
338
339void
340readcmdfile(char *name)
341{
342 int fd;
343
344 INTOFF;
345 if ((fd = open(name, O_RDONLY)) >= 0)
346 setinputfd(fd, 1);
347 else
348 error("Can't open %s", name);
349 INTON;
350 cmdloop(0);
351 popfile();
352}
353
354
355
356/*
357 * Take commands from a file. To be compatible we should do a path
358 * search for the file, which is necessary to find sub-commands.
359 */
360
361
362STATIC char *
363find_dot_file(char *basename)
364{
365 char *fullname;
366 const char *path = pathval();
367 struct stat statb;
368
369 /* don't try this for absolute or relative paths */
370 if (strchr(basename, '/'))
371 return basename;
372
373 while ((fullname = padvance(&path, basename)) != NULL) {
374 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
375 /*
376 * Don't bother freeing here, since it will
377 * be freed by the caller.
378 */
379 return fullname;
380 }
381 stunalloc(fullname);
382 }
383
384 /* not found in the PATH */
385 error("%s: not found", basename);
386 /* NOTREACHED */
387}
388
389int
390dotcmd(int argc, char **argv)
391{
392 exitstatus = 0;
393
394 if (argc >= 2) { /* That's what SVR2 does */
395 char *fullname;
396 struct stackmark smark;
397
398 setstackmark(&smark);
399 fullname = find_dot_file(argv[1]);
400 setinputfile(fullname, 1);
401 commandname = fullname;
402 cmdloop(0);
403 popfile();
404 popstackmark(&smark);
405 }
406 return exitstatus;
407}
408
409
410int
411exitcmd(int argc, char **argv)
412{
413 if (stoppedjobs())
414 return 0;
415 if (argc > 1)
416 exitstatus = number(argv[1]);
417 exitshell(exitstatus);
418 /* NOTREACHED */
419}
Note: See TracBrowser for help on using the repository browser.