1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 #ifndef lint
36 #if 0
37 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/28/95";
38 #endif
39 #endif /* not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: head/bin/sh/main.c 364919 2020-08-28 15:35:45Z jilles $");
42
43 #include <stdio.h>
44 #include <signal.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <locale.h>
49 #include <errno.h>
50
51 #include "shell.h"
52 #include "main.h"
53 #include "mail.h"
54 #include "options.h"
55 #include "output.h"
56 #include "parser.h"
57 #include "nodes.h"
58 #include "expand.h"
59 #include "eval.h"
60 #include "jobs.h"
61 #include "input.h"
62 #include "trap.h"
63 #include "var.h"
64 #include "show.h"
65 #include "memalloc.h"
66 #include "error.h"
67 #include "mystring.h"
68 #include "exec.h"
69 #include "cd.h"
70 #include "redir.h"
71 #include "builtins.h"
72
73 int rootpid;
74 int rootshell;
75 struct jmploc main_handler;
76 int localeisutf8, initial_localeisutf8;
77
78 static void reset(void);
79 static void cmdloop(int);
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
main(int argc,char * argv[])92 main(int argc, char *argv[])
93 {
94 struct stackmark smark, smark2;
95 volatile int state;
96 char *shinit;
97
98 (void) setlocale(LC_ALL, "");
99 initcharset();
100 state = 0;
101 if (setjmp(main_handler.loc)) {
102 if (state == 0 || iflag == 0 || ! rootshell ||
103 exception == EXEXIT)
104 exitshell(exitstatus);
105 reset();
106 if (exception == EXINT)
107 out2fmt_flush("\n");
108 popstackmark(&smark);
109 FORCEINTON; /* enable interrupts */
110 if (state == 1)
111 goto state1;
112 else if (state == 2)
113 goto state2;
114 else if (state == 3)
115 goto state3;
116 else
117 goto state4;
118 }
119 handler = &main_handler;
120 #ifdef DEBUG
121 opentrace();
122 trputs("Shell args: "); trargs(argv);
123 #endif
124 rootpid = getpid();
125 rootshell = 1;
126 INTOFF;
127 initvar();
128 setstackmark(&smark);
129 setstackmark(&smark2);
130 procargs(argc, argv);
131 trap_init();
132 pwd_init(iflag);
133 INTON;
134 if (iflag)
135 chkmail(1);
136 if (argv[0] && argv[0][0] == '-') {
137 state = 1;
138 read_profile("/etc/profile");
139 state1:
140 state = 2;
141 if (privileged == 0)
142 read_profile("${HOME-}/.profile");
143 else
144 read_profile("/etc/suid_profile");
145 }
146 state2:
147 state = 3;
148 if (!privileged && iflag) {
149 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
150 state = 3;
151 read_profile(shinit);
152 }
153 }
154 state3:
155 state = 4;
156 popstackmark(&smark2);
157 if (minusc) {
158 evalstring(minusc, sflag ? 0 : EV_EXIT);
159 }
160 state4:
161 if (sflag || minusc == NULL) {
162 cmdloop(1);
163 }
164 exitshell(exitstatus);
165 /*NOTREACHED*/
166 return 0;
167 }
168
169 static void
reset(void)170 reset(void)
171 {
172 reseteval();
173 resetinput();
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 static void
cmdloop(int top)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 (pendingsig)
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 == SKIPRETURN)
221 evalskip = 0;
222 break;
223 }
224 }
225 popstackmark(&smark);
226 if (top && iflag) {
227 out2c('\n');
228 flushout(out2);
229 }
230 }
231
232
233
234 /*
235 * Read /etc/profile or .profile. Return on error.
236 */
237
238 static void
read_profile(const char * name)239 read_profile(const char *name)
240 {
241 int fd;
242 const char *expandedname;
243
244 expandedname = expandstr(name);
245 if (expandedname == NULL)
246 return;
247 INTOFF;
248 if ((fd = open(expandedname, O_RDONLY | O_CLOEXEC_MAYBE)) >= 0)
249 setinputfd(fd, 1);
250 INTON;
251 if (fd < 0)
252 return;
253 cmdloop(0);
254 popfile();
255 }
256
257
258
259 /*
260 * Read a file containing shell functions.
261 */
262
263 void
readcmdfile(const char * name)264 readcmdfile(const char *name)
265 {
266 setinputfile(name, 1);
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 *
find_dot_file(char * basename)280 find_dot_file(char *basename)
281 {
282 char *fullname;
283 const char *opt;
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, &opt, basename)) != NULL) {
292 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
293 /*
294 * Don't bother freeing here, since it will
295 * be freed by the caller.
296 */
297 return fullname;
298 }
299 stunalloc(fullname);
300 }
301 return basename;
302 }
303
304 int
dotcmd(int argc,char ** argv)305 dotcmd(int argc, char **argv)
306 {
307 char *filename, *fullname;
308
309 if (argc < 2)
310 error("missing filename");
311
312 exitstatus = 0;
313
314 /*
315 * Because we have historically not supported any options,
316 * only treat "--" specially.
317 */
318 filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1];
319
320 fullname = find_dot_file(filename);
321 setinputfile(fullname, 1);
322 commandname = fullname;
323 cmdloop(0);
324 popfile();
325 return exitstatus;
326 }
327
328
329 int
exitcmd(int argc,char ** argv)330 exitcmd(int argc, char **argv)
331 {
332 if (stoppedjobs())
333 return 0;
334 if (argc > 1)
335 exitshell(number(argv[1]));
336 else
337 exitshell_savedstatus();
338 }
339