1*f438a6a0Shannken /* $NetBSD: error.h,v 1.25 2023/03/21 08:31:30 hannken Exp $ */ 249f0ad86Scgd 361f28255Scgd /*- 437ed7877Sjtc * Copyright (c) 1991, 1993 537ed7877Sjtc * The Regents of the University of California. All rights reserved. 661f28255Scgd * 761f28255Scgd * This code is derived from software contributed to Berkeley by 861f28255Scgd * Kenneth Almquist. 961f28255Scgd * 1061f28255Scgd * Redistribution and use in source and binary forms, with or without 1161f28255Scgd * modification, are permitted provided that the following conditions 1261f28255Scgd * are met: 1361f28255Scgd * 1. Redistributions of source code must retain the above copyright 1461f28255Scgd * notice, this list of conditions and the following disclaimer. 1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright 1661f28255Scgd * notice, this list of conditions and the following disclaimer in the 1761f28255Scgd * documentation and/or other materials provided with the distribution. 18b5b29542Sagc * 3. Neither the name of the University nor the names of its contributors 1961f28255Scgd * may be used to endorse or promote products derived from this software 2061f28255Scgd * without specific prior written permission. 2161f28255Scgd * 2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3261f28255Scgd * SUCH DAMAGE. 3361f28255Scgd * 3407bae7edSchristos * @(#)error.h 8.2 (Berkeley) 5/4/95 3561f28255Scgd */ 3661f28255Scgd 37c02b3bbdSchristos #include <stdarg.h> 38c02b3bbdSchristos 3961f28255Scgd /* 4061f28255Scgd * Types of operations (passed to the errmsg routine). 4161f28255Scgd */ 4261f28255Scgd 43d45073cdSkre #define E_OPEN 0x1 /* opening a file */ 44d45073cdSkre #define E_CREAT 0x2 /* creating a file */ 45d45073cdSkre #define E_EXEC 0x4 /* executing a program */ 4661f28255Scgd 4761f28255Scgd 4861f28255Scgd /* 4961f28255Scgd * We enclose jmp_buf in a structure so that we can declare pointers to 5061f28255Scgd * jump locations. The global variable handler contains the location to 5161f28255Scgd * jump to when an exception occurs, and the global variable exception 5261f28255Scgd * contains a code identifying the exeception. To implement nested 5361f28255Scgd * exception handlers, the user should save the value of handler on entry 5461f28255Scgd * to an inner scope, set handler to point to a jmploc structure for the 5561f28255Scgd * inner scope, and restore handler on exit from the scope. 5661f28255Scgd */ 5761f28255Scgd 5861f28255Scgd #include <setjmp.h> 5961f28255Scgd 6061f28255Scgd struct jmploc { 61*f438a6a0Shannken sigjmp_buf loc; 6261f28255Scgd }; 6361f28255Scgd 644084f829Skre extern volatile int errors_suppressed; 654084f829Skre extern const char * volatile currentcontext; 664084f829Skre 6761f28255Scgd extern struct jmploc *handler; 6861f28255Scgd extern int exception; 69c02b3bbdSchristos extern int exerrno; /* error for EXEXEC */ 7061f28255Scgd 7161f28255Scgd /* exceptions */ 7261f28255Scgd #define EXINT 0 /* SIGINT received */ 7361f28255Scgd #define EXERROR 1 /* a generic error */ 7461f28255Scgd #define EXSHELLPROC 2 /* execute a shell procedure */ 752a9c11ddSchristos #define EXEXEC 3 /* command execution failed */ 768a9a9619Skre #define EXEXIT 4 /* shell wants to exit(exitstatus) */ 7761f28255Scgd 7861f28255Scgd 7961f28255Scgd /* 8061f28255Scgd * These macros allow the user to suspend the handling of interrupt signals 8161f28255Scgd * over a period of time. This is similar to SIGHOLD to or sigblock, but 8261f28255Scgd * much more efficient and portable. (But hacking the kernel is so much 8361f28255Scgd * more fun than worrying about efficiency and portability. :-)) 8461f28255Scgd */ 8561f28255Scgd 8661f28255Scgd extern volatile int suppressint; 8761f28255Scgd extern volatile int intpending; 8861f28255Scgd 8961f28255Scgd #define INTOFF suppressint++ 9057731ef9Skre #define INTON do { if (--suppressint == 0 && intpending) onint(); } while (0) 9157731ef9Skre #define FORCEINTON do { suppressint = 0; if (intpending) onint(); } while (0) 9257731ef9Skre #define CLEAR_PENDING_INT (intpending = 0) 9361f28255Scgd #define int_pending() intpending 9461f28255Scgd 95cd127009Sapb #if ! defined(SHELL_BUILTIN) 9666dd2755Sjoerg void exraise(int) __dead; 97c02b3bbdSchristos void onint(void); 9866dd2755Sjoerg void error(const char *, ...) __dead __printflike(1, 2); 9966dd2755Sjoerg void exerror(int, const char *, ...) __dead __printflike(2, 3); 100c02b3bbdSchristos const char *errmsg(int, int); 101cd127009Sapb #endif /* ! SHELL_BUILTIN */ 102c02b3bbdSchristos 10366dd2755Sjoerg void sh_err(int, const char *, ...) __dead __printflike(2, 3); 10466dd2755Sjoerg void sh_verr(int, const char *, va_list) __dead __printflike(2, 0); 10566dd2755Sjoerg void sh_errx(int, const char *, ...) __dead __printflike(2, 3); 10666dd2755Sjoerg void sh_verrx(int, const char *, va_list) __dead __printflike(2, 0); 10766dd2755Sjoerg void sh_warn(const char *, ...) __printflike(1, 2); 10866dd2755Sjoerg void sh_vwarn(const char *, va_list) __printflike(1, 0); 10966dd2755Sjoerg void sh_warnx(const char *, ...) __printflike(1, 2); 11066dd2755Sjoerg void sh_vwarnx(const char *, va_list) __printflike(1, 0); 111c02b3bbdSchristos 11266dd2755Sjoerg void sh_exit(int) __dead; 11361f28255Scgd 11461f28255Scgd 11561f28255Scgd /* 11661f28255Scgd * BSD setjmp saves the signal mask, which violates ANSI C and takes time, 11791522b5bSkre * so we use sigsetjmp instead, and explicitly do not save it. 11891522b5bSkre * sh does a lot of setjmp() calls (fewer longjmp though). 11961f28255Scgd */ 12061f28255Scgd 12191522b5bSkre #define setjmp(jmploc) sigsetjmp((jmploc), 0) 12291522b5bSkre #define longjmp(jmploc, val) siglongjmp((jmploc), (val)) 123