]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/sh/error.c
zfs: merge openzfs/zfs@a94860a6d
[FreeBSD/FreeBSD.git] / bin / sh / error.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)error.c     8.2 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
39 /*
40  * Errors and exceptions.
41  */
42
43 #include "shell.h"
44 #include "eval.h"
45 #include "main.h"
46 #include "options.h"
47 #include "output.h"
48 #include "error.h"
49 #include "nodes.h" /* show.h needs nodes.h */
50 #include "show.h"
51 #include "trap.h"
52 #include <signal.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <errno.h>
56
57
58 /*
59  * Code to handle exceptions in C.
60  */
61
62 struct jmploc *handler;
63 volatile sig_atomic_t exception;
64 volatile sig_atomic_t suppressint;
65 volatile sig_atomic_t intpending;
66
67
68 static void verrorwithstatus(int, const char *, va_list) __printf0like(2, 0) __dead2;
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  * Interrupts are disabled; they should be re-enabled when the exception is
76  * caught.
77  */
78
79 void
80 exraise(int e)
81 {
82         INTOFF;
83         if (handler == NULL)
84                 abort();
85         exception = e;
86         longjmp(handler->loc, 1);
87 }
88
89
90 /*
91  * Called from trap.c when a SIGINT is received and not suppressed, or when
92  * an interrupt is pending and interrupts are re-enabled using INTON.
93  * (If the user specifies that SIGINT is to be trapped or ignored using the
94  * trap builtin, then this routine is not called.)  Suppressint is nonzero
95  * when interrupts are held using the INTOFF macro.  If SIGINTs are not
96  * suppressed and the shell is not a root shell, then we want to be
97  * terminated if we get here, as if we were terminated directly by a SIGINT.
98  * Arrange for this here.
99  */
100
101 void
102 onint(void)
103 {
104         sigset_t sigs;
105
106         intpending = 0;
107         sigemptyset(&sigs);
108         sigprocmask(SIG_SETMASK, &sigs, NULL);
109
110         /*
111          * This doesn't seem to be needed, since main() emits a newline.
112          */
113 #if 0
114         if (tcgetpgrp(0) == getpid())
115                 write(STDERR_FILENO, "\n", 1);
116 #endif
117         if (rootshell && iflag)
118                 exraise(EXINT);
119         else {
120                 signal(SIGINT, SIG_DFL);
121                 kill(getpid(), SIGINT);
122                 _exit(128 + SIGINT);
123         }
124 }
125
126
127 static void
128 vwarning(const char *msg, va_list ap)
129 {
130         if (commandname)
131                 outfmt(out2, "%s: ", commandname);
132         else if (arg0)
133                 outfmt(out2, "%s: ", arg0);
134         doformat(out2, msg, ap);
135         out2fmt_flush("\n");
136 }
137
138
139 void
140 warning(const char *msg, ...)
141 {
142         va_list ap;
143         va_start(ap, msg);
144         vwarning(msg, ap);
145         va_end(ap);
146 }
147
148
149 /*
150  * Exverror is called to raise the error exception.  If the first argument
151  * is not NULL then error prints an error message using printf style
152  * formatting.  It then raises the error exception.
153  */
154 static void
155 verrorwithstatus(int status, const char *msg, va_list ap)
156 {
157         /*
158          * An interrupt trumps an error.  Certain places catch error
159          * exceptions or transform them to a plain nonzero exit code
160          * in child processes, and if an error exception can be handled,
161          * an interrupt can be handled as well.
162          *
163          * exraise() will disable interrupts for the exception handler.
164          */
165         FORCEINTON;
166
167 #ifdef DEBUG
168         if (msg)
169                 TRACE(("verrorwithstatus(%d, \"%s\") pid=%d\n",
170                     status, msg, getpid()));
171         else
172                 TRACE(("verrorwithstatus(%d, NULL) pid=%d\n",
173                     status, getpid()));
174 #endif
175         if (msg)
176                 vwarning(msg, ap);
177         flushall();
178         exitstatus = status;
179         exraise(EXERROR);
180 }
181
182
183 void
184 error(const char *msg, ...)
185 {
186         va_list ap;
187         va_start(ap, msg);
188         verrorwithstatus(2, msg, ap);
189         va_end(ap);
190 }
191
192
193 void
194 errorwithstatus(int status, const char *msg, ...)
195 {
196         va_list ap;
197         va_start(ap, msg);
198         verrorwithstatus(status, msg, ap);
199         va_end(ap);
200 }