| 1 |
/* |
| 2 |
* tclWinTest.c -- |
| 3 |
* |
| 4 |
* Contains commands for platform specific tests on Windows. |
| 5 |
* |
| 6 |
* Copyright (c) 1996 Sun Microsystems, Inc. |
| 7 |
* |
| 8 |
* See the file "license.terms" for information on usage and redistribution |
| 9 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 10 |
* |
| 11 |
* RCS: @(#) $Id: tclWinTest.c,v 1.8.2.1 2003/04/12 20:11:34 kennykb Exp $ |
| 12 |
*/ |
| 13 |
|
| 14 |
#define USE_COMPAT_CONST |
| 15 |
#include "tclWinInt.h" |
| 16 |
|
| 17 |
/* |
| 18 |
* Forward declarations of procedures defined later in this file: |
| 19 |
*/ |
| 20 |
int TclplatformtestInit _ANSI_ARGS_((Tcl_Interp *interp)); |
| 21 |
static int TesteventloopCmd _ANSI_ARGS_((ClientData dummy, |
| 22 |
Tcl_Interp *interp, int argc, char **argv)); |
| 23 |
static int TestvolumetypeCmd _ANSI_ARGS_((ClientData dummy, |
| 24 |
Tcl_Interp *interp, int objc, |
| 25 |
Tcl_Obj *CONST objv[])); |
| 26 |
static int TestwinclockCmd _ANSI_ARGS_(( ClientData dummy, |
| 27 |
Tcl_Interp* interp, |
| 28 |
int objc, |
| 29 |
Tcl_Obj *CONST objv[] )); |
| 30 |
static int TestwinsleepCmd _ANSI_ARGS_(( ClientData dummy, |
| 31 |
Tcl_Interp* interp, |
| 32 |
int objc, |
| 33 |
Tcl_Obj *CONST objv[] )); |
| 34 |
static Tcl_ObjCmdProc TestExceptionCmd; |
| 35 |
|
| 36 |
|
| 37 |
/* |
| 38 |
*---------------------------------------------------------------------- |
| 39 |
* |
| 40 |
* TclplatformtestInit -- |
| 41 |
* |
| 42 |
* Defines commands that test platform specific functionality for |
| 43 |
* Windows platforms. |
| 44 |
* |
| 45 |
* Results: |
| 46 |
* A standard Tcl result. |
| 47 |
* |
| 48 |
* Side effects: |
| 49 |
* Defines new commands. |
| 50 |
* |
| 51 |
*---------------------------------------------------------------------- |
| 52 |
*/ |
| 53 |
|
| 54 |
int |
| 55 |
TclplatformtestInit(interp) |
| 56 |
Tcl_Interp *interp; /* Interpreter to add commands to. */ |
| 57 |
{ |
| 58 |
/* |
| 59 |
* Add commands for platform specific tests for Windows here. |
| 60 |
*/ |
| 61 |
|
| 62 |
Tcl_CreateCommand(interp, "testeventloop", TesteventloopCmd, |
| 63 |
(ClientData) 0, (Tcl_CmdDeleteProc *) NULL); |
| 64 |
Tcl_CreateObjCommand(interp, "testvolumetype", TestvolumetypeCmd, |
| 65 |
(ClientData) 0, (Tcl_CmdDeleteProc *) NULL); |
| 66 |
Tcl_CreateObjCommand(interp, "testwinclock", TestwinclockCmd, |
| 67 |
(ClientData) 0, (Tcl_CmdDeleteProc *) NULL); |
| 68 |
Tcl_CreateObjCommand( interp, |
| 69 |
"testwinsleep", |
| 70 |
TestwinsleepCmd, |
| 71 |
(ClientData) 0, |
| 72 |
(Tcl_CmdDeleteProc *) NULL ); |
| 73 |
Tcl_CreateObjCommand(interp, "testexcept", TestExceptionCmd, NULL, NULL); |
| 74 |
return TCL_OK; |
| 75 |
} |
| 76 |
|
| 77 |
/* |
| 78 |
*---------------------------------------------------------------------- |
| 79 |
* |
| 80 |
* TesteventloopCmd -- |
| 81 |
* |
| 82 |
* This procedure implements the "testeventloop" command. It is |
| 83 |
* used to test the Tcl notifier from an "external" event loop |
| 84 |
* (i.e. not Tcl_DoOneEvent()). |
| 85 |
* |
| 86 |
* Results: |
| 87 |
* A standard Tcl result. |
| 88 |
* |
| 89 |
* Side effects: |
| 90 |
* None. |
| 91 |
* |
| 92 |
*---------------------------------------------------------------------- |
| 93 |
*/ |
| 94 |
|
| 95 |
static int |
| 96 |
TesteventloopCmd(clientData, interp, argc, argv) |
| 97 |
ClientData clientData; /* Not used. */ |
| 98 |
Tcl_Interp *interp; /* Current interpreter. */ |
| 99 |
int argc; /* Number of arguments. */ |
| 100 |
char **argv; /* Argument strings. */ |
| 101 |
{ |
| 102 |
static int *framePtr = NULL; /* Pointer to integer on stack frame of |
| 103 |
* innermost invocation of the "wait" |
| 104 |
* subcommand. */ |
| 105 |
|
| 106 |
if (argc < 2) { |
| 107 |
Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0], |
| 108 |
" option ... \"", (char *) NULL); |
| 109 |
return TCL_ERROR; |
| 110 |
} |
| 111 |
if (strcmp(argv[1], "done") == 0) { |
| 112 |
*framePtr = 1; |
| 113 |
} else if (strcmp(argv[1], "wait") == 0) { |
| 114 |
int *oldFramePtr; |
| 115 |
int done; |
| 116 |
MSG msg; |
| 117 |
int oldMode = Tcl_SetServiceMode(TCL_SERVICE_ALL); |
| 118 |
|
| 119 |
/* |
| 120 |
* Save the old stack frame pointer and set up the current frame. |
| 121 |
*/ |
| 122 |
|
| 123 |
oldFramePtr = framePtr; |
| 124 |
framePtr = &done; |
| 125 |
|
| 126 |
/* |
| 127 |
* Enter a standard Windows event loop until the flag changes. |
| 128 |
* Note that we do not explicitly call Tcl_ServiceEvent(). |
| 129 |
*/ |
| 130 |
|
| 131 |
done = 0; |
| 132 |
while (!done) { |
| 133 |
if (!GetMessage(&msg, NULL, 0, 0)) { |
| 134 |
/* |
| 135 |
* The application is exiting, so repost the quit message |
| 136 |
* and start unwinding. |
| 137 |
*/ |
| 138 |
|
| 139 |
PostQuitMessage(msg.wParam); |
| 140 |
break; |
| 141 |
} |
| 142 |
TranslateMessage(&msg); |
| 143 |
DispatchMessage(&msg); |
| 144 |
} |
| 145 |
(void) Tcl_SetServiceMode(oldMode); |
| 146 |
framePtr = oldFramePtr; |
| 147 |
} else { |
| 148 |
Tcl_AppendResult(interp, "bad option \"", argv[1], |
| 149 |
"\": must be done or wait", (char *) NULL); |
| 150 |
return TCL_ERROR; |
| 151 |
} |
| 152 |
return TCL_OK; |
| 153 |
} |
| 154 |
|
| 155 |
/* |
| 156 |
*---------------------------------------------------------------------- |
| 157 |
* |
| 158 |
* Testvolumetype -- |
| 159 |
* |
| 160 |
* This procedure implements the "testvolumetype" command. It is |
| 161 |
* used to check the volume type (FAT, NTFS) of a volume. |
| 162 |
* |
| 163 |
* Results: |
| 164 |
* A standard Tcl result. |
| 165 |
* |
| 166 |
* Side effects: |
| 167 |
* None. |
| 168 |
* |
| 169 |
*---------------------------------------------------------------------- |
| 170 |
*/ |
| 171 |
|
| 172 |
static int |
| 173 |
TestvolumetypeCmd(clientData, interp, objc, objv) |
| 174 |
ClientData clientData; /* Not used. */ |
| 175 |
Tcl_Interp *interp; /* Current interpreter. */ |
| 176 |
int objc; /* Number of arguments. */ |
| 177 |
Tcl_Obj *CONST objv[]; /* Argument objects. */ |
| 178 |
{ |
| 179 |
#define VOL_BUF_SIZE 32 |
| 180 |
int found; |
| 181 |
char volType[VOL_BUF_SIZE]; |
| 182 |
char *path; |
| 183 |
|
| 184 |
if (objc > 2) { |
| 185 |
Tcl_WrongNumArgs(interp, 1, objv, "?name?"); |
| 186 |
return TCL_ERROR; |
| 187 |
} |
| 188 |
if (objc == 2) { |
| 189 |
/* |
| 190 |
* path has to be really a proper volume, but we don't |
| 191 |
* get query APIs for that until NT5 |
| 192 |
*/ |
| 193 |
path = Tcl_GetString(objv[1]); |
| 194 |
} else { |
| 195 |
path = NULL; |
| 196 |
} |
| 197 |
found = GetVolumeInformationA(path, NULL, 0, NULL, NULL, |
| 198 |
NULL, volType, VOL_BUF_SIZE); |
| 199 |
|
| 200 |
if (found == 0) { |
| 201 |
Tcl_AppendResult(interp, "could not get volume type for \"", |
| 202 |
(path?path:""), "\"", (char *) NULL); |
| 203 |
TclWinConvertError(GetLastError()); |
| 204 |
return TCL_ERROR; |
| 205 |
} |
| 206 |
Tcl_SetResult(interp, volType, TCL_VOLATILE); |
| 207 |
return TCL_OK; |
| 208 |
#undef VOL_BUF_SIZE |
| 209 |
} |
| 210 |
|
| 211 |
/* |
| 212 |
*---------------------------------------------------------------------- |
| 213 |
* |
| 214 |
* TestwinclockCmd -- |
| 215 |
* |
| 216 |
* Command that returns the seconds and microseconds portions of |
| 217 |
* the system clock and of the Tcl clock so that they can be |
| 218 |
* compared to validate that the Tcl clock is staying in sync. |
| 219 |
* |
| 220 |
* Usage: |
| 221 |
* testclock |
| 222 |
* |
| 223 |
* Parameters: |
| 224 |
* None. |
| 225 |
* |
| 226 |
* Results: |
| 227 |
* Returns a standard Tcl result comprising a four-element list: |
| 228 |
* the seconds and microseconds portions of the system clock, |
| 229 |
* and the seconds and microseconds portions of the Tcl clock. |
| 230 |
* |
| 231 |
* Side effects: |
| 232 |
* None. |
| 233 |
* |
| 234 |
*---------------------------------------------------------------------- |
| 235 |
*/ |
| 236 |
|
| 237 |
static int |
| 238 |
TestwinclockCmd( ClientData dummy, |
| 239 |
/* Unused */ |
| 240 |
Tcl_Interp* interp, |
| 241 |
/* Tcl interpreter */ |
| 242 |
int objc, |
| 243 |
/* Argument count */ |
| 244 |
Tcl_Obj *CONST objv[] ) |
| 245 |
/* Argument vector */ |
| 246 |
{ |
| 247 |
CONST static FILETIME posixEpoch = { 0xD53E8000, 0x019DB1DE }; |
| 248 |
/* The Posix epoch, expressed as a |
| 249 |
* Windows FILETIME */ |
| 250 |
Tcl_Time tclTime; /* Tcl clock */ |
| 251 |
FILETIME sysTime; /* System clock */ |
| 252 |
Tcl_Obj* result; /* Result of the command */ |
| 253 |
LARGE_INTEGER t1, t2; |
| 254 |
LARGE_INTEGER p1, p2; |
| 255 |
|
| 256 |
if ( objc != 1 ) { |
| 257 |
Tcl_WrongNumArgs( interp, 1, objv, "" ); |
| 258 |
return TCL_ERROR; |
| 259 |
} |
| 260 |
|
| 261 |
QueryPerformanceCounter( &p1 ); |
| 262 |
|
| 263 |
Tcl_GetTime( &tclTime ); |
| 264 |
GetSystemTimeAsFileTime( &sysTime ); |
| 265 |
t1.LowPart = posixEpoch.dwLowDateTime; |
| 266 |
t1.HighPart = posixEpoch.dwHighDateTime; |
| 267 |
t2.LowPart = sysTime.dwLowDateTime; |
| 268 |
t2.HighPart = sysTime.dwHighDateTime; |
| 269 |
t2.QuadPart -= t1.QuadPart; |
| 270 |
|
| 271 |
QueryPerformanceCounter( &p2 ); |
| 272 |
|
| 273 |
result = Tcl_NewObj(); |
| 274 |
Tcl_ListObjAppendElement |
| 275 |
( interp, result, Tcl_NewIntObj( (int) (t2.QuadPart / 10000000 ) ) ); |
| 276 |
Tcl_ListObjAppendElement |
| 277 |
( interp, result, |
| 278 |
Tcl_NewIntObj( (int) ( (t2.QuadPart / 10 ) % 1000000 ) ) ); |
| 279 |
Tcl_ListObjAppendElement( interp, result, Tcl_NewIntObj( tclTime.sec ) ); |
| 280 |
Tcl_ListObjAppendElement( interp, result, Tcl_NewIntObj( tclTime.usec ) ); |
| 281 |
|
| 282 |
Tcl_ListObjAppendElement( interp, result, Tcl_NewWideIntObj( p1.QuadPart ) ); |
| 283 |
Tcl_ListObjAppendElement( interp, result, Tcl_NewWideIntObj( p2.QuadPart ) ); |
| 284 |
|
| 285 |
Tcl_SetObjResult( interp, result ); |
| 286 |
|
| 287 |
return TCL_OK; |
| 288 |
} |
| 289 |
|
| 290 |
/* |
| 291 |
*---------------------------------------------------------------------- |
| 292 |
* |
| 293 |
* Testwinsleepcmd -- |
| 294 |
* |
| 295 |
* Causes this process to wait for the given number of milliseconds |
| 296 |
* by means of a direct call to Sleep. |
| 297 |
* |
| 298 |
* Usage: |
| 299 |
* testwinsleep <n> |
| 300 |
* |
| 301 |
* Parameters: |
| 302 |
* n - the number of milliseconds to sleep |
| 303 |
* |
| 304 |
* Results: |
| 305 |
* None. |
| 306 |
* |
| 307 |
* Side effects: |
| 308 |
* Sleeps for the requisite number of milliseconds. |
| 309 |
* |
| 310 |
*---------------------------------------------------------------------- |
| 311 |
*/ |
| 312 |
|
| 313 |
static int |
| 314 |
TestwinsleepCmd( ClientData clientData, |
| 315 |
/* Unused */ |
| 316 |
Tcl_Interp* interp, |
| 317 |
/* Tcl interpreter */ |
| 318 |
int objc, |
| 319 |
/* Parameter count */ |
| 320 |
Tcl_Obj * CONST * objv ) |
| 321 |
/* Parameter vector */ |
| 322 |
{ |
| 323 |
int ms; |
| 324 |
if ( objc != 2 ) { |
| 325 |
Tcl_WrongNumArgs( interp, 1, objv, "ms" ); |
| 326 |
return TCL_ERROR; |
| 327 |
} |
| 328 |
if ( Tcl_GetIntFromObj( interp, objv[1], &ms ) != TCL_OK ) { |
| 329 |
return TCL_ERROR; |
| 330 |
} |
| 331 |
Sleep( (DWORD) ms ); |
| 332 |
return TCL_OK; |
| 333 |
} |
| 334 |
|
| 335 |
/* |
| 336 |
*---------------------------------------------------------------------- |
| 337 |
* |
| 338 |
* TestExceptionCmd -- |
| 339 |
* |
| 340 |
* Causes this process to end with the named exception. Used for |
| 341 |
* testing Tcl_WaitPid(). |
| 342 |
* |
| 343 |
* Usage: |
| 344 |
* testexcept <type> |
| 345 |
* |
| 346 |
* Parameters: |
| 347 |
* Type of exception. |
| 348 |
* |
| 349 |
* Results: |
| 350 |
* None, this process closes now and doesn't return. |
| 351 |
* |
| 352 |
* Side effects: |
| 353 |
* This Tcl process closes, hard... Bang! |
| 354 |
* |
| 355 |
*---------------------------------------------------------------------- |
| 356 |
*/ |
| 357 |
|
| 358 |
static int |
| 359 |
TestExceptionCmd( |
| 360 |
ClientData dummy, /* Unused */ |
| 361 |
Tcl_Interp* interp, /* Tcl interpreter */ |
| 362 |
int objc, /* Argument count */ |
| 363 |
Tcl_Obj *CONST objv[]) /* Argument vector */ |
| 364 |
{ |
| 365 |
static char *cmds[] = { |
| 366 |
"access_violation", |
| 367 |
"datatype_misalignment", |
| 368 |
"array_bounds", |
| 369 |
"float_denormal", |
| 370 |
"float_divbyzero", |
| 371 |
"float_inexact", |
| 372 |
"float_invalidop", |
| 373 |
"float_overflow", |
| 374 |
"float_stack", |
| 375 |
"float_underflow", |
| 376 |
"int_divbyzero", |
| 377 |
"int_overflow", |
| 378 |
"private_instruction", |
| 379 |
"inpageerror", |
| 380 |
"illegal_instruction", |
| 381 |
"noncontinue", |
| 382 |
"stack_overflow", |
| 383 |
"invalid_disp", |
| 384 |
"guard_page", |
| 385 |
"invalid_handle", |
| 386 |
"ctrl+c", |
| 387 |
NULL |
| 388 |
}; |
| 389 |
static DWORD exceptions[] = { |
| 390 |
EXCEPTION_ACCESS_VIOLATION, |
| 391 |
EXCEPTION_DATATYPE_MISALIGNMENT, |
| 392 |
EXCEPTION_ARRAY_BOUNDS_EXCEEDED, |
| 393 |
EXCEPTION_FLT_DENORMAL_OPERAND, |
| 394 |
EXCEPTION_FLT_DIVIDE_BY_ZERO, |
| 395 |
EXCEPTION_FLT_INEXACT_RESULT, |
| 396 |
EXCEPTION_FLT_INVALID_OPERATION, |
| 397 |
EXCEPTION_FLT_OVERFLOW, |
| 398 |
EXCEPTION_FLT_STACK_CHECK, |
| 399 |
EXCEPTION_FLT_UNDERFLOW, |
| 400 |
EXCEPTION_INT_DIVIDE_BY_ZERO, |
| 401 |
EXCEPTION_INT_OVERFLOW, |
| 402 |
EXCEPTION_PRIV_INSTRUCTION, |
| 403 |
EXCEPTION_IN_PAGE_ERROR, |
| 404 |
EXCEPTION_ILLEGAL_INSTRUCTION, |
| 405 |
EXCEPTION_NONCONTINUABLE_EXCEPTION, |
| 406 |
EXCEPTION_STACK_OVERFLOW, |
| 407 |
EXCEPTION_INVALID_DISPOSITION, |
| 408 |
EXCEPTION_GUARD_PAGE, |
| 409 |
EXCEPTION_INVALID_HANDLE, |
| 410 |
CONTROL_C_EXIT |
| 411 |
}; |
| 412 |
int cmd; |
| 413 |
|
| 414 |
if ( objc != 2 ) { |
| 415 |
Tcl_WrongNumArgs(interp, 0, objv, "<type-of-exception>"); |
| 416 |
return TCL_ERROR; |
| 417 |
} |
| 418 |
if (Tcl_GetIndexFromObj(interp, objv[1], cmds, "command", 0, |
| 419 |
&cmd) != TCL_OK) { |
| 420 |
return TCL_ERROR; |
| 421 |
} |
| 422 |
|
| 423 |
/* |
| 424 |
* Make sure the GPF dialog doesn't popup. |
| 425 |
*/ |
| 426 |
|
| 427 |
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); |
| 428 |
|
| 429 |
/* |
| 430 |
* As Tcl does not handle structured exceptions, this falls all the way |
| 431 |
* back up the instruction stack to the C run-time portion that called |
| 432 |
* main() where the process will now be terminated with this exception |
| 433 |
* code by the default handler the C run-time provides. |
| 434 |
*/ |
| 435 |
|
| 436 |
/* SMASH! */ |
| 437 |
RaiseException(exceptions[cmd], EXCEPTION_NONCONTINUABLE, 0, NULL); |
| 438 |
|
| 439 |
/* NOTREACHED */ |
| 440 |
return TCL_OK; |
| 441 |
} |