(root)/
gcc-13.2.0/
libgm2/
libm2log/
Break.c
       1  /* Break.c implements an interrupt handler for SIGINT.
       2  
       3  Copyright (C) 2004-2022 Free Software Foundation, Inc.
       4  Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
       5  
       6  This file is part of GNU Modula-2.
       7  
       8  GNU Modula-2 is free software; you can redistribute it and/or modify
       9  it under the terms of the GNU General Public License as published by
      10  the Free Software Foundation; either version 3, or (at your option)
      11  any later version.
      12  
      13  GNU Modula-2 is distributed in the hope that it will be useful, but
      14  WITHOUT ANY WARRANTY; without even the implied warranty of
      15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16  General Public License for more details.
      17  
      18  Under Section 7 of GPL version 3, you are granted additional
      19  permissions described in the GCC Runtime Library Exception, version
      20  3.1, as published by the Free Software Foundation.
      21  
      22  You should have received a copy of the GNU General Public License and
      23  a copy of the GCC Runtime Library Exception along with this program;
      24  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      25  <http://www.gnu.org/licenses/>.  */
      26  
      27  #include <config.h>
      28  
      29  #if defined(HAVE_STDIO_H)
      30  #include <stdio.h>
      31  #endif
      32  
      33  #if defined(HAVE_STDARG_H)
      34  #include <stdarg.h>
      35  #endif
      36  
      37  #if defined(HAVE_STDLIB_H)
      38  #include <stdlib.h>
      39  #endif
      40  
      41  #if defined(HAVE_MALLOC_H)
      42  #include <malloc.h>
      43  #endif
      44  
      45  typedef void (*PROC) (void);
      46  
      47  #if defined(HAVE_SIGNAL_H)
      48  #include <signal.h>
      49  
      50  struct plist
      51  {
      52    PROC proc;
      53    struct plist *next;
      54  };
      55  
      56  static struct plist *head = NULL;
      57  
      58  /* localHandler - dismisses the parameter, p, and invokes the GNU
      59     Modula-2 handler.  */
      60  
      61  static void
      62  localHandler (int p)
      63  {
      64    if (head != NULL)
      65      head->proc ();
      66  }
      67  
      68  /* EnableBreak - enable the current break handler.  */
      69  
      70  void
      71  Break_EnableBreak (void)
      72  {
      73    signal (SIGINT, localHandler);
      74  }
      75  
      76  /* DisableBreak - disable the current break handler (and all
      77     installed handlers).  */
      78  
      79  void
      80  Break_DisableBreak (void)
      81  {
      82    signal (SIGINT, SIG_IGN);
      83  }
      84  
      85  /* InstallBreak - installs a procedure, p, to be invoked when a
      86     ctrl-c is caught.  Any number of these procedures may be stacked.
      87     Only the top procedure is run when ctrl-c is caught.  */
      88  
      89  void
      90  Break_InstallBreak (PROC p)
      91  {
      92    struct plist *q = (struct plist *)malloc (sizeof (struct plist));
      93  
      94    if (q == NULL)
      95      {
      96        perror ("out of memory error in module Break");
      97        exit (1);
      98      }
      99    q->next = head;
     100    head = q;
     101    head->proc = p;
     102  }
     103  
     104  /* UnInstallBreak - pops the break handler stack.  */
     105  
     106  void
     107  Break_UnInstallBreak (void)
     108  {
     109    struct plist *q = head;
     110  
     111    if (head != NULL)
     112      {
     113        head = head->next;
     114        free (q);
     115      }
     116  }
     117  #else
     118  void
     119  Break_EnableBreak (void)
     120  {
     121  }
     122  void
     123  Break_DisableBreak (void)
     124  {
     125  }
     126  void
     127  Break_InstallBreak (PROC *p)
     128  {
     129  }
     130  void
     131  Break_UnInstallBreak (void)
     132  {
     133  }
     134  #endif