source: trunk/src/kash/main.c

Last change on this file was 3477, checked in by bird, 5 years ago

kash: Use kHlpAssert instead of assert.h (debugger stops on the assertion rather than at exit process code).

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 11.2 KB
RevLine 
[626]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
[1214]35#if 0
[626]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 */
[1214]45#endif
46
[626]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"
[881]73#include "shinstance.h"
[626]74
75#define PROFILE 0
76
[880]77/*int rootpid;
78int rootshell;*/
[1198]79#ifdef unused_variables
[626]80STATIC union node *curcmd;
81STATIC union node *prevcmd;
[1198]82#endif
[626]83
[881]84STATIC void read_profile(struct shinstance *, const char *);
85STATIC char *find_dot_file(struct shinstance *, char *);
[2286]86int main(int, char **, char **);
[2298]87SH_NORETURN_1 void shell_main(shinstance *, int, char **) SH_NORETURN_2;
[880]88#ifdef _MSC_VER
89extern void init_syntax(void);
90#endif
[1198]91STATIC int usage(const char *argv0);
92STATIC int version(const char *argv0);
[626]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
[3438]103#if K_OS == K_OS_WINDOWS && defined(SH_FORKED_MODE)
[2292]104real_main(int argc, char **argv, char **envp)
105#else
[2286]106main(int argc, char **argv, char **envp)
[2292]107#endif
[626]108{
[879]109 shinstance *psh;
110
[880]111 /*
[1198]112 * Global initializations.
113 */
[879]114 setlocale(LC_ALL, "");
[880]115#ifdef _MSC_VER
116 init_syntax();
117#endif
[1198]118
[879]119 /*
[1198]120 * Check for --version and --help.
[1214]121 */
[1198]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 /*
[879]130 * Create the root shell instance.
131 */
[3438]132 psh = sh_create_root_shell(argv, envp);
[879]133 if (!psh)
134 return 2;
[880]135 shthread_set_shell(psh);
[3438]136 shell_main(psh, argc, psh->orgargv);
[2298]137 /* Not reached. */
138 return 89;
[1198]139}
[879]140
[2298]141SH_NORETURN_1 void
[879]142shell_main(shinstance *psh, int argc, char **argv)
143{
[626]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 */
[880]156 switch (psh->exception) {
[626]157 case EXSHELLPROC:
[880]158 psh->rootpid = /*getpid()*/ psh->pid;
159 psh->rootshell = 1;
160 psh->minusc = NULL;
[881]161 state = 3;
[626]162 break;
163
164 case EXEXEC:
[880]165 psh->exitstatus = psh->exerrno;
[626]166 break;
167
168 case EXERROR:
[880]169 psh->exitstatus = 2;
[626]170 break;
171
172 default:
173 break;
174 }
175
[880]176 if (psh->exception != EXSHELLPROC) {
[1198]177 if (state == 0 || iflag(psh) == 0 || ! psh->rootshell)
[881]178 exitshell(psh, psh->exitstatus);
[626]179 }
[880]180 reset(psh);
181 if (psh->exception == EXINT
[626]182#if ATTY
[880]183 && (! attyset(psh) || equal(termval(psh), "emacs"))
[626]184#endif
185 ) {
[880]186 out2c(psh, '\n');
[881]187 flushout(&psh->errout);
[626]188 }
[880]189 popstackmark(psh, &smark);
[626]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 }
[879]200 psh->handler = &jmploc;
[880]201 psh->rootpid = /*getpid()*/ psh->pid;
202 psh->rootshell = 1;
[626]203#ifdef DEBUG
204#if DEBUG == 2
[1198]205 debug(psh) = 1;
[626]206#endif
[880]207 opentrace(psh);
[1199]208 trputs(psh, "Shell args: "); trargs(psh, argv);
[626]209#endif
[880]210
211 init(psh);
212 setstackmark(psh, &smark);
213 procargs(psh, argc, argv);
[626]214 if (argv[0] && argv[0][0] == '-') {
215 state = 1;
[879]216 read_profile(psh, "/etc/profile");
[626]217state1:
218 state = 2;
[879]219 read_profile(psh, ".profile");
[626]220 }
221state2:
222 state = 3;
[1207]223 if (sh_getuid(psh) == sh_geteuid(psh) && sh_getgid(psh) == sh_getegid(psh)) {
[879]224 if ((shinit = lookupvar(psh, "ENV")) != NULL && *shinit != '\0') {
[626]225 state = 3;
[879]226 read_profile(psh, shinit);
[626]227 }
228 }
229state3:
230 state = 4;
[1198]231 if (sflag(psh) == 0 || psh->minusc) {
[626]232 static int sigs[] = {
[809]233 SIGINT, SIGQUIT, SIGHUP,
[626]234#ifdef SIGTSTP
235 SIGTSTP,
236#endif
237 SIGPIPE
238 };
239#define SIGSSIZE (sizeof(sigs)/sizeof(sigs[0]))
[2311]240 unsigned i;
[626]241
242 for (i = 0; i < SIGSSIZE; i++)
[3435]243 setsignal(psh, sigs[i]);
[626]244 }
245
[879]246 if (psh->minusc)
247 evalstring(psh, psh->minusc, 0);
[626]248
[1198]249 if (sflag(psh) || psh->minusc == NULL) {
[626]250state4: /* XXX ??? - why isn't this before the "if" statement */
[879]251 cmdloop(psh, 1);
[626]252 }
[879]253 exitshell(psh, psh->exitstatus);
[626]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
[880]264cmdloop(struct shinstance *psh, int top)
[626]265{
266 union node *n;
267 struct stackmark smark;
268 int inter;
269 int numeof = 0;
270
[1199]271 TRACE((psh, "cmdloop(%d) called\n", top));
[880]272 setstackmark(psh, &smark);
[626]273 for (;;) {
[880]274 if (psh->pendingsigs)
275 dotrap(psh);
[626]276 inter = 0;
[1198]277 if (iflag(psh) && top) {
[626]278 inter = 1;
[880]279 showjobs(psh, psh->out2, SHOW_CHANGED);
280 chkmail(psh, 0);
281 flushout(&psh->errout);
[626]282 }
[880]283 n = parsecmd(psh, inter);
[626]284 /* showtree(n); DEBUG */
285 if (n == NEOF) {
286 if (!top || numeof >= 50)
287 break;
[880]288 if (!stoppedjobs(psh)) {
[1198]289 if (!Iflag(psh))
[626]290 break;
[880]291 out2str(psh, "\nUse \"exit\" to leave shell.\n");
[626]292 }
293 numeof++;
[1198]294 } else if (n != NULL && nflag(psh) == 0) {
[880]295 psh->job_warning = (psh->job_warning == 2) ? 1 : 0;
[626]296 numeof = 0;
[880]297 evaltree(psh, n, 0);
[626]298 }
[880]299 popstackmark(psh, &smark);
300 setstackmark(psh, &smark);
301 if (psh->evalskip == SKIPFILE) {
302 psh->evalskip = 0;
[626]303 break;
304 }
305 }
[880]306 popstackmark(psh, &smark);
[626]307}
308
309
310
311/*
312 * Read /etc/profile or .profile. Return on error.
313 */
314
315STATIC void
[880]316read_profile(struct shinstance *psh, const char *name)
[626]317{
318 int fd;
319 int xflag_set = 0;
320 int vflag_set = 0;
321
322 INTOFF;
[1222]323 if ((fd = shfile_open(&psh->fdtab, name, O_RDONLY, 0)) >= 0)
[880]324 setinputfd(psh, fd, 1);
[626]325 INTON;
326 if (fd < 0)
327 return;
328 /* -q turns off -x and -v just when executing init files */
[1198]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;
[626]334 }
[880]335 cmdloop(psh, 0);
[1198]336 if (qflag(psh)) {
[626]337 if (xflag_set)
[1198]338 xflag(psh) = 1;
[626]339 if (vflag_set)
[1198]340 vflag(psh) = 1;
[626]341 }
[880]342 popfile(psh);
[626]343}
344
345
346
347/*
348 * Read a file containing shell functions.
349 */
350
351void
[880]352readcmdfile(struct shinstance *psh, char *name)
[626]353{
354 int fd;
355
356 INTOFF;
[1222]357 if ((fd = shfile_open(&psh->fdtab, name, O_RDONLY, 0)) >= 0)
[880]358 setinputfd(psh, fd, 1);
[626]359 else
[881]360 error(psh, "Can't open %s", name);
[626]361 INTON;
[880]362 cmdloop(psh, 0);
363 popfile(psh);
[626]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 *
[880]375find_dot_file(struct shinstance *psh, char *basename)
[626]376{
377 char *fullname;
[880]378 const char *path = pathval(psh);
[626]379
380 /* don't try this for absolute or relative paths */
381 if (strchr(basename, '/'))
382 return basename;
383
[880]384 while ((fullname = padvance(psh, &path, basename)) != NULL) {
[3474]385 if (shfile_stat_isreg(&psh->fdtab, fullname) > 0) {
[626]386 /*
387 * Don't bother freeing here, since it will
388 * be freed by the caller.
389 */
390 return fullname;
391 }
[880]392 stunalloc(psh, fullname);
[626]393 }
394
395 /* not found in the PATH */
[881]396 error(psh, "%s: not found", basename);
[626]397 /* NOTREACHED */
[881]398 return NULL;
[626]399}
400
401int
[880]402dotcmd(struct shinstance *psh, int argc, char **argv)
[626]403{
[880]404 psh->exitstatus = 0;
[626]405
406 if (argc >= 2) { /* That's what SVR2 does */
[3438]407 char * const savedcommandname = psh->commandname;
408 int const savedcommandnamemalloc = psh->commandnamemalloc;
[626]409 char *fullname;
410 struct stackmark smark;
411
[880]412 setstackmark(psh, &smark);
413 fullname = find_dot_file(psh, argv[1]);
414 setinputfile(psh, fullname, 1);
415 psh->commandname = fullname;
[3438]416 psh->commandnamemalloc = 0;
[880]417 cmdloop(psh, 0);
418 popfile(psh);
[3438]419 psh->commandname = savedcommandname;
420 psh->commandnamemalloc = savedcommandnamemalloc;
[880]421 popstackmark(psh, &smark);
[626]422 }
[880]423 return psh->exitstatus;
[626]424}
425
426
427int
[880]428exitcmd(struct shinstance *psh, int argc, char **argv)
[626]429{
[880]430 if (stoppedjobs(psh))
[626]431 return 0;
432 if (argc > 1)
[1198]433 psh->exitstatus = number(psh, argv[1]);
[881]434 exitshell(psh, psh->exitstatus);
[626]435 /* NOTREACHED */
[881]436 return 1;
[626]437}
[880]438
[1198]439
440STATIC const char *
[2311]441strip_argv0(const char *argv0, unsigned *lenp)
[1198]442{
443 const char *tmp;
444
445 /* skip the path */
446 for (tmp = strpbrk(argv0, "\\/:"); tmp; tmp = strpbrk(argv0, "\\/:"))
447 argv0 = tmp + 1;
448
449 /* find the end, ignoring extenions */
450 tmp = strrchr(argv0, '.');
451 if (!tmp)
452 tmp = strchr(argv0, '\0');
[2311]453 *lenp = (unsigned)(tmp - argv0);
[1198]454 return argv0;
455}
456
457STATIC int
458usage(const char *argv0)
459{
[2311]460 unsigned len;
[1229]461 argv0 = strip_argv0(argv0, &len);
[1198]462
463 fprintf(stdout,
464 "usage: %.*s [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
465 " [+o option_name] [command_file [argument ...]]\n"
466 " or: %.*s -c [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
467 " [+o option_name] command_string [command_name [argument ...]]\n"
468 " or: %.*s -s [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]\n"
469 " [+o option_name] [argument ...]\n"
470 " or: %.*s --help\n"
471 " or: %.*s --version\n",
472 len, argv0, len, argv0, len, argv0, len, argv0, len, argv0);
473 return 0;
474}
475
476STATIC int
477version(const char *argv0)
478{
[2311]479 unsigned len;
[1198]480 strip_argv0(argv0, &len);
481
482 fprintf(stdout,
[2457]483 "%.*s - kBuild version %d.%d.%d (r%u)\n",
[1198]484 len, argv0,
[2457]485 KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH, KBUILD_SVN_REV);
[1198]486 return 0;
487}
488
489
[880]490/*
491 * Local Variables:
492 * c-file-style: bsd
493 * End:
494 */
Note: See TracBrowser for help on using the repository browser.