Debian Codegraph @

gcc/m2/gm2-libs-log/Break.c

raw
1 /* Break.c - access to the signal handler for catching control C.
2
3 Copyright (C) 2004-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
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 <stdio.h>
28 #include <stdarg.h>
29 #include "gm2-libs-host.h"
30
31 #if defined(HAVE_STDLIB_H)
32 #include <stdlib.h>
33 #endif
34
35 #if defined(HAVE_MALLOC_H)
36 #include <malloc.h>
37 #endif
38
39 typedef void (*PROC) (void);
40
41 #if defined(HAVE_SIGNAL_H)
42 #include <signal.h>
43
44 struct plist
45 {
46 PROC proc;
47 struct plist *next;
48 };
49
50 static struct plist *head = NULL;
51
52 /* localHandler - dismisses the parameter, p, and invokes the GNU
53 Modula-2 handler. */
54
55 static void
56 localHandler (int p)
57 {
58 if (head != NULL)
59 head->proc ();
60 }
61
62 /* EnableBreak - enable the current break handler. */
63
64 void
65 Break_EnableBreak (void)
66 {
67 signal (SIGINT, localHandler);
68 }
69
70 /* DisableBreak - disable the current break handler (and all
71 installed handlers). */
72
73 void
74 Break_DisableBreak (void)
75 {
76 signal (SIGINT, SIG_IGN);
77 }
78
79 /* InstallBreak - installs a procedure, p, to be invoked when a
80 ctrl-c is caught. Any number of these procedures may be stacked.
81 Only the top procedure is run when ctrl-c is caught. */
82
83 void
84 Break_InstallBreak (PROC p)
85 {
86 struct plist *q = (struct plist *)malloc (sizeof (struct plist));
87
88 if (q == NULL)
89 {
90 perror ("out of memory error in module Break");
91 exit (1);
92 }
93 q->next = head;
94 head = q;
95 head->proc = p;
96 }
97
98 /* UnInstallBreak - pops the break handler stack. */
99
100 void
101 Break_UnInstallBreak (void)
102 {
103 struct plist *q = head;
104
105 if (head != NULL)
106 {
107 head = head->next;
108 free (q);
109 }
110 }
111 #else
112 void
113 Break_EnableBreak (void)
114 {
115 }
116 void
117 Break_DisableBreak (void)
118 {
119 }
120 void
121 Break_InstallBreak (PROC *p)
122 {
123 }
124 void
125 Break_UnInstallBreak (void)
126 {
127 }
128 #endif