Debian Codegraph @

generic/tclTest.c

raw
1 /*
2 * tclTest.c --
3 *
4 * This file contains C command functions for a bunch of additional Tcl
5 * commands that are used for testing out Tcl's C interfaces. These
6 * commands are not normally included in Tcl applications; they're only
7 * used for testing.
8 *
9 * Copyright (c) 1993-1994 The Regents of the University of California.
10 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
11 * Copyright (c) 1998-2000 Ajuba Solutions.
12 * Copyright (c) 2003 Kevin B. Kenny. All rights reserved.
13 *
14 * See the file "license.terms" for information on usage and redistribution of
15 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 */
17
18 #undef STATIC_BUILD
19 #ifndef USE_TCL_STUBS
20 # define USE_TCL_STUBS
21 #endif
22 #include "tclInt.h"
23 #include "tclOO.h"
24 #include <math.h>
25
26 /*
27 * Required for Testregexp*Cmd
28 */
29 #include "tclRegexp.h"
30
31 /*
32 * Required for TestlocaleCmd
33 */
34 #include <locale.h>
35
36 /*
37 * Required for the TestChannelCmd and TestChannelEventCmd
38 */
39 #include "tclIO.h"
40
41 /*
42 * Declare external functions used in Windows tests.
43 */
44
45 /*
46 * TCL_STORAGE_CLASS is set unconditionally to DLLEXPORT because the
47 * Tcltest_Init declaration is in the source file itself, which is only
48 * accessed when we are building a library.
49 */
50
51 #undef TCL_STORAGE_CLASS
52 #define TCL_STORAGE_CLASS DLLEXPORT
53 EXTERN int Tcltest_Init(Tcl_Interp *interp);
54 EXTERN int Tcltest_SafeInit(Tcl_Interp *interp);
55
56 /*
57 * Dynamic string shared by TestdcallCmd and DelCallbackProc; used to collect
58 * the results of the various deletion callbacks.
59 */
60
61 static Tcl_DString delString;
62 static Tcl_Interp *delInterp;
63
64 /*
65 * One of the following structures exists for each asynchronous handler
66 * created by the "testasync" command".
67 */
68
69 typedef struct TestAsyncHandler {
70 int id; /* Identifier for this handler. */
71 Tcl_AsyncHandler handler; /* Tcl's token for the handler. */
72 char *command; /* Command to invoke when the handler is
73 * invoked. */
74 struct TestAsyncHandler *nextPtr;
75 /* Next is list of handlers. */
76 } TestAsyncHandler;
77
78 TCL_DECLARE_MUTEX(asyncTestMutex)
79
80 static TestAsyncHandler *firstHandler = NULL;
81
82 /*
83 * The dynamic string below is used by the "testdstring" command to test the
84 * dynamic string facilities.
85 */
86
87 static Tcl_DString dstring;
88
89 /*
90 * The command trace below is used by the "testcmdtraceCmd" command to test
91 * the command tracing facilities.
92 */
93
94 static Tcl_Trace cmdTrace;
95
96 /*
97 * One of the following structures exists for each command created by
98 * TestdelCmd:
99 */
100
101 typedef struct {
102 Tcl_Interp *interp; /* Interpreter in which command exists. */
103 char *deleteCmd; /* Script to execute when command is deleted.
104 * Malloc'ed. */
105 } DelCmd;
106
107 /*
108 * The following is used to keep track of an encoding that invokes a Tcl
109 * command.
110 */
111
112 typedef struct {
113 Tcl_Interp *interp;
114 char *toUtfCmd;
115 char *fromUtfCmd;
116 } TclEncoding;
117
118 /*
119 * The counter below is used to determine if the TestsaveresultFree routine
120 * was called for a result.
121 */
122
123 static int freeCount;
124
125 /*
126 * Boolean flag used by the "testsetmainloop" and "testexitmainloop" commands.
127 */
128
129 static int exitMainLoop = 0;
130
131 /*
132 * Event structure used in testing the event queue management procedures.
133 */
134
135 typedef struct {
136 Tcl_Event header; /* Header common to all events */
137 Tcl_Interp *interp; /* Interpreter that will handle the event */
138 Tcl_Obj *command; /* Command to evaluate when the event occurs */
139 Tcl_Obj *tag; /* Tag for this event used to delete it */
140 } TestEvent;
141
142 /*
143 * Simple detach/attach facility for testchannel cut|splice. Allow testing of
144 * channel transfer in core testsuite.
145 */
146
147 typedef struct TestChannel {
148 Tcl_Channel chan; /* Detached channel */
149 struct TestChannel *nextPtr;/* Next in detached channel pool */
150 } TestChannel;
151
152 static TestChannel *firstDetached;
153
154 /*
155 * Forward declarations for procedures defined later in this file:
156 */
157
158 static int AsyncHandlerProc(ClientData clientData,
159 Tcl_Interp *interp, int code);
160 #ifdef TCL_THREADS
161 static Tcl_ThreadCreateType AsyncThreadProc(ClientData);
162 #endif
163 static void CleanupTestSetassocdataTests(
164 ClientData clientData, Tcl_Interp *interp);
165 static void CmdDelProc1(ClientData clientData);
166 static void CmdDelProc2(ClientData clientData);
167 static Tcl_CmdProc CmdProc1;
168 static Tcl_CmdProc CmdProc2;
169 static void CmdTraceDeleteProc(
170 ClientData clientData, Tcl_Interp *interp,
171 int level, char *command, Tcl_CmdProc *cmdProc,
172 ClientData cmdClientData, int argc,
173 const char *argv[]);
174 static void CmdTraceProc(ClientData clientData,
175 Tcl_Interp *interp, int level, char *command,
176 Tcl_CmdProc *cmdProc, ClientData cmdClientData,
177 int argc, const char *argv[]);
178 static Tcl_CmdProc CreatedCommandProc;
179 static Tcl_CmdProc CreatedCommandProc2;
180 static void DelCallbackProc(ClientData clientData,
181 Tcl_Interp *interp);
182 static Tcl_CmdProc DelCmdProc;
183 static void DelDeleteProc(ClientData clientData);
184 static void EncodingFreeProc(ClientData clientData);
185 static int EncodingToUtfProc(ClientData clientData,
186 const char *src, int srcLen, int flags,
187 Tcl_EncodingState *statePtr, char *dst,
188 int dstLen, int *srcReadPtr, int *dstWrotePtr,
189 int *dstCharsPtr);
190 static int EncodingFromUtfProc(ClientData clientData,
191 const char *src, int srcLen, int flags,
192 Tcl_EncodingState *statePtr, char *dst,
193 int dstLen, int *srcReadPtr, int *dstWrotePtr,
194 int *dstCharsPtr);
195 static void ExitProcEven(ClientData clientData);
196 static void ExitProcOdd(ClientData clientData);
197 static Tcl_ObjCmdProc GetTimesObjCmd;
198 static void MainLoop(void);
199 static Tcl_CmdProc NoopCmd;
200 static Tcl_ObjCmdProc NoopObjCmd;
201 static int ObjTraceProc(ClientData clientData,
202 Tcl_Interp *interp, int level, const char *command,
203 Tcl_Command commandToken, int objc,
204 Tcl_Obj *const objv[]);
205 static void ObjTraceDeleteProc(ClientData clientData);
206 static void PrintParse(Tcl_Interp *interp, Tcl_Parse *parsePtr);
207 static Tcl_FreeProc SpecialFree;
208 static int StaticInitProc(Tcl_Interp *interp);
209 static Tcl_CmdProc TestasyncCmd;
210 static Tcl_ObjCmdProc TestbumpinterpepochObjCmd;
211 static Tcl_ObjCmdProc TestsetbytearraylengthObjCmd;
212 static Tcl_ObjCmdProc TestpurebytesobjObjCmd;
213 static Tcl_ObjCmdProc TestbytestringObjCmd;
214 static Tcl_ObjCmdProc TeststringbytesObjCmd;
215 static Tcl_CmdProc TestcmdinfoCmd;
216 static Tcl_CmdProc TestcmdtokenCmd;
217 static Tcl_CmdProc TestcmdtraceCmd;
218 static Tcl_CmdProc TestconcatobjCmd;
219 static Tcl_CmdProc TestcreatecommandCmd;
220 static Tcl_CmdProc TestdcallCmd;
221 static Tcl_CmdProc TestdelCmd;
222 static Tcl_CmdProc TestdelassocdataCmd;
223 static Tcl_ObjCmdProc TestdoubledigitsObjCmd;
224 static Tcl_CmdProc TestdstringCmd;
225 static Tcl_ObjCmdProc TestencodingObjCmd;
226 static Tcl_ObjCmdProc TestevalexObjCmd;
227 static Tcl_ObjCmdProc TestevalobjvObjCmd;
228 static Tcl_ObjCmdProc TesteventObjCmd;
229 static int TesteventProc(Tcl_Event *event, int flags);
230 static int TesteventDeleteProc(Tcl_Event *event,
231 ClientData clientData);
232 static Tcl_CmdProc TestexithandlerCmd;
233 static Tcl_CmdProc TestexprlongCmd;
234 static Tcl_ObjCmdProc TestexprlongobjCmd;
235 static Tcl_CmdProc TestexprdoubleCmd;
236 static Tcl_ObjCmdProc TestexprdoubleobjCmd;
237 static Tcl_ObjCmdProc TestexprparserObjCmd;
238 static Tcl_CmdProc TestexprstringCmd;
239 static Tcl_ObjCmdProc TestfileCmd;
240 static Tcl_ObjCmdProc TestfilelinkCmd;
241 static Tcl_CmdProc TestfeventCmd;
242 static Tcl_CmdProc TestgetassocdataCmd;
243 static Tcl_CmdProc TestgetintCmd;
244 static Tcl_CmdProc TestgetplatformCmd;
245 static Tcl_ObjCmdProc TestgetvarfullnameCmd;
246 static Tcl_CmdProc TestinterpdeleteCmd;
247 static Tcl_CmdProc TestlinkCmd;
248 static Tcl_ObjCmdProc TestlocaleCmd;
249 static int TestMathFunc(ClientData clientData,
250 Tcl_Interp *interp, Tcl_Value *args,
251 Tcl_Value *resultPtr);
252 static int TestMathFunc2(ClientData clientData,
253 Tcl_Interp *interp, Tcl_Value *args,
254 Tcl_Value *resultPtr);
255 static Tcl_CmdProc TestmainthreadCmd;
256 static Tcl_CmdProc TestsetmainloopCmd;
257 static Tcl_CmdProc TestexitmainloopCmd;
258 static Tcl_CmdProc TestpanicCmd;
259 static Tcl_ObjCmdProc TestparseargsCmd;
260 static Tcl_ObjCmdProc TestparserObjCmd;
261 static Tcl_ObjCmdProc TestparsevarObjCmd;
262 static Tcl_ObjCmdProc TestparsevarnameObjCmd;
263 static Tcl_ObjCmdProc TestprintObjCmd;
264 static Tcl_ObjCmdProc TestregexpObjCmd;
265 static Tcl_ObjCmdProc TestreturnObjCmd;
266 static void TestregexpXflags(const char *string,
267 int length, int *cflagsPtr, int *eflagsPtr);
268 static Tcl_ObjCmdProc TestsaveresultCmd;
269 static Tcl_FreeProc TestsaveresultFree;
270 static Tcl_CmdProc TestsetassocdataCmd;
271 static Tcl_CmdProc TestsetCmd;
272 static Tcl_CmdProc Testset2Cmd;
273 static Tcl_CmdProc TestseterrorcodeCmd;
274 static Tcl_ObjCmdProc TestsetobjerrorcodeCmd;
275 static Tcl_CmdProc TestsetplatformCmd;
276 static Tcl_ObjCmdProc TestSizeCmd;
277 static Tcl_CmdProc TeststaticpkgCmd;
278 static Tcl_CmdProc TesttranslatefilenameCmd;
279 static Tcl_CmdProc TestupvarCmd;
280 static Tcl_ObjCmdProc TestWrongNumArgsObjCmd;
281 static Tcl_ObjCmdProc TestGetIndexFromObjStructObjCmd;
282 static Tcl_CmdProc TestChannelCmd;
283 static Tcl_CmdProc TestChannelEventCmd;
284 static Tcl_ObjCmdProc TestFilesystemObjCmd;
285 static Tcl_ObjCmdProc TestSimpleFilesystemObjCmd;
286 static void TestReport(const char *cmd, Tcl_Obj *arg1,
287 Tcl_Obj *arg2);
288 static Tcl_Obj * TestReportGetNativePath(Tcl_Obj *pathPtr);
289 static Tcl_FSStatProc TestReportStat;
290 static Tcl_FSAccessProc TestReportAccess;
291 static Tcl_FSOpenFileChannelProc TestReportOpenFileChannel;
292 static Tcl_FSMatchInDirectoryProc TestReportMatchInDirectory;
293 static Tcl_FSChdirProc TestReportChdir;
294 static Tcl_FSLstatProc TestReportLstat;
295 static Tcl_FSCopyFileProc TestReportCopyFile;
296 static Tcl_FSDeleteFileProc TestReportDeleteFile;
297 static Tcl_FSRenameFileProc TestReportRenameFile;
298 static Tcl_FSCreateDirectoryProc TestReportCreateDirectory;
299 static Tcl_FSCopyDirectoryProc TestReportCopyDirectory;
300 static Tcl_FSRemoveDirectoryProc TestReportRemoveDirectory;
301 static int TestReportLoadFile(Tcl_Interp *interp, Tcl_Obj *pathPtr,
302 Tcl_LoadHandle *handlePtr, Tcl_FSUnloadFileProc **unloadProcPtr);
303 static Tcl_FSLinkProc TestReportLink;
304 static Tcl_FSFileAttrStringsProc TestReportFileAttrStrings;
305 static Tcl_FSFileAttrsGetProc TestReportFileAttrsGet;
306 static Tcl_FSFileAttrsSetProc TestReportFileAttrsSet;
307 static Tcl_FSUtimeProc TestReportUtime;
308 static Tcl_FSNormalizePathProc TestReportNormalizePath;
309 static Tcl_FSPathInFilesystemProc TestReportInFilesystem;
310 static Tcl_FSFreeInternalRepProc TestReportFreeInternalRep;
311 static Tcl_FSDupInternalRepProc TestReportDupInternalRep;
312 static Tcl_CmdProc TestServiceModeCmd;
313 static Tcl_FSStatProc SimpleStat;
314 static Tcl_FSAccessProc SimpleAccess;
315 static Tcl_FSOpenFileChannelProc SimpleOpenFileChannel;
316 static Tcl_FSListVolumesProc SimpleListVolumes;
317 static Tcl_FSPathInFilesystemProc SimplePathInFilesystem;
318 static Tcl_Obj * SimpleRedirect(Tcl_Obj *pathPtr);
319 static Tcl_FSMatchInDirectoryProc SimpleMatchInDirectory;
320 static Tcl_ObjCmdProc TestUtfNextCmd;
321 static Tcl_ObjCmdProc TestUtfPrevCmd;
322 static Tcl_ObjCmdProc TestNumUtfCharsCmd;
323 static Tcl_ObjCmdProc TestGetUniCharCmd;
324 static Tcl_ObjCmdProc TestFindFirstCmd;
325 static Tcl_ObjCmdProc TestFindLastCmd;
326 static Tcl_ObjCmdProc TestHashSystemHashCmd;
327
328 static Tcl_NRPostProc NREUnwind_callback;
329 static Tcl_ObjCmdProc TestNREUnwind;
330 static Tcl_ObjCmdProc TestNRELevels;
331 static Tcl_ObjCmdProc TestInterpResolverCmd;
332 #if defined(HAVE_CPUID) && !defined(MAC_OSX_TCL)
333 static Tcl_ObjCmdProc TestcpuidCmd;
334 #endif
335 static Tcl_ObjCmdProc TestApplyLambdaObjCmd;
336
337 static const Tcl_Filesystem testReportingFilesystem = {
338 "reporting",
339 sizeof(Tcl_Filesystem),
340 TCL_FILESYSTEM_VERSION_1,
341 TestReportInFilesystem, /* path in */
342 TestReportDupInternalRep,
343 TestReportFreeInternalRep,
344 NULL, /* native to norm */
345 NULL, /* convert to native */
346 TestReportNormalizePath,
347 NULL, /* path type */
348 NULL, /* separator */
349 TestReportStat,
350 TestReportAccess,
351 TestReportOpenFileChannel,
352 TestReportMatchInDirectory,
353 TestReportUtime,
354 TestReportLink,
355 NULL /* list volumes */,
356 TestReportFileAttrStrings,
357 TestReportFileAttrsGet,
358 TestReportFileAttrsSet,
359 TestReportCreateDirectory,
360 TestReportRemoveDirectory,
361 TestReportDeleteFile,
362 TestReportCopyFile,
363 TestReportRenameFile,
364 TestReportCopyDirectory,
365 TestReportLstat,
366 (Tcl_FSLoadFileProc *) TestReportLoadFile,
367 NULL /* cwd */,
368 TestReportChdir
369 };
370
371 static const Tcl_Filesystem simpleFilesystem = {
372 "simple",
373 sizeof(Tcl_Filesystem),
374 TCL_FILESYSTEM_VERSION_1,
375 SimplePathInFilesystem,
376 NULL,
377 NULL,
378 /* No internal to normalized, since we don't create any
379 * pure 'internal' Tcl_Obj path representations */
380 NULL,
381 /* No create native rep function, since we don't use it
382 * or 'Tcl_FSNewNativePath' */
383 NULL,
384 /* Normalize path isn't needed - we assume paths only have
385 * one representation */
386 NULL,
387 NULL,
388 NULL,
389 SimpleStat,
390 SimpleAccess,
391 SimpleOpenFileChannel,
392 SimpleMatchInDirectory,
393 NULL,
394 /* We choose not to support symbolic links inside our vfs's */
395 NULL,
396 SimpleListVolumes,
397 NULL,
398 NULL,
399 NULL,
400 NULL,
401 NULL,
402 NULL,
403 /* No copy file - fallback will occur at Tcl level */
404 NULL,
405 /* No rename file - fallback will occur at Tcl level */
406 NULL,
407 /* No copy directory - fallback will occur at Tcl level */
408 NULL,
409 /* Use stat for lstat */
410 NULL,
411 /* No load - fallback on core implementation */
412 NULL,
413 /* We don't need a getcwd or chdir - fallback on Tcl's versions */
414 NULL,
415 NULL
416 };
417
418
419 /*
420 *----------------------------------------------------------------------
421 *
422 * Tcltest_Init --
423 *
424 * This procedure performs application-specific initialization. Most
425 * applications, especially those that incorporate additional packages,
426 * will have their own version of this procedure.
427 *
428 * Results:
429 * Returns a standard Tcl completion code, and leaves an error message in
430 * the interp's result if an error occurs.
431 *
432 * Side effects:
433 * Depends on the startup script.
434 *
435 *----------------------------------------------------------------------
436 */
437
438 int
439 Tcltest_Init(
440 Tcl_Interp *interp) /* Interpreter for application. */
441 {
442 Tcl_ValueType t3ArgTypes[2];
443
444 Tcl_Obj **objv, *objPtr;
445 int objc, index;
446 static const char *const specialOptions[] = {
447 "-appinitprocerror", "-appinitprocdeleteinterp",
448 "-appinitprocclosestderr", "-appinitprocsetrcfile", NULL
449 };
450
451 if (Tcl_InitStubs(interp, "8.5-", 0) == NULL) {
452 return TCL_ERROR;
453 }
454 if (Tcl_TomMath_InitStubs(interp, "8.5") == NULL) {
455 return TCL_ERROR;
456 }
457 if (Tcl_OOInitStubs(interp) == NULL) {
458 return TCL_ERROR;
459 }
460 /* TIP #268: Full patchlevel instead of just major.minor */
461
462 if (Tcl_PkgProvide(interp, "Tcltest", TCL_PATCH_LEVEL) == TCL_ERROR) {
463 return TCL_ERROR;
464 }
465
466 /*
467 * Create additional commands and math functions for testing Tcl.
468 */
469
470 Tcl_CreateObjCommand(interp, "gettimes", GetTimesObjCmd, NULL, NULL);
471 Tcl_CreateCommand(interp, "noop", NoopCmd, NULL, NULL);
472 Tcl_CreateObjCommand(interp, "noop", NoopObjCmd, NULL, NULL);
473 Tcl_CreateObjCommand(interp, "testpurebytesobj", TestpurebytesobjObjCmd, NULL, NULL);
474 Tcl_CreateObjCommand(interp, "testsetbytearraylength", TestsetbytearraylengthObjCmd, NULL, NULL);
475 Tcl_CreateObjCommand(interp, "testbytestring", TestbytestringObjCmd, NULL, NULL);
476 Tcl_CreateObjCommand(interp, "teststringbytes", TeststringbytesObjCmd, NULL, NULL);
477 Tcl_CreateObjCommand(interp, "testwrongnumargs", TestWrongNumArgsObjCmd,
478 NULL, NULL);
479 Tcl_CreateObjCommand(interp, "testfilesystem", TestFilesystemObjCmd,
480 NULL, NULL);
481 Tcl_CreateObjCommand(interp, "testsimplefilesystem", TestSimpleFilesystemObjCmd,
482 NULL, NULL);
483 Tcl_CreateObjCommand(interp, "testgetindexfromobjstruct",
484 TestGetIndexFromObjStructObjCmd, NULL, NULL);
485 Tcl_CreateCommand(interp, "testasync", TestasyncCmd, NULL, NULL);
486 Tcl_CreateObjCommand(interp, "testbumpinterpepoch",
487 TestbumpinterpepochObjCmd, NULL, NULL);
488 Tcl_CreateCommand(interp, "testchannel", TestChannelCmd,
489 NULL, NULL);
490 Tcl_CreateCommand(interp, "testchannelevent", TestChannelEventCmd,
491 NULL, NULL);
492 Tcl_CreateCommand(interp, "testcmdtoken", TestcmdtokenCmd, NULL,
493 NULL);
494 Tcl_CreateCommand(interp, "testcmdinfo", TestcmdinfoCmd, NULL,
495 NULL);
496 Tcl_CreateCommand(interp, "testcmdtrace", TestcmdtraceCmd,
497 NULL, NULL);
498 Tcl_CreateCommand(interp, "testconcatobj", TestconcatobjCmd,
499 NULL, NULL);
500 Tcl_CreateCommand(interp, "testcreatecommand", TestcreatecommandCmd,
501 NULL, NULL);
502 Tcl_CreateCommand(interp, "testdcall", TestdcallCmd, NULL, NULL);
503 Tcl_CreateCommand(interp, "testdel", TestdelCmd, NULL, NULL);
504 Tcl_CreateCommand(interp, "testdelassocdata", TestdelassocdataCmd,
505 NULL, NULL);
506 Tcl_CreateObjCommand(interp, "testdoubledigits", TestdoubledigitsObjCmd,
507 NULL, NULL);
508 Tcl_DStringInit(&dstring);
509 Tcl_CreateCommand(interp, "testdstring", TestdstringCmd, NULL,
510 NULL);
511 Tcl_CreateObjCommand(interp, "testencoding", TestencodingObjCmd, NULL,
512 NULL);
513 Tcl_CreateObjCommand(interp, "testevalex", TestevalexObjCmd,
514 NULL, NULL);
515 Tcl_CreateObjCommand(interp, "testevalobjv", TestevalobjvObjCmd,
516 NULL, NULL);
517 Tcl_CreateObjCommand(interp, "testevent", TesteventObjCmd,
518 NULL, NULL);
519 Tcl_CreateCommand(interp, "testexithandler", TestexithandlerCmd,
520 NULL, NULL);
521 Tcl_CreateCommand(interp, "testexprlong", TestexprlongCmd,
522 NULL, NULL);
523 Tcl_CreateObjCommand(interp, "testexprlongobj", TestexprlongobjCmd,
524 NULL, NULL);
525 Tcl_CreateCommand(interp, "testexprdouble", TestexprdoubleCmd,
526 NULL, NULL);
527 Tcl_CreateObjCommand(interp, "testexprdoubleobj", TestexprdoubleobjCmd,
528 NULL, NULL);
529 Tcl_CreateObjCommand(interp, "testexprparser", TestexprparserObjCmd,
530 NULL, NULL);
531 Tcl_CreateCommand(interp, "testexprstring", TestexprstringCmd,
532 NULL, NULL);
533 Tcl_CreateCommand(interp, "testfevent", TestfeventCmd, NULL,
534 NULL);
535 Tcl_CreateObjCommand(interp, "testfilelink", TestfilelinkCmd,
536 NULL, NULL);
537 Tcl_CreateObjCommand(interp, "testfile", TestfileCmd,
538 NULL, NULL);
539 Tcl_CreateObjCommand(interp, "testhashsystemhash",
540 TestHashSystemHashCmd, NULL, NULL);
541 Tcl_CreateCommand(interp, "testgetassocdata", TestgetassocdataCmd,
542 NULL, NULL);
543 Tcl_CreateCommand(interp, "testgetint", TestgetintCmd,
544 NULL, NULL);
545 Tcl_CreateCommand(interp, "testgetplatform", TestgetplatformCmd,
546 NULL, NULL);
547 Tcl_CreateObjCommand(interp, "testgetvarfullname",
548 TestgetvarfullnameCmd, NULL, NULL);
549 Tcl_CreateCommand(interp, "testinterpdelete", TestinterpdeleteCmd,
550 NULL, NULL);
551 Tcl_CreateCommand(interp, "testlink", TestlinkCmd, NULL, NULL);
552 Tcl_CreateObjCommand(interp, "testlocale", TestlocaleCmd, NULL,
553 NULL);
554 Tcl_CreateCommand(interp, "testpanic", TestpanicCmd, NULL, NULL);
555 Tcl_CreateObjCommand(interp, "testparseargs", TestparseargsCmd,NULL,NULL);
556 Tcl_CreateObjCommand(interp, "testparser", TestparserObjCmd,
557 NULL, NULL);
558 Tcl_CreateObjCommand(interp, "testparsevar", TestparsevarObjCmd,
559 NULL, NULL);
560 Tcl_CreateObjCommand(interp, "testparsevarname", TestparsevarnameObjCmd,
561 NULL, NULL);
562 Tcl_CreateObjCommand(interp, "testprint", TestprintObjCmd,
563 NULL, NULL);
564 Tcl_CreateObjCommand(interp, "testregexp", TestregexpObjCmd,
565 NULL, NULL);
566 Tcl_CreateObjCommand(interp, "testreturn", TestreturnObjCmd,
567 NULL, NULL);
568 Tcl_CreateObjCommand(interp, "testsaveresult", TestsaveresultCmd,
569 NULL, NULL);
570 Tcl_CreateCommand(interp, "testservicemode", TestServiceModeCmd,
571 NULL, NULL);
572 Tcl_CreateCommand(interp, "testsetassocdata", TestsetassocdataCmd,
573 NULL, NULL);
574 Tcl_CreateCommand(interp, "testsetnoerr", TestsetCmd,
575 NULL, NULL);
576 Tcl_CreateCommand(interp, "testseterr", TestsetCmd,
577 INT2PTR(TCL_LEAVE_ERR_MSG), NULL);
578 Tcl_CreateCommand(interp, "testset2", Testset2Cmd,
579 INT2PTR(TCL_LEAVE_ERR_MSG), NULL);
580 Tcl_CreateCommand(interp, "testseterrorcode", TestseterrorcodeCmd,
581 NULL, NULL);
582 Tcl_CreateObjCommand(interp, "testsetobjerrorcode",
583 TestsetobjerrorcodeCmd, NULL, NULL);
584 Tcl_CreateObjCommand(interp, "testutfnext",
585 TestUtfNextCmd, NULL, NULL);
586 Tcl_CreateObjCommand(interp, "testutfprev",
587 TestUtfPrevCmd, NULL, NULL);
588 Tcl_CreateObjCommand(interp, "testnumutfchars",
589 TestNumUtfCharsCmd, NULL, NULL);
590 Tcl_CreateObjCommand(interp, "testgetunichar",
591 TestGetUniCharCmd, NULL, NULL);
592 Tcl_CreateObjCommand(interp, "testfindfirst",
593 TestFindFirstCmd, NULL, NULL);
594 Tcl_CreateObjCommand(interp, "testfindlast",
595 TestFindLastCmd, NULL, NULL);
596 Tcl_CreateCommand(interp, "testsetplatform", TestsetplatformCmd,
597 NULL, NULL);
598 Tcl_CreateObjCommand(interp, "testsize", TestSizeCmd, NULL, NULL);
599 Tcl_CreateCommand(interp, "teststaticpkg", TeststaticpkgCmd,
600 NULL, NULL);
601 Tcl_CreateCommand(interp, "testtranslatefilename",
602 TesttranslatefilenameCmd, NULL, NULL);
603 Tcl_CreateCommand(interp, "testupvar", TestupvarCmd, NULL, NULL);
604 Tcl_CreateMathFunc(interp, "T1", 0, NULL, TestMathFunc, (ClientData)123);
605 Tcl_CreateMathFunc(interp, "T2", 0, NULL, TestMathFunc, (ClientData)345);
606 Tcl_CreateCommand(interp, "testmainthread", TestmainthreadCmd, NULL,
607 NULL);
608 Tcl_CreateCommand(interp, "testsetmainloop", TestsetmainloopCmd,
609 NULL, NULL);
610 Tcl_CreateCommand(interp, "testexitmainloop", TestexitmainloopCmd,
611 NULL, NULL);
612 #if defined(HAVE_CPUID) && !defined(MAC_OSX_TCL)
613 Tcl_CreateObjCommand(interp, "testcpuid", TestcpuidCmd,
614 NULL, NULL);
615 #endif
616 t3ArgTypes[0] = TCL_EITHER;
617 t3ArgTypes[1] = TCL_EITHER;
618 Tcl_CreateMathFunc(interp, "T3", 2, t3ArgTypes, TestMathFunc2,
619 NULL);
620
621 Tcl_CreateObjCommand(interp, "testnreunwind", TestNREUnwind,
622 NULL, NULL);
623 Tcl_CreateObjCommand(interp, "testnrelevels", TestNRELevels,
624 NULL, NULL);
625 Tcl_CreateObjCommand(interp, "testinterpresolver", TestInterpResolverCmd,
626 NULL, NULL);
627 Tcl_CreateObjCommand(interp, "testapplylambda", TestApplyLambdaObjCmd,
628 NULL, NULL);
629
630 if (TclObjTest_Init(interp) != TCL_OK) {
631 return TCL_ERROR;
632 }
633 if (Procbodytest_Init(interp) != TCL_OK) {
634 return TCL_ERROR;
635 }
636 #ifdef TCL_THREADS
637 if (TclThread_Init(interp) != TCL_OK) {
638 return TCL_ERROR;
639 }
640 #endif
641
642 /*
643 * Check for special options used in ../tests/main.test
644 */
645
646 objPtr = Tcl_GetVar2Ex(interp, "argv", NULL, TCL_GLOBAL_ONLY);
647 if (objPtr != NULL) {
648 if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv) != TCL_OK) {
649 return TCL_ERROR;
650 }
651 if (objc && (Tcl_GetIndexFromObj(NULL, objv[0], specialOptions, NULL,
652 TCL_EXACT, &index) == TCL_OK)) {
653 switch (index) {
654 case 0:
655 return TCL_ERROR;
656 case 1:
657 Tcl_DeleteInterp(interp);
658 return TCL_ERROR;
659 case 2: {
660 int mode;
661 Tcl_UnregisterChannel(interp,
662 Tcl_GetChannel(interp, "stderr", &mode));
663 return TCL_ERROR;
664 }
665 case 3:
666 if (objc-1) {
667 Tcl_SetVar2Ex(interp, "tcl_rcFileName", NULL, objv[1],
668 TCL_GLOBAL_ONLY);
669 }
670 return TCL_ERROR;
671 }
672 }
673 }
674
675 /*
676 * And finally add any platform specific test commands.
677 */
678
679 return TclplatformtestInit(interp);
680 }
681
682 /*
683 *----------------------------------------------------------------------
684 *
685 * Tcltest_SafeInit --
686 *
687 * This procedure performs application-specific initialization. Most
688 * applications, especially those that incorporate additional packages,
689 * will have their own version of this procedure.
690 *
691 * Results:
692 * Returns a standard Tcl completion code, and leaves an error message in
693 * the interp's result if an error occurs.
694 *
695 * Side effects:
696 * Depends on the startup script.
697 *
698 *----------------------------------------------------------------------
699 */
700
701 int
702 Tcltest_SafeInit(
703 Tcl_Interp *interp) /* Interpreter for application. */
704 {
705 if (Tcl_InitStubs(interp, "8.5-", 0) == NULL) {
706 return TCL_ERROR;
707 }
708 return Procbodytest_SafeInit(interp);
709 }
710
711 /*
712 *----------------------------------------------------------------------
713 *
714 * TestasyncCmd --
715 *
716 * This procedure implements the "testasync" command. It is used
717 * to test the asynchronous handler facilities of Tcl.
718 *
719 * Results:
720 * A standard Tcl result.
721 *
722 * Side effects:
723 * Creates, deletes, and invokes handlers.
724 *
725 *----------------------------------------------------------------------
726 */
727
728 static int
729 TestasyncCmd(
730 ClientData dummy, /* Not used. */
731 Tcl_Interp *interp, /* Current interpreter. */
732 int argc, /* Number of arguments. */
733 const char **argv) /* Argument strings. */
734 {
735 TestAsyncHandler *asyncPtr, *prevPtr;
736 int id, code;
737 static int nextId = 1;
738 (void)dummy;
739
740 if (argc < 2) {
741 wrongNumArgs:
742 Tcl_AppendResult(interp, "wrong # args", NULL);
743 return TCL_ERROR;
744 }
745 if (strcmp(argv[1], "create") == 0) {
746 if (argc != 3) {
747 goto wrongNumArgs;
748 }
749 asyncPtr = (TestAsyncHandler *)ckalloc(sizeof(TestAsyncHandler));
750 asyncPtr->command = (char *)ckalloc(strlen(argv[2]) + 1);
751 strcpy(asyncPtr->command, argv[2]);
752 Tcl_MutexLock(&asyncTestMutex);
753 asyncPtr->id = nextId;
754 nextId++;
755 asyncPtr->handler = Tcl_AsyncCreate(AsyncHandlerProc,
756 INT2PTR(asyncPtr->id));
757 asyncPtr->nextPtr = firstHandler;
758 firstHandler = asyncPtr;
759 Tcl_MutexUnlock(&asyncTestMutex);
760 Tcl_SetObjResult(interp, Tcl_NewIntObj(asyncPtr->id));
761 } else if (strcmp(argv[1], "delete") == 0) {
762 if (argc == 2) {
763 Tcl_MutexLock(&asyncTestMutex);
764 while (firstHandler != NULL) {
765 asyncPtr = firstHandler;
766 firstHandler = asyncPtr->nextPtr;
767 Tcl_AsyncDelete(asyncPtr->handler);
768 ckfree(asyncPtr->command);
769 ckfree(asyncPtr);
770 }
771 Tcl_MutexUnlock(&asyncTestMutex);
772 return TCL_OK;
773 }
774 if (argc != 3) {
775 goto wrongNumArgs;
776 }
777 if (Tcl_GetInt(interp, argv[2], &id) != TCL_OK) {
778 return TCL_ERROR;
779 }
780 Tcl_MutexLock(&asyncTestMutex);
781 for (prevPtr = NULL, asyncPtr = firstHandler; asyncPtr != NULL;
782 prevPtr = asyncPtr, asyncPtr = asyncPtr->nextPtr) {
783 if (asyncPtr->id != id) {
784 continue;
785 }
786 if (prevPtr == NULL) {
787 firstHandler = asyncPtr->nextPtr;
788 } else {
789 prevPtr->nextPtr = asyncPtr->nextPtr;
790 }
791 Tcl_AsyncDelete(asyncPtr->handler);
792 ckfree(asyncPtr->command);
793 ckfree(asyncPtr);
794 break;
795 }
796 Tcl_MutexUnlock(&asyncTestMutex);
797 } else if (strcmp(argv[1], "mark") == 0) {
798 if (argc != 5) {
799 goto wrongNumArgs;
800 }
801 if ((Tcl_GetInt(interp, argv[2], &id) != TCL_OK)
802 || (Tcl_GetInt(interp, argv[4], &code) != TCL_OK)) {
803 return TCL_ERROR;
804 }
805 Tcl_MutexLock(&asyncTestMutex);
806 for (asyncPtr = firstHandler; asyncPtr != NULL;
807 asyncPtr = asyncPtr->nextPtr) {
808 if (asyncPtr->id == id) {
809 Tcl_AsyncMark(asyncPtr->handler);
810 break;
811 }
812 }
813 Tcl_SetObjResult(interp, Tcl_NewStringObj(argv[3], -1));
814 Tcl_MutexUnlock(&asyncTestMutex);
815 return code;
816 #ifdef TCL_THREADS
817 } else if (strcmp(argv[1], "marklater") == 0) {
818 if (argc != 3) {
819 goto wrongNumArgs;
820 }
821 if (Tcl_GetInt(interp, argv[2], &id) != TCL_OK) {
822 return TCL_ERROR;
823 }
824 Tcl_MutexLock(&asyncTestMutex);
825 for (asyncPtr = firstHandler; asyncPtr != NULL;
826 asyncPtr = asyncPtr->nextPtr) {
827 if (asyncPtr->id == id) {
828 Tcl_ThreadId threadID;
829 if (Tcl_CreateThread(&threadID, AsyncThreadProc,
830 INT2PTR(id), TCL_THREAD_STACK_DEFAULT,
831 TCL_THREAD_NOFLAGS) != TCL_OK) {
832 Tcl_AppendResult(interp, "can't create thread", NULL);
833 Tcl_MutexUnlock(&asyncTestMutex);
834 return TCL_ERROR;
835 }
836 break;
837 }
838 }
839 Tcl_MutexUnlock(&asyncTestMutex);
840 } else {
841 Tcl_AppendResult(interp, "bad option \"", argv[1],
842 "\": must be create, delete, int, mark, or marklater", NULL);
843 return TCL_ERROR;
844 #else /* !TCL_THREADS */
845 } else {
846 Tcl_AppendResult(interp, "bad option \"", argv[1],
847 "\": must be create, delete, int, or mark", NULL);
848 return TCL_ERROR;
849 #endif
850 }
851 return TCL_OK;
852 }
853
854 static int
855 AsyncHandlerProc(
856 ClientData clientData, /* If of TestAsyncHandler structure.
857 * in global list. */
858 Tcl_Interp *interp, /* Interpreter in which command was
859 * executed, or NULL. */
860 int code) /* Current return code from command. */
861 {
862 TestAsyncHandler *asyncPtr;
863 int id = PTR2INT(clientData);
864 const char *listArgv[4], *cmd;
865 char string[TCL_INTEGER_SPACE];
866
867 Tcl_MutexLock(&asyncTestMutex);
868 for (asyncPtr = firstHandler; asyncPtr != NULL;
869 asyncPtr = asyncPtr->nextPtr) {
870 if (asyncPtr->id == id) {
871 break;
872 }
873 }
874 Tcl_MutexUnlock(&asyncTestMutex);
875
876 if (!asyncPtr) {
877 /* Woops - this one was deleted between the AsyncMark and now */
878 return TCL_OK;
879 }
880
881 TclFormatInt(string, code);
882 listArgv[0] = asyncPtr->command;
883 listArgv[1] = Tcl_GetString(Tcl_GetObjResult(interp));
884 listArgv[2] = string;
885 listArgv[3] = NULL;
886 cmd = Tcl_Merge(3, listArgv);
887 if (interp != NULL) {
888 code = Tcl_EvalEx(interp, cmd, -1, 0);
889 } else {
890 /*
891 * this should not happen, but by definition of how async handlers are
892 * invoked, it's possible. Better error checking is needed here.
893 */
894 }
895 ckfree(cmd);
896 return code;
897 }
898
899 /*
900 *----------------------------------------------------------------------
901 *
902 * AsyncThreadProc --
903 *
904 * Delivers an asynchronous event to a handler in another thread.
905 *
906 * Results:
907 * None.
908 *
909 * Side effects:
910 * Invokes Tcl_AsyncMark on the handler
911 *
912 *----------------------------------------------------------------------
913 */
914
915 #ifdef TCL_THREADS
916 static Tcl_ThreadCreateType
917 AsyncThreadProc(
918 ClientData clientData) /* Parameter is the id of a
919 * TestAsyncHandler, defined above. */
920 {
921 TestAsyncHandler *asyncPtr;
922 int id = PTR2INT(clientData);
923
924 Tcl_Sleep(1);
925 Tcl_MutexLock(&asyncTestMutex);
926 for (asyncPtr = firstHandler; asyncPtr != NULL;
927 asyncPtr = asyncPtr->nextPtr) {
928 if (asyncPtr->id == id) {
929 Tcl_AsyncMark(asyncPtr->handler);
930 break;
931 }
932 }
933 Tcl_MutexUnlock(&asyncTestMutex);
934 Tcl_ExitThread(TCL_OK);
935 TCL_THREAD_CREATE_RETURN;
936 }
937 #endif
938
939 static int
940 TestbumpinterpepochObjCmd(
941 ClientData dummy, /* Not used. */
942 Tcl_Interp *interp, /* Current interpreter. */
943 int objc, /* Number of arguments. */
944 Tcl_Obj *const objv[]) /* Argument objects. */
945 {
946 Interp *iPtr = (Interp *)interp;
947
948 if (objc != 1) {
949 Tcl_WrongNumArgs(interp, 1, objv, "");
950 return TCL_ERROR;
951 }
952 iPtr->compileEpoch++;
953 return TCL_OK;
954 }
955
956 /*
957 *----------------------------------------------------------------------
958 *
959 * TestcmdinfoCmd --
960 *
961 * This procedure implements the "testcmdinfo" command. It is used to
962 * test Tcl_GetCommandInfo, Tcl_SetCommandInfo, and command creation and
963 * deletion.
964 *
965 * Results:
966 * A standard Tcl result.
967 *
968 * Side effects:
969 * Creates and deletes various commands and modifies their data.
970 *
971 *----------------------------------------------------------------------
972 */
973
974 static int
975 TestcmdinfoCmd(
976 ClientData dummy, /* Not used. */
977 Tcl_Interp *interp, /* Current interpreter. */
978 int argc, /* Number of arguments. */
979 const char **argv) /* Argument strings. */
980 {
981 Tcl_CmdInfo info;
982 (void)dummy;
983
984 if (argc != 3) {
985 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
986 " option cmdName\"", NULL);
987 return TCL_ERROR;
988 }
989 if (strcmp(argv[1], "create") == 0) {
990 Tcl_CreateCommand(interp, argv[2], CmdProc1, (ClientData)"original",
991 CmdDelProc1);
992 } else if (strcmp(argv[1], "delete") == 0) {
993 Tcl_DStringInit(&delString);
994 Tcl_DeleteCommand(interp, argv[2]);
995 Tcl_DStringResult(interp, &delString);
996 } else if (strcmp(argv[1], "get") == 0) {
997 if (Tcl_GetCommandInfo(interp, argv[2], &info) ==0) {
998 Tcl_AppendResult(interp, "??", NULL);
999 return TCL_OK;
1000 }
1001 if (info.proc == CmdProc1) {
1002 Tcl_AppendResult(interp, "CmdProc1", " ",
1003 (char *) info.clientData, NULL);
1004 } else if (info.proc == CmdProc2) {
1005 Tcl_AppendResult(interp, "CmdProc2", " ",
1006 (char *) info.clientData, NULL);
1007 } else {
1008 Tcl_AppendResult(interp, "unknown", NULL);
1009 }
1010 if (info.deleteProc == CmdDelProc1) {
1011 Tcl_AppendResult(interp, " CmdDelProc1", " ",
1012 (char *) info.deleteData, NULL);
1013 } else if (info.deleteProc == CmdDelProc2) {
1014 Tcl_AppendResult(interp, " CmdDelProc2", " ",
1015 (char *) info.deleteData, NULL);
1016 } else {
1017 Tcl_AppendResult(interp, " unknown", NULL);
1018 }
1019 Tcl_AppendResult(interp, " ", info.namespacePtr->fullName, NULL);
1020 if (info.isNativeObjectProc) {
1021 Tcl_AppendResult(interp, " nativeObjectProc", NULL);
1022 } else {
1023 Tcl_AppendResult(interp, " stringProc", NULL);
1024 }
1025 } else if (strcmp(argv[1], "modify") == 0) {
1026 info.proc = CmdProc2;
1027 info.clientData = (ClientData)"new_command_data";
1028 info.objProc = NULL;
1029 info.objClientData = NULL;
1030 info.deleteProc = CmdDelProc2;
1031 info.deleteData = (ClientData)"new_delete_data";
1032 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(Tcl_SetCommandInfo(interp, argv[2], &info)));
1033 } else {
1034 Tcl_AppendResult(interp, "bad option \"", argv[1],
1035 "\": must be create, delete, get, or modify", NULL);
1036 return TCL_ERROR;
1037 }
1038 return TCL_OK;
1039 }
1040
1041 static int
1042 CmdProc1(
1043 ClientData clientData, /* String to return. */
1044 Tcl_Interp *interp, /* Current interpreter. */
1045 int argc, /* Number of arguments. */
1046 const char **argv) /* Argument strings. */
1047 {
1048 Tcl_AppendResult(interp, "CmdProc1 ", (char *) clientData, NULL);
1049 return TCL_OK;
1050 }
1051
1052 static int
1053 CmdProc2(
1054 ClientData clientData, /* String to return. */
1055 Tcl_Interp *interp, /* Current interpreter. */
1056 int argc, /* Number of arguments. */
1057 const char **argv) /* Argument strings. */
1058 {
1059 Tcl_AppendResult(interp, "CmdProc2 ", (char *) clientData, NULL);
1060 return TCL_OK;
1061 }
1062
1063 static void
1064 CmdDelProc1(
1065 ClientData clientData) /* String to save. */
1066 {
1067 Tcl_DStringInit(&delString);
1068 Tcl_DStringAppend(&delString, "CmdDelProc1 ", -1);
1069 Tcl_DStringAppend(&delString, (char *) clientData, -1);
1070 }
1071
1072 static void
1073 CmdDelProc2(
1074 ClientData clientData) /* String to save. */
1075 {
1076 Tcl_DStringInit(&delString);
1077 Tcl_DStringAppend(&delString, "CmdDelProc2 ", -1);
1078 Tcl_DStringAppend(&delString, (char *) clientData, -1);
1079 }
1080
1081 /*
1082 *----------------------------------------------------------------------
1083 *
1084 * TestcmdtokenCmd --
1085 *
1086 * This procedure implements the "testcmdtoken" command. It is used to
1087 * test Tcl_Command tokens and procedures such as Tcl_GetCommandFullName.
1088 *
1089 * Results:
1090 * A standard Tcl result.
1091 *
1092 * Side effects:
1093 * Creates and deletes various commands and modifies their data.
1094 *
1095 *----------------------------------------------------------------------
1096 */
1097
1098 static int
1099 TestcmdtokenCmd(
1100 ClientData dummy, /* Not used. */
1101 Tcl_Interp *interp, /* Current interpreter. */
1102 int argc, /* Number of arguments. */
1103 const char **argv) /* Argument strings. */
1104 {
1105 Tcl_Command token;
1106 int *l;
1107 char buf[30];
1108
1109 if (argc != 3) {
1110 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1111 " option arg\"", NULL);
1112 return TCL_ERROR;
1113 }
1114 if (strcmp(argv[1], "create") == 0) {
1115 token = Tcl_CreateCommand(interp, argv[2], CmdProc1,
1116 (ClientData)"original", NULL);
1117 snprintf(buf, sizeof(buf), "%p", (void *)token);
1118 Tcl_SetResult(interp, buf, TCL_VOLATILE);
1119 } else if (strcmp(argv[1], "name") == 0) {
1120 Tcl_Obj *objPtr;
1121
1122 if (sscanf(argv[2], "%p", &l) != 1) {
1123 Tcl_AppendResult(interp, "bad command token \"", argv[2],
1124 "\"", NULL);
1125 return TCL_ERROR;
1126 }
1127
1128 objPtr = Tcl_NewObj();
1129 Tcl_GetCommandFullName(interp, (Tcl_Command) l, objPtr);
1130
1131 Tcl_AppendElement(interp,
1132 Tcl_GetCommandName(interp, (Tcl_Command) l));
1133 Tcl_AppendElement(interp, Tcl_GetString(objPtr));
1134 Tcl_DecrRefCount(objPtr);
1135 } else {
1136 Tcl_AppendResult(interp, "bad option \"", argv[1],
1137 "\": must be create or name", NULL);
1138 return TCL_ERROR;
1139 }
1140 return TCL_OK;
1141 }
1142
1143 /*
1144 *----------------------------------------------------------------------
1145 *
1146 * TestcmdtraceCmd --
1147 *
1148 * This procedure implements the "testcmdtrace" command. It is used
1149 * to test Tcl_CreateTrace and Tcl_DeleteTrace.
1150 *
1151 * Results:
1152 * A standard Tcl result.
1153 *
1154 * Side effects:
1155 * Creates and deletes a command trace, and tests the invocation of
1156 * a procedure by the command trace.
1157 *
1158 *----------------------------------------------------------------------
1159 */
1160
1161 static int
1162 TestcmdtraceCmd(
1163 ClientData dummy, /* Not used. */
1164 Tcl_Interp *interp, /* Current interpreter. */
1165 int argc, /* Number of arguments. */
1166 const char **argv) /* Argument strings. */
1167 {
1168 Tcl_DString buffer;
1169 int result;
1170
1171 if (argc != 3) {
1172 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1173 " option script\"", NULL);
1174 return TCL_ERROR;
1175 }
1176
1177 if (strcmp(argv[1], "tracetest") == 0) {
1178 Tcl_DStringInit(&buffer);
1179 cmdTrace = Tcl_CreateTrace(interp, 50000, CmdTraceProc, &buffer);
1180 result = Tcl_EvalEx(interp, argv[2], -1, 0);
1181 if (result == TCL_OK) {
1182 Tcl_ResetResult(interp);
1183 Tcl_AppendResult(interp, Tcl_DStringValue(&buffer), NULL);
1184 }
1185 Tcl_DeleteTrace(interp, cmdTrace);
1186 Tcl_DStringFree(&buffer);
1187 } else if (strcmp(argv[1], "deletetest") == 0) {
1188 /*
1189 * Create a command trace then eval a script to check whether it is
1190 * called. Note that this trace procedure removes itself as a further
1191 * check of the robustness of the trace proc calling code in
1192 * TclNRExecuteByteCode.
1193 */
1194
1195 cmdTrace = Tcl_CreateTrace(interp, 50000, CmdTraceDeleteProc, NULL);
1196 Tcl_EvalEx(interp, argv[2], -1, 0);
1197 } else if (strcmp(argv[1], "leveltest") == 0) {
1198 Interp *iPtr = (Interp *) interp;
1199 Tcl_DStringInit(&buffer);
1200 cmdTrace = Tcl_CreateTrace(interp, iPtr->numLevels + 4, CmdTraceProc,
1201 &buffer);
1202 result = Tcl_EvalEx(interp, argv[2], -1, 0);
1203 if (result == TCL_OK) {
1204 Tcl_ResetResult(interp);
1205 Tcl_AppendResult(interp, Tcl_DStringValue(&buffer), NULL);
1206 }
1207 Tcl_DeleteTrace(interp, cmdTrace);
1208 Tcl_DStringFree(&buffer);
1209 } else if (strcmp(argv[1], "resulttest") == 0) {
1210 /* Create an object-based trace, then eval a script. This is used
1211 * to test return codes other than TCL_OK from the trace engine.
1212 */
1213
1214 static int deleteCalled;
1215
1216 deleteCalled = 0;
1217 cmdTrace = Tcl_CreateObjTrace(interp, 50000,
1218 TCL_ALLOW_INLINE_COMPILATION, ObjTraceProc,
1219 &deleteCalled, ObjTraceDeleteProc);
1220 result = Tcl_Eval(interp, argv[2]);
1221 Tcl_DeleteTrace(interp, cmdTrace);
1222 if (!deleteCalled) {
1223 Tcl_AppendResult(interp, "Delete wasn't called", NULL);
1224 return TCL_ERROR;
1225 } else {
1226 return result;
1227 }
1228 } else if (strcmp(argv[1], "doubletest") == 0) {
1229 Tcl_Trace t1, t2;
1230
1231 Tcl_DStringInit(&buffer);
1232 t1 = Tcl_CreateTrace(interp, 1, CmdTraceProc, &buffer);
1233 t2 = Tcl_CreateTrace(interp, 50000, CmdTraceProc, &buffer);
1234 result = Tcl_Eval(interp, argv[2]);
1235 if (result == TCL_OK) {
1236 Tcl_ResetResult(interp);
1237 Tcl_AppendResult(interp, Tcl_DStringValue(&buffer), NULL);
1238 }
1239 Tcl_DeleteTrace(interp, t2);
1240 Tcl_DeleteTrace(interp, t1);
1241 Tcl_DStringFree(&buffer);
1242 } else {
1243 Tcl_AppendResult(interp, "bad option \"", argv[1],
1244 "\": must be tracetest, deletetest, doubletest or resulttest", NULL);
1245 return TCL_ERROR;
1246 }
1247 return TCL_OK;
1248 }
1249
1250 static void
1251 CmdTraceProc(
1252 ClientData clientData, /* Pointer to buffer in which the
1253 * command and arguments are appended.
1254 * Accumulates test result. */
1255 Tcl_Interp *interp, /* Current interpreter. */
1256 int level, /* Current trace level. */
1257 char *command, /* The command being traced (after
1258 * substitutions). */
1259 Tcl_CmdProc *cmdProc, /* Points to command's command procedure. */
1260 ClientData cmdClientData, /* Client data associated with command
1261 * procedure. */
1262 int argc, /* Number of arguments. */
1263 const char *argv[]) /* Argument strings. */
1264 {
1265 Tcl_DString *bufPtr = (Tcl_DString *) clientData;
1266 int i;
1267
1268 Tcl_DStringAppendElement(bufPtr, command);
1269
1270 Tcl_DStringStartSublist(bufPtr);
1271 for (i = 0; i < argc; i++) {
1272 Tcl_DStringAppendElement(bufPtr, argv[i]);
1273 }
1274 Tcl_DStringEndSublist(bufPtr);
1275 }
1276
1277 static void
1278 CmdTraceDeleteProc(
1279 ClientData clientData, /* Unused. */
1280 Tcl_Interp *interp, /* Current interpreter. */
1281 int level, /* Current trace level. */
1282 char *command, /* The command being traced (after
1283 * substitutions). */
1284 Tcl_CmdProc *cmdProc, /* Points to command's command procedure. */
1285 ClientData cmdClientData, /* Client data associated with command
1286 * procedure. */
1287 int argc, /* Number of arguments. */
1288 const char *argv[]) /* Argument strings. */
1289 {
1290 /*
1291 * Remove ourselves to test whether calling Tcl_DeleteTrace within a trace
1292 * callback causes the for loop in TclNRExecuteByteCode that calls traces to
1293 * reference freed memory.
1294 */
1295
1296 Tcl_DeleteTrace(interp, cmdTrace);
1297 }
1298
1299 static int
1300 ObjTraceProc(
1301 ClientData clientData, /* unused */
1302 Tcl_Interp *interp, /* Tcl interpreter */
1303 int level, /* Execution level */
1304 const char *command, /* Command being executed */
1305 Tcl_Command token, /* Command information */
1306 int objc, /* Parameter count */
1307 Tcl_Obj *const objv[]) /* Parameter list */
1308 {
1309 const char *word = Tcl_GetString(objv[0]);
1310
1311 if (!strcmp(word, "Error")) {
1312 Tcl_SetObjResult(interp, Tcl_NewStringObj(command, -1));
1313 return TCL_ERROR;
1314 } else if (!strcmp(word, "Break")) {
1315 return TCL_BREAK;
1316 } else if (!strcmp(word, "Continue")) {
1317 return TCL_CONTINUE;
1318 } else if (!strcmp(word, "Return")) {
1319 return TCL_RETURN;
1320 } else if (!strcmp(word, "OtherStatus")) {
1321 return 6;
1322 } else {
1323 return TCL_OK;
1324 }
1325 }
1326
1327 static void
1328 ObjTraceDeleteProc(
1329 ClientData clientData)
1330 {
1331 int *intPtr = (int *) clientData;
1332 *intPtr = 1; /* Record that the trace was deleted */
1333 }
1334
1335 /*
1336 *----------------------------------------------------------------------
1337 *
1338 * TestcreatecommandCmd --
1339 *
1340 * This procedure implements the "testcreatecommand" command. It is used
1341 * to test that the Tcl_CreateCommand creates a new command in the
1342 * namespace specified as part of its name, if any. It also checks that
1343 * the namespace code ignore single ":"s in the middle or end of a
1344 * command name.
1345 *
1346 * Results:
1347 * A standard Tcl result.
1348 *
1349 * Side effects:
1350 * Creates and deletes two commands ("test_ns_basic::createdcommand"
1351 * and "value:at:").
1352 *
1353 *----------------------------------------------------------------------
1354 */
1355
1356 static int
1357 TestcreatecommandCmd(
1358 ClientData dummy, /* Not used. */
1359 Tcl_Interp *interp, /* Current interpreter. */
1360 int argc, /* Number of arguments. */
1361 const char **argv) /* Argument strings. */
1362 {
1363 if (argc != 2) {
1364 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
1365 " option\"", NULL);
1366 return TCL_ERROR;
1367 }
1368 if (strcmp(argv[1], "create") == 0) {
1369 Tcl_CreateCommand(interp, "test_ns_basic::createdcommand",
1370 CreatedCommandProc, NULL, NULL);
1371 } else if (strcmp(argv[1], "delete") == 0) {
1372 Tcl_DeleteCommand(interp, "test_ns_basic::createdcommand");
1373 } else if (strcmp(argv[1], "create2") == 0) {
1374 Tcl_CreateCommand(interp, "value:at:",
1375 CreatedCommandProc2, NULL, NULL);
1376 } else if (strcmp(argv[1], "delete2") == 0) {
1377 Tcl_DeleteCommand(interp, "value:at:");
1378 } else {
1379 Tcl_AppendResult(interp, "bad option \"", argv[1],
1380 "\": must be create, delete, create2, or delete2", NULL);
1381 return TCL_ERROR;
1382 }
1383 return TCL_OK;
1384 }
1385
1386 static int
1387 CreatedCommandProc(
1388 ClientData clientData, /* String to return. */
1389 Tcl_Interp *interp, /* Current interpreter. */
1390 int argc, /* Number of arguments. */
1391 const char **argv) /* Argument strings. */
1392 {
1393 Tcl_CmdInfo info;
1394 int found;
1395
1396 found = Tcl_GetCommandInfo(interp, "test_ns_basic::createdcommand",
1397 &info);
1398 if (!found) {
1399 Tcl_AppendResult(interp, "CreatedCommandProc could not get command info for test_ns_basic::createdcommand",
1400 (char *)NULL);
1401 return TCL_ERROR;
1402 }
1403 Tcl_AppendResult(interp, "CreatedCommandProc in ",
1404 info.namespacePtr->fullName, NULL);
1405 return TCL_OK;
1406 }
1407
1408 static int
1409 CreatedCommandProc2(
1410 ClientData clientData, /* String to return. */
1411 Tcl_Interp *interp, /* Current interpreter. */
1412 int argc, /* Number of arguments. */
1413 const char **argv) /* Argument strings. */
1414 {
1415 Tcl_CmdInfo info;
1416 int found;
1417
1418 found = Tcl_GetCommandInfo(interp, "value:at:", &info);
1419 if (!found) {
1420 Tcl_AppendResult(interp, "CreatedCommandProc2 could not get command info for test_ns_basic::createdcommand",
1421 (char *)NULL);
1422 return TCL_ERROR;
1423 }
1424 Tcl_AppendResult(interp, "CreatedCommandProc2 in ",
1425 info.namespacePtr->fullName, NULL);
1426 return TCL_OK;
1427 }
1428
1429 /*
1430 *----------------------------------------------------------------------
1431 *
1432 * TestdcallCmd --
1433 *
1434 * This procedure implements the "testdcall" command. It is used
1435 * to test Tcl_CallWhenDeleted.
1436 *
1437 * Results:
1438 * A standard Tcl result.
1439 *
1440 * Side effects:
1441 * Creates and deletes interpreters.
1442 *
1443 *----------------------------------------------------------------------
1444 */
1445
1446 static int
1447 TestdcallCmd(
1448 ClientData dummy, /* Not used. */
1449 Tcl_Interp *interp, /* Current interpreter. */
1450 int argc, /* Number of arguments. */
1451 const char **argv) /* Argument strings. */
1452 {
1453 int i, id;
1454
1455 delInterp = Tcl_CreateInterp();
1456 Tcl_DStringInit(&delString);
1457 for (i = 1; i < argc; i++) {
1458 if (Tcl_GetInt(interp, argv[i], &id) != TCL_OK) {
1459 return TCL_ERROR;
1460 }
1461 if (id < 0) {
1462 Tcl_DontCallWhenDeleted(delInterp, DelCallbackProc,
1463 INT2PTR(-id));
1464 } else {
1465 Tcl_CallWhenDeleted(delInterp, DelCallbackProc,
1466 INT2PTR(id));
1467 }
1468 }
1469 Tcl_DeleteInterp(delInterp);
1470 Tcl_DStringResult(interp, &delString);
1471 return TCL_OK;
1472 }
1473
1474 /*
1475 * The deletion callback used by TestdcallCmd:
1476 */
1477
1478 static void
1479 DelCallbackProc(
1480 ClientData clientData, /* Numerical value to append to delString. */
1481 Tcl_Interp *interp) /* Interpreter being deleted. */
1482 {
1483 int id = PTR2INT(clientData);
1484 char buffer[TCL_INTEGER_SPACE];
1485
1486 TclFormatInt(buffer, id);
1487 Tcl_DStringAppendElement(&delString, buffer);
1488 if (interp != delInterp) {
1489 Tcl_DStringAppendElement(&delString, "bogus interpreter argument!");
1490 }
1491 }
1492
1493 /*
1494 *----------------------------------------------------------------------
1495 *
1496 * TestdelCmd --
1497 *
1498 * This procedure implements the "testdel" command. It is used
1499 * to test calling of command deletion callbacks.
1500 *
1501 * Results:
1502 * A standard Tcl result.
1503 *
1504 * Side effects:
1505 * Creates a command.
1506 *
1507 *----------------------------------------------------------------------
1508 */
1509
1510 static int
1511 TestdelCmd(
1512 ClientData dummy, /* Not used. */
1513 Tcl_Interp *interp, /* Current interpreter. */
1514 int argc, /* Number of arguments. */
1515 const char **argv) /* Argument strings. */
1516 {
1517 DelCmd *dPtr;
1518 Tcl_Interp *child;
1519
1520 if (argc != 4) {
1521 Tcl_AppendResult(interp, "wrong # args", NULL);
1522 return TCL_ERROR;
1523 }
1524
1525 child = Tcl_GetChild(interp, argv[1]);
1526 if (child == NULL) {
1527 return TCL_ERROR;
1528 }
1529
1530 dPtr = (DelCmd *)ckalloc(sizeof(DelCmd));
1531 dPtr->interp = interp;
1532 dPtr->deleteCmd = (char *)ckalloc(strlen(argv[3]) + 1);
1533 strcpy(dPtr->deleteCmd, argv[3]);
1534
1535 Tcl_CreateCommand(child, argv[2], DelCmdProc, dPtr,
1536 DelDeleteProc);
1537 return TCL_OK;
1538 }
1539
1540 static int
1541 DelCmdProc(
1542 ClientData clientData, /* String result to return. */
1543 Tcl_Interp *interp, /* Current interpreter. */
1544 int argc, /* Number of arguments. */
1545 const char **argv) /* Argument strings. */
1546 {
1547 DelCmd *dPtr = (DelCmd *) clientData;
1548
1549 Tcl_AppendResult(interp, dPtr->deleteCmd, NULL);
1550 ckfree(dPtr->deleteCmd);
1551 ckfree(dPtr);
1552 return TCL_OK;
1553 }
1554
1555 static void
1556 DelDeleteProc(
1557 ClientData clientData) /* String command to evaluate. */
1558 {
1559 DelCmd *dPtr = (DelCmd *)clientData;
1560
1561 Tcl_Eval(dPtr->interp, dPtr->deleteCmd);
1562 Tcl_ResetResult(dPtr->interp);
1563 ckfree(dPtr->deleteCmd);
1564 ckfree(dPtr);
1565 }
1566
1567 /*
1568 *----------------------------------------------------------------------
1569 *
1570 * TestdelassocdataCmd --
1571 *
1572 * This procedure implements the "testdelassocdata" command. It is used
1573 * to test Tcl_DeleteAssocData.
1574 *
1575 * Results:
1576 * A standard Tcl result.
1577 *
1578 * Side effects:
1579 * Deletes an association between a key and associated data from an
1580 * interpreter.
1581 *
1582 *----------------------------------------------------------------------
1583 */
1584
1585 static int
1586 TestdelassocdataCmd(
1587 ClientData clientData, /* Not used. */
1588 Tcl_Interp *interp, /* Current interpreter. */
1589 int argc, /* Number of arguments. */
1590 const char **argv) /* Argument strings. */
1591 {
1592 if (argc != 2) {
1593 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
1594 " data_key\"", NULL);
1595 return TCL_ERROR;
1596 }
1597 Tcl_DeleteAssocData(interp, argv[1]);
1598 return TCL_OK;
1599 }
1600
1601 /*
1602 *-----------------------------------------------------------------------------
1603 *
1604 * TestdoubledigitsCmd --
1605 *
1606 * This procedure implements the 'testdoubledigits' command. It is
1607 * used to test the low-level floating-point formatting primitives
1608 * in Tcl.
1609 *
1610 * Usage:
1611 * testdoubledigits fpval ndigits type ?shorten"
1612 *
1613 * Parameters:
1614 * fpval - Floating-point value to format.
1615 * ndigits - Digit count to request from Tcl_DoubleDigits
1616 * type - One of 'shortest', 'Steele', 'e', 'f'
1617 * shorten - Indicates that the 'shorten' flag should be passed in.
1618 *
1619 *-----------------------------------------------------------------------------
1620 */
1621
1622 static int
1623 TestdoubledigitsObjCmd(
1624 ClientData unused, /* NULL */
1625 Tcl_Interp* interp, /* Tcl interpreter */
1626 int objc, /* Parameter count */
1627 Tcl_Obj* const objv[]) /* Parameter vector */
1628 {
1629 static const char *const options[] = {
1630 "shortest",
1631 "Steele",
1632 "e",
1633 "f",
1634 NULL
1635 };
1636 static const int types[] = {
1637 TCL_DD_SHORTEST,
1638 TCL_DD_STEELE,
1639 TCL_DD_E_FORMAT,
1640 TCL_DD_F_FORMAT
1641 };
1642
1643 const Tcl_ObjType* doubleType;
1644 double d;
1645 int status;
1646 int ndigits;
1647 int type;
1648 int decpt;
1649 int signum;
1650 char *str;
1651 char *endPtr;
1652 Tcl_Obj* strObj;
1653 Tcl_Obj* retval;
1654
1655 if (objc < 4 || objc > 5) {
1656 Tcl_WrongNumArgs(interp, 1, objv, "fpval ndigits type ?shorten?");
1657 return TCL_ERROR;
1658 }
1659 status = Tcl_GetDoubleFromObj(interp, objv[1], &d);
1660 if (status != TCL_OK) {
1661 doubleType = Tcl_GetObjType("double");
1662 if (objv[1]->typePtr == doubleType
1663 || TclIsNaN(objv[1]->internalRep.doubleValue)) {
1664 status = TCL_OK;
1665 memcpy(&d, &(objv[1]->internalRep.doubleValue), sizeof(double));
1666 }
1667 }
1668 if (status != TCL_OK
1669 || Tcl_GetIntFromObj(interp, objv[2], &ndigits) != TCL_OK
1670 || Tcl_GetIndexFromObj(interp, objv[3], options, "conversion type",
1671 TCL_EXACT, &type) != TCL_OK) {
1672 fprintf(stderr, "bad value? %g\n", d);
1673 return TCL_ERROR;
1674 }
1675 type = types[type];
1676 if (objc > 4) {
1677 if (strcmp(Tcl_GetString(objv[4]), "shorten")) {
1678 Tcl_SetObjResult(interp, Tcl_NewStringObj("bad flag", -1));
1679 return TCL_ERROR;
1680 }
1681 type |= TCL_DD_SHORTEN_FLAG;
1682 }
1683 str = TclDoubleDigits(d, ndigits, type, &decpt, &signum, &endPtr);
1684 strObj = Tcl_NewStringObj(str, endPtr-str);
1685 ckfree(str);
1686 retval = Tcl_NewListObj(1, &strObj);
1687 Tcl_ListObjAppendElement(NULL, retval, Tcl_NewIntObj(decpt));
1688 strObj = Tcl_NewStringObj(signum ? "-" : "+", 1);
1689 Tcl_ListObjAppendElement(NULL, retval, strObj);
1690 Tcl_SetObjResult(interp, retval);
1691 return TCL_OK;
1692 }
1693
1694 /*
1695 *----------------------------------------------------------------------
1696 *
1697 * TestdstringCmd --
1698 *
1699 * This procedure implements the "testdstring" command. It is used
1700 * to test the dynamic string facilities of Tcl.
1701 *
1702 * Results:
1703 * A standard Tcl result.
1704 *
1705 * Side effects:
1706 * Creates, deletes, and invokes handlers.
1707 *
1708 *----------------------------------------------------------------------
1709 */
1710
1711 static int
1712 TestdstringCmd(
1713 ClientData dummy, /* Not used. */
1714 Tcl_Interp *interp, /* Current interpreter. */
1715 int argc, /* Number of arguments. */
1716 const char **argv) /* Argument strings. */
1717 {
1718 int count;
1719
1720 if (argc < 2) {
1721 wrongNumArgs:
1722 Tcl_AppendResult(interp, "wrong # args", NULL);
1723 return TCL_ERROR;
1724 }
1725 if (strcmp(argv[1], "append") == 0) {
1726 if (argc != 4) {
1727 goto wrongNumArgs;
1728 }
1729 if (Tcl_GetInt(interp, argv[3], &count) != TCL_OK) {
1730 return TCL_ERROR;
1731 }
1732 Tcl_DStringAppend(&dstring, argv[2], count);
1733 } else if (strcmp(argv[1], "element") == 0) {
1734 if (argc != 3) {
1735 goto wrongNumArgs;
1736 }
1737 Tcl_DStringAppendElement(&dstring, argv[2]);
1738 } else if (strcmp(argv[1], "end") == 0) {
1739 if (argc != 2) {
1740 goto wrongNumArgs;
1741 }
1742 Tcl_DStringEndSublist(&dstring);
1743 } else if (strcmp(argv[1], "free") == 0) {
1744 if (argc != 2) {
1745 goto wrongNumArgs;
1746 }
1747 Tcl_DStringFree(&dstring);
1748 } else if (strcmp(argv[1], "get") == 0) {
1749 if (argc != 2) {
1750 goto wrongNumArgs;
1751 }
1752 Tcl_SetResult(interp, Tcl_DStringValue(&dstring), TCL_VOLATILE);
1753 } else if (strcmp(argv[1], "gresult") == 0) {
1754 if (argc != 3) {
1755 goto wrongNumArgs;
1756 }
1757 if (strcmp(argv[2], "staticsmall") == 0) {
1758 Tcl_AppendResult(interp, "short", NULL);
1759 } else if (strcmp(argv[2], "staticlarge") == 0) {
1760 Tcl_AppendResult(interp, "first0 first1 first2 first3 first4 first5 first6 first7 first8 first9\nsecond0 second1 second2 second3 second4 second5 second6 second7 second8 second9\nthird0 third1 third2 third3 third4 third5 third6 third7 third8 third9\nfourth0 fourth1 fourth2 fourth3 fourth4 fourth5 fourth6 fourth7 fourth8 fourth9\nfifth0 fifth1 fifth2 fifth3 fifth4 fifth5 fifth6 fifth7 fifth8 fifth9\nsixth0 sixth1 sixth2 sixth3 sixth4 sixth5 sixth6 sixth7 sixth8 sixth9\nseventh0 seventh1 seventh2 seventh3 seventh4 seventh5 seventh6 seventh7 seventh8 seventh9\n", NULL);
1761 } else if (strcmp(argv[2], "free") == 0) {
1762 char *s = (char *)ckalloc(100);
1763 strcpy(s, "This is a malloc-ed string");
1764 Tcl_SetResult(interp, s, TCL_DYNAMIC);
1765 } else if (strcmp(argv[2], "special") == 0) {
1766 char *s = (char *)ckalloc(100) + 16;
1767 strcpy(s, "This is a specially-allocated string");
1768 Tcl_SetResult(interp, s, SpecialFree);
1769 } else {
1770 Tcl_AppendResult(interp, "bad gresult option \"", argv[2],
1771 "\": must be staticsmall, staticlarge, free, or special",
1772 (char *)NULL);
1773 return TCL_ERROR;
1774 }
1775 Tcl_DStringGetResult(interp, &dstring);
1776 } else if (strcmp(argv[1], "length") == 0) {
1777
1778 if (argc != 2) {
1779 goto wrongNumArgs;
1780 }
1781 Tcl_SetObjResult(interp, Tcl_NewIntObj(Tcl_DStringLength(&dstring)));
1782 } else if (strcmp(argv[1], "result") == 0) {
1783 if (argc != 2) {
1784 goto wrongNumArgs;
1785 }
1786 Tcl_DStringResult(interp, &dstring);
1787 } else if (strcmp(argv[1], "trunc") == 0) {
1788 if (argc != 3) {
1789 goto wrongNumArgs;
1790 }
1791 if (Tcl_GetInt(interp, argv[2], &count) != TCL_OK) {
1792 return TCL_ERROR;
1793 }
1794 Tcl_DStringSetLength(&dstring, count);
1795 } else if (strcmp(argv[1], "start") == 0) {
1796 if (argc != 2) {
1797 goto wrongNumArgs;
1798 }
1799 Tcl_DStringStartSublist(&dstring);
1800 } else {
1801 Tcl_AppendResult(interp, "bad option \"", argv[1],
1802 "\": must be append, element, end, free, get, length, "
1803 "result, trunc, or start", NULL);
1804 return TCL_ERROR;
1805 }
1806 return TCL_OK;
1807 }
1808
1809 /*
1810 * The procedure below is used as a special freeProc to test how well
1811 * Tcl_DStringGetResult handles freeProc's other than free.
1812 */
1813
1814 static void SpecialFree(
1815 #if TCL_MAJOR_VERSION > 8
1816 void *blockPtr /* Block to free. */
1817 #else
1818 char *blockPtr /* Block to free. */
1819 #endif
1820 ) {
1821 ckfree((char *)blockPtr - 16);
1822 }
1823
1824 /*
1825 *------------------------------------------------------------------------
1826 *
1827 * UtfTransformFn --
1828 *
1829 * Implements a direct call into Tcl_UtfToExternal and Tcl_ExternalToUtf
1830 * as otherwise there is no script level command that directly exercises
1831 * these functions (i/o command cannot test all combinations)
1832 * The arguments at the script level are roughly those of the above
1833 * functions:
1834 * encodingname srcbytes flags state dstlen ?srcreadvar? ?dstwrotevar? ?dstcharsvar?
1835 *
1836 * Results:
1837 * TCL_OK or TCL_ERROR. This any errors running the test, NOT the
1838 * result of Tcl_UtfToExternal or Tcl_ExternalToUtf.
1839 *
1840 * Side effects:
1841 *
1842 * The result in the interpreter is a list of the return code from the
1843 * Tcl_UtfToExternal/Tcl_ExternalToUtf functions, the encoding state, and
1844 * an encoded binary string of length dstLen. Note the string is the
1845 * entire output buffer, not just the part containing the decoded
1846 * portion. This allows for additional checks at test script level.
1847 *
1848 * If any of the srcreadvar, dstwrotevar and
1849 * dstcharsvar are specified and not empty, they are treated as names
1850 * of variables where the *srcRead, *dstWrote and *dstChars output
1851 * from the functions are stored.
1852 *
1853 * The function also checks internally whether nuls are correctly
1854 * appended as requested but the TCL_ENCODING_NO_TERMINATE flag
1855 * and that no buffer overflows occur.
1856 *------------------------------------------------------------------------
1857 */
1858 typedef int
1859 UtfTransformFn(Tcl_Interp *interp, Tcl_Encoding encoding, const char *src, int srcLen, int flags, Tcl_EncodingState *statePtr,
1860 char *dst, int dstLen, int *srcReadPtr, int *dstWrotePtr, int *dstCharsPtr);
1861 static int UtfExtWrapper(
1862 Tcl_Interp *interp, UtfTransformFn *transformer, int objc, Tcl_Obj *const objv[])
1863 {
1864 Tcl_Encoding encoding;
1865 Tcl_EncodingState encState, *encStatePtr;
1866 int srcLen, bufLen;
1867 const unsigned char *bytes;
1868 unsigned char *bufPtr;
1869 int srcRead, dstLen, dstWrote, dstChars;
1870 Tcl_Obj *srcReadVar, *dstWroteVar, *dstCharsVar;
1871 int result;
1872 int flags;
1873 Tcl_Obj **flagObjs;
1874 int nflags;
1875 static const struct {
1876 const char *flagKey;
1877 int flag;
1878 } flagMap[] = {
1879 {"start", TCL_ENCODING_START},
1880 {"end", TCL_ENCODING_END},
1881 {"stoponerror", TCL_ENCODING_STOPONERROR},
1882 {"noterminate", TCL_ENCODING_NO_TERMINATE},
1883 {"charlimit", TCL_ENCODING_CHAR_LIMIT},
1884 {NULL, 0}
1885 };
1886 int i;
1887 Tcl_WideInt wide;
1888
1889 if (objc < 7 || objc > 10) {
1890 Tcl_WrongNumArgs(interp,
1891 2,
1892 objv,
1893 "encoding srcbytes flags state dstlen ?srcreadvar? ?dstwrotevar? ?dstcharsvar?");
1894 return TCL_ERROR;
1895 }
1896 if (Tcl_GetEncodingFromObj(interp, objv[2], &encoding) != TCL_OK) {
1897 return TCL_ERROR;
1898 }
1899
1900 /* Flags may be specified as list of integers and keywords */
1901 flags = 0;
1902 if (Tcl_ListObjGetElements(interp, objv[4], &nflags, &flagObjs) != TCL_OK) {
1903 return TCL_ERROR;
1904 }
1905
1906 for (i = 0; i < nflags; ++i) {
1907 int flag;
1908 if (Tcl_GetIntFromObj(NULL, flagObjs[i], &flag) == TCL_OK) {
1909 flags |= flag;
1910 } else {
1911 int idx;
1912 if (Tcl_GetIndexFromObjStruct(interp,
1913 flagObjs[i],
1914 flagMap,
1915 sizeof(flagMap[0]),
1916 "flag",
1917 0,
1918 &idx) != TCL_OK) {
1919 return TCL_ERROR;
1920 }
1921 flags |= flagMap[idx].flag;
1922 }
1923 }
1924
1925 /* Assumes state is integer if not "" */
1926 if (Tcl_GetWideIntFromObj(interp, objv[5], &wide) == TCL_OK) {
1927 encState = (Tcl_EncodingState)(size_t)wide;
1928 encStatePtr = &encState;
1929 } else if (Tcl_GetCharLength(objv[5]) == 0) {
1930 encStatePtr = NULL;
1931 } else {
1932 return TCL_ERROR;
1933 }
1934
1935 if (Tcl_GetIntFromObj(interp, objv[6], &dstLen) != TCL_OK) {
1936 return TCL_ERROR;
1937 }
1938 srcReadVar = NULL;
1939 dstWroteVar = NULL;
1940 dstCharsVar = NULL;
1941 if (objc > 7) {
1942 /* Has caller requested srcRead? */
1943 if (Tcl_GetCharLength(objv[7])) {
1944 srcReadVar = objv[7];
1945 }
1946 if (objc > 8) {
1947 /* Ditto for dstWrote */
1948 if (Tcl_GetCharLength(objv[8])) {
1949 dstWroteVar = objv[8];
1950 }
1951 if (objc > 9) {
1952 if (Tcl_GetCharLength(objv[9])) {
1953 dstCharsVar = objv[9];
1954 }
1955 }
1956 }
1957 }
1958 if (flags & TCL_ENCODING_CHAR_LIMIT) {
1959 /* Caller should have specified the dest char limit */
1960 Tcl_Obj *valueObj;
1961 if (dstCharsVar == NULL ||
1962 (valueObj = Tcl_ObjGetVar2(interp, dstCharsVar, NULL, 0)) == NULL
1963 ) {
1964 Tcl_SetResult(interp,
1965 "dstCharsVar must be specified with integer value if "
1966 "TCL_ENCODING_CHAR_LIMIT set in flags.", TCL_STATIC);
1967 return TCL_ERROR;
1968 }
1969 if (Tcl_GetIntFromObj(interp, valueObj, &dstChars) != TCL_OK) {
1970 return TCL_ERROR;
1971 }
1972 } else {
1973 dstChars = 0; /* Only used for output */
1974 }
1975
1976 bufLen = dstLen + 4; /* 4 -> overflow detection */
1977 bufPtr = (unsigned char *) ckalloc(bufLen);
1978 memset(bufPtr, 0xFF, dstLen); /* Need to check nul terminator */
1979 memmove(bufPtr + dstLen, "\xAB\xCD\xEF\xAB", 4); /* overflow detection */
1980 bytes = Tcl_GetByteArrayFromObj(objv[3], &srcLen); /* Last! to avoid shimmering */
1981 result = (*transformer)(interp, encoding, (const char *)bytes, srcLen, flags,
1982 encStatePtr, (char *) bufPtr, dstLen,
1983 srcReadVar ? &srcRead : NULL,
1984 &dstWrote,
1985 dstCharsVar ? &dstChars : NULL);
1986 if (memcmp(bufPtr + bufLen - 4, "\xAB\xCD\xEF\xAB", 4)) {
1987 Tcl_SetResult(interp,
1988 "Tcl_ExternalToUtf wrote past output buffer",
1989 TCL_STATIC);
1990 result = TCL_ERROR;
1991 } else if (result != TCL_ERROR) {
1992 Tcl_Obj *resultObjs[3];
1993 switch (result) {
1994 case TCL_OK:
1995 resultObjs[0] = Tcl_NewStringObj("ok", -1);
1996 break;
1997 case TCL_CONVERT_MULTIBYTE:
1998 resultObjs[0] = Tcl_NewStringObj("multibyte", -1);
1999 break;
2000 case TCL_CONVERT_SYNTAX:
2001 resultObjs[0] = Tcl_NewStringObj("syntax", -1);
2002 break;
2003 case TCL_CONVERT_UNKNOWN:
2004 resultObjs[0] = Tcl_NewStringObj("unknown", -1);
2005 break;
2006 case TCL_CONVERT_NOSPACE:
2007 resultObjs[0] = Tcl_NewStringObj("nospace", -1);
2008 break;
2009 default:
2010 resultObjs[0] = Tcl_NewIntObj(result);
2011 break;
2012 }
2013 result = TCL_OK;
2014 resultObjs[1] =
2015 encStatePtr ? Tcl_NewWideIntObj((Tcl_WideInt)(size_t)encState) : Tcl_NewObj();
2016 resultObjs[2] = Tcl_NewByteArrayObj(bufPtr, dstLen);
2017 if (srcReadVar) {
2018 if (Tcl_ObjSetVar2(interp,
2019 srcReadVar,
2020 NULL,
2021 Tcl_NewIntObj(srcRead),
2022 TCL_LEAVE_ERR_MSG) == NULL) {
2023 result = TCL_ERROR;
2024 }
2025 }
2026 if (dstWroteVar) {
2027 if (Tcl_ObjSetVar2(interp,
2028 dstWroteVar,
2029 NULL,
2030 Tcl_NewIntObj(dstWrote),
2031 TCL_LEAVE_ERR_MSG) == NULL) {
2032 result = TCL_ERROR;
2033 }
2034 }
2035 if (dstCharsVar) {
2036 if (Tcl_ObjSetVar2(interp,
2037 dstCharsVar,
2038 NULL,
2039 Tcl_NewIntObj(dstChars),
2040 TCL_LEAVE_ERR_MSG) == NULL) {
2041 result = TCL_ERROR;
2042 }
2043 }
2044 Tcl_SetObjResult(interp, Tcl_NewListObj(3, resultObjs));
2045 }
2046
2047 ckfree(bufPtr);
2048 Tcl_FreeEncoding(encoding); /* Free returned reference */
2049 return result;
2050 }
2051
2052 /*
2053 *----------------------------------------------------------------------
2054 *
2055 * TestencodingCmd --
2056 *
2057 * This procedure implements the "testencoding" command. It is used
2058 * to test the encoding package.
2059 *
2060 * Results:
2061 * A standard Tcl result.
2062 *
2063 * Side effects:
2064 * Load encodings.
2065 *
2066 *----------------------------------------------------------------------
2067 */
2068
2069 static int
2070 TestencodingObjCmd(
2071 ClientData dummy, /* Not used. */
2072 Tcl_Interp *interp, /* Current interpreter. */
2073 int objc, /* Number of arguments. */
2074 Tcl_Obj *const objv[]) /* Argument objects. */
2075 {
2076 Tcl_Encoding encoding;
2077 int index, length;
2078 const char *string;
2079 TclEncoding *encodingPtr;
2080 static const char *const optionStrings[] = {
2081 "create", "delete", "Tcl_ExternalToUtf", "Tcl_UtfToExternal", NULL
2082 };
2083 enum options {
2084 ENC_CREATE, ENC_DELETE, ENC_EXTTOUTF, ENC_UTFTOEXT
2085 };
2086
2087 if (objc < 2) {
2088 Tcl_WrongNumArgs(interp, 1, objv, "command ?args?");
2089 return TCL_ERROR;
2090 }
2091
2092 if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
2093 &index) != TCL_OK) {
2094 return TCL_ERROR;
2095 }
2096
2097 switch ((enum options) index) {
2098 case ENC_CREATE: {
2099 Tcl_EncodingType type;
2100
2101 if (objc != 5) {
2102 Tcl_WrongNumArgs(interp, 2, objv, "name toutfcmd fromutfcmd");
2103 return TCL_ERROR;
2104 }
2105 encodingPtr = (TclEncoding *)ckalloc(sizeof(TclEncoding));
2106 encodingPtr->interp = interp;
2107
2108 string = Tcl_GetStringFromObj(objv[3], &length);
2109 encodingPtr->toUtfCmd = (char *)ckalloc(length + 1);
2110 memcpy(encodingPtr->toUtfCmd, string, length + 1);
2111
2112 string = Tcl_GetStringFromObj(objv[4], &length);
2113 encodingPtr->fromUtfCmd = (char *)ckalloc(length + 1);
2114 memcpy(encodingPtr->fromUtfCmd, string, length + 1);
2115
2116 string = Tcl_GetStringFromObj(objv[2], &length);
2117
2118 type.encodingName = string;
2119 type.toUtfProc = EncodingToUtfProc;
2120 type.fromUtfProc = EncodingFromUtfProc;
2121 type.freeProc = EncodingFreeProc;
2122 type.clientData = encodingPtr;
2123 type.nullSize = 1;
2124
2125 Tcl_CreateEncoding(&type);
2126 break;
2127 }
2128 case ENC_DELETE:
2129 if (objc != 3) {
2130 return TCL_ERROR;
2131 }
2132 if (TCL_OK != Tcl_GetEncodingFromObj(interp, objv[2], &encoding)) {
2133 return TCL_ERROR;
2134 }
2135 Tcl_FreeEncoding(encoding); /* Free returned reference */
2136 Tcl_FreeEncoding(encoding); /* Free to match CREATE */
2137 break;
2138 case ENC_EXTTOUTF:
2139 return UtfExtWrapper(interp,Tcl_ExternalToUtf,objc,objv);
2140 case ENC_UTFTOEXT:
2141 return UtfExtWrapper(interp,Tcl_UtfToExternal,objc,objv);
2142 }
2143 return TCL_OK;
2144 }
2145
2146 static int
2147 EncodingToUtfProc(
2148 ClientData clientData, /* TclEncoding structure. */
2149 const char *src, /* Source string in specified encoding. */
2150 int srcLen, /* Source string length in bytes. */
2151 int flags, /* Conversion control flags. */
2152 Tcl_EncodingState *statePtr,/* Current state. */
2153 char *dst, /* Output buffer. */
2154 int dstLen, /* The maximum length of output buffer. */
2155 int *srcReadPtr, /* Filled with number of bytes read. */
2156 int *dstWrotePtr, /* Filled with number of bytes stored. */
2157 int *dstCharsPtr) /* Filled with number of chars stored. */
2158 {
2159 int len;
2160 TclEncoding *encodingPtr;
2161
2162 encodingPtr = (TclEncoding *) clientData;
2163 Tcl_EvalEx(encodingPtr->interp, encodingPtr->toUtfCmd, -1, TCL_EVAL_GLOBAL);
2164
2165 len = strlen(Tcl_GetStringResult(encodingPtr->interp));
2166 if (len > dstLen) {
2167 len = dstLen;
2168 }
2169 memcpy(dst, Tcl_GetStringResult(encodingPtr->interp), len);
2170 Tcl_ResetResult(encodingPtr->interp);
2171
2172 *srcReadPtr = srcLen;
2173 *dstWrotePtr = len;
2174 *dstCharsPtr = len;
2175 return TCL_OK;
2176 }
2177
2178 static int
2179 EncodingFromUtfProc(
2180 ClientData clientData, /* TclEncoding structure. */
2181 const char *src, /* Source string in specified encoding. */
2182 int srcLen, /* Source string length in bytes. */
2183 int flags, /* Conversion control flags. */
2184 Tcl_EncodingState *statePtr,/* Current state. */
2185 char *dst, /* Output buffer. */
2186 int dstLen, /* The maximum length of output buffer. */
2187 int *srcReadPtr, /* Filled with number of bytes read. */
2188 int *dstWrotePtr, /* Filled with number of bytes stored. */
2189 int *dstCharsPtr) /* Filled with number of chars stored. */
2190 {
2191 int len;
2192 TclEncoding *encodingPtr;
2193
2194 encodingPtr = (TclEncoding *) clientData;
2195 Tcl_EvalEx(encodingPtr->interp, encodingPtr->fromUtfCmd, -1, TCL_EVAL_GLOBAL);
2196
2197 len = strlen(Tcl_GetStringResult(encodingPtr->interp));
2198 if (len > dstLen) {
2199 len = dstLen;
2200 }
2201 memcpy(dst, Tcl_GetStringResult(encodingPtr->interp), len);
2202 Tcl_ResetResult(encodingPtr->interp);
2203
2204 *srcReadPtr = srcLen;
2205 *dstWrotePtr = len;
2206 *dstCharsPtr = len;
2207 return TCL_OK;
2208 }
2209
2210 static void
2211 EncodingFreeProc(
2212 ClientData clientData) /* ClientData associated with type. */
2213 {
2214 TclEncoding *encodingPtr = (TclEncoding *)clientData;
2215
2216 ckfree(encodingPtr->toUtfCmd);
2217 ckfree(encodingPtr->fromUtfCmd);
2218 ckfree(encodingPtr);
2219 }
2220
2221 /*
2222 *----------------------------------------------------------------------
2223 *
2224 * TestevalexObjCmd --
2225 *
2226 * This procedure implements the "testevalex" command. It is
2227 * used to test Tcl_EvalEx.
2228 *
2229 * Results:
2230 * A standard Tcl result.
2231 *
2232 * Side effects:
2233 * None.
2234 *
2235 *----------------------------------------------------------------------
2236 */
2237
2238 static int
2239 TestevalexObjCmd(
2240 ClientData dummy, /* Not used. */
2241 Tcl_Interp *interp, /* Current interpreter. */
2242 int objc, /* Number of arguments. */
2243 Tcl_Obj *const objv[]) /* Argument objects. */
2244 {
2245 int length, flags;
2246 const char *script;
2247
2248 flags = 0;
2249 if (objc == 3) {
2250 const char *global = Tcl_GetString(objv[2]);
2251 if (strcmp(global, "global") != 0) {
2252 Tcl_AppendResult(interp, "bad value \"", global,
2253 "\": must be global", NULL);
2254 return TCL_ERROR;
2255 }
2256 flags = TCL_EVAL_GLOBAL;
2257 } else if (objc != 2) {
2258 Tcl_WrongNumArgs(interp, 1, objv, "script ?global?");
2259 return TCL_ERROR;
2260 }
2261
2262 script = Tcl_GetStringFromObj(objv[1], &length);
2263 return Tcl_EvalEx(interp, script, length, flags);
2264 }
2265
2266 /*
2267 *----------------------------------------------------------------------
2268 *
2269 * TestevalobjvObjCmd --
2270 *
2271 * This procedure implements the "testevalobjv" command. It is
2272 * used to test Tcl_EvalObjv.
2273 *
2274 * Results:
2275 * A standard Tcl result.
2276 *
2277 * Side effects:
2278 * None.
2279 *
2280 *----------------------------------------------------------------------
2281 */
2282
2283 static int
2284 TestevalobjvObjCmd(
2285 ClientData dummy, /* Not used. */
2286 Tcl_Interp *interp, /* Current interpreter. */
2287 int objc, /* Number of arguments. */
2288 Tcl_Obj *const objv[]) /* Argument objects. */
2289 {
2290 int evalGlobal;
2291
2292 if (objc < 3) {
2293 Tcl_WrongNumArgs(interp, 1, objv, "global word ?word ...?");
2294 return TCL_ERROR;
2295 }
2296 if (Tcl_GetIntFromObj(interp, objv[1], &evalGlobal) != TCL_OK) {
2297 return TCL_ERROR;
2298 }
2299 return Tcl_EvalObjv(interp, objc-2, objv+2,
2300 (evalGlobal) ? TCL_EVAL_GLOBAL : 0);
2301 }
2302
2303 /*
2304 *----------------------------------------------------------------------
2305 *
2306 * TesteventObjCmd --
2307 *
2308 * This procedure implements a 'testevent' command. The command
2309 * is used to test event queue management.
2310 *
2311 * The command takes two forms:
2312 * - testevent queue name position script
2313 * Queues an event at the given position in the queue, and
2314 * associates a given name with it (the same name may be
2315 * associated with multiple events). When the event comes
2316 * to the head of the queue, executes the given script at
2317 * global level in the current interp. The position may be
2318 * one of 'head', 'tail' or 'mark'.
2319 * - testevent delete name
2320 * Deletes any events associated with the given name from
2321 * the queue.
2322 *
2323 * Return value:
2324 * Returns a standard Tcl result.
2325 *
2326 * Side effects:
2327 * Manipulates the event queue as directed.
2328 *
2329 *----------------------------------------------------------------------
2330 */
2331
2332 static int
2333 TesteventObjCmd(
2334 ClientData unused, /* Not used */
2335 Tcl_Interp *interp, /* Tcl interpreter */
2336 int objc, /* Parameter count */
2337 Tcl_Obj *const objv[]) /* Parameter vector */
2338 {
2339 static const char *const subcommands[] = { /* Possible subcommands */
2340 "queue", "delete", NULL
2341 };
2342 int subCmdIndex; /* Index of the chosen subcommand */
2343 static const char *const positions[] = { /* Possible queue positions */
2344 "head", "tail", "mark", NULL
2345 };
2346 int posIndex; /* Index of the chosen position */
2347 static const Tcl_QueuePosition posNum[] = {
2348 /* Interpretation of the chosen position */
2349 TCL_QUEUE_HEAD,
2350 TCL_QUEUE_TAIL,
2351 TCL_QUEUE_MARK
2352 };
2353 TestEvent *ev; /* Event to be queued */
2354
2355 if (objc < 2) {
2356 Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?arg ...?");
2357 return TCL_ERROR;
2358 }
2359 if (Tcl_GetIndexFromObj(interp, objv[1], subcommands, "subcommand",
2360 TCL_EXACT, &subCmdIndex) != TCL_OK) {
2361 return TCL_ERROR;
2362 }
2363 switch (subCmdIndex) {
2364 case 0: /* queue */
2365 if (objc != 5) {
2366 Tcl_WrongNumArgs(interp, 2, objv, "name position script");
2367 return TCL_ERROR;
2368 }
2369 if (Tcl_GetIndexFromObj(interp, objv[3], positions,
2370 "position specifier", TCL_EXACT, &posIndex) != TCL_OK) {
2371 return TCL_ERROR;
2372 }
2373 ev = (TestEvent *)ckalloc(sizeof(TestEvent));
2374 ev->header.proc = TesteventProc;
2375 ev->header.nextPtr = NULL;
2376 ev->interp = interp;
2377 ev->command = objv[4];
2378 Tcl_IncrRefCount(ev->command);
2379 ev->tag = objv[2];
2380 Tcl_IncrRefCount(ev->tag);
2381 Tcl_QueueEvent((Tcl_Event *) ev, posNum[posIndex]);
2382 break;
2383
2384 case 1: /* delete */
2385 if (objc != 3) {
2386 Tcl_WrongNumArgs(interp, 2, objv, "name");
2387 return TCL_ERROR;
2388 }
2389 Tcl_DeleteEvents(TesteventDeleteProc, objv[2]);
2390 break;
2391 }
2392
2393 return TCL_OK;
2394 }
2395
2396 /*
2397 *----------------------------------------------------------------------
2398 *
2399 * TesteventProc --
2400 *
2401 * Delivers a test event to the Tcl interpreter as part of event
2402 * queue testing.
2403 *
2404 * Results:
2405 * Returns 1 if the event has been serviced, 0 otherwise.
2406 *
2407 * Side effects:
2408 * Evaluates the event's callback script, so has whatever side effects
2409 * the callback has. The return value of the callback script becomes the
2410 * return value of this function. If the callback script reports an
2411 * error, it is reported as a background error.
2412 *
2413 *----------------------------------------------------------------------
2414 */
2415
2416 static int
2417 TesteventProc(
2418 Tcl_Event *event, /* Event to deliver */
2419 int flags) /* Current flags for Tcl_ServiceEvent */
2420 {
2421 TestEvent *ev = (TestEvent *) event;
2422 Tcl_Interp *interp = ev->interp;
2423 Tcl_Obj *command = ev->command;
2424 int result = Tcl_EvalObjEx(interp, command,
2425 TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT);
2426 int retval;
2427
2428 if (result != TCL_OK) {
2429 Tcl_AddErrorInfo(interp,
2430 " (command bound to \"testevent\" callback)");
2431 Tcl_BackgroundError(interp);
2432 return 1; /* Avoid looping on errors */
2433 }
2434 if (Tcl_GetBooleanFromObj(interp, Tcl_GetObjResult(interp),
2435 &retval) != TCL_OK) {
2436 Tcl_AddErrorInfo(interp,
2437 " (return value from \"testevent\" callback)");
2438 Tcl_BackgroundError(interp);
2439 return 1;
2440 }
2441 if (retval) {
2442 Tcl_DecrRefCount(ev->tag);
2443 Tcl_DecrRefCount(ev->command);
2444 }
2445
2446 return retval;
2447 }
2448
2449 /*
2450 *----------------------------------------------------------------------
2451 *
2452 * TesteventDeleteProc --
2453 *
2454 * Removes some set of events from the queue.
2455 *
2456 * This procedure is used as part of testing event queue management.
2457 *
2458 * Results:
2459 * Returns 1 if a given event should be deleted, 0 otherwise.
2460 *
2461 * Side effects:
2462 * None.
2463 *
2464 *----------------------------------------------------------------------
2465 */
2466
2467 static int
2468 TesteventDeleteProc(
2469 Tcl_Event *event, /* Event to examine */
2470 ClientData clientData) /* Tcl_Obj containing the name of the event(s)
2471 * to remove */
2472 {
2473 TestEvent *ev; /* Event to examine */
2474 const char *evNameStr;
2475 Tcl_Obj *targetName; /* Name of the event(s) to delete */
2476 const char *targetNameStr;
2477
2478 if (event->proc != TesteventProc) {
2479 return 0;
2480 }
2481 targetName = (Tcl_Obj *) clientData;
2482 targetNameStr = (char *) Tcl_GetString(targetName);
2483 ev = (TestEvent *) event;
2484 evNameStr = Tcl_GetString(ev->tag);
2485 if (strcmp(evNameStr, targetNameStr) == 0) {
2486 Tcl_DecrRefCount(ev->tag);
2487 Tcl_DecrRefCount(ev->command);
2488 return 1;
2489 } else {
2490 return 0;
2491 }
2492 }
2493
2494 /*
2495 *----------------------------------------------------------------------
2496 *
2497 * TestexithandlerCmd --
2498 *
2499 * This procedure implements the "testexithandler" command. It is
2500 * used to test Tcl_CreateExitHandler and Tcl_DeleteExitHandler.
2501 *
2502 * Results:
2503 * A standard Tcl result.
2504 *
2505 * Side effects:
2506 * None.
2507 *
2508 *----------------------------------------------------------------------
2509 */
2510
2511 static int
2512 TestexithandlerCmd(
2513 ClientData clientData, /* Not used. */
2514 Tcl_Interp *interp, /* Current interpreter. */
2515 int argc, /* Number of arguments. */
2516 const char **argv) /* Argument strings. */
2517 {
2518 int value;
2519
2520 if (argc != 3) {
2521 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
2522 " create|delete value\"", NULL);
2523 return TCL_ERROR;
2524 }
2525 if (Tcl_GetInt(interp, argv[2], &value) != TCL_OK) {
2526 return TCL_ERROR;
2527 }
2528 if (strcmp(argv[1], "create") == 0) {
2529 Tcl_CreateExitHandler((value & 1) ? ExitProcOdd : ExitProcEven,
2530 INT2PTR(value));
2531 } else if (strcmp(argv[1], "delete") == 0) {
2532 Tcl_DeleteExitHandler((value & 1) ? ExitProcOdd : ExitProcEven,
2533 INT2PTR(value));
2534 } else {
2535 Tcl_AppendResult(interp, "bad option \"", argv[1],
2536 "\": must be create or delete", NULL);
2537 return TCL_ERROR;
2538 }
2539 return TCL_OK;
2540 }
2541
2542 static void
2543 ExitProcOdd(
2544 ClientData clientData) /* Integer value to print. */
2545 {
2546 char buf[16 + TCL_INTEGER_SPACE];
2547 int len;
2548
2549 snprintf(buf, sizeof(buf), "odd %d\n", (int)PTR2INT(clientData));
2550 len = strlen(buf);
2551 if (len != (int) write(1, buf, len)) {
2552 Tcl_Panic("ExitProcOdd: unable to write to stdout");
2553 }
2554 }
2555
2556 static void
2557 ExitProcEven(
2558 ClientData clientData) /* Integer value to print. */
2559 {
2560 char buf[16 + TCL_INTEGER_SPACE];
2561 int len;
2562
2563 snprintf(buf, sizeof(buf), "even %d\n", (int)PTR2INT(clientData));
2564 len = strlen(buf);
2565 if (len != (int) write(1, buf, len)) {
2566 Tcl_Panic("ExitProcEven: unable to write to stdout");
2567 }
2568 }
2569
2570 /*
2571 *----------------------------------------------------------------------
2572 *
2573 * TestexprlongCmd --
2574 *
2575 * This procedure verifies that Tcl_ExprLong does not modify the
2576 * interpreter result if there is no error.
2577 *
2578 * Results:
2579 * A standard Tcl result.
2580 *
2581 * Side effects:
2582 * None.
2583 *
2584 *----------------------------------------------------------------------
2585 */
2586
2587 static int
2588 TestexprlongCmd(
2589 ClientData clientData, /* Not used. */
2590 Tcl_Interp *interp, /* Current interpreter. */
2591 int argc, /* Number of arguments. */
2592 const char **argv) /* Argument strings. */
2593 {
2594 long exprResult;
2595 char buf[4 + TCL_INTEGER_SPACE];
2596 int result;
2597
2598 if (argc != 2) {
2599 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
2600 " expression\"", NULL);
2601 return TCL_ERROR;
2602 }
2603 Tcl_AppendResult(interp, "This is a result", NULL);
2604 result = Tcl_ExprLong(interp, argv[1], &exprResult);
2605 if (result != TCL_OK) {
2606 return result;
2607 }
2608 snprintf(buf, sizeof(buf), ": %ld", exprResult);
2609 Tcl_AppendResult(interp, buf, NULL);
2610 return TCL_OK;
2611 }
2612
2613 /*
2614 *----------------------------------------------------------------------
2615 *
2616 * TestexprlongobjCmd --
2617 *
2618 * This procedure verifies that Tcl_ExprLongObj does not modify the
2619 * interpreter result if there is no error.
2620 *
2621 * Results:
2622 * A standard Tcl result.
2623 *
2624 * Side effects:
2625 * None.
2626 *
2627 *----------------------------------------------------------------------
2628 */
2629
2630 static int
2631 TestexprlongobjCmd(
2632 ClientData clientData, /* Not used. */
2633 Tcl_Interp *interp, /* Current interpreter. */
2634 int objc, /* Number of arguments. */
2635 Tcl_Obj *const *objv) /* Argument objects. */
2636 {
2637 long exprResult;
2638 char buf[4 + TCL_INTEGER_SPACE];
2639 int result;
2640
2641 if (objc != 2) {
2642 Tcl_WrongNumArgs(interp, 1, objv, "expression");
2643 return TCL_ERROR;
2644 }
2645 Tcl_AppendResult(interp, "This is a result", NULL);
2646 result = Tcl_ExprLongObj(interp, objv[1], &exprResult);
2647 if (result != TCL_OK) {
2648 return result;
2649 }
2650 snprintf(buf, sizeof(buf), ": %ld", exprResult);
2651 Tcl_AppendResult(interp, buf, NULL);
2652 return TCL_OK;
2653 }
2654
2655 /*
2656 *----------------------------------------------------------------------
2657 *
2658 * TestexprdoubleCmd --
2659 *
2660 * This procedure verifies that Tcl_ExprDouble does not modify the
2661 * interpreter result if there is no error.
2662 *
2663 * Results:
2664 * A standard Tcl result.
2665 *
2666 * Side effects:
2667 * None.
2668 *
2669 *----------------------------------------------------------------------
2670 */
2671
2672 static int
2673 TestexprdoubleCmd(
2674 ClientData clientData, /* Not used. */
2675 Tcl_Interp *interp, /* Current interpreter. */
2676 int argc, /* Number of arguments. */
2677 const char **argv) /* Argument strings. */
2678 {
2679 double exprResult;
2680 char buf[4 + TCL_DOUBLE_SPACE];
2681 int result;
2682
2683 if (argc != 2) {
2684 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
2685 " expression\"", NULL);
2686 return TCL_ERROR;
2687 }
2688 Tcl_AppendResult(interp, "This is a result", NULL);
2689 result = Tcl_ExprDouble(interp, argv[1], &exprResult);
2690 if (result != TCL_OK) {
2691 return result;
2692 }
2693 strcpy(buf, ": ");
2694 Tcl_PrintDouble(interp, exprResult, buf+2);
2695 Tcl_AppendResult(interp, buf, NULL);
2696 return TCL_OK;
2697 }
2698
2699 /*
2700 *----------------------------------------------------------------------
2701 *
2702 * TestexprdoubleobjCmd --
2703 *
2704 * This procedure verifies that Tcl_ExprLongObj does not modify the
2705 * interpreter result if there is no error.
2706 *
2707 * Results:
2708 * A standard Tcl result.
2709 *
2710 * Side effects:
2711 * None.
2712 *
2713 *----------------------------------------------------------------------
2714 */
2715
2716 static int
2717 TestexprdoubleobjCmd(
2718 ClientData clientData, /* Not used. */
2719 Tcl_Interp *interp, /* Current interpreter. */
2720 int objc, /* Number of arguments. */
2721 Tcl_Obj *const *objv) /* Argument objects. */
2722 {
2723 double exprResult;
2724 char buf[4 + TCL_DOUBLE_SPACE];
2725 int result;
2726
2727 if (objc != 2) {
2728 Tcl_WrongNumArgs(interp, 1, objv, "expression");
2729 return TCL_ERROR;
2730 }
2731 Tcl_AppendResult(interp, "This is a result", NULL);
2732 result = Tcl_ExprDoubleObj(interp, objv[1], &exprResult);
2733 if (result != TCL_OK) {
2734 return result;
2735 }
2736 strcpy(buf, ": ");
2737 Tcl_PrintDouble(interp, exprResult, buf+2);
2738 Tcl_AppendResult(interp, buf, NULL);
2739 return TCL_OK;
2740 }
2741
2742 /*
2743 *----------------------------------------------------------------------
2744 *
2745 * TestexprstringCmd --
2746 *
2747 * This procedure tests the basic operation of Tcl_ExprString.
2748 *
2749 * Results:
2750 * A standard Tcl result.
2751 *
2752 * Side effects:
2753 * None.
2754 *
2755 *----------------------------------------------------------------------
2756 */
2757
2758 static int
2759 TestexprstringCmd(
2760 ClientData clientData, /* Not used. */
2761 Tcl_Interp *interp, /* Current interpreter. */
2762 int argc, /* Number of arguments. */
2763 const char **argv) /* Argument strings. */
2764 {
2765 if (argc != 2) {
2766 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
2767 " expression\"", NULL);
2768 return TCL_ERROR;
2769 }
2770 return Tcl_ExprString(interp, argv[1]);
2771 }
2772
2773 /*
2774 *----------------------------------------------------------------------
2775 *
2776 * TestfilelinkCmd --
2777 *
2778 * This procedure implements the "testfilelink" command. It is used to
2779 * test the effects of creating and manipulating filesystem links in Tcl.
2780 *
2781 * Results:
2782 * A standard Tcl result.
2783 *
2784 * Side effects:
2785 * May create a link on disk.
2786 *
2787 *----------------------------------------------------------------------
2788 */
2789
2790 static int
2791 TestfilelinkCmd(
2792 ClientData clientData, /* Not used. */
2793 Tcl_Interp *interp, /* Current interpreter. */
2794 int objc, /* Number of arguments. */
2795 Tcl_Obj *const objv[]) /* The argument objects. */
2796 {
2797 Tcl_Obj *contents;
2798
2799 if (objc < 2 || objc > 3) {
2800 Tcl_WrongNumArgs(interp, 1, objv, "source ?target?");
2801 return TCL_ERROR;
2802 }
2803
2804 if (Tcl_FSConvertToPathType(interp, objv[1]) != TCL_OK) {
2805 return TCL_ERROR;
2806 }
2807
2808 if (objc == 3) {
2809 /* Create link from source to target */
2810 contents = Tcl_FSLink(objv[1], objv[2],
2811 TCL_CREATE_SYMBOLIC_LINK|TCL_CREATE_HARD_LINK);
2812 if (contents == NULL) {
2813 Tcl_AppendResult(interp, "could not create link from \"",
2814 Tcl_GetString(objv[1]), "\" to \"",
2815 Tcl_GetString(objv[2]), "\": ",
2816 Tcl_PosixError(interp), NULL);
2817 return TCL_ERROR;
2818 }
2819 } else {
2820 /* Read link */
2821 contents = Tcl_FSLink(objv[1], NULL, 0);
2822 if (contents == NULL) {
2823 Tcl_AppendResult(interp, "could not read link \"",
2824 Tcl_GetString(objv[1]), "\": ",
2825 Tcl_PosixError(interp), NULL);
2826 return TCL_ERROR;
2827 }
2828 }
2829 Tcl_SetObjResult(interp, contents);
2830 if (objc == 2) {
2831 /*
2832 * If we are creating a link, this will actually just
2833 * be objv[3], and we don't own it
2834 */
2835 Tcl_DecrRefCount(contents);
2836 }
2837 return TCL_OK;
2838 }
2839
2840 /*
2841 *----------------------------------------------------------------------
2842 *
2843 * TestgetassocdataCmd --
2844 *
2845 * This procedure implements the "testgetassocdata" command. It is
2846 * used to test Tcl_GetAssocData.
2847 *
2848 * Results:
2849 * A standard Tcl result.
2850 *
2851 * Side effects:
2852 * None.
2853 *
2854 *----------------------------------------------------------------------
2855 */
2856
2857 static int
2858 TestgetassocdataCmd(
2859 ClientData clientData, /* Not used. */
2860 Tcl_Interp *interp, /* Current interpreter. */
2861 int argc, /* Number of arguments. */
2862 const char **argv) /* Argument strings. */
2863 {
2864 char *res;
2865
2866 if (argc != 2) {
2867 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
2868 " data_key\"", NULL);
2869 return TCL_ERROR;
2870 }
2871 res = (char *) Tcl_GetAssocData(interp, argv[1], NULL);
2872 if (res != NULL) {
2873 Tcl_AppendResult(interp, res, NULL);
2874 }
2875 return TCL_OK;
2876 }
2877
2878 /*
2879 *----------------------------------------------------------------------
2880 *
2881 * TestgetplatformCmd --
2882 *
2883 * This procedure implements the "testgetplatform" command. It is
2884 * used to retrieve the value of the tclPlatform global variable.
2885 *
2886 * Results:
2887 * A standard Tcl result.
2888 *
2889 * Side effects:
2890 * None.
2891 *
2892 *----------------------------------------------------------------------
2893 */
2894
2895 static int
2896 TestgetplatformCmd(
2897 ClientData clientData, /* Not used. */
2898 Tcl_Interp *interp, /* Current interpreter. */
2899 int argc, /* Number of arguments. */
2900 const char **argv) /* Argument strings. */
2901 {
2902 static const char *const platformStrings[] = { "unix", "mac", "windows" };
2903 TclPlatformType *platform;
2904
2905 platform = TclGetPlatform();
2906
2907 if (argc != 1) {
2908 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
2909 (char *)NULL);
2910 return TCL_ERROR;
2911 }
2912
2913 Tcl_AppendResult(interp, platformStrings[*platform], NULL);
2914 return TCL_OK;
2915 }
2916
2917 /*
2918 *----------------------------------------------------------------------
2919 *
2920 * TestinterpdeleteCmd --
2921 *
2922 * This procedure tests the code in tclInterp.c that deals with
2923 * interpreter deletion. It deletes a user-specified interpreter
2924 * from the hierarchy, and subsequent code checks integrity.
2925 *
2926 * Results:
2927 * A standard Tcl result.
2928 *
2929 * Side effects:
2930 * Deletes one or more interpreters.
2931 *
2932 *----------------------------------------------------------------------
2933 */
2934
2935 static int
2936 TestinterpdeleteCmd(
2937 ClientData dummy, /* Not used. */
2938 Tcl_Interp *interp, /* Current interpreter. */
2939 int argc, /* Number of arguments. */
2940 const char **argv) /* Argument strings. */
2941 {
2942 Tcl_Interp *childToDelete;
2943
2944 if (argc != 2) {
2945 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
2946 " path\"", NULL);
2947 return TCL_ERROR;
2948 }
2949 childToDelete = Tcl_GetChild(interp, argv[1]);
2950 if (childToDelete == NULL) {
2951 return TCL_ERROR;
2952 }
2953 Tcl_DeleteInterp(childToDelete);
2954 return TCL_OK;
2955 }
2956
2957 /*
2958 *----------------------------------------------------------------------
2959 *
2960 * TestlinkCmd --
2961 *
2962 * This procedure implements the "testlink" command. It is used
2963 * to test Tcl_LinkVar and related library procedures.
2964 *
2965 * Results:
2966 * A standard Tcl result.
2967 *
2968 * Side effects:
2969 * Creates and deletes various variable links, plus returns
2970 * values of the linked variables.
2971 *
2972 *----------------------------------------------------------------------
2973 */
2974
2975 static int
2976 TestlinkCmd(
2977 ClientData dummy, /* Not used. */
2978 Tcl_Interp *interp, /* Current interpreter. */
2979 int argc, /* Number of arguments. */
2980 const char **argv) /* Argument strings. */
2981 {
2982 static int intVar = 43;
2983 static int boolVar = 4;
2984 static double realVar = 1.23;
2985 static Tcl_WideInt wideVar = 79;
2986 static char *stringVar = NULL;
2987 static char charVar = '@';
2988 static unsigned char ucharVar = 130;
2989 static short shortVar = 3000;
2990 static unsigned short ushortVar = 60000;
2991 static unsigned int uintVar = 0xBEEFFEED;
2992 static long longVar = 123456789L;
2993 static unsigned long ulongVar = 3456789012UL;
2994 static float floatVar = 4.5;
2995 static Tcl_WideUInt uwideVar = 123;
2996 static int created = 0;
2997 char buffer[2*TCL_DOUBLE_SPACE];
2998 int writable, flag;
2999 Tcl_Obj *tmp;
3000
3001 if (argc < 2) {
3002 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
3003 " option ?arg arg arg arg arg arg arg arg arg arg arg arg"
3004 " arg arg?\"", NULL);
3005 return TCL_ERROR;
3006 }
3007 if (strcmp(argv[1], "create") == 0) {
3008 if (argc != 16) {
3009 Tcl_AppendResult(interp, "wrong # args: should be \"",
3010 argv[0], " ", argv[1],
3011 " intRO realRO boolRO stringRO wideRO charRO ucharRO shortRO"
3012 " ushortRO uintRO longRO ulongRO floatRO uwideRO\"", NULL);
3013 return TCL_ERROR;
3014 }
3015 if (created) {
3016 Tcl_UnlinkVar(interp, "int");
3017 Tcl_UnlinkVar(interp, "real");
3018 Tcl_UnlinkVar(interp, "bool");
3019 Tcl_UnlinkVar(interp, "string");
3020 Tcl_UnlinkVar(interp, "wide");
3021 Tcl_UnlinkVar(interp, "char");
3022 Tcl_UnlinkVar(interp, "uchar");
3023 Tcl_UnlinkVar(interp, "short");
3024 Tcl_UnlinkVar(interp, "ushort");
3025 Tcl_UnlinkVar(interp, "uint");
3026 Tcl_UnlinkVar(interp, "long");
3027 Tcl_UnlinkVar(interp, "ulong");
3028 Tcl_UnlinkVar(interp, "float");
3029 Tcl_UnlinkVar(interp, "uwide");
3030 }
3031 created = 1;
3032 if (Tcl_GetBoolean(interp, argv[2], &writable) != TCL_OK) {
3033 return TCL_ERROR;
3034 }
3035 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3036 if (Tcl_LinkVar(interp, "int", (char *)&intVar,
3037 TCL_LINK_INT | flag) != TCL_OK) {
3038 return TCL_ERROR;
3039 }
3040 if (Tcl_GetBoolean(interp, argv[3], &writable) != TCL_OK) {
3041 return TCL_ERROR;
3042 }
3043 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3044 if (Tcl_LinkVar(interp, "real", (char *)&realVar,
3045 TCL_LINK_DOUBLE | flag) != TCL_OK) {
3046 return TCL_ERROR;
3047 }
3048 if (Tcl_GetBoolean(interp, argv[4], &writable) != TCL_OK) {
3049 return TCL_ERROR;
3050 }
3051 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3052 if (Tcl_LinkVar(interp, "bool", (char *)&boolVar,
3053 TCL_LINK_BOOLEAN | flag) != TCL_OK) {
3054 return TCL_ERROR;
3055 }
3056 if (Tcl_GetBoolean(interp, argv[5], &writable) != TCL_OK) {
3057 return TCL_ERROR;
3058 }
3059 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3060 if (Tcl_LinkVar(interp, "string", (char *)&stringVar,
3061 TCL_LINK_STRING | flag) != TCL_OK) {
3062 return TCL_ERROR;
3063 }
3064 if (Tcl_GetBoolean(interp, argv[6], &writable) != TCL_OK) {
3065 return TCL_ERROR;
3066 }
3067 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3068 if (Tcl_LinkVar(interp, "wide", (char *)&wideVar,
3069 TCL_LINK_WIDE_INT | flag) != TCL_OK) {
3070 return TCL_ERROR;
3071 }
3072 if (Tcl_GetBoolean(interp, argv[7], &writable) != TCL_OK) {
3073 return TCL_ERROR;
3074 }
3075 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3076 if (Tcl_LinkVar(interp, "char", (char *)&charVar,
3077 TCL_LINK_CHAR | flag) != TCL_OK) {
3078 return TCL_ERROR;
3079 }
3080 if (Tcl_GetBoolean(interp, argv[8], &writable) != TCL_OK) {
3081 return TCL_ERROR;
3082 }
3083 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3084 if (Tcl_LinkVar(interp, "uchar", (char *)&ucharVar,
3085 TCL_LINK_UCHAR | flag) != TCL_OK) {
3086 return TCL_ERROR;
3087 }
3088 if (Tcl_GetBoolean(interp, argv[9], &writable) != TCL_OK) {
3089 return TCL_ERROR;
3090 }
3091 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3092 if (Tcl_LinkVar(interp, "short", (char *)&shortVar,
3093 TCL_LINK_SHORT | flag) != TCL_OK) {
3094 return TCL_ERROR;
3095 }
3096 if (Tcl_GetBoolean(interp, argv[10], &writable) != TCL_OK) {
3097 return TCL_ERROR;
3098 }
3099 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3100 if (Tcl_LinkVar(interp, "ushort", (char *)&ushortVar,
3101 TCL_LINK_USHORT | flag) != TCL_OK) {
3102 return TCL_ERROR;
3103 }
3104 if (Tcl_GetBoolean(interp, argv[11], &writable) != TCL_OK) {
3105 return TCL_ERROR;
3106 }
3107 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3108 if (Tcl_LinkVar(interp, "uint", (char *)&uintVar,
3109 TCL_LINK_UINT | flag) != TCL_OK) {
3110 return TCL_ERROR;
3111 }
3112 if (Tcl_GetBoolean(interp, argv[12], &writable) != TCL_OK) {
3113 return TCL_ERROR;
3114 }
3115 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3116 if (Tcl_LinkVar(interp, "long", (char *)&longVar,
3117 TCL_LINK_LONG | flag) != TCL_OK) {
3118 return TCL_ERROR;
3119 }
3120 if (Tcl_GetBoolean(interp, argv[13], &writable) != TCL_OK) {
3121 return TCL_ERROR;
3122 }
3123 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3124 if (Tcl_LinkVar(interp, "ulong", (char *)&ulongVar,
3125 TCL_LINK_ULONG | flag) != TCL_OK) {
3126 return TCL_ERROR;
3127 }
3128 if (Tcl_GetBoolean(interp, argv[14], &writable) != TCL_OK) {
3129 return TCL_ERROR;
3130 }
3131 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3132 if (Tcl_LinkVar(interp, "float", (char *)&floatVar,
3133 TCL_LINK_FLOAT | flag) != TCL_OK) {
3134 return TCL_ERROR;
3135 }
3136 if (Tcl_GetBoolean(interp, argv[15], &writable) != TCL_OK) {
3137 return TCL_ERROR;
3138 }
3139 flag = writable ? 0 : TCL_LINK_READ_ONLY;
3140 if (Tcl_LinkVar(interp, "uwide", (char *)&uwideVar,
3141 TCL_LINK_WIDE_UINT | flag) != TCL_OK) {
3142 return TCL_ERROR;
3143 }
3144
3145 } else if (strcmp(argv[1], "delete") == 0) {
3146 Tcl_UnlinkVar(interp, "int");
3147 Tcl_UnlinkVar(interp, "real");
3148 Tcl_UnlinkVar(interp, "bool");
3149 Tcl_UnlinkVar(interp, "string");
3150 Tcl_UnlinkVar(interp, "wide");
3151 Tcl_UnlinkVar(interp, "char");
3152 Tcl_UnlinkVar(interp, "uchar");
3153 Tcl_UnlinkVar(interp, "short");
3154 Tcl_UnlinkVar(interp, "ushort");
3155 Tcl_UnlinkVar(interp, "uint");
3156 Tcl_UnlinkVar(interp, "long");
3157 Tcl_UnlinkVar(interp, "ulong");
3158 Tcl_UnlinkVar(interp, "float");
3159 Tcl_UnlinkVar(interp, "uwide");
3160 created = 0;
3161 } else if (strcmp(argv[1], "get") == 0) {
3162 TclFormatInt(buffer, intVar);
3163 Tcl_AppendElement(interp, buffer);
3164 Tcl_PrintDouble(NULL, realVar, buffer);
3165 Tcl_AppendElement(interp, buffer);
3166 TclFormatInt(buffer, boolVar);
3167 Tcl_AppendElement(interp, buffer);
3168 Tcl_AppendElement(interp, (stringVar == NULL) ? "-" : stringVar);
3169 /*
3170 * Wide ints only have an object-based interface.
3171 */
3172 tmp = Tcl_NewWideIntObj(wideVar);
3173 Tcl_AppendElement(interp, Tcl_GetString(tmp));
3174 Tcl_DecrRefCount(tmp);
3175 TclFormatInt(buffer, (int) charVar);
3176 Tcl_AppendElement(interp, buffer);
3177 TclFormatInt(buffer, (int) ucharVar);
3178 Tcl_AppendElement(interp, buffer);
3179 TclFormatInt(buffer, (int) shortVar);
3180 Tcl_AppendElement(interp, buffer);
3181 TclFormatInt(buffer, (int) ushortVar);
3182 Tcl_AppendElement(interp, buffer);
3183 TclFormatInt(buffer, (int) uintVar);
3184 Tcl_AppendElement(interp, buffer);
3185 tmp = Tcl_NewLongObj(longVar);
3186 Tcl_AppendElement(interp, Tcl_GetString(tmp));
3187 Tcl_DecrRefCount(tmp);
3188 tmp = Tcl_NewLongObj((long)ulongVar);
3189 Tcl_AppendElement(interp, Tcl_GetString(tmp));
3190 Tcl_DecrRefCount(tmp);
3191 Tcl_PrintDouble(NULL, (double)floatVar, buffer);
3192 Tcl_AppendElement(interp, buffer);
3193 tmp = Tcl_NewWideIntObj((Tcl_WideInt)uwideVar);
3194 Tcl_AppendElement(interp, Tcl_GetString(tmp));
3195 Tcl_DecrRefCount(tmp);
3196 } else if (strcmp(argv[1], "set") == 0) {
3197 int v;
3198
3199 if (argc != 16) {
3200 Tcl_AppendResult(interp, "wrong # args: should be \"",
3201 argv[0], " ", argv[1],
3202 " intValue realValue boolValue stringValue wideValue"
3203 " charValue ucharValue shortValue ushortValue uintValue"
3204 " longValue ulongValue floatValue uwideValue\"", NULL);
3205 return TCL_ERROR;
3206 }
3207 if (argv[2][0] != 0) {
3208 if (Tcl_GetInt(interp, argv[2], &intVar) != TCL_OK) {
3209 return TCL_ERROR;
3210 }
3211 }
3212 if (argv[3][0] != 0) {
3213 if (Tcl_GetDouble(interp, argv[3], &realVar) != TCL_OK) {
3214 return TCL_ERROR;
3215 }
3216 }
3217 if (argv[4][0] != 0) {
3218 if (Tcl_GetInt(interp, argv[4], &boolVar) != TCL_OK) {
3219 return TCL_ERROR;
3220 }
3221 }
3222 if (argv[5][0] != 0) {
3223 if (stringVar != NULL) {
3224 ckfree(stringVar);
3225 }
3226 if (strcmp(argv[5], "-") == 0) {
3227 stringVar = NULL;
3228 } else {
3229 stringVar = (char *)ckalloc(strlen(argv[5]) + 1);
3230 strcpy(stringVar, argv[5]);
3231 }
3232 }
3233 if (argv[6][0] != 0) {
3234 tmp = Tcl_NewStringObj(argv[6], -1);
3235 if (Tcl_GetWideIntFromObj(interp, tmp, &wideVar) != TCL_OK) {
3236 Tcl_DecrRefCount(tmp);
3237 return TCL_ERROR;
3238 }
3239 Tcl_DecrRefCount(tmp);
3240 }
3241 if (argv[7][0]) {
3242 if (Tcl_GetInt(interp, argv[7], &v) != TCL_OK) {
3243 return TCL_ERROR;
3244 }
3245 charVar = (char) v;
3246 }
3247 if (argv[8][0]) {
3248 if (Tcl_GetInt(interp, argv[8], &v) != TCL_OK) {
3249 return TCL_ERROR;
3250 }
3251 ucharVar = (unsigned char) v;
3252 }
3253 if (argv[9][0]) {
3254 if (Tcl_GetInt(interp, argv[9], &v) != TCL_OK) {
3255 return TCL_ERROR;
3256 }
3257 shortVar = (short) v;
3258 }
3259 if (argv[10][0]) {
3260 if (Tcl_GetInt(interp, argv[10], &v) != TCL_OK) {
3261 return TCL_ERROR;
3262 }
3263 ushortVar = (unsigned short) v;
3264 }
3265 if (argv[11][0]) {
3266 if (Tcl_GetInt(interp, argv[11], &v) != TCL_OK) {
3267 return TCL_ERROR;
3268 }
3269 uintVar = (unsigned int) v;
3270 }
3271 if (argv[12][0]) {
3272 if (Tcl_GetInt(interp, argv[12], &v) != TCL_OK) {
3273 return TCL_ERROR;
3274 }
3275 longVar = (long) v;
3276 }
3277 if (argv[13][0]) {
3278 if (Tcl_GetInt(interp, argv[13], &v) != TCL_OK) {
3279 return TCL_ERROR;
3280 }
3281 ulongVar = (unsigned long) v;
3282 }
3283 if (argv[14][0]) {
3284 double d;
3285 if (Tcl_GetDouble(interp, argv[14], &d) != TCL_OK) {
3286 return TCL_ERROR;
3287 }
3288 floatVar = (float) d;
3289 }
3290 if (argv[15][0]) {
3291 Tcl_WideInt w;
3292 tmp = Tcl_NewStringObj(argv[15], -1);
3293 if (Tcl_GetWideIntFromObj(interp, tmp, &w) != TCL_OK) {
3294 Tcl_DecrRefCount(tmp);
3295 return TCL_ERROR;
3296 }
3297 Tcl_DecrRefCount(tmp);
3298 uwideVar = (Tcl_WideUInt)w;
3299 }
3300 } else if (strcmp(argv[1], "update") == 0) {
3301 int v;
3302
3303 if (argc != 16) {
3304 Tcl_AppendResult(interp, "wrong # args: should be \"",
3305 argv[0], " ", argv[1],
3306 " intValue realValue boolValue stringValue wideValue"
3307 " charValue ucharValue shortValue ushortValue uintValue"
3308 " longValue ulongValue floatValue uwideValue\"", NULL);
3309 return TCL_ERROR;
3310 }
3311 if (argv[2][0] != 0) {
3312 if (Tcl_GetInt(interp, argv[2], &intVar) != TCL_OK) {
3313 return TCL_ERROR;
3314 }
3315 Tcl_UpdateLinkedVar(interp, "int");
3316 }
3317 if (argv[3][0] != 0) {
3318 if (Tcl_GetDouble(interp, argv[3], &realVar) != TCL_OK) {
3319 return TCL_ERROR;
3320 }
3321 Tcl_UpdateLinkedVar(interp, "real");
3322 }
3323 if (argv[4][0] != 0) {
3324 if (Tcl_GetInt(interp, argv[4], &boolVar) != TCL_OK) {
3325 return TCL_ERROR;
3326 }
3327 Tcl_UpdateLinkedVar(interp, "bool");
3328 }
3329 if (argv[5][0] != 0) {
3330 if (stringVar != NULL) {
3331 ckfree(stringVar);
3332 }
3333 if (strcmp(argv[5], "-") == 0) {
3334 stringVar = NULL;
3335 } else {
3336 stringVar = (char *)ckalloc(strlen(argv[5]) + 1);
3337 strcpy(stringVar, argv[5]);
3338 }
3339 Tcl_UpdateLinkedVar(interp, "string");
3340 }
3341 if (argv[6][0] != 0) {
3342 tmp = Tcl_NewStringObj(argv[6], -1);
3343 if (Tcl_GetWideIntFromObj(interp, tmp, &wideVar) != TCL_OK) {
3344 Tcl_DecrRefCount(tmp);
3345 return TCL_ERROR;
3346 }
3347 Tcl_DecrRefCount(tmp);
3348 Tcl_UpdateLinkedVar(interp, "wide");
3349 }
3350 if (argv[7][0]) {
3351 if (Tcl_GetInt(interp, argv[7], &v) != TCL_OK) {
3352 return TCL_ERROR;
3353 }
3354 charVar = (char) v;
3355 Tcl_UpdateLinkedVar(interp, "char");
3356 }
3357 if (argv[8][0]) {
3358 if (Tcl_GetInt(interp, argv[8], &v) != TCL_OK) {
3359 return TCL_ERROR;
3360 }
3361 ucharVar = (unsigned char) v;
3362 Tcl_UpdateLinkedVar(interp, "uchar");
3363 }
3364 if (argv[9][0]) {
3365 if (Tcl_GetInt(interp, argv[9], &v) != TCL_OK) {
3366 return TCL_ERROR;
3367 }
3368 shortVar = (short) v;
3369 Tcl_UpdateLinkedVar(interp, "short");
3370 }
3371 if (argv[10][0]) {
3372 if (Tcl_GetInt(interp, argv[10], &v) != TCL_OK) {
3373 return TCL_ERROR;
3374 }
3375 ushortVar = (unsigned short) v;
3376 Tcl_UpdateLinkedVar(interp, "ushort");
3377 }
3378 if (argv[11][0]) {
3379 if (Tcl_GetInt(interp, argv[11], &v) != TCL_OK) {
3380 return TCL_ERROR;
3381 }
3382 uintVar = (unsigned int) v;
3383 Tcl_UpdateLinkedVar(interp, "uint");
3384 }
3385 if (argv[12][0]) {
3386 if (Tcl_GetInt(interp, argv[12], &v) != TCL_OK) {
3387 return TCL_ERROR;
3388 }
3389 longVar = (long) v;
3390 Tcl_UpdateLinkedVar(interp, "long");
3391 }
3392 if (argv[13][0]) {
3393 if (Tcl_GetInt(interp, argv[13], &v) != TCL_OK) {
3394 return TCL_ERROR;
3395 }
3396 ulongVar = (unsigned long) v;
3397 Tcl_UpdateLinkedVar(interp, "ulong");
3398 }
3399 if (argv[14][0]) {
3400 double d;
3401 if (Tcl_GetDouble(interp, argv[14], &d) != TCL_OK) {
3402 return TCL_ERROR;
3403 }
3404 floatVar = (float) d;
3405 Tcl_UpdateLinkedVar(interp, "float");
3406 }
3407 if (argv[15][0]) {
3408 Tcl_WideInt w;
3409 tmp = Tcl_NewStringObj(argv[15], -1);
3410 if (Tcl_GetWideIntFromObj(interp, tmp, &w) != TCL_OK) {
3411 Tcl_DecrRefCount(tmp);
3412 return TCL_ERROR;
3413 }
3414 Tcl_DecrRefCount(tmp);
3415 uwideVar = (Tcl_WideUInt)w;
3416 Tcl_UpdateLinkedVar(interp, "uwide");
3417 }
3418 } else {
3419 Tcl_AppendResult(interp, "bad option \"", argv[1],
3420 "\": should be create, delete, get, set, or update", NULL);
3421 return TCL_ERROR;
3422 }
3423 return TCL_OK;
3424 }
3425
3426 /*
3427 *----------------------------------------------------------------------
3428 *
3429 * TestlocaleCmd --
3430 *
3431 * This procedure implements the "testlocale" command. It is used
3432 * to test the effects of setting different locales in Tcl.
3433 *
3434 * Results:
3435 * A standard Tcl result.
3436 *
3437 * Side effects:
3438 * Modifies the current C locale.
3439 *
3440 *----------------------------------------------------------------------
3441 */
3442
3443 static int
3444 TestlocaleCmd(
3445 ClientData clientData, /* Not used. */
3446 Tcl_Interp *interp, /* Current interpreter. */
3447 int objc, /* Number of arguments. */
3448 Tcl_Obj *const objv[]) /* The argument objects. */
3449 {
3450 int index;
3451 const char *locale;
3452
3453 static const char *const optionStrings[] = {
3454 "ctype", "numeric", "time", "collate", "monetary",
3455 "all", NULL
3456 };
3457 static const int lcTypes[] = {
3458 LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY,
3459 LC_ALL
3460 };
3461
3462 /*
3463 * LC_CTYPE, etc. correspond to the indices for the strings.
3464 */
3465
3466 if (objc < 2 || objc > 3) {
3467 Tcl_WrongNumArgs(interp, 1, objv, "category ?locale?");
3468 return TCL_ERROR;
3469 }
3470
3471 if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
3472 &index) != TCL_OK) {
3473 return TCL_ERROR;
3474 }
3475
3476 if (objc == 3) {
3477 locale = Tcl_GetString(objv[2]);
3478 } else {
3479 locale = NULL;
3480 }
3481 locale = setlocale(lcTypes[index], locale);
3482 if (locale) {
3483 Tcl_SetStringObj(Tcl_GetObjResult(interp), locale, -1);
3484 }
3485 return TCL_OK;
3486 }
3487
3488 /*
3489 *----------------------------------------------------------------------
3490 *
3491 * TestMathFunc --
3492 *
3493 * This is a user-defined math procedure to test out math procedures
3494 * with no arguments.
3495 *
3496 * Results:
3497 * A normal Tcl completion code.
3498 *
3499 * Side effects:
3500 * None.
3501 *
3502 *----------------------------------------------------------------------
3503 */
3504
3505 static int
3506 TestMathFunc(
3507 ClientData clientData, /* Integer value to return. */
3508 Tcl_Interp *interp, /* Not used. */
3509 Tcl_Value *args, /* Not used. */
3510 Tcl_Value *resultPtr) /* Where to store result. */
3511 {
3512 resultPtr->type = TCL_INT;
3513 resultPtr->intValue = PTR2INT(clientData);
3514 return TCL_OK;
3515 }
3516
3517 /*
3518 *----------------------------------------------------------------------
3519 *
3520 * TestMathFunc2 --
3521 *
3522 * This is a user-defined math procedure to test out math procedures
3523 * that do have arguments, in this case 2.
3524 *
3525 * Results:
3526 * A normal Tcl completion code.
3527 *
3528 * Side effects:
3529 * None.
3530 *
3531 *----------------------------------------------------------------------
3532 */
3533
3534 static int
3535 TestMathFunc2(
3536 ClientData clientData, /* Integer value to return. */
3537 Tcl_Interp *interp, /* Used to report errors. */
3538 Tcl_Value *args, /* Points to an array of two Tcl_Value structs
3539 * for the two arguments. */
3540 Tcl_Value *resultPtr) /* Where to store the result. */
3541 {
3542 int result = TCL_OK;
3543
3544 /*
3545 * Return the maximum of the two arguments with the correct type.
3546 */
3547
3548 if (args[0].type == TCL_INT) {
3549 int i0 = args[0].intValue;
3550
3551 if (args[1].type == TCL_INT) {
3552 int i1 = args[1].intValue;
3553
3554 resultPtr->type = TCL_INT;
3555 resultPtr->intValue = ((i0 > i1)? i0 : i1);
3556 } else if (args[1].type == TCL_DOUBLE) {
3557 double d0 = i0;
3558 double d1 = args[1].doubleValue;
3559
3560 resultPtr->type = TCL_DOUBLE;
3561 resultPtr->doubleValue = ((d0 > d1)? d0 : d1);
3562 } else if (args[1].type == TCL_WIDE_INT) {
3563 Tcl_WideInt w0 = Tcl_LongAsWide(i0);
3564 Tcl_WideInt w1 = args[1].wideValue;
3565
3566 resultPtr->type = TCL_WIDE_INT;
3567 resultPtr->wideValue = ((w0 > w1)? w0 : w1);
3568 } else {
3569 Tcl_SetResult(interp, "T3: wrong type for arg 2", TCL_STATIC);
3570 result = TCL_ERROR;
3571 }
3572 } else if (args[0].type == TCL_DOUBLE) {
3573 double d0 = args[0].doubleValue;
3574
3575 if (args[1].type == TCL_INT) {
3576 double d1 = args[1].intValue;
3577
3578 resultPtr->type = TCL_DOUBLE;
3579 resultPtr->doubleValue = ((d0 > d1)? d0 : d1);
3580 } else if (args[1].type == TCL_DOUBLE) {
3581 double d1 = args[1].doubleValue;
3582
3583 resultPtr->type = TCL_DOUBLE;
3584 resultPtr->doubleValue = ((d0 > d1)? d0 : d1);
3585 } else if (args[1].type == TCL_WIDE_INT) {
3586 double d1 = Tcl_WideAsDouble(args[1].wideValue);
3587
3588 resultPtr->type = TCL_DOUBLE;
3589 resultPtr->doubleValue = ((d0 > d1)? d0 : d1);
3590 } else {
3591 Tcl_SetResult(interp, "T3: wrong type for arg 2", TCL_STATIC);
3592 result = TCL_ERROR;
3593 }
3594 } else if (args[0].type == TCL_WIDE_INT) {
3595 Tcl_WideInt w0 = args[0].wideValue;
3596
3597 if (args[1].type == TCL_INT) {
3598 Tcl_WideInt w1 = Tcl_LongAsWide(args[1].intValue);
3599
3600 resultPtr->type = TCL_WIDE_INT;
3601 resultPtr->wideValue = ((w0 > w1)? w0 : w1);
3602 } else if (args[1].type == TCL_DOUBLE) {
3603 double d0 = Tcl_WideAsDouble(w0);
3604 double d1 = args[1].doubleValue;
3605
3606 resultPtr->type = TCL_DOUBLE;
3607 resultPtr->doubleValue = ((d0 > d1)? d0 : d1);
3608 } else if (args[1].type == TCL_WIDE_INT) {
3609 Tcl_WideInt w1 = args[1].wideValue;
3610
3611 resultPtr->type = TCL_WIDE_INT;
3612 resultPtr->wideValue = ((w0 > w1)? w0 : w1);
3613 } else {
3614 Tcl_SetResult(interp, "T3: wrong type for arg 2", TCL_STATIC);
3615 result = TCL_ERROR;
3616 }
3617 } else {
3618 Tcl_SetResult(interp, "T3: wrong type for arg 1", TCL_STATIC);
3619 result = TCL_ERROR;
3620 }
3621 return result;
3622 }
3623
3624 /*
3625 *----------------------------------------------------------------------
3626 *
3627 * CleanupTestSetassocdataTests --
3628 *
3629 * This function is called when an interpreter is deleted to clean
3630 * up any data left over from running the testsetassocdata command.
3631 *
3632 * Results:
3633 * None.
3634 *
3635 * Side effects:
3636 * Releases storage.
3637 *
3638 *----------------------------------------------------------------------
3639 */
3640
3641 static void
3642 CleanupTestSetassocdataTests(
3643 ClientData clientData, /* Data to be released. */
3644 Tcl_Interp *interp) /* Interpreter being deleted. */
3645 {
3646 ckfree(clientData);
3647 }
3648
3649 /*
3650 *----------------------------------------------------------------------
3651 *
3652 * TestparserObjCmd --
3653 *
3654 * This procedure implements the "testparser" command. It is
3655 * used for testing the new Tcl script parser in Tcl 8.1.
3656 *
3657 * Results:
3658 * A standard Tcl result.
3659 *
3660 * Side effects:
3661 * None.
3662 *
3663 *----------------------------------------------------------------------
3664 */
3665
3666 static int
3667 TestparserObjCmd(
3668 ClientData clientData, /* Not used. */
3669 Tcl_Interp *interp, /* Current interpreter. */
3670 int objc, /* Number of arguments. */
3671 Tcl_Obj *const objv[]) /* The argument objects. */
3672 {
3673 const char *script;
3674 int length, dummy;
3675 Tcl_Parse parse;
3676
3677 if (objc != 3) {
3678 Tcl_WrongNumArgs(interp, 1, objv, "script length");
3679 return TCL_ERROR;
3680 }
3681 script = Tcl_GetStringFromObj(objv[1], &dummy);
3682 if (Tcl_GetIntFromObj(interp, objv[2], &length)) {
3683 return TCL_ERROR;
3684 }
3685 if (length == 0) {
3686 length = dummy;
3687 }
3688 if (Tcl_ParseCommand(interp, script, length, 0, &parse) != TCL_OK) {
3689 Tcl_AddErrorInfo(interp, "\n (remainder of script: \"");
3690 Tcl_AddErrorInfo(interp, parse.term);
3691 Tcl_AddErrorInfo(interp, "\")");
3692 return TCL_ERROR;
3693 }
3694
3695 /*
3696 * The parse completed successfully. Just print out the contents
3697 * of the parse structure into the interpreter's result.
3698 */
3699
3700 PrintParse(interp, &parse);
3701 Tcl_FreeParse(&parse);
3702 return TCL_OK;
3703 }
3704
3705 /*
3706 *----------------------------------------------------------------------
3707 *
3708 * TestexprparserObjCmd --
3709 *
3710 * This procedure implements the "testexprparser" command. It is
3711 * used for testing the new Tcl expression parser in Tcl 8.1.
3712 *
3713 * Results:
3714 * A standard Tcl result.
3715 *
3716 * Side effects:
3717 * None.
3718 *
3719 *----------------------------------------------------------------------
3720 */
3721
3722 static int
3723 TestexprparserObjCmd(
3724 ClientData clientData, /* Not used. */
3725 Tcl_Interp *interp, /* Current interpreter. */
3726 int objc, /* Number of arguments. */
3727 Tcl_Obj *const objv[]) /* The argument objects. */
3728 {
3729 const char *script;
3730 int length, dummy;
3731 Tcl_Parse parse;
3732
3733 if (objc != 3) {
3734 Tcl_WrongNumArgs(interp, 1, objv, "expr length");
3735 return TCL_ERROR;
3736 }
3737 script = Tcl_GetStringFromObj(objv[1], &dummy);
3738 if (Tcl_GetIntFromObj(interp, objv[2], &length)) {
3739 return TCL_ERROR;
3740 }
3741 if (length == 0) {
3742 length = dummy;
3743 }
3744 parse.commentStart = NULL;
3745 parse.commentSize = 0;
3746 parse.commandStart = NULL;
3747 parse.commandSize = 0;
3748 if (Tcl_ParseExpr(interp, script, length, &parse) != TCL_OK) {
3749 Tcl_AddErrorInfo(interp, "\n (remainder of expr: \"");
3750 Tcl_AddErrorInfo(interp, parse.term);
3751 Tcl_AddErrorInfo(interp, "\")");
3752 return TCL_ERROR;
3753 }
3754
3755 /*
3756 * The parse completed successfully. Just print out the contents
3757 * of the parse structure into the interpreter's result.
3758 */
3759
3760 PrintParse(interp, &parse);
3761 Tcl_FreeParse(&parse);
3762 return TCL_OK;
3763 }
3764
3765 /*
3766 *----------------------------------------------------------------------
3767 *
3768 * PrintParse --
3769 *
3770 * This procedure prints out the contents of a Tcl_Parse structure
3771 * in the result of an interpreter.
3772 *
3773 * Results:
3774 * Interp's result is set to a prettily formatted version of the
3775 * contents of parsePtr.
3776 *
3777 * Side effects:
3778 * None.
3779 *
3780 *----------------------------------------------------------------------
3781 */
3782
3783 static void
3784 PrintParse(
3785 Tcl_Interp *interp, /* Interpreter whose result is to be set to
3786 * the contents of a parse structure. */
3787 Tcl_Parse *parsePtr) /* Parse structure to print out. */
3788 {
3789 Tcl_Obj *objPtr;
3790 const char *typeString;
3791 Tcl_Token *tokenPtr;
3792 int i;
3793
3794 objPtr = Tcl_GetObjResult(interp);
3795 if (parsePtr->commentSize > 0) {
3796 Tcl_ListObjAppendElement(NULL, objPtr,
3797 Tcl_NewStringObj(parsePtr->commentStart,
3798 parsePtr->commentSize));
3799 } else {
3800 Tcl_ListObjAppendElement(NULL, objPtr, Tcl_NewStringObj("-", 1));
3801 }
3802 Tcl_ListObjAppendElement(NULL, objPtr,
3803 Tcl_NewStringObj(parsePtr->commandStart, parsePtr->commandSize));
3804 Tcl_ListObjAppendElement(NULL, objPtr,
3805 Tcl_NewIntObj(parsePtr->numWords));
3806 for (i = 0; i < parsePtr->numTokens; i++) {
3807 tokenPtr = &parsePtr->tokenPtr[i];
3808 switch (tokenPtr->type) {
3809 case TCL_TOKEN_EXPAND_WORD:
3810 typeString = "expand";
3811 break;
3812 case TCL_TOKEN_WORD:
3813 typeString = "word";
3814 break;
3815 case TCL_TOKEN_SIMPLE_WORD:
3816 typeString = "simple";
3817 break;
3818 case TCL_TOKEN_TEXT:
3819 typeString = "text";
3820 break;
3821 case TCL_TOKEN_BS:
3822 typeString = "backslash";
3823 break;
3824 case TCL_TOKEN_COMMAND:
3825 typeString = "command";
3826 break;
3827 case TCL_TOKEN_VARIABLE:
3828 typeString = "variable";
3829 break;
3830 case TCL_TOKEN_SUB_EXPR:
3831 typeString = "subexpr";
3832 break;
3833 case TCL_TOKEN_OPERATOR:
3834 typeString = "operator";
3835 break;
3836 default:
3837 typeString = "??";
3838 break;
3839 }
3840 Tcl_ListObjAppendElement(NULL, objPtr,
3841 Tcl_NewStringObj(typeString, -1));
3842 Tcl_ListObjAppendElement(NULL, objPtr,
3843 Tcl_NewStringObj(tokenPtr->start, tokenPtr->size));
3844 Tcl_ListObjAppendElement(NULL, objPtr,
3845 Tcl_NewIntObj(tokenPtr->numComponents));
3846 }
3847 Tcl_ListObjAppendElement(NULL, objPtr,
3848 parsePtr->commandStart ?
3849 Tcl_NewStringObj(parsePtr->commandStart + parsePtr->commandSize,
3850 -1) : Tcl_NewObj());
3851 }
3852
3853 /*
3854 *----------------------------------------------------------------------
3855 *
3856 * TestparsevarObjCmd --
3857 *
3858 * This procedure implements the "testparsevar" command. It is
3859 * used for testing Tcl_ParseVar.
3860 *
3861 * Results:
3862 * A standard Tcl result.
3863 *
3864 * Side effects:
3865 * None.
3866 *
3867 *----------------------------------------------------------------------
3868 */
3869
3870 static int
3871 TestparsevarObjCmd(
3872 ClientData clientData, /* Not used. */
3873 Tcl_Interp *interp, /* Current interpreter. */
3874 int objc, /* Number of arguments. */
3875 Tcl_Obj *const objv[]) /* The argument objects. */
3876 {
3877 const char *value, *name, *termPtr;
3878
3879 if (objc != 2) {
3880 Tcl_WrongNumArgs(interp, 1, objv, "varName");
3881 return TCL_ERROR;
3882 }
3883 name = Tcl_GetString(objv[1]);
3884 value = Tcl_ParseVar(interp, name, &termPtr);
3885 if (value == NULL) {
3886 return TCL_ERROR;
3887 }
3888
3889 Tcl_AppendElement(interp, value);
3890 Tcl_AppendElement(interp, termPtr);
3891 return TCL_OK;
3892 }
3893
3894 /*
3895 *----------------------------------------------------------------------
3896 *
3897 * TestparsevarnameObjCmd --
3898 *
3899 * This procedure implements the "testparsevarname" command. It is
3900 * used for testing the new Tcl script parser in Tcl 8.1.
3901 *
3902 * Results:
3903 * A standard Tcl result.
3904 *
3905 * Side effects:
3906 * None.
3907 *
3908 *----------------------------------------------------------------------
3909 */
3910
3911 static int
3912 TestparsevarnameObjCmd(
3913 ClientData clientData, /* Not used. */
3914 Tcl_Interp *interp, /* Current interpreter. */
3915 int objc, /* Number of arguments. */
3916 Tcl_Obj *const objv[]) /* The argument objects. */
3917 {
3918 const char *script;
3919 int append, length, dummy;
3920 Tcl_Parse parse;
3921
3922 if (objc != 4) {
3923 Tcl_WrongNumArgs(interp, 1, objv, "script length append");
3924 return TCL_ERROR;
3925 }
3926 script = Tcl_GetStringFromObj(objv[1], &dummy);
3927 if (Tcl_GetIntFromObj(interp, objv[2], &length)) {
3928 return TCL_ERROR;
3929 }
3930 if (length == 0) {
3931 length = dummy;
3932 }
3933 if (Tcl_GetIntFromObj(interp, objv[3], &append)) {
3934 return TCL_ERROR;
3935 }
3936 if (Tcl_ParseVarName(interp, script, length, &parse, append) != TCL_OK) {
3937 Tcl_AddErrorInfo(interp, "\n (remainder of script: \"");
3938 Tcl_AddErrorInfo(interp, parse.term);
3939 Tcl_AddErrorInfo(interp, "\")");
3940 return TCL_ERROR;
3941 }
3942
3943 /*
3944 * The parse completed successfully. Just print out the contents
3945 * of the parse structure into the interpreter's result.
3946 */
3947
3948 parse.commentSize = 0;
3949 parse.commandStart = script + parse.tokenPtr->size;
3950 parse.commandSize = 0;
3951 PrintParse(interp, &parse);
3952 Tcl_FreeParse(&parse);
3953 return TCL_OK;
3954 }
3955
3956 /*
3957 *----------------------------------------------------------------------
3958 *
3959 * TestprintObjCmd --
3960 *
3961 * This procedure implements the "testprint" command. It is
3962 * used for being able to test the Tcl_ObjPrintf() function.
3963 *
3964 * Results:
3965 * A standard Tcl result.
3966 *
3967 * Side effects:
3968 * None.
3969 *
3970 *----------------------------------------------------------------------
3971 */
3972
3973 static int
3974 TestprintObjCmd(
3975 void *clientData,
3976 Tcl_Interp *interp, /* Current interpreter. */
3977 int objc, /* Number of arguments. */
3978 Tcl_Obj *const objv[]) /* The argument objects. */
3979 {
3980 Tcl_WideInt argv1 = 0;
3981 long argv2;
3982
3983 if (objc != 3) {
3984 Tcl_WrongNumArgs(interp, 1, objv, "format longint");
3985 return TCL_OK;
3986 }
3987
3988 Tcl_GetWideIntFromObj(interp, objv[2], &argv1);
3989 argv2 = (long)argv1;
3990 Tcl_SetObjResult(interp, Tcl_ObjPrintf(Tcl_GetString(objv[1]), argv2, argv2, argv2, argv2));
3991 return TCL_OK;
3992 }
3993
3994 /*
3995 *----------------------------------------------------------------------
3996 *
3997 * TestregexpObjCmd --
3998 *
3999 * This procedure implements the "testregexp" command. It is used to give
4000 * a direct interface for regexp flags. It's identical to
4001 * Tcl_RegexpObjCmd except for the -xflags option, and the consequences
4002 * thereof (including the REG_EXPECT kludge).
4003 *
4004 * Results:
4005 * A standard Tcl result.
4006 *
4007 * Side effects:
4008 * See the user documentation.
4009 *
4010 *----------------------------------------------------------------------
4011 */
4012
4013 static int
4014 TestregexpObjCmd(
4015 ClientData dummy, /* Not used. */
4016 Tcl_Interp *interp, /* Current interpreter. */
4017 int objc, /* Number of arguments. */
4018 Tcl_Obj *const objv[]) /* Argument objects. */
4019 {
4020 int i, ii, indices, stringLength, match, about;
4021 int hasxflags, cflags, eflags;
4022 Tcl_RegExp regExpr;
4023 const char *string;
4024 Tcl_Obj *objPtr;
4025 Tcl_RegExpInfo info;
4026 static const char *const options[] = {
4027 "-indices", "-nocase", "-about", "-expanded",
4028 "-line", "-linestop", "-lineanchor",
4029 "-xflags",
4030 "--", NULL
4031 };
4032 enum optionsEnum {
4033 REGEXP_INDICES, REGEXP_NOCASE, REGEXP_ABOUT, REGEXP_EXPANDED,
4034 REGEXP_MULTI, REGEXP_NOCROSS, REGEXP_NEWL,
4035 REGEXP_XFLAGS,
4036 REGEXP_LAST
4037 };
4038
4039 indices = 0;
4040 about = 0;
4041 cflags = REG_ADVANCED;
4042 eflags = 0;
4043 hasxflags = 0;
4044
4045 for (i = 1; i < objc; i++) {
4046 const char *name;
4047 int index;
4048
4049 name = Tcl_GetString(objv[i]);
4050 if (name[0] != '-') {
4051 break;
4052 }
4053 if (Tcl_GetIndexFromObj(interp, objv[i], options, "switch", TCL_EXACT,
4054 &index) != TCL_OK) {
4055 return TCL_ERROR;
4056 }
4057 switch ((enum optionsEnum) index) {
4058 case REGEXP_INDICES:
4059 indices = 1;
4060 break;
4061 case REGEXP_NOCASE:
4062 cflags |= REG_ICASE;
4063 break;
4064 case REGEXP_ABOUT:
4065 about = 1;
4066 break;
4067 case REGEXP_EXPANDED:
4068 cflags |= REG_EXPANDED;
4069 break;
4070 case REGEXP_MULTI:
4071 cflags |= REG_NEWLINE;
4072 break;
4073 case REGEXP_NOCROSS:
4074 cflags |= REG_NLSTOP;
4075 break;
4076 case REGEXP_NEWL:
4077 cflags |= REG_NLANCH;
4078 break;
4079 case REGEXP_XFLAGS:
4080 hasxflags = 1;
4081 break;
4082 case REGEXP_LAST:
4083 i++;
4084 goto endOfForLoop;
4085 }
4086 }
4087
4088 endOfForLoop:
4089 if (objc - i < hasxflags + 2 - about) {
4090 Tcl_WrongNumArgs(interp, 1, objv,
4091 "?-switch ...? exp string ?matchVar? ?subMatchVar ...?");
4092 return TCL_ERROR;
4093 }
4094 objc -= i;
4095 objv += i;
4096
4097 if (hasxflags) {
4098 string = Tcl_GetStringFromObj(objv[0], &stringLength);
4099 TestregexpXflags(string, stringLength, &cflags, &eflags);
4100 objc--;
4101 objv++;
4102 }
4103
4104 regExpr = Tcl_GetRegExpFromObj(interp, objv[0], cflags);
4105 if (regExpr == NULL) {
4106 return TCL_ERROR;
4107 }
4108
4109 if (about) {
4110 if (TclRegAbout(interp, regExpr) < 0) {
4111 return TCL_ERROR;
4112 }
4113 return TCL_OK;
4114 }
4115
4116 objPtr = objv[1];
4117 match = Tcl_RegExpExecObj(interp, regExpr, objPtr, 0 /* offset */,
4118 objc-2 /* nmatches */, eflags);
4119
4120 if (match < 0) {
4121 return TCL_ERROR;
4122 }
4123 if (match == 0) {
4124 /*
4125 * Set the interpreter's object result to an integer object w/
4126 * value 0.
4127 */
4128
4129 Tcl_SetIntObj(Tcl_GetObjResult(interp), 0);
4130 if (objc > 2 && (cflags&REG_EXPECT) && indices) {
4131 const char *varName;
4132 const char *value;
4133 int start, end;
4134 char resinfo[TCL_INTEGER_SPACE * 2];
4135
4136 varName = Tcl_GetString(objv[2]);
4137 TclRegExpRangeUniChar(regExpr, -1, &start, &end);
4138 snprintf(resinfo, sizeof(resinfo), "%d %d", start, end-1);
4139 value = Tcl_SetVar(interp, varName, resinfo, 0);
4140 if (value == NULL) {
4141 Tcl_AppendResult(interp, "couldn't set variable \"",
4142 varName, "\"", NULL);
4143 return TCL_ERROR;
4144 }
4145 } else if (cflags & TCL_REG_CANMATCH) {
4146 const char *varName;
4147 const char *value;
4148 char resinfo[TCL_INTEGER_SPACE * 2];
4149
4150 Tcl_RegExpGetInfo(regExpr, &info);
4151 varName = Tcl_GetString(objv[2]);
4152 snprintf(resinfo, sizeof(resinfo), "%ld", info.extendStart);
4153 value = Tcl_SetVar(interp, varName, resinfo, 0);
4154 if (value == NULL) {
4155 Tcl_AppendResult(interp, "couldn't set variable \"",
4156 varName, "\"", NULL);
4157 return TCL_ERROR;
4158 }
4159 }
4160 return TCL_OK;
4161 }
4162
4163 /*
4164 * If additional variable names have been specified, return
4165 * index information in those variables.
4166 */
4167
4168 objc -= 2;
4169 objv += 2;
4170
4171 Tcl_RegExpGetInfo(regExpr, &info);
4172 for (i = 0; i < objc; i++) {
4173 int start, end;
4174 Tcl_Obj *newPtr, *varPtr, *valuePtr;
4175
4176 varPtr = objv[i];
4177 ii = ((cflags&REG_EXPECT) && i == objc-1) ? -1 : i;
4178 if (indices) {
4179 Tcl_Obj *objs[2];
4180
4181 if (ii == -1) {
4182 TclRegExpRangeUniChar(regExpr, ii, &start, &end);
4183 } else if (ii > info.nsubs) {
4184 start = -1;
4185 end = -1;
4186 } else {
4187 start = info.matches[ii].start;
4188 end = info.matches[ii].end;
4189 }
4190
4191 /*
4192 * Adjust index so it refers to the last character in the match
4193 * instead of the first character after the match.
4194 */
4195
4196 if (end >= 0) {
4197 end--;
4198 }
4199
4200 objs[0] = Tcl_NewLongObj(start);
4201 objs[1] = Tcl_NewLongObj(end);
4202
4203 newPtr = Tcl_NewListObj(2, objs);
4204 } else {
4205 if (ii == -1) {
4206 TclRegExpRangeUniChar(regExpr, ii, &start, &end);
4207 newPtr = Tcl_GetRange(objPtr, start, end);
4208 } else if (ii > info.nsubs || info.matches[ii].end <= 0) {
4209 newPtr = Tcl_NewObj();
4210 } else {
4211 newPtr = Tcl_GetRange(objPtr, info.matches[ii].start,
4212 info.matches[ii].end - 1);
4213 }
4214 }
4215 valuePtr = Tcl_ObjSetVar2(interp, varPtr, NULL, newPtr, TCL_LEAVE_ERR_MSG);
4216 if (valuePtr == NULL) {
4217 return TCL_ERROR;
4218 }
4219 }
4220
4221 /*
4222 * Set the interpreter's object result to an integer object w/ value 1.
4223 */
4224
4225 Tcl_SetIntObj(Tcl_GetObjResult(interp), 1);
4226 return TCL_OK;
4227 }
4228
4229 /*
4230 *---------------------------------------------------------------------------
4231 *
4232 * TestregexpXflags --
4233 *
4234 * Parse a string of extended regexp flag letters, for testing.
4235 *
4236 * Results:
4237 * No return value (you're on your own for errors here).
4238 *
4239 * Side effects:
4240 * Modifies *cflagsPtr, a regcomp flags word, and *eflagsPtr, a
4241 * regexec flags word, as appropriate.
4242 *
4243 *----------------------------------------------------------------------
4244 */
4245
4246 static void
4247 TestregexpXflags(
4248 const char *string, /* The string of flags. */
4249 int length, /* The length of the string in bytes. */
4250 int *cflagsPtr, /* compile flags word */
4251 int *eflagsPtr) /* exec flags word */
4252 {
4253 int i, cflags, eflags;
4254
4255 cflags = *cflagsPtr;
4256 eflags = *eflagsPtr;
4257 for (i = 0; i < length; i++) {
4258 switch (string[i]) {
4259 case 'a':
4260 cflags |= REG_ADVF;
4261 break;
4262 case 'b':
4263 cflags &= ~REG_ADVANCED;
4264 break;
4265 case 'c':
4266 cflags |= TCL_REG_CANMATCH;
4267 break;
4268 case 'e':
4269 cflags &= ~REG_ADVANCED;
4270 cflags |= REG_EXTENDED;
4271 break;
4272 case 'q':
4273 cflags &= ~REG_ADVANCED;
4274 cflags |= REG_QUOTE;
4275 break;
4276 case 'o': /* o for opaque */
4277 cflags |= REG_NOSUB;
4278 break;
4279 case 's': /* s for start */
4280 cflags |= REG_BOSONLY;
4281 break;
4282 case '+':
4283 cflags |= REG_FAKE;
4284 break;
4285 case ',':
4286 cflags |= REG_PROGRESS;
4287 break;
4288 case '.':
4289 cflags |= REG_DUMP;
4290 break;
4291 case ':':
4292 eflags |= REG_MTRACE;
4293 break;
4294 case ';':
4295 eflags |= REG_FTRACE;
4296 break;
4297 case '^':
4298 eflags |= REG_NOTBOL;
4299 break;
4300 case '$':
4301 eflags |= REG_NOTEOL;
4302 break;
4303 case 't':
4304 cflags |= REG_EXPECT;
4305 break;
4306 case '%':
4307 eflags |= REG_SMALL;
4308 break;
4309 }
4310 }
4311
4312 *cflagsPtr = cflags;
4313 *eflagsPtr = eflags;
4314 }
4315
4316 /*
4317 *----------------------------------------------------------------------
4318 *
4319 * TestreturnObjCmd --
4320 *
4321 * This procedure implements the "testreturn" command. It is
4322 * used to verify that a
4323 * return TCL_RETURN;
4324 * has same behavior as
4325 * return Tcl_SetReturnOptions(interp, Tcl_NewObj());
4326 *
4327 * Results:
4328 * A standard Tcl result.
4329 *
4330 * Side effects:
4331 * See the user documentation.
4332 *
4333 *----------------------------------------------------------------------
4334 */
4335
4336 static int
4337 TestreturnObjCmd(
4338 ClientData dummy, /* Not used. */
4339 Tcl_Interp *interp, /* Current interpreter. */
4340 int objc, /* Number of arguments. */
4341 Tcl_Obj *const objv[]) /* Argument objects. */
4342 {
4343 return TCL_RETURN;
4344 }
4345
4346 /*
4347 *----------------------------------------------------------------------
4348 *
4349 * TestsetassocdataCmd --
4350 *
4351 * This procedure implements the "testsetassocdata" command. It is used
4352 * to test Tcl_SetAssocData.
4353 *
4354 * Results:
4355 * A standard Tcl result.
4356 *
4357 * Side effects:
4358 * Modifies or creates an association between a key and associated
4359 * data for this interpreter.
4360 *
4361 *----------------------------------------------------------------------
4362 */
4363
4364 static int
4365 TestsetassocdataCmd(
4366 ClientData clientData, /* Not used. */
4367 Tcl_Interp *interp, /* Current interpreter. */
4368 int argc, /* Number of arguments. */
4369 const char **argv) /* Argument strings. */
4370 {
4371 char *buf, *oldData;
4372 Tcl_InterpDeleteProc *procPtr;
4373
4374 if (argc != 3) {
4375 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
4376 " data_key data_item\"", NULL);
4377 return TCL_ERROR;
4378 }
4379
4380 buf = (char *)ckalloc(strlen(argv[2]) + 1);
4381 strcpy(buf, argv[2]);
4382
4383 /*
4384 * If we previously associated a malloced value with the variable,
4385 * free it before associating a new value.
4386 */
4387
4388 oldData = (char *) Tcl_GetAssocData(interp, argv[1], &procPtr);
4389 if ((oldData != NULL) && (procPtr == CleanupTestSetassocdataTests)) {
4390 ckfree(oldData);
4391 }
4392
4393 Tcl_SetAssocData(interp, argv[1], CleanupTestSetassocdataTests, buf);
4394 return TCL_OK;
4395 }
4396
4397 /*
4398 *----------------------------------------------------------------------
4399 *
4400 * TestsetplatformCmd --
4401 *
4402 * This procedure implements the "testsetplatform" command. It is
4403 * used to change the tclPlatform global variable so all file
4404 * name conversions can be tested on a single platform.
4405 *
4406 * Results:
4407 * A standard Tcl result.
4408 *
4409 * Side effects:
4410 * Sets the tclPlatform global variable.
4411 *
4412 *----------------------------------------------------------------------
4413 */
4414
4415 static int
4416 TestsetplatformCmd(
4417 ClientData clientData, /* Not used. */
4418 Tcl_Interp *interp, /* Current interpreter. */
4419 int argc, /* Number of arguments. */
4420 const char **argv) /* Argument strings. */
4421 {
4422 size_t length;
4423 TclPlatformType *platform;
4424
4425 platform = TclGetPlatform();
4426
4427 if (argc != 2) {
4428 Tcl_AppendResult(interp, "wrong # arguments: should be \"", argv[0],
4429 " platform\"", NULL);
4430 return TCL_ERROR;
4431 }
4432
4433 length = strlen(argv[1]);
4434 if (strncmp(argv[1], "unix", length) == 0) {
4435 *platform = TCL_PLATFORM_UNIX;
4436 } else if (strncmp(argv[1], "windows", length) == 0) {
4437 *platform = TCL_PLATFORM_WINDOWS;
4438 } else {
4439 Tcl_AppendResult(interp, "unsupported platform: should be one of "
4440 "unix, or windows", NULL);
4441 return TCL_ERROR;
4442 }
4443 return TCL_OK;
4444 }
4445
4446 static int
4447 TestSizeCmd(
4448 ClientData clientData, /* Unused */
4449 Tcl_Interp* interp, /* Tcl interpreter */
4450 int objc, /* Parameter count */
4451 Tcl_Obj *const * objv) /* Parameter vector */
4452 {
4453 if (objc != 2) {
4454 goto syntax;
4455 }
4456 if (strcmp(Tcl_GetString(objv[1]), "st_mtime") == 0) {
4457 Tcl_StatBuf *statPtr;
4458 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(sizeof(statPtr->st_mtime)));
4459 return TCL_OK;
4460 }
4461
4462 syntax:
4463 Tcl_WrongNumArgs(interp, 1, objv, "st_mtime");
4464 return TCL_ERROR;
4465 }
4466
4467 /*
4468 *----------------------------------------------------------------------
4469 *
4470 * TeststaticpkgCmd --
4471 *
4472 * This procedure implements the "teststaticpkg" command.
4473 * It is used to test the procedure Tcl_StaticPackage.
4474 *
4475 * Results:
4476 * A standard Tcl result.
4477 *
4478 * Side effects:
4479 * When the package given by argv[1] is loaded into an interpreter,
4480 * variable "x" in that interpreter is set to "loaded".
4481 *
4482 *----------------------------------------------------------------------
4483 */
4484
4485 static int
4486 TeststaticpkgCmd(
4487 ClientData dummy, /* Not used. */
4488 Tcl_Interp *interp, /* Current interpreter. */
4489 int argc, /* Number of arguments. */
4490 const char **argv) /* Argument strings. */
4491 {
4492 int safe, loaded;
4493
4494 if (argc != 4) {
4495 Tcl_AppendResult(interp, "wrong # arguments: should be \"",
4496 argv[0], " prefix safe loaded\"", NULL);
4497 return TCL_ERROR;
4498 }
4499 if (Tcl_GetInt(interp, argv[2], &safe) != TCL_OK) {
4500 return TCL_ERROR;
4501 }
4502 if (Tcl_GetInt(interp, argv[3], &loaded) != TCL_OK) {
4503 return TCL_ERROR;
4504 }
4505 tclStubsPtr->tcl_StaticPackage((loaded) ? interp : NULL, argv[1],
4506 StaticInitProc, (safe) ? StaticInitProc : NULL);
4507 return TCL_OK;
4508 }
4509
4510 static int
4511 StaticInitProc(
4512 Tcl_Interp *interp) /* Interpreter in which package is supposedly
4513 * being loaded. */
4514 {
4515 Tcl_SetVar(interp, "x", "loaded", TCL_GLOBAL_ONLY);
4516 return TCL_OK;
4517 }
4518
4519 /*
4520 *----------------------------------------------------------------------
4521 *
4522 * TesttranslatefilenameCmd --
4523 *
4524 * This procedure implements the "testtranslatefilename" command.
4525 * It is used to test the Tcl_TranslateFileName command.
4526 *
4527 * Results:
4528 * A standard Tcl result.
4529 *
4530 * Side effects:
4531 * None.
4532 *
4533 *----------------------------------------------------------------------
4534 */
4535
4536 static int
4537 TesttranslatefilenameCmd(
4538 ClientData dummy, /* Not used. */
4539 Tcl_Interp *interp, /* Current interpreter. */
4540 int argc, /* Number of arguments. */
4541 const char **argv) /* Argument strings. */
4542 {
4543 Tcl_DString buffer;
4544 const char *result;
4545
4546 if (argc != 2) {
4547 Tcl_AppendResult(interp, "wrong # arguments: should be \"",
4548 argv[0], " path\"", NULL);
4549 return TCL_ERROR;
4550 }
4551 result = Tcl_TranslateFileName(interp, argv[1], &buffer);
4552 if (result == NULL) {
4553 return TCL_ERROR;
4554 }
4555 Tcl_AppendResult(interp, result, NULL);
4556 Tcl_DStringFree(&buffer);
4557 return TCL_OK;
4558 }
4559
4560 /*
4561 *----------------------------------------------------------------------
4562 *
4563 * TestupvarCmd --
4564 *
4565 * This procedure implements the "testupvar" command. It is used
4566 * to test Tcl_UpVar and Tcl_UpVar2.
4567 *
4568 * Results:
4569 * A standard Tcl result.
4570 *
4571 * Side effects:
4572 * Creates or modifies an "upvar" reference.
4573 *
4574 *----------------------------------------------------------------------
4575 */
4576
4577 static int
4578 TestupvarCmd(
4579 ClientData dummy, /* Not used. */
4580 Tcl_Interp *interp, /* Current interpreter. */
4581 int argc, /* Number of arguments. */
4582 const char **argv) /* Argument strings. */
4583 {
4584 int flags = 0;
4585
4586 if ((argc != 5) && (argc != 6)) {
4587 Tcl_AppendResult(interp, "wrong # arguments: should be \"",
4588 argv[0], " level name ?name2? dest global\"", NULL);
4589 return TCL_ERROR;
4590 }
4591
4592 if (argc == 5) {
4593 if (strcmp(argv[4], "global") == 0) {
4594 flags = TCL_GLOBAL_ONLY;
4595 } else if (strcmp(argv[4], "namespace") == 0) {
4596 flags = TCL_NAMESPACE_ONLY;
4597 }
4598 return Tcl_UpVar(interp, argv[1], argv[2], argv[3], flags);
4599 } else {
4600 if (strcmp(argv[5], "global") == 0) {
4601 flags = TCL_GLOBAL_ONLY;
4602 } else if (strcmp(argv[5], "namespace") == 0) {
4603 flags = TCL_NAMESPACE_ONLY;
4604 }
4605 return Tcl_UpVar2(interp, argv[1], argv[2],
4606 (argv[3][0] == 0) ? NULL : argv[3], argv[4],
4607 flags);
4608 }
4609 }
4610
4611 /*
4612 *----------------------------------------------------------------------
4613 *
4614 * TestseterrorcodeCmd --
4615 *
4616 * This procedure implements the "testseterrorcodeCmd". This tests up to
4617 * five elements passed to the Tcl_SetErrorCode command.
4618 *
4619 * Results:
4620 * A standard Tcl result. Always returns TCL_ERROR so that
4621 * the error code can be tested.
4622 *
4623 * Side effects:
4624 * None.
4625 *
4626 *----------------------------------------------------------------------
4627 */
4628
4629 static int
4630 TestseterrorcodeCmd(
4631 ClientData dummy, /* Not used. */
4632 Tcl_Interp *interp, /* Current interpreter. */
4633 int argc, /* Number of arguments. */
4634 const char **argv) /* Argument strings. */
4635 {
4636 if (argc > 6) {
4637 Tcl_AppendResult(interp, "too many args", NULL);
4638 return TCL_ERROR;
4639 }
4640 switch (argc) {
4641 case 1:
4642 Tcl_SetErrorCode(interp, "NONE", (char *)NULL);
4643 break;
4644 case 2:
4645 Tcl_SetErrorCode(interp, argv[1], (char *)NULL);
4646 break;
4647 case 3:
4648 Tcl_SetErrorCode(interp, argv[1], argv[2], (char *)NULL);
4649 break;
4650 case 4:
4651 Tcl_SetErrorCode(interp, argv[1], argv[2], argv[3], (char *)NULL);
4652 break;
4653 case 5:
4654 Tcl_SetErrorCode(interp, argv[1], argv[2], argv[3], argv[4], (char *)NULL);
4655 break;
4656 case 6:
4657 Tcl_SetErrorCode(interp, argv[1], argv[2], argv[3], argv[4],
4658 argv[5], (char *)NULL);
4659 }
4660 return TCL_ERROR;
4661 }
4662
4663 /*
4664 *----------------------------------------------------------------------
4665 *
4666 * TestsetobjerrorcodeCmd --
4667 *
4668 * This procedure implements the "testsetobjerrorcodeCmd".
4669 * This tests the Tcl_SetObjErrorCode function.
4670 *
4671 * Results:
4672 * A standard Tcl result. Always returns TCL_ERROR so that
4673 * the error code can be tested.
4674 *
4675 * Side effects:
4676 * None.
4677 *
4678 *----------------------------------------------------------------------
4679 */
4680
4681 static int
4682 TestsetobjerrorcodeCmd(
4683 ClientData dummy, /* Not used. */
4684 Tcl_Interp *interp, /* Current interpreter. */
4685 int objc, /* Number of arguments. */
4686 Tcl_Obj *const objv[]) /* The argument objects. */
4687 {
4688 Tcl_SetObjErrorCode(interp, Tcl_ConcatObj(objc - 1, objv + 1));
4689 return TCL_ERROR;
4690 }
4691
4692 /*
4693 *----------------------------------------------------------------------
4694 *
4695 * TestfeventCmd --
4696 *
4697 * This procedure implements the "testfevent" command. It is
4698 * used for testing the "fileevent" command.
4699 *
4700 * Results:
4701 * A standard Tcl result.
4702 *
4703 * Side effects:
4704 * Creates and deletes interpreters.
4705 *
4706 *----------------------------------------------------------------------
4707 */
4708
4709 static int
4710 TestfeventCmd(
4711 ClientData clientData, /* Not used. */
4712 Tcl_Interp *interp, /* Current interpreter. */
4713 int argc, /* Number of arguments. */
4714 const char **argv) /* Argument strings. */
4715 {
4716 static Tcl_Interp *interp2 = NULL;
4717 int code;
4718 Tcl_Channel chan;
4719
4720 if (argc < 2) {
4721 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4722 " option ?arg ...?", NULL);
4723 return TCL_ERROR;
4724 }
4725 if (strcmp(argv[1], "cmd") == 0) {
4726 if (argc != 3) {
4727 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
4728 " cmd script", NULL);
4729 return TCL_ERROR;
4730 }
4731 if (interp2 != NULL) {
4732 code = Tcl_EvalEx(interp2, argv[2], -1, TCL_EVAL_GLOBAL);
4733 Tcl_SetObjResult(interp, Tcl_GetObjResult(interp2));
4734 return code;
4735 } else {
4736 Tcl_AppendResult(interp,
4737 "called \"testfevent code\" before \"testfevent create\"",
4738 (char *)NULL);
4739 return TCL_ERROR;
4740 }
4741 } else if (strcmp(argv[1], "create") == 0) {
4742 if (interp2 != NULL) {
4743 Tcl_DeleteInterp(interp2);
4744 }
4745 interp2 = Tcl_CreateInterp();
4746 return Tcl_Init(interp2);
4747 } else if (strcmp(argv[1], "delete") == 0) {
4748 if (interp2 != NULL) {
4749 Tcl_DeleteInterp(interp2);
4750 }
4751 interp2 = NULL;
4752 } else if (strcmp(argv[1], "share") == 0) {
4753 if (interp2 != NULL) {
4754 chan = Tcl_GetChannel(interp, argv[2], NULL);
4755 if (chan == (Tcl_Channel) NULL) {
4756 return TCL_ERROR;
4757 }
4758 Tcl_RegisterChannel(interp2, chan);
4759 }
4760 }
4761
4762 return TCL_OK;
4763 }
4764
4765 /*
4766 *----------------------------------------------------------------------
4767 *
4768 * TestpanicCmd --
4769 *
4770 * Calls the panic routine.
4771 *
4772 * Results:
4773 * Always returns TCL_OK.
4774 *
4775 * Side effects:
4776 * May exit application.
4777 *
4778 *----------------------------------------------------------------------
4779 */
4780
4781 static int
4782 TestpanicCmd(
4783 ClientData dummy, /* Not used. */
4784 Tcl_Interp *interp, /* Current interpreter. */
4785 int argc, /* Number of arguments. */
4786 const char **argv) /* Argument strings. */
4787 {
4788 char *argString;
4789
4790 /*
4791 * Put the arguments into a var args structure
4792 * Append all of the arguments together separated by spaces
4793 */
4794
4795 argString = Tcl_Merge(argc-1, argv+1);
4796 Tcl_Panic("%s", argString);
4797 ckfree(argString);
4798
4799 return TCL_OK;
4800 }
4801
4802 static int
4803 TestfileCmd(
4804 ClientData dummy, /* Not used. */
4805 Tcl_Interp *interp, /* Current interpreter. */
4806 int argc, /* Number of arguments. */
4807 Tcl_Obj *const argv[]) /* The argument objects. */
4808 {
4809 int force, i, j, result;
4810 Tcl_Obj *error = NULL;
4811 const char *subcmd;
4812
4813 if (argc < 3) {
4814 return TCL_ERROR;
4815 }
4816
4817 force = 0;
4818 i = 2;
4819 if (strcmp(Tcl_GetString(argv[2]), "-force") == 0) {
4820 force = 1;
4821 i = 3;
4822 }
4823
4824 if (argc - i > 2) {
4825 return TCL_ERROR;
4826 }
4827
4828 for (j = i; j < argc; j++) {
4829 if (Tcl_FSGetNormalizedPath(interp, argv[j]) == NULL) {
4830 return TCL_ERROR;
4831 }
4832 }
4833
4834 subcmd = Tcl_GetString(argv[1]);
4835
4836 if (strcmp(subcmd, "mv") == 0) {
4837 result = TclpObjRenameFile(argv[i], argv[i + 1]);
4838 } else if (strcmp(subcmd, "cp") == 0) {
4839 result = TclpObjCopyFile(argv[i], argv[i + 1]);
4840 } else if (strcmp(subcmd, "rm") == 0) {
4841 result = TclpObjDeleteFile(argv[i]);
4842 } else if (strcmp(subcmd, "mkdir") == 0) {
4843 result = TclpObjCreateDirectory(argv[i]);
4844 } else if (strcmp(subcmd, "cpdir") == 0) {
4845 result = TclpObjCopyDirectory(argv[i], argv[i + 1], &error);
4846 } else if (strcmp(subcmd, "rmdir") == 0) {
4847 result = TclpObjRemoveDirectory(argv[i], force, &error);
4848 } else {
4849 result = TCL_ERROR;
4850 goto end;
4851 }
4852
4853 if (result != TCL_OK) {
4854 if (error != NULL) {
4855 if (Tcl_GetString(error)[0] != '\0') {
4856 Tcl_AppendResult(interp, Tcl_GetString(error), " ", NULL);
4857 }
4858 Tcl_DecrRefCount(error);
4859 }
4860 Tcl_AppendResult(interp, Tcl_ErrnoId(), NULL);
4861 }
4862
4863 end:
4864 return result;
4865 }
4866
4867 /*
4868 *----------------------------------------------------------------------
4869 *
4870 * TestgetvarfullnameCmd --
4871 *
4872 * Implements the "testgetvarfullname" cmd that is used when testing
4873 * the Tcl_GetVariableFullName procedure.
4874 *
4875 * Results:
4876 * A standard Tcl result.
4877 *
4878 * Side effects:
4879 * None.
4880 *
4881 *----------------------------------------------------------------------
4882 */
4883
4884 static int
4885 TestgetvarfullnameCmd(
4886 ClientData dummy, /* Not used. */
4887 Tcl_Interp *interp, /* Current interpreter. */
4888 int objc, /* Number of arguments. */
4889 Tcl_Obj *const objv[]) /* The argument objects. */
4890 {
4891 const char *name, *arg;
4892 int flags = 0;
4893 Tcl_Namespace *namespacePtr;
4894 Tcl_CallFrame *framePtr;
4895 Tcl_Var variable;
4896
4897 if (objc != 3) {
4898 Tcl_WrongNumArgs(interp, 1, objv, "name scope");
4899 return TCL_ERROR;
4900 }
4901
4902 name = Tcl_GetString(objv[1]);
4903
4904 arg = Tcl_GetString(objv[2]);
4905 if (strcmp(arg, "global") == 0) {
4906 flags = TCL_GLOBAL_ONLY;
4907 } else if (strcmp(arg, "namespace") == 0) {
4908 flags = TCL_NAMESPACE_ONLY;
4909 }
4910
4911 /*
4912 * This command, like any other created with Tcl_Create[Obj]Command, runs
4913 * in the global namespace. As a "namespace-aware" command that needs to
4914 * run in a particular namespace, it must activate that namespace itself.
4915 */
4916
4917 if (flags == TCL_NAMESPACE_ONLY) {
4918 namespacePtr = Tcl_FindNamespace(interp, "::test_ns_var", NULL,
4919 TCL_LEAVE_ERR_MSG);
4920 if (namespacePtr == NULL) {
4921 return TCL_ERROR;
4922 }
4923 (void) TclPushStackFrame(interp, &framePtr, namespacePtr,
4924 /*isProcCallFrame*/ 0);
4925 }
4926
4927 variable = Tcl_FindNamespaceVar(interp, name, NULL,
4928 (flags | TCL_LEAVE_ERR_MSG));
4929
4930 if (flags == TCL_NAMESPACE_ONLY) {
4931 TclPopStackFrame(interp);
4932 }
4933 if (variable == (Tcl_Var) NULL) {
4934 return TCL_ERROR;
4935 }
4936 Tcl_GetVariableFullName(interp, variable, Tcl_GetObjResult(interp));
4937 return TCL_OK;
4938 }
4939
4940 /*
4941 *----------------------------------------------------------------------
4942 *
4943 * GetTimesObjCmd --
4944 *
4945 * This procedure implements the "gettimes" command. It is used for
4946 * computing the time needed for various basic operations such as reading
4947 * variables, allocating memory, sprintf, converting variables, etc.
4948 *
4949 * Results:
4950 * A standard Tcl result.
4951 *
4952 * Side effects:
4953 * Allocates and frees memory, sets a variable "a" in the interpreter.
4954 *
4955 *----------------------------------------------------------------------
4956 */
4957
4958 static int
4959 GetTimesObjCmd(
4960 ClientData unused, /* Unused. */
4961 Tcl_Interp *interp, /* The current interpreter. */
4962 int objc, /* Number of arguments. (not used)*/
4963 Tcl_Obj *const dummy[]) /* The argument objects (not used). */
4964 {
4965 Interp *iPtr = (Interp *) interp;
4966 int i, n;
4967 double timePer;
4968 Tcl_Time start, stop;
4969 Tcl_Obj *objPtr, **objv;
4970 const char *s;
4971 char newString[TCL_INTEGER_SPACE];
4972 (void)objc;
4973 (void)dummy;
4974
4975 /* alloc & free 100000 times */
4976 fprintf(stderr, "alloc & free 100000 6 word items\n");
4977 Tcl_GetTime(&start);
4978 for (i = 0; i < 100000; i++) {
4979 objPtr = (Tcl_Obj *)ckalloc(sizeof(Tcl_Obj));
4980 ckfree(objPtr);
4981 }
4982 Tcl_GetTime(&stop);
4983 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
4984 fprintf(stderr, " %.3f usec per alloc+free\n", timePer/100000);
4985
4986 /* alloc 5000 times */
4987 fprintf(stderr, "alloc 5000 6 word items\n");
4988 objv = (Tcl_Obj **)ckalloc(5000 * sizeof(Tcl_Obj *));
4989 Tcl_GetTime(&start);
4990 for (i = 0; i < 5000; i++) {
4991 objv[i] = (Tcl_Obj *)ckalloc(sizeof(Tcl_Obj));
4992 }
4993 Tcl_GetTime(&stop);
4994 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
4995 fprintf(stderr, " %.3f usec per alloc\n", timePer/5000);
4996
4997 /* free 5000 times */
4998 fprintf(stderr, "free 5000 6 word items\n");
4999 Tcl_GetTime(&start);
5000 for (i = 0; i < 5000; i++) {
5001 ckfree(objv[i]);
5002 }
5003 Tcl_GetTime(&stop);
5004 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
5005 fprintf(stderr, " %.3f usec per free\n", timePer/5000);
5006
5007 /* Tcl_NewObj 5000 times */
5008 fprintf(stderr, "Tcl_NewObj 5000 times\n");
5009 Tcl_GetTime(&start);
5010 for (i = 0; i < 5000; i++) {
5011 objv[i] = Tcl_NewObj();
5012 }
5013 Tcl_GetTime(&stop);
5014 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
5015 fprintf(stderr, " %.3f usec per Tcl_NewObj\n", timePer/5000);
5016
5017 /* Tcl_DecrRefCount 5000 times */
5018 fprintf(stderr, "Tcl_DecrRefCount 5000 times\n");
5019 Tcl_GetTime(&start);
5020 for (i = 0; i < 5000; i++) {
5021 objPtr = objv[i];
5022 Tcl_DecrRefCount(objPtr);
5023 }
5024 Tcl_GetTime(&stop);
5025 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
5026 fprintf(stderr, " %.3f usec per Tcl_DecrRefCount\n", timePer/5000);
5027 ckfree(objv);
5028
5029 /* TclGetString 100000 times */
5030 fprintf(stderr, "TclGetStringFromObj of \"12345\" 100000 times\n");
5031 objPtr = Tcl_NewStringObj("12345", -1);
5032 Tcl_GetTime(&start);
5033 for (i = 0; i < 100000; i++) {
5034 (void) TclGetString(objPtr);
5035 }
5036 Tcl_GetTime(&stop);
5037 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
5038 fprintf(stderr, " %.3f usec per TclGetStringFromObj of \"12345\"\n",
5039 timePer/100000);
5040
5041 /* Tcl_GetIntFromObj 100000 times */
5042 fprintf(stderr, "Tcl_GetIntFromObj of \"12345\" 100000 times\n");
5043 Tcl_GetTime(&start);
5044 for (i = 0; i < 100000; i++) {
5045 if (Tcl_GetIntFromObj(interp, objPtr, &n) != TCL_OK) {
5046 return TCL_ERROR;
5047 }
5048 }
5049 Tcl_GetTime(&stop);
5050 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
5051 fprintf(stderr, " %.3f usec per Tcl_GetIntFromObj of \"12345\"\n",
5052 timePer/100000);
5053 Tcl_DecrRefCount(objPtr);
5054
5055 /* Tcl_GetInt 100000 times */
5056 fprintf(stderr, "Tcl_GetInt of \"12345\" 100000 times\n");
5057 Tcl_GetTime(&start);
5058 for (i = 0; i < 100000; i++) {
5059 if (Tcl_GetInt(interp, "12345", &n) != TCL_OK) {
5060 return TCL_ERROR;
5061 }
5062 }
5063 Tcl_GetTime(&stop);
5064 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
5065 fprintf(stderr, " %.3f usec per Tcl_GetInt of \"12345\"\n",
5066 timePer/100000);
5067
5068 /* snprintf 100000 times */
5069 fprintf(stderr, "snprintf of 12345 100000 times\n");
5070 Tcl_GetTime(&start);
5071 for (i = 0; i < 100000; i++) {
5072 snprintf(newString, sizeof(newString), "%d", 12345);
5073 }
5074 Tcl_GetTime(&stop);
5075 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
5076 fprintf(stderr, " %.3f usec per snprintf of 12345\n",
5077 timePer/100000);
5078
5079 /* hashtable lookup 100000 times */
5080 fprintf(stderr, "hashtable lookup of \"gettimes\" 100000 times\n");
5081 Tcl_GetTime(&start);
5082 for (i = 0; i < 100000; i++) {
5083 (void) Tcl_FindHashEntry(&iPtr->globalNsPtr->cmdTable, "gettimes");
5084 }
5085 Tcl_GetTime(&stop);
5086 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
5087 fprintf(stderr, " %.3f usec per hashtable lookup of \"gettimes\"\n",
5088 timePer/100000);
5089
5090 /* Tcl_SetVar 100000 times */
5091 fprintf(stderr, "Tcl_SetVar of \"12345\" 100000 times\n");
5092 Tcl_GetTime(&start);
5093 for (i = 0; i < 100000; i++) {
5094 s = Tcl_SetVar(interp, "a", "12345", TCL_LEAVE_ERR_MSG);
5095 if (s == NULL) {
5096 return TCL_ERROR;
5097 }
5098 }
5099 Tcl_GetTime(&stop);
5100 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
5101 fprintf(stderr, " %.3f usec per Tcl_SetVar of a to \"12345\"\n",
5102 timePer/100000);
5103
5104 /* Tcl_GetVar 100000 times */
5105 fprintf(stderr, "Tcl_GetVar of a==\"12345\" 100000 times\n");
5106 Tcl_GetTime(&start);
5107 for (i = 0; i < 100000; i++) {
5108 s = Tcl_GetVar(interp, "a", TCL_LEAVE_ERR_MSG);
5109 if (s == NULL) {
5110 return TCL_ERROR;
5111 }
5112 }
5113 Tcl_GetTime(&stop);
5114 timePer = (stop.sec - start.sec)*1000000 + (stop.usec - start.usec);
5115 fprintf(stderr, " %.3f usec per Tcl_GetVar of a==\"12345\"\n",
5116 timePer/100000);
5117
5118 Tcl_ResetResult(interp);
5119 return TCL_OK;
5120 }
5121
5122 /*
5123 *----------------------------------------------------------------------
5124 *
5125 * NoopCmd --
5126 *
5127 * This procedure is just used to time the overhead involved in
5128 * parsing and invoking a command.
5129 *
5130 * Results:
5131 * None.
5132 *
5133 * Side effects:
5134 * None.
5135 *
5136 *----------------------------------------------------------------------
5137 */
5138
5139 static int
5140 NoopCmd(
5141 ClientData unused, /* Unused. */
5142 Tcl_Interp *interp, /* The current interpreter. */
5143 int argc, /* The number of arguments. */
5144 const char **argv) /* The argument strings. */
5145 {
5146 return TCL_OK;
5147 }
5148
5149 /*
5150 *----------------------------------------------------------------------
5151 *
5152 * NoopObjCmd --
5153 *
5154 * This object-based procedure is just used to time the overhead
5155 * involved in parsing and invoking a command.
5156 *
5157 * Results:
5158 * Returns the TCL_OK result code.
5159 *
5160 * Side effects:
5161 * None.
5162 *
5163 *----------------------------------------------------------------------
5164 */
5165
5166 static int
5167 NoopObjCmd(
5168 ClientData unused, /* Not used. */
5169 Tcl_Interp *interp, /* Current interpreter. */
5170 int objc, /* Number of arguments. */
5171 Tcl_Obj *const objv[]) /* The argument objects. */
5172 {
5173 return TCL_OK;
5174 }
5175
5176 /*
5177 *----------------------------------------------------------------------
5178 *
5179 * TeststringbytesObjCmd --
5180 * Returns bytearray value of the bytes in argument string rep
5181 *
5182 * Results:
5183 * Returns the TCL_OK result code.
5184 *
5185 * Side effects:
5186 * None.
5187 *
5188 *----------------------------------------------------------------------
5189 */
5190
5191 static int
5192 TeststringbytesObjCmd(
5193 ClientData dummy,
5194 Tcl_Interp *interp, /* Current interpreter. */
5195 int objc, /* Number of arguments. */
5196 Tcl_Obj *const objv[]) /* The argument objects. */
5197 {
5198 int n;
5199 const unsigned char *p;
5200 (void)dummy;
5201
5202 if (objc != 2) {
5203 Tcl_WrongNumArgs(interp, 1, objv, "value");
5204 return TCL_ERROR;
5205 }
5206 p = (const unsigned char *)Tcl_GetStringFromObj(objv[1], &n);
5207 Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(p, n));
5208 return TCL_OK;
5209 }
5210
5211 /*
5212 *----------------------------------------------------------------------
5213 *
5214 * TestpurebytesobjObjCmd --
5215 *
5216 * This object-based procedure constructs a pure bytes object
5217 * without type and with internal representation containing NULL's.
5218 *
5219 * If no argument supplied it returns empty object with tclEmptyStringRep,
5220 * otherwise it returns this as pure bytes object with bytes value equal
5221 * string.
5222 *
5223 * Results:
5224 * Returns the TCL_OK result code.
5225 *
5226 * Side effects:
5227 * None.
5228 *
5229 *----------------------------------------------------------------------
5230 */
5231
5232 static int
5233 TestpurebytesobjObjCmd(
5234 ClientData dummy, /* Not used. */
5235 Tcl_Interp *interp, /* Current interpreter. */
5236 int objc, /* Number of arguments. */
5237 Tcl_Obj *const objv[]) /* The argument objects. */
5238 {
5239 Tcl_Obj *objPtr;
5240 (void)dummy;
5241
5242 if (objc > 2) {
5243 Tcl_WrongNumArgs(interp, 1, objv, "?string?");
5244 return TCL_ERROR;
5245 }
5246 objPtr = Tcl_NewObj();
5247 /*
5248 objPtr->internalRep.twoPtrValue.ptr1 = NULL;
5249 objPtr->internalRep.twoPtrValue.ptr2 = NULL;
5250 */
5251 memset(&objPtr->internalRep, 0, sizeof(objPtr->internalRep));
5252 if (objc == 2) {
5253 const char *s = Tcl_GetString(objv[1]);
5254 objPtr->length = objv[1]->length;
5255 objPtr->bytes = (char *)ckalloc(objPtr->length + 1);
5256 memcpy(objPtr->bytes, s, objPtr->length);
5257 objPtr->bytes[objPtr->length] = 0;
5258 }
5259 Tcl_SetObjResult(interp, objPtr);
5260 return TCL_OK;
5261 }
5262
5263 /*
5264 *----------------------------------------------------------------------
5265 *
5266 * TestsetbytearraylengthObjCmd --
5267 *
5268 * Testing command 'testsetbytearraylength` used to test the public
5269 * interface routine Tcl_SetByteArrayLength().
5270 *
5271 * Results:
5272 * Returns the TCL_OK result code.
5273 *
5274 * Side effects:
5275 * None.
5276 *
5277 *----------------------------------------------------------------------
5278 */
5279
5280 static int
5281 TestsetbytearraylengthObjCmd(
5282 ClientData dummy, /* Not used. */
5283 Tcl_Interp *interp, /* Current interpreter. */
5284 int objc, /* Number of arguments. */
5285 Tcl_Obj *const objv[]) /* The argument objects. */
5286 {
5287 int n;
5288 Tcl_Obj *obj = NULL;
5289 (void)dummy;
5290
5291 if (objc != 3) {
5292 Tcl_WrongNumArgs(interp, 1, objv, "value length");
5293 return TCL_ERROR;
5294 }
5295 if (TCL_OK != Tcl_GetIntFromObj(interp, objv[2], &n)) {
5296 return TCL_ERROR;
5297 }
5298 if (Tcl_IsShared(objv[1])) {
5299 obj = Tcl_DuplicateObj(objv[1]);
5300 } else {
5301 obj = objv[1];
5302 }
5303 Tcl_SetByteArrayLength(obj, n);
5304 Tcl_SetObjResult(interp, obj);
5305 return TCL_OK;
5306 }
5307
5308 /*
5309 *----------------------------------------------------------------------
5310 *
5311 * TestbytestringObjCmd --
5312 *
5313 * This object-based procedure constructs a string which can
5314 * possibly contain invalid UTF-8 bytes.
5315 *
5316 * Results:
5317 * Returns the TCL_OK result code.
5318 *
5319 * Side effects:
5320 * None.
5321 *
5322 *----------------------------------------------------------------------
5323 */
5324
5325 static int
5326 TestbytestringObjCmd(
5327 ClientData dummy, /* Not used. */
5328 Tcl_Interp *interp, /* Current interpreter. */
5329 int objc, /* Number of arguments. */
5330 Tcl_Obj *const objv[]) /* The argument objects. */
5331 {
5332 int n = 0;
5333 const char *p;
5334 (void)dummy;
5335
5336 if (objc != 2) {
5337 Tcl_WrongNumArgs(interp, 1, objv, "bytearray");
5338 return TCL_ERROR;
5339 }
5340
5341 p = (const char *)Tcl_GetByteArrayFromObj(objv[1], &n);
5342 Tcl_SetObjResult(interp, Tcl_NewStringObj(p, n));
5343 return TCL_OK;
5344 }
5345
5346 /*
5347 *----------------------------------------------------------------------
5348 *
5349 * TestsetCmd --
5350 *
5351 * Implements the "testset{err,noerr}" cmds that are used when testing
5352 * Tcl_Set/GetVar C Api with/without TCL_LEAVE_ERR_MSG flag
5353 *
5354 * Results:
5355 * A standard Tcl result.
5356 *
5357 * Side effects:
5358 * Variables may be set.
5359 *
5360 *----------------------------------------------------------------------
5361 */
5362
5363 static int
5364 TestsetCmd(
5365 ClientData data, /* Additional flags for Get/SetVar2. */
5366 Tcl_Interp *interp,/* Current interpreter. */
5367 int argc, /* Number of arguments. */
5368 const char **argv) /* Argument strings. */
5369 {
5370 int flags = PTR2INT(data);
5371 const char *value;
5372
5373 if (argc == 2) {
5374 Tcl_AppendResult(interp, "before get", NULL);
5375 value = Tcl_GetVar2(interp, argv[1], NULL, flags);
5376 if (value == NULL) {
5377 return TCL_ERROR;
5378 }
5379 Tcl_AppendElement(interp, value);
5380 return TCL_OK;
5381 } else if (argc == 3) {
5382 Tcl_AppendResult(interp, "before set", NULL);
5383 value = Tcl_SetVar2(interp, argv[1], NULL, argv[2], flags);
5384 if (value == NULL) {
5385 return TCL_ERROR;
5386 }
5387 Tcl_AppendElement(interp, value);
5388 return TCL_OK;
5389 } else {
5390 Tcl_AppendResult(interp, "wrong # args: should be \"",
5391 argv[0], " varName ?newValue?\"", NULL);
5392 return TCL_ERROR;
5393 }
5394 }
5395 static int
5396 Testset2Cmd(
5397 ClientData data, /* Additional flags for Get/SetVar2. */
5398 Tcl_Interp *interp,/* Current interpreter. */
5399 int argc, /* Number of arguments. */
5400 const char **argv) /* Argument strings. */
5401 {
5402 int flags = PTR2INT(data);
5403 const char *value;
5404
5405 if (argc == 3) {
5406 Tcl_AppendResult(interp, "before get", NULL);
5407 value = Tcl_GetVar2(interp, argv[1], argv[2], flags);
5408 if (value == NULL) {
5409 return TCL_ERROR;
5410 }
5411 Tcl_AppendElement(interp, value);
5412 return TCL_OK;
5413 } else if (argc == 4) {
5414 Tcl_AppendResult(interp, "before set", NULL);
5415 value = Tcl_SetVar2(interp, argv[1], argv[2], argv[3], flags);
5416 if (value == NULL) {
5417 return TCL_ERROR;
5418 }
5419 Tcl_AppendElement(interp, value);
5420 return TCL_OK;
5421 } else {
5422 Tcl_AppendResult(interp, "wrong # args: should be \"",
5423 argv[0], " varName elemName ?newValue?\"", NULL);
5424 return TCL_ERROR;
5425 }
5426 }
5427
5428 /*
5429 *----------------------------------------------------------------------
5430 *
5431 * TestsaveresultCmd --
5432 *
5433 * Implements the "testsaveresult" cmd that is used when testing the
5434 * Tcl_SaveResult, Tcl_RestoreResult, and Tcl_DiscardResult interfaces.
5435 *
5436 * Results:
5437 * A standard Tcl result.
5438 *
5439 * Side effects:
5440 * None.
5441 *
5442 *----------------------------------------------------------------------
5443 */
5444
5445 static int
5446 TestsaveresultCmd(
5447 ClientData dummy, /* Not used. */
5448 Tcl_Interp *interp,/* Current interpreter. */
5449 int objc, /* Number of arguments. */
5450 Tcl_Obj *const objv[]) /* The argument objects. */
5451 {
5452 Interp* iPtr = (Interp*) interp;
5453 int discard, result, index;
5454 Tcl_SavedResult state;
5455 Tcl_Obj *objPtr;
5456 static const char *const optionStrings[] = {
5457 "append", "dynamic", "free", "object", "small", NULL
5458 };
5459 enum options {
5460 RESULT_APPEND, RESULT_DYNAMIC, RESULT_FREE, RESULT_OBJECT, RESULT_SMALL
5461 };
5462
5463 /*
5464 * Parse arguments
5465 */
5466
5467 if (objc != 4) {
5468 Tcl_WrongNumArgs(interp, 1, objv, "type script discard");
5469 return TCL_ERROR;
5470 }
5471 if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
5472 &index) != TCL_OK) {
5473 return TCL_ERROR;
5474 }
5475 if (Tcl_GetBooleanFromObj(interp, objv[3], &discard) != TCL_OK) {
5476 return TCL_ERROR;
5477 }
5478
5479 objPtr = NULL; /* Lint. */
5480 switch ((enum options) index) {
5481 case RESULT_SMALL:
5482 Tcl_SetResult(interp, "small result", TCL_VOLATILE);
5483 break;
5484 case RESULT_APPEND:
5485 Tcl_AppendResult(interp, "append result", NULL);
5486 break;
5487 case RESULT_FREE: {
5488 char *buf = ckalloc(200);
5489
5490 strcpy(buf, "free result");
5491 Tcl_SetResult(interp, buf, TCL_DYNAMIC);
5492 break;
5493 }
5494 case RESULT_DYNAMIC:
5495 Tcl_SetResult(interp, (char *)"dynamic result", TestsaveresultFree);
5496 break;
5497 case RESULT_OBJECT:
5498 objPtr = Tcl_NewStringObj("object result", -1);
5499 Tcl_SetObjResult(interp, objPtr);
5500 break;
5501 }
5502
5503 freeCount = 0;
5504 Tcl_SaveResult(interp, &state);
5505
5506 if (((enum options) index) == RESULT_OBJECT) {
5507 result = Tcl_EvalObjEx(interp, objv[2], 0);
5508 } else {
5509 result = Tcl_Eval(interp, Tcl_GetString(objv[2]));
5510 }
5511
5512 if (discard) {
5513 Tcl_DiscardResult(&state);
5514 } else {
5515 Tcl_RestoreResult(interp, &state);
5516 result = TCL_OK;
5517 }
5518
5519 switch ((enum options) index) {
5520 case RESULT_DYNAMIC: {
5521 int present = iPtr->freeProc == TestsaveresultFree;
5522 int called = freeCount;
5523
5524 Tcl_AppendElement(interp, called ? "called" : "notCalled");
5525 Tcl_AppendElement(interp, present ? "present" : "missing");
5526 break;
5527 }
5528 case RESULT_OBJECT:
5529 Tcl_AppendElement(interp, Tcl_GetObjResult(interp) == objPtr
5530 ? "same" : "different");
5531 break;
5532 default:
5533 break;
5534 }
5535 return result;
5536 }
5537
5538 /*
5539 *----------------------------------------------------------------------
5540 *
5541 * TestsaveresultFree --
5542 *
5543 * Special purpose freeProc used by TestsaveresultCmd.
5544 *
5545 * Results:
5546 * None.
5547 *
5548 * Side effects:
5549 * Increments the freeCount.
5550 *
5551 *----------------------------------------------------------------------
5552 */
5553
5554 static void
5555 TestsaveresultFree(
5556 #if TCL_MAJOR_VERSION > 8
5557 void *blockPtr)
5558 #else
5559 char *blockPtr)
5560 #endif
5561 {
5562 freeCount++;
5563 }
5564
5565 /*
5566 *----------------------------------------------------------------------
5567 *
5568 * TestmainthreadCmd --
5569 *
5570 * Implements the "testmainthread" cmd that is used to test the
5571 * 'Tcl_GetCurrentThread' API.
5572 *
5573 * Results:
5574 * A standard Tcl result.
5575 *
5576 * Side effects:
5577 * None.
5578 *
5579 *----------------------------------------------------------------------
5580 */
5581
5582 static int
5583 TestmainthreadCmd(
5584 ClientData dummy, /* Not used. */
5585 Tcl_Interp *interp,/* Current interpreter. */
5586 int argc, /* Number of arguments. */
5587 const char **argv) /* Argument strings. */
5588 {
5589 if (argc == 1) {
5590 Tcl_Obj *idObj = Tcl_NewWideIntObj((Tcl_WideInt)(size_t)Tcl_GetCurrentThread());
5591
5592 Tcl_SetObjResult(interp, idObj);
5593 return TCL_OK;
5594 } else {
5595 Tcl_AppendResult(interp, "wrong # args", NULL);
5596 return TCL_ERROR;
5597 }
5598 }
5599
5600 /*
5601 *----------------------------------------------------------------------
5602 *
5603 * MainLoop --
5604 *
5605 * A main loop set by TestsetmainloopCmd below.
5606 *
5607 * Results:
5608 * None.
5609 *
5610 * Side effects:
5611 * Event handlers could do anything.
5612 *
5613 *----------------------------------------------------------------------
5614 */
5615
5616 static void
5617 MainLoop(void)
5618 {
5619 while (!exitMainLoop) {
5620 Tcl_DoOneEvent(0);
5621 }
5622 fprintf(stdout,"Exit MainLoop\n");
5623 fflush(stdout);
5624 }
5625
5626 /*
5627 *----------------------------------------------------------------------
5628 *
5629 * TestsetmainloopCmd --
5630 *
5631 * Implements the "testsetmainloop" cmd that is used to test the
5632 * 'Tcl_SetMainLoop' API.
5633 *
5634 * Results:
5635 * A standard Tcl result.
5636 *
5637 * Side effects:
5638 * None.
5639 *
5640 *----------------------------------------------------------------------
5641 */
5642
5643 static int
5644 TestsetmainloopCmd(
5645 ClientData dummy, /* Not used. */
5646 Tcl_Interp *interp,/* Current interpreter. */
5647 int argc, /* Number of arguments. */
5648 const char **argv) /* Argument strings. */
5649 {
5650 exitMainLoop = 0;
5651 Tcl_SetMainLoop(MainLoop);
5652 return TCL_OK;
5653 }
5654
5655 /*
5656 *----------------------------------------------------------------------
5657 *
5658 * TestexitmainloopCmd --
5659 *
5660 * Implements the "testexitmainloop" cmd that is used to test the
5661 * 'Tcl_SetMainLoop' API.
5662 *
5663 * Results:
5664 * A standard Tcl result.
5665 *
5666 * Side effects:
5667 * None.
5668 *
5669 *----------------------------------------------------------------------
5670 */
5671
5672 static int
5673 TestexitmainloopCmd(
5674 ClientData dummy, /* Not used. */
5675 Tcl_Interp *interp,/* Current interpreter. */
5676 int argc, /* Number of arguments. */
5677 const char **argv) /* Argument strings. */
5678 {
5679 exitMainLoop = 1;
5680 return TCL_OK;
5681 }
5682
5683 /*
5684 *----------------------------------------------------------------------
5685 *
5686 * TestChannelCmd --
5687 *
5688 * Implements the Tcl "testchannel" debugging command and its
5689 * subcommands. This is part of the testing environment.
5690 *
5691 * Results:
5692 * A standard Tcl result.
5693 *
5694 * Side effects:
5695 * None.
5696 *
5697 *----------------------------------------------------------------------
5698 */
5699
5700 static int
5701 TestChannelCmd(
5702 ClientData clientData, /* Not used. */
5703 Tcl_Interp *interp, /* Interpreter for result. */
5704 int argc, /* Count of additional args. */
5705 const char **argv) /* Additional arg strings. */
5706 {
5707 const char *cmdName; /* Sub command. */
5708 Tcl_HashTable *hTblPtr; /* Hash table of channels. */
5709 Tcl_HashSearch hSearch; /* Search variable. */
5710 Tcl_HashEntry *hPtr; /* Search variable. */
5711 Channel *chanPtr; /* The actual channel. */
5712 ChannelState *statePtr; /* state info for channel */
5713 Tcl_Channel chan; /* The opaque type. */
5714 size_t len; /* Length of subcommand string. */
5715 int IOQueued; /* How much IO is queued inside channel? */
5716 char buf[TCL_INTEGER_SPACE];/* For snprintf. */
5717 int mode; /* rw mode of the channel */
5718
5719 if (argc < 2) {
5720 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5721 " subcommand ?additional args..?\"", NULL);
5722 return TCL_ERROR;
5723 }
5724 cmdName = argv[1];
5725 len = strlen(cmdName);
5726
5727 chanPtr = NULL;
5728
5729 if (argc > 2) {
5730 if ((cmdName[0] == 's') && (strncmp(cmdName, "splice", len) == 0)) {
5731 /* For splice access the pool of detached channels.
5732 * Locate channel, remove from the list.
5733 */
5734
5735 TestChannel **nextPtrPtr, *curPtr;
5736
5737 chan = (Tcl_Channel) NULL;
5738 for (nextPtrPtr = &firstDetached, curPtr = firstDetached;
5739 curPtr != NULL;
5740 nextPtrPtr = &(curPtr->nextPtr), curPtr = curPtr->nextPtr) {
5741
5742 if (strcmp(argv[2], Tcl_GetChannelName(curPtr->chan)) == 0) {
5743 *nextPtrPtr = curPtr->nextPtr;
5744 curPtr->nextPtr = NULL;
5745 chan = curPtr->chan;
5746 ckfree(curPtr);
5747 break;
5748 }
5749 }
5750 } else {
5751 chan = Tcl_GetChannel(interp, argv[2], &mode);
5752 }
5753 if (chan == (Tcl_Channel) NULL) {
5754 return TCL_ERROR;
5755 }
5756 chanPtr = (Channel *) chan;
5757 statePtr = chanPtr->state;
5758 chanPtr = statePtr->topChanPtr;
5759 chan = (Tcl_Channel) chanPtr;
5760 } else {
5761 statePtr = NULL;
5762 chan = NULL;
5763 }
5764
5765 if ((cmdName[0] == 's') && (strncmp(cmdName, "setchannelerror", len) == 0)) {
5766
5767 Tcl_Obj *msg = Tcl_NewStringObj(argv[3], -1);
5768
5769 Tcl_IncrRefCount(msg);
5770 Tcl_SetChannelError(chan, msg);
5771 Tcl_DecrRefCount(msg);
5772
5773 Tcl_GetChannelError(chan, &msg);
5774 Tcl_SetObjResult(interp, msg);
5775 Tcl_DecrRefCount(msg);
5776 return TCL_OK;
5777 }
5778 if ((cmdName[0] == 's') && (strncmp(cmdName, "setchannelerrorinterp", len) == 0)) {
5779
5780 Tcl_Obj *msg = Tcl_NewStringObj(argv[3], -1);
5781
5782 Tcl_IncrRefCount(msg);
5783 Tcl_SetChannelErrorInterp(interp, msg);
5784 Tcl_DecrRefCount(msg);
5785
5786 Tcl_GetChannelErrorInterp(interp, &msg);
5787 Tcl_SetObjResult(interp, msg);
5788 Tcl_DecrRefCount(msg);
5789 return TCL_OK;
5790 }
5791
5792 /*
5793 * "cut" is actually more a simplified detach facility as provided by the
5794 * Thread package. Without the safeguards of a regular command (no
5795 * checking that the command is truly cut'able, no mutexes for
5796 * thread-safety). Its complementary command is "splice", see below.
5797 */
5798
5799 if ((cmdName[0] == 'c') && (strncmp(cmdName, "cut", len) == 0)) {
5800 TestChannel *det;
5801
5802 if (argc != 3) {
5803 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5804 " cut channelName\"", NULL);
5805 return TCL_ERROR;
5806 }
5807
5808 Tcl_RegisterChannel(NULL, chan); /* prevent closing */
5809 Tcl_UnregisterChannel(interp, chan);
5810
5811 Tcl_CutChannel(chan);
5812
5813 /* Remember the channel in the pool of detached channels */
5814
5815 det = (TestChannel *)ckalloc(sizeof(TestChannel));
5816 det->chan = chan;
5817 det->nextPtr = firstDetached;
5818 firstDetached = det;
5819
5820 return TCL_OK;
5821 }
5822
5823 if ((cmdName[0] == 'c') &&
5824 (strncmp(cmdName, "clearchannelhandlers", len) == 0)) {
5825 if (argc != 3) {
5826 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5827 " clearchannelhandlers channelName\"", NULL);
5828 return TCL_ERROR;
5829 }
5830 Tcl_ClearChannelHandlers(chan);
5831 return TCL_OK;
5832 }
5833
5834 if ((cmdName[0] == 'i') && (strncmp(cmdName, "info", len) == 0)) {
5835 if (argc != 3) {
5836 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
5837 " info channelName\"", NULL);
5838 return TCL_ERROR;
5839 }
5840 Tcl_AppendElement(interp, argv[2]);
5841 Tcl_AppendElement(interp, Tcl_ChannelName(chanPtr->typePtr));
5842 if (statePtr->flags & TCL_READABLE) {
5843 Tcl_AppendElement(interp, "read");
5844 } else {
5845 Tcl_AppendElement(interp, "");
5846 }
5847 if (statePtr->flags & TCL_WRITABLE) {
5848 Tcl_AppendElement(interp, "write");
5849 } else {
5850 Tcl_AppendElement(interp, "");
5851 }
5852 if (statePtr->flags & CHANNEL_NONBLOCKING) {
5853 Tcl_AppendElement(interp, "nonblocking");
5854 } else {
5855 Tcl_AppendElement(interp, "blocking");
5856 }
5857 if (statePtr->flags & CHANNEL_LINEBUFFERED) {
5858 Tcl_AppendElement(interp, "line");
5859 } else if (statePtr->flags & CHANNEL_UNBUFFERED) {
5860 Tcl_AppendElement(interp, "none");
5861 } else {
5862 Tcl_AppendElement(interp, "full");
5863 }
5864 if (statePtr->flags & BG_FLUSH_SCHEDULED) {
5865 Tcl_AppendElement(interp, "async_flush");
5866 } else {
5867 Tcl_AppendElement(interp, "");
5868 }
5869 if (statePtr->flags & CHANNEL_EOF) {
5870 Tcl_AppendElement(interp, "eof");
5871 } else {
5872 Tcl_AppendElement(interp, "");
5873 }
5874 if (statePtr->flags & CHANNEL_BLOCKED) {
5875 Tcl_AppendElement(interp, "blocked");
5876 } else {
5877 Tcl_AppendElement(interp, "unblocked");
5878 }
5879 if (statePtr->inputTranslation == TCL_TRANSLATE_AUTO) {
5880 Tcl_AppendElement(interp, "auto");
5881 if (statePtr->flags & INPUT_SAW_CR) {
5882 Tcl_AppendElement(interp, "saw_cr");
5883 } else {
5884 Tcl_AppendElement(interp, "");
5885 }
5886 } else if (statePtr->inputTranslation == TCL_TRANSLATE_LF) {
5887 Tcl_AppendElement(interp, "lf");
5888 Tcl_AppendElement(interp, "");
5889 } else if (statePtr->inputTranslation == TCL_TRANSLATE_CR) {
5890 Tcl_AppendElement(interp, "cr");
5891 Tcl_AppendElement(interp, "");
5892 } else if (statePtr->inputTranslation == TCL_TRANSLATE_CRLF) {
5893 Tcl_AppendElement(interp, "crlf");
5894 if (statePtr->flags & INPUT_SAW_CR) {
5895 Tcl_AppendElement(interp, "queued_cr");
5896 } else {
5897 Tcl_AppendElement(interp, "");
5898 }
5899 }
5900 if (statePtr->outputTranslation == TCL_TRANSLATE_AUTO) {
5901 Tcl_AppendElement(interp, "auto");
5902 } else if (statePtr->outputTranslation == TCL_TRANSLATE_LF) {
5903 Tcl_AppendElement(interp, "lf");
5904 } else if (statePtr->outputTranslation == TCL_TRANSLATE_CR) {
5905 Tcl_AppendElement(interp, "cr");
5906 } else if (statePtr->outputTranslation == TCL_TRANSLATE_CRLF) {
5907 Tcl_AppendElement(interp, "crlf");
5908 }
5909 IOQueued = Tcl_InputBuffered(chan);
5910 TclFormatInt(buf, IOQueued);
5911 Tcl_AppendElement(interp, buf);
5912
5913 IOQueued = Tcl_OutputBuffered(chan);
5914 TclFormatInt(buf, IOQueued);
5915 Tcl_AppendElement(interp, buf);
5916
5917 TclFormatInt(buf, (int)Tcl_Tell(chan));
5918 Tcl_AppendElement(interp, buf);
5919
5920 TclFormatInt(buf, statePtr->refCount);
5921 Tcl_AppendElement(interp, buf);
5922
5923 return TCL_OK;
5924 }
5925
5926 if ((cmdName[0] == 'i') &&
5927 (strncmp(cmdName, "inputbuffered", len) == 0)) {
5928 if (argc != 3) {
5929 Tcl_AppendResult(interp, "channel name required", NULL);
5930 return TCL_ERROR;
5931 }
5932 IOQueued = Tcl_InputBuffered(chan);
5933 TclFormatInt(buf, IOQueued);
5934 Tcl_AppendResult(interp, buf, NULL);
5935 return TCL_OK;
5936 }
5937
5938 if ((cmdName[0] == 'i') && (strncmp(cmdName, "isshared", len) == 0)) {
5939 if (argc != 3) {
5940 Tcl_AppendResult(interp, "channel name required", NULL);
5941 return TCL_ERROR;
5942 }
5943
5944 TclFormatInt(buf, Tcl_IsChannelShared(chan));
5945 Tcl_AppendResult(interp, buf, NULL);
5946 return TCL_OK;
5947 }
5948
5949 if ((cmdName[0] == 'i') && (strncmp(cmdName, "isstandard", len) == 0)) {
5950 if (argc != 3) {
5951 Tcl_AppendResult(interp, "channel name required", NULL);
5952 return TCL_ERROR;
5953 }
5954
5955 TclFormatInt(buf, Tcl_IsStandardChannel(chan));
5956 Tcl_AppendResult(interp, buf, NULL);
5957 return TCL_OK;
5958 }
5959
5960 if ((cmdName[0] == 'm') && (strncmp(cmdName, "mode", len) == 0)) {
5961 if (argc != 3) {
5962 Tcl_AppendResult(interp, "channel name required", NULL);
5963 return TCL_ERROR;
5964 }
5965
5966 if (statePtr->flags & TCL_READABLE) {
5967 Tcl_AppendElement(interp, "read");
5968 } else {
5969 Tcl_AppendElement(interp, "");
5970 }
5971 if (statePtr->flags & TCL_WRITABLE) {
5972 Tcl_AppendElement(interp, "write");
5973 } else {
5974 Tcl_AppendElement(interp, "");
5975 }
5976 return TCL_OK;
5977 }
5978
5979 if ((cmdName[0] == 'm') && (strncmp(cmdName, "mthread", len) == 0)) {
5980 if (argc != 3) {
5981 Tcl_AppendResult(interp, "channel name required", NULL);
5982 return TCL_ERROR;
5983 }
5984
5985 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(
5986 (Tcl_WideInt)(size_t)Tcl_GetChannelThread(chan)));
5987 return TCL_OK;
5988 }
5989
5990 if ((cmdName[0] == 'n') && (strncmp(cmdName, "name", len) == 0)) {
5991 if (argc != 3) {
5992 Tcl_AppendResult(interp, "channel name required", NULL);
5993 return TCL_ERROR;
5994 }
5995 Tcl_AppendResult(interp, statePtr->channelName, NULL);
5996 return TCL_OK;
5997 }
5998
5999 if ((cmdName[0] == 'o') && (strncmp(cmdName, "open", len) == 0)) {
6000 hTblPtr = (Tcl_HashTable *) Tcl_GetAssocData(interp, "tclIO", NULL);
6001 if (hTblPtr == NULL) {
6002 return TCL_OK;
6003 }
6004 for (hPtr = Tcl_FirstHashEntry(hTblPtr, &hSearch);
6005 hPtr != NULL;
6006 hPtr = Tcl_NextHashEntry(&hSearch)) {
6007 Tcl_AppendElement(interp, (char *)Tcl_GetHashKey(hTblPtr, hPtr));
6008 }
6009 return TCL_OK;
6010 }
6011
6012 if ((cmdName[0] == 'o') &&
6013 (strncmp(cmdName, "outputbuffered", len) == 0)) {
6014 if (argc != 3) {
6015 Tcl_AppendResult(interp, "channel name required", NULL);
6016 return TCL_ERROR;
6017 }
6018
6019 IOQueued = Tcl_OutputBuffered(chan);
6020 TclFormatInt(buf, IOQueued);
6021 Tcl_AppendResult(interp, buf, NULL);
6022 return TCL_OK;
6023 }
6024
6025 if ((cmdName[0] == 'q') &&
6026 (strncmp(cmdName, "queuedcr", len) == 0)) {
6027 if (argc != 3) {
6028 Tcl_AppendResult(interp, "channel name required", NULL);
6029 return TCL_ERROR;
6030 }
6031
6032 Tcl_AppendResult(interp,
6033 (statePtr->flags & INPUT_SAW_CR) ? "1" : "0", NULL);
6034 return TCL_OK;
6035 }
6036
6037 if ((cmdName[0] == 'r') && (strncmp(cmdName, "readable", len) == 0)) {
6038 hTblPtr = (Tcl_HashTable *) Tcl_GetAssocData(interp, "tclIO", NULL);
6039 if (hTblPtr == NULL) {
6040 return TCL_OK;
6041 }
6042 for (hPtr = Tcl_FirstHashEntry(hTblPtr, &hSearch);
6043 hPtr != NULL;
6044 hPtr = Tcl_NextHashEntry(&hSearch)) {
6045 chanPtr = (Channel *) Tcl_GetHashValue(hPtr);
6046 statePtr = chanPtr->state;
6047 if (statePtr->flags & TCL_READABLE) {
6048 Tcl_AppendElement(interp, (char *)Tcl_GetHashKey(hTblPtr, hPtr));
6049 }
6050 }
6051 return TCL_OK;
6052 }
6053
6054 if ((cmdName[0] == 'r') && (strncmp(cmdName, "refcount", len) == 0)) {
6055 if (argc != 3) {
6056 Tcl_AppendResult(interp, "channel name required", NULL);
6057 return TCL_ERROR;
6058 }
6059
6060 TclFormatInt(buf, statePtr->refCount);
6061 Tcl_AppendResult(interp, buf, NULL);
6062 return TCL_OK;
6063 }
6064
6065 /*
6066 * "splice" is actually more a simplified attach facility as provided by
6067 * the Thread package. Without the safeguards of a regular command (no
6068 * checking that the command is truly cut'able, no mutexes for
6069 * thread-safety). Its complementary command is "cut", see above.
6070 */
6071
6072 if ((cmdName[0] == 's') && (strncmp(cmdName, "splice", len) == 0)) {
6073 if (argc != 3) {
6074 Tcl_AppendResult(interp, "channel name required", NULL);
6075 return TCL_ERROR;
6076 }
6077
6078 Tcl_SpliceChannel(chan);
6079
6080 Tcl_RegisterChannel(interp, chan);
6081 Tcl_UnregisterChannel(NULL, chan);
6082
6083 return TCL_OK;
6084 }
6085
6086 if ((cmdName[0] == 't') && (strncmp(cmdName, "type", len) == 0)) {
6087 if (argc != 3) {
6088 Tcl_AppendResult(interp, "channel name required", NULL);
6089 return TCL_ERROR;
6090 }
6091 Tcl_AppendResult(interp, Tcl_ChannelName(chanPtr->typePtr), NULL);
6092 return TCL_OK;
6093 }
6094
6095 if ((cmdName[0] == 'w') && (strncmp(cmdName, "writable", len) == 0)) {
6096 hTblPtr = (Tcl_HashTable *) Tcl_GetAssocData(interp, "tclIO", NULL);
6097 if (hTblPtr == NULL) {
6098 return TCL_OK;
6099 }
6100 for (hPtr = Tcl_FirstHashEntry(hTblPtr, &hSearch);
6101 hPtr != NULL; hPtr = Tcl_NextHashEntry(&hSearch)) {
6102 chanPtr = (Channel *) Tcl_GetHashValue(hPtr);
6103 statePtr = chanPtr->state;
6104 if (statePtr->flags & TCL_WRITABLE) {
6105 Tcl_AppendElement(interp, (char *)Tcl_GetHashKey(hTblPtr, hPtr));
6106 }
6107 }
6108 return TCL_OK;
6109 }
6110
6111 if ((cmdName[0] == 't') && (strncmp(cmdName, "transform", len) == 0)) {
6112 /*
6113 * Syntax: transform channel -command command
6114 */
6115
6116 if (argc != 5) {
6117 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
6118 " transform channelId -command cmd\"", NULL);
6119 return TCL_ERROR;
6120 }
6121 if (strcmp(argv[3], "-command") != 0) {
6122 Tcl_AppendResult(interp, "bad argument \"", argv[3],
6123 "\": should be \"-command\"", NULL);
6124 return TCL_ERROR;
6125 }
6126
6127 return TclChannelTransform(interp, chan,
6128 Tcl_NewStringObj(argv[4], -1));
6129 }
6130
6131 if ((cmdName[0] == 'u') && (strncmp(cmdName, "unstack", len) == 0)) {
6132 /*
6133 * Syntax: unstack channel
6134 */
6135
6136 if (argc != 3) {
6137 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
6138 " unstack channel\"", NULL);
6139 return TCL_ERROR;
6140 }
6141 return Tcl_UnstackChannel(interp, chan);
6142 }
6143
6144 Tcl_AppendResult(interp, "bad option \"", cmdName, "\": should be "
6145 "cut, clearchannelhandlers, info, isshared, mode, open, "
6146 "readable, splice, writable, transform, unstack", NULL);
6147 return TCL_ERROR;
6148 }
6149
6150 /*
6151 *----------------------------------------------------------------------
6152 *
6153 * TestChannelEventCmd --
6154 *
6155 * This procedure implements the "testchannelevent" command. It is used
6156 * to test the Tcl channel event mechanism.
6157 *
6158 * Results:
6159 * A standard Tcl result.
6160 *
6161 * Side effects:
6162 * Creates, deletes and returns channel event handlers.
6163 *
6164 *----------------------------------------------------------------------
6165 */
6166
6167 static int
6168 TestChannelEventCmd(
6169 ClientData dummy, /* Not used. */
6170 Tcl_Interp *interp, /* Current interpreter. */
6171 int argc, /* Number of arguments. */
6172 const char **argv) /* Argument strings. */
6173 {
6174 Tcl_Obj *resultListPtr;
6175 Channel *chanPtr;
6176 ChannelState *statePtr; /* state info for channel */
6177 EventScriptRecord *esPtr, *prevEsPtr, *nextEsPtr;
6178 const char *cmd;
6179 int index, i, mask, len;
6180
6181 if ((argc < 3) || (argc > 5)) {
6182 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
6183 " channelName cmd ?arg1? ?arg2?\"", NULL);
6184 return TCL_ERROR;
6185 }
6186 chanPtr = (Channel *) Tcl_GetChannel(interp, argv[1], NULL);
6187 if (chanPtr == NULL) {
6188 return TCL_ERROR;
6189 }
6190 statePtr = chanPtr->state;
6191
6192 cmd = argv[2];
6193 len = strlen(cmd);
6194 if ((cmd[0] == 'a') && (strncmp(cmd, "add", len) == 0)) {
6195 if (argc != 5) {
6196 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
6197 " channelName add eventSpec script\"", NULL);
6198 return TCL_ERROR;
6199 }
6200 if (strcmp(argv[3], "readable") == 0) {
6201 mask = TCL_READABLE;
6202 } else if (strcmp(argv[3], "writable") == 0) {
6203 mask = TCL_WRITABLE;
6204 } else if (strcmp(argv[3], "none") == 0) {
6205 mask = 0;
6206 } else {
6207 Tcl_AppendResult(interp, "bad event name \"", argv[3],
6208 "\": must be readable, writable, or none", NULL);
6209 return TCL_ERROR;
6210 }
6211
6212 esPtr = (EventScriptRecord *)ckalloc(sizeof(EventScriptRecord));
6213 esPtr->nextPtr = statePtr->scriptRecordPtr;
6214 statePtr->scriptRecordPtr = esPtr;
6215
6216 esPtr->chanPtr = chanPtr;
6217 esPtr->interp = interp;
6218 esPtr->mask = mask;
6219 esPtr->scriptPtr = Tcl_NewStringObj(argv[4], -1);
6220 Tcl_IncrRefCount(esPtr->scriptPtr);
6221
6222 Tcl_CreateChannelHandler((Tcl_Channel) chanPtr, mask,
6223 TclChannelEventScriptInvoker, esPtr);
6224
6225 return TCL_OK;
6226 }
6227
6228 if ((cmd[0] == 'd') && (strncmp(cmd, "delete", len) == 0)) {
6229 if (argc != 4) {
6230 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
6231 " channelName delete index\"", NULL);
6232 return TCL_ERROR;
6233 }
6234 if (Tcl_GetInt(interp, argv[3], &index) == TCL_ERROR) {
6235 return TCL_ERROR;
6236 }
6237 if (index < 0) {
6238 Tcl_AppendResult(interp, "bad event index: ", argv[3],
6239 ": must be nonnegative", NULL);
6240 return TCL_ERROR;
6241 }
6242 for (i = 0, esPtr = statePtr->scriptRecordPtr;
6243 (i < index) && (esPtr != NULL);
6244 i++, esPtr = esPtr->nextPtr) {
6245 /* Empty loop body. */
6246 }
6247 if (esPtr == NULL) {
6248 Tcl_AppendResult(interp, "bad event index ", argv[3],
6249 ": out of range", NULL);
6250 return TCL_ERROR;
6251 }
6252 if (esPtr == statePtr->scriptRecordPtr) {
6253 statePtr->scriptRecordPtr = esPtr->nextPtr;
6254 } else {
6255 for (prevEsPtr = statePtr->scriptRecordPtr;
6256 (prevEsPtr != NULL) &&
6257 (prevEsPtr->nextPtr != esPtr);
6258 prevEsPtr = prevEsPtr->nextPtr) {
6259 /* Empty loop body. */
6260 }
6261 if (prevEsPtr == NULL) {
6262 Tcl_Panic("TestChannelEventCmd: damaged event script list");
6263 }
6264 prevEsPtr->nextPtr = esPtr->nextPtr;
6265 }
6266 Tcl_DeleteChannelHandler((Tcl_Channel) chanPtr,
6267 TclChannelEventScriptInvoker, esPtr);
6268 Tcl_DecrRefCount(esPtr->scriptPtr);
6269 ckfree(esPtr);
6270
6271 return TCL_OK;
6272 }
6273
6274 if ((cmd[0] == 'l') && (strncmp(cmd, "list", len) == 0)) {
6275 if (argc != 3) {
6276 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
6277 " channelName list\"", NULL);
6278 return TCL_ERROR;
6279 }
6280 resultListPtr = Tcl_GetObjResult(interp);
6281 for (esPtr = statePtr->scriptRecordPtr;
6282 esPtr != NULL;
6283 esPtr = esPtr->nextPtr) {
6284 if (esPtr->mask) {
6285 Tcl_ListObjAppendElement(interp, resultListPtr, Tcl_NewStringObj(
6286 (esPtr->mask == TCL_READABLE) ? "readable" : "writable", -1));
6287 } else {
6288 Tcl_ListObjAppendElement(interp, resultListPtr,
6289 Tcl_NewStringObj("none", -1));
6290 }
6291 Tcl_ListObjAppendElement(interp, resultListPtr, esPtr->scriptPtr);
6292 }
6293 Tcl_SetObjResult(interp, resultListPtr);
6294 return TCL_OK;
6295 }
6296
6297 if ((cmd[0] == 'r') && (strncmp(cmd, "removeall", len) == 0)) {
6298 if (argc != 3) {
6299 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
6300 " channelName removeall\"", NULL);
6301 return TCL_ERROR;
6302 }
6303 for (esPtr = statePtr->scriptRecordPtr;
6304 esPtr != NULL;
6305 esPtr = nextEsPtr) {
6306 nextEsPtr = esPtr->nextPtr;
6307 Tcl_DeleteChannelHandler((Tcl_Channel) chanPtr,
6308 TclChannelEventScriptInvoker, esPtr);
6309 Tcl_DecrRefCount(esPtr->scriptPtr);
6310 ckfree(esPtr);
6311 }
6312 statePtr->scriptRecordPtr = NULL;
6313 return TCL_OK;
6314 }
6315
6316 if ((cmd[0] == 's') && (strncmp(cmd, "set", len) == 0)) {
6317 if (argc != 5) {
6318 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
6319 " channelName delete index event\"", NULL);
6320 return TCL_ERROR;
6321 }
6322 if (Tcl_GetInt(interp, argv[3], &index) == TCL_ERROR) {
6323 return TCL_ERROR;
6324 }
6325 if (index < 0) {
6326 Tcl_AppendResult(interp, "bad event index: ", argv[3],
6327 ": must be nonnegative", NULL);
6328 return TCL_ERROR;
6329 }
6330 for (i = 0, esPtr = statePtr->scriptRecordPtr;
6331 (i < index) && (esPtr != NULL);
6332 i++, esPtr = esPtr->nextPtr) {
6333 /* Empty loop body. */
6334 }
6335 if (esPtr == NULL) {
6336 Tcl_AppendResult(interp, "bad event index ", argv[3],
6337 ": out of range", NULL);
6338 return TCL_ERROR;
6339 }
6340
6341 if (strcmp(argv[4], "readable") == 0) {
6342 mask = TCL_READABLE;
6343 } else if (strcmp(argv[4], "writable") == 0) {
6344 mask = TCL_WRITABLE;
6345 } else if (strcmp(argv[4], "none") == 0) {
6346 mask = 0;
6347 } else {
6348 Tcl_AppendResult(interp, "bad event name \"", argv[4],
6349 "\": must be readable, writable, or none", NULL);
6350 return TCL_ERROR;
6351 }
6352 esPtr->mask = mask;
6353 Tcl_CreateChannelHandler((Tcl_Channel) chanPtr, mask,
6354 TclChannelEventScriptInvoker, esPtr);
6355 return TCL_OK;
6356 }
6357 Tcl_AppendResult(interp, "bad command ", cmd, ", must be one of "
6358 "add, delete, list, set, or removeall", NULL);
6359 return TCL_ERROR;
6360 }
6361
6362 /*
6363 *----------------------------------------------------------------------
6364 *
6365 * TestServiceModeCmd --
6366 *
6367 * This procedure implements the "testservicemode" command which gets or
6368 * sets the current Tcl ServiceMode. There are several tests which open
6369 * a file and assign various handlers to it. For these tests to be
6370 * deterministic it is important that file events not be processed until
6371 * all of the handlers are in place.
6372 *
6373 * Results:
6374 * A standard Tcl result.
6375 *
6376 * Side effects:
6377 * May change the ServiceMode setting.
6378 *
6379 *----------------------------------------------------------------------
6380 */
6381
6382 static int
6383 TestServiceModeCmd(
6384 ClientData dummy, /* Not used. */
6385 Tcl_Interp *interp, /* Current interpreter. */
6386 int argc, /* Number of arguments. */
6387 const char **argv) /* Argument strings. */
6388 {
6389 int newmode, oldmode;
6390 if (argc > 2) {
6391 Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
6392 " ?newmode?\"", NULL);
6393 return TCL_ERROR;
6394 }
6395 oldmode = (Tcl_GetServiceMode() != TCL_SERVICE_NONE);
6396 if (argc == 2) {
6397 if (Tcl_GetInt(interp, argv[1], &newmode) == TCL_ERROR) {
6398 return TCL_ERROR;
6399 }
6400 if (newmode == 0) {
6401 Tcl_SetServiceMode(TCL_SERVICE_NONE);
6402 } else {
6403 Tcl_SetServiceMode(TCL_SERVICE_ALL);
6404 }
6405 }
6406 Tcl_SetObjResult(interp, Tcl_NewIntObj(oldmode));
6407 return TCL_OK;
6408 }
6409
6410 /*
6411 *----------------------------------------------------------------------
6412 *
6413 * TestWrongNumArgsObjCmd --
6414 *
6415 * Test the Tcl_WrongNumArgs function.
6416 *
6417 * Results:
6418 * Standard Tcl result.
6419 *
6420 * Side effects:
6421 * Sets interpreter result.
6422 *
6423 *----------------------------------------------------------------------
6424 */
6425
6426 static int
6427 TestWrongNumArgsObjCmd(
6428 ClientData dummy, /* Not used. */
6429 Tcl_Interp *interp, /* Current interpreter. */
6430 int objc, /* Number of arguments. */
6431 Tcl_Obj *const objv[]) /* Argument objects. */
6432 {
6433 int i, length;
6434 const char *msg;
6435
6436 if (objc < 3) {
6437 goto insufArgs;
6438 }
6439
6440 if (Tcl_GetIntFromObj(interp, objv[1], &i) != TCL_OK) {
6441 return TCL_ERROR;
6442 }
6443
6444 msg = Tcl_GetStringFromObj(objv[2], &length);
6445 if (length == 0) {
6446 msg = NULL;
6447 }
6448
6449 if (i > objc - 3) {
6450 /*
6451 * Asked for more arguments than were given.
6452 */
6453 insufArgs:
6454 Tcl_AppendResult(interp, "insufficient arguments", NULL);
6455 return TCL_ERROR;
6456 }
6457
6458 Tcl_WrongNumArgs(interp, i, &(objv[3]), msg);
6459 return TCL_OK;
6460 }
6461
6462 /*
6463 *----------------------------------------------------------------------
6464 *
6465 * TestGetIndexFromObjStructObjCmd --
6466 *
6467 * Test the Tcl_GetIndexFromObjStruct function.
6468 *
6469 * Results:
6470 * Standard Tcl result.
6471 *
6472 * Side effects:
6473 * Sets interpreter result.
6474 *
6475 *----------------------------------------------------------------------
6476 */
6477
6478 static int
6479 TestGetIndexFromObjStructObjCmd(
6480 ClientData dummy, /* Not used. */
6481 Tcl_Interp *interp, /* Current interpreter. */
6482 int objc, /* Number of arguments. */
6483 Tcl_Obj *const objv[]) /* Argument objects. */
6484 {
6485 const char *const ary[] = {
6486 "a", "b", "c", "d", "ee", "ff", NULL, NULL
6487 };
6488 int idx,target, flags = 0;
6489
6490 if (objc != 3 && objc != 4) {
6491 Tcl_WrongNumArgs(interp, 1, objv, "argument targetvalue ?flags?");
6492 return TCL_ERROR;
6493 }
6494 if (Tcl_GetIntFromObj(interp, objv[2], &target) != TCL_OK) {
6495 return TCL_ERROR;
6496 }
6497 if ((objc > 3) && (Tcl_GetIntFromObj(interp, objv[3], &flags) != TCL_OK)) {
6498 return TCL_ERROR;
6499 }
6500 if (Tcl_GetIndexFromObjStruct(interp, (Tcl_GetString(objv[1])[0] ? objv[1] : NULL), ary, 2*sizeof(char *),
6501 "dummy", flags, &idx) != TCL_OK) {
6502 return TCL_ERROR;
6503 }
6504 if (idx != target) {
6505 char buffer[64];
6506 snprintf(buffer, sizeof(buffer), "%d", idx);
6507 Tcl_AppendResult(interp, "index value comparison failed: got ",
6508 buffer, NULL);
6509 snprintf(buffer, sizeof(buffer), "%d", target);
6510 Tcl_AppendResult(interp, " when ", buffer, " expected", NULL);
6511 return TCL_ERROR;
6512 }
6513 Tcl_WrongNumArgs(interp, objc, objv, NULL);
6514 return TCL_OK;
6515 }
6516
6517 /*
6518 *----------------------------------------------------------------------
6519 *
6520 * TestFilesystemObjCmd --
6521 *
6522 * This procedure implements the "testfilesystem" command. It is used to
6523 * test Tcl_FSRegister, Tcl_FSUnregister, and can be used to test that
6524 * the pluggable filesystem works.
6525 *
6526 * Results:
6527 * A standard Tcl result.
6528 *
6529 * Side effects:
6530 * Inserts or removes a filesystem from Tcl's stack.
6531 *
6532 *----------------------------------------------------------------------
6533 */
6534
6535 static int
6536 TestFilesystemObjCmd(
6537 ClientData dummy,
6538 Tcl_Interp *interp,
6539 int objc,
6540 Tcl_Obj *const objv[])
6541 {
6542 int res, boolVal;
6543 const char *msg;
6544
6545 if (objc != 2) {
6546 Tcl_WrongNumArgs(interp, 1, objv, "boolean");
6547 return TCL_ERROR;
6548 }
6549 if (Tcl_GetBooleanFromObj(interp, objv[1], &boolVal) != TCL_OK) {
6550 return TCL_ERROR;
6551 }
6552 if (boolVal) {
6553 res = Tcl_FSRegister(interp, &testReportingFilesystem);
6554 msg = (res == TCL_OK) ? "registered" : "failed";
6555 } else {
6556 res = Tcl_FSUnregister(&testReportingFilesystem);
6557 msg = (res == TCL_OK) ? "unregistered" : "failed";
6558 }
6559 Tcl_SetObjResult(interp, Tcl_NewStringObj(msg , -1));
6560 return res;
6561 }
6562
6563 static int
6564 TestReportInFilesystem(
6565 Tcl_Obj *pathPtr,
6566 ClientData *clientDataPtr)
6567 {
6568 static Tcl_Obj *lastPathPtr = NULL;
6569 Tcl_Obj *newPathPtr;
6570
6571 if (pathPtr == lastPathPtr) {
6572 /* Reject all files second time around */
6573 return -1;
6574 }
6575
6576 /* Try to claim all files first time around */
6577
6578 newPathPtr = Tcl_DuplicateObj(pathPtr);
6579 lastPathPtr = newPathPtr;
6580 Tcl_IncrRefCount(newPathPtr);
6581 if (Tcl_FSGetFileSystemForPath(newPathPtr) == NULL) {
6582 /* Nothing claimed it. Therefore we don't either */
6583 Tcl_DecrRefCount(newPathPtr);
6584 lastPathPtr = NULL;
6585 return -1;
6586 }
6587 lastPathPtr = NULL;
6588 *clientDataPtr = newPathPtr;
6589 return TCL_OK;
6590 }
6591
6592 /*
6593 * Simple helper function to extract the native vfs representation of a path
6594 * object, or NULL if no such representation exists.
6595 */
6596
6597 static Tcl_Obj *
6598 TestReportGetNativePath(
6599 Tcl_Obj *pathPtr)
6600 {
6601 return (Tcl_Obj*) Tcl_FSGetInternalRep(pathPtr, &testReportingFilesystem);
6602 }
6603
6604 static void
6605 TestReportFreeInternalRep(
6606 ClientData clientData)
6607 {
6608 Tcl_Obj *nativeRep = (Tcl_Obj *) clientData;
6609
6610 if (nativeRep != NULL) {
6611 /* Free the path */
6612 Tcl_DecrRefCount(nativeRep);
6613 }
6614 }
6615
6616 static ClientData
6617 TestReportDupInternalRep(
6618 ClientData clientData)
6619 {
6620 Tcl_Obj *original = (Tcl_Obj *) clientData;
6621
6622 Tcl_IncrRefCount(original);
6623 return clientData;
6624 }
6625
6626 static void
6627 TestReport(
6628 const char *cmd,
6629 Tcl_Obj *path,
6630 Tcl_Obj *arg2)
6631 {
6632 Tcl_Interp *interp = (Tcl_Interp *) Tcl_FSData(&testReportingFilesystem);
6633
6634 if (interp == NULL) {
6635 /* This is bad, but not much we can do about it */
6636 } else {
6637 Tcl_Obj *savedResult;
6638 Tcl_DString ds;
6639
6640 Tcl_DStringInit(&ds);
6641 Tcl_DStringAppend(&ds, "lappend filesystemReport ", -1);
6642 Tcl_DStringStartSublist(&ds);
6643 Tcl_DStringAppendElement(&ds, cmd);
6644 if (path != NULL) {
6645 Tcl_DStringAppendElement(&ds, Tcl_GetString(path));
6646 }
6647 if (arg2 != NULL) {
6648 Tcl_DStringAppendElement(&ds, Tcl_GetString(arg2));
6649 }
6650 Tcl_DStringEndSublist(&ds);
6651 savedResult = Tcl_GetObjResult(interp);
6652 Tcl_IncrRefCount(savedResult);
6653 Tcl_SetObjResult(interp, Tcl_NewObj());
6654 Tcl_Eval(interp, Tcl_DStringValue(&ds));
6655 Tcl_DStringFree(&ds);
6656 Tcl_ResetResult(interp);
6657 Tcl_SetObjResult(interp, savedResult);
6658 Tcl_DecrRefCount(savedResult);
6659 }
6660 }
6661
6662 static int
6663 TestReportStat(
6664 Tcl_Obj *path, /* Path of file to stat (in current CP). */
6665 Tcl_StatBuf *buf) /* Filled with results of stat call. */
6666 {
6667 TestReport("stat", path, NULL);
6668 return Tcl_FSStat(TestReportGetNativePath(path), buf);
6669 }
6670
6671 static int
6672 TestReportLstat(
6673 Tcl_Obj *path, /* Path of file to stat (in current CP). */
6674 Tcl_StatBuf *buf) /* Filled with results of stat call. */
6675 {
6676 TestReport("lstat", path, NULL);
6677 return Tcl_FSLstat(TestReportGetNativePath(path), buf);
6678 }
6679
6680 static int
6681 TestReportAccess(
6682 Tcl_Obj *path, /* Path of file to access (in current CP). */
6683 int mode) /* Permission setting. */
6684 {
6685 TestReport("access", path, NULL);
6686 return Tcl_FSAccess(TestReportGetNativePath(path), mode);
6687 }
6688
6689 static Tcl_Channel
6690 TestReportOpenFileChannel(
6691 Tcl_Interp *interp, /* Interpreter for error reporting; can be
6692 * NULL. */
6693 Tcl_Obj *fileName, /* Name of file to open. */
6694 int mode, /* POSIX open mode. */
6695 int permissions) /* If the open involves creating a file, with
6696 * what modes to create it? */
6697 {
6698 TestReport("open", fileName, NULL);
6699 return TclpOpenFileChannel(interp, TestReportGetNativePath(fileName),
6700 mode, permissions);
6701 }
6702
6703 static int
6704 TestReportMatchInDirectory(
6705 Tcl_Interp *interp, /* Interpreter for error messages. */
6706 Tcl_Obj *resultPtr, /* Object to lappend results. */
6707 Tcl_Obj *dirPtr, /* Contains path to directory to search. */
6708 const char *pattern, /* Pattern to match against. */
6709 Tcl_GlobTypeData *types) /* Object containing list of acceptable types.
6710 * May be NULL. */
6711 {
6712 if (types != NULL && types->type & TCL_GLOB_TYPE_MOUNT) {
6713 TestReport("matchmounts", dirPtr, NULL);
6714 return TCL_OK;
6715 } else {
6716 TestReport("matchindirectory", dirPtr, NULL);
6717 return Tcl_FSMatchInDirectory(interp, resultPtr,
6718 TestReportGetNativePath(dirPtr), pattern, types);
6719 }
6720 }
6721
6722 static int
6723 TestReportChdir(
6724 Tcl_Obj *dirName)
6725 {
6726 TestReport("chdir", dirName, NULL);
6727 return Tcl_FSChdir(TestReportGetNativePath(dirName));
6728 }
6729
6730 static int
6731 TestReportLoadFile(
6732 Tcl_Interp *interp, /* Used for error reporting. */
6733 Tcl_Obj *fileName, /* Name of the file containing the desired
6734 * code. */
6735 Tcl_LoadHandle *handlePtr, /* Filled with token for dynamically loaded
6736 * file which will be passed back to
6737 * (*unloadProcPtr)() to unload the file. */
6738 Tcl_FSUnloadFileProc **unloadProcPtr)
6739 /* Filled with address of Tcl_FSUnloadFileProc
6740 * function which should be used for
6741 * this file. */
6742 {
6743 TestReport("loadfile", fileName, NULL);
6744 return Tcl_FSLoadFile(interp, TestReportGetNativePath(fileName), NULL,
6745 NULL, NULL, NULL, handlePtr, unloadProcPtr);
6746 }
6747
6748 static Tcl_Obj *
6749 TestReportLink(
6750 Tcl_Obj *path, /* Path of file to readlink or link */
6751 Tcl_Obj *to, /* Path of file to link to, or NULL */
6752 int linkType)
6753 {
6754 TestReport("link", path, to);
6755 return Tcl_FSLink(TestReportGetNativePath(path), to, linkType);
6756 }
6757
6758 static int
6759 TestReportRenameFile(
6760 Tcl_Obj *src, /* Pathname of file or dir to be renamed
6761 * (UTF-8). */
6762 Tcl_Obj *dst) /* New pathname of file or directory
6763 * (UTF-8). */
6764 {
6765 TestReport("renamefile", src, dst);
6766 return Tcl_FSRenameFile(TestReportGetNativePath(src),
6767 TestReportGetNativePath(dst));
6768 }
6769
6770 static int
6771 TestReportCopyFile(
6772 Tcl_Obj *src, /* Pathname of file to be copied (UTF-8). */
6773 Tcl_Obj *dst) /* Pathname of file to copy to (UTF-8). */
6774 {
6775 TestReport("copyfile", src, dst);
6776 return Tcl_FSCopyFile(TestReportGetNativePath(src),
6777 TestReportGetNativePath(dst));
6778 }
6779
6780 static int
6781 TestReportDeleteFile(
6782 Tcl_Obj *path) /* Pathname of file to be removed (UTF-8). */
6783 {
6784 TestReport("deletefile", path, NULL);
6785 return Tcl_FSDeleteFile(TestReportGetNativePath(path));
6786 }
6787
6788 static int
6789 TestReportCreateDirectory(
6790 Tcl_Obj *path) /* Pathname of directory to create (UTF-8). */
6791 {
6792 TestReport("createdirectory", path, NULL);
6793 return Tcl_FSCreateDirectory(TestReportGetNativePath(path));
6794 }
6795
6796 static int
6797 TestReportCopyDirectory(
6798 Tcl_Obj *src, /* Pathname of directory to be copied
6799 * (UTF-8). */
6800 Tcl_Obj *dst, /* Pathname of target directory (UTF-8). */
6801 Tcl_Obj **errorPtr) /* If non-NULL, to be filled with UTF-8 name
6802 * of file causing error. */
6803 {
6804 TestReport("copydirectory", src, dst);
6805 return Tcl_FSCopyDirectory(TestReportGetNativePath(src),
6806 TestReportGetNativePath(dst), errorPtr);
6807 }
6808
6809 static int
6810 TestReportRemoveDirectory(
6811 Tcl_Obj *path, /* Pathname of directory to be removed
6812 * (UTF-8). */
6813 int recursive, /* If non-zero, removes directories that
6814 * are nonempty. Otherwise, will only remove
6815 * empty directories. */
6816 Tcl_Obj **errorPtr) /* If non-NULL, to be filled with UTF-8 name
6817 * of file causing error. */
6818 {
6819 TestReport("removedirectory", path, NULL);
6820 return Tcl_FSRemoveDirectory(TestReportGetNativePath(path), recursive,
6821 errorPtr);
6822 }
6823
6824 static const char *const *
6825 TestReportFileAttrStrings(
6826 Tcl_Obj *fileName,
6827 Tcl_Obj **objPtrRef)
6828 {
6829 TestReport("fileattributestrings", fileName, NULL);
6830 return Tcl_FSFileAttrStrings(TestReportGetNativePath(fileName), objPtrRef);
6831 }
6832
6833 static int
6834 TestReportFileAttrsGet(
6835 Tcl_Interp *interp, /* The interpreter for error reporting. */
6836 int index, /* index of the attribute command. */
6837 Tcl_Obj *fileName, /* filename we are operating on. */
6838 Tcl_Obj **objPtrRef) /* for output. */
6839 {
6840 TestReport("fileattributesget", fileName, NULL);
6841 return Tcl_FSFileAttrsGet(interp, index,
6842 TestReportGetNativePath(fileName), objPtrRef);
6843 }
6844
6845 static int
6846 TestReportFileAttrsSet(
6847 Tcl_Interp *interp, /* The interpreter for error reporting. */
6848 int index, /* index of the attribute command. */
6849 Tcl_Obj *fileName, /* filename we are operating on. */
6850 Tcl_Obj *objPtr) /* for input. */
6851 {
6852 TestReport("fileattributesset", fileName, objPtr);
6853 return Tcl_FSFileAttrsSet(interp, index,
6854 TestReportGetNativePath(fileName), objPtr);
6855 }
6856
6857 static int
6858 TestReportUtime(
6859 Tcl_Obj *fileName,
6860 struct utimbuf *tval)
6861 {
6862 TestReport("utime", fileName, NULL);
6863 return Tcl_FSUtime(TestReportGetNativePath(fileName), tval);
6864 }
6865
6866 static int
6867 TestReportNormalizePath(
6868 Tcl_Interp *interp,
6869 Tcl_Obj *pathPtr,
6870 int nextCheckpoint)
6871 {
6872 TestReport("normalizepath", pathPtr, NULL);
6873 return nextCheckpoint;
6874 }
6875
6876 static int
6877 SimplePathInFilesystem(
6878 Tcl_Obj *pathPtr,
6879 ClientData *clientDataPtr)
6880 {
6881 const char *str = Tcl_GetString(pathPtr);
6882
6883 if (strncmp(str, "simplefs:/", 10)) {
6884 return -1;
6885 }
6886 return TCL_OK;
6887 }
6888
6889 /*
6890 * This is a slightly 'hacky' filesystem which is used just to test a few
6891 * important features of the vfs code: (1) that you can load a shared library
6892 * from a vfs, (2) that when copying files from one fs to another, the 'mtime'
6893 * is preserved. (3) that recursive cross-filesystem directory copies have the
6894 * correct behaviour with/without -force.
6895 *
6896 * It treats any file in 'simplefs:/' as a file, which it routes to the
6897 * current directory. The real file it uses is whatever follows the trailing
6898 * '/' (e.g. 'foo' in 'simplefs:/foo'), and that file exists or not according
6899 * to what is in the native pwd.
6900 *
6901 * Please do not consider this filesystem a model of how things are to be
6902 * done. It is quite the opposite! But, it does allow us to test some
6903 * important features.
6904 */
6905
6906 static int
6907 TestSimpleFilesystemObjCmd(
6908 ClientData dummy,
6909 Tcl_Interp *interp,
6910 int objc,
6911 Tcl_Obj *const objv[])
6912 {
6913 int res, boolVal;
6914 const char *msg;
6915
6916 if (objc != 2) {
6917 Tcl_WrongNumArgs(interp, 1, objv, "boolean");
6918 return TCL_ERROR;
6919 }
6920 if (Tcl_GetBooleanFromObj(interp, objv[1], &boolVal) != TCL_OK) {
6921 return TCL_ERROR;
6922 }
6923 if (boolVal) {
6924 res = Tcl_FSRegister(interp, &simpleFilesystem);
6925 msg = (res == TCL_OK) ? "registered" : "failed";
6926 } else {
6927 res = Tcl_FSUnregister(&simpleFilesystem);
6928 msg = (res == TCL_OK) ? "unregistered" : "failed";
6929 }
6930 Tcl_SetObjResult(interp, Tcl_NewStringObj(msg , -1));
6931 return res;
6932 }
6933
6934 /*
6935 * Treats a file name 'simplefs:/foo' by using the file 'foo' in the current
6936 * (native) directory.
6937 */
6938
6939 static Tcl_Obj *
6940 SimpleRedirect(
6941 Tcl_Obj *pathPtr) /* Name of file to copy. */
6942 {
6943 int len;
6944 const char *str;
6945 Tcl_Obj *origPtr;
6946
6947 /*
6948 * We assume the same name in the current directory is ok.
6949 */
6950
6951 str = Tcl_GetStringFromObj(pathPtr, &len);
6952 if (len < 10 || strncmp(str, "simplefs:/", 10)) {
6953 /* Probably shouldn't ever reach here */
6954 Tcl_IncrRefCount(pathPtr);
6955 return pathPtr;
6956 }
6957 origPtr = Tcl_NewStringObj(str+10, -1);
6958 Tcl_IncrRefCount(origPtr);
6959 return origPtr;
6960 }
6961
6962 static int
6963 SimpleMatchInDirectory(
6964 Tcl_Interp *interp, /* Interpreter for error
6965 * messages. */
6966 Tcl_Obj *resultPtr, /* Object to lappend results. */
6967 Tcl_Obj *dirPtr, /* Contains path to directory to search. */
6968 const char *pattern, /* Pattern to match against. */
6969 Tcl_GlobTypeData *types) /* Object containing list of acceptable types.
6970 * May be NULL. */
6971 {
6972 int res;
6973 Tcl_Obj *origPtr;
6974 Tcl_Obj *resPtr;
6975
6976 /* We only provide a new volume, therefore no mounts at all */
6977 if (types != NULL && types->type & TCL_GLOB_TYPE_MOUNT) {
6978 return TCL_OK;
6979 }
6980
6981 /*
6982 * We assume the same name in the current directory is ok.
6983 */
6984 resPtr = Tcl_NewObj();
6985 Tcl_IncrRefCount(resPtr);
6986 origPtr = SimpleRedirect(dirPtr);
6987 res = Tcl_FSMatchInDirectory(interp, resPtr, origPtr, pattern, types);
6988 if (res == TCL_OK) {
6989 int gLength, j;
6990 Tcl_ListObjLength(NULL, resPtr, &gLength);
6991 for (j = 0; j < gLength; j++) {
6992 Tcl_Obj *gElt, *nElt;
6993 Tcl_ListObjIndex(NULL, resPtr, j, &gElt);
6994 nElt = Tcl_NewStringObj("simplefs:/",10);
6995 Tcl_AppendObjToObj(nElt, gElt);
6996 Tcl_ListObjAppendElement(NULL, resultPtr, nElt);
6997 }
6998 }
6999 Tcl_DecrRefCount(origPtr);
7000 Tcl_DecrRefCount(resPtr);
7001 return res;
7002 }
7003
7004 static Tcl_Channel
7005 SimpleOpenFileChannel(
7006 Tcl_Interp *interp, /* Interpreter for error reporting; can be
7007 * NULL. */
7008 Tcl_Obj *pathPtr, /* Name of file to open. */
7009 int mode, /* POSIX open mode. */
7010 int permissions) /* If the open involves creating a file, with
7011 * what modes to create it? */
7012 {
7013 Tcl_Obj *tempPtr;
7014 Tcl_Channel chan;
7015
7016 if ((mode & O_ACCMODE) != O_RDONLY) {
7017 Tcl_AppendResult(interp, "read-only", NULL);
7018 return NULL;
7019 }
7020
7021 tempPtr = SimpleRedirect(pathPtr);
7022 chan = Tcl_FSOpenFileChannel(interp, tempPtr, "r", permissions);
7023 Tcl_DecrRefCount(tempPtr);
7024 return chan;
7025 }
7026
7027 static int
7028 SimpleAccess(
7029 Tcl_Obj *pathPtr, /* Path of file to access (in current CP). */
7030 int mode) /* Permission setting. */
7031 {
7032 Tcl_Obj *tempPtr = SimpleRedirect(pathPtr);
7033 int res = Tcl_FSAccess(tempPtr, mode);
7034
7035 Tcl_DecrRefCount(tempPtr);
7036 return res;
7037 }
7038
7039 static int
7040 SimpleStat(
7041 Tcl_Obj *pathPtr, /* Path of file to stat (in current CP). */
7042 Tcl_StatBuf *bufPtr) /* Filled with results of stat call. */
7043 {
7044 Tcl_Obj *tempPtr = SimpleRedirect(pathPtr);
7045 int res = Tcl_FSStat(tempPtr, bufPtr);
7046
7047 Tcl_DecrRefCount(tempPtr);
7048 return res;
7049 }
7050
7051 static Tcl_Obj *
7052 SimpleListVolumes(void)
7053 {
7054 /* Add one new volume */
7055 Tcl_Obj *retVal;
7056
7057 retVal = Tcl_NewStringObj("simplefs:/", -1);
7058 Tcl_IncrRefCount(retVal);
7059 return retVal;
7060 }
7061
7062 /*
7063 * Used to check operations of Tcl_UtfNext.
7064 *
7065 * Usage: testutfnext $bytes $offset
7066 */
7067
7068 static int
7069 TestUtfNextCmd(
7070 ClientData dummy,
7071 Tcl_Interp *interp,
7072 int objc,
7073 Tcl_Obj *const objv[])
7074 {
7075 int numBytes; /* Number of bytes supplied in the test string */
7076 int offset; /* Number of bytes we are permitted to read */
7077 char *bytes;
7078 const char *result, *first;
7079 char buffer[32];
7080 static const char tobetested[] = "A\xA0\xC0\xC1\xC2\xD0\xE0\xE8\xF2\xF7\xF8\xFE\xFF";
7081 const char *p = tobetested;
7082 (void)dummy;
7083
7084 if (objc < 2 || objc > 3) {
7085 Tcl_WrongNumArgs(interp, 1, objv, "string ?numBytes?");
7086 return TCL_ERROR;
7087 }
7088
7089 bytes = Tcl_GetStringFromObj(objv[1], &numBytes);
7090
7091 offset = numBytes +TCL_UTF_MAX -1; /* If no constraint is given, allow
7092 * the terminating NUL to limit
7093 * operations. */
7094
7095 if (objc == 3) {
7096 if (TCL_OK != TclGetIntForIndex(interp, objv[2], numBytes, &offset)) {
7097 return TCL_ERROR;
7098 }
7099 if (offset < 0) {
7100 offset = 0;
7101 }
7102 if (offset > numBytes +TCL_UTF_MAX -1) {
7103 offset = numBytes +TCL_UTF_MAX -1;
7104 }
7105 }
7106
7107 if (numBytes > (int)sizeof(buffer) - 3) {
7108 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
7109 "\"testutfnext\" can only handle %d bytes",
7110 (int)sizeof(buffer) - 4));
7111 return TCL_ERROR;
7112 }
7113
7114 memcpy(buffer + 1, bytes, numBytes);
7115 buffer[0] = buffer[numBytes + 1] = buffer[numBytes + 2] = buffer[numBytes + 3] = '\xA0';
7116
7117 if (!Tcl_UtfCharComplete(buffer + 1, offset)) {
7118 /* Cannot scan a complete sequence from the data */
7119
7120 Tcl_SetObjResult(interp, Tcl_NewIntObj(0));
7121 return TCL_OK;
7122 }
7123
7124 first = result = Tcl_UtfNext(buffer + 1);
7125 while ((buffer[0] = *p++) != '\0') {
7126 /* Run Tcl_UtfNext with many more possible bytes at src[-1], all should give the same result */
7127 result = Tcl_UtfNext(buffer + 1);
7128 if (first != result) {
7129 Tcl_AppendResult(interp, "Tcl_UtfNext is not supposed to read src[-1]", NULL);
7130 return TCL_ERROR;
7131 }
7132 }
7133 p = tobetested;
7134 while ((buffer[numBytes + 1] = *p++) != '\0') {
7135 /* Run Tcl_UtfNext with many more possible bytes at src[end], all should give the same result */
7136 result = Tcl_UtfNext(buffer + 1);
7137 if (first != result) {
7138 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
7139 "Tcl_UtfNext is not supposed to read src[end]\n"
7140 "Different result when src[end] is %#x", UCHAR(p[-1])));
7141 return TCL_ERROR;
7142 }
7143 }
7144
7145 Tcl_SetObjResult(interp, Tcl_NewIntObj(first - buffer - 1));
7146
7147 return TCL_OK;
7148 }
7149 /*
7150 * Used to check operations of Tcl_UtfPrev.
7151 *
7152 * Usage: testutfprev $bytes $offset
7153 */
7154
7155 static int
7156 TestUtfPrevCmd(
7157 ClientData clientData,
7158 Tcl_Interp *interp,
7159 int objc,
7160 Tcl_Obj *const objv[])
7161 {
7162 int numBytes, offset;
7163 char *bytes;
7164 const char *result;
7165
7166 if (objc < 2 || objc > 3) {
7167 Tcl_WrongNumArgs(interp, 1, objv, "bytes ?offset?");
7168 return TCL_ERROR;
7169 }
7170
7171 bytes = Tcl_GetStringFromObj(objv[1], &numBytes);
7172
7173 if (objc == 3) {
7174 if (TCL_OK != TclGetIntForIndex(interp, objv[2], numBytes, &offset)) {
7175 return TCL_ERROR;
7176 }
7177 if (offset < 0) {
7178 offset = 0;
7179 }
7180 if (offset > numBytes) {
7181 offset = numBytes;
7182 }
7183 } else {
7184 offset = numBytes;
7185 }
7186 result = TclUtfPrev(bytes + offset, bytes);
7187 Tcl_SetObjResult(interp, Tcl_NewIntObj(result - bytes));
7188 return TCL_OK;
7189 }
7190
7191 /*
7192 * Used to check correct string-length determining in Tcl_NumUtfChars
7193 */
7194
7195 static int
7196 TestNumUtfCharsCmd(
7197 ClientData clientData,
7198 Tcl_Interp *interp,
7199 int objc,
7200 Tcl_Obj *const objv[])
7201 {
7202 if (objc > 1) {
7203 int numBytes, len, limit = -1;
7204 const char *bytes = Tcl_GetStringFromObj(objv[1], &numBytes);
7205
7206 if (objc > 2) {
7207 if (TclGetIntForIndex(interp, objv[2], numBytes, &limit) != TCL_OK) {
7208 return TCL_ERROR;
7209 }
7210 if (limit > numBytes + 1) {
7211 limit = numBytes + 1;
7212 }
7213 }
7214 len = Tcl_NumUtfChars(bytes, limit);
7215 Tcl_SetObjResult(interp, Tcl_NewIntObj(len));
7216 }
7217 return TCL_OK;
7218 }
7219
7220
7221 /*
7222 * Used to check correct operation of Tcl_GetUniChar
7223 * testgetunichar STRING INDEX
7224 * This differs from just using "string index" in being a direct
7225 * call to Tcl_GetUniChar without any prior range checking.
7226 */
7227 static int
7228 TestGetUniCharCmd(
7229 ClientData dummy, /* Not used. */
7230 Tcl_Interp *interp, /* Current interpreter */
7231 int objc, /* Number of arguments */
7232 Tcl_Obj *const objv[] /* Argument strings */
7233 )
7234 {
7235 int index;
7236 int c ;
7237 if (objc != 3) {
7238 Tcl_WrongNumArgs(interp, 1, objv, "STRING INDEX");
7239 return TCL_ERROR;
7240 }
7241 Tcl_GetIntFromObj(interp, objv[2], &index);
7242 c = Tcl_GetUniChar(objv[1], index);
7243 Tcl_SetObjResult(interp, Tcl_NewIntObj(c));
7244
7245 return TCL_OK;
7246 }
7247
7248 /*
7249 * Used to check correct operation of Tcl_UtfFindFirst
7250 */
7251
7252 static int
7253 TestFindFirstCmd(
7254 ClientData clientData,
7255 Tcl_Interp *interp,
7256 int objc,
7257 Tcl_Obj *const objv[])
7258 {
7259 if (objc > 1) {
7260 int len = -1;
7261
7262 if (objc > 2) {
7263 (void) Tcl_GetIntFromObj(interp, objv[2], &len);
7264 }
7265 Tcl_SetObjResult(interp, Tcl_NewStringObj(Tcl_UtfFindFirst(Tcl_GetString(objv[1]), len), -1));
7266 }
7267 return TCL_OK;
7268 }
7269
7270 /*
7271 * Used to check correct operation of Tcl_UtfFindLast
7272 */
7273
7274 static int
7275 TestFindLastCmd(
7276 ClientData clientData,
7277 Tcl_Interp *interp,
7278 int objc,
7279 Tcl_Obj *const objv[])
7280 {
7281 if (objc > 1) {
7282 int len = -1;
7283
7284 if (objc > 2) {
7285 (void) Tcl_GetIntFromObj(interp, objv[2], &len);
7286 }
7287 Tcl_SetObjResult(interp, Tcl_NewStringObj(Tcl_UtfFindLast(Tcl_GetString(objv[1]), len), -1));
7288 }
7289 return TCL_OK;
7290 }
7291
7292 #if defined(HAVE_CPUID) && !defined(MAC_OSX_TCL)
7293 /*
7294 *----------------------------------------------------------------------
7295 *
7296 * TestcpuidCmd --
7297 *
7298 * Retrieves CPU ID information.
7299 *
7300 * Usage:
7301 * testwincpuid <eax>
7302 *
7303 * Parameters:
7304 * eax - The value to pass in the EAX register to a CPUID instruction.
7305 *
7306 * Results:
7307 * Returns a four-element list containing the values from the EAX, EBX,
7308 * ECX and EDX registers returned from the CPUID instruction.
7309 *
7310 * Side effects:
7311 * None.
7312 *
7313 *----------------------------------------------------------------------
7314 */
7315
7316 static int
7317 TestcpuidCmd(
7318 ClientData dummy,
7319 Tcl_Interp* interp, /* Tcl interpreter */
7320 int objc, /* Parameter count */
7321 Tcl_Obj *const * objv) /* Parameter vector */
7322 {
7323 int status, index, i;
7324 unsigned int regs[4];
7325 Tcl_Obj *regsObjs[4];
7326
7327 if (objc != 2) {
7328 Tcl_WrongNumArgs(interp, 1, objv, "eax");
7329 return TCL_ERROR;
7330 }
7331 if (Tcl_GetIntFromObj(interp, objv[1], &index) != TCL_OK) {
7332 return TCL_ERROR;
7333 }
7334 status = TclWinCPUID((unsigned) index, regs);
7335 if (status != TCL_OK) {
7336 Tcl_SetObjResult(interp,
7337 Tcl_NewStringObj("operation not available", -1));
7338 return status;
7339 }
7340 for (i=0 ; i<4 ; ++i) {
7341 regsObjs[i] = Tcl_NewIntObj((int) regs[i]);
7342 }
7343 Tcl_SetObjResult(interp, Tcl_NewListObj(4, regsObjs));
7344 return TCL_OK;
7345 }
7346 #endif
7347
7348 /*
7349 * Used to do basic checks of the TCL_HASH_KEY_SYSTEM_HASH flag
7350 */
7351
7352 static int
7353 TestHashSystemHashCmd(
7354 ClientData dummy,
7355 Tcl_Interp *interp,
7356 int objc,
7357 Tcl_Obj *const objv[])
7358 {
7359 static const Tcl_HashKeyType hkType = {
7360 TCL_HASH_KEY_TYPE_VERSION, TCL_HASH_KEY_SYSTEM_HASH,
7361 NULL, NULL, NULL, NULL
7362 };
7363 Tcl_HashTable hash;
7364 Tcl_HashEntry *hPtr;
7365 int i, isNew, limit = 100;
7366 (void)dummy;
7367
7368 if (objc>1 && Tcl_GetIntFromObj(interp, objv[1], &limit)!=TCL_OK) {
7369 return TCL_ERROR;
7370 }
7371
7372 Tcl_InitCustomHashTable(&hash, TCL_CUSTOM_TYPE_KEYS, &hkType);
7373
7374 if (hash.numEntries != 0) {
7375 Tcl_AppendResult(interp, "non-zero initial size", NULL);
7376 Tcl_DeleteHashTable(&hash);
7377 return TCL_ERROR;
7378 }
7379
7380 for (i=0 ; i<limit ; i++) {
7381 hPtr = Tcl_CreateHashEntry(&hash, INT2PTR(i), &isNew);
7382 if (!isNew) {
7383 Tcl_SetObjResult(interp, Tcl_NewIntObj(i));
7384 Tcl_AppendToObj(Tcl_GetObjResult(interp)," creation problem", -1);
7385 Tcl_DeleteHashTable(&hash);
7386 return TCL_ERROR;
7387 }
7388 Tcl_SetHashValue(hPtr, INT2PTR(i+42));
7389 }
7390
7391 if (hash.numEntries != limit) {
7392 Tcl_AppendResult(interp, "unexpected maximal size", NULL);
7393 Tcl_DeleteHashTable(&hash);
7394 return TCL_ERROR;
7395 }
7396
7397 for (i=0 ; i<limit ; i++) {
7398 hPtr = Tcl_FindHashEntry(&hash, (char *) INT2PTR(i));
7399 if (hPtr == NULL) {
7400 Tcl_SetObjResult(interp, Tcl_NewIntObj(i));
7401 Tcl_AppendToObj(Tcl_GetObjResult(interp)," lookup problem",-1);
7402 Tcl_DeleteHashTable(&hash);
7403 return TCL_ERROR;
7404 }
7405 if (PTR2INT(Tcl_GetHashValue(hPtr)) != i+42) {
7406 Tcl_SetObjResult(interp, Tcl_NewIntObj(i));
7407 Tcl_AppendToObj(Tcl_GetObjResult(interp)," value problem",-1);
7408 Tcl_DeleteHashTable(&hash);
7409 return TCL_ERROR;
7410 }
7411 Tcl_DeleteHashEntry(hPtr);
7412 }
7413
7414 if (hash.numEntries != 0) {
7415 Tcl_AppendResult(interp, "non-zero final size", NULL);
7416 Tcl_DeleteHashTable(&hash);
7417 return TCL_ERROR;
7418 }
7419
7420 Tcl_DeleteHashTable(&hash);
7421 Tcl_AppendResult(interp, "OK", NULL);
7422 return TCL_OK;
7423 }
7424
7425 /*
7426 * Used for testing Tcl_GetInt which is no longer used directly by the
7427 * core very much.
7428 */
7429 static int
7430 TestgetintCmd(
7431 ClientData dummy,
7432 Tcl_Interp *interp,
7433 int argc,
7434 const char **argv)
7435 {
7436 (void)dummy;
7437
7438 if (argc < 2) {
7439 Tcl_SetResult(interp, "wrong # args", TCL_STATIC);
7440 return TCL_ERROR;
7441 } else {
7442 int val, i, total=0;
7443
7444 for (i=1 ; i<argc ; i++) {
7445 if (Tcl_GetInt(interp, argv[i], &val) != TCL_OK) {
7446 return TCL_ERROR;
7447 }
7448 total += val;
7449 }
7450 Tcl_SetObjResult(interp, Tcl_NewIntObj(total));
7451 return TCL_OK;
7452 }
7453 }
7454
7455 static int
7456 NREUnwind_callback(
7457 ClientData data[],
7458 Tcl_Interp *interp,
7459 int result)
7460 {
7461 void *cStackPtr = TclGetCStackPtr();
7462 (void)result;
7463
7464 if (data[0] == INT2PTR(-1)) {
7465 Tcl_NRAddCallback(interp, NREUnwind_callback, cStackPtr, INT2PTR(-1),
7466 INT2PTR(-1), NULL);
7467 } else if (data[1] == INT2PTR(-1)) {
7468 Tcl_NRAddCallback(interp, NREUnwind_callback, data[0], cStackPtr,
7469 INT2PTR(-1), NULL);
7470 } else if (data[2] == INT2PTR(-1)) {
7471 Tcl_NRAddCallback(interp, NREUnwind_callback, data[0], data[1],
7472 cStackPtr, NULL);
7473 } else {
7474 Tcl_Obj *idata[3];
7475 idata[0] = Tcl_NewIntObj((int) ((char *) data[1] - (char *) data[0]));
7476 idata[1] = Tcl_NewIntObj((int) ((char *) data[2] - (char *) data[0]));
7477 idata[2] = Tcl_NewIntObj((int) ((char *) cStackPtr - (char *) data[0]));
7478 Tcl_SetObjResult(interp, Tcl_NewListObj(3, idata));
7479 }
7480 return TCL_OK;
7481 }
7482
7483 static int
7484 TestNREUnwind(
7485 ClientData dummy,
7486 Tcl_Interp *interp,
7487 int objc,
7488 Tcl_Obj *const objv[])
7489 {
7490 (void)dummy;
7491 (void)objc;
7492 (void)objv;
7493
7494 /*
7495 * Insure that callbacks effectively run at the proper level during the
7496 * unwinding of the NRE stack.
7497 */
7498
7499 Tcl_NRAddCallback(interp, NREUnwind_callback, INT2PTR(-1), INT2PTR(-1),
7500 INT2PTR(-1), NULL);
7501 return TCL_OK;
7502 }
7503
7504
7505 static int
7506 TestNRELevels(
7507 ClientData dummy,
7508 Tcl_Interp *interp,
7509 int objc,
7510 Tcl_Obj *const objv[])
7511 {
7512 Interp *iPtr = (Interp *) interp;
7513 static ptrdiff_t *refDepth = NULL;
7514 ptrdiff_t depth;
7515 Tcl_Obj *levels[6];
7516 int i = 0;
7517 NRE_callback *cbPtr = iPtr->execEnvPtr->callbackPtr;
7518 (void)dummy;
7519 (void)objc;
7520 (void)objv;
7521
7522 if (refDepth == NULL) {
7523 refDepth = (ptrdiff_t *)TclGetCStackPtr();
7524 }
7525
7526 depth = (refDepth - (ptrdiff_t *)TclGetCStackPtr());
7527
7528 levels[0] = Tcl_NewIntObj(depth);
7529 levels[1] = Tcl_NewIntObj(iPtr->numLevels);
7530 levels[2] = Tcl_NewIntObj(iPtr->cmdFramePtr->level);
7531 levels[3] = Tcl_NewIntObj(iPtr->varFramePtr->level);
7532 levels[4] = Tcl_NewIntObj(iPtr->execEnvPtr->execStackPtr->tosPtr
7533 - iPtr->execEnvPtr->execStackPtr->stackWords);
7534
7535 while (cbPtr) {
7536 i++;
7537 cbPtr = cbPtr->nextPtr;
7538 }
7539 levels[5] = Tcl_NewIntObj(i);
7540
7541 Tcl_SetObjResult(interp, Tcl_NewListObj(6, levels));
7542 return TCL_OK;
7543 }
7544
7545 /*
7546 *----------------------------------------------------------------------
7547 *
7548 * TestconcatobjCmd --
7549 *
7550 * This procedure implements the "testconcatobj" command. It is used
7551 * to test that Tcl_ConcatObj does indeed return a fresh Tcl_Obj in all
7552 * cases and that it never corrupts its arguments. In other words, that
7553 * [Bug 1447328] was fixed properly.
7554 *
7555 * Results:
7556 * A standard Tcl result.
7557 *
7558 * Side effects:
7559 * None.
7560 *
7561 *----------------------------------------------------------------------
7562 */
7563
7564 static int
7565 TestconcatobjCmd(
7566 ClientData dummy, /* Not used. */
7567 Tcl_Interp *interp, /* Current interpreter. */
7568 int argc, /* Number of arguments. */
7569 const char **argv) /* Argument strings. */
7570 {
7571 Tcl_Obj *list1Ptr, *list2Ptr, *emptyPtr, *concatPtr, *tmpPtr;
7572 int result = TCL_OK, len;
7573 Tcl_Obj *objv[3];
7574
7575 /*
7576 * Set the start of the error message as obj result; it will be cleared at
7577 * the end if no errors were found.
7578 */
7579
7580 Tcl_SetObjResult(interp,
7581 Tcl_NewStringObj("Tcl_ConcatObj is unsafe:", -1));
7582
7583 emptyPtr = Tcl_NewObj();
7584
7585 list1Ptr = Tcl_NewStringObj("foo bar sum", -1);
7586 Tcl_ListObjLength(NULL, list1Ptr, &len);
7587 if (list1Ptr->bytes != NULL) {
7588 ckfree(list1Ptr->bytes);
7589 list1Ptr->bytes = NULL;
7590 }
7591
7592 list2Ptr = Tcl_NewStringObj("eeny meeny", -1);
7593 Tcl_ListObjLength(NULL, list2Ptr, &len);
7594 if (list2Ptr->bytes != NULL) {
7595 ckfree(list2Ptr->bytes);
7596 list2Ptr->bytes = NULL;
7597 }
7598
7599 /*
7600 * Verify that concat'ing a list obj with one or more empty strings does
7601 * return a fresh Tcl_Obj (see also [Bug 2055782]).
7602 */
7603
7604 tmpPtr = Tcl_DuplicateObj(list1Ptr);
7605
7606 objv[0] = tmpPtr;
7607 objv[1] = emptyPtr;
7608 concatPtr = Tcl_ConcatObj(2, objv);
7609 if (concatPtr->refCount != 0) {
7610 result = TCL_ERROR;
7611 Tcl_AppendResult(interp,
7612 "\n\t* (a) concatObj does not have refCount 0", NULL);
7613 }
7614 if (concatPtr == tmpPtr) {
7615 result = TCL_ERROR;
7616 Tcl_AppendResult(interp, "\n\t* (a) concatObj is not a new obj ",
7617 (char *)NULL);
7618 switch (tmpPtr->refCount) {
7619 case 0:
7620 Tcl_AppendResult(interp, "(no new refCount)", NULL);
7621 break;
7622 case 1:
7623 Tcl_AppendResult(interp, "(refCount added)", NULL);
7624 break;
7625 default:
7626 Tcl_AppendResult(interp, "(more than one refCount added!)", NULL);
7627 Tcl_Panic("extremely unsafe behaviour by Tcl_ConcatObj()");
7628 }
7629 tmpPtr = Tcl_DuplicateObj(list1Ptr);
7630 objv[0] = tmpPtr;
7631 }
7632 Tcl_DecrRefCount(concatPtr);
7633
7634 Tcl_IncrRefCount(tmpPtr);
7635 concatPtr = Tcl_ConcatObj(2, objv);
7636 if (concatPtr->refCount != 0) {
7637 result = TCL_ERROR;
7638 Tcl_AppendResult(interp,
7639 "\n\t* (b) concatObj does not have refCount 0", NULL);
7640 }
7641 if (concatPtr == tmpPtr) {
7642 result = TCL_ERROR;
7643 Tcl_AppendResult(interp, "\n\t* (b) concatObj is not a new obj ",
7644 (char *)NULL);
7645 switch (tmpPtr->refCount) {
7646 case 0:
7647 Tcl_AppendResult(interp, "(refCount removed?)", NULL);
7648 Tcl_Panic("extremely unsafe behaviour by Tcl_ConcatObj()");
7649 break;
7650 case 1:
7651 Tcl_AppendResult(interp, "(no new refCount)", NULL);
7652 break;
7653 case 2:
7654 Tcl_AppendResult(interp, "(refCount added)", NULL);
7655 Tcl_DecrRefCount(tmpPtr);
7656 break;
7657 default:
7658 Tcl_AppendResult(interp, "(more than one refCount added!)", NULL);
7659 Tcl_Panic("extremely unsafe behaviour by Tcl_ConcatObj()");
7660 }
7661 tmpPtr = Tcl_DuplicateObj(list1Ptr);
7662 objv[0] = tmpPtr;
7663 }
7664 Tcl_DecrRefCount(concatPtr);
7665
7666 objv[0] = emptyPtr;
7667 objv[1] = tmpPtr;
7668 objv[2] = emptyPtr;
7669 concatPtr = Tcl_ConcatObj(3, objv);
7670 if (concatPtr->refCount != 0) {
7671 result = TCL_ERROR;
7672 Tcl_AppendResult(interp,
7673 "\n\t* (c) concatObj does not have refCount 0", NULL);
7674 }
7675 if (concatPtr == tmpPtr) {
7676 result = TCL_ERROR;
7677 Tcl_AppendResult(interp, "\n\t* (c) concatObj is not a new obj ",
7678 NULL);
7679 switch (tmpPtr->refCount) {
7680 case 0:
7681 Tcl_AppendResult(interp, "(no new refCount)", NULL);
7682 break;
7683 case 1:
7684 Tcl_AppendResult(interp, "(refCount added)", NULL);
7685 break;
7686 default:
7687 Tcl_AppendResult(interp, "(more than one refCount added!)", NULL);
7688 Tcl_Panic("extremely unsafe behaviour by Tcl_ConcatObj()");
7689 }
7690 tmpPtr = Tcl_DuplicateObj(list1Ptr);
7691 objv[1] = tmpPtr;
7692 }
7693 Tcl_DecrRefCount(concatPtr);
7694
7695 Tcl_IncrRefCount(tmpPtr);
7696 concatPtr = Tcl_ConcatObj(3, objv);
7697 if (concatPtr->refCount != 0) {
7698 result = TCL_ERROR;
7699 Tcl_AppendResult(interp,
7700 "\n\t* (d) concatObj does not have refCount 0", NULL);
7701 }
7702 if (concatPtr == tmpPtr) {
7703 result = TCL_ERROR;
7704 Tcl_AppendResult(interp, "\n\t* (d) concatObj is not a new obj ",
7705 NULL);
7706 switch (tmpPtr->refCount) {
7707 case 0:
7708 Tcl_AppendResult(interp, "(refCount removed?)", NULL);
7709 Tcl_Panic("extremely unsafe behaviour by Tcl_ConcatObj()");
7710 break;
7711 case 1:
7712 Tcl_AppendResult(interp, "(no new refCount)", NULL);
7713 break;
7714 case 2:
7715 Tcl_AppendResult(interp, "(refCount added)", NULL);
7716 Tcl_DecrRefCount(tmpPtr);
7717 break;
7718 default:
7719 Tcl_AppendResult(interp, "(more than one refCount added!)", NULL);
7720 Tcl_Panic("extremely unsafe behaviour by Tcl_ConcatObj()");
7721 }
7722 tmpPtr = Tcl_DuplicateObj(list1Ptr);
7723 objv[1] = tmpPtr;
7724 }
7725 Tcl_DecrRefCount(concatPtr);
7726
7727 /*
7728 * Verify that an unshared list is not corrupted when concat'ing things to
7729 * it.
7730 */
7731
7732 objv[0] = tmpPtr;
7733 objv[1] = list2Ptr;
7734 concatPtr = Tcl_ConcatObj(2, objv);
7735 if (concatPtr->refCount != 0) {
7736 result = TCL_ERROR;
7737 Tcl_AppendResult(interp,
7738 "\n\t* (e) concatObj does not have refCount 0", NULL);
7739 }
7740 if (concatPtr == tmpPtr) {
7741 result = TCL_ERROR;
7742 Tcl_AppendResult(interp, "\n\t* (e) concatObj is not a new obj ",
7743 NULL);
7744
7745 (void) Tcl_ListObjLength(NULL, concatPtr, &len);
7746 switch (tmpPtr->refCount) {
7747 case 3:
7748 Tcl_AppendResult(interp, "(failed to concat)", NULL);
7749 break;
7750 default:
7751 Tcl_AppendResult(interp, "(corrupted input!)", NULL);
7752 }
7753 if (Tcl_IsShared(tmpPtr)) {
7754 Tcl_DecrRefCount(tmpPtr);
7755 }
7756 tmpPtr = Tcl_DuplicateObj(list1Ptr);
7757 objv[0] = tmpPtr;
7758 }
7759 Tcl_DecrRefCount(concatPtr);
7760
7761 objv[0] = tmpPtr;
7762 objv[1] = list2Ptr;
7763 Tcl_IncrRefCount(tmpPtr);
7764 concatPtr = Tcl_ConcatObj(2, objv);
7765 if (concatPtr->refCount != 0) {
7766 result = TCL_ERROR;
7767 Tcl_AppendResult(interp,
7768 "\n\t* (f) concatObj does not have refCount 0", NULL);
7769 }
7770 if (concatPtr == tmpPtr) {
7771 result = TCL_ERROR;
7772 Tcl_AppendResult(interp, "\n\t* (f) concatObj is not a new obj ",
7773 (char *)NULL);
7774
7775 (void) Tcl_ListObjLength(NULL, concatPtr, &len);
7776 switch (tmpPtr->refCount) {
7777 case 3:
7778 Tcl_AppendResult(interp, "(failed to concat)", NULL);
7779 break;
7780 default:
7781 Tcl_AppendResult(interp, "(corrupted input!)", NULL);
7782 }
7783 if (Tcl_IsShared(tmpPtr)) {
7784 Tcl_DecrRefCount(tmpPtr);
7785 }
7786 tmpPtr = Tcl_DuplicateObj(list1Ptr);
7787 objv[0] = tmpPtr;
7788 }
7789 Tcl_DecrRefCount(concatPtr);
7790
7791 objv[0] = tmpPtr;
7792 objv[1] = list2Ptr;
7793 Tcl_IncrRefCount(tmpPtr);
7794 Tcl_IncrRefCount(tmpPtr);
7795 concatPtr = Tcl_ConcatObj(2, objv);
7796 if (concatPtr->refCount != 0) {
7797 result = TCL_ERROR;
7798 Tcl_AppendResult(interp,
7799 "\n\t* (g) concatObj does not have refCount 0", NULL);
7800 }
7801 if (concatPtr == tmpPtr) {
7802 result = TCL_ERROR;
7803 Tcl_AppendResult(interp, "\n\t* (g) concatObj is not a new obj ",
7804 (char *)NULL);
7805
7806 (void) Tcl_ListObjLength(NULL, concatPtr, &len);
7807 switch (tmpPtr->refCount) {
7808 case 3:
7809 Tcl_AppendResult(interp, "(failed to concat)", NULL);
7810 break;
7811 default:
7812 Tcl_AppendResult(interp, "(corrupted input!)", NULL);
7813 }
7814 Tcl_DecrRefCount(tmpPtr);
7815 if (Tcl_IsShared(tmpPtr)) {
7816 Tcl_DecrRefCount(tmpPtr);
7817 }
7818 tmpPtr = Tcl_DuplicateObj(list1Ptr);
7819 objv[0] = tmpPtr;
7820 }
7821 Tcl_DecrRefCount(concatPtr);
7822
7823 /*
7824 * Clean everything up. Note that we don't actually know how many
7825 * references there are to tmpPtr here; in the no-error case, it should be
7826 * five... [Bug 2895367]
7827 */
7828
7829 Tcl_DecrRefCount(list1Ptr);
7830 Tcl_DecrRefCount(list2Ptr);
7831 Tcl_DecrRefCount(emptyPtr);
7832 while (tmpPtr->refCount > 1) {
7833 Tcl_DecrRefCount(tmpPtr);
7834 }
7835 Tcl_DecrRefCount(tmpPtr);
7836
7837 if (result == TCL_OK) {
7838 Tcl_ResetResult(interp);
7839 }
7840 return result;
7841 }
7842
7843 /*
7844 *----------------------------------------------------------------------
7845 *
7846 * TestparseargsCmd --
7847 *
7848 * This procedure implements the "testparseargs" command. It is used to
7849 * test that Tcl_ParseArgsObjv does indeed return the right number of
7850 * arguments. In other words, that [Bug 3413857] was fixed properly.
7851 * Also test for bug [7cb7409e05]
7852 *
7853 * Results:
7854 * A standard Tcl result.
7855 *
7856 * Side effects:
7857 * None.
7858 *
7859 *----------------------------------------------------------------------
7860 */
7861
7862 static int
7863 ParseMedia(
7864 void *clientData,
7865 Tcl_Interp *interp,
7866 int objc,
7867 Tcl_Obj *const *objv,
7868 void *dstPtr)
7869 {
7870 static const char *const mediaOpts[] = {"A4", "Legal", "Letter", NULL};
7871 static const char *const ExtendedMediaOpts[] = {
7872 "Paper size is ISO A4", "Paper size is US Legal",
7873 "Paper size is US Letter", NULL};
7874 int index;
7875 const char **media = (const char **) dstPtr;
7876
7877 if (Tcl_GetIndexFromObjStruct(interp, objv[0], mediaOpts,
7878 sizeof(char *), "media", 0, &index) != TCL_OK) {
7879 return -1;
7880 }
7881
7882 *media = ExtendedMediaOpts[index];
7883 return 1;
7884 }
7885
7886 static int
7887 TestparseargsCmd(
7888 ClientData dummy, /* Not used. */
7889 Tcl_Interp *interp, /* Current interpreter. */
7890 int objc, /* Number of arguments. */
7891 Tcl_Obj *const objv[]) /* Arguments. */
7892 {
7893 static int foo = 0;
7894 const char *media = NULL, *color = NULL;
7895 int count = objc;
7896 Tcl_Obj **remObjv, *result[5];
7897 const Tcl_ArgvInfo argTable[] = {
7898 {TCL_ARGV_CONSTANT, "-bool", INT2PTR(1), &foo, "booltest", NULL},
7899 {TCL_ARGV_STRING, "-colormode" , NULL, &color, "color mode", NULL},
7900 {TCL_ARGV_GENFUNC, "-media", ParseMedia, &media, "media page size", NULL},
7901 TCL_ARGV_AUTO_REST, TCL_ARGV_AUTO_HELP, TCL_ARGV_TABLE_END
7902 };
7903
7904 foo = 0;
7905 if (Tcl_ParseArgsObjv(interp, argTable, &count, objv, &remObjv)!=TCL_OK) {
7906 return TCL_ERROR;
7907 }
7908 result[0] = Tcl_NewIntObj(foo);
7909 result[1] = Tcl_NewIntObj(count);
7910 result[2] = Tcl_NewListObj(count, remObjv);
7911 result[3] = Tcl_NewStringObj(color ? color : "NULL", -1);
7912 result[4] = Tcl_NewStringObj(media ? media : "NULL", -1);
7913 Tcl_SetObjResult(interp, Tcl_NewListObj(5, result));
7914 ckfree(remObjv);
7915 return TCL_OK;
7916 }
7917
7918 /**
7919 * Test harness for command and variable resolvers.
7920 */
7921
7922 static int
7923 InterpCmdResolver(
7924 Tcl_Interp *interp,
7925 const char *name,
7926 Tcl_Namespace *dummy,
7927 int flags,
7928 Tcl_Command *rPtr)
7929 {
7930 Interp *iPtr = (Interp *) interp;
7931 CallFrame *varFramePtr = iPtr->varFramePtr;
7932 Proc *procPtr = (varFramePtr->isProcCallFrame & FRAME_IS_PROC) ?
7933 varFramePtr->procPtr : NULL;
7934 Namespace *callerNsPtr = varFramePtr->nsPtr;
7935 Tcl_Command resolvedCmdPtr = NULL;
7936 (void)dummy;
7937 (void)flags;
7938
7939 /*
7940 * Just do something special on a cmd literal "z" in two cases:
7941 * A) when the caller is a proc "x", and the proc is either in "::" or in "::ns2".
7942 * B) the caller's namespace is "ctx1" or "ctx2"
7943 */
7944 if ( (name[0] == 'z') && (name[1] == '\0') ) {
7945 Namespace *ns2NsPtr = (Namespace *) Tcl_FindNamespace(interp, "::ns2", NULL, 0);
7946
7947 if (procPtr != NULL
7948 && ((procPtr->cmdPtr->nsPtr == iPtr->globalNsPtr)
7949 || (ns2NsPtr != NULL && procPtr->cmdPtr->nsPtr == ns2NsPtr)
7950 )
7951 ) {
7952 /*
7953 * Case A)
7954 *
7955 * - The context, in which this resolver becomes active, is
7956 * determined by the name of the caller proc, which has to be
7957 * named "x".
7958 *
7959 * - To determine the name of the caller proc, the proc is taken
7960 * from the topmost stack frame.
7961 *
7962 * - Note that the context is NOT provided during byte-code
7963 * compilation (e.g. in TclProcCompileProc)
7964 *
7965 * When these conditions hold, this function resolves the
7966 * passed-in cmd literal into a cmd "y", which is taken from the
7967 * the global namespace (for simplicity).
7968 */
7969
7970 const char *callingCmdName =
7971 Tcl_GetCommandName(interp, (Tcl_Command) procPtr->cmdPtr);
7972
7973 if ( callingCmdName[0] == 'x' && callingCmdName[1] == '\0' ) {
7974 resolvedCmdPtr = Tcl_FindCommand(interp, "y", NULL, TCL_GLOBAL_ONLY);
7975 }
7976 } else if (callerNsPtr != NULL) {
7977 /*
7978 * Case B)
7979 *
7980 * - The context, in which this resolver becomes active, is
7981 * determined by the name of the parent namespace, which has
7982 * to be named "ctx1" or "ctx2".
7983 *
7984 * - To determine the name of the parent namesace, it is taken
7985 * from the 2nd highest stack frame.
7986 *
7987 * - Note that the context can be provided during byte-code
7988 * compilation (e.g. in TclProcCompileProc)
7989 *
7990 * When these conditions hold, this function resolves the
7991 * passed-in cmd literal into a cmd "y" or "Y" depending on the
7992 * context. The resolved procs are taken from the the global
7993 * namespace (for simplicity).
7994 */
7995
7996 CallFrame *parentFramePtr = varFramePtr->callerPtr;
7997 const char *context = parentFramePtr != NULL ? parentFramePtr->nsPtr->name : "(NULL)";
7998
7999 if (strcmp(context, "ctx1") == 0 && (name[0] == 'z') && (name[1] == '\0')) {
8000 resolvedCmdPtr = Tcl_FindCommand(interp, "y", NULL, TCL_GLOBAL_ONLY);
8001 /* fprintf(stderr, "... y ==> %p\n", resolvedCmdPtr);*/
8002
8003 } else if (strcmp(context, "ctx2") == 0 && (name[0] == 'z') && (name[1] == '\0')) {
8004 resolvedCmdPtr = Tcl_FindCommand(interp, "Y", NULL, TCL_GLOBAL_ONLY);
8005 /*fprintf(stderr, "... Y ==> %p\n", resolvedCmdPtr);*/
8006 }
8007 }
8008
8009 if (resolvedCmdPtr != NULL) {
8010 *rPtr = resolvedCmdPtr;
8011 return TCL_OK;
8012 }
8013 }
8014 return TCL_CONTINUE;
8015 }
8016
8017 static int
8018 InterpVarResolver(
8019 Tcl_Interp *interp,
8020 const char *name,
8021 Tcl_Namespace *context,
8022 int flags,
8023 Tcl_Var *rPtr)
8024 {
8025 /*
8026 * Don't resolve the variable; use standard rules.
8027 */
8028
8029 return TCL_CONTINUE;
8030 }
8031
8032 typedef struct MyResolvedVarInfo {
8033 Tcl_ResolvedVarInfo vInfo; /* This must be the first element. */
8034 Tcl_Var var;
8035 Tcl_Obj *nameObj;
8036 } MyResolvedVarInfo;
8037
8038 static inline void
8039 HashVarFree(
8040 Tcl_Var var)
8041 {
8042 if (VarHashRefCount(var) < 2) {
8043 ckfree(var);
8044 } else {
8045 VarHashRefCount(var)--;
8046 }
8047 }
8048
8049 static void
8050 MyCompiledVarFree(
8051 Tcl_ResolvedVarInfo *vInfoPtr)
8052 {
8053 MyResolvedVarInfo *resVarInfo = (MyResolvedVarInfo *) vInfoPtr;
8054
8055 Tcl_DecrRefCount(resVarInfo->nameObj);
8056 if (resVarInfo->var) {
8057 HashVarFree(resVarInfo->var);
8058 }
8059 ckfree(vInfoPtr);
8060 }
8061
8062 #define TclVarHashGetValue(hPtr) \
8063 ((Var *) ((char *)hPtr - TclOffset(VarInHash, entry)))
8064
8065 static Tcl_Var
8066 MyCompiledVarFetch(
8067 Tcl_Interp *interp,
8068 Tcl_ResolvedVarInfo *vinfoPtr)
8069 {
8070 MyResolvedVarInfo *resVarInfo = (MyResolvedVarInfo *) vinfoPtr;
8071 Tcl_Var var = resVarInfo->var;
8072 int isNewVar;
8073 Interp *iPtr = (Interp *) interp;
8074 Tcl_HashEntry *hPtr;
8075
8076 if (var != NULL) {
8077 if (!(((Var *) var)->flags & VAR_DEAD_HASH)) {
8078 /*
8079 * The cached variable is valid, return it.
8080 */
8081
8082 return var;
8083 }
8084
8085 /*
8086 * The variable is not valid anymore. Clean it up.
8087 */
8088
8089 HashVarFree(var);
8090 }
8091
8092 hPtr = Tcl_CreateHashEntry((Tcl_HashTable *) &iPtr->globalNsPtr->varTable,
8093 (char *) resVarInfo->nameObj, &isNewVar);
8094 if (hPtr) {
8095 var = (Tcl_Var) TclVarHashGetValue(hPtr);
8096 } else {
8097 var = NULL;
8098 }
8099 resVarInfo->var = var;
8100
8101 /*
8102 * Increment the reference counter to avoid ckfree() of the variable in
8103 * Tcl's FreeVarEntry(); for cleanup, we provide our own HashVarFree();
8104 */
8105
8106 VarHashRefCount(var)++;
8107 return var;
8108 }
8109
8110 static int
8111 InterpCompiledVarResolver(
8112 Tcl_Interp *interp,
8113 const char *name,
8114 int length,
8115 Tcl_Namespace *context,
8116 Tcl_ResolvedVarInfo **rPtr)
8117 {
8118 if (*name == 'T') {
8119 MyResolvedVarInfo *resVarInfo = (MyResolvedVarInfo *)ckalloc(sizeof(MyResolvedVarInfo));
8120
8121 resVarInfo->vInfo.fetchProc = MyCompiledVarFetch;
8122 resVarInfo->vInfo.deleteProc = MyCompiledVarFree;
8123 resVarInfo->var = NULL;
8124 resVarInfo->nameObj = Tcl_NewStringObj(name, -1);
8125 Tcl_IncrRefCount(resVarInfo->nameObj);
8126 *rPtr = &resVarInfo->vInfo;
8127 return TCL_OK;
8128 }
8129 return TCL_CONTINUE;
8130 }
8131
8132 static int
8133 TestInterpResolverCmd(
8134 ClientData clientData,
8135 Tcl_Interp *interp,
8136 int objc,
8137 Tcl_Obj *const objv[])
8138 {
8139 static const char *const table[] = {
8140 "down", "up", NULL
8141 };
8142 int idx;
8143 #define RESOLVER_KEY "testInterpResolver"
8144
8145 if ((objc < 2) || (objc > 3)) {
8146 Tcl_WrongNumArgs(interp, 1, objv, "up|down ?interp?");
8147 return TCL_ERROR;
8148 }
8149 if (objc == 3) {
8150 interp = Tcl_GetChild(interp, Tcl_GetString(objv[2]));
8151 if (interp == NULL) {
8152 Tcl_AppendResult(interp, "provided interpreter not found", NULL);
8153 return TCL_ERROR;
8154 }
8155 }
8156 if (Tcl_GetIndexFromObj(interp, objv[1], table, "operation", TCL_EXACT,
8157 &idx) != TCL_OK) {
8158 return TCL_ERROR;
8159 }
8160 switch (idx) {
8161 case 1: /* up */
8162 Tcl_AddInterpResolvers(interp, RESOLVER_KEY, InterpCmdResolver,
8163 InterpVarResolver, InterpCompiledVarResolver);
8164 break;
8165 case 0: /*down*/
8166 if (!Tcl_RemoveInterpResolvers(interp, RESOLVER_KEY)) {
8167 Tcl_AppendResult(interp, "could not remove the resolver scheme",
8168 (char *)NULL);
8169 return TCL_ERROR;
8170 }
8171 }
8172 return TCL_OK;
8173 }
8174
8175 /*
8176 *------------------------------------------------------------------------
8177 *
8178 * TestApplyLambdaObjCmd --
8179 *
8180 * Implements the Tcl command testapplylambda. This tests the apply
8181 * implementation handling of a lambda where the lambda has a list
8182 * internal representation where the second element's internal
8183 * representation is already a byte code object.
8184 *
8185 * Results:
8186 * TCL_OK - Success. Caller should check result is 42
8187 * TCL_ERROR - Error.
8188 *
8189 * Side effects:
8190 * In the presence of the apply bug, may panic. Otherwise
8191 * Interpreter result holds result or error message.
8192 *
8193 *------------------------------------------------------------------------
8194 */
8195 int TestApplyLambdaObjCmd (
8196 ClientData notUsed,
8197 Tcl_Interp *interp, /* Current interpreter. */
8198 int objc, /* Number of arguments. */
8199 Tcl_Obj *const objv[]) /* Argument objects. */
8200 {
8201 Tcl_Obj *lambdaObjs[2];
8202 Tcl_Obj *evalObjs[2];
8203 Tcl_Obj *lambdaObj;
8204 int result;
8205
8206 /* Create a lambda {{} {set a 42}} */
8207 lambdaObjs[0] = Tcl_NewObj(); /* No parameters */
8208 lambdaObjs[1] = Tcl_NewStringObj("set a 42", -1); /* Body */
8209 lambdaObj = Tcl_NewListObj(2, lambdaObjs);
8210 Tcl_IncrRefCount(lambdaObj);
8211
8212 /* Create the command "apply {{} {set a 42}" */
8213 evalObjs[0] = Tcl_NewStringObj("apply", -1);
8214 Tcl_IncrRefCount(evalObjs[0]);
8215 /*
8216 * NOTE: IMPORTANT TO EXHIBIT THE BUG. We duplicate the lambda because
8217 * it will get shimmered to a Lambda internal representation but we
8218 * want to hold on to our list representation.
8219 */
8220 evalObjs[1] = Tcl_DuplicateObj(lambdaObj);
8221 Tcl_IncrRefCount(evalObjs[1]);
8222
8223 /* Evaluate it */
8224 result = Tcl_EvalObjv(interp, 2, evalObjs, TCL_EVAL_GLOBAL);
8225 if (result != TCL_OK) {
8226 Tcl_DecrRefCount(evalObjs[0]);
8227 Tcl_DecrRefCount(evalObjs[1]);
8228 return result;
8229 }
8230 /*
8231 * So far so good. At this point,
8232 * - evalObjs[1] has an internal representation of Lambda
8233 * - lambdaObj[1] ({set a 42}) has been shimmered to
8234 * an internal representation of ByteCode.
8235 */
8236 Tcl_DecrRefCount(evalObjs[1]); /* Don't need this anymore */
8237 /*
8238 * The bug trigger. Repeating the command but:
8239 * - we are calling apply with a lambda that is a list (as BEFORE),
8240 * BUT
8241 * - The body of the lambda (lambdaObjs[1]) ALREADY has internal
8242 * representation of ByteCode and thus will not be compiled again
8243 */
8244 evalObjs[1] = lambdaObj; /* lambdaObj already has a ref count so
8245 no need for IncrRef */
8246 result = Tcl_EvalObjv(interp, 2, evalObjs, TCL_EVAL_GLOBAL);
8247 Tcl_DecrRefCount(evalObjs[0]);
8248 Tcl_DecrRefCount(lambdaObj);
8249
8250 return result;
8251 }
8252
8253 /*
8254 * Local Variables:
8255 * mode: c
8256 * c-basic-offset: 4
8257 * fill-column: 78
8258 * tab-width: 8
8259 * indent-tabs-mode: nil
8260 * End:
8261 */
8262