C-----------------------------------------------------------------
C	Exit Handler for DM
C-----------------------------------------------------------------
	Subroutine DM_Exit_Handler
	Integer*2 K
	Real Terminal_Delay
	Include 'Common.Dat'

	Data Terminal_Delay /0.05/

C-----------------------------------------------------------------
	Have_DCL_Setup = .FALSE.  ! Can't Close Mbx from Exit_Handler,
C				    But it will go away anyway.
	Call Cancel_DCL  ! Get rid of subprocess.
	Call SMG$Set_Broadcast_Trapping (PasteBoard_ID,%Val(0)) !Disabled
	Call SMG$Delete_PasteBoard (PasteBoard_ID,1)
  	Call SMG$Delete_Virtual_Keyboard (Keyboard_ID)

C		Under some conditions, this is needed.
	Call Lib$Wait(Terminal_Delay)
	Call WriteTerm (LargeScreen)
	Call WriteTerm (Cursor_On)
	Call WriteTerm (Reterminal)    ! Put the tube back the way it was.
	Call WriteTerm (CSI//'1;1H')   ! Cursor at 1,1

	If (Exit_Com(1:4) .EQ. 'zzzz') then
		Stat = Lib$Set_Symbol('DM$command', 'goto getout')
C			NOTE: The following two instructions are not exactly
C			"honest".  See Normal_Keys.for for what the
C			'X' command does (Original_x = Current_x)
		If (Current_Device .NE. Original_Device) then
		   Call Str$Trim (%Descr(Original_Device)
	1		,%Descr(Original_Device),%Ref(K))
		   Call Lib$Set_Logical('SYS$DISK',Original_Device(:K))
		End If
	      	Call Sys$SetdDir( Original_Dir, %Val(0), %Val(0) ) 
	Else
		Call Save_Stack       ! Save current environment
		Stat = Lib$Set_Symbol('DM$command', exit_com)
	End If 

C		GO OUT TO DCL  (DM.COM) AND RUN A COMMAND
C		Will run 'exit_com' unless it's value is 'GOTO GETOUT'

	Return
	End
