466
|
1 <refentry id="longjmp">
|
|
2 <refnamediv>
|
568
|
3 <refname>Longjmp</refname>
|
|
4 <refname>Setjmp</refname>
|
|
5 <refpurpose>jump to another function</refpurpose>
|
466
|
6 </refnamediv>
|
|
7
|
|
8 <refsynopsisdiv>
|
|
9 <funcsynopsis>
|
468
|
10 <funcsynopsisinfo>
|
|
11 #include <setjmp.h>
|
|
12 </funcsynopsisinfo>
|
466
|
13 <funcprototype>
|
568
|
14 <funcdef><function>setjmp</function></funcdef>
|
|
15 <paramdef>jmp_buf <parameter>env</parameter></paramdef>
|
|
16 </funcprototype>
|
|
17
|
|
18 <funcprototype>
|
466
|
19 <funcdef><function>longjmp</function></funcdef>
|
568
|
20 <paramdef>jmp_buf <parameter>env</parameter></paramdef>
|
|
21 <paramdef>int <parameter>val</parameter></paramdef>
|
466
|
22 </funcprototype>
|
|
23 </funcsynopsis>
|
|
24
|
|
25 </refsynopsisdiv>
|
|
26
|
|
27 <refsect1><title>Description</title>
|
|
28 <para>
|
568
|
29 These functions allow the return of program control directly to
|
|
30 a higher level function. They are most useful when dealing
|
|
31 with errors and interrupts encountered in a low level routine.
|
|
32 </para>
|
|
33 <para>
|
|
34 "Goto" in C has scope only in the function in which it is
|
|
35 used; i.e. the label which is the object of a "goto" may only
|
|
36 be in the same function. Control can only be transferred
|
|
37 elsewhere by means of the function call, which, of course
|
|
38 returns to the caller. In certain abnormal situations a
|
|
39 programmer would prefer to be able to start some section of
|
|
40 code again, but this would mean returning up a ladder of
|
|
41 function calls with error indications all the way.
|
|
42 </para>
|
|
43 <para>
|
|
44 Setjmp is used to "mark" a point in the program where a
|
|
45 subsequent longjmp can reach. It places in the buffer, defined
|
|
46 in the header file, enough information for longjmp to restore
|
|
47 the environment to that existing at the relevant call to
|
|
48 setjmp.
|
|
49 </para>
|
|
50 <para>
|
|
51 Longjmp is called with the environment buffer as an argument
|
|
52 and also, a value which can be used by the caller of setjmp as,
|
|
53 perhaps, an error status.
|
|
54 </para>
|
|
55 <para>
|
|
56 To set the system up, a function will call setjmp to set up the
|
|
57 buffer, and if the returned value is zero, the program will
|
|
58 know that the call was the "first time through". If, however,
|
|
59 the returned value is non-zero, it must be a longjmp returning
|
|
60 from some deeper level of the program.
|
|
61 </para>
|
|
62 <para>
|
|
63 NOTE that the function calling setjmp must <emphasis>not have returned</emphasis>
|
|
64 at the time of calling longjmp, and the environment buffer must
|
|
65 be declared <emphasis>globally</emphasis>.
|
466
|
66 </para>
|
|
67 </refsect1>
|
|
68 </refentry>
|