]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/sh/main.c
sh: Fix some warnings in code for arithmetic expressions.
[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                         exitshell(exitstatus);
116                 reset();
117                 if (exception == EXINT)
118                         out2fmt_flush("\n");
119                 popstackmark(&smark);
120                 FORCEINTON;                             /* enable interrupts */
121                 if (state == 1)
122                         goto state1;
123                 else if (state == 2)
124                         goto state2;
125                 else if (state == 3)
126                         goto state3;
127                 else
128                         goto state4;
129         }
130         handler = &main_handler;
131 #ifdef DEBUG
132         opentrace();
133         trputs("Shell args:  ");  trargs(argv);
134 #endif
135         rootpid = getpid();
136         rootshell = 1;
137         init();
138         setstackmark(&smark);
139         procargs(argc, argv);
140         pwd_init(iflag);
141         if (iflag)
142                 chkmail(1);
143         if (argv[0] && argv[0][0] == '-') {
144                 state = 1;
145                 read_profile("/etc/profile");
146 state1:
147                 state = 2;
148                 if (privileged == 0)
149                         read_profile(".profile");
150                 else
151                         read_profile("/etc/suid_profile");
152         }
153 state2:
154         state = 3;
155         if (!privileged && iflag) {
156                 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
157                         state = 3;
158                         read_profile(shinit);
159                 }
160         }
161 state3:
162         state = 4;
163         if (minusc) {
164                 evalstring(minusc, sflag ? 0 : EV_EXIT);
165         }
166         if (sflag || minusc == NULL) {
167 state4: /* XXX ??? - why isn't this before the "if" statement */
168                 cmdloop(1);
169         }
170         exitshell(exitstatus);
171         /*NOTREACHED*/
172         return 0;
173 }
174
175
176 /*
177  * Read and execute commands.  "Top" is nonzero for the top level command
178  * loop; it turns on prompting if the shell is interactive.
179  */
180
181 void
182 cmdloop(int top)
183 {
184         union node *n;
185         struct stackmark smark;
186         int inter;
187         int numeof = 0;
188
189         TRACE(("cmdloop(%d) called\n", top));
190         setstackmark(&smark);
191         for (;;) {
192                 if (pendingsigs)
193                         dotrap();
194                 inter = 0;
195                 if (iflag && top) {
196                         inter++;
197                         showjobs(1, SHOWJOBS_DEFAULT);
198                         chkmail(0);
199                         flushout(&output);
200                 }
201                 n = parsecmd(inter);
202                 /* showtree(n); DEBUG */
203                 if (n == NEOF) {
204                         if (!top || numeof >= 50)
205                                 break;
206                         if (!stoppedjobs()) {
207                                 if (!Iflag)
208                                         break;
209                                 out2fmt_flush("\nUse \"exit\" to leave shell.\n");
210                         }
211                         numeof++;
212                 } else if (n != NULL && nflag == 0) {
213                         job_warning = (job_warning == 2) ? 1 : 0;
214                         numeof = 0;
215                         evaltree(n, 0);
216                 }
217                 popstackmark(&smark);
218                 setstackmark(&smark);
219                 if (evalskip != 0) {
220                         if (evalskip == SKIPFILE)
221                                 evalskip = 0;
222                         break;
223                 }
224         }
225         popstackmark(&smark);
226 }
227
228
229
230 /*
231  * Read /etc/profile or .profile.  Return on error.
232  */
233
234 static void
235 read_profile(const char *name)
236 {
237         int fd;
238
239         INTOFF;
240         if ((fd = open(name, O_RDONLY)) >= 0)
241                 setinputfd(fd, 1);
242         INTON;
243         if (fd < 0)
244                 return;
245         cmdloop(0);
246         popfile();
247 }
248
249
250
251 /*
252  * Read a file containing shell functions.
253  */
254
255 void
256 readcmdfile(const char *name)
257 {
258         int fd;
259
260         INTOFF;
261         if ((fd = open(name, O_RDONLY)) >= 0)
262                 setinputfd(fd, 1);
263         else
264                 error("Can't open %s: %s", name, strerror(errno));
265         INTON;
266         cmdloop(0);
267         popfile();
268 }
269
270
271
272 /*
273  * Take commands from a file.  To be compatible we should do a path
274  * search for the file, which is necessary to find sub-commands.
275  */
276
277
278 static char *
279 find_dot_file(char *basename)
280 {
281         static char localname[FILENAME_MAX+1];
282         char *fullname;
283         const char *path = pathval();
284         struct stat statb;
285
286         /* don't try this for absolute or relative paths */
287         if( strchr(basename, '/'))
288                 return basename;
289
290         while ((fullname = padvance(&path, basename)) != NULL) {
291                 strcpy(localname, fullname);
292                 stunalloc(fullname);
293                 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode))
294                         return localname;
295         }
296         return basename;
297 }
298
299 int
300 dotcmd(int argc, char **argv)
301 {
302         char *filename, *fullname;
303
304         if (argc < 2)
305                 error("missing filename");
306
307         exitstatus = 0;
308
309         /*
310          * Because we have historically not supported any options,
311          * only treat "--" specially.
312          */
313         filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1];
314
315         fullname = find_dot_file(filename);
316         setinputfile(fullname, 1);
317         commandname = fullname;
318         cmdloop(0);
319         popfile();
320         return exitstatus;
321 }
322
323
324 int
325 exitcmd(int argc, char **argv)
326 {
327         if (stoppedjobs())
328                 return 0;
329         if (argc > 1)
330                 exitshell(number(argv[1]));
331         else
332                 exitshell_savedstatus();
333 }