summaryrefslogtreecommitdiff
path: root/unwind_prot.c
blob: 60e911dd3fdf303eb02d1375187749d8e1d3e24f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* I can't stand it anymore!  Please can't we just write the
   whole Unix system in lisp or something? */

/* Copyright (C) 1987,1989 Free Software Foundation, Inc.

This file is part of GNU Bash, the Bourne Again SHell.

Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING.  If not, write to the Free Software
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */

/* **************************************************************** */
/*								    */
/*		      Unwind Protection Scheme for Bash		    */
/*								    */
/* **************************************************************** */
#include "config.h"

#include "bashtypes.h"
#include "bashansi.h"

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#include "command.h"
#include "general.h"
#include "unwind_prot.h"
#include "quit.h"
#include "sig.h"

/* If CLEANUP is null, then ARG contains a tag to throw back to. */
typedef struct _uwp {
  struct _uwp *next;
  Function *cleanup;
  char *arg;
} UNWIND_ELT;

/* Structure describing a saved variable and the value to restore it to.
   If a cleanup function is set to restore_variable, the `arg' pointer
   points to this. */
typedef struct {
  int *variable;
  char *desired_setting;
  int size;
} SAVED_VAR;

static void unwind_frame_discard_internal (), unwind_frame_run_internal ();
static void add_unwind_protect_internal (), remove_unwind_protect_internal ();
static void run_unwind_protects_internal (), without_interrupts ();

static void restore_variable ();
static void discard_saved_var ();

static UNWIND_ELT *unwind_protect_list = (UNWIND_ELT *)NULL;

extern int interrupt_immediately;

/* Run a function without interrupts.  This relies on the fact that the
   FUNCTION cannot change the value of interrupt_immediately.  (I.e., does
   not call QUIT (). */
static void
without_interrupts (function, arg1, arg2)
     VFunction *function;
     char *arg1, *arg2;
{
  int old_interrupt_immediately;

  old_interrupt_immediately = interrupt_immediately;
  interrupt_immediately = 0;

  (*function)(arg1, arg2);

  interrupt_immediately = old_interrupt_immediately;
}

/* Start the beginning of a region. */
void
begin_unwind_frame (tag)
     char *tag;
{
  add_unwind_protect ((Function *)NULL, tag);
}

/* Discard the unwind protects back to TAG. */
void
discard_unwind_frame (tag)
     char *tag;
{
  if (unwind_protect_list)
    without_interrupts (unwind_frame_discard_internal, tag, (char *)NULL);
}

/* Run the unwind protects back to TAG. */
void
run_unwind_frame (tag)
     char *tag;
{
  if (unwind_protect_list)
    without_interrupts (unwind_frame_run_internal, tag, (char *)NULL);
}

/* Add the function CLEANUP with ARG to the list of unwindable things. */
void
add_unwind_protect (cleanup, arg)
     Function *cleanup;
     char *arg;
{
  without_interrupts (add_unwind_protect_internal, (char *)cleanup, arg);
}

/* Remove the top unwind protect from the list. */
void
remove_unwind_protect ()
{
  if (unwind_protect_list)
    without_interrupts
      (remove_unwind_protect_internal, (char *)NULL, (char *)NULL);
}

/* Run the list of cleanup functions in unwind_protect_list. */
void
run_unwind_protects ()
{
  if (unwind_protect_list)
    without_interrupts
      (run_unwind_protects_internal, (char *)NULL, (char *)NULL);
}

/* **************************************************************** */
/*								    */
/*                        The Actual Functions                 	    */
/*								    */
/* **************************************************************** */

static void
add_unwind_protect_internal (cleanup, arg)
     Function *cleanup;
     char *arg;
{
  UNWIND_ELT *elt;

  elt = (UNWIND_ELT *)xmalloc (sizeof (UNWIND_ELT));
  elt->cleanup = cleanup;
  elt->arg = arg;
  elt->next = unwind_protect_list;
  unwind_protect_list = elt;
}

static void
remove_unwind_protect_internal ()
{
  UNWIND_ELT *elt;

  elt = unwind_protect_list;
  if (elt)
    {
      unwind_protect_list = unwind_protect_list->next;
      if (elt->cleanup && elt->cleanup == (Function *)restore_variable)
	discard_saved_var ((SAVED_VAR *)elt->arg);
      free (elt);
    }
}

static void
run_unwind_protects_internal ()
{
  UNWIND_ELT *t, *elt = unwind_protect_list;

  while (elt)
   {
      /* This function can be run at strange times, like when unwinding
	 the entire world of unwind protects.  Thus, we may come across
	 an element which is simply a label for a catch frame.  Don't call
	 the non-existant function. */
      if (elt->cleanup)
	(*(elt->cleanup)) (elt->arg);

      t = elt;
      elt = elt->next;
      free (t);
    }
  unwind_protect_list = elt;
}

static void
unwind_frame_discard_internal (tag)
     char *tag;
{
  UNWIND_ELT *elt;

  while (elt = unwind_protect_list)
    {
      unwind_protect_list = unwind_protect_list->next;
      if (elt->cleanup == 0 && (STREQ (elt->arg, tag)))
	{
	  free (elt);
	  break;
	}
      else if (elt->cleanup && elt->cleanup == (Function *)restore_variable)
        {
          discard_saved_var ((SAVED_VAR *)elt->arg);
          free (elt);
        }
      else
	free (elt);
    }
}

static void
unwind_frame_run_internal (tag)
     char *tag;
{
  UNWIND_ELT *elt;

  while (elt = unwind_protect_list)
    {
      unwind_protect_list = elt->next;

      /* If tag, then compare. */
      if (!elt->cleanup)
	{
	  if (STREQ (elt->arg, tag))
	    {
	      free (elt);
	      break;
	    }
	  free (elt);
	  continue;
	}
      else
	{
	  (*(elt->cleanup)) (elt->arg);
	  free (elt);
	}
    }
}

static void
discard_saved_var (sv)
     SAVED_VAR *sv;
{
  if (sv->size != sizeof (int))
    free (sv->desired_setting);
  free (sv);
}

/* Restore the value of a variable, based on the contents of SV.  If
   sv->size is greater than sizeof (int), sv->desired_setting points to
   a block of memory SIZE bytes long holding the value, rather than the
   value itself.  This block of memory is copied back into the variable. */
static void
restore_variable (sv)
     SAVED_VAR *sv;
{
  if (sv->size != sizeof (int))
    {
      FASTCOPY ((char *)sv->desired_setting, (char *)sv->variable, sv->size);
      free (sv->desired_setting);
    }
  else
    *(sv->variable) = (int)sv->desired_setting;

  free (sv);
}

/* Save the value of a variable so it will be restored when unwind-protects
   are run.  VAR is a pointer to the variable.  VALUE is the value to be
   saved.  SIZE is the size in bytes of VALUE.  If SIZE is bigger than what
   can be saved in an int, memory will be allocated and the value saved
   into that using bcopy (). */
void
unwind_protect_var (var, value, size)
     int *var;
     char *value;
     int size;
{
  SAVED_VAR *s = (SAVED_VAR *)xmalloc (sizeof (SAVED_VAR));

  s->variable = var;
  if (size != sizeof (int))
    {
      /* There is a problem here when VALUE is 0.  This tries to copy the
	  first SIZE bytes starting at memory location 0 into
	  s->desired_setting.  There is no guarantee that these bytes are
	  0, or make a valid null pointer.  We can try to bzero the space,
	  or just save it as 0 (or (void *)0).  If we do the latter, make
	  sure restore_variable is changed to understand it. */
      s->desired_setting = (char *)xmalloc (size);
      if (value == 0)
	bzero ((char *)s->desired_setting, size);
      else
	FASTCOPY (value, (char *)s->desired_setting, size);
    }
  else
    s->desired_setting = value;
  s->size = size;
  add_unwind_protect ((Function *)restore_variable, (char *)s);
}