VirtualBox

source: kBuild/trunk/src/kash/main.c@ 3451

Last change on this file since 3451 was 3438, checked in by bird, 5 years ago

kash: Hammering on threaded mode.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette