aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

error.c (5106B)


      1 /* -
      2  * spdx-license-identifier: bsd-3-clause
      3  *
      4  * copyright (c) 1991, 1993
      5  * the regents of the university of california. 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 "error.h"
     40 #include "eval.h"
     41 #include "main.h"
     42 #include "nodes.h" /* show.h needs nodes.h */
     43 #include "options.h"
     44 #include "output.h"
     45 #include "shell.h"
     46 #include "show.h"
     47 #include "trap.h"
     48 #include <errno.h>
     49 #include <signal.h>
     50 #include <stdlib.h>
     51 #include <unistd.h>
     52 
     53 /*
     54  * code to handle exceptions in c
     55  */
     56 
     57 struct jmploc        *handler;
     58 volatile sig_atomic_t exception;
     59 volatile sig_atomic_t suppressint;
     60 volatile sig_atomic_t intpending;
     61 
     62 static void verrorwithstatus(int, const char *, va_list) __printf0like(2, 0) __dead2;
     63 
     64 /*
     65  * called to raise an exception. since c doesnt include exceptions, we
     66  * just do a longjmp to the exception handler. the type of exception is
     67  * stored in the global variable "exception"
     68  *
     69  * interrupts are disabled; they should be re-enabled when the exception is
     70  * caught
     71  */
     72 
     73 void
     74 exraise(int e)
     75 {
     76   INTOFF;
     77   if (handler == NULL)
     78     abort();
     79   exception = e;
     80   longjmp(handler->loc, 1);
     81 }
     82 
     83 /*
     84  * called from trap.c when a SIGINT is received and not suppressed, or when
     85  * an interrupt is pending and interrupts are re-enabled using INTON
     86  * (if the user specifies that SIGINT is to be trapped or ignored using the
     87  * trap builtin, then this routine is not called.) suppressint is nonzero
     88  * when interrupts are held using the INTOFF macro. if sigints are not
     89  * suppressed and the shell is not a root shell, then we want to be
     90  * terminated if we get here, as if we were terminated directly by a SIGINT
     91  * arrange for this here
     92  */
     93 
     94 void
     95 onint(void)
     96 {
     97   sigset_t sigs;
     98 
     99   intpending = 0;
    100   sigemptyset(&sigs);
    101   sigprocmask(SIG_SETMASK, &sigs, NULL);
    102 
    103   /*
    104    * this doesnt seem to be needed, since main() emits a newline
    105    */
    106 #if 0
    107 	if (tcgetpgrp(0) == getpid())
    108 		write(STDERR_FILENO, "\n", 1);
    109 #endif
    110   if (rootshell && iflag)
    111     exraise(EXINT);
    112   else {
    113     signal(SIGINT, SIG_DFL);
    114     kill(getpid(), SIGINT);
    115     _exit(128 + SIGINT);
    116   }
    117 }
    118 
    119 static void
    120 vwarning(const char *msg, va_list ap)
    121 {
    122   if (commandname)
    123     outfmt(out2, "%s: ", commandname);
    124   else if (arg0)
    125     outfmt(out2, "%s: ", arg0);
    126   doformat(out2, msg, ap);
    127   out2fmt_flush("\n");
    128 }
    129 
    130 void
    131 warning(const char *msg, ...)
    132 {
    133   va_list ap;
    134   va_start(ap, msg);
    135   vwarning(msg, ap);
    136   va_end(ap);
    137 }
    138 
    139 /*
    140  * exverror is called to raise the error exception. if the first argument
    141  * is not NULL then error prints an error message using printf style
    142  * formatting. it then raises the error exception
    143  */
    144 static void
    145 verrorwithstatus(int status, const char *msg, va_list ap)
    146 {
    147   /*
    148    * an interrupt trumps an error. certain places catch error
    149    * exceptions or transform them to a plain nonzero exit code
    150    * in child processes, and if an error exception can be handled,
    151    * an interrupt can be handled as well
    152    *
    153    * exraise() will disable interrupts for the exception handler
    154    */
    155   FORCEINTON;
    156 
    157 #ifdef DEBUG
    158   if (msg)
    159     TRACE(("verrorwithstatus(%d, \"%s\") pid=%d\n", status, msg, getpid()));
    160   else
    161     TRACE(("verrorwithstatus(%d, NULL) pid=%d\n", status, getpid()));
    162 #endif
    163   if (msg)
    164     vwarning(msg, ap);
    165   flushall();
    166   exitstatus = status;
    167   exraise(EXERROR);
    168 }
    169 
    170 void
    171 error(const char *msg, ...)
    172 {
    173   va_list ap;
    174   va_start(ap, msg);
    175   verrorwithstatus(2, msg, ap);
    176   va_end(ap);
    177 }
    178 
    179 void
    180 errorwithstatus(int status, const char *msg, ...)
    181 {
    182   va_list ap;
    183   va_start(ap, msg);
    184   verrorwithstatus(status, msg, ap);
    185   va_end(ap);
    186 }