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