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