]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - bin/sh/main.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 #include "builtins.h"
76
77 int rootpid;
78 int rootshell;
79 struct jmploc main_handler;
80 int localeisutf8, initial_localeisutf8;
81
82 static void cmdloop(int);
83 static void read_profile(char *);
84 static char *find_dot_file(char *);
85
86 /*
87  * Main routine.  We initialize things, parse the arguments, execute
88  * profiles if we're a login shell, and then call cmdloop to execute
89  * commands.  The setjmp call sets up the location to jump to when an
90  * exception occurs.  When an exception occurs the variable "state"
91  * is used to figure out how far we had gotten.
92  */
93
94 int
95 main(int argc, char *argv[])
96 {
97         struct stackmark smark, smark2;
98         volatile int state;
99         char *shinit;
100
101         (void) setlocale(LC_ALL, "");
102         initcharset();
103         state = 0;
104         if (setjmp(main_handler.loc)) {
105                 switch (exception) {
106                 case EXEXEC:
107                         exitstatus = exerrno;
108                         break;
109
110                 case EXERROR:
111                         exitstatus = 2;
112                         break;
113
114                 default:
115                         break;
116                 }
117
118                 if (state == 0 || iflag == 0 || ! rootshell ||
119                     exception == EXEXIT)
120                         exitshell(exitstatus);
121                 reset();
122                 if (exception == EXINT)
123                         out2fmt_flush("\n");
124                 popstackmark(&smark);
125                 FORCEINTON;                             /* enable interrupts */
126                 if (state == 1)
127                         goto state1;
128                 else if (state == 2)
129                         goto state2;
130                 else if (state == 3)
131                         goto state3;
132                 else
133                         goto state4;
134         }
135         handler = &main_handler;
136 #ifdef DEBUG
137         opentrace();
138         trputs("Shell args:  ");  trargs(argv);
139 #endif
140         rootpid = getpid();
141         rootshell = 1;
142         init();
143         setstackmark(&smark);
144         setstackmark(&smark2);
145         procargs(argc, argv);
146         pwd_init(iflag);
147         if (iflag)
148                 chkmail(1);
149         if (argv[0] && argv[0][0] == '-') {
150                 state = 1;
151                 read_profile("/etc/profile");
152 state1:
153                 state = 2;
154                 if (privileged == 0)
155                         read_profile("${HOME-}/.profile");
156                 else
157                         read_profile("/etc/suid_profile");
158         }
159 state2:
160         state = 3;
161         if (!privileged && iflag) {
162                 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
163                         state = 3;
164                         read_profile(shinit);
165                 }
166         }
167 state3:
168         state = 4;
169         popstackmark(&smark2);
170         if (minusc) {
171                 evalstring(minusc, sflag ? 0 : EV_EXIT);
172         }
173         if (sflag || minusc == NULL) {
174 state4: /* XXX ??? - why isn't this before the "if" statement */
175                 cmdloop(1);
176         }
177         exitshell(exitstatus);
178         /*NOTREACHED*/
179         return 0;
180 }
181
182
183 /*
184  * Read and execute commands.  "Top" is nonzero for the top level command
185  * loop; it turns on prompting if the shell is interactive.
186  */
187
188 static void
189 cmdloop(int top)
190 {
191         union node *n;
192         struct stackmark smark;
193         int inter;
194         int numeof = 0;
195
196         TRACE(("cmdloop(%d) called\n", top));
197         setstackmark(&smark);
198         for (;;) {
199                 if (pendingsigs)
200                         dotrap();
201                 inter = 0;
202                 if (iflag && top) {
203                         inter++;
204                         showjobs(1, SHOWJOBS_DEFAULT);
205                         chkmail(0);
206                         flushout(&output);
207                 }
208                 n = parsecmd(inter);
209                 /* showtree(n); DEBUG */
210                 if (n == NEOF) {
211                         if (!top || numeof >= 50)
212                                 break;
213                         if (!stoppedjobs()) {
214                                 if (!Iflag)
215                                         break;
216                                 out2fmt_flush("\nUse \"exit\" to leave shell.\n");
217                         }
218                         numeof++;
219                 } else if (n != NULL && nflag == 0) {
220                         job_warning = (job_warning == 2) ? 1 : 0;
221                         numeof = 0;
222                         evaltree(n, 0);
223                 }
224                 popstackmark(&smark);
225                 setstackmark(&smark);
226                 if (evalskip != 0) {
227                         if (evalskip == SKIPFILE)
228                                 evalskip = 0;
229                         break;
230                 }
231         }
232         popstackmark(&smark);
233 }
234
235
236
237 /*
238  * Read /etc/profile or .profile.  Return on error.
239  */
240
241 static void
242 read_profile(char *name)
243 {
244         int fd;
245         const char *expandedname;
246
247         expandedname = expandstr(name);
248         if (expandedname == NULL)
249                 return;
250         INTOFF;
251         if ((fd = open(expandedname, O_RDONLY)) >= 0)
252                 setinputfd(fd, 1);
253         INTON;
254         if (fd < 0)
255                 return;
256         cmdloop(0);
257         popfile();
258 }
259
260
261
262 /*
263  * Read a file containing shell functions.
264  */
265
266 void
267 readcmdfile(const char *name)
268 {
269         setinputfile(name, 1);
270         cmdloop(0);
271         popfile();
272 }
273
274
275
276 /*
277  * Take commands from a file.  To be compatible we should do a path
278  * search for the file, which is necessary to find sub-commands.
279  */
280
281
282 static char *
283 find_dot_file(char *basename)
284 {
285         char *fullname;
286         const char *path = pathval();
287         struct stat statb;
288
289         /* don't try this for absolute or relative paths */
290         if( strchr(basename, '/'))
291                 return basename;
292
293         while ((fullname = padvance(&path, basename)) != NULL) {
294                 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
295                         /*
296                          * Don't bother freeing here, since it will
297                          * be freed by the caller.
298                          */
299                         return fullname;
300                 }
301                 stunalloc(fullname);
302         }
303         return basename;
304 }
305
306 int
307 dotcmd(int argc, char **argv)
308 {
309         char *filename, *fullname;
310
311         if (argc < 2)
312                 error("missing filename");
313
314         exitstatus = 0;
315
316         /*
317          * Because we have historically not supported any options,
318          * only treat "--" specially.
319          */
320         filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1];
321
322         fullname = find_dot_file(filename);
323         setinputfile(fullname, 1);
324         commandname = fullname;
325         cmdloop(0);
326         popfile();
327         return exitstatus;
328 }
329
330
331 int
332 exitcmd(int argc, char **argv)
333 {
334         if (stoppedjobs())
335                 return 0;
336         if (argc > 1)
337                 exitshell(number(argv[1]));
338         else
339                 exitshell_savedstatus();
340 }