source: trunk/src/kash/main.c@ 1214

Last change on this file since 1214 was 1214, checked in by bird, 18 years ago

some more cleanup.

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