Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 7077 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

1 /*-
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
100 #ifdef __GLIBC__
101 dash_errno = __errno_location();
102 #endif
103
104 #if PROFILE
105 monitor(4, etext, profile_buf, sizeof profile_buf, 50);
106 #endif
107 state = 0;
108 if (unlikely(setjmp(jmploc.loc))) {
109 int e;
110 int s;
111
112 reset();
113
114 e = exception;
115 if (e == EXERROR)
116 exitstatus = 2;
117
118 s = state;
119 if (e == EXEXIT || s == 0 || iflag == 0 || shlvl)
120 exitshell();
121
122 if (e == EXINT
123 #if ATTY
124 && (! attyset() || equal(termval(), "emacs"))
125 #endif
126 ) {
127 out2c('\n');
128 #ifdef FLUSHERR
129 flushout(out2);
130 #endif
131 }
132 popstackmark(&smark);
133 FORCEINTON; /* enable interrupts */
134 if (s == 1)
135 goto state1;
136 else if (s == 2)
137 goto state2;
138 else if (s == 3)
139 goto state3;
140 else
141 goto state4;
142 }
143 handler = &jmploc;
144 #ifdef DEBUG
145 opentrace();
146 trputs("Shell args: "); trargs(argv);
147 #endif
148 rootpid = getpid();
149 init();
150 setstackmark(&smark);
151 procargs(argc, argv);
152 if (argv[0] && argv[0][0] == '-') {
153 state = 1;
154 read_profile("/etc/profile");
155 state1:
156 state = 2;
157 read_profile(".profile");
158 }
159 state2:
160 state = 3;
161 if (
162 #ifndef linux
163 getuid() == geteuid() && getgid() == getegid() &&
164 #endif
165 iflag
166 ) {
167 if ((shinit = lookupvar("ENV")) != NULL && *shinit != '\0') {
168 read_profile(shinit);
169 }
170 }
171 state3:
172 state = 4;
173 if (minusc)
174 evalstring(minusc, 0);
175
176 if (sflag || minusc == NULL) {
177 state4: /* XXX ??? - why isn't this before the "if" statement */
178 cmdloop(1);
179 }
180 #if PROFILE
181 monitor(0);
182 #endif
183 #if GPROF
184 {
185 extern void _mcleanup(void);
186 _mcleanup();
187 }
188 #endif
189 exitshell();
190 /* NOTREACHED */
191 }
192
193
194 /*
195 * Read and execute commands. "Top" is nonzero for the top level command
196 * loop; it turns on prompting if the shell is interactive.
197 */
198
199 static int
200 cmdloop(int top)
201 {
202 union node *n;
203 struct stackmark smark;
204 int inter;
205 int numeof = 0;
206
207 TRACE(("cmdloop(%d) called\n", top));
208 #ifdef HETIO
209 if(iflag && top)
210 hetio_init();
211 #endif
212 for (;;) {
213 int skip;
214
215 setstackmark(&smark);
216 if (jobctl)
217 showjobs(out2, SHOW_CHANGED);
218 inter = 0;
219 if (iflag && top) {
220 inter++;
221 chkmail();
222 }
223 n = parsecmd(inter);
224 /* showtree(n); DEBUG */
225 if (n == NEOF) {
226 if (!top || numeof >= 50)
227 break;
228 if (!stoppedjobs()) {
229 if (!Iflag)
230 break;
231 out2str("\nUse \"exit\" to leave shell.\n");
232 }
233 numeof++;
234 } else if (nflag == 0) {
235 job_warning = (job_warning == 2) ? 1 : 0;
236 numeof = 0;
237 evaltree(n, 0);
238 }
239 popstackmark(&smark);
240
241 skip = evalskip;
242 if (skip) {
243 evalskip = 0;
244 return skip & SKIPEVAL;
245 }
246 }
247
248 return 0;
249 }
250
251
252
253 /*
254 * Read /etc/profile or .profile. Return on error.
255 */
256
257 STATIC void
258 read_profile(const char *name)
259 {
260 int skip;
261
262 if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0)
263 return;
264
265 skip = cmdloop(0);
266 popfile();
267
268 if (skip)
269 exitshell();
270 }
271
272
273
274 /*
275 * Read a file containing shell functions.
276 */
277
278 void
279 readcmdfile(char *name)
280 {
281 setinputfile(name, INPUT_PUSH_FILE);
282 cmdloop(0);
283 popfile();
284 }
285
286
287
288 /*
289 * Take commands from a file. To be compatible we should do a path
290 * search for the file, which is necessary to find sub-commands.
291 */
292
293
294 STATIC char *
295 find_dot_file(char *basename)
296 {
297 char *fullname;
298 const char *path = pathval();
299 struct stat statb;
300
301 /* don't try this for absolute or relative paths */
302 if (strchr(basename, '/'))
303 return basename;
304
305 while ((fullname = padvance(&path, basename)) != NULL) {
306 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
307 /*
308 * Don't bother freeing here, since it will
309 * be freed by the caller.
310 */
311 return fullname;
312 }
313 stunalloc(fullname);
314 }
315
316 /* not found in the PATH */
317 sh_error("%s: not found", basename);
318 /* NOTREACHED */
319 }
320
321 int
322 dotcmd(int argc, char **argv)
323 {
324 int status = 0;
325
326 if (argc >= 2) { /* That's what SVR2 does */
327 char *fullname;
328
329 fullname = find_dot_file(argv[1]);
330 setinputfile(fullname, INPUT_PUSH_FILE);
331 commandname = fullname;
332 cmdloop(0);
333 popfile();
334 status = exitstatus;
335 }
336 return status;
337 }
338
339
340 int
341 exitcmd(int argc, char **argv)
342 {
343 if (stoppedjobs())
344 return 0;
345 if (argc > 1)
346 exitstatus = number(argv[1]);
347 exraise(EXEXIT);
348 /* NOTREACHED */
349 }