]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/sh/main.c
MFC
[FreeBSD/FreeBSD.git] / bin / sh / main.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 static char const copyright[] =
35 "@(#) Copyright (c) 1991, 1993\n\
36         The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)main.c      8.6 (Berkeley) 5/28/95";
42 #endif
43 #endif /* not lint */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <stdio.h>
48 #include <signal.h>
49 #include <sys/stat.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <locale.h>
53 #include <errno.h>
54
55 #include "shell.h"
56 #include "main.h"
57 #include "mail.h"
58 #include "options.h"
59 #include "output.h"
60 #include "parser.h"
61 #include "nodes.h"
62 #include "expand.h"
63 #include "eval.h"
64 #include "jobs.h"
65 #include "input.h"
66 #include "trap.h"
67 #include "var.h"
68 #include "show.h"
69 #include "memalloc.h"
70 #include "error.h"
71 #include "init.h"
72 #include "mystring.h"
73 #include "exec.h"
74 #include "cd.h"
75
76 int rootpid;
77 int rootshell;
78 struct jmploc main_handler;
79
80 static void read_profile(const char *);
81 static char *find_dot_file(char *);
82
83 /*
84  * Main routine.  We initialize things, parse the arguments, execute
85  * profiles if we're a login shell, and then call cmdloop to execute
86  * commands.  The setjmp call sets up the location to jump to when an
87  * exception occurs.  When an exception occurs the variable "state"
88  * is used to figure out how far we had gotten.
89  */
90
91 int
92 main(int argc, char *argv[])
93 {
94         struct stackmark smark;
95         volatile int state;
96         char *shinit;
97
98         (void) setlocale(LC_ALL, "");
99         state = 0;
100         if (setjmp(main_handler.loc)) {
101                 switch (exception) {
102                 case EXEXEC:
103                         exitstatus = exerrno;
104                         break;
105
106                 case EXERROR:
107                         exitstatus = 2;
108                         break;
109
110                 default:
111                         break;
112                 }
113
114                 if (state == 0 || iflag == 0 || ! rootshell ||
115                     exception == EXEXIT)
116                         exitshell(exitstatus);
117                 reset();
118                 if (exception == EXINT)
119                         out2fmt_flush("\n");
120                 popstackmark(&smark);
121                 FORCEINTON;                             /* enable interrupts */
122                 if (state == 1)
123                         goto state1;
124                 else if (state == 2)
125                         goto state2;
126                 else if (state == 3)
127                         goto state3;
128                 else
129                         goto state4;
130         }
131         handler = &main_handler;
132 #ifdef DEBUG
133         opentrace();
134         trputs("Shell args:  ");  trargs(argv);
135 #endif
136         rootpid = getpid();
137         rootshell = 1;
138         init();
139         setstackmark(&smark);
140         procargs(argc, argv);
141         pwd_init(iflag);
142         if (iflag)
143                 chkmail(1);
144         if (argv[0] && argv[0][0] == '-') {
145                 state = 1;
146                 read_profile("/etc/profile");
147 state1:
148                 state = 2;
149                 if (privileged == 0)
150                         read_profile(".profile");
151                 else
152                         read_profile("/etc/suid_profile");
153         }
154 state2:
155         state = 3;
156         if (!privileged && iflag) {
157                 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
158                         state = 3;
159                         read_profile(shinit);
160                 }
161         }
162 state3:
163         state = 4;
164         if (minusc) {
165                 evalstring(minusc, sflag ? 0 : EV_EXIT);
166         }
167         if (sflag || minusc == NULL) {
168 state4: /* XXX ??? - why isn't this before the "if" statement */
169                 cmdloop(1);
170         }
171         exitshell(exitstatus);
172         /*NOTREACHED*/
173         return 0;
174 }
175
176
177 /*
178  * Read and execute commands.  "Top" is nonzero for the top level command
179  * loop; it turns on prompting if the shell is interactive.
180  */
181
182 void
183 cmdloop(int top)
184 {
185         union node *n;
186         struct stackmark smark;
187         int inter;
188         int numeof = 0;
189
190         TRACE(("cmdloop(%d) called\n", top));
191         setstackmark(&smark);
192         for (;;) {
193                 if (pendingsigs)
194                         dotrap();
195                 inter = 0;
196                 if (iflag && top) {
197                         inter++;
198                         showjobs(1, SHOWJOBS_DEFAULT);
199                         chkmail(0);
200                         flushout(&output);
201                 }
202                 n = parsecmd(inter);
203                 /* showtree(n); DEBUG */
204                 if (n == NEOF) {
205                         if (!top || numeof >= 50)
206                                 break;
207                         if (!stoppedjobs()) {
208                                 if (!Iflag)
209                                         break;
210                                 out2fmt_flush("\nUse \"exit\" to leave shell.\n");
211                         }
212                         numeof++;
213                 } else if (n != NULL && nflag == 0) {
214                         job_warning = (job_warning == 2) ? 1 : 0;
215                         numeof = 0;
216                         evaltree(n, 0);
217                 }
218                 popstackmark(&smark);
219                 setstackmark(&smark);
220                 if (evalskip != 0) {
221                         if (evalskip == SKIPFILE)
222                                 evalskip = 0;
223                         break;
224                 }
225         }
226         popstackmark(&smark);
227 }
228
229
230
231 /*
232  * Read /etc/profile or .profile.  Return on error.
233  */
234
235 static void
236 read_profile(const char *name)
237 {
238         int fd;
239
240         INTOFF;
241         if ((fd = open(name, O_RDONLY)) >= 0)
242                 setinputfd(fd, 1);
243         INTON;
244         if (fd < 0)
245                 return;
246         cmdloop(0);
247         popfile();
248 }
249
250
251
252 /*
253  * Read a file containing shell functions.
254  */
255
256 void
257 readcmdfile(const char *name)
258 {
259         int fd;
260
261         INTOFF;
262         if ((fd = open(name, O_RDONLY)) >= 0)
263                 setinputfd(fd, 1);
264         else
265                 error("Can't open %s: %s", name, strerror(errno));
266         INTON;
267         cmdloop(0);
268         popfile();
269 }
270
271
272
273 /*
274  * Take commands from a file.  To be compatible we should do a path
275  * search for the file, which is necessary to find sub-commands.
276  */
277
278
279 static char *
280 find_dot_file(char *basename)
281 {
282         static char localname[FILENAME_MAX+1];
283         char *fullname;
284         const char *path = pathval();
285         struct stat statb;
286
287         /* don't try this for absolute or relative paths */
288         if( strchr(basename, '/'))
289                 return basename;
290
291         while ((fullname = padvance(&path, basename)) != NULL) {
292                 strcpy(localname, fullname);
293                 stunalloc(fullname);
294                 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
295                         return localname;
296         }
297         return basename;
298 }
299
300 int
301 dotcmd(int argc, char **argv)
302 {
303         char *filename, *fullname;
304
305         if (argc < 2)
306                 error("missing filename");
307
308         exitstatus = 0;
309
310         /*
311          * Because we have historically not supported any options,
312          * only treat "--" specially.
313          */
314         filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1];
315
316         fullname = find_dot_file(filename);
317         setinputfile(fullname, 1);
318         commandname = fullname;
319         cmdloop(0);
320         popfile();
321         return exitstatus;
322 }
323
324
325 int
326 exitcmd(int argc, char **argv)
327 {
328         if (stoppedjobs())
329                 return 0;
330         if (argc > 1)
331                 exitshell(number(argv[1]));
332         else
333                 exitshell_savedstatus();
334 }