[vox-tech] Where setjmp/longjmp went after C

Jeff Newmiller vox-tech@lists.lugod.org
Sat, 16 Mar 2002 13:25:59 -0800 (PST)


  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.
  Send mail to mime@docserver.cac.washington.edu for more info.

---1463789056-2126768675-1016313959=:4229
Content-Type: TEXT/PLAIN; charset=US-ASCII

The attached two files demonstrate a rudimentary "structured
setjmp/longjmp" that mimics the "exceptions" of Ada and C++ (and other
languages).  I wrote an article about this in spring 1991 for C User's
Journal, and my bad luck was that two other people beat me to the punch,
so it never got published.  What I have here is pulled from memory, but I
think it gets the idea across.

The real value of this technique comes into play when everyone uses the
same system... and you need language support to avoid certain problems
with auto variables... so I would only recommend using this if you are
stuck in C. :)

The basic idea is that you can detect certain problems deep in your
program, but if you want those pieces to be re-usable in, say, a GUI or
embedded environment, then you won't be able to report problems to the
user directly in those subroutines.  The mid- or hi level portions of the
program may already be tied into the user interface, so having them pass
on the problem to the user in an environmentally appropriate way makes
sense.  This technique is one way to propagate abnormal conditions out of
those re-usable routines.

Note that it should not be used in places where you expect to encounter
problems regularly... it is less efficient than simple tests on return
values, and you don't want all your program logic to depend on
exceptions... it gets hard to follow if you have a lot of kinds of
exceptions.  However, they are useful when you have unusual problems that
low-level routines cannot handle themselves.

j1.c is the demonstration source... jbexcept.h are the macros to be shared
throughout all source files, and j1_nomacro.c is the de-macroized j1.c so
you can follow things as they are implemented.  See the comment at the
beginning of j1.c for how to run it.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil@dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...2k
---------------------------------------------------------------------------

---1463789056-2126768675-1016313959=:4229
Content-Type: TEXT/x-csrc; name="j1_nomacro.c"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.21.0203161325591.4229@mirimichi.pvusa.localnet>
Content-Description: 
Content-Disposition: attachment; filename="j1_nomacro.c"

LyogajFfbm9tYWNyby5jIC0gZGVtb25zdHJhdGUgc2ltcGxpZmllZCBsb25n
am1wLCBtYWNyb3MgcmVtb3ZlZA0KICovDQoNCiNpbmNsdWRlIDxzdGRpby5o
Pg0KLyojaW5jbHVkZSAiamJleGNlcHQuaCIqLw0KDQpqYm5vZGUgKmpibm9k
ZV9oZWFkID0gKCh2b2lkICopMCkgOyAgDQoNCnZvaWQgZiggY2hhciBjICkN
CnsNCiAgcHJpbnRmKCAiaW4gZlxuIiApOw0KICBpZiAoICdmJyA9PSBjICkN
CiAgICBsb25nam1wKCBqYm5vZGVfaGVhZC0+dGhpcywgKCAxICkgKTsgOw0K
fQ0KDQp2b2lkIGcoIGNoYXIgYyApDQp7DQogIHByaW50ZiggImluIGdcbiIg
KTsNCiAgaWYgKCAnZycgPT0gYyApDQogICAgbG9uZ2ptcCggamJub2RlX2hl
YWQtPnRoaXMsICggMiApICk7IDsNCiAgaWYgKCAnaCcgPT0gYyApDQogICAg
bG9uZ2ptcCggamJub2RlX2hlYWQtPnRoaXMsKCAzICkgKTsgOw0KICBwcmlu
dGYoICJyZXR1cm4gbm9ybWFsbHkgZnJvbSBnXG4iICk7DQp9DQoNCnZvaWQg
YiggY2hhciB2b2xhdGlsZSBjICkNCnsNCiAgaW50IGNhdWdodD0wOw0KDQog
IHByaW50ZiggImVudGVyZWQgYiB3aXRoIGM9JWNcbiIsIGMgKTsNCiAgew0K
ICAgIGpibm9kZSBqYm5vZGVfbG9jYWw7DQoNCiAgICBqYm5vZGVfbG9jYWwu
bmV4dCA9IGpibm9kZV9oZWFkOw0KICAgIGpibm9kZV9oZWFkID0gJmpibm9k
ZV9sb2NhbDsNCiAgICBzd2l0Y2goICggKiggJmNhdWdodCAgKSApID0gIF9z
ZXRqbXAoIGpibm9kZV9oZWFkLT50aGlzICkgKSB7DQogICAgY2Fz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KICByZXR1cm4gMDsNCn0NCg0K
---1463789056-2126768675-1016313959=:4229
Content-Type: TEXT/x-csrc; name="j1.c"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.21.0203161325592.4229@mirimichi.pvusa.localnet>
Content-Description: 
Content-Disposition: attachment; filename="j1.c"

LyogajEuYyAtIGRlbW9uc3RyYXRlIHNpbXBsaWZpZWQgbG9uZ2ptcA0KIGNv
bXBpbGUgd2l0aCAtTzMgdG8gc2VlIHByb2JsZW0gd2l0aCAicmVmZXJlbmNl
ZCIgdmFyaWFibGUNCiBydW4gd2l0aCBzaW5nbGUgY2hhcmFjdGVyIGFyZ3Vt
ZW50cw0KICAgZiA6IG1hdGggZXJyb3IgaGFuZGxlZCBhdCBtaWRsZXZlbCwg
aGkgbGV2ZWwgZ2l2ZW4gYmV0dGVyIGV4cGxhbmF0aW9uDQogICBnIDogYmV0
dGVyIGV4cGxhbmF0aW9uIHBhc3NlZCBvbiBieSBtaWRsZXZlbA0KICAgaCA6
IHVuZXhwZWN0ZWQgcHJvYmxlbSBwYXNzZWQgb24gYnkgbWlkbGV2ZWwsIHVz
ZXIgd2FybmVkDQoNCiAqLw0KDQojaW5jbHVkZSA8c3RkaW8uaD4NCiNpbmNs
dWRlICJqYmV4Y2VwdC5oIg0KDQovKiBvbmUgZ2xvYmFsIHZhcmlhYmxlIHJl
cXVpcmVkICovDQpqYm5vZGUgKmpibm9kZV9oZWFkID0gTlVMTDsgLyogZ2xv
YmFsIGxpbmthZ2UgZGVmaW5pdGlvbiAqLw0KDQovKiBmIGFuZCBnIHdvdWxk
IGluIGdlbmVyYWwgbm90IGJlIHVzaW5nIHN0ZGlvIHRvIGNvbW11bmljYXRl
DQogICB0byB1c2VyICovDQp2b2lkIGYoIGNoYXIgYyApDQp7DQogIHByaW50
ZiggImluIGZcbiIgKTsNCiAgaWYgKCAnZicgPT0gYyApDQogICAgSkJfUkFJ
U0UoIEVYQ0VQX01BVEhFUlIgKTsNCn0NCg0Kd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pCX0VORFRSWTsNCiAgcHJpbnRmKCAiZW5kIG9mIHByb2dyYW1c
biIgKTsNCiAgcmV0dXJuIDA7DQp9DQoNCg==
---1463789056-2126768675-1016313959=:4229
Content-Type: TEXT/x-chdr; name="jbexcept.h"
Content-Transfer-Encoding: BASE64
Content-ID: <Pine.LNX.4.21.0203161325593.4229@mirimichi.pvusa.localnet>
Content-Description: 
Content-Disposition: attachment; filename="jbexcept.h"

LyogamJleGNlcHQuaCAtIHNpbXBsaWZpZWQgdXNlIG9mIHNldGptcC9sb25n
am1wDQogICBieSBKZWZmIE5ld21pbGxlcg0KICAgVGhpcyBpcyBub3QgYXMg
cm9idXN0IGFzIEMrKyBleGNlcHRpb25zIGFyZSwgYmVjYXVzZQ0KICAgYXV0
byB2YXJpYWJsZXMgb3B0aW1pemVkIGludG8gcmVnaXN0ZXJzIGFyZW4ndA0K
ICAgcmVzdG9yZWQgYWZ0ZXIgdGhlIGxvbmdqbXAvUkFJU0UuDQogKi8NCg0K
I2lmbmRlZiBKQkVYQ0VQVF9IDQojZGVmaW5lIEpCRVhDRVBUX0gNCg0KI2lu
Y2x1ZGUgPHNldGptcC5oPg0KDQp0eXBlZGVmIHN0cnVjdCBqYm5vZGVfdCB7
DQogIHN0cnVjdCBqYm5vZGVfdCAqbmV4dDsNCiAgam1wX2J1ZiB0aGlzOw0K
fSBqYm5vZGU7DQoNCmV4dGVybiBqYm5vZGUgKmpibm9kZV9oZWFkOw0KDQoj
ZGVmaW5lIEpCX0xOIGpibm9kZV9sb2NhbC5uZXh0PWpibm9kZV9oZWFkO2pi
bm9kZV9oZWFkPSZqYm5vZGVfbG9jYWw7DQojZGVmaW5lIEpCX1VMTiBqYm5v
ZGVfaGVhZD1qYm5vZGVfbG9jYWwubmV4dDsNCiNkZWZpbmUgSkJfVFJZKHYp
IHtqYm5vZGUgamJub2RlX2xvY2FsO0pCX0xOO3N3aXRjaCgoKih2KSk9c2V0
am1wKGpibm9kZV9oZWFkLT50aGlzKSl7Y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