2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1997-2005
5 * Herbert Xu <herbert@gondor.apana.org.au>. 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
72 short profile_buf
[16384];
75 MKINIT
struct jmploc main_handler
;
77 STATIC
void read_profile(const char *);
78 STATIC
char *find_dot_file(char *);
79 static int cmdloop(int);
80 int main(int, char **);
83 * Main routine. We initialize things, parse the arguments, execute
84 * profiles if we're a login shell, and then call cmdloop to execute
85 * commands. The setjmp call sets up the location to jump to when an
86 * exception occurs. When an exception occurs the variable "state"
87 * is used to figure out how far we had gotten.
91 main(int argc
, char **argv
)
95 struct stackmark smark
;
99 dash_errno
= __errno_location();
103 monitor(4, etext
, profile_buf
, sizeof profile_buf
, 50);
106 setlocale(LC_ALL
, "");
109 if (unlikely(setjmp(main_handler
.loc
))) {
118 if (e
== EXEND
|| e
== EXEXIT
|| s
== 0 || iflag
== 0 || shlvl
)
125 && (! attyset() || equal(termval(), "emacs"))
133 popstackmark(&smark
);
134 FORCEINTON
; /* enable interrupts */
144 handler
= &main_handler
;
147 trputs("Shell args: "); trargs(argv
);
151 setstackmark(&smark
);
152 login
= procargs(argc
, argv
);
155 read_profile("/etc/profile");
158 read_profile("$HOME/.profile");
164 getuid() == geteuid() && getgid() == getegid() &&
168 if ((shinit
= lookupvar("ENV")) != NULL
&& *shinit
!= '\0') {
169 read_profile(shinit
);
172 popstackmark(&smark
);
176 evalstring(minusc
, sflag
? 0 : EV_EXIT
);
178 if (sflag
|| minusc
== NULL
) {
179 state4
: /* XXX ??? - why isn't this before the "if" statement */
188 extern void _mcleanup(void);
198 * Read and execute commands. "Top" is nonzero for the top level command
199 * loop; it turns on prompting if the shell is interactive.
206 struct stackmark smark
;
211 TRACE(("cmdloop(%d) called\n", top
));
215 setstackmark(&smark
);
217 showjobs(out2
, SHOW_CHANGED
);
224 /* showtree(n); DEBUG */
226 if (!top
|| numeof
>= 50)
228 if (!stoppedjobs()) {
238 out2str("\nUse \"exit\" to leave shell.\n");
244 job_warning
= (job_warning
== 2) ? 1 : 0;
250 popstackmark(&smark
);
254 evalskip
&= ~(SKIPFUNC
| SKIPFUNCDEF
);
265 * Read /etc/profile or .profile. Return on error.
269 read_profile(const char *name
)
271 name
= expandstr(name
);
272 if (setinputfile(name
, INPUT_PUSH_FILE
| INPUT_NOFILE_OK
) < 0)
282 * Read a file containing shell functions.
286 readcmdfile(char *name
)
288 setinputfile(name
, INPUT_PUSH_FILE
);
296 * Take commands from a file. To be compatible we should do a path
297 * search for the file, which is necessary to find sub-commands.
302 find_dot_file(char *basename
)
305 const char *path
= pathval();
309 /* don't try this for absolute or relative paths */
310 if (strchr(basename
, '/'))
313 while ((len
= padvance(&path
, basename
)) >= 0) {
314 fullname
= stackblock();
315 if ((!pathopt
|| *pathopt
== 'f') &&
316 !stat64(fullname
, &statb
) && S_ISREG(statb
.st_mode
)) {
317 /* This will be freed by the caller. */
322 /* not found in the PATH */
323 sh_error("%s: not found", basename
);
328 dotcmd(int argc
, char **argv
)
338 fullname
= find_dot_file(*argv
);
339 setinputfile(fullname
, INPUT_PUSH_FILE
);
340 commandname
= fullname
;
350 exitcmd(int argc
, char **argv
)
356 savestatus
= number(argv
[1]);
366 handler
= &main_handler
;