VirtualBox

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

Last change on this file since 2304 was 2298, checked in by bird, 16 years ago

kash: no return indicators that works for both gcc and msc (not pretty, but wtf).

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

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