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 | #if 0
|
---|
36 | #ifndef lint
|
---|
37 | __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
|
---|
38 | The Regents of the University of California. All rights reserved.\n");
|
---|
39 | #endif /* not lint */
|
---|
40 | #ifndef lint
|
---|
41 | static char sccsid[] = "@(#)main.c 8.7 (Berkeley) 7/19/95";
|
---|
42 | #else
|
---|
43 | __RCSID("$NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv Exp $");
|
---|
44 | #endif /* not lint */
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | #include <errno.h>
|
---|
48 | #include <stdio.h>
|
---|
49 | #include <sys/stat.h>
|
---|
50 | #include <locale.h>
|
---|
51 |
|
---|
52 |
|
---|
53 | #include "shell.h"
|
---|
54 | #include "main.h"
|
---|
55 | #include "mail.h"
|
---|
56 | #include "options.h"
|
---|
57 | #include "output.h"
|
---|
58 | #include "parser.h"
|
---|
59 | #include "nodes.h"
|
---|
60 | #include "expand.h"
|
---|
61 | #include "eval.h"
|
---|
62 | #include "jobs.h"
|
---|
63 | #include "input.h"
|
---|
64 | #include "trap.h"
|
---|
65 | #include "var.h"
|
---|
66 | #include "show.h"
|
---|
67 | #include "memalloc.h"
|
---|
68 | #include "error.h"
|
---|
69 | #include "init.h"
|
---|
70 | #include "mystring.h"
|
---|
71 | #include "exec.h"
|
---|
72 | #include "cd.h"
|
---|
73 | #include "shinstance.h"
|
---|
74 |
|
---|
75 | #define PROFILE 0
|
---|
76 |
|
---|
77 | /*int rootpid;
|
---|
78 | int rootshell;*/
|
---|
79 | #ifdef unused_variables
|
---|
80 | STATIC union node *curcmd;
|
---|
81 | STATIC union node *prevcmd;
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | STATIC void read_profile(struct shinstance *, const char *);
|
---|
85 | STATIC char *find_dot_file(struct shinstance *, char *);
|
---|
86 | int main(int, char **, char **);
|
---|
87 | int shell_main(shinstance *, int, char **);
|
---|
88 | #ifdef _MSC_VER
|
---|
89 | extern void init_syntax(void);
|
---|
90 | #endif
|
---|
91 | STATIC int usage(const char *argv0);
|
---|
92 | STATIC int version(const char *argv0);
|
---|
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 | #if K_OS == K_OS_WINDOWS
|
---|
104 | real_main(int argc, char **argv, char **envp)
|
---|
105 | #else
|
---|
106 | main(int argc, char **argv, char **envp)
|
---|
107 | #endif
|
---|
108 | {
|
---|
109 | shinstance *psh;
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * Global initializations.
|
---|
113 | */
|
---|
114 | setlocale(LC_ALL, "");
|
---|
115 | #ifdef _MSC_VER
|
---|
116 | init_syntax();
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Check for --version and --help.
|
---|
121 | */
|
---|
122 | if (argc > 1 && argv[1][0] == '-' && argv[1][1] == '-') {
|
---|
123 | if (!strcmp(argv[1], "--help"))
|
---|
124 | return usage(argv[0]);
|
---|
125 | if (!strcmp(argv[1], "--version"))
|
---|
126 | return version(argv[0]);
|
---|
127 | }
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Create the root shell instance.
|
---|
131 | */
|
---|
132 | psh = sh_create_root_shell(NULL, argc, argv, envp);
|
---|
133 | if (!psh)
|
---|
134 | return 2;
|
---|
135 | shthread_set_shell(psh);
|
---|
136 | return shell_main(psh, argc, psh->argptr);
|
---|
137 | }
|
---|
138 |
|
---|
139 | int
|
---|
140 | shell_main(shinstance *psh, int argc, char **argv)
|
---|
141 | {
|
---|
142 | struct jmploc jmploc;
|
---|
143 | struct stackmark smark;
|
---|
144 | volatile int state;
|
---|
145 | char *shinit;
|
---|
146 |
|
---|
147 | state = 0;
|
---|
148 | if (setjmp(jmploc.loc)) {
|
---|
149 | /*
|
---|
150 | * When a shell procedure is executed, we raise the
|
---|
151 | * exception EXSHELLPROC to clean up before executing
|
---|
152 | * the shell procedure.
|
---|
153 | */
|
---|
154 | switch (psh->exception) {
|
---|
155 | case EXSHELLPROC:
|
---|
156 | psh->rootpid = /*getpid()*/ psh->pid;
|
---|
157 | psh->rootshell = 1;
|
---|
158 | psh->minusc = NULL;
|
---|
159 | state = 3;
|
---|
160 | break;
|
---|
161 |
|
---|
162 | case EXEXEC:
|
---|
163 | psh->exitstatus = psh->exerrno;
|
---|
164 | break;
|
---|
165 |
|
---|
166 | case EXERROR:
|
---|
167 | psh->exitstatus = 2;
|
---|
168 | break;
|
---|
169 |
|
---|
170 | default:
|
---|
171 | break;
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (psh->exception != EXSHELLPROC) {
|
---|
175 | if (state == 0 || iflag(psh) == 0 || ! psh->rootshell)
|
---|
176 | exitshell(psh, psh->exitstatus);
|
---|
177 | }
|
---|
178 | reset(psh);
|
---|
179 | if (psh->exception == EXINT
|
---|
180 | #if ATTY
|
---|
181 | && (! attyset(psh) || equal(termval(psh), "emacs"))
|
---|
182 | #endif
|
---|
183 | ) {
|
---|
184 | out2c(psh, '\n');
|
---|
185 | flushout(&psh->errout);
|
---|
186 | }
|
---|
187 | popstackmark(psh, &smark);
|
---|
188 | FORCEINTON; /* enable interrupts */
|
---|
189 | if (state == 1)
|
---|
190 | goto state1;
|
---|
191 | else if (state == 2)
|
---|
192 | goto state2;
|
---|
193 | else if (state == 3)
|
---|
194 | goto state3;
|
---|
195 | else
|
---|
196 | goto state4;
|
---|
197 | }
|
---|
198 | psh->handler = &jmploc;
|
---|
199 | psh->rootpid = /*getpid()*/ psh->pid;
|
---|
200 | psh->rootshell = 1;
|
---|
201 | #ifdef DEBUG
|
---|
202 | #if DEBUG == 2
|
---|
203 | debug(psh) = 1;
|
---|
204 | #endif
|
---|
205 | opentrace(psh);
|
---|
206 | trputs(psh, "Shell args: "); trargs(psh, argv);
|
---|
207 | #endif
|
---|
208 |
|
---|
209 | init(psh);
|
---|
210 | setstackmark(psh, &smark);
|
---|
211 | procargs(psh, argc, argv);
|
---|
212 | if (argv[0] && argv[0][0] == '-') {
|
---|
213 | state = 1;
|
---|
214 | read_profile(psh, "/etc/profile");
|
---|
215 | state1:
|
---|
216 | state = 2;
|
---|
217 | read_profile(psh, ".profile");
|
---|
218 | }
|
---|
219 | state2:
|
---|
220 | state = 3;
|
---|
221 | if (sh_getuid(psh) == sh_geteuid(psh) && sh_getgid(psh) == sh_getegid(psh)) {
|
---|
222 | if ((shinit = lookupvar(psh, "ENV")) != NULL && *shinit != '\0') {
|
---|
223 | state = 3;
|
---|
224 | read_profile(psh, shinit);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | state3:
|
---|
228 | state = 4;
|
---|
229 | if (sflag(psh) == 0 || psh->minusc) {
|
---|
230 | static int sigs[] = {
|
---|
231 | SIGINT, SIGQUIT, SIGHUP,
|
---|
232 | #ifdef SIGTSTP
|
---|
233 | SIGTSTP,
|
---|
234 | #endif
|
---|
235 | SIGPIPE
|
---|
236 | };
|
---|
237 | #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
|
---|
238 | int i;
|
---|
239 |
|
---|
240 | for (i = 0; i < SIGSSIZE; i++)
|
---|
241 | setsignal(psh, sigs[i], 0);
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (psh->minusc)
|
---|
245 | evalstring(psh, psh->minusc, 0);
|
---|
246 |
|
---|
247 | if (sflag(psh) || psh->minusc == NULL) {
|
---|
248 | state4: /* XXX ??? - why isn't this before the "if" statement */
|
---|
249 | cmdloop(psh, 1);
|
---|
250 | }
|
---|
251 | exitshell(psh, psh->exitstatus);
|
---|
252 | /* NOTREACHED */
|
---|
253 | return 1;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Read and execute commands. "Top" is nonzero for the top level command
|
---|
259 | * loop; it turns on prompting if the shell is interactive.
|
---|
260 | */
|
---|
261 |
|
---|
262 | void
|
---|
263 | cmdloop(struct shinstance *psh, int top)
|
---|
264 | {
|
---|
265 | union node *n;
|
---|
266 | struct stackmark smark;
|
---|
267 | int inter;
|
---|
268 | int numeof = 0;
|
---|
269 |
|
---|
270 | TRACE((psh, "cmdloop(%d) called\n", top));
|
---|
271 | setstackmark(psh, &smark);
|
---|
272 | for (;;) {
|
---|
273 | if (psh->pendingsigs)
|
---|
274 | dotrap(psh);
|
---|
275 | inter = 0;
|
---|
276 | if (iflag(psh) && top) {
|
---|
277 | inter = 1;
|
---|
278 | showjobs(psh, psh->out2, SHOW_CHANGED);
|
---|
279 | chkmail(psh, 0);
|
---|
280 | flushout(&psh->errout);
|
---|
281 | }
|
---|
282 | n = parsecmd(psh, inter);
|
---|
283 | /* showtree(n); DEBUG */
|
---|
284 | if (n == NEOF) {
|
---|
285 | if (!top || numeof >= 50)
|
---|
286 | break;
|
---|
287 | if (!stoppedjobs(psh)) {
|
---|
288 | if (!Iflag(psh))
|
---|
289 | break;
|
---|
290 | out2str(psh, "\nUse \"exit\" to leave shell.\n");
|
---|
291 | }
|
---|
292 | numeof++;
|
---|
293 | } else if (n != NULL && nflag(psh) == 0) {
|
---|
294 | psh->job_warning = (psh->job_warning == 2) ? 1 : 0;
|
---|
295 | numeof = 0;
|
---|
296 | evaltree(psh, n, 0);
|
---|
297 | }
|
---|
298 | popstackmark(psh, &smark);
|
---|
299 | setstackmark(psh, &smark);
|
---|
300 | if (psh->evalskip == SKIPFILE) {
|
---|
301 | psh->evalskip = 0;
|
---|
302 | break;
|
---|
303 | }
|
---|
304 | }
|
---|
305 | popstackmark(psh, &smark);
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 |
|
---|
310 | /*
|
---|
311 | * Read /etc/profile or .profile. Return on error.
|
---|
312 | */
|
---|
313 |
|
---|
314 | STATIC void
|
---|
315 | read_profile(struct shinstance *psh, const char *name)
|
---|
316 | {
|
---|
317 | int fd;
|
---|
318 | int xflag_set = 0;
|
---|
319 | int vflag_set = 0;
|
---|
320 |
|
---|
321 | INTOFF;
|
---|
322 | if ((fd = shfile_open(&psh->fdtab, name, O_RDONLY, 0)) >= 0)
|
---|
323 | setinputfd(psh, fd, 1);
|
---|
324 | INTON;
|
---|
325 | if (fd < 0)
|
---|
326 | return;
|
---|
327 | /* -q turns off -x and -v just when executing init files */
|
---|
328 | if (qflag(psh)) {
|
---|
329 | if (xflag(psh))
|
---|
330 | xflag(psh) = 0, xflag_set = 1;
|
---|
331 | if (vflag(psh))
|
---|
332 | vflag(psh) = 0, vflag_set = 1;
|
---|
333 | }
|
---|
334 | cmdloop(psh, 0);
|
---|
335 | if (qflag(psh)) {
|
---|
336 | if (xflag_set)
|
---|
337 | xflag(psh) = 1;
|
---|
338 | if (vflag_set)
|
---|
339 | vflag(psh) = 1;
|
---|
340 | }
|
---|
341 | popfile(psh);
|
---|
342 | }
|
---|
343 |
|
---|
344 |
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * Read a file containing shell functions.
|
---|
348 | */
|
---|
349 |
|
---|
350 | void
|
---|
351 | readcmdfile(struct shinstance *psh, char *name)
|
---|
352 | {
|
---|
353 | int fd;
|
---|
354 |
|
---|
355 | INTOFF;
|
---|
356 | if ((fd = shfile_open(&psh->fdtab, name, O_RDONLY, 0)) >= 0)
|
---|
357 | setinputfd(psh, fd, 1);
|
---|
358 | else
|
---|
359 | error(psh, "Can't open %s", name);
|
---|
360 | INTON;
|
---|
361 | cmdloop(psh, 0);
|
---|
362 | popfile(psh);
|
---|
363 | }
|
---|
364 |
|
---|
365 |
|
---|
366 |
|
---|
367 | /*
|
---|
368 | * Take commands from a file. To be compatible we should do a path
|
---|
369 | * search for the file, which is necessary to find sub-commands.
|
---|
370 | */
|
---|
371 |
|
---|
372 |
|
---|
373 | STATIC char *
|
---|
374 | find_dot_file(struct shinstance *psh, char *basename)
|
---|
375 | {
|
---|
376 | char *fullname;
|
---|
377 | const char *path = pathval(psh);
|
---|
378 | struct stat statb;
|
---|
379 |
|
---|
380 | /* don't try this for absolute or relative paths */
|
---|
381 | if (strchr(basename, '/'))
|
---|
382 | return basename;
|
---|
383 |
|
---|
384 | while ((fullname = padvance(psh, &path, basename)) != NULL) {
|
---|
385 | if ((shfile_stat(&psh->fdtab, fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
|
---|
386 | /*
|
---|
387 | * Don't bother freeing here, since it will
|
---|
388 | * be freed by the caller.
|
---|
389 | */
|
---|
390 | return fullname;
|
---|
391 | }
|
---|
392 | stunalloc(psh, fullname);
|
---|
393 | }
|
---|
394 |
|
---|
395 | /* not found in the PATH */
|
---|
396 | error(psh, "%s: not found", basename);
|
---|
397 | /* NOTREACHED */
|
---|
398 | return NULL;
|
---|
399 | }
|
---|
400 |
|
---|
401 | int
|
---|
402 | dotcmd(struct shinstance *psh, int argc, char **argv)
|
---|
403 | {
|
---|
404 | psh->exitstatus = 0;
|
---|
405 |
|
---|
406 | if (argc >= 2) { /* That's what SVR2 does */
|
---|
407 | char *fullname;
|
---|
408 | struct stackmark smark;
|
---|
409 |
|
---|
410 | setstackmark(psh, &smark);
|
---|
411 | fullname = find_dot_file(psh, argv[1]);
|
---|
412 | setinputfile(psh, fullname, 1);
|
---|
413 | psh->commandname = fullname;
|
---|
414 | cmdloop(psh, 0);
|
---|
415 | popfile(psh);
|
---|
416 | popstackmark(psh, &smark);
|
---|
417 | }
|
---|
418 | return psh->exitstatus;
|
---|
419 | }
|
---|
420 |
|
---|
421 |
|
---|
422 | int
|
---|
423 | exitcmd(struct shinstance *psh, int argc, char **argv)
|
---|
424 | {
|
---|
425 | if (stoppedjobs(psh))
|
---|
426 | return 0;
|
---|
427 | if (argc > 1)
|
---|
428 | psh->exitstatus = number(psh, argv[1]);
|
---|
429 | exitshell(psh, psh->exitstatus);
|
---|
430 | /* NOTREACHED */
|
---|
431 | return 1;
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | STATIC const char *
|
---|
436 | strip_argv0(const char *argv0, size_t *lenp)
|
---|
437 | {
|
---|
438 | const char *tmp;
|
---|
439 |
|
---|
440 | /* skip the path */
|
---|
441 | for (tmp = strpbrk(argv0, "\\/:"); tmp; tmp = strpbrk(argv0, "\\/:"))
|
---|
442 | argv0 = tmp + 1;
|
---|
443 |
|
---|
444 | /* find the end, ignoring extenions */
|
---|
445 | tmp = strrchr(argv0, '.');
|
---|
446 | if (!tmp)
|
---|
447 | tmp = strchr(argv0, '\0');
|
---|
448 | *lenp = tmp - argv0;
|
---|
449 | return argv0;
|
---|
450 | }
|
---|
451 |
|
---|
452 | STATIC int
|
---|
453 | usage(const char *argv0)
|
---|
454 | {
|
---|
455 | size_t len;
|
---|
456 | argv0 = strip_argv0(argv0, &len);
|
---|
457 |
|
---|
458 | fprintf(stdout,
|
---|
459 | "usage: %.*s [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
|
---|
460 | " [+o option_name] [command_file [argument ...]]\n"
|
---|
461 | " or: %.*s -c [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
|
---|
462 | " [+o option_name] command_string [command_name [argument ...]]\n"
|
---|
463 | " or: %.*s -s [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
|
---|
464 | " [+o option_name] [argument ...]\n"
|
---|
465 | " or: %.*s --help\n"
|
---|
466 | " or: %.*s --version\n",
|
---|
467 | len, argv0, len, argv0, len, argv0, len, argv0, len, argv0);
|
---|
468 | return 0;
|
---|
469 | }
|
---|
470 |
|
---|
471 | STATIC int
|
---|
472 | version(const char *argv0)
|
---|
473 | {
|
---|
474 | size_t len;
|
---|
475 | strip_argv0(argv0, &len);
|
---|
476 |
|
---|
477 | fprintf(stdout,
|
---|
478 | "%.*s - kBuild version %d.%d.%d\n",
|
---|
479 | len, argv0,
|
---|
480 | KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
|
---|
481 | return 0;
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | /*
|
---|
486 | * Local Variables:
|
---|
487 | * c-file-style: bsd
|
---|
488 | * End:
|
---|
489 | */
|
---|