Magellan Linux

Contents of /trunk/mkinitrd-magellan/klibc/usr/dash/error.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: 5189 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 /*
36 * Errors and exceptions.
37 */
38
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <string.h>
45
46 #include "shell.h"
47 #include "main.h"
48 #include "options.h"
49 #include "output.h"
50 #include "error.h"
51 #include "show.h"
52 #include "eval.h"
53 #include "parser.h"
54 #include "trap.h"
55 #include "system.h"
56
57
58 /*
59 * Code to handle exceptions in C.
60 */
61
62 struct jmploc *handler;
63 int exception;
64 int suppressint;
65 volatile sig_atomic_t intpending;
66
67
68 static void exverror(int, const char *, va_list)
69 __attribute__((__noreturn__));
70
71 /*
72 * Called to raise an exception. Since C doesn't include exceptions, we
73 * just do a longjmp to the exception handler. The type of exception is
74 * stored in the global variable "exception".
75 */
76
77 void
78 exraise(int e)
79 {
80 #ifdef DEBUG
81 if (handler == NULL)
82 abort();
83 #endif
84 INTOFF;
85
86 exception = e;
87 longjmp(handler->loc, 1);
88 }
89
90
91 /*
92 * Called from trap.c when a SIGINT is received. (If the user specifies
93 * that SIGINT is to be trapped or ignored using the trap builtin, then
94 * this routine is not called.) Suppressint is nonzero when interrupts
95 * are held using the INTOFF macro. (The test for iflag is just
96 * defensive programming.)
97 */
98
99 void
100 onint(void) {
101 int i;
102
103 intpending = 0;
104 sigclearmask();
105 i = EXSIG;
106 if (gotsig[SIGINT - 1] && !trap[SIGINT]) {
107 if (!(rootshell && iflag)) {
108 signal(SIGINT, SIG_DFL);
109 raise(SIGINT);
110 }
111 i = EXINT;
112 }
113 exraise(i);
114 /* NOTREACHED */
115 }
116
117 static void
118 exvwarning2(const char *msg, va_list ap)
119 {
120 struct output *errs;
121 const char *name;
122 const char *fmt;
123
124 errs = out2;
125 name = arg0;
126 fmt = "%s: ";
127 if (commandname) {
128 name = commandname;
129 fmt = "%s: %d: ";
130 }
131 outfmt(errs, fmt, name, startlinno);
132 doformat(errs, msg, ap);
133 #if FLUSHERR
134 outc('\n', errs);
135 #else
136 outcslow('\n', errs);
137 #endif
138 }
139
140 #define exvwarning(a, b, c) exvwarning2(b, c)
141
142 /*
143 * Exverror is called to raise the error exception. If the second argument
144 * is not NULL then error prints an error message using printf style
145 * formatting. It then raises the error exception.
146 */
147 static void
148 exverror(int cond, const char *msg, va_list ap)
149 {
150 #ifdef DEBUG
151 if (msg) {
152 TRACE(("exverror(%d, \"", cond));
153 TRACEV((msg, ap));
154 TRACE(("\") pid=%d\n", getpid()));
155 } else
156 TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
157 if (msg)
158 #endif
159 exvwarning(-1, msg, ap);
160
161 flushall();
162 exraise(cond);
163 /* NOTREACHED */
164 }
165
166
167 void
168 sh_error(const char *msg, ...)
169 {
170 va_list ap;
171
172 va_start(ap, msg);
173 exverror(EXERROR, msg, ap);
174 /* NOTREACHED */
175 va_end(ap);
176 }
177
178
179 void
180 exerror(int cond, const char *msg, ...)
181 {
182 va_list ap;
183
184 va_start(ap, msg);
185 exverror(cond, msg, ap);
186 /* NOTREACHED */
187 va_end(ap);
188 }
189
190 /*
191 * error/warning routines for external builtins
192 */
193
194 void
195 sh_warnx(const char *fmt, ...)
196 {
197 va_list ap;
198
199 va_start(ap, fmt);
200 exvwarning(-1, fmt, ap);
201 va_end(ap);
202 }
203
204
205 /*
206 * Return a string describing an error. The returned string may be a
207 * pointer to a static buffer that will be overwritten on the next call.
208 * Action describes the operation that got the error.
209 */
210
211 const char *
212 errmsg(int e, int action)
213 {
214 if (e != ENOENT && e != ENOTDIR)
215 return strerror(e);
216
217 if (action & E_OPEN)
218 return "No such file";
219 else if (action & E_CREAT)
220 return "Directory nonexistent";
221 else
222 return "not found";
223 }
224
225
226 #ifdef REALLY_SMALL
227 void
228 __inton() {
229 if (--suppressint == 0 && intpending) {
230 onint();
231 }
232 }
233 #endif