kbuild-0.1.5.ebuild: 0.1.4 -> 0.1.5.
[kbuild-mirror.git] / src / kash / main.c
blobb808ea39544790748d34702bf46ce64aac719daa
1 /* $NetBSD: main.c,v 1.48 2003/09/14 12:09:29 jmmv Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
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.
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.
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
47 #include <errno.h>
48 #include <stdio.h>
49 #include <sys/stat.h>
50 #include <locale.h>
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"
75 #define PROFILE 0
77 /*int rootpid;
78 int rootshell;*/
79 #ifdef unused_variables
80 STATIC union node *curcmd;
81 STATIC union node *prevcmd;
82 #endif
84 STATIC void read_profile(struct shinstance *, const char *);
85 STATIC char *find_dot_file(struct shinstance *, char *);
86 int main(int, 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);
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.
103 main(int argc, char **argv)
105 shinstance *psh;
108 * Global initializations.
110 setlocale(LC_ALL, "");
111 #ifdef _MSC_VER
112 init_syntax();
113 #endif
116 * Check for --version and --help.
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]);
126 * Create the root shell instance.
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);
136 shell_main(shinstance *psh, int argc, char **argv)
138 struct jmploc jmploc;
139 struct stackmark smark;
140 volatile int state;
141 char *shinit;
143 state = 0;
144 if (setjmp(jmploc.loc)) {
146 * When a shell procedure is executed, we raise the
147 * exception EXSHELLPROC to clean up before executing
148 * the shell procedure.
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;
158 case EXEXEC:
159 psh->exitstatus = psh->exerrno;
160 break;
162 case EXERROR:
163 psh->exitstatus = 2;
164 break;
166 default:
167 break;
170 if (psh->exception != EXSHELLPROC) {
171 if (state == 0 || iflag(psh) == 0 || ! psh->rootshell)
172 exitshell(psh, psh->exitstatus);
174 reset(psh);
175 if (psh->exception == EXINT
176 #if ATTY
177 && (! attyset(psh) || equal(termval(psh), "emacs"))
178 #endif
180 out2c(psh, '\n');
181 flushout(&psh->errout);
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;
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
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");
211 state1:
212 state = 2;
213 read_profile(psh, ".profile");
215 state2:
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);
223 state3:
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
233 #define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
234 int i;
236 for (i = 0; i < SIGSSIZE; i++)
237 setsignal(psh, sigs[i], 0);
240 if (psh->minusc)
241 evalstring(psh, psh->minusc, 0);
243 if (sflag(psh) || psh->minusc == NULL) {
244 state4: /* XXX ??? - why isn't this before the "if" statement */
245 cmdloop(psh, 1);
247 exitshell(psh, psh->exitstatus);
248 /* NOTREACHED */
249 return 1;
254 * Read and execute commands. "Top" is nonzero for the top level command
255 * loop; it turns on prompting if the shell is interactive.
258 void
259 cmdloop(struct shinstance *psh, int top)
261 union node *n;
262 struct stackmark smark;
263 int inter;
264 int numeof = 0;
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);
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");
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);
294 popstackmark(psh, &smark);
295 setstackmark(psh, &smark);
296 if (psh->evalskip == SKIPFILE) {
297 psh->evalskip = 0;
298 break;
301 popstackmark(psh, &smark);
307 * Read /etc/profile or .profile. Return on error.
310 STATIC void
311 read_profile(struct shinstance *psh, const char *name)
313 int fd;
314 int xflag_set = 0;
315 int vflag_set = 0;
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;
330 cmdloop(psh, 0);
331 if (qflag(psh)) {
332 if (xflag_set)
333 xflag(psh) = 1;
334 if (vflag_set)
335 vflag(psh) = 1;
337 popfile(psh);
343 * Read a file containing shell functions.
346 void
347 readcmdfile(struct shinstance *psh, char *name)
349 int fd;
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);
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.
369 STATIC char *
370 find_dot_file(struct shinstance *psh, char *basename)
372 char *fullname;
373 const char *path = pathval(psh);
374 struct stat statb;
376 /* don't try this for absolute or relative paths */
377 if (strchr(basename, '/'))
378 return basename;
380 while ((fullname = padvance(psh, &path, basename)) != NULL) {
381 if ((shfile_stat(&psh->fdtab, fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
383 * Don't bother freeing here, since it will
384 * be freed by the caller.
386 return fullname;
388 stunalloc(psh, fullname);
391 /* not found in the PATH */
392 error(psh, "%s: not found", basename);
393 /* NOTREACHED */
394 return NULL;
398 dotcmd(struct shinstance *psh, int argc, char **argv)
400 psh->exitstatus = 0;
402 if (argc >= 2) { /* That's what SVR2 does */
403 char *fullname;
404 struct stackmark smark;
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);
414 return psh->exitstatus;
419 exitcmd(struct shinstance *psh, int argc, char **argv)
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;
431 STATIC const char *
432 strip_argv0(const char *argv0, size_t *lenp)
434 const char *tmp;
436 /* skip the path */
437 for (tmp = strpbrk(argv0, "\\/:"); tmp; tmp = strpbrk(argv0, "\\/:"))
438 argv0 = tmp + 1;
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;
448 STATIC int
449 usage(const char *argv0)
451 size_t len;
452 argv0 = strip_argv0(argv0, &len);
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;
467 STATIC int
468 version(const char *argv0)
470 size_t len;
471 strip_argv0(argv0, &len);
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;
482 * Local Variables:
483 * c-file-style: bsd
484 * End: