Re: [cc65] Re: signals

From: dominic beesley <dominic1brahms.demon.co.uk>
Date: 2005-05-31 13:41:09
Jonathan Graham Harston wrote:

>>Message-ID: <4294A998.2010908@brahms.demon.co.uk>
>>    
>>
> 
>dominic beesley <dominic@brahms.demon.co.uk> wrote:
>  
>
>>Some quick questions on signals, trying to get escape and
>>error handling working for BBC...
>>    
>>
>[snip]
>  
>
>>I want to override _sig_dfl for BBC micro so that it traps
>>SIGINT and aborts to the OS when Escape is pressed and not
>>captured. What is the best way of doing this? I don't want to move
>>    
>>
> 
>The best way is to not do this. The default action of Escape is to
>set a flag that the foreground program checks. The background (ie,
>the escape) should not bomb out on the foreground's behalf. The
>foreground may be halfway through a disk write!
> 
>The foreground has to explicity check the escape flag if it wants
>to act upon it. BASIC does this between every statement. A BASIC
>statement itself is uninterruptable. Try:
> 
>A$=STRING$(255,"*")
>PRINT A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$;A$
> 
>and you will see that you cannot Escape from the PRINT statement,
>only prior to it and after it.
> 
>A 'C' program compiles to a chunk of machine code, essentially
>what a single BASIC statement is. Effectively, BASIC's ':' and
>'eol' are effectively a <if escape, generate error> command. If
>you do not explicity do that yourself in any program you write,
>Escape is going to be ignored.
> 
>Consider the machine code pseudo-code: [1]
> 
>.label
>print "hello"
>goto labal
> 
>This will go on forever until a reset. Compare this with: [2]
> 
>.label
>print "hello"
>check escape
>if notset goto lebal
>ret
> 
>This will loop as long as the escape state is not set. When escape
>is set, the loop terminates.
> 
>BASIC checks the Escape flag and generates an "Escape" error. VIEW
>checks the Escape flag and toggles between Command and Editing.
>Repton checks the Escape flags and kills the current life.
> 
>If you want to change away from the default action of escape, you
>should do some other /background/ action, such as setting a 'C'
>program variable. Something like:
> 
>...
>signal(_sig_esc,my_esc());
>...
> 
>my_esc()
>{ a_global_variable=1; }
>  
>
This is now doable with the signal library.

> 
>Remember, in 'C', you're effectively writing machine code, so
>*you* the programmer have to do the background checking that a
>higher-level language does for you. I would recommend you provide
>some library functions to check for and acknowledge Escape, so
>that you can do something like:
> 
>for(;;) {
>  printf("Hello\n");
>  if(os_checkescape()) {
>    os_ackescape();
>    break;
>    }
>  }
>  
>
I've written C programs for about 15 years and probably nearly as many
platforms I've never had to do anything like that there's always been some
key or combination of (CTRL-C, DEL, ALT-BKSP, RST, funny
squiggle that nobody new the name for but we called 'arse') that has been
there to break out of program flow. This doesn't require explicit code
unless you want it to be ignored.

> 
>signalling a BRK (ie, a BBC error) is allowed to change the
>program flow, as a BRK on the BBC is a foreground action, the
>equivalent of BASIC's ON ERROR. With the appropriate code, you
>should be able to do something like this:
> 
>main()
>{
>signal(_sig_brk, myerror());
>for(;;) {
>  printf("Hello\n");
>  if(os_checkescape()) { os_error(17, "Escape"); }
>  }
>}
> 
>myerror()
>{
>os_report();
>main();
>}
> 
>Remember, that the code that processes BRK must reset the
>language's stacks, otherwise each BRK pushes the program further
>and further into the stack.
>  
>
If its a signal handler it will yes, but then again you should return. 
Its easy
to exhaust the stack on quite a few platorms by not returning from signal
DOS used to be a swine for this as I recall.

Also on many you've got to be careful about what you expect to be safe;
structures may be half updated, variables not correctly set (although this
is probably worse in 6502 as even a simple increment on a 16bit no. will
take a number of instructions).

> 
>JGH
> 
>[1]
>.label
>ldx #0
>.loop
>lda hello,x:jsr osasci:inx:cmp #13:bne loop
>jmp label
>.hello
>equs "Hello":equb 13
> 
>[2]
>.label
>ldx #0
>.loop
>lda hello,x:jsr osasci:inx:cmp #13:bne loop
>bit &FF:bpl label  ; loop back if Escape not set
>rts
>.hello
>equs "Hello":equb 13
> 
>  
>
I do take on board what you are saying but it seems to me that if no Escape
handler is provided and the code is in a long loop the user may well just
press Escape, get bored then press CTRL-Break. With consequences for
files etc that are slightly worse.

Dom
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Tue May 31 13:52:08 2005

This archive was generated by hypermail 2.1.8 : 2005-05-31 13:52:10 CEST