Magellan Linux

Annotation of /trunk/mkinitrd-magellan/klibc/usr/dash/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1122 - (hide annotations) (download)
Wed Aug 18 21:11:40 2010 UTC (13 years, 9 months ago) by niro
File MIME type: text/plain
File size: 7065 byte(s)
-updated to klibc-1.5.19
1 niro 532 /*-
2     * Copyright (c) 1991, 1993
3     * The Regents of the University of California. All rights reserved.
4     * Copyright (c) 1997-2005
5     * Herbert Xu <herbert@gondor.apana.org.au>. 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     #include <stdio.h>
36     #include <signal.h>
37     #include <sys/stat.h>
38     #include <unistd.h>
39     #include <fcntl.h>
40    
41    
42     #include "shell.h"
43     #include "main.h"
44     #include "mail.h"
45     #include "options.h"
46     #include "output.h"
47     #include "parser.h"
48     #include "nodes.h"
49     #include "expand.h"
50     #include "eval.h"
51     #include "jobs.h"
52     #include "input.h"
53     #include "trap.h"
54     #include "var.h"
55     #include "show.h"
56     #include "memalloc.h"
57     #include "error.h"
58     #include "init.h"
59     #include "mystring.h"
60     #include "exec.h"
61     #include "cd.h"
62    
63     #ifdef HETIO
64     #include "hetio.h"
65     #endif
66    
67     #define PROFILE 0
68    
69     int rootpid;
70     int shlvl;
71     #ifdef __GLIBC__
72     int *dash_errno;
73     #endif
74     #if PROFILE
75     short profile_buf[16384];
76     extern int etext();
77     #endif
78    
79     STATIC void read_profile(const char *);
80     STATIC char *find_dot_file(char *);
81     static int cmdloop(int);
82     int main(int, char **);
83    
84     /*
85     * Main routine. We initialize things, parse the arguments, execute
86     * profiles if we're a login shell, and then call cmdloop to execute
87     * commands. The setjmp call sets up the location to jump to when an
88     * exception occurs. When an exception occurs the variable "state"
89     * is used to figure out how far we had gotten.
90     */
91    
92     int
93     main(int argc, char **argv)
94     {
95     char *shinit;
96     volatile int state;
97     struct jmploc jmploc;
98     struct stackmark smark;
99 niro 1122 int login;
100 niro 532
101     #ifdef __GLIBC__
102     dash_errno = __errno_location();
103     #endif
104    
105     #if PROFILE
106     monitor(4, etext, profile_buf, sizeof profile_buf, 50);
107     #endif
108     state = 0;
109     if (unlikely(setjmp(jmploc.loc))) {
110     int e;
111     int s;
112    
113     reset();
114    
115     e = exception;
116     if (e == EXERROR)
117     exitstatus = 2;
118    
119     s = state;
120     if (e == EXEXIT || s == 0 || iflag == 0 || shlvl)
121     exitshell();
122    
123     if (e == EXINT
124     #if ATTY
125     && (! attyset() || equal(termval(), "emacs"))
126     #endif
127     ) {
128     out2c('\n');
129     #ifdef FLUSHERR
130     flushout(out2);
131     #endif
132     }
133     popstackmark(&smark);
134     FORCEINTON; /* enable interrupts */
135     if (s == 1)
136     goto state1;
137     else if (s == 2)
138     goto state2;
139     else if (s == 3)
140     goto state3;
141     else
142     goto state4;
143     }
144     handler = &jmploc;
145     #ifdef DEBUG
146     opentrace();
147     trputs("Shell args: "); trargs(argv);
148     #endif
149     rootpid = getpid();
150     init();
151     setstackmark(&smark);
152 niro 1122 login = procargs(argc, argv);
153     if (login) {
154 niro 532 state = 1;
155     read_profile("/etc/profile");
156     state1:
157     state = 2;
158 niro 1122 read_profile("$HOME/.profile");
159 niro 532 }
160     state2:
161     state = 3;
162     if (
163     #ifndef linux
164     getuid() == geteuid() && getgid() == getegid() &&
165     #endif
166     iflag
167     ) {
168     if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
169     read_profile(shinit);
170     }
171     }
172 niro 1122 popstackmark(&smark);
173 niro 532 state3:
174     state = 4;
175     if (minusc)
176     evalstring(minusc, 0);
177    
178     if (sflag || minusc == NULL) {
179     state4: /* XXX ??? - why isn't this before the "if" statement */
180     cmdloop(1);
181     }
182     #if PROFILE
183     monitor(0);
184     #endif
185     #if GPROF
186     {
187     extern void _mcleanup(void);
188     _mcleanup();
189     }
190     #endif
191     exitshell();
192     /* NOTREACHED */
193     }
194    
195    
196     /*
197     * Read and execute commands. "Top" is nonzero for the top level command
198     * loop; it turns on prompting if the shell is interactive.
199     */
200    
201     static int
202     cmdloop(int top)
203     {
204     union node *n;
205     struct stackmark smark;
206     int inter;
207     int numeof = 0;
208    
209     TRACE(("cmdloop(%d) called\n", top));
210     #ifdef HETIO
211     if(iflag && top)
212     hetio_init();
213     #endif
214     for (;;) {
215     int skip;
216    
217     setstackmark(&smark);
218     if (jobctl)
219     showjobs(out2, SHOW_CHANGED);
220     inter = 0;
221     if (iflag && top) {
222     inter++;
223     chkmail();
224     }
225     n = parsecmd(inter);
226     /* showtree(n); DEBUG */
227     if (n == NEOF) {
228     if (!top || numeof >= 50)
229     break;
230     if (!stoppedjobs()) {
231     if (!Iflag)
232     break;
233     out2str("\nUse \"exit\" to leave shell.\n");
234     }
235     numeof++;
236     } else if (nflag == 0) {
237     job_warning = (job_warning == 2) ? 1 : 0;
238     numeof = 0;
239     evaltree(n, 0);
240     }
241     popstackmark(&smark);
242    
243     skip = evalskip;
244     if (skip) {
245     evalskip = 0;
246 niro 1122 break;
247 niro 532 }
248     }
249    
250     return 0;
251     }
252    
253    
254    
255     /*
256     * Read /etc/profile or .profile. Return on error.
257     */
258    
259     STATIC void
260     read_profile(const char *name)
261     {
262 niro 1122 name = expandstr(name);
263 niro 532 if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0)
264     return;
265    
266 niro 1122 cmdloop(0);
267 niro 532 popfile();
268     }
269    
270    
271    
272     /*
273     * Read a file containing shell functions.
274     */
275    
276     void
277     readcmdfile(char *name)
278     {
279     setinputfile(name, INPUT_PUSH_FILE);
280     cmdloop(0);
281     popfile();
282     }
283    
284    
285    
286     /*
287     * Take commands from a file. To be compatible we should do a path
288     * search for the file, which is necessary to find sub-commands.
289     */
290    
291    
292     STATIC char *
293     find_dot_file(char *basename)
294     {
295     char *fullname;
296     const char *path = pathval();
297     struct stat statb;
298    
299     /* don't try this for absolute or relative paths */
300     if (strchr(basename, '/'))
301     return basename;
302    
303     while ((fullname = padvance(&path, basename)) != NULL) {
304     if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
305     /*
306     * Don't bother freeing here, since it will
307     * be freed by the caller.
308     */
309     return fullname;
310     }
311     stunalloc(fullname);
312     }
313    
314     /* not found in the PATH */
315     sh_error("%s: not found", basename);
316     /* NOTREACHED */
317     }
318    
319     int
320     dotcmd(int argc, char **argv)
321     {
322     int status = 0;
323    
324     if (argc >= 2) { /* That's what SVR2 does */
325     char *fullname;
326    
327     fullname = find_dot_file(argv[1]);
328     setinputfile(fullname, INPUT_PUSH_FILE);
329     commandname = fullname;
330     cmdloop(0);
331     popfile();
332     status = exitstatus;
333     }
334     return status;
335     }
336    
337    
338     int
339     exitcmd(int argc, char **argv)
340     {
341     if (stoppedjobs())
342     return 0;
343     if (argc > 1)
344     exitstatus = number(argv[1]);
345     exraise(EXEXIT);
346     /* NOTREACHED */
347     }