2
0

catch.pas 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. {
  2. Copyright (c) 1998-2002 by Michael Van Canneyt
  3. Unit to catch segmentation faults and Ctrl-C and exit gracefully
  4. under linux and go32v2
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *********************************************************************
  17. }
  18. Unit catch;
  19. {$i fpcdefs.inc}
  20. {$ifdef DEBUG}
  21. {$define NOCATCH}
  22. {$endif DEBUG}
  23. interface
  24. uses
  25. { you cannot safely raise an exception inside a signal handler on any OS,
  26. and on darwin this even often crashes
  27. }
  28. {$if defined(unix) and not defined(darwin) }
  29. {$ifndef darwin}
  30. {$define has_signal}
  31. BaseUnix,Unix,
  32. {$endif}
  33. {$endif}
  34. {$ifdef go32v2}
  35. {$define has_signal}
  36. dpmiexcp,
  37. {$endif}
  38. {$ifdef watcom}
  39. {$define has_signal}
  40. dpmiexcp,
  41. {$endif}
  42. verbose;
  43. {$ifdef has_signal}
  44. Var
  45. NewSignal,
  46. OldSigInt : SignalHandler;
  47. {$endif}
  48. Implementation
  49. uses
  50. comphook;
  51. {$ifdef has_signal}
  52. {$ifdef unix}
  53. Procedure CatchSignal(Sig : Longint);cdecl;
  54. {$else}
  55. Function CatchSignal(Sig : longint):longint; cdecl;
  56. {$endif}
  57. begin
  58. case Sig of
  59. SIGINT :
  60. raise EControlCAbort.Create;
  61. end;
  62. {$ifndef unix}
  63. CatchSignal:=0;
  64. {$endif}
  65. end;
  66. {$endif def has_signal}
  67. begin
  68. {$ifndef nocatch}
  69. {$ifdef has_signal}
  70. NewSignal:=SignalHandler(@CatchSignal);
  71. OldSigInt:={$ifdef Unix}fpSignal{$else}Signal{$endif}(SIGINT,NewSignal);
  72. {$endif}
  73. {$endif nocatch}
  74. end.