]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/sh/main.c
ssh: Update to OpenSSH 9.3p2
[FreeBSD/FreeBSD.git] / bin / sh / main.c
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 static char const copyright[] =
37 "@(#) Copyright (c) 1991, 1993\n\
38         The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)main.c      8.6 (Berkeley) 5/28/95";
44 #endif
45 #endif /* not lint */
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48
49 #include <stdio.h>
50 #include <signal.h>
51 #include <sys/stat.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <locale.h>
55 #include <errno.h>
56
57 #include "shell.h"
58 #include "main.h"
59 #include "mail.h"
60 #include "options.h"
61 #include "output.h"
62 #include "parser.h"
63 #include "nodes.h"
64 #include "expand.h"
65 #include "eval.h"
66 #include "jobs.h"
67 #include "input.h"
68 #include "trap.h"
69 #include "var.h"
70 #include "show.h"
71 #include "memalloc.h"
72 #include "error.h"
73 #include "mystring.h"
74 #include "exec.h"
75 #include "cd.h"
76 #include "redir.h"
77 #include "builtins.h"
78 #ifndef NO_HISTORY
79 #include "myhistedit.h"
80 #endif
81
82 int rootpid;
83 int rootshell;
84 struct jmploc main_handler;
85 int localeisutf8, initial_localeisutf8;
86
87 static void reset(void);
88 static void cmdloop(int);
89 static void read_profile(const char *);
90 static char *find_dot_file(char *);
91
92 /*
93  * Main routine.  We initialize things, parse the arguments, execute
94  * profiles if we're a login shell, and then call cmdloop to execute
95  * commands.  The setjmp call sets up the location to jump to when an
96  * exception occurs.  When an exception occurs the variable "state"
97  * is used to figure out how far we had gotten.
98  */
99
100 int
101 main(int argc, char *argv[])
102 {
103         /*
104          * As smark is accessed after a longjmp, it cannot be a local in main().
105          * The C standard specifies that the values of non-volatile local
106          * variables are unspecified after a jump if modified between the
107          * setjmp and longjmp.
108          */
109         static struct stackmark smark, smark2;
110         volatile int state;
111         char *shinit;
112
113         (void) setlocale(LC_ALL, "");
114         initcharset();
115         state = 0;
116         if (setjmp(main_handler.loc)) {
117                 if (state == 0 || iflag == 0 || ! rootshell ||
118                     exception == EXEXIT)
119                         exitshell(exitstatus);
120                 reset();
121                 if (exception == EXINT)
122                         out2fmt_flush("\n");
123                 popstackmark(&smark);
124                 FORCEINTON;                             /* enable interrupts */
125                 if (state == 1)
126                         goto state1;
127                 else if (state == 2)
128                         goto state2;
129                 else if (state == 3)
130                         goto state3;
131                 else
132                         goto state4;
133         }
134         handler = &main_handler;
135 #ifdef DEBUG
136         opentrace();
137         trputs("Shell args:  ");  trargs(argv);
138 #endif
139         rootpid = getpid();
140         rootshell = 1;
141         INTOFF;
142         initvar();
143         setstackmark(&smark);
144         setstackmark(&smark2);
145         procargs(argc, argv);
146         trap_init();
147         pwd_init(iflag);
148         INTON;
149         if (iflag)
150                 chkmail(1);
151         if (argv[0] && argv[0][0] == '-') {
152                 state = 1;
153                 read_profile("/etc/profile");
154 state1:
155                 state = 2;
156                 if (privileged == 0)
157                         read_profile("${HOME-}/.profile");
158                 else
159                         read_profile("/etc/suid_profile");
160         }
161 state2:
162         state = 3;
163         if (!privileged && iflag) {
164                 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
165                         state = 3;
166                         read_profile(shinit);
167                 }
168         }
169 #ifndef NO_HISTORY
170         if (iflag)
171                 histload();
172 #endif
173 state3:
174         state = 4;
175         popstackmark(&smark2);
176         if (minusc) {
177                 evalstring(minusc, sflag ? 0 : EV_EXIT);
178         }
179 state4:
180         if (sflag || minusc == NULL) {
181                 cmdloop(1);
182         }
183         exitshell(exitstatus);
184         /*NOTREACHED*/
185         return 0;
186 }
187
188 static void
189 reset(void)
190 {
191         reseteval();
192         resetinput();
193 }
194
195 /*
196  * Read and execute commands.  "Top" is nonzero for the top level command
197  * loop; it turns on prompting if the shell is interactive.
198  */
199
200 static void
201 cmdloop(int top)
202 {
203         union node *n;
204         struct stackmark smark;
205         int inter;
206         int numeof = 0;
207
208         TRACE(("cmdloop(%d) called\n", top));
209         setstackmark(&smark);
210         for (;;) {
211                 if (pendingsig)
212                         dotrap();
213                 inter = 0;
214                 if (iflag && top) {
215                         inter++;
216                         showjobs(1, SHOWJOBS_DEFAULT);
217                         chkmail(0);
218                         flushout(&output);
219                 }
220                 n = parsecmd(inter);
221                 /* showtree(n); DEBUG */
222                 if (n == NEOF) {
223                         if (!top || numeof >= 50)
224                                 break;
225                         if (!stoppedjobs()) {
226                                 if (!Iflag)
227                                         break;
228                                 out2fmt_flush("\nUse \"exit\" to leave shell.\n");
229                         }
230                         numeof++;
231                 } else if (n != NULL && nflag == 0) {
232                         job_warning = (job_warning == 2) ? 1 : 0;
233                         numeof = 0;
234                         evaltree(n, 0);
235                 }
236                 popstackmark(&smark);
237                 setstackmark(&smark);
238                 if (evalskip != 0) {
239                         if (evalskip == SKIPRETURN)
240                                 evalskip = 0;
241                         break;
242                 }
243         }
244         popstackmark(&smark);
245         if (top && iflag) {
246                 out2c('\n');
247                 flushout(out2);
248         }
249 }
250
251
252
253 /*
254  * Read /etc/profile or .profile.  Return on error.
255  */
256
257 static void
258 read_profile(const char *name)
259 {
260         int fd;
261         const char *expandedname;
262         int oflags = O_RDONLY | O_CLOEXEC;
263
264         if (verifyflag)
265                 oflags |= O_VERIFY;
266
267         expandedname = expandstr(name);
268         if (expandedname == NULL)
269                 return;
270         INTOFF;
271         if ((fd = open(expandedname, oflags)) >= 0)
272                 setinputfd(fd, 1);
273         INTON;
274         if (fd < 0)
275                 return;
276         cmdloop(0);
277         popfile();
278 }
279
280
281
282 /*
283  * Read a file containing shell functions.
284  */
285
286 void
287 readcmdfile(const char *name, int verify)
288 {
289         setinputfile(name, 1, verify);
290         cmdloop(0);
291         popfile();
292 }
293
294
295
296 /*
297  * Take commands from a file.  To be compatible we should do a path
298  * search for the file, which is necessary to find sub-commands.
299  */
300
301
302 static char *
303 find_dot_file(char *basename)
304 {
305         char *fullname;
306         const char *opt;
307         const char *path = pathval();
308         struct stat statb;
309
310         /* don't try this for absolute or relative paths */
311         if( strchr(basename, '/'))
312                 return basename;
313
314         while ((fullname = padvance(&path, &opt, basename)) != NULL) {
315                 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
316                         /*
317                          * Don't bother freeing here, since it will
318                          * be freed by the caller.
319                          */
320                         return fullname;
321                 }
322                 stunalloc(fullname);
323         }
324         return basename;
325 }
326
327 int
328 dotcmd(int argc, char **argv)
329 {
330         char *filename, *fullname;
331
332         if (argc < 2)
333                 error("missing filename");
334
335         exitstatus = 0;
336
337         /*
338          * Because we have historically not supported any options,
339          * only treat "--" specially.
340          */
341         filename = argc > 2 && strcmp(argv[1], "--") == 0 ? argv[2] : argv[1];
342
343         fullname = find_dot_file(filename);
344         setinputfile(fullname, 1, -1 /* verify */);
345         commandname = fullname;
346         cmdloop(0);
347         popfile();
348         return exitstatus;
349 }
350
351
352 int
353 exitcmd(int argc, char **argv)
354 {
355         if (stoppedjobs())
356                 return 0;
357         if (argc > 1)
358                 exitshell(number(argv[1]));
359         else
360                 exitshell_savedstatus();
361 }