VirtualBox

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

Last change on this file since 1228 was 1222, checked in by bird, 17 years ago

more fixes - it all compiles now (linking fails of course).

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