Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!iuvax!pur-ee!pur-phy!ng From: ng@pur-phy (Nicholas J. Giordano) Newsgroups: comp.sys.amiga.tech Subject: Need help with exception handling Message-ID: <1360@pur-phy> Date: 2 Sep 88 17:02:18 GMT References: <8808311759.AA19249@cory.Berkeley.EDU> <2582@sugar.uu.net> <4625@cbmvax.UUCP> Reply-To: ng@newton.physics.purdue.edu.UUCP (Nicholas J. Giordano) Organization: Purdue Univ. Physics Dept., W. Lafayette, IN Lines: 108 About a month ago there was a discussion of how to catch signals like ^C, etc. An example program was posted to the net (see below), and people seemed to agree that it was the right way to go about things, although one must be careful not too take control away from certain system routines, without letting them finish their work. Well, I was playing with this last night, and I ran into a problem which I don't understand. The program below catches ^C, ^D, ^F, and ^E, prints a little message, and then returns. If a ^C was typed, a global variable is set, and the program finishes. There is a busy loop in which nothing is done [while {}] for illustrative purposes, and the program works fine when this loop is empty. However, when I added some code inside this loop, the program crashes (gurus). The code I added was a simple printf, and is indicated below. Can somebody tell me why there is a guru, and what I need to do to get rid of it?? Thanks. Nick NOTE: I have removed some of the comments from the original program. Also, this is compiled with Manx 3.6a with 16 bit ints. /* An illustration of how to write an Exception Handler */ /* Bill Kinnersley - IPHWK@MTSUNIX1.BITNET - 7/23/88 */ /* Compiled using Aztec 3.4a, 16-bit integers. */ /* ^D, ^E, ^F will print a message, ^C will abort. */ #include #include #include #include #include extern short Enable_Abort; long aborted, cursigs, mysigs, oldsigs; struct Task *mytask; main() { int i = 0; install(); Enable_Abort = 0; /* Busy loop simulating something useful... */ /* Note that each Exception DOES break into this loop. */ /* The aborted flag is polled only because */ /* I want to generate a clean exit from main(), */ /* not try to call cleanup from inside the handler. */ while (!aborted) { printf("%d\n",++i); <----------- Adding this brings the guru } cleanup(); } APTR oldcode; install() { int face(); mysigs = SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E | SIGBREAKF_CTRL_F; mytask = FindTask(0L); Forbid(); oldcode = mytask->tc_ExceptCode; mytask->tc_ExceptCode = (APTR)face; Permit(); oldsigs = SetExcept(mysigs, mysigs); } cleanup() { /* Restore the previous Exception Handler (ROM-wack?) */ Forbid(); mytask->tc_ExceptCode = oldcode; Permit(); SetExcept(0L,mysigs); } #asm _face: ;the signals will be passed to me in d0 move.l d0,-(a7) ;restore a4, which points to Aztec's small data segment */ public _geta4 jsr _geta4 move.l (a7)+,_cursigs jsr _hdlr ;return signals to the system so they can be reset move.l _cursigs,d0 rts #endasm hdlr() { /* Note that even AmigaDOS stuff can be done from here */ printf("Exception %lx Control-",cursigs); if (cursigs & SIGBREAKF_CTRL_C) {printf("C\n"); aborted=1;} if (cursigs & SIGBREAKF_CTRL_D) printf("D\n"); if (cursigs & SIGBREAKF_CTRL_E) printf("E\n"); if (cursigs & SIGBREAKF_CTRL_F) printf("F\n"); }