No empty .Rs/.Re
[netbsd-mini2440.git] / bin / sh / main.c
blob912a3acdff13cc04bc9680a6a48ddf9d203d7164
1 /* $NetBSD: main.c,v 1.52 2008/10/16 14:55:28 dholland Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
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.
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.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95";
44 #else
45 __RCSID("$NetBSD: main.c,v 1.52 2008/10/16 14:55:28 dholland Exp $");
46 #endif
47 #endif /* not lint */
49 #include <errno.h>
50 #include <stdio.h>
51 #include <signal.h>
52 #include <sys/stat.h>
53 #include <unistd.h>
54 #include <locale.h>
55 #include <fcntl.h>
58 #include "shell.h"
59 #include "main.h"
60 #include "mail.h"
61 #include "options.h"
62 #include "output.h"
63 #include "parser.h"
64 #include "nodes.h"
65 #include "expand.h"
66 #include "eval.h"
67 #include "jobs.h"
68 #include "input.h"
69 #include "trap.h"
70 #include "var.h"
71 #include "show.h"
72 #include "memalloc.h"
73 #include "error.h"
74 #include "init.h"
75 #include "mystring.h"
76 #include "exec.h"
77 #include "cd.h"
79 #define PROFILE 0
81 int rootpid;
82 int rootshell;
83 #if PROFILE
84 short profile_buf[16384];
85 extern int etext();
86 #endif
88 STATIC void read_profile(const char *);
89 STATIC char *find_dot_file(char *);
90 int main(int, char **);
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.
101 main(int argc, char **argv)
103 struct jmploc jmploc;
104 struct stackmark smark;
105 volatile int state;
106 char *shinit;
108 setlocale(LC_ALL, "");
110 #if PROFILE
111 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
112 #endif
113 state = 0;
114 if (setjmp(jmploc.loc)) {
116 * When a shell procedure is executed, we raise the
117 * exception EXSHELLPROC to clean up before executing
118 * the shell procedure.
120 switch (exception) {
121 case EXSHELLPROC:
122 rootpid = getpid();
123 rootshell = 1;
124 minusc = NULL;
125 state = 3;
126 break;
128 case EXEXEC:
129 exitstatus = exerrno;
130 break;
132 case EXERROR:
133 exitstatus = 2;
134 break;
136 default:
137 break;
140 if (exception != EXSHELLPROC) {
141 if (state == 0 || iflag == 0 || ! rootshell)
142 exitshell(exitstatus);
144 reset();
145 if (exception == EXINT
146 #if ATTY
147 && (! attyset() || equal(termval(), "emacs"))
148 #endif
150 out2c('\n');
151 flushout(&errout);
153 popstackmark(&smark);
154 FORCEINTON; /* enable interrupts */
155 if (state == 1)
156 goto state1;
157 else if (state == 2)
158 goto state2;
159 else if (state == 3)
160 goto state3;
161 else
162 goto state4;
164 handler = &jmploc;
165 #ifdef DEBUG
166 #if DEBUG == 2
167 debug = 1;
168 #endif
169 opentrace();
170 trputs("Shell args: "); trargs(argv);
171 #endif
172 rootpid = getpid();
173 rootshell = 1;
174 init();
175 initpwd();
176 setstackmark(&smark);
177 procargs(argc, argv);
178 if (argv[0] && argv[0][0] == '-') {
179 state = 1;
180 read_profile("/etc/profile");
181 state1:
182 state = 2;
183 read_profile(".profile");
185 state2:
186 state = 3;
187 if (getuid() == geteuid() && getgid() == getegid()) {
188 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
189 state = 3;
190 read_profile(shinit);
193 state3:
194 state = 4;
195 if (sflag == 0 || minusc) {
196 static int sigs[] = {
197 SIGINT, SIGQUIT, SIGHUP,
198 #ifdef SIGTSTP
199 SIGTSTP,
200 #endif
201 SIGPIPE
203 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
204 size_t i;
206 for (i = 0; i < SIGSSIZE; i++)
207 setsignal(sigs[i], 0);
210 if (minusc)
211 evalstring(minusc, 0);
213 if (sflag || minusc == NULL) {
214 state4: /* XXX ??? - why isn't this before the "if" statement */
215 cmdloop(1);
217 #if PROFILE
218 monitor(0);
219 #endif
220 exitshell(exitstatus);
221 /* NOTREACHED */
226 * Read and execute commands. "Top" is nonzero for the top level command
227 * loop; it turns on prompting if the shell is interactive.
230 void
231 cmdloop(int top)
233 union node *n;
234 struct stackmark smark;
235 int inter;
236 int numeof = 0;
238 TRACE(("cmdloop(%d) called\n", top));
239 setstackmark(&smark);
240 for (;;) {
241 if (pendingsigs)
242 dotrap();
243 inter = 0;
244 if (iflag == 1 && top) {
245 inter = 1;
246 showjobs(out2, SHOW_CHANGED);
247 chkmail(0);
248 flushout(&errout);
250 n = parsecmd(inter);
251 /* showtree(n); DEBUG */
252 if (n == NEOF) {
253 if (!top || numeof >= 50)
254 break;
255 if (!stoppedjobs()) {
256 if (!Iflag)
257 break;
258 out2str("\nUse \"exit\" to leave shell.\n");
260 numeof++;
261 } else if (n != NULL && nflag == 0) {
262 job_warning = (job_warning == 2) ? 1 : 0;
263 numeof = 0;
264 evaltree(n, 0);
266 popstackmark(&smark);
267 setstackmark(&smark);
268 if (evalskip == SKIPFILE) {
269 evalskip = 0;
270 break;
273 popstackmark(&smark);
279 * Read /etc/profile or .profile. Return on error.
282 STATIC void
283 read_profile(const char *name)
285 int fd;
286 int xflag_set = 0;
287 int vflag_set = 0;
289 INTOFF;
290 if ((fd = open(name, O_RDONLY)) >= 0)
291 setinputfd(fd, 1);
292 INTON;
293 if (fd < 0)
294 return;
295 /* -q turns off -x and -v just when executing init files */
296 if (qflag) {
297 if (xflag)
298 xflag = 0, xflag_set = 1;
299 if (vflag)
300 vflag = 0, vflag_set = 1;
302 cmdloop(0);
303 if (qflag) {
304 if (xflag_set)
305 xflag = 1;
306 if (vflag_set)
307 vflag = 1;
309 popfile();
315 * Read a file containing shell functions.
318 void
319 readcmdfile(char *name)
321 int fd;
323 INTOFF;
324 if ((fd = open(name, O_RDONLY)) >= 0)
325 setinputfd(fd, 1);
326 else
327 error("Can't open %s", name);
328 INTON;
329 cmdloop(0);
330 popfile();
336 * Take commands from a file. To be compatible we should do a path
337 * search for the file, which is necessary to find sub-commands.
341 STATIC char *
342 find_dot_file(char *basename)
344 char *fullname;
345 const char *path = pathval();
346 struct stat statb;
348 /* don't try this for absolute or relative paths */
349 if (strchr(basename, '/'))
350 return basename;
352 while ((fullname = padvance(&path, basename)) != NULL) {
353 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
355 * Don't bother freeing here, since it will
356 * be freed by the caller.
358 return fullname;
360 stunalloc(fullname);
363 /* not found in the PATH */
364 error("%s: not found", basename);
365 /* NOTREACHED */
369 dotcmd(int argc, char **argv)
371 exitstatus = 0;
373 if (argc >= 2) { /* That's what SVR2 does */
374 char *fullname;
375 struct stackmark smark;
377 setstackmark(&smark);
378 fullname = find_dot_file(argv[1]);
379 setinputfile(fullname, 1);
380 commandname = fullname;
381 cmdloop(0);
382 popfile();
383 popstackmark(&smark);
385 return exitstatus;
390 exitcmd(int argc, char **argv)
392 if (stoppedjobs())
393 return 0;
394 if (argc > 1)
395 exitstatus = number(argv[1]);
396 exitshell(exitstatus);
397 /* NOTREACHED */