Deleted Added
full compact
main.c (18267) main.c (19240)
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
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.
23 *
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.
35 *
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
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.
23 *
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.
35 *
36 * $Id: main.c,v 1.7 1996/09/12 02:23:33 bde Exp $
36 * $Id: main.c,v 1.8 1996/09/12 12:41:46 adam Exp $
37 */
38
39#ifndef lint
40static 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
46static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/28/95";
47#endif /* not lint */
48
49#include <stdio.h>
50#include <signal.h>
51#include <sys/stat.h>
52#include <unistd.h>
53#include <fcntl.h>
54#include <locale.h>
55
56
57#include "shell.h"
58#include "main.h"
59#include "mail.h"
60#include "options.h"
61#include "output.h"
62#include "parser.h"
63#include "nodes.h"
64#include "expand.h"
65#include "eval.h"
66#include "jobs.h"
67#include "input.h"
68#include "trap.h"
69#include "var.h"
70#include "show.h"
71#include "memalloc.h"
72#include "error.h"
73#include "init.h"
74#include "mystring.h"
75#include "exec.h"
76
77#define PROFILE 0
78
79int rootpid;
80int rootshell;
81STATIC union node *curcmd;
82STATIC union node *prevcmd;
83extern int errno;
84#if PROFILE
85short profile_buf[16384];
86extern int etext();
87#endif
88
89STATIC void read_profile __P((char *));
90STATIC char *find_dot_file __P((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
100int
101main(argc, argv)
102 int argc;
103 char **argv;
104{
105 struct jmploc jmploc;
106 struct stackmark smark;
107 volatile int state;
108 char *shinit;
109
110#if PROFILE
111 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
112#endif
113 (void) setlocale(LC_ALL, "");
114 state = 0;
115 if (setjmp(jmploc.loc)) {
116 /*
117 * When a shell procedure is executed, we raise the
118 * exception EXSHELLPROC to clean up before executing
119 * the shell procedure.
120 */
121 if (exception == EXERROR)
122 exitstatus = 2;
123 if (exception == EXSHELLPROC) {
124 rootpid = getpid();
125 rootshell = 1;
126 minusc = NULL;
127 state = 3;
128 } else if (state == 0 || iflag == 0 || ! rootshell)
129 exitshell(2);
130 reset();
131 if (exception == EXINT
132#if ATTY
133 && (! attyset() || equal(termval(), "emacs"))
134#endif
135 ) {
136 out2c('\n');
137 flushout(&errout);
138 }
139 popstackmark(&smark);
140 FORCEINTON; /* enable interrupts */
141 if (state == 1)
142 goto state1;
143 else if (state == 2)
144 goto state2;
145 else if (state == 3)
146 goto state3;
147 else
148 goto state4;
149 }
150 handler = &jmploc;
151#ifdef DEBUG
152 opentrace();
153 trputs("Shell args: "); trargs(argv);
154#endif
155 rootpid = getpid();
156 rootshell = 1;
157 init();
158 setstackmark(&smark);
159 procargs(argc, argv);
160 if (argv[0] && argv[0][0] == '-') {
161 state = 1;
162 read_profile("/etc/profile");
163state1:
164 state = 2;
37 */
38
39#ifndef lint
40static 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
46static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/28/95";
47#endif /* not lint */
48
49#include <stdio.h>
50#include <signal.h>
51#include <sys/stat.h>
52#include <unistd.h>
53#include <fcntl.h>
54#include <locale.h>
55
56
57#include "shell.h"
58#include "main.h"
59#include "mail.h"
60#include "options.h"
61#include "output.h"
62#include "parser.h"
63#include "nodes.h"
64#include "expand.h"
65#include "eval.h"
66#include "jobs.h"
67#include "input.h"
68#include "trap.h"
69#include "var.h"
70#include "show.h"
71#include "memalloc.h"
72#include "error.h"
73#include "init.h"
74#include "mystring.h"
75#include "exec.h"
76
77#define PROFILE 0
78
79int rootpid;
80int rootshell;
81STATIC union node *curcmd;
82STATIC union node *prevcmd;
83extern int errno;
84#if PROFILE
85short profile_buf[16384];
86extern int etext();
87#endif
88
89STATIC void read_profile __P((char *));
90STATIC char *find_dot_file __P((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
100int
101main(argc, argv)
102 int argc;
103 char **argv;
104{
105 struct jmploc jmploc;
106 struct stackmark smark;
107 volatile int state;
108 char *shinit;
109
110#if PROFILE
111 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
112#endif
113 (void) setlocale(LC_ALL, "");
114 state = 0;
115 if (setjmp(jmploc.loc)) {
116 /*
117 * When a shell procedure is executed, we raise the
118 * exception EXSHELLPROC to clean up before executing
119 * the shell procedure.
120 */
121 if (exception == EXERROR)
122 exitstatus = 2;
123 if (exception == EXSHELLPROC) {
124 rootpid = getpid();
125 rootshell = 1;
126 minusc = NULL;
127 state = 3;
128 } else if (state == 0 || iflag == 0 || ! rootshell)
129 exitshell(2);
130 reset();
131 if (exception == EXINT
132#if ATTY
133 && (! attyset() || equal(termval(), "emacs"))
134#endif
135 ) {
136 out2c('\n');
137 flushout(&errout);
138 }
139 popstackmark(&smark);
140 FORCEINTON; /* enable interrupts */
141 if (state == 1)
142 goto state1;
143 else if (state == 2)
144 goto state2;
145 else if (state == 3)
146 goto state3;
147 else
148 goto state4;
149 }
150 handler = &jmploc;
151#ifdef DEBUG
152 opentrace();
153 trputs("Shell args: "); trargs(argv);
154#endif
155 rootpid = getpid();
156 rootshell = 1;
157 init();
158 setstackmark(&smark);
159 procargs(argc, argv);
160 if (argv[0] && argv[0][0] == '-') {
161 state = 1;
162 read_profile("/etc/profile");
163state1:
164 state = 2;
165 read_profile(".profile");
165 if (privileged == 0)
166 read_profile(".profile");
167 else
168 read_profile("/etc/suid_profile");
166 }
167state2:
168 state = 3;
169 }
170state2:
171 state = 3;
169 if (getuid() == geteuid() && getgid() == getegid()) {
172 if (privileged == 0) {
170 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
171 state = 3;
172 read_profile(shinit);
173 }
174 }
175state3:
176 state = 4;
177 if (minusc) {
178 evalstring(minusc);
179 }
180 if (sflag || minusc == NULL) {
181state4: /* XXX ??? - why isn't this before the "if" statement */
182 cmdloop(1);
183 }
184#if PROFILE
185 monitor(0);
186#endif
187 exitshell(exitstatus);
188 /*NOTREACHED*/
189 return 0;
190}
191
192
193/*
194 * Read and execute commands. "Top" is nonzero for the top level command
195 * loop; it turns on prompting if the shell is interactive.
196 */
197
198void
199cmdloop(top)
200 int top;
201{
202 union node *n;
203 struct stackmark smark;
204 int inter;
205 int numeof = 0;
206
207 TRACE(("cmdloop(%d) called\n", top));
208 setstackmark(&smark);
209 for (;;) {
210 if (pendingsigs)
211 dotrap();
212 inter = 0;
213 if (iflag && top) {
214 inter++;
215 showjobs(1);
216 chkmail(0);
217 flushout(&output);
218 }
219 n = parsecmd(inter);
220 /* showtree(n); DEBUG */
221 if (n == NEOF) {
222 if (!top || numeof >= 50)
223 break;
224 if (!stoppedjobs()) {
225 if (!Iflag)
226 break;
227 out2str("\nUse \"exit\" to leave shell.\n");
228 }
229 numeof++;
230 } else if (n != NULL && nflag == 0) {
231 job_warning = (job_warning == 2) ? 1 : 0;
232 numeof = 0;
233 evaltree(n, 0);
234 }
235 popstackmark(&smark);
236 }
237 popstackmark(&smark); /* unnecessary */
238}
239
240
241
242/*
243 * Read /etc/profile or .profile. Return on error.
244 */
245
246STATIC void
247read_profile(name)
248 char *name;
249 {
250 int fd;
251
252 INTOFF;
253 if ((fd = open(name, O_RDONLY)) >= 0)
254 setinputfd(fd, 1);
255 INTON;
256 if (fd < 0)
257 return;
258 cmdloop(0);
259 popfile();
260}
261
262
263
264/*
265 * Read a file containing shell functions.
266 */
267
268void
269readcmdfile(name)
270 char *name;
271{
272 int fd;
273
274 INTOFF;
275 if ((fd = open(name, O_RDONLY)) >= 0)
276 setinputfd(fd, 1);
277 else
278 error("Can't open %s", name);
279 INTON;
280 cmdloop(0);
281 popfile();
282}
283
284
285
286/*
287 * Take commands from a file. To be compatable we should do a path
288 * search for the file, which is necessary to find sub-commands.
289 */
290
291
292STATIC char *
293find_dot_file(basename)
294 char *basename;
295{
296 static char localname[FILENAME_MAX+1];
297 char *fullname;
298 char *path = pathval();
299 struct stat statb;
300
301 /* don't try this for absolute or relative paths */
302 if( strchr(basename, '/'))
303 return basename;
304
305 while ((fullname = padvance(&path, basename)) != NULL) {
306 strcpy(localname, fullname);
307 stunalloc(fullname);
308 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
309 return localname;
310 }
311 return basename;
312}
313
314int
315dotcmd(argc, argv)
316 int argc;
317 char **argv;
318{
319 struct strlist *sp;
320 exitstatus = 0;
321
322 for (sp = cmdenviron; sp ; sp = sp->next)
323 setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED);
324
325 if (argc >= 2) { /* That's what SVR2 does */
326 char *fullname = find_dot_file(argv[1]);
327
328 setinputfile(fullname, 1);
329 commandname = fullname;
330 cmdloop(0);
331 popfile();
332 }
333 return exitstatus;
334}
335
336
337int
338exitcmd(argc, argv)
339 int argc;
340 char **argv;
341{
342 extern int oexitstatus;
343
344 if (stoppedjobs())
345 return 0;
346 exitstatus = (argc > 1) ? number(argv[1]) : oexitstatus;
347 exitshell(exitstatus);
348 /*NOTREACHED*/
349 return 0;
350}
351
352
353#ifdef notdef
354/*
355 * Should never be called.
356 */
357
358void
359exit(exitstatus) {
360 _exit(exitstatus);
361}
362#endif
173 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
174 state = 3;
175 read_profile(shinit);
176 }
177 }
178state3:
179 state = 4;
180 if (minusc) {
181 evalstring(minusc);
182 }
183 if (sflag || minusc == NULL) {
184state4: /* XXX ??? - why isn't this before the "if" statement */
185 cmdloop(1);
186 }
187#if PROFILE
188 monitor(0);
189#endif
190 exitshell(exitstatus);
191 /*NOTREACHED*/
192 return 0;
193}
194
195
196/*
197 * Read and execute commands. "Top" is nonzero for the top level command
198 * loop; it turns on prompting if the shell is interactive.
199 */
200
201void
202cmdloop(top)
203 int top;
204{
205 union node *n;
206 struct stackmark smark;
207 int inter;
208 int numeof = 0;
209
210 TRACE(("cmdloop(%d) called\n", top));
211 setstackmark(&smark);
212 for (;;) {
213 if (pendingsigs)
214 dotrap();
215 inter = 0;
216 if (iflag && top) {
217 inter++;
218 showjobs(1);
219 chkmail(0);
220 flushout(&output);
221 }
222 n = parsecmd(inter);
223 /* showtree(n); DEBUG */
224 if (n == NEOF) {
225 if (!top || numeof >= 50)
226 break;
227 if (!stoppedjobs()) {
228 if (!Iflag)
229 break;
230 out2str("\nUse \"exit\" to leave shell.\n");
231 }
232 numeof++;
233 } else if (n != NULL && nflag == 0) {
234 job_warning = (job_warning == 2) ? 1 : 0;
235 numeof = 0;
236 evaltree(n, 0);
237 }
238 popstackmark(&smark);
239 }
240 popstackmark(&smark); /* unnecessary */
241}
242
243
244
245/*
246 * Read /etc/profile or .profile. Return on error.
247 */
248
249STATIC void
250read_profile(name)
251 char *name;
252 {
253 int fd;
254
255 INTOFF;
256 if ((fd = open(name, O_RDONLY)) >= 0)
257 setinputfd(fd, 1);
258 INTON;
259 if (fd < 0)
260 return;
261 cmdloop(0);
262 popfile();
263}
264
265
266
267/*
268 * Read a file containing shell functions.
269 */
270
271void
272readcmdfile(name)
273 char *name;
274{
275 int fd;
276
277 INTOFF;
278 if ((fd = open(name, O_RDONLY)) >= 0)
279 setinputfd(fd, 1);
280 else
281 error("Can't open %s", name);
282 INTON;
283 cmdloop(0);
284 popfile();
285}
286
287
288
289/*
290 * Take commands from a file. To be compatable we should do a path
291 * search for the file, which is necessary to find sub-commands.
292 */
293
294
295STATIC char *
296find_dot_file(basename)
297 char *basename;
298{
299 static char localname[FILENAME_MAX+1];
300 char *fullname;
301 char *path = pathval();
302 struct stat statb;
303
304 /* don't try this for absolute or relative paths */
305 if( strchr(basename, '/'))
306 return basename;
307
308 while ((fullname = padvance(&path, basename)) != NULL) {
309 strcpy(localname, fullname);
310 stunalloc(fullname);
311 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
312 return localname;
313 }
314 return basename;
315}
316
317int
318dotcmd(argc, argv)
319 int argc;
320 char **argv;
321{
322 struct strlist *sp;
323 exitstatus = 0;
324
325 for (sp = cmdenviron; sp ; sp = sp->next)
326 setvareq(savestr(sp->text), VSTRFIXED|VTEXTFIXED);
327
328 if (argc >= 2) { /* That's what SVR2 does */
329 char *fullname = find_dot_file(argv[1]);
330
331 setinputfile(fullname, 1);
332 commandname = fullname;
333 cmdloop(0);
334 popfile();
335 }
336 return exitstatus;
337}
338
339
340int
341exitcmd(argc, argv)
342 int argc;
343 char **argv;
344{
345 extern int oexitstatus;
346
347 if (stoppedjobs())
348 return 0;
349 exitstatus = (argc > 1) ? number(argv[1]) : oexitstatus;
350 exitshell(exitstatus);
351 /*NOTREACHED*/
352 return 0;
353}
354
355
356#ifdef notdef
357/*
358 * Should never be called.
359 */
360
361void
362exit(exitstatus) {
363 _exit(exitstatus);
364}
365#endif