Tcl Source Code

Attachment Details
Login
Overview

Artifact ID: d1c7b905bea18592a5192f6593b9d2de3408540c
Ticket: 219355ffffffffffffffffffffffffffffffffff
Date: 2001-05-18 06:23:41
User: davygrvy
Artifact Attached: 4ec6824466648c184a7f0124fc59f5f9282feb81
Filename:noBoom.c
Description:fixed code
Content:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
   108
   109
   110
   111
   112
   113
   114
   115
   116
   117
   118
/*
 * free-form code mods by David Gravereaux <[email protected]>
 * May 17, 01
 */

#define TCL_THREADS 1
#include <tcl.h>

#ifdef _MSC_VER
#   ifdef _DEBUG
#	pragma comment (lib, "tcl" STRINGIFY(JOIN(TCL_MAJOR_VERSION,TCL_MINOR_VERSION)) "dt.lib")
#   else
#	pragma comment (lib, "tcl" STRINGIFY(JOIN(TCL_MAJOR_VERSION,TCL_MINOR_VERSION)) "t.lib")
#   endif
#endif

#include <windows.h>

BOOL __stdcall sigHandler (DWORD fdwCtrlType);
Tcl_AsyncHandler exitToken; 
Tcl_AsyncProc asyncExit;

int main( int argc, char* argv[] )
{
    Tcl_Interp* m_pInterp;

    printf( "start!\n");

    Tcl_FindExecutable(argv[0]);
    exitToken = Tcl_AsyncCreate(asyncExit, NULL);
    SetConsoleCtrlHandler(sigHandler, TRUE);

    m_pInterp = Tcl_CreateInterp();
    Tcl_Init(m_pInterp);    //no init errors!

    /* do work here */

    Tcl_DeleteInterp(m_pInterp);
    Tcl_Finalize();

    printf( "close the window!\n");
    getchar();    //at this pause we click the 'x' on the console.
    return 0;
}

/* 
 *---------------------------------------------------------------------- 
 * 
 * asyncExit -- 
 * 
 *	The AsyncProc for the exitToken. 
 * 
 * Results: 
 *	doesn't actually return. 
 * 
 * Side effects: 
 *	tclsh cleanly exits. 
 * 
 *---------------------------------------------------------------------- 
 */ 

int
asyncExit (ClientData clientData, Tcl_Interp *interp, int code) 
{ 
    Tcl_Exit(-1);

    /* NOTREACHED */ 
    return code; 
} 

/* 
 *---------------------------------------------------------------------- 
 * 
 * sigHandler -- 
 * 
 *	Signal handler for the Win32 OS. Catches Ctrl+C, Ctrl+Break and 
 *	other exits. This is needed so tclsh can do it's real clean-up 
 *	and not an unclean crash terminate. 
 * 
 * Results: 
 *	TRUE. 
 * 
 * Side effects: 
 *	effects the way the app exits from a signal. This is an 
 *	operating system supplied thread and unsafe to call ANY 
 *	Tcl commands except for Tcl_AsyncMark. 
 * 
 *---------------------------------------------------------------------- 
 */ 

BOOL __stdcall
sigHandler (DWORD fdwCtrlType) 
{ 
    /* 
     * If Tcl is currently executing some bytecode or in the eventloop, 
     * this will cause Tcl to enter asyncExit at the next command 
     * boundry.  This is protected in Win32 SEH to ensure mutex access
     * will not cause an exception should Tcl_Finalize() have already
     * cleaned-up the async subsystem.
     */
    __try {
	Tcl_AsyncMark(exitToken);
    }
    __except (EXCEPTION_EXECUTE_HANDLER) {
	/* ignore exceptions */
    }

    /* 
     * This will cause Tcl_Gets in Tcl_Main() to drop-out with an <EOF> 
     * should it be blocked on input and our Tcl_AsyncMark didn't grab 
     * the attention of the interpreter. 
     */
    CloseHandle(GetStdHandle(STD_INPUT_HANDLE)); 

    /* indicate to the OS not to call the default terminator */ 
    return TRUE; 
}