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