main.c revision 1.57.8.1 1 /* $NetBSD: main.c,v 1.57.8.1 2014/08/19 23:45:11 tls Exp $ */
2
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 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95";
44 #else
45 __RCSID("$NetBSD: main.c,v 1.57.8.1 2014/08/19 23:45:11 tls Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <errno.h>
50 #include <stdio.h>
51 #include <signal.h>
52 #include <sys/stat.h>
53 #include <unistd.h>
54 #include <stdlib.h>
55 #include <locale.h>
56 #include <fcntl.h>
57
58
59 #include "shell.h"
60 #include "main.h"
61 #include "mail.h"
62 #include "options.h"
63 #include "builtins.h"
64 #include "output.h"
65 #include "parser.h"
66 #include "nodes.h"
67 #include "expand.h"
68 #include "eval.h"
69 #include "jobs.h"
70 #include "input.h"
71 #include "trap.h"
72 #include "var.h"
73 #include "show.h"
74 #include "memalloc.h"
75 #include "error.h"
76 #include "init.h"
77 #include "mystring.h"
78 #include "exec.h"
79 #include "cd.h"
80
81 #define PROFILE 0
82
83 int rootpid;
84 int rootshell;
85 int posix;
86 #if PROFILE
87 short profile_buf[16384];
88 extern int etext();
89 #endif
90
91 STATIC void read_profile(const char *);
92 int main(int, char **);
93
94 /*
95 * Main routine. We initialize things, parse the arguments, execute
96 * profiles if we're a login shell, and then call cmdloop to execute
97 * commands. The setjmp call sets up the location to jump to when an
98 * exception occurs. When an exception occurs the variable "state"
99 * is used to figure out how far we had gotten.
100 */
101
102 int
103 main(int argc, char **argv)
104 {
105 struct jmploc jmploc;
106 struct stackmark smark;
107 volatile int state;
108 char *shinit;
109
110 setlocale(LC_ALL, "");
111
112 posix = getenv("POSIXLY_CORRECT") != NULL;
113 #if PROFILE
114 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
115 #endif
116 state = 0;
117 if (setjmp(jmploc.loc)) {
118 /*
119 * When a shell procedure is executed, we raise the
120 * exception EXSHELLPROC to clean up before executing
121 * the shell procedure.
122 */
123 switch (exception) {
124 case EXSHELLPROC:
125 rootpid = getpid();
126 rootshell = 1;
127 minusc = NULL;
128 state = 3;
129 break;
130
131 case EXEXEC:
132 exitstatus = exerrno;
133 break;
134
135 case EXERROR:
136 exitstatus = 2;
137 break;
138
139 default:
140 break;
141 }
142
143 if (exception != EXSHELLPROC) {
144 if (state == 0 || iflag == 0 || ! rootshell)
145 exitshell(exitstatus);
146 }
147 reset();
148 if (exception == EXINT
149 #if ATTY
150 && (! attyset() || equal(termval(), "emacs"))
151 #endif
152 ) {
153 out2c('\n');
154 flushout(&errout);
155 }
156 popstackmark(&smark);
157 FORCEINTON; /* enable interrupts */
158 if (state == 1)
159 goto state1;
160 else if (state == 2)
161 goto state2;
162 else if (state == 3)
163 goto state3;
164 else
165 goto state4;
166 }
167 handler = &jmploc;
168 #ifdef DEBUG
169 #if DEBUG == 2
170 debug = 1;
171 #endif
172 opentrace();
173 trputs("Shell args: "); trargs(argv);
174 #endif
175 rootpid = getpid();
176 rootshell = 1;
177 init();
178 initpwd();
179 setstackmark(&smark);
180 procargs(argc, argv);
181 if (argv[0] && argv[0][0] == '-') {
182 state = 1;
183 read_profile("/etc/profile");
184 state1:
185 state = 2;
186 read_profile(".profile");
187 }
188 state2:
189 state = 3;
190 if ((iflag || !posix) &&
191 getuid() == geteuid() && getgid() == getegid()) {
192 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
193 state = 3;
194 read_profile(shinit);
195 }
196 }
197 state3:
198 state = 4;
199 if (sflag == 0 || minusc) {
200 static int sigs[] = {
201 SIGINT, SIGQUIT, SIGHUP,
202 #ifdef SIGTSTP
203 SIGTSTP,
204 #endif
205 SIGPIPE
206 };
207 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
208 size_t i;
209
210 for (i = 0; i < SIGSSIZE; i++)
211 setsignal(sigs[i], 0);
212 }
213
214 if (minusc)
215 evalstring(minusc, 0);
216
217 if (sflag || minusc == NULL) {
218 state4: /* XXX ??? - why isn't this before the "if" statement */
219 cmdloop(1);
220 }
221 #if PROFILE
222 monitor(0);
223 #endif
224 exitshell(exitstatus);
225 /* NOTREACHED */
226 }
227
228
229 /*
230 * Read and execute commands. "Top" is nonzero for the top level command
231 * loop; it turns on prompting if the shell is interactive.
232 */
233
234 void
235 cmdloop(int top)
236 {
237 union node *n;
238 struct stackmark smark;
239 int inter;
240 int numeof = 0;
241 enum skipstate skip;
242
243 TRACE(("cmdloop(%d) called\n", top));
244 setstackmark(&smark);
245 for (;;) {
246 if (pendingsigs)
247 dotrap();
248 inter = 0;
249 if (iflag == 1 && top) {
250 inter = 1;
251 showjobs(out2, SHOW_CHANGED);
252 chkmail(0);
253 flushout(&errout);
254 }
255 n = parsecmd(inter);
256 /* showtree(n); DEBUG */
257 if (n == NEOF) {
258 if (!top || numeof >= 50)
259 break;
260 if (!stoppedjobs()) {
261 if (!Iflag)
262 break;
263 out2str("\nUse \"exit\" to leave shell.\n");
264 }
265 numeof++;
266 } else if (n != NULL && nflag == 0) {
267 job_warning = (job_warning == 2) ? 1 : 0;
268 numeof = 0;
269 evaltree(n, 0);
270 }
271 popstackmark(&smark);
272 setstackmark(&smark);
273
274 /*
275 * Any SKIP* can occur here! SKIP(FUNC|BREAK|CONT) occur when
276 * a dotcmd is in a loop or a function body and appropriate
277 * built-ins occurs in file scope in the sourced file. Values
278 * other than SKIPFILE are reset by the appropriate eval*()
279 * that contained the dotcmd() call.
280 */
281 skip = current_skipstate();
282 if (skip != SKIPNONE) {
283 if (skip == SKIPFILE)
284 stop_skipping();
285 break;
286 }
287 }
288 popstackmark(&smark);
289 }
290
291
292
293 /*
294 * Read /etc/profile or .profile. Return on error.
295 */
296
297 STATIC void
298 read_profile(const char *name)
299 {
300 int fd;
301 int xflag_set = 0;
302 int vflag_set = 0;
303
304 INTOFF;
305 if ((fd = open(name, O_RDONLY)) >= 0)
306 setinputfd(fd, 1);
307 INTON;
308 if (fd < 0)
309 return;
310 /* -q turns off -x and -v just when executing init files */
311 if (qflag) {
312 if (xflag)
313 xflag = 0, xflag_set = 1;
314 if (vflag)
315 vflag = 0, vflag_set = 1;
316 }
317 cmdloop(0);
318 if (qflag) {
319 if (xflag_set)
320 xflag = 1;
321 if (vflag_set)
322 vflag = 1;
323 }
324 popfile();
325 }
326
327
328
329 /*
330 * Read a file containing shell functions.
331 */
332
333 void
334 readcmdfile(char *name)
335 {
336 int fd;
337
338 INTOFF;
339 if ((fd = open(name, O_RDONLY)) >= 0)
340 setinputfd(fd, 1);
341 else
342 error("Can't open %s", name);
343 INTON;
344 cmdloop(0);
345 popfile();
346 }
347
348
349
350 int
351 exitcmd(int argc, char **argv)
352 {
353 if (stoppedjobs())
354 return 0;
355 if (argc > 1)
356 exitstatus = number(argv[1]);
357 exitshell(exitstatus);
358 /* NOTREACHED */
359 }
360