1 /* $NetBSD: main.c,v 1.59 2015/05/26 21:35:15 christos Exp $ */
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)main.c 8.7 (Berkeley) 7/19/95";
45 __RCSID("$NetBSD: main.c,v 1.59 2015/05/26 21:35:15 christos Exp $");
87 short profile_buf
[16384];
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
;
115 setlocale(LC_ALL
, "");
117 posix
= getenv("POSIXLY_CORRECT") != NULL
;
119 monitor(4, etext
, profile_buf
, sizeof profile_buf
, 50);
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.
137 exitstatus
= exerrno
;
148 if (exception
!= EXSHELLPROC
) {
149 if (state
== 0 || iflag
== 0 || ! rootshell
)
150 exitshell(exitstatus
);
153 if (exception
== EXINT
155 && (! attyset() || equal(termval(), "emacs"))
161 popstackmark(&smark
);
162 FORCEINTON
; /* enable interrupts */
178 trputs("Shell args: "); trargs(argv
);
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())) {
194 /* PS1 might need to be changed accordingly. */
198 if (argv
[0] && argv
[0][0] == '-') {
200 read_profile("/etc/profile");
203 read_profile(".profile");
207 if ((iflag
|| !posix
) &&
208 getuid() == geteuid() && getgid() == getegid()) {
209 if ((shinit
= lookupvar("ENV")) != NULL
&& *shinit
!= '\0') {
211 read_profile(shinit
);
216 if (sflag
== 0 || minusc
) {
217 static int sigs
[] = {
218 SIGINT
, SIGQUIT
, SIGHUP
,
224 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
227 for (i
= 0; i
< SIGSSIZE
; i
++)
228 setsignal(sigs
[i
], 0);
232 evalstring(minusc
, 0);
234 if (sflag
|| minusc
== NULL
) {
235 state4
: /* XXX ??? - why isn't this before the "if" statement */
241 exitshell(exitstatus
);
247 * Read and execute commands. "Top" is nonzero for the top level command
248 * loop; it turns on prompting if the shell is interactive.
255 struct stackmark smark
;
260 TRACE(("cmdloop(%d) called\n", top
));
261 setstackmark(&smark
);
266 if (iflag
== 1 && top
) {
268 showjobs(out2
, SHOW_CHANGED
);
273 /* showtree(n); DEBUG */
275 if (!top
|| numeof
>= 50)
277 if (!stoppedjobs()) {
280 out2str("\nUse \"exit\" to leave shell.\n");
283 } else if (n
!= NULL
&& nflag
== 0) {
284 job_warning
= (job_warning
== 2) ? 1 : 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
)
305 popstackmark(&smark
);
311 * Read /etc/profile or .profile. Return on error.
315 read_profile(const char *name
)
322 if ((fd
= open(name
, O_RDONLY
)) >= 0)
327 /* -q turns off -x and -v just when executing init files */
330 xflag
= 0, xflag_set
= 1;
332 vflag
= 0, vflag_set
= 1;
347 * Read a file containing shell functions.
351 readcmdfile(char *name
)
356 if ((fd
= open(name
, O_RDONLY
)) >= 0)
359 error("Can't open %s", name
);
368 exitcmd(int argc
, char **argv
)
373 exitstatus
= number(argv
[1]);
374 exitshell(exitstatus
);