1 | /* $NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv 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 | #ifdef HAVE_SYS_CDEFS_H
|
---|
36 | #include <sys/cdefs.h>
|
---|
37 | #endif
|
---|
38 | #ifndef lint
|
---|
39 | __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
|
---|
40 | The Regents of the University of California. All rights reserved.\n");
|
---|
41 | #endif /* not lint */
|
---|
42 |
|
---|
43 | #ifndef lint
|
---|
44 | #if 0
|
---|
45 | static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95";
|
---|
46 | #else
|
---|
47 | __RCSID("$NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv Exp $");
|
---|
48 | #endif
|
---|
49 | #endif /* not lint */
|
---|
50 |
|
---|
51 | #include <errno.h>
|
---|
52 | #include <stdio.h>
|
---|
53 | #include <signal.h>
|
---|
54 | #include <sys/stat.h>
|
---|
55 | #include <unistd.h>
|
---|
56 | #include <locale.h>
|
---|
57 | #include <fcntl.h>
|
---|
58 |
|
---|
59 |
|
---|
60 | #include "shell.h"
|
---|
61 | #include "main.h"
|
---|
62 | #include "mail.h"
|
---|
63 | #include "options.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 | #include "shinstance.h"
|
---|
81 |
|
---|
82 | #define PROFILE 0
|
---|
83 |
|
---|
84 | /*int rootpid;
|
---|
85 | int rootshell;*/
|
---|
86 | STATIC union node *curcmd;
|
---|
87 | STATIC union node *prevcmd;
|
---|
88 |
|
---|
89 | STATIC void read_profile(struct shinstance *, const char *);
|
---|
90 | STATIC char *find_dot_file(struct shinstance *, char *);
|
---|
91 | int main(int, char **);
|
---|
92 | int shell_main(shinstance *, int, char **);
|
---|
93 | #ifdef _MSC_VER
|
---|
94 | extern void init_syntax(void);
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Main routine. We initialize things, parse the arguments, execute
|
---|
99 | * profiles if we're a login shell, and then call cmdloop to execute
|
---|
100 | * commands. The setjmp call sets up the location to jump to when an
|
---|
101 | * exception occurs. When an exception occurs the variable "state"
|
---|
102 | * is used to figure out how far we had gotten.
|
---|
103 | */
|
---|
104 |
|
---|
105 | int
|
---|
106 | main(int argc, char **argv)
|
---|
107 | {
|
---|
108 | shinstance *psh;
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Global initializations.
|
---|
112 | */
|
---|
113 | setlocale(LC_ALL, "");
|
---|
114 | #ifdef _MSC_VER
|
---|
115 | init_syntax();
|
---|
116 | #endif
|
---|
117 | /*
|
---|
118 | * Create the root shell instance.
|
---|
119 | */
|
---|
120 | psh = create_root_shell(NULL, argc, argv);
|
---|
121 | if (!psh)
|
---|
122 | return 2;
|
---|
123 | shthread_set_shell(psh);
|
---|
124 | return shell_main(psh, argc, argv);
|
---|
125 | }
|
---|
126 |
|
---|
127 | int
|
---|
128 | shell_main(shinstance *psh, int argc, char **argv)
|
---|
129 | {
|
---|
130 | struct jmploc jmploc;
|
---|
131 | struct stackmark smark;
|
---|
132 | volatile int state;
|
---|
133 | char *shinit;
|
---|
134 |
|
---|
135 | state = 0;
|
---|
136 | if (setjmp(jmploc.loc)) {
|
---|
137 | /*
|
---|
138 | * When a shell procedure is executed, we raise the
|
---|
139 | * exception EXSHELLPROC to clean up before executing
|
---|
140 | * the shell procedure.
|
---|
141 | */
|
---|
142 | switch (psh->exception) {
|
---|
143 | case EXSHELLPROC:
|
---|
144 | psh->rootpid = /*getpid()*/ psh->pid;
|
---|
145 | psh->rootshell = 1;
|
---|
146 | psh->minusc = NULL;
|
---|
147 | state = 3;
|
---|
148 | break;
|
---|
149 |
|
---|
150 | case EXEXEC:
|
---|
151 | psh->exitstatus = psh->exerrno;
|
---|
152 | break;
|
---|
153 |
|
---|
154 | case EXERROR:
|
---|
155 | psh->exitstatus = 2;
|
---|
156 | break;
|
---|
157 |
|
---|
158 | default:
|
---|
159 | break;
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (psh->exception != EXSHELLPROC) {
|
---|
163 | if (state == 0 || psh->iflag == 0 || ! psh->rootshell)
|
---|
164 | exitshell(psh, psh->exitstatus);
|
---|
165 | }
|
---|
166 | reset(psh);
|
---|
167 | if (psh->exception == EXINT
|
---|
168 | #if ATTY
|
---|
169 | && (! attyset(psh) || equal(termval(psh), "emacs"))
|
---|
170 | #endif
|
---|
171 | ) {
|
---|
172 | out2c(psh, '\n');
|
---|
173 | flushout(&psh->errout);
|
---|
174 | }
|
---|
175 | popstackmark(psh, &smark);
|
---|
176 | FORCEINTON; /* enable interrupts */
|
---|
177 | if (state == 1)
|
---|
178 | goto state1;
|
---|
179 | else if (state == 2)
|
---|
180 | goto state2;
|
---|
181 | else if (state == 3)
|
---|
182 | goto state3;
|
---|
183 | else
|
---|
184 | goto state4;
|
---|
185 | }
|
---|
186 | psh->handler = &jmploc;
|
---|
187 | psh->rootpid = /*getpid()*/ psh->pid;
|
---|
188 | psh->rootshell = 1;
|
---|
189 | #ifdef DEBUG
|
---|
190 | #if DEBUG == 2
|
---|
191 | debug = 1;
|
---|
192 | #endif
|
---|
193 | opentrace(psh);
|
---|
194 | trputs("Shell args: "); trargs(argv);
|
---|
195 | #endif
|
---|
196 |
|
---|
197 | init(psh);
|
---|
198 | setstackmark(psh, &smark);
|
---|
199 | procargs(psh, argc, argv);
|
---|
200 | if (argv[0] && argv[0][0] == '-') {
|
---|
201 | state = 1;
|
---|
202 | read_profile(psh, "/etc/profile");
|
---|
203 | state1:
|
---|
204 | state = 2;
|
---|
205 | read_profile(psh, ".profile");
|
---|
206 | }
|
---|
207 | state2:
|
---|
208 | state = 3;
|
---|
209 | if (getuid() == geteuid() && getgid() == getegid()) {
|
---|
210 | if ((shinit = lookupvar(psh, "ENV")) != NULL && *shinit != '\0') {
|
---|
211 | state = 3;
|
---|
212 | read_profile(psh, shinit);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | state3:
|
---|
216 | state = 4;
|
---|
217 | if (psh->sflag == 0 || psh->minusc) {
|
---|
218 | static int sigs[] = {
|
---|
219 | SIGINT, SIGQUIT, SIGHUP,
|
---|
220 | #ifdef SIGTSTP
|
---|
221 | SIGTSTP,
|
---|
222 | #endif
|
---|
223 | SIGPIPE
|
---|
224 | };
|
---|
225 | #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
|
---|
226 | int i;
|
---|
227 |
|
---|
228 | for (i = 0; i < SIGSSIZE; i++)
|
---|
229 | setsignal(psh, sigs[i], 0);
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (psh->minusc)
|
---|
233 | evalstring(psh, psh->minusc, 0);
|
---|
234 |
|
---|
235 | if (psh->sflag || psh->minusc == NULL) {
|
---|
236 | state4: /* XXX ??? - why isn't this before the "if" statement */
|
---|
237 | cmdloop(psh, 1);
|
---|
238 | }
|
---|
239 | exitshell(psh, psh->exitstatus);
|
---|
240 | /* NOTREACHED */
|
---|
241 | return 1;
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * Read and execute commands. "Top" is nonzero for the top level command
|
---|
247 | * loop; it turns on prompting if the shell is interactive.
|
---|
248 | */
|
---|
249 |
|
---|
250 | void
|
---|
251 | cmdloop(struct shinstance *psh, int top)
|
---|
252 | {
|
---|
253 | union node *n;
|
---|
254 | struct stackmark smark;
|
---|
255 | int inter;
|
---|
256 | int numeof = 0;
|
---|
257 |
|
---|
258 | TRACE(("cmdloop(%d) called\n", top));
|
---|
259 | setstackmark(psh, &smark);
|
---|
260 | for (;;) {
|
---|
261 | if (psh->pendingsigs)
|
---|
262 | dotrap(psh);
|
---|
263 | inter = 0;
|
---|
264 | if (psh->iflag && top) {
|
---|
265 | inter = 1;
|
---|
266 | showjobs(psh, psh->out2, SHOW_CHANGED);
|
---|
267 | chkmail(psh, 0);
|
---|
268 | flushout(&psh->errout);
|
---|
269 | }
|
---|
270 | n = parsecmd(psh, inter);
|
---|
271 | /* showtree(n); DEBUG */
|
---|
272 | if (n == NEOF) {
|
---|
273 | if (!top || numeof >= 50)
|
---|
274 | break;
|
---|
275 | if (!stoppedjobs(psh)) {
|
---|
276 | if (!psh->Iflag)
|
---|
277 | break;
|
---|
278 | out2str(psh, "\nUse \"exit\" to leave shell.\n");
|
---|
279 | }
|
---|
280 | numeof++;
|
---|
281 | } else if (n != NULL && psh->nflag == 0) {
|
---|
282 | psh->job_warning = (psh->job_warning == 2) ? 1 : 0;
|
---|
283 | numeof = 0;
|
---|
284 | evaltree(psh, n, 0);
|
---|
285 | }
|
---|
286 | popstackmark(psh, &smark);
|
---|
287 | setstackmark(psh, &smark);
|
---|
288 | if (psh->evalskip == SKIPFILE) {
|
---|
289 | psh->evalskip = 0;
|
---|
290 | break;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | popstackmark(psh, &smark);
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 |
|
---|
298 | /*
|
---|
299 | * Read /etc/profile or .profile. Return on error.
|
---|
300 | */
|
---|
301 |
|
---|
302 | STATIC void
|
---|
303 | read_profile(struct shinstance *psh, const char *name)
|
---|
304 | {
|
---|
305 | int fd;
|
---|
306 | int xflag_set = 0;
|
---|
307 | int vflag_set = 0;
|
---|
308 |
|
---|
309 | INTOFF;
|
---|
310 | if ((fd = shfile_open(psh, name, O_RDONLY)) >= 0)
|
---|
311 | setinputfd(psh, fd, 1);
|
---|
312 | INTON;
|
---|
313 | if (fd < 0)
|
---|
314 | return;
|
---|
315 | /* -q turns off -x and -v just when executing init files */
|
---|
316 | if (psh->qflag) {
|
---|
317 | if (psh->xflag)
|
---|
318 | psh->xflag = 0, xflag_set = 1;
|
---|
319 | if (psh->vflag)
|
---|
320 | psh->vflag = 0, vflag_set = 1;
|
---|
321 | }
|
---|
322 | cmdloop(psh, 0);
|
---|
323 | if (psh->qflag) {
|
---|
324 | if (xflag_set)
|
---|
325 | psh->xflag = 1;
|
---|
326 | if (vflag_set)
|
---|
327 | psh->vflag = 1;
|
---|
328 | }
|
---|
329 | popfile(psh);
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 |
|
---|
334 | /*
|
---|
335 | * Read a file containing shell functions.
|
---|
336 | */
|
---|
337 |
|
---|
338 | void
|
---|
339 | readcmdfile(struct shinstance *psh, char *name)
|
---|
340 | {
|
---|
341 | int fd;
|
---|
342 |
|
---|
343 | INTOFF;
|
---|
344 | if ((fd = open(name, O_RDONLY)) >= 0)
|
---|
345 | setinputfd(psh, fd, 1);
|
---|
346 | else
|
---|
347 | error(psh, "Can't open %s", name);
|
---|
348 | INTON;
|
---|
349 | cmdloop(psh, 0);
|
---|
350 | popfile(psh);
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 |
|
---|
355 | /*
|
---|
356 | * Take commands from a file. To be compatible we should do a path
|
---|
357 | * search for the file, which is necessary to find sub-commands.
|
---|
358 | */
|
---|
359 |
|
---|
360 |
|
---|
361 | STATIC char *
|
---|
362 | find_dot_file(struct shinstance *psh, char *basename)
|
---|
363 | {
|
---|
364 | char *fullname;
|
---|
365 | const char *path = pathval(psh);
|
---|
366 | struct stat statb;
|
---|
367 |
|
---|
368 | /* don't try this for absolute or relative paths */
|
---|
369 | if (strchr(basename, '/'))
|
---|
370 | return basename;
|
---|
371 |
|
---|
372 | while ((fullname = padvance(psh, &path, basename)) != NULL) {
|
---|
373 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
|
---|
374 | /*
|
---|
375 | * Don't bother freeing here, since it will
|
---|
376 | * be freed by the caller.
|
---|
377 | */
|
---|
378 | return fullname;
|
---|
379 | }
|
---|
380 | stunalloc(psh, fullname);
|
---|
381 | }
|
---|
382 |
|
---|
383 | /* not found in the PATH */
|
---|
384 | error(psh, "%s: not found", basename);
|
---|
385 | /* NOTREACHED */
|
---|
386 | return NULL;
|
---|
387 | }
|
---|
388 |
|
---|
389 | int
|
---|
390 | dotcmd(struct shinstance *psh, int argc, char **argv)
|
---|
391 | {
|
---|
392 | psh->exitstatus = 0;
|
---|
393 |
|
---|
394 | if (argc >= 2) { /* That's what SVR2 does */
|
---|
395 | char *fullname;
|
---|
396 | struct stackmark smark;
|
---|
397 |
|
---|
398 | setstackmark(psh, &smark);
|
---|
399 | fullname = find_dot_file(psh, argv[1]);
|
---|
400 | setinputfile(psh, fullname, 1);
|
---|
401 | psh->commandname = fullname;
|
---|
402 | cmdloop(psh, 0);
|
---|
403 | popfile(psh);
|
---|
404 | popstackmark(psh, &smark);
|
---|
405 | }
|
---|
406 | return psh->exitstatus;
|
---|
407 | }
|
---|
408 |
|
---|
409 |
|
---|
410 | int
|
---|
411 | exitcmd(struct shinstance *psh, int argc, char **argv)
|
---|
412 | {
|
---|
413 | if (stoppedjobs(psh))
|
---|
414 | return 0;
|
---|
415 | if (argc > 1)
|
---|
416 | psh->exitstatus = number(argv[1]);
|
---|
417 | exitshell(psh, psh->exitstatus);
|
---|
418 | /* NOTREACHED */
|
---|
419 | return 1;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /*
|
---|
423 | * Local Variables:
|
---|
424 | * c-file-style: bsd
|
---|
425 | * End:
|
---|
426 | */
|
---|