~ubuntu-branches/ubuntu/hardy/klibc/hardy-updates

« back to all changes in this revision

Viewing changes to ash/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeff Bailey
  • Date: 2006-01-04 20:24:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060104202452-ec4v3n829rymukuv
Tags: 1.1.15-0ubuntu1
* New upstream version.

* Patch to fix compilation on parisc64 kernels.

Show diffs side-by-side

added added

removed removed

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