source: trunk/src/ash-messup/main.c@ 879

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

hacking...

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