Sync some manuals from bin & sbin with NetBSD-8
[minix.git] / bin / sh / main.c
blob819a745756bfc36b3ea9e2b259ce955a580586b5
1 /* $NetBSD: main.c,v 1.59 2015/05/26 21:35:15 christos 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.59 2015/05/26 21:35:15 christos 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 <stdlib.h>
55 #include <locale.h>
56 #include <fcntl.h>
59 #include "shell.h"
60 #include "main.h"
61 #include "mail.h"
62 #include "options.h"
63 #include "builtins.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"
81 #define PROFILE 0
83 int rootpid;
84 int rootshell;
85 int posix;
86 #if PROFILE
87 short profile_buf[16384];
88 extern int etext();
89 #endif
91 STATIC void read_profile(const char *);
92 int main(int, char **);
95 * Main routine. We initialize things, parse the arguments, execute
96 * profiles if we're a login shell, and then call cmdloop to execute
97 * commands. The setjmp call sets up the location to jump to when an
98 * exception occurs. When an exception occurs the variable "state"
99 * is used to figure out how far we had gotten.
103 main(int argc, char **argv)
105 struct jmploc jmploc;
106 struct stackmark smark;
107 volatile int state;
108 char *shinit;
109 uid_t uid;
110 gid_t gid;
112 uid = getuid();
113 gid = getgid();
115 setlocale(LC_ALL, "");
117 posix = getenv("POSIXLY_CORRECT") != NULL;
118 #if PROFILE
119 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
120 #endif
121 state = 0;
122 if (setjmp(jmploc.loc)) {
124 * When a shell procedure is executed, we raise the
125 * exception EXSHELLPROC to clean up before executing
126 * the shell procedure.
128 switch (exception) {
129 case EXSHELLPROC:
130 rootpid = getpid();
131 rootshell = 1;
132 minusc = NULL;
133 state = 3;
134 break;
136 case EXEXEC:
137 exitstatus = exerrno;
138 break;
140 case EXERROR:
141 exitstatus = 2;
142 break;
144 default:
145 break;
148 if (exception != EXSHELLPROC) {
149 if (state == 0 || iflag == 0 || ! rootshell)
150 exitshell(exitstatus);
152 reset();
153 if (exception == EXINT
154 #if ATTY
155 && (! attyset() || equal(termval(), "emacs"))
156 #endif
158 out2c('\n');
159 flushout(&errout);
161 popstackmark(&smark);
162 FORCEINTON; /* enable interrupts */
163 if (state == 1)
164 goto state1;
165 else if (state == 2)
166 goto state2;
167 else if (state == 3)
168 goto state3;
169 else
170 goto state4;
172 handler = &jmploc;
173 #ifdef DEBUG
174 #if DEBUG == 2
175 debug = 1;
176 #endif
177 opentrace();
178 trputs("Shell args: "); trargs(argv);
179 #endif
180 rootpid = getpid();
181 rootshell = 1;
182 init();
183 initpwd();
184 setstackmark(&smark);
185 procargs(argc, argv);
188 * Limit bogus system(3) or popen(3) calls in setuid binaries,
189 * by requiring the -p flag
191 if (!pflag && (uid != geteuid() || gid != getegid())) {
192 setuid(uid);
193 setgid(gid);
194 /* PS1 might need to be changed accordingly. */
195 choose_ps1();
198 if (argv[0] && argv[0][0] == '-') {
199 state = 1;
200 read_profile("/etc/profile");
201 state1:
202 state = 2;
203 read_profile(".profile");
205 state2:
206 state = 3;
207 if ((iflag || !posix) &&
208 getuid() == geteuid() && getgid() == getegid()) {
209 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
210 state = 3;
211 read_profile(shinit);
214 state3:
215 state = 4;
216 if (sflag == 0 || minusc) {
217 static int sigs[] = {
218 SIGINT, SIGQUIT, SIGHUP,
219 #ifdef SIGTSTP
220 SIGTSTP,
221 #endif
222 SIGPIPE
224 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
225 size_t i;
227 for (i = 0; i < SIGSSIZE; i++)
228 setsignal(sigs[i], 0);
231 if (minusc)
232 evalstring(minusc, 0);
234 if (sflag || minusc == NULL) {
235 state4: /* XXX ??? - why isn't this before the "if" statement */
236 cmdloop(1);
238 #if PROFILE
239 monitor(0);
240 #endif
241 exitshell(exitstatus);
242 /* NOTREACHED */
247 * Read and execute commands. "Top" is nonzero for the top level command
248 * loop; it turns on prompting if the shell is interactive.
251 void
252 cmdloop(int top)
254 union node *n;
255 struct stackmark smark;
256 int inter;
257 int numeof = 0;
258 enum skipstate skip;
260 TRACE(("cmdloop(%d) called\n", top));
261 setstackmark(&smark);
262 for (;;) {
263 if (pendingsigs)
264 dotrap();
265 inter = 0;
266 if (iflag == 1 && top) {
267 inter = 1;
268 showjobs(out2, SHOW_CHANGED);
269 chkmail(0);
270 flushout(&errout);
272 n = parsecmd(inter);
273 /* showtree(n); DEBUG */
274 if (n == NEOF) {
275 if (!top || numeof >= 50)
276 break;
277 if (!stoppedjobs()) {
278 if (!Iflag)
279 break;
280 out2str("\nUse \"exit\" to leave shell.\n");
282 numeof++;
283 } else if (n != NULL && nflag == 0) {
284 job_warning = (job_warning == 2) ? 1 : 0;
285 numeof = 0;
286 evaltree(n, 0);
288 popstackmark(&smark);
289 setstackmark(&smark);
292 * Any SKIP* can occur here! SKIP(FUNC|BREAK|CONT) occur when
293 * a dotcmd is in a loop or a function body and appropriate
294 * built-ins occurs in file scope in the sourced file. Values
295 * other than SKIPFILE are reset by the appropriate eval*()
296 * that contained the dotcmd() call.
298 skip = current_skipstate();
299 if (skip != SKIPNONE) {
300 if (skip == SKIPFILE)
301 stop_skipping();
302 break;
305 popstackmark(&smark);
311 * Read /etc/profile or .profile. Return on error.
314 STATIC void
315 read_profile(const char *name)
317 int fd;
318 int xflag_set = 0;
319 int vflag_set = 0;
321 INTOFF;
322 if ((fd = open(name, O_RDONLY)) >= 0)
323 setinputfd(fd, 1);
324 INTON;
325 if (fd < 0)
326 return;
327 /* -q turns off -x and -v just when executing init files */
328 if (qflag) {
329 if (xflag)
330 xflag = 0, xflag_set = 1;
331 if (vflag)
332 vflag = 0, vflag_set = 1;
334 cmdloop(0);
335 if (qflag) {
336 if (xflag_set)
337 xflag = 1;
338 if (vflag_set)
339 vflag = 1;
341 popfile();
347 * Read a file containing shell functions.
350 void
351 readcmdfile(char *name)
353 int fd;
355 INTOFF;
356 if ((fd = open(name, O_RDONLY)) >= 0)
357 setinputfd(fd, 1);
358 else
359 error("Can't open %s", name);
360 INTON;
361 cmdloop(0);
362 popfile();
368 exitcmd(int argc, char **argv)
370 if (stoppedjobs())
371 return 0;
372 if (argc > 1)
373 exitstatus = number(argv[1]);
374 exitshell(exitstatus);
375 /* NOTREACHED */