Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.5 KiB

  1. /*++
  2. Copyright (c) 1988-1999 Microsoft Corporation
  3. Module Name:
  4. csig.c
  5. Abstract:
  6. Interrupt (^C) processing
  7. --*/
  8. #include "cmd.h"
  9. //
  10. // console mode at program startup time. Used to reset mode
  11. // after running another process.
  12. //
  13. extern DWORD dwCurInputConMode;
  14. extern DWORD dwCurOutputConMode;
  15. extern int Ctrlc;
  16. VOID ResetCtrlC();
  17. int SigHandFlag = FALSE ;
  18. /* Commands that temporarily change directories, save a ptr to the original
  19. * directory string here so that it can be restored by SigHand() if the
  20. * command is interrupted before it has a chance to do it, itself.
  21. */
  22. TCHAR *SaveDir = NULL ;
  23. unsigned SIGNALcnt = 0;
  24. extern int PipeCnt ; /* M016 - Cnt of active pipes */
  25. extern int LastRetCode ;
  26. extern jmp_buf MainEnv ;
  27. extern jmp_buf CmdJBuf1 ;
  28. extern unsigned long OHTbl[] ; /* M024 - Revised to be bit map */
  29. extern PHANDLE FFhandles; /* @@1 */
  30. extern unsigned FFhndlsaved; /* @@1 */
  31. extern struct sellist *prexxsellist;
  32. extern struct rio *rioCur ; /* M000 */
  33. extern TCHAR *Fvars ; /* M026 */
  34. extern TCHAR **Fsubs ; /* M026 */
  35. extern TCHAR *save_Fvars ; /* @@ */
  36. extern TCHAR **save_Fsubs ; /* @@ */
  37. extern int FvarsSaved; /* @@ */
  38. extern TCHAR InternalError[] ;
  39. extern int EchoFlag ;
  40. extern int EchoSave ; /* M013 - Used to restore echo status */
  41. extern TCHAR ComSpec[] ; /* M008 - For clearing SM shared memory */
  42. extern TCHAR ComSpecStr[] ; /* M026 - Use ComSpec for SM memory */
  43. extern TCHAR *CmdSpec ; /* M026 */
  44. extern unsigned Heof;
  45. extern unsigned start_type ; /* Flag to indicate which API started the */
  46. /* program. D64 */
  47. extern BOOL CtrlCSeen;
  48. extern PTCHAR pszTitleCur;
  49. extern BOOLEAN fTitleChanged;
  50. void
  51. Abort( void )
  52. {
  53. DEBUG((SHGRP, MSLVL, "SIGHAND: Aborting Command")) ;
  54. SigCleanUp();
  55. longjmp(MainEnv, 1) ;
  56. CMDexit( FAILURE );
  57. }
  58. void
  59. CtrlCAbort( ) {
  60. struct batdata *bdat;
  61. if (CurrentBatchFile) {
  62. if (PromptUser(NULL, MSG_BATCH_TERM, MSG_NOYES_RESPONSE_DATA) != 1) {
  63. ResetCtrlC();
  64. return;
  65. }
  66. //
  67. // End local environments ( Otherwise we can end up with garbage
  68. // in the main environment if any batch file used the setlocal
  69. // command ).
  70. //
  71. bdat = CurrentBatchFile;
  72. while ( bdat ) {
  73. EndAllLocals( bdat );
  74. bdat = bdat->backptr;
  75. }
  76. }
  77. SigCleanUp();
  78. longjmp(MainEnv, 1) ;
  79. }
  80. void
  81. CheckCtrlC (
  82. ) {
  83. if (CtrlCSeen) {
  84. CtrlCAbort();
  85. }
  86. }
  87. void
  88. ExitAbort(
  89. IN ULONG rcExitCode
  90. )
  91. {
  92. SigCleanUp();
  93. longjmp(MainEnv, rcExitCode) ;
  94. CMDexit( FAILURE );
  95. }
  96. /*** SigCleanUp - close files and reset I/O after a signal
  97. *
  98. * Purpose:
  99. * This function is called to finish the cleanup after an int 23 or 24.
  100. * It resets all redirection back to the main level and it closes all
  101. * files except those for stdin, stdout, stderr, stdaux and stdprint.
  102. *
  103. * void SigCleanUp()
  104. *
  105. * Args:
  106. * None.
  107. *
  108. * Returns:
  109. * Nothing.
  110. *
  111. * Notes:
  112. * - M024 * Revised handle closing to be bit map based rather than struct.
  113. *
  114. */
  115. void SigCleanUp() /* M000 - Now void */
  116. {
  117. int cnt, cnt2 ;
  118. unsigned long mask;
  119. Heof = FALSE;
  120. #ifndef WIN95_CMD
  121. if (CurrentBatchFile) {
  122. // Following CmdBatNotification call is a cleanup for the
  123. // same call made from BatProc (in cbatch.c).
  124. CmdBatNotification (CMD_BAT_OPERATION_TERMINATING);
  125. EchoFlag = EchoSave ;
  126. GotoFlag = FALSE ;
  127. eEndlocal( NULL ) ;
  128. CurrentBatchFile = NULL ;
  129. } ;
  130. #endif // WIN95_CMD
  131. if (!FvarsSaved) { /* @WM If already saved, don't save again */
  132. save_Fvars = Fvars; /* @@ */
  133. save_Fsubs = Fsubs; /* @@ */
  134. FvarsSaved = TRUE; /* @@ */
  135. }
  136. Fvars = NULL ; /* M026 - Must kill FOR */
  137. Fsubs = NULL ; /* ...variable subst's */
  138. /* M000 - New method is simpler. If redirection has been done, the highest
  139. * numbered handle resulting from redirection is saved, then the linked
  140. * riodata list is unlinked until the first (main) level of redirection is
  141. * reached at which time ResetRedir is used to reset it. Then all open
  142. * handles from 5 to the highest numbered redirection handle (minimum of
  143. * 19) are freed.
  144. * M014 - Altered this to use actual global pointer when unwinding the
  145. * riodata list to fix bug. Also revised the ->stdio element to conform
  146. * to new data structure. Note that ResetRedir automatically resets the
  147. * rioCur pointer to the last valid riodata structure before returning;
  148. * same as if "rioCur=rioCur->back" was in the while loop.
  149. */
  150. DEBUG((SHGRP, MSLVL, "SCLEANUP: Resetting redirection.")) ;
  151. while (rioCur)
  152. ResetRedir() ;
  153. DEBUG((SHGRP, MSLVL, "SCLEANUP: Breaking pipes.")) ;
  154. BreakPipes() ;
  155. DEBUG((SHGRP, MSLVL, "SCLEANUP: Now closing extra handles.")) ;
  156. for (cnt = 0; cnt < 3; cnt++) {
  157. if (OHTbl[cnt]) { /* Any handles to reset? */
  158. mask = 1; /* @@1 */
  159. for (cnt2 = 0; cnt2 < 32; cnt2++, mask <<= 1) { /* @@1 */
  160. if ((OHTbl[cnt] & mask) && /* @@1 */
  161. ((cnt == 0 && cnt2 > 2) || cnt != 0) ) { /* @@1 */
  162. /* Don't close STDIN, STDOUT, STDERR */ /* @@1 */
  163. Cclose(cnt2 + 32*cnt); /* @@1 */
  164. } /* @@1 */
  165. } /* @@1 */
  166. } /* @@1 */
  167. }
  168. /* Close find first handles */ /* @@1 */
  169. while (FFhndlsaved) { /* findclose will dec this @@1 */
  170. findclose(FFhandles[FFhndlsaved - 1]); /* @@1 */
  171. } /* @@1 */
  172. ResetConTitle( pszTitleCur );
  173. ResetConsoleMode();
  174. DEBUG((SHGRP, MSLVL, "SCLEANUP: Returning.")) ;
  175. }