1 /* I can't stand it anymore! Please can't we just write the
2 whole Unix system in lisp or something? */
4 /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 /* **************************************************************** */
24 /* Unwind Protection Scheme for Bash */
26 /* **************************************************************** */
29 #include "bashtypes.h"
32 #if defined (HAVE_UNISTD_H)
41 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
46 #include "unwind_prot.h"
50 /* Structure describing a saved variable and the value to restore it to. */
54 char desired_setting[1]; /* actual size is `size' */
57 /* If HEAD.CLEANUP is null, then ARG.V contains a tag to throw back to.
58 If HEAD.CLEANUP is restore_variable, then SV.V contains the saved
59 variable. Otherwise, call HEAD.CLEANUP (ARG.V) to clean up. */
66 struct uwp_head uwp_head;
70 struct uwp_head uwp_head;
76 static void without_interrupts __P((VFunction *, char *, char *));
77 static void unwind_frame_discard_internal __P((char *, char *));
78 static void unwind_frame_run_internal __P((char *, char *));
79 static void add_unwind_protect_internal __P((Function *, char *));
80 static void remove_unwind_protect_internal __P((char *, char *));
81 static void run_unwind_protects_internal __P((char *, char *));
82 static void clear_unwind_protects_internal __P((char *, char *));
83 static inline void restore_variable __P((SAVED_VAR *));
84 static void unwind_protect_mem_internal __P((char *, char *));
86 static UNWIND_ELT *unwind_protect_list = (UNWIND_ELT *)NULL;
88 #define uwpalloc(elt) (elt) = (UNWIND_ELT *)xmalloc (sizeof (UNWIND_ELT))
89 #define uwpfree(elt) free(elt)
91 /* Run a function without interrupts. This relies on the fact that the
92 FUNCTION cannot change the value of interrupt_immediately. (I.e., does
95 without_interrupts (function, arg1, arg2)
99 int old_interrupt_immediately;
101 old_interrupt_immediately = interrupt_immediately;
102 interrupt_immediately = 0;
104 (*function)(arg1, arg2);
106 interrupt_immediately = old_interrupt_immediately;
109 /* Start the beginning of a region. */
111 begin_unwind_frame (tag)
114 add_unwind_protect ((Function *)NULL, tag);
117 /* Discard the unwind protects back to TAG. */
119 discard_unwind_frame (tag)
122 if (unwind_protect_list)
123 without_interrupts (unwind_frame_discard_internal, tag, (char *)NULL);
126 /* Run the unwind protects back to TAG. */
128 run_unwind_frame (tag)
131 if (unwind_protect_list)
132 without_interrupts (unwind_frame_run_internal, tag, (char *)NULL);
135 /* Add the function CLEANUP with ARG to the list of unwindable things. */
137 add_unwind_protect (cleanup, arg)
141 without_interrupts (add_unwind_protect_internal, (char *)cleanup, arg);
144 /* Remove the top unwind protect from the list. */
146 remove_unwind_protect ()
148 if (unwind_protect_list)
150 (remove_unwind_protect_internal, (char *)NULL, (char *)NULL);
153 /* Run the list of cleanup functions in unwind_protect_list. */
155 run_unwind_protects ()
157 if (unwind_protect_list)
159 (run_unwind_protects_internal, (char *)NULL, (char *)NULL);
162 /* Erase the unwind-protect list. If flags is 1, free the elements. */
164 clear_unwind_protect_list (flags)
169 if (unwind_protect_list)
171 flag = flags ? "" : (char *)NULL;
173 (clear_unwind_protects_internal, flag, (char *)NULL);
177 /* **************************************************************** */
179 /* The Actual Functions */
181 /* **************************************************************** */
184 add_unwind_protect_internal (cleanup, arg)
191 elt->head.next = unwind_protect_list;
192 elt->head.cleanup = cleanup;
194 unwind_protect_list = elt;
198 remove_unwind_protect_internal (ignore1, ignore2)
199 char *ignore1, *ignore2;
203 elt = unwind_protect_list;
206 unwind_protect_list = unwind_protect_list->head.next;
212 run_unwind_protects_internal (ignore1, ignore2)
213 char *ignore1, *ignore2;
215 unwind_frame_run_internal ((char *) NULL, (char *) NULL);
219 clear_unwind_protects_internal (flag, ignore)
224 while (unwind_protect_list)
225 remove_unwind_protect_internal ((char *)NULL, (char *)NULL);
227 unwind_protect_list = (UNWIND_ELT *)NULL;
231 unwind_frame_discard_internal (tag, ignore)
236 while (elt = unwind_protect_list)
238 unwind_protect_list = unwind_protect_list->head.next;
239 if (elt->head.cleanup == 0 && (STREQ (elt->arg.v, tag)))
249 /* Restore the value of a variable, based on the contents of SV.
250 sv->desired_setting is a block of memory SIZE bytes long holding the
251 value itself. This block of memory is copied back into the variable. */
253 restore_variable (sv)
256 FASTCOPY (sv->desired_setting, sv->variable, sv->size);
260 unwind_frame_run_internal (tag, ignore)
265 while (elt = unwind_protect_list)
267 unwind_protect_list = elt->head.next;
269 /* If tag, then compare. */
270 if (!elt->head.cleanup)
272 if (tag && STREQ (elt->arg.v, tag))
280 if (elt->head.cleanup == (Function *) restore_variable)
281 restore_variable (&elt->sv.v);
283 (*(elt->head.cleanup)) (elt->arg.v);
291 unwind_protect_mem_internal (var, psize)
298 size = *(int *) psize;
299 allocated = size + offsetof (UNWIND_ELT, sv.v.desired_setting[0]);
300 elt = (UNWIND_ELT *)xmalloc (allocated);
301 elt->head.next = unwind_protect_list;
302 elt->head.cleanup = (Function *) restore_variable;
303 elt->sv.v.variable = var;
304 elt->sv.v.size = size;
305 FASTCOPY (var, elt->sv.v.desired_setting, size);
306 unwind_protect_list = elt;
309 /* Save the value of a variable so it will be restored when unwind-protects
310 are run. VAR is a pointer to the variable. SIZE is the size in
313 unwind_protect_mem (var, size)
317 without_interrupts (unwind_protect_mem_internal, var, (char *) &size);