1 /* $NetBSD: main.c,v 1.52 2008/10/16 14:55:28 dholland 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.52 2008/10/16 14:55:28 dholland Exp $");
84 short profile_buf
[16384];
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
;
108 setlocale(LC_ALL
, "");
111 monitor(4, etext
, profile_buf
, sizeof profile_buf
, 50);
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.
129 exitstatus
= exerrno
;
140 if (exception
!= EXSHELLPROC
) {
141 if (state
== 0 || iflag
== 0 || ! rootshell
)
142 exitshell(exitstatus
);
145 if (exception
== EXINT
147 && (! attyset() || equal(termval(), "emacs"))
153 popstackmark(&smark
);
154 FORCEINTON
; /* enable interrupts */
170 trputs("Shell args: "); trargs(argv
);
176 setstackmark(&smark
);
177 procargs(argc
, argv
);
178 if (argv
[0] && argv
[0][0] == '-') {
180 read_profile("/etc/profile");
183 read_profile(".profile");
187 if (getuid() == geteuid() && getgid() == getegid()) {
188 if ((shinit
= lookupvar("ENV")) != NULL
&& *shinit
!= '\0') {
190 read_profile(shinit
);
195 if (sflag
== 0 || minusc
) {
196 static int sigs
[] = {
197 SIGINT
, SIGQUIT
, SIGHUP
,
203 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
206 for (i
= 0; i
< SIGSSIZE
; i
++)
207 setsignal(sigs
[i
], 0);
211 evalstring(minusc
, 0);
213 if (sflag
|| minusc
== NULL
) {
214 state4
: /* XXX ??? - why isn't this before the "if" statement */
220 exitshell(exitstatus
);
226 * Read and execute commands. "Top" is nonzero for the top level command
227 * loop; it turns on prompting if the shell is interactive.
234 struct stackmark smark
;
238 TRACE(("cmdloop(%d) called\n", top
));
239 setstackmark(&smark
);
244 if (iflag
== 1 && top
) {
246 showjobs(out2
, SHOW_CHANGED
);
251 /* showtree(n); DEBUG */
253 if (!top
|| numeof
>= 50)
255 if (!stoppedjobs()) {
258 out2str("\nUse \"exit\" to leave shell.\n");
261 } else if (n
!= NULL
&& nflag
== 0) {
262 job_warning
= (job_warning
== 2) ? 1 : 0;
266 popstackmark(&smark
);
267 setstackmark(&smark
);
268 if (evalskip
== SKIPFILE
) {
273 popstackmark(&smark
);
279 * Read /etc/profile or .profile. Return on error.
283 read_profile(const char *name
)
290 if ((fd
= open(name
, O_RDONLY
)) >= 0)
295 /* -q turns off -x and -v just when executing init files */
298 xflag
= 0, xflag_set
= 1;
300 vflag
= 0, vflag_set
= 1;
315 * Read a file containing shell functions.
319 readcmdfile(char *name
)
324 if ((fd
= open(name
, O_RDONLY
)) >= 0)
327 error("Can't open %s", name
);
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.
342 find_dot_file(char *basename
)
345 const char *path
= pathval();
348 /* don't try this for absolute or relative paths */
349 if (strchr(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.
363 /* not found in the PATH */
364 error("%s: not found", basename
);
369 dotcmd(int argc
, char **argv
)
373 if (argc
>= 2) { /* That's what SVR2 does */
375 struct stackmark smark
;
377 setstackmark(&smark
);
378 fullname
= find_dot_file(argv
[1]);
379 setinputfile(fullname
, 1);
380 commandname
= fullname
;
383 popstackmark(&smark
);
390 exitcmd(int argc
, char **argv
)
395 exitstatus
= number(argv
[1]);
396 exitshell(exitstatus
);