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

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

KMK.

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