1 @node Non-Local Exits, Signal Handling, Date and Time, Top
2 @c %MENU% Jumping out of nested function calls
3 @chapter Non-Local Exits
4 @cindex non-local exits
7 Sometimes when your program detects an unusual situation inside a deeply
8 nested set of function calls, you would like to be able to immediately
9 return to an outer level of control. This section describes how you can
10 do such @dfn{non-local exits} using the @code{setjmp} and @code{longjmp}
14 * Intro: Non-Local Intro. When and how to use these facilities.
15 * Details: Non-Local Details. Functions for nonlocal exits.
16 * Non-Local Exits and Signals:: Portability issues.
19 @node Non-Local Intro, Non-Local Details, , Non-Local Exits
20 @section Introduction to Non-Local Exits
22 As an example of a situation where a non-local exit can be useful,
23 suppose you have an interactive program that has a ``main loop'' that
24 prompts for and executes commands. Suppose the ``read'' command reads
25 input from a file, doing some lexical analysis and parsing of the input
26 while processing it. If a low-level input error is detected, it would
27 be useful to be able to return immediately to the ``main loop'' instead
28 of having to make each of the lexical analysis, parsing, and processing
29 phases all have to explicitly deal with error situations initially
30 detected by nested calls.
32 (On the other hand, if each of these phases has to do a substantial
33 amount of cleanup when it exits---such as closing files, deallocating
34 buffers or other data structures, and the like---then it can be more
35 appropriate to do a normal return and have each phase do its own
36 cleanup, because a non-local exit would bypass the intervening phases and
37 their associated cleanup code entirely. Alternatively, you could use a
38 non-local exit but do the cleanup explicitly either before or after
39 returning to the ``main loop''.)
41 In some ways, a non-local exit is similar to using the @samp{return}
42 statement to return from a function. But while @samp{return} abandons
43 only a single function call, transferring control back to the point at
44 which it was called, a non-local exit can potentially abandon many
45 levels of nested function calls.
47 You identify return points for non-local exits calling the function
48 @code{setjmp}. This function saves information about the execution
49 environment in which the call to @code{setjmp} appears in an object of
50 type @code{jmp_buf}. Execution of the program continues normally after
51 the call to @code{setjmp}, but if a exit is later made to this return
52 point by calling @code{longjmp} with the corresponding @w{@code{jmp_buf}}
53 object, control is transferred back to the point where @code{setjmp} was
54 called. The return value from @code{setjmp} is used to distinguish
55 between an ordinary return and a return made by a call to
56 @code{longjmp}, so calls to @code{setjmp} usually appear in an @samp{if}
59 Here is how the example program described above might be set up:
62 @include setjmp.c.texi
65 The function @code{abort_to_main_loop} causes an immediate transfer of
66 control back to the main loop of the program, no matter where it is
69 The flow of control inside the @code{main} function may appear a little
70 mysterious at first, but it is actually a common idiom with
71 @code{setjmp}. A normal call to @code{setjmp} returns zero, so the
72 ``else'' clause of the conditional is executed. If
73 @code{abort_to_main_loop} is called somewhere within the execution of
74 @code{do_command}, then it actually appears as if the @emph{same} call
75 to @code{setjmp} in @code{main} were returning a second time with a value
79 So, the general pattern for using @code{setjmp} looks something like:
82 if (setjmp (@var{buffer}))
83 /* @r{Code to clean up after premature return.} */
86 /* @r{Code to be executed normally after setting up the return point.} */
90 @node Non-Local Details, Non-Local Exits and Signals, Non-Local Intro, Non-Local Exits
91 @section Details of Non-Local Exits
93 Here are the details on the functions and data structures used for
94 performing non-local exits. These facilities are declared in
100 @deftp {Data Type} jmp_buf
101 Objects of type @code{jmp_buf} hold the state information to
102 be restored by a non-local exit. The contents of a @code{jmp_buf}
103 identify a specific place to return to.
108 @deftypefn Macro int setjmp (jmp_buf @var{state})
109 When called normally, @code{setjmp} stores information about the
110 execution state of the program in @var{state} and returns zero. If
111 @code{longjmp} is later used to perform a non-local exit to this
112 @var{state}, @code{setjmp} returns a nonzero value.
117 @deftypefun void longjmp (jmp_buf @var{state}, int @var{value})
118 This function restores current execution to the state saved in
119 @var{state}, and continues execution from the call to @code{setjmp} that
120 established that return point. Returning from @code{setjmp} by means of
121 @code{longjmp} returns the @var{value} argument that was passed to
122 @code{longjmp}, rather than @code{0}. (But if @var{value} is given as
123 @code{0}, @code{setjmp} returns @code{1}).@refill
126 There are a lot of obscure but important restrictions on the use of
127 @code{setjmp} and @code{longjmp}. Most of these restrictions are
128 present because non-local exits require a fair amount of magic on the
129 part of the C compiler and can interact with other parts of the language
132 The @code{setjmp} function is actually a macro without an actual
133 function definition, so you shouldn't try to @samp{#undef} it or take
134 its address. In addition, calls to @code{setjmp} are safe in only the
139 As the test expression of a selection or iteration
140 statement (such as @samp{if}, @samp{switch}, or @samp{while}).
143 As one operand of a equality or comparison operator that appears as the
144 test expression of a selection or iteration statement. The other
145 operand must be an integer constant expression.
148 As the operand of a unary @samp{!} operator, that appears as the
149 test expression of a selection or iteration statement.
152 By itself as an expression statement.
155 Return points are valid only during the dynamic extent of the function
156 that called @code{setjmp} to establish them. If you @code{longjmp} to
157 a return point that was established in a function that has already
158 returned, unpredictable and disastrous things are likely to happen.
160 You should use a nonzero @var{value} argument to @code{longjmp}. While
161 @code{longjmp} refuses to pass back a zero argument as the return value
162 from @code{setjmp}, this is intended as a safety net against accidental
163 misuse and is not really good programming style.
165 When you perform a non-local exit, accessible objects generally retain
166 whatever values they had at the time @code{longjmp} was called. The
167 exception is that the values of automatic variables local to the
168 function containing the @code{setjmp} call that have been changed since
169 the call to @code{setjmp} are indeterminate, unless you have declared
170 them @code{volatile}.
172 @node Non-Local Exits and Signals,, Non-Local Details, Non-Local Exits
173 @section Non-Local Exits and Signals
175 In BSD Unix systems, @code{setjmp} and @code{longjmp} also save and
176 restore the set of blocked signals; see @ref{Blocking Signals}. However,
177 the POSIX.1 standard requires @code{setjmp} and @code{longjmp} not to
178 change the set of blocked signals, and provides an additional pair of
179 functions (@code{sigsetjmp} and @code{sigsetjmp}) to get the BSD
182 The behavior of @code{setjmp} and @code{longjmp} in the GNU library is
183 controlled by feature test macros; see @ref{Feature Test Macros}. The
184 default in the GNU system is the POSIX.1 behavior rather than the BSD
187 The facilities in this section are declared in the header file
193 @deftp {Data Type} sigjmp_buf
194 This is similar to @code{jmp_buf}, except that it can also store state
195 information about the set of blocked signals.
200 @deftypefun int sigsetjmp (sigjmp_buf @var{state}, int @var{savesigs})
201 This is similar to @code{setjmp}. If @var{savesigs} is nonzero, the set
202 of blocked signals is saved in @var{state} and will be restored if a
203 @code{siglongjmp} is later performed with this @var{state}.
208 @deftypefun void siglongjmp (sigjmp_buf @var{state}, int @var{value})
209 This is similar to @code{longjmp} except for the type of its @var{state}
210 argument. If the @code{sigsetjmp} call that set this @var{state} used a
211 nonzero @var{savesigs} flag, @code{siglongjmp} also restores the set of