PDA

View Full Version : User interrupt



KawaGeo
18-06-2006, 07:38 PM
I was trying to convert everything in ANSI C to Pascal but I was stuck with signal functions. I don't know what equivalent to catch a control-C signal before exiting a console-based program. Anyone knows?

Thanks.

tanffn
19-06-2006, 05:52 AM
Edit: ..answered the wrong thing.. :oops:

Clootie
19-06-2006, 06:07 AM
Could you post C code doing Ctrl+C catching?

KawaGeo
19-06-2006, 04:40 PM
The C code is too long to post, 5 or 6 pages long. It is lua.c, Lua interpreter, which you could get from lua.org.

I have converted the code nearly completely to Pascal code. I had to comment out the 'signal' functions since I don't know what equivalent to replace them. I am planning to put up a project with the lua.c clone at LuaForge.org. I'll make an announcement as soon as the project gets off the grounds.

Thanks for your interest.

Clootie
19-06-2006, 07:18 PM
Hmm... It's an ANSI C RTL function: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_signal.asp

But:

Note: SIGINT is not supported for any Win32 application, including Windows 98/Me and Windows NT/2000/XP. When a CTRL+C interrupt occurs, Win32 operating systems generate a new thread to specifically handle that interrupt. This can cause a single-thread application such as UNIX, to become multithreaded, resulting in unexpected behavior.


Have you expereimented that actually happends when you press Ctrl+C in Delphi/FPC console program? Is it exits or returns some string as a Read[Ln]?

KawaGeo
19-06-2006, 07:38 PM
Thanks for the reply.

While running the clone (I named it 'LuaTut', short for Lua Tutor), the program merely exits and the command prompts once you press ctrl-C key. No message whatever.

I used Delphi to develop the console application for Windows XP platform.

Clootie
19-06-2006, 07:50 PM
Seems you has to assign your own error handling procedure procedure to ErrorProc variable.

If you include SysUtils in you app - it assigns it to:

{ RTL error handler }

type
TExceptRec = record
EClass: ExceptClass;
EIdent: string;
end;

const
ExceptMap: array[Ord(reDivByZero)..Ord(High(TRuntimeError))] of TExceptRec = (
(EClass: EDivByZero; EIdent: SDivByZero),
(EClass: ERangeError; EIdent: SRangeError),
(EClass: EIntOverflow; EIdent: SIntOverflow),
(EClass: EInvalidOp; EIdent: SInvalidOp),
(EClass: EZeroDivide; EIdent: SZeroDivide),
(EClass: EOverflow; EIdent: SOverflow),
(EClass: EUnderflow; EIdent: SUnderflow),
(EClass: EInvalidCast; EIdent: SInvalidCast),
(EClass: EAccessViolation; EIdent: SAccessViolationNoArg),
(EClass: EPrivilege; EIdent: SPrivilege),
(EClass: EControlC; EIdent: SControlC),
(EClass: EStackOverflow; EIdent: SStackOverflow),
(EClass: EVariantError; EIdent: SInvalidVarCast),
(EClass: EVariantError; EIdent: SInvalidVarOp),
(EClass: EVariantError; EIdent: SDispatchError),
(EClass: EVariantError; EIdent: SVarArrayCreate),
(EClass: EVariantError; EIdent: SVarInvalid),
(EClass: EVariantError; EIdent: SVarArrayBounds),
(EClass: EAssertionFailed; EIdent: SAssertionFailed),
(EClass: EExternalException; EIdent: SExternalException),
(EClass: EIntfCastError; EIdent: SIntfCastError),
(EClass: ESafecallException; EIdent: SSafecallException)
{$IFDEF LINUX}
,
(EClass: EQuit; EIdent: SQuit),
(EClass: ECodesetConversion; EIdent: SCodesetConversionError)
{$ENDIF}
);

procedure ErrorHandler(ErrorCode: Byte; ErrorAddr: Pointer); export;
var
E: Exception;
begin
case ErrorCode of
Ord(reOutOfMemory):
E := OutOfMemory;
Ord(reInvalidPtr):
E := InvalidPointer;
Ord(reDivByZero)..Ord(High(TRuntimeError)):
begin
with ExceptMap[ErrorCode] do
E := EClass.Create(EIdent);
end;
else
E := CreateInOutError;
end;
raise E at ErrorAddr;
end;

KawaGeo
21-06-2006, 07:29 PM
See the announcement at http://www.pascalgamedevelopment.com/forums/viewtopic.php?t=3370

You'll see the omissions in DoCall function in mainunit.pas which is part of LuaTut package. That's where ctrl-C was unable to catch. Hope you have a suggestion to replace the ommision.

Thank you for your time.