Tcl.xs

filter: Statementstmt 6.5% 34 / 523 Branchbran 4.2% 15 / 354 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 5.5% 49 / 877 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Tcl.pm ↑ Summary
1 /*
2 * Tcl.xs --
3 *
4 * This file contains XS code for the Perl's Tcl bridge module.
5 *
6 * Copyright (c) 1994-1997, Malcolm Beattie
7 * Copyright (c) 2003-2018, Vadim Konovalov
8 * Copyright (c) 2004 ActiveState Corp., a division of Sophos PLC
9 *
10 */
11  
12 #define PERL_NO_GET_CONTEXT /* we want efficiency */
13 #include "EXTERN.h"
14 #include "perl.h"
15 #include "XSUB.h"
16  
17 #ifndef DEBUG_REFCOUNTS
18 #define DEBUG_REFCOUNTS 0
19 #endif
20  
21 /*
22 * Until we update for 8.4 CONST-ness
23 */
24 #define USE_NON_CONST
25  
26 /*
27 * Both Perl and Tcl use these macros
28 */
29 #undef STRINGIFY
30 #undef JOIN
31  
32 #include <tcl.h>
33  
34 #ifdef USE_TCL_STUBS
35  
36 /* Let Tcl_Init() stub could be pointed by a function pointer.
37 * Since tcl8.5, Tcl_Init() is defined as a macro with an argument and
38 * unable to be assigned to a function pointer. We need to redefine it back
39 * to tcl8.4 style .
40 */
41 #ifdef Tcl_Init
42 # undef Tcl_Init
43 # define Tcl_Init (tclStubsPtr->tcl_Init)
44 #endif
45  
46 /*
47 * If we use the Tcl stubs mechanism, this provides us Tcl version
48 * and direct dll independence, but we must force the loading of
49 * the dll ourselves based on a set of heuristics in NpLoadLibrary.
50 */
51  
52 #ifndef TCL_LIB_FILE
53 # ifdef WIN32
54 # define TCL_LIB_FILE "tcl84.dll"
55 # elif defined(__APPLE__)
56 # define TCL_LIB_FILE "Tcl"
57 # elif defined(__hpux)
58 # define TCL_LIB_FILE "libtcl8.4.sl"
59 # else
60 # define TCL_LIB_FILE "libtcl8.4.so"
61 # endif
62 #endif
63  
64 /*
65 * Default directory in which to look for Tcl/Tk libraries. The
66 * symbol is defined by Makefile.
67 */
68  
69 #ifndef LIB_RUNTIME_DIR
70 # define LIB_RUNTIME_DIR "."
71 #endif
72 static char defaultLibraryDir[sizeof(LIB_RUNTIME_DIR)+200] = LIB_RUNTIME_DIR;
73  
74 #if defined(WIN32)
75  
76 #ifndef HMODULE
77 #define HMODULE void *
78 #endif
79 #define WIN32_LEAN_AND_MEAN
80 #include <windows.h>
81 #undef WIN32_LEAN_AND_MEAN
82 #define dlopen(libname, flags) LoadLibrary(libname)
83 #define dlclose(path) ((void *) FreeLibrary((HMODULE) path))
84 #define DLSYM(handle, symbol, type, proc) \
85 (proc = (type) GetProcAddress((HINSTANCE) handle, symbol))
86 #define snprintf _snprintf
87  
88 #elif defined(__APPLE__)
89  
90 #include <CoreServices/CoreServices.h>
91  
92 static short DOMAINS[] = {
93 kUserDomain,
94 kLocalDomain,
95 kNetworkDomain,
96 kSystemDomain
97 };
98 static const int DOMAINS_LEN = sizeof(DOMAINS)/sizeof(DOMAINS[0]);
99  
100 #elif defined(__hpux)
101 /* HPUX requires shl_* routines */
102 #include <dl.h>
103 #define HMODULE shl_t
104 #define dlopen(libname, flags) shl_load(libname, \
105 BIND_DEFERRED|BIND_VERBOSE|DYNAMIC_PATH, 0L)
106 #define dlclose(path) shl_unload((shl_t) path)
107 #define DLSYM(handle, symbol, type, proc) \
108 if (shl_findsym(&handle, symbol, (short) TYPE_PROCEDURE, \
109 (void *) &proc) != 0) { proc = NULL; }
110 #endif
111  
112 #ifndef HMODULE
113 #include <dlfcn.h>
114 #define HMODULE void *
115 #define DLSYM(handle, symbol, type, proc) \
116 (proc = (type) dlsym(handle, symbol))
117 #endif
118  
119 #ifndef MAX_PATH
120 #define MAX_PATH 1024
121 #endif
122  
123 /*
124 * Tcl library handle
125 */
126 static HMODULE tclHandle = NULL;
127  
128 static Tcl_Interp *g_Interp = NULL;
129 static int (* tclKit_AppInit)(Tcl_Interp *) = NULL;
130  
131 #else
132  
133 /*
134 * !USE_TCL_STUBS
135 */
136  
137 static int (* tclKit_AppInit)(Tcl_Interp *) = Tcl_Init;
138  
139 #if defined(HAVE_TKINIT) && defined(WIN32)
140 HANDLE _hinst = 0;
141 BOOL APIENTRY
142 DllMain(HINSTANCE hInst, DWORD reason, LPVOID reserved) {
143 _hinst = hInst;
144 return TRUE;
145 }
146 #endif
147  
148 #endif
149  
150 typedef Tcl_Interp *Tcl;
151 typedef AV *Tcl__Var;
152  
153 #ifdef HAVE_TKINIT
154 EXTERN char * TclSetPreInitScript (char * string);
155 void TclpInitLibraryPath(char **valuePtr, int *lengthPtr, Tcl_Encoding *encodingPtr);
156 EXTERN void TkWinSetHINSTANCE (HINSTANCE hInstance);
157 #endif
158  
159 #ifdef HAVE_BLTINIT
160 extern Tcl_PackageInitProc Blt_Init, Blt_SafeInit;
161 #endif
162  
163 /*
164 * Variables denoting the Tcl object types defined in the core.
165 * These may not exist - guard against NULL result.
166 */
167  
168 static Tcl_ObjType *tclBooleanTypePtr = NULL;
169 static Tcl_ObjType *tclByteArrayTypePtr = NULL;
170 static Tcl_ObjType *tclDoubleTypePtr = NULL;
171 static Tcl_ObjType *tclIntTypePtr = NULL;
172 static Tcl_ObjType *tclListTypePtr = NULL;
173 static Tcl_ObjType *tclStringTypePtr = NULL;
174 static Tcl_ObjType *tclWideIntTypePtr = NULL;
175  
176 /*
177 * This tells us whether Tcl is in a "callable" state. Set to 1 in BOOT
178 * and 0 in Tcl__Finalize (END). Once finalized, we should not make any
179 * more calls to Tcl_* APIs.
180 * hvInterps is a hash that records all live interps, so that we can
181 * force their deletion before the finalization.
182 */
183 static int initialized = 0;
184 static HV *hvInterps = NULL;
185  
186 /*
187 * FUNCTIONS
188 */
189  
190 #ifdef USE_TCL_STUBS
191 /*
192 *----------------------------------------------------------------------
193 *
194 * NpLoadLibrary --
195 *
196 *
197 * Results:
198 * Stores the handle of the library found in tclHandle and the
199 * name it successfully loaded from in dllFilename (if dllFilenameSize
200 is != 0).
201 *
202 * Side effects:
203 * Loads the library - user needs to dlclose it..
204 *
205 *----------------------------------------------------------------------
206 */
207  
208 static int
209 20 NpLoadLibrary(pTHX_ HMODULE *tclHandle, char *dllFilename, int dllFilenameSize)
210 {
211 char *dl_path, libname[MAX_PATH];
212 20 HMODULE handle = (HMODULE) NULL;
213  
214 /*
215 * Try a user-supplied Tcl dll to start with.
216 * If the var is supplied, force this to be correct or error out.
217 */
218 20 dl_path = SvPV_nolen(get_sv("Tcl::DL_PATH", TRUE));
219 20 ▶ if (dl_path && *dl_path) {
Branch: 2 branches
BranchTrueFalseTotal
if (dl_path && *dl_path) { 20 0 20
if (dl_path && *dl_path) { 0 20 20
220 0 handle = dlopen(dl_path, RTLD_NOW | RTLD_GLOBAL);
221 0 ▶ if (handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (handle) { 0 0 0
222 0 memcpy(libname, dl_path, MAX_PATH);
223 } else {
224 #if !defined(WIN32) && !defined(__hpux)
225 0 char *error = dlerror();
226 0 ▶ if (error != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (error != NULL) { 0 0 0
227 0 warn("%s",error);
228 }
229 #endif
230 0 warn("NpLoadLibrary: could not find Tcl library at '%s'", dl_path);
231 0 return TCL_ERROR;
232 }
233 }
234  
235 #ifdef __APPLE__
236 if (!handle) {
237 OSErr oserr;
238 FSRef ref;
239 int i;
240  
241 for (i = 0; i < DOMAINS_LEN; i++) {
242 oserr = FSFindFolder(DOMAINS[i], kFrameworksFolderType,
243 kDontCreateFolder, &ref);
244 if (oserr != noErr) {
245 continue;
246 }
247 oserr = FSRefMakePath(&ref, (UInt8*)libname, sizeof(libname));
248 if (oserr != noErr) {
249 continue;
250 }
251 /*
252 * This should really just try loading Tcl.framework/Tcl, but will
253 * fail if the user has requested an alternate TCL_LIB_FILE.
254 */
255 strcat(libname, "/Tcl.framework/" TCL_LIB_FILE);
256 /* printf("Try \"%s\"\n", libname); */
257 handle = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
258 if (handle) {
259 break;
260 }
261 }
262 }
263 #endif
264  
265 20 ▶ if (!handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (!handle) { 20 0 20
266 if (strlen(TCL_LIB_FILE) < 3) {
267 warn("Invalid base Tcl library filename provided: '%s'", TCL_LIB_FILE);
268 return TCL_ERROR;
269 }
270  
271 /* Try based on full path. */
272 20 snprintf(libname, MAX_PATH-1, "%s/%s", defaultLibraryDir, TCL_LIB_FILE);
273 20 handle = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
274 20 ▶ if (!handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (!handle) { 20 0 20
275 /* Try based on anywhere in the path. */
276 20 strcpy(libname, TCL_LIB_FILE);
277 20 handle = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
278 }
279 20 ▶ if (!handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (!handle) { 20 0 20
280 /* Try different versions anywhere in the path. */
281 20 warn("failed dlopen(%s,...);", libname);
282 20 char *pos = strstr(libname, "tcl8")+4;
283 20 ▶ if (*pos == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (*pos == '.') { 20 0 20
284 20 pos++;
285 }
286 20 *pos = '6'; /* count down from '9' to '0': 8.9, 8.8, 8.7, 8.6, ... */
287 do {
288 140 warn("trying dlopen(%s,...);", libname);
289 140 handle = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
290 140 ▶ } while (!handle && (--*pos >= '0'));
Branch: 2 branches
BranchTrueFalseTotal
} while (!handle && (--*pos >= '0')); 140 0 140
} while (!handle && (--*pos >= '0')); 120 20 140
291 20 ▶ if (!handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (!handle) { 20 0 20
292 20 warn("failed all posible tcl vers 8.x from 9 down to 0");
293 20 return TCL_ERROR;
294 }
295 }
296 }
297  
298 #ifdef WIN32
299 if (!handle) {
300 char path[MAX_PATH], vers[MAX_PATH];
301 DWORD result, size = MAX_PATH;
302 HKEY regKey;
303 #define TCL_REG_DIR_KEY "Software\\ActiveState\\ActiveTcl"
304  
305 result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TCL_REG_DIR_KEY, 0,
306 KEY_READ, &regKey);
307 if (result != ERROR_SUCCESS) {
308 warn("Could not access registry \"HKLM\\%s\"\n", TCL_REG_DIR_KEY);
309  
310 result = RegOpenKeyEx(HKEY_CURRENT_USER, TCL_REG_DIR_KEY, 0,
311 KEY_READ, &regKey);
312 if (result != ERROR_SUCCESS) {
313 warn("Could not access registry \"HKCU\\%s\"\n",
314 TCL_REG_DIR_KEY);
315 return TCL_ERROR;
316 }
317 }
318  
319 result = RegQueryValueEx(regKey, "CurrentVersion", NULL, NULL,
320 vers, &size);
321 RegCloseKey(regKey);
322 if (result != ERROR_SUCCESS) {
323 warn("Could not access registry \"%s\" CurrentVersion\n",
324 TCL_REG_DIR_KEY);
325 return TCL_ERROR;
326 }
327  
328 snprintf(path, MAX_PATH-1, "%s\\%s", TCL_REG_DIR_KEY, vers);
329  
330 result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &regKey);
331 if (result != ERROR_SUCCESS) {
332 warn("Could not access registry \"%s\"\n", path);
333 return TCL_ERROR;
334 }
335  
336 size = MAX_PATH;
337 result = RegQueryValueEx(regKey, NULL, NULL, NULL, path, &size);
338 RegCloseKey(regKey);
339 if (result != ERROR_SUCCESS) {
340 warn("Could not access registry \"%s\" Default\n", TCL_REG_DIR_KEY);
341 return TCL_ERROR;
342 }
343  
344 warn("Found current Tcl installation at \"%s\"\n", path);
345  
346 snprintf(libname, MAX_PATH-1, "%s\\bin\\%s", path, TCL_LIB_FILE);
347 handle = dlopen(libname, RTLD_NOW | RTLD_GLOBAL);
348 }
349 #endif
350  
351 0 ▶ if (!handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (!handle) { 0 0 0
352 0 warn("NpLoadLibrary: could not find Tcl dll\n");
353 0 return TCL_ERROR;
354 }
355 0 *tclHandle = handle;
356 0 ▶ if (dllFilenameSize > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (dllFilenameSize > 0) { 0 0 0
357 0 memcpy(dllFilename, libname, dllFilenameSize);
358 }
359 0 return TCL_OK;
360 }
361  
362 /*
363 *----------------------------------------------------------------------
364 *
365 * NpInitialize --
366 *
367 * Create the main interpreter.
368 *
369 * Results:
370 * TCL_OK or TCL_ERROR - whether succeeded or not
371 *
372 * Side effects:
373 * Will panic if called twice. (Must call DestroyMainInterp in between)
374 *
375 *----------------------------------------------------------------------
376 */
377  
378 static int
379 20 NpInitialize(pTHX_ SV *X)
380 {
381 static Tcl_Interp * (* createInterp)() = NULL;
382 static void (* findExecutable)(char *) = NULL;
383 /*
384 * We want the Tcl_InitStubs func static to ourselves - before Tcl
385 * is loaded dyanmically and possibly changes it.
386 * Variable initstubs have to be declared as volatile to prevent
387 * compiler optimizing it out.
388 */
389 static CONST char *(*volatile initstubs)(Tcl_Interp *, CONST char *, int)
390 = Tcl_InitStubs;
391 char dllFilename[MAX_PATH];
392 20 dllFilename[0] = '\0';
393  
394 #ifdef USE_TCL_STUBS
395 /*
396 * Determine the libname and version number dynamically
397 */
398 20 ▶ if (tclHandle == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tclHandle == NULL) { 20 0 20
399 /*
400 * First see if some other part didn't already load Tcl.
401 */
402 20 DLSYM(tclHandle, "Tcl_CreateInterp", Tcl_Interp * (*)(), createInterp);
403  
404 20 ▶ if (createInterp == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (createInterp == NULL) { 20 0 20
405 20 ▶ if (NpLoadLibrary(aTHX_ &tclHandle, dllFilename, MAX_PATH)
Branch: 1 branch
BranchTrueFalseTotal
if (NpLoadLibrary(aTHX_ &tclHandle, dllFilename, MAX_PATH) 20 0 20
406 != TCL_OK) {
407 20 warn("Failed to load Tcl dll!");
408 20 return TCL_ERROR;
409 }
410 }
411  
412 0 DLSYM(tclHandle, "Tcl_CreateInterp", Tcl_Interp * (*)(), createInterp);
413 0 ▶ if (createInterp == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (createInterp == NULL) { 0 0 0
414 #if !defined(WIN32) && !defined(__hpux)
415 0 char *error = dlerror();
416 0 ▶ if (error != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (error != NULL) { 0 0 0
417 0 warn("%s",error);
418 }
419 #endif
420 0 return TCL_ERROR;
421 }
422 0 DLSYM(tclHandle, "Tcl_FindExecutable", void (*)(char *),
423 findExecutable);
424  
425 0 DLSYM(tclHandle, "TclKit_AppInit", int (*)(Tcl_Interp *),
426 tclKit_AppInit);
427 }
428 #else
429 createInterp = Tcl_CreateInterp;
430 findExecutable = Tcl_FindExecutable;
431 #endif
432  
433 #ifdef WIN32
434 if (dllFilename[0] == '\0') {
435 GetModuleFileNameA((HINSTANCE) tclHandle, dllFilename, MAX_PATH);
436 }
437 findExecutable(dllFilename);
438 #else
439 0 ▶ findExecutable(X && SvPOK(X) ? SvPV_nolen(X) : NULL);
Branch: 2 branches
BranchTrueFalseTotal
findExecutable(X && SvPOK(X) ? SvPV_nolen(X) : NULL); 0 0 0
findExecutable(X && SvPOK(X) ? SvPV_nolen(X) : NULL); 0 0 0
440 #endif
441  
442 0 g_Interp = createInterp();
443 0 ▶ if (g_Interp == (Tcl_Interp *) NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_Interp == (Tcl_Interp *) NULL) { 0 0 0
444 0 warn("Failed to create main Tcl interpreter!");
445 0 return TCL_ERROR;
446 }
447  
448 /*
449 * Until Tcl_InitStubs is called, we cannot make any Tcl/Tk API
450 * calls without grabbing them by symbol out of the dll.
451 * This will be Tcl_PkgRequire for non-stubs builds.
452 */
453 0 ▶ if (initstubs(g_Interp, "8.4", 0) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (initstubs(g_Interp, "8.4", 0) == NULL) { 0 0 0
454 0 warn("Failed to initialize Tcl stubs!");
455 0 return TCL_ERROR;
456 }
457  
458 /*
459 * If we didn't find TclKit_AppInit, then this is a regular Tcl
460 * installation, so invoke Tcl_Init.
461 * Otherwise, we need to set the kit path to indicate we want to
462 * use the dll as our base kit.
463 */
464 0 ▶ if (tclKit_AppInit == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tclKit_AppInit == NULL) { 0 0 0
465 0 tclKit_AppInit = Tcl_Init;
466 } else {
467 0 char * (* tclKit_SetKitPath)(char *) = NULL;
468 /*
469 * We need to see if this has TclKit_SetKitPath. This is in
470 * special base kit dlls that have embedded data in the dll.
471 */
472 0 ▶ if (dllFilename[0] != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
if (dllFilename[0] != '\0') { 0 0 0
473 0 DLSYM(tclHandle, "TclKit_SetKitPath", char * (*)(char *),
474 tclKit_SetKitPath);
475 0 ▶ if (tclKit_SetKitPath != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tclKit_SetKitPath != NULL) { 0 0 0
476 /*
477 * XXX: Need to figure out how to populate dllFilename if
478 * NpLoadLibrary didn't do it for us on Unix.
479 */
480 0 tclKit_SetKitPath(dllFilename);
481 }
482 }
483 }
484 0 ▶ if (tclKit_AppInit(g_Interp) != TCL_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (tclKit_AppInit(g_Interp) != TCL_OK) { 0 0 0
485 0 CONST84 char *msg = Tcl_GetVar(g_Interp, "errorInfo", TCL_GLOBAL_ONLY);
486 0 ▶ warn("Failed to initialize Tcl with %s:\n%s",
Branch: 1 branch
BranchTrueFalseTotal
warn("Failed to initialize Tcl with %s:\n%s", 0 0 0
487 (tclKit_AppInit == Tcl_Init) ? "Tcl_Init" : "TclKit_AppInit",
488 msg);
489 0 return TCL_ERROR;
490 }
491  
492 /*
493 * Hold on to the interp handle until finalize, as special
494 * kit-based interps require the first initialized interp to
495 * remain alive.
496 */
497  
498 0 return TCL_OK;
499 }
500 #endif
501  
502 #if DEBUG_REFCOUNTS
503 static void
504 check_refcounts(Tcl_Obj *objPtr) {
505 int rc = objPtr->refCount;
506 if (rc != 1) {
507 fprintf(stderr, "objPtr %p refcount %d\n", objPtr, rc); fflush(stderr);
508 }
509 if (objPtr->typePtr == tclListTypePtr) {
510 int objc, i;
511 Tcl_Obj **objv;
512  
513 Tcl_ListObjGetElements(NULL, objPtr, &objc, &objv);
514 for (i = 0; i < objc; i++) {
515 check_refcounts(objv[i]);
516 }
517 }
518 }
519 #endif
520  
521 static int
522 0 has_highbit(CONST char *s, int len)
523 {
524 0 CONST char *e = s + len;
525 0 ▶ while (s < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < e) { 0 0 0
526 0 ▶ if (*s++ & 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if (*s++ & 0x80) 0 0 0
527 0 return 1;
528 }
529 0 return 0;
530 }
531  
532 static SV *
533 0 SvFromTclObj(pTHX_ Tcl_Obj *objPtr)
534 {
535 SV *sv;
536 int len;
537 char *str;
538  
539 0 ▶ if (objPtr == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (objPtr == NULL) { 0 0 0
540 /*
541 * Use newSV(0) instead of &PL_sv_undef as it may be stored in an AV.
542 * It also provides symmetry with the other newSV* calls below.
543 * This SV will also be mortalized later.
544 */
545 0 sv = newSV(0);
546 }
547 0 ▶ else if (objPtr->typePtr == tclIntTypePtr) {
Branch: 1 branch
BranchTrueFalseTotal
else if (objPtr->typePtr == tclIntTypePtr) { 0 0 0
548 0 sv = newSViv(objPtr->internalRep.longValue);
549 }
550 0 ▶ else if (objPtr->typePtr == tclDoubleTypePtr) {
Branch: 1 branch
BranchTrueFalseTotal
else if (objPtr->typePtr == tclDoubleTypePtr) { 0 0 0
551 0 sv = newSVnv(objPtr->internalRep.doubleValue);
552 }
553 0 ▶ else if (objPtr->typePtr == tclBooleanTypePtr) {
Branch: 1 branch
BranchTrueFalseTotal
else if (objPtr->typePtr == tclBooleanTypePtr) { 0 0 0
554 /*
555 * Booleans can originate as words (yes/true/...), so if there is a
556 * string rep, use it instead. We could check if the first byte
557 * isdigit(). No need to check utf-8 as the all valid boolean words
558 * are ascii-7.
559 */
560 0 ▶ if (objPtr->typePtr == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (objPtr->typePtr == NULL) { 0 0 0
561 0 ▶ sv = newSVsv(boolSV(objPtr->internalRep.longValue != 0));
Branch: 1 branch
BranchTrueFalseTotal
sv = newSVsv(boolSV(objPtr->internalRep.longValue != 0)); 0 0 0
562 } else {
563 0 str = Tcl_GetStringFromObj(objPtr, &len);
564 0 sv = newSVpvn(str, len);
565 }
566 }
567 0 ▶ else if (objPtr->typePtr == tclByteArrayTypePtr) {
Branch: 1 branch
BranchTrueFalseTotal
else if (objPtr->typePtr == tclByteArrayTypePtr) { 0 0 0
568 0 str = (char *) Tcl_GetByteArrayFromObj(objPtr, &len);
569 0 sv = newSVpvn(str, len);
570 }
571 0 ▶ else if (objPtr->typePtr == tclListTypePtr) {
Branch: 1 branch
BranchTrueFalseTotal
else if (objPtr->typePtr == tclListTypePtr) { 0 0 0
572 /*
573 * tclListTypePtr should become an AV.
574 * This code needs to reconcile with G_ context in prepare_Tcl_result
575 * and user's expectations of how data will be passed in. The key is
576 * that a stringified-list and pure-list should be operable in the
577 * same way in Perl.
578 *
579 * We have to watch for "empty" lists, which could equate to the
580 * empty string. Tcl's literal object sharing means that "" could
581 * be typed as a list, although we don't want to see it that way.
582 * Just treat empty list objects as an empty (not undef) SV.
583 */
584 int objc;
585 Tcl_Obj **objv;
586  
587 0 Tcl_ListObjGetElements(NULL, objPtr, &objc, &objv);
588 0 ▶ if (objc) {
Branch: 1 branch
BranchTrueFalseTotal
if (objc) { 0 0 0
589 int i;
590 0 AV *av = newAV();
591  
592 0 ▶ for (i = 0; i < objc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < objc; i++) { 0 0 0
593 0 av_push(av, SvFromTclObj(aTHX_ objv[i]));
594 }
595 0 sv = sv_bless(newRV_noinc((SV *) av), gv_stashpv("Tcl::List", 1));
596 }
597 else {
598 0 sv = newSVpvn("", 0);
599 }
600 }
601 /* tclStringTypePtr is true unicode */
602 /* tclWideIntTypePtr is 64-bit int */
603 else {
604 0 str = Tcl_GetStringFromObj(objPtr, &len);
605 0 sv = newSVpvn(str, len);
606 /* should turn on, but let's check this first for efficiency */
607 0 ▶ if (len && has_highbit(str, len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (len && has_highbit(str, len)) { 0 0 0
if (len && has_highbit(str, len)) { 0 0 0
608 /*
609 * Tcl can encode NULL as overlong utf-8 \300\200 (\xC0\x80).
610 * Tcl itself doesn't require this, but some extensions do when
611 * they pass the string data to native C APIs (like strlen).
612 * Tk is the most notable case for this (calling out to native UI
613 * toolkit APIs that don't take counted strings).
614 * s/\300\200/\0/g
615 */
616 char *nul_start;
617 STRLEN len;
618 0 char *s = SvPV(sv, len);
619 0 char *end = s + len;
620 0 ▶ while ((nul_start = memchr(s, '\300', len))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((nul_start = memchr(s, '\300', len))) { 0 0 0
621 0 ▶ if (nul_start + 1 < end && nul_start[1] == '\200') {
Branch: 2 branches
BranchTrueFalseTotal
if (nul_start + 1 < end && nul_start[1] == '\200') { 0 0 0
if (nul_start + 1 < end && nul_start[1] == '\200') { 0 0 0
622 /* found it */
623 0 nul_start[0] = '\0';
624 0 memmove(nul_start + 1, nul_start + 2,
625 0 end - (nul_start + 2));
626 0 len--;
627 0 end--;
628 0 *end = '\0';
629 0 SvCUR_set(sv, SvCUR(sv) - 1);
630 }
631 0 len -= (nul_start + 1) - s;
632 0 s = nul_start + 1;
633 }
634 0 SvUTF8_on(sv);
635 }
636 }
637 0 return sv;
638 }
639  
640 /*
641 * Create a Tcl_Obj from a Perl SV.
642 * Return Tcl_Obj with refcount = 0. Caller should call Tcl_IncrRefCount
643 * or pass of to function that does (manage object lifetime).
644 */
645 static Tcl_Obj *
646 0 TclObjFromSv(pTHX_ SV *sv)
647 {
648 0 Tcl_Obj *objPtr = NULL;
649  
650 0 ▶ if (SvGMAGICAL(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(sv)) 0 0 0
651 0 mg_get(sv);
652  
653 0 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV &&
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV && 0 0 0
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV && 0 0 0
654 0 ▶ (!SvOBJECT(SvRV(sv)) || sv_isa(sv, "Tcl::List")))
Branch: 2 branches
BranchTrueFalseTotal
(!SvOBJECT(SvRV(sv)) || sv_isa(sv, "Tcl::List"))) 0 0 0
(!SvOBJECT(SvRV(sv)) || sv_isa(sv, "Tcl::List"))) 0 0 0
655 0 {
656 /*
657 * Recurse into ARRAYs, turning them into Tcl list Objs
658 */
659 SV **svp;
660 0 AV *av = (AV *) SvRV(sv);
661 0 I32 avlen = av_len(av);
662 int i;
663  
664 0 objPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
665  
666 0 ▶ for (i = 0; i <= avlen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= avlen; i++) { 0 0 0
667 0 svp = av_fetch(av, i, FALSE);
668 0 ▶ if (svp == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp == NULL) { 0 0 0
669 /* watch for sparse arrays - translate as empty element */
670 /* XXX: Is this handling refcount on NewObj right? */
671 0 Tcl_ListObjAppendElement(NULL, objPtr, Tcl_NewObj());
672 } else {
673 0 ▶ if (SvROK(*svp) && (AV *) SvRV(*svp) == av) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(*svp) && (AV *) SvRV(*svp) == av) { 0 0 0
if (SvROK(*svp) && (AV *) SvRV(*svp) == av) { 0 0 0
674 /* XXX: Is this a proper check for cyclical reference? */
675 0 croak("cyclical array reference found");
676 abort();
677 }
678 0 Tcl_ListObjAppendElement(NULL, objPtr,
679 TclObjFromSv(aTHX_ sv_mortalcopy(*svp)));
680 }
681 }
682 }
683 0 ▶ else if (SvPOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvPOK(sv)) { 0 0 0
684 STRLEN length;
685 0 char *str = SvPV(sv, length);
686 /*
687 * Tcl's "String" object expects utf-8 strings. If we aren't sure
688 * that we have a utf-8 data, pass it as a Tcl ByteArray (C char*).
689 *
690 * XXX Possible optimization opportunity here. Tcl will actually
691 * XXX accept and handle most latin-1 char sequences correctly, but
692 * XXX not blocks of truly binary data. This code is 100% correct,
693 * XXX but could be tweaked to improve performance.
694 */
695 0 ▶ if (SvUTF8(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(sv)) { 0 0 0
696 /*
697 * Tcl allows NULL to be encoded overlong as \300\200 (\xC0\x80).
698 * Tcl itself doesn't require this, but some extensions do when
699 * they pass the string data to native C APIs (like strlen).
700 * Tk is the most notable case for this (calling out to native UI
701 * toolkit APIs that don't take counted strings).
702 */
703 0 ▶ if (memchr(str, '\0', length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(str, '\0', length)) { 0 0 0
704 /* ($sv_copy = $sv) =~ s/\0/\300\200/g */
705 0 SV *sv_copy = sv_mortalcopy(sv);
706 STRLEN len;
707 0 char *s = SvPV(sv_copy, len);
708 char *nul;
709  
710 0 ▶ while ((nul = memchr(s, '\0', len))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((nul = memchr(s, '\0', len))) { 0 0 0
711 0 STRLEN i = nul - SvPVX(sv_copy);
712 0 ▶ s = SvGROW(sv_copy, SvCUR(sv_copy) + 2);
Branch: 2 branches
BranchTrueFalseTotal
s = SvGROW(sv_copy, SvCUR(sv_copy) + 2); 0 0 0
s = SvGROW(sv_copy, SvCUR(sv_copy) + 2); 0 0 0
713 0 nul = s + i;
714 0 memmove(nul + 2, nul + 1, SvEND(sv_copy) - (nul + 1));
715 0 nul[0] = '\300';
716 0 nul[1] = '\200';
717 0 SvCUR_set(sv_copy, SvCUR(sv_copy) + 1);
718 0 s = nul + 2;
719 0 len = SvEND(sv_copy) - s;
720 }
721 0 str = SvPV(sv_copy, length);
722 }
723 0 objPtr = Tcl_NewStringObj(str, length);
724 } else {
725 0 objPtr = Tcl_NewByteArrayObj((unsigned char *)str, length);
726 }
727 }
728 0 ▶ else if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvNOK(sv)) { 0 0 0
729 0 double dval = SvNV(sv);
730 int ival;
731 /*
732 * Perl does math with doubles by default, so 0 + 1 == 1.0.
733 * Check for int-equiv doubles and make those ints.
734 * XXX This check possibly only necessary for <=5.6.x
735 */
736 0 ▶ if (((double)(ival = SvIV(sv)) == dval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (((double)(ival = SvIV(sv)) == dval)) { 0 0 0
737 0 objPtr = Tcl_NewIntObj(ival);
738 } else {
739 0 objPtr = Tcl_NewDoubleObj(dval);
740 }
741 }
742 0 ▶ else if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvIOK(sv)) { 0 0 0
743 0 objPtr = Tcl_NewIntObj(SvIV(sv));
744 }
745 else {
746 /*
747 * Catch-all
748 * XXX: Should we recurse other REFs, or better to stringify them?
749 */
750 STRLEN length;
751 0 char *str = SvPV(sv, length);
752 /*
753 * Tcl's "String" object expects utf-8 strings. If we aren't sure
754 * that we have a utf-8 data, pass it as a Tcl ByteArray (C char*).
755 */
756 0 ▶ if (SvUTF8(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(sv)) { 0 0 0
757 /*
758 * Should we consider overlong NULL encoding for Tcl here?
759 */
760 0 objPtr = Tcl_NewStringObj(str, length);
761 } else {
762 0 objPtr = Tcl_NewByteArrayObj((unsigned char *) str, length);
763 }
764 }
765  
766 0 return objPtr;
767 }
768  
769 0 int Tcl_EvalInPerl(ClientData clientData, Tcl_Interp *interp,
770 int objc, Tcl_Obj *CONST objv[])
771 {
772 dTHX; /* fetch context */
773 0 dSP;
774 I32 count;
775 SV *sv;
776 int rc;
777  
778 /*
779 * This is the command created in Tcl to eval stuff in Perl
780 */
781  
782 0 ▶ if (objc != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (objc != 2) { 0 0 0
783 0 Tcl_WrongNumArgs(interp, 1, objv, "string");
784 }
785  
786 0 ENTER;
787 0 SAVETMPS;
788  
789 0 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 0 0
790 0 PUTBACK;
791 0 count = perl_eval_sv(sv_2mortal(SvFromTclObj(aTHX_ objv[1])),
792 G_EVAL|G_SCALAR);
793 0 SPAGAIN;
794  
795 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
796 0 ▶ Tcl_SetResult(interp, SvPV_nolen(ERRSV), TCL_VOLATILE);
Branch: 1 branch
BranchTrueFalseTotal
Tcl_SetResult(interp, SvPV_nolen(ERRSV), TCL_VOLATILE); 0 0 0
797 0 POPs; /* pop the undef off the stack */
798 0 rc = TCL_ERROR;
799 }
800 else {
801 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
802 0 croak("Perl sub bound to Tcl proc returned %ld args, expected 1",
803 (long)count);
804 }
805 0 sv = POPs; /* pop the undef off the stack */
806  
807 0 ▶ if (SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv)) { 0 0 0
808 0 Tcl_Obj *objPtr = TclObjFromSv(aTHX_ sv);
809 /* Tcl_SetObjResult will incr refcount */
810 0 Tcl_SetObjResult(interp, objPtr);
811 }
812 0 rc = TCL_OK;
813 }
814  
815 0 PUTBACK;
816 /*
817 * If the routine returned undef, it indicates that it has done the
818 * SetResult itself and that we should return TCL_ERROR
819 */
820  
821 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
822 0 LEAVE;
823 0 return rc;
824 }
825  
826 0 int Tcl_PerlCallWrapper(ClientData clientData, Tcl_Interp *interp,
827 int objc, Tcl_Obj *CONST objv[])
828 {
829 dTHX; /* fetch context */
830 0 dSP;
831 0 AV *av = (AV *) clientData;
832 I32 count;
833 SV *sv;
834 int flag;
835 int rc;
836  
837 /*
838 * av = [$perlsub, $realclientdata, $interp, $deleteProc]
839 * (where $deleteProc is optional but we don't need it here anyway)
840 */
841  
842 0 ▶ if (AvFILL(av) != 3 && AvFILL(av) != 4)
Branch: 2 branches
BranchTrueFalseTotal
if (AvFILL(av) != 3 && AvFILL(av) != 4) 0 0 0
if (AvFILL(av) != 3 && AvFILL(av) != 4) 0 0 0
843 0 croak("bad clientdata argument passed to Tcl_PerlCallWrapper");
844  
845 0 flag = SvIV(*av_fetch(av, 3, FALSE));
846  
847 0 ENTER;
848 0 SAVETMPS;
849  
850 0 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 0 0
851 0 ▶ if (flag & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (flag & 1) { 0 0 0
852 0 ▶ if (objc) {
Branch: 1 branch
BranchTrueFalseTotal
if (objc) { 0 0 0
853 0 objc--;
854 0 objv++;
855 0 ▶ EXTEND(sp, objc);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(sp, objc); 0 0 0
EXTEND(sp, objc); 0 0 0
856 }
857 }
858 else {
859 0 ▶ EXTEND(sp, objc + 2);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(sp, objc + 2); 0 0 0
EXTEND(sp, objc + 2); 0 0 0
860 /*
861 * Place clientData and original interp on the stack, then the
862 * Tcl object invoke list, including the command name. Users
863 * who only want the args from Tcl can splice off the first 3 args
864 */
865 0 PUSHs(sv_mortalcopy(*av_fetch(av, 1, FALSE)));
866 0 PUSHs(sv_mortalcopy(*av_fetch(av, 2, FALSE)));
867 }
868 0 ▶ while (objc--) {
Branch: 1 branch
BranchTrueFalseTotal
while (objc--) { 0 0 0
869 0 PUSHs(sv_2mortal(SvFromTclObj(aTHX_ *objv++)));
870 }
871 0 PUTBACK;
872 0 count = perl_call_sv(*av_fetch(av, 0, FALSE), G_EVAL|G_SCALAR);
873 0 SPAGAIN;
874  
875 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
876 0 ▶ Tcl_SetResult(interp, SvPV_nolen(ERRSV), TCL_VOLATILE);
Branch: 1 branch
BranchTrueFalseTotal
Tcl_SetResult(interp, SvPV_nolen(ERRSV), TCL_VOLATILE); 0 0 0
877 0 POPs; /* pop the undef off the stack */
878 0 rc = TCL_ERROR;
879 }
880 else {
881 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
882 0 croak("Perl sub bound to Tcl proc returned %ld args, expected 1",
883 (long)count);
884 }
885 0 sv = POPs; /* pop the undef off the stack */
886  
887 0 ▶ if (SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(sv)) { 0 0 0
888 0 Tcl_Obj *objPtr = TclObjFromSv(aTHX_ sv);
889 /* Tcl_SetObjResult will incr refcount */
890 0 Tcl_SetObjResult(interp, objPtr);
891 }
892 0 rc = TCL_OK;
893 }
894  
895 0 PUTBACK;
896 /*
897 * If the routine returned undef, it indicates that it has done the
898 * SetResult itself and that we should return TCL_ERROR
899 */
900  
901 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
902 0 LEAVE;
903 0 return rc;
904 }
905  
906 void
907 0 Tcl_PerlCallDeleteProc(ClientData clientData)
908 {
909 dTHX; /* fetch context */
910 0 AV *av = (AV *) clientData;
911  
912 /*
913 * av = [$perlsub, $realclientdata, $interp, $deleteProc]
914 * (where $deleteProc is optional but we don't need it here anyway)
915 */
916  
917 0 ▶ if (AvFILL(av) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (AvFILL(av) == 4) { 0 0 0
918 0 dSP;
919  
920 0 ▶ PUSHMARK(sp);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(sp); 0 0 0
921 0 ▶ EXTEND(sp, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(sp, 1); 0 0 0
922 0 PUSHs(sv_mortalcopy(*av_fetch(av, 1, FALSE)));
923 0 PUTBACK;
924 0 (void) perl_call_sv(*av_fetch(av, 4, FALSE), G_SCALAR|G_DISCARD);
925 }
926 0 ▶ else if (AvFILL(av) != 3) {
Branch: 1 branch
BranchTrueFalseTotal
else if (AvFILL(av) != 3) { 0 0 0
927 0 croak("bad clientdata argument passed to Tcl_PerlCallDeleteProc");
928 }
929  
930 0 SvREFCNT_dec(av);
931 /* it got double tapped when it was made
932 ie AV *av = (AV *) SvREFCNT_inc((SV *) newAV());
933 so undouble tap it now
934 */
935 0 SvREFCNT_dec(av);
936  
937 0 }
938  
939 void
940 0 prepare_Tcl_result(pTHX_ Tcl interp, char *caller)
941 {
942 0 dSP;
943 Tcl_Obj *objPtr, **objv;
944 int gimme, objc, i;
945  
946 0 objPtr = Tcl_GetObjResult(interp);
947  
948 0 gimme = GIMME_V;
949 0 ▶ if (gimme == G_SCALAR) {
Branch: 1 branch
BranchTrueFalseTotal
if (gimme == G_SCALAR) { 0 0 0
950 /*
951 * This checks Tcl_Obj type. XPUSH not needed because we
952 * are called when there is enough space on the stack.
953 */
954 0 PUSHs(sv_2mortal(SvFromTclObj(aTHX_ objPtr)));
955 }
956 0 ▶ else if (gimme == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
else if (gimme == G_ARRAY) { 0 0 0
957 0 ▶ if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv)
Branch: 1 branch
BranchTrueFalseTotal
if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv) 0 0 0
958 != TCL_OK) {
959 0 croak("%s called in list context did not return a valid Tcl list",
960 caller);
961 }
962 0 ▶ if (objc) {
Branch: 1 branch
BranchTrueFalseTotal
if (objc) { 0 0 0
963 0 ▶ EXTEND(sp, objc);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(sp, objc); 0 0 0
EXTEND(sp, objc); 0 0 0
964 0 ▶ for (i = 0; i < objc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < objc; i++) { 0 0 0
965 /*
966 * This checks Tcl_Obj type
967 */
968 0 PUSHs(sv_2mortal(SvFromTclObj(aTHX_ objv[i])));
969 }
970 }
971 }
972 else {
973 /* G_VOID context - ignore result */
974 }
975 0 PUTBACK;
976 0 return;
977 }
978  
979 char *
980 0 var_trace(ClientData clientData, Tcl_Interp *interp,
981 char *name1, char *name2, int flags)
982 {
983 dTHX; /* fetch context */
984  
985 0 ▶ if (flags & TCL_TRACE_READS) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & TCL_TRACE_READS) { 0 0 0
986 0 warn("TCL_TRACE_READS\n");
987 }
988 0 ▶ else if (flags & TCL_TRACE_WRITES) {
Branch: 1 branch
BranchTrueFalseTotal
else if (flags & TCL_TRACE_WRITES) { 0 0 0
989 0 warn("TCL_TRACE_WRITES\n");
990 }
991 0 ▶ else if (flags & TCL_TRACE_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
else if (flags & TCL_TRACE_ARRAY) { 0 0 0
992 0 warn("TCL_TRACE_ARRAY\n");
993 }
994 0 ▶ else if (flags & TCL_TRACE_UNSETS) {
Branch: 1 branch
BranchTrueFalseTotal
else if (flags & TCL_TRACE_UNSETS) { 0 0 0
995 0 warn("TCL_TRACE_UNSETS\n");
996 }
997 0 return 0;
998 }
999  
1000 MODULE = Tcl PACKAGE = Tcl PREFIX = Tcl_
1001  
1002 SV *
1003 Tcl__new(class = "Tcl")
1004 char * class
1005 CODE:
1006 0 RETVAL = newSV(0);
1007 /*
1008 * We might consider Tcl_Preserve/Tcl_Release of the interp.
1009 */
1010 0 ▶ if (initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (initialized) { 0 0 0
1011 0 Tcl interp = Tcl_CreateInterp();
1012 0 Tcl_CreateObjCommand(interp, "::perl::Eval", Tcl_EvalInPerl,
1013 (ClientData) NULL, NULL);
1014 /*
1015 * Add to the global hash of live interps.
1016 */
1017 0 ▶ if (hvInterps) {
Branch: 1 branch
BranchTrueFalseTotal
if (hvInterps) { 0 0 0
1018 0 (void) hv_store(hvInterps, (const char *) &interp,
1019 sizeof(Tcl), &PL_sv_undef, 0);
1020 }
1021 0 sv_setref_pv(RETVAL, class, (void*)interp);
1022 }
1023 OUTPUT:
1024 RETVAL
1025  
1026 SV *
1027 Tcl_CreateSlave(master,name,safe)
1028 Tcl master
1029 char * name
1030 int safe
1031 CODE:
1032 0 RETVAL = newSV(0);
1033 /*
1034 * We might consider Tcl_Preserve/Tcl_Release of the interp.
1035 */
1036 0 ▶ if (initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (initialized) { 0 0 0
1037 0 Tcl interp = Tcl_CreateSlave(master,name,safe);
1038 /*
1039 * Add to the global hash of live interps.
1040 */
1041 0 ▶ if (hvInterps) {
Branch: 1 branch
BranchTrueFalseTotal
if (hvInterps) { 0 0 0
1042 0 (void) hv_store(hvInterps, (const char *) &interp,
1043 sizeof(Tcl), &PL_sv_undef, 0);
1044 }
1045 /* Create lets us set a class, should we do this too? */
1046 0 sv_setref_pv(RETVAL, "Tcl", (void*)interp);
1047 }
1048 OUTPUT:
1049 RETVAL
1050  
1051 SV *
1052 Tcl_result(interp)
1053 Tcl interp
1054 CODE:
1055 0 ▶ if (initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (initialized) { 0 0 0
1056 0 RETVAL = SvFromTclObj(aTHX_ Tcl_GetObjResult(interp));
1057 }
1058 else {
1059 0 RETVAL = &PL_sv_undef;
1060 }
1061 OUTPUT:
1062 RETVAL
1063  
1064 void
1065 Tcl_Eval(interp, script, flags = 0)
1066 Tcl interp
1067 SV * script
1068 int flags
1069 SV * interpsv = ST(0);
1070 STRLEN length = NO_INIT
1071 char * cscript = NO_INIT
1072 PPCODE:
1073 0 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 0 0 0
1074 0 (void) sv_2mortal(SvREFCNT_inc(interpsv));
1075 0 PUTBACK;
1076 0 Tcl_ResetResult(interp);
1077 /* sv_mortalcopy here prevents stringifying script -
1078 cscript = SvPV(sv_mortalcopy(script), length);
1079 - but then we have problems when script is large,
1080 case covered with t/eval.t, NOT ok 6 */
1081 0 cscript = SvPV(script, length);
1082 0 ▶ if (Tcl_EvalEx(interp, cscript, length, flags) != TCL_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (Tcl_EvalEx(interp, cscript, length, flags) != TCL_OK) { 0 0 0
1083 0 croak("%s", Tcl_GetStringResult(interp));
1084 }
1085 0 prepare_Tcl_result(aTHX_ interp, "Tcl::Eval");
1086 0 SPAGAIN;
1087  
1088 #ifdef HAVE_TKINIT
1089  
1090 char*
1091 Tcl_SetPreInitScript(script)
1092 char * script
1093 CODE:
1094 if (!initialized) { return; }
1095 RETVAL = TclSetPreInitScript(script);
1096 OUTPUT:
1097 RETVAL
1098  
1099 void
1100 TclpInitLibraryPath(path)
1101 char * path
1102 PPCODE:
1103 int lengthPtr=0;
1104 Tcl_Encoding encodingPtr;
1105  
1106 if (!initialized) { return; }
1107 /* interface to TclpInitLibraryPath changed between 8.4.x and 8.5.x */
1108 TclpInitLibraryPath(&path, &lengthPtr, &encodingPtr);
1109  
1110 void
1111 Tcl_SetDefaultEncodingDir(script)
1112 char * script
1113 PPCODE:
1114 if (!initialized) { return; }
1115 Tcl_SetDefaultEncodingDir(script);
1116  
1117 char*
1118 Tcl_GetDefaultEncodingDir(void)
1119 CODE:
1120 if (!initialized) { return; }
1121 RETVAL = Tcl_GetDefaultEncodingDir();
1122 OUTPUT:
1123 RETVAL
1124  
1125 void*
1126 Tcl_GetEncoding(interp, enc)
1127 Tcl interp
1128 char *enc
1129 PPCODE:
1130 if (!initialized) { return; }
1131 Tcl_GetEncoding(interp,enc);
1132  
1133 #endif /* HAVE_TKINIT */
1134  
1135 void
1136 Tcl_EvalFile(interp, filename)
1137 Tcl interp
1138 char * filename
1139 SV * interpsv = ST(0);
1140 PPCODE:
1141 0 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 0 0 0
1142 0 (void) sv_2mortal(SvREFCNT_inc(interpsv));
1143 0 PUTBACK;
1144 0 Tcl_ResetResult(interp);
1145 0 ▶ if (Tcl_EvalFile(interp, filename) != TCL_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (Tcl_EvalFile(interp, filename) != TCL_OK) { 0 0 0
1146 0 croak("%s", Tcl_GetStringResult(interp));
1147 }
1148 0 prepare_Tcl_result(aTHX_ interp, "Tcl::EvalFile");
1149 0 SPAGAIN;
1150  
1151 void
1152 Tcl_EvalFileHandle(interp, handle)
1153 Tcl interp
1154 PerlIO* handle
1155 int append = 0;
1156 SV * interpsv = ST(0);
1157 SV * sv = sv_newmortal();
1158 char * s = NO_INIT
1159 PPCODE:
1160 0 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 0 0 0
1161 0 (void) sv_2mortal(SvREFCNT_inc(interpsv));
1162 0 PUTBACK;
1163 0 ▶ while ((s = sv_gets(sv, handle, append)))
Branch: 1 branch
BranchTrueFalseTotal
while ((s = sv_gets(sv, handle, append))) 0 0 0
1164 {
1165 0 ▶ if (!Tcl_CommandComplete(s))
Branch: 1 branch
BranchTrueFalseTotal
if (!Tcl_CommandComplete(s)) 0 0 0
1166 0 append = 1;
1167 else
1168 {
1169 0 Tcl_ResetResult(interp);
1170 0 ▶ if (Tcl_Eval(interp, s) != TCL_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (Tcl_Eval(interp, s) != TCL_OK) 0 0 0
1171 0 croak("%s", Tcl_GetStringResult(interp));
1172 0 append = 0;
1173 }
1174 }
1175 0 ▶ if (append)
Branch: 1 branch
BranchTrueFalseTotal
if (append) 0 0 0
1176 0 croak("unexpected end of file in Tcl::EvalFileHandle");
1177 0 prepare_Tcl_result(aTHX_ interp, "Tcl::EvalFileHandle");
1178 0 SPAGAIN;
1179  
1180 void
1181 Tcl_invoke(interp, sv, ...)
1182 Tcl interp
1183 SV * sv
1184 PPCODE:
1185 {
1186 /*
1187 * 'Tcl::invoke' invokes the command directly, avoiding
1188 * command tracing and the ::unknown mechanism.
1189 */
1190 #define NUM_OBJS 16
1191 Tcl_Obj *baseobjv[NUM_OBJS];
1192 0 Tcl_Obj **objv = baseobjv;
1193 char *cmdName;
1194 int objc, i, result;
1195 STRLEN length;
1196 Tcl_CmdInfo cmdinfo;
1197  
1198 0 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 0 0 0
1199  
1200 0 objv = baseobjv;
1201 0 objc = items-1;
1202 0 ▶ if (objc > NUM_OBJS) {
Branch: 1 branch
BranchTrueFalseTotal
if (objc > NUM_OBJS) { 0 0 0
1203 0 New(666, objv, objc, Tcl_Obj *);
1204 }
1205  
1206 0 SP += items;
1207 0 PUTBACK;
1208  
1209 /* Verify first arg is a Tcl command */
1210 0 cmdName = SvPV(sv, length);
1211 0 ▶ if (!Tcl_GetCommandInfo(interp, cmdName, &cmdinfo)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!Tcl_GetCommandInfo(interp, cmdName, &cmdinfo)) { 0 0 0
1212 0 croak("Tcl procedure '%s' not found", cmdName);
1213 }
1214  
1215 0 ▶ if (cmdinfo.objProc && cmdinfo.isNativeObjectProc) {
Branch: 2 branches
BranchTrueFalseTotal
if (cmdinfo.objProc && cmdinfo.isNativeObjectProc) { 0 0 0
if (cmdinfo.objProc && cmdinfo.isNativeObjectProc) { 0 0 0
1216 /*
1217 * We might want to check that this isn't
1218 * TclInvokeStringCommand, which just means we waste time
1219 * making Tcl_Obj's.
1220 *
1221 * Emulate TclInvokeObjectCommand (from Tcl), namely create the
1222 * object argument array "objv" before calling right procedure
1223 */
1224 0 objv[0] = Tcl_NewStringObj(cmdName, length);
1225 0 Tcl_IncrRefCount(objv[0]);
1226 0 ▶ for (i = 1; i < objc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < objc; i++) { 0 0 0
1227 /*
1228 * Use efficient Sv to Tcl_Obj conversion.
1229 * This returns Tcl_Obj with refcount 1.
1230 * This can cause recursive calls if we have tied vars.
1231 */
1232 0 objv[i] = TclObjFromSv(aTHX_ sv_mortalcopy(ST(i+1)));
1233 0 Tcl_IncrRefCount(objv[i]);
1234 }
1235 0 SP -= items;
1236 0 PUTBACK;
1237  
1238 /*
1239 * Result interp result and invoke the command's object-based
1240 * Tcl_ObjCmdProc.
1241 */
1242 #if DEBUG_REFCOUNTS
1243 for (i = 1; i < objc; i++) { check_refcounts(objv[i]); }
1244 #endif
1245 0 Tcl_ResetResult(interp);
1246 0 result = (*cmdinfo.objProc)(cmdinfo.objClientData, interp,
1247 objc, objv);
1248  
1249 /*
1250 * Decrement ref count for first arg, others decr'd below
1251 */
1252 0 ▶ Tcl_DecrRefCount(objv[0]);
Branch: 1 branch
BranchTrueFalseTotal
Tcl_DecrRefCount(objv[0]); 0 0 0
1253 }
1254 else {
1255 /*
1256 * we have cmdinfo.objProc==0
1257 * prepare string arguments into argv (1st is already done)
1258 * and call found procedure
1259 */
1260 char *baseargv[NUM_OBJS];
1261 0 char **argv = baseargv;
1262  
1263 0 ▶ if (objc > NUM_OBJS) {
Branch: 1 branch
BranchTrueFalseTotal
if (objc > NUM_OBJS) { 0 0 0
1264 0 New(666, argv, objc, char *);
1265 }
1266  
1267 0 argv[0] = cmdName;
1268 0 ▶ for (i = 1; i < objc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < objc; i++) { 0 0 0
1269 /*
1270 * We need the inefficient round-trip through Tcl_Obj to
1271 * ensure that we are listify-ing correctly.
1272 * This can cause recursive calls if we have tied vars.
1273 */
1274 0 objv[i] = TclObjFromSv(aTHX_ sv_mortalcopy(ST(i+1)));
1275 0 Tcl_IncrRefCount(objv[i]);
1276 0 argv[i] = Tcl_GetString(objv[i]);
1277 }
1278 0 SP -= items;
1279 0 PUTBACK;
1280  
1281 /*
1282 * Result interp result and invoke the command's string-based
1283 * procedure.
1284 */
1285 #if DEBUG_REFCOUNTS
1286 for (i = 1; i < objc; i++) { check_refcounts(objv[i]); }
1287 #endif
1288 0 Tcl_ResetResult(interp);
1289 0 result = (*cmdinfo.proc)(cmdinfo.clientData, interp,
1290 objc, argv);
1291  
1292 0 ▶ if (argv != baseargv) {
Branch: 1 branch
BranchTrueFalseTotal
if (argv != baseargv) { 0 0 0
1293 0 Safefree(argv);
1294 }
1295 }
1296  
1297 /*
1298 * Decrement the ref counts for the argument objects created above
1299 */
1300 0 ▶ for (i = 1; i < objc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < objc; i++) { 0 0 0
1301 0 ▶ Tcl_DecrRefCount(objv[i]);
Branch: 1 branch
BranchTrueFalseTotal
Tcl_DecrRefCount(objv[i]); 0 0 0
1302 }
1303  
1304 0 ▶ if (result != TCL_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (result != TCL_OK) { 0 0 0
1305 0 croak("%s", Tcl_GetStringResult(interp));
1306 }
1307 0 prepare_Tcl_result(aTHX_ interp, "Tcl::invoke");
1308  
1309 0 ▶ if (objv != baseobjv) {
Branch: 1 branch
BranchTrueFalseTotal
if (objv != baseobjv) { 0 0 0
1310 0 Safefree(objv);
1311 }
1312 0 SPAGAIN;
1313 #undef NUM_OBJS
1314 }
1315  
1316  
1317 void
1318 Tcl_icall(interp, sv, ...)
1319 Tcl interp
1320 SV * sv
1321 PPCODE:
1322 {
1323 /*
1324 * 'Tcl::icall' passes the args to Tcl to invoke. It will do
1325 * command tracing and call ::unknown mechanism for unrecognized
1326 * commands.
1327 */
1328 #define NUM_OBJS 16
1329 Tcl_Obj *baseobjv[NUM_OBJS];
1330 0 Tcl_Obj **objv = baseobjv;
1331 int objc, i, result;
1332  
1333 0 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 0 0 0
1334  
1335 0 objc = items-1;
1336 0 ▶ if (objc > NUM_OBJS) {
Branch: 1 branch
BranchTrueFalseTotal
if (objc > NUM_OBJS) { 0 0 0
1337 0 New(666, objv, objc, Tcl_Obj *);
1338 }
1339  
1340 0 SP += items;
1341 0 PUTBACK;
1342 0 ▶ for (i = 0; i < objc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < objc; i++) { 0 0 0
1343 /*
1344 * Use efficient Sv to Tcl_Obj conversion.
1345 * This returns Tcl_Obj with refcount 1.
1346 * This can cause recursive calls if we have tied vars.
1347 */
1348 0 objv[i] = TclObjFromSv(aTHX_ sv_mortalcopy(ST(i+1)));
1349 0 Tcl_IncrRefCount(objv[i]);
1350 }
1351 0 SP -= items;
1352 0 PUTBACK;
1353  
1354 /*
1355 * Reset current result and invoke using Tcl_EvalObjv.
1356 * This will trigger command traces and handle async signals.
1357 */
1358 #if DEBUG_REFCOUNTS
1359 for (i = 1; i < objc; i++) { check_refcounts(objv[i]); }
1360 #endif
1361 0 Tcl_ResetResult(interp);
1362 0 result = Tcl_EvalObjv(interp, objc, objv, 0);
1363  
1364 /*
1365 * Decrement the ref counts for the argument objects created above
1366 */
1367 0 ▶ for (i = 0; i < objc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < objc; i++) { 0 0 0
1368 0 ▶ Tcl_DecrRefCount(objv[i]);
Branch: 1 branch
BranchTrueFalseTotal
Tcl_DecrRefCount(objv[i]); 0 0 0
1369 }
1370  
1371 0 ▶ if (result != TCL_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (result != TCL_OK) { 0 0 0
1372 0 croak("%s", Tcl_GetStringResult(interp));
1373 }
1374 0 prepare_Tcl_result(aTHX_ interp, "Tcl::icall");
1375  
1376 0 ▶ if (objv != baseobjv) {
Branch: 1 branch
BranchTrueFalseTotal
if (objv != baseobjv) { 0 0 0
1377 0 Safefree(objv);
1378 }
1379 0 SPAGAIN;
1380 #undef NUM_OBJS
1381 }
1382  
1383  
1384 void
1385 Tcl_DESTROY(interp)
1386 Tcl interp
1387 CODE:
1388 0 ▶ if (initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (initialized) { 0 0 0
1389 0 Tcl_DeleteInterp(interp);
1390 /*
1391 * Remove from the global hash of live interps.
1392 */
1393 0 ▶ if (hvInterps) {
Branch: 1 branch
BranchTrueFalseTotal
if (hvInterps) { 0 0 0
1394 0 (void) hv_delete(hvInterps, (const char *) interp,
1395 sizeof(Tcl), G_DISCARD);
1396 }
1397 }
1398  
1399 void
1400 Tcl__Finalize(interp=NULL)
1401 Tcl interp
1402 CODE:
1403 /*
1404 * This should be called from the END block - when we no
1405 * longer plan to use Tcl *AT ALL*.
1406 */
1407 20 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 20 0 20
1408 0 ▶ if (hvInterps) {
Branch: 1 branch
BranchTrueFalseTotal
if (hvInterps) { 0 0 0
1409 /*
1410 * Delete all the global hash of live interps.
1411 */
1412 HE *he;
1413  
1414 0 hv_iterinit(hvInterps);
1415 0 he = hv_iternext(hvInterps);
1416 0 ▶ while (he) {
Branch: 1 branch
BranchTrueFalseTotal
while (he) { 0 0 0
1417 I32 len;
1418 0 interp = *((Tcl *) hv_iterkey(he, &len));
1419 0 Tcl_DeleteInterp(interp);
1420 0 he = hv_iternext(hvInterps);
1421 }
1422 0 hv_undef(hvInterps);
1423 0 hvInterps = NULL;
1424 }
1425 #ifdef USE_TCL_STUBS
1426 0 ▶ if (g_Interp) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_Interp) { 0 0 0
1427 0 Tcl_DeleteInterp(g_Interp);
1428 0 g_Interp = NULL;
1429 }
1430 #endif
1431 0 initialized = 0;
1432 0 Tcl_Finalize();
1433 #ifdef USE_TCL_STUBS
1434 0 ▶ if (tclHandle) {
Branch: 1 branch
BranchTrueFalseTotal
if (tclHandle) { 0 0 0
1435 0 dlclose(tclHandle);
1436 0 tclHandle = NULL;
1437 }
1438 #endif
1439  
1440  
1441 void
1442 Tcl_Init(interp)
1443 Tcl interp
1444 CODE:
1445 0 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 0 0 0
1446 0 ▶ if (tclKit_AppInit(interp) != TCL_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (tclKit_AppInit(interp) != TCL_OK) { 0 0 0
1447 0 croak("%s", Tcl_GetStringResult(interp));
1448 }
1449  
1450 #ifdef HAVE_DDEINIT
1451  
1452 void
1453 Dde_Init(interp)
1454 Tcl interp
1455 CODE:
1456 Dde_Init(interp);
1457  
1458 #endif
1459  
1460 #ifdef HAVE_TKINIT
1461  
1462 void
1463 Tk_Init(interp)
1464 Tcl interp
1465 CODE:
1466 Tk_Init(interp);
1467  
1468 #endif
1469  
1470 #ifdef HAVE_TIXINIT
1471  
1472 void
1473 Tix_Init(interp)
1474 Tcl interp
1475 CODE:
1476 Tix_Init(interp);
1477  
1478 #endif
1479  
1480 #ifdef HAVE_BLTINIT
1481  
1482 void
1483 Blt_Init(interp)
1484 Tcl interp
1485 CODE:
1486 Blt_Init(interp);
1487  
1488 void
1489 Blt_StaticPackage(interp)
1490 Tcl interp
1491 PPCODE:
1492 Tcl_StaticPackage(interp, "BLT", Blt_Init, Blt_SafeInit);
1493  
1494 #endif
1495  
1496 #ifdef HAVE_MEMCHANINIT
1497  
1498 void
1499 Memchan_Init(interp)
1500 Tcl interp
1501 CODE:
1502 Memchan_Init(interp);
1503  
1504 #endif
1505  
1506 #ifdef HAVE_TRFINIT
1507  
1508 void
1509 Trf_Init(interp)
1510 Tcl interp
1511 CODE:
1512 Trf_Init(interp);
1513  
1514 #endif
1515  
1516 #ifdef HAVE_VFSINIT
1517  
1518 void
1519 Vfs_Init(interp)
1520 Tcl interp
1521 CODE:
1522 Vfs_Init(interp);
1523  
1524 #endif
1525  
1526 int
1527 Tcl_DoOneEvent(interp, flags)
1528 Tcl interp
1529 int flags
1530 CODE:
1531 0 ▶ RETVAL = initialized ? Tcl_DoOneEvent(flags) : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = initialized ? Tcl_DoOneEvent(flags) : 0; 0 0 0
RETVAL = initialized ? Tcl_DoOneEvent(flags) : 0; 0 0 0
1532 OUTPUT:
1533 RETVAL
1534  
1535 void
1536 Tcl_CreateCommand(interp,cmdName,cmdProc,clientData=&PL_sv_undef,deleteProc=&PL_sv_undef,flags=0)
1537 Tcl interp
1538 char * cmdName
1539 SV * cmdProc
1540 SV * clientData
1541 SV * deleteProc
1542 int flags
1543 CODE:
1544 0 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 0 0 0
1545 0 ▶ if (SvIOK(cmdProc))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(cmdProc)) 0 0 0
1546 0 Tcl_CreateCommand(interp, cmdName, (Tcl_CmdProc *) SvIV(cmdProc),
1547 0 INT2PTR(ClientData, SvIV(clientData)), NULL);
1548 else {
1549 0 AV *av = (AV *) SvREFCNT_inc((SV *) newAV());
1550 0 av_store(av, 0, newSVsv(cmdProc));
1551 0 av_store(av, 1, newSVsv(clientData));
1552 0 av_store(av, 2, newSVsv(ST(0)));
1553 0 av_store(av, 3, newSViv(flags));
1554 0 ▶ if (SvOK(deleteProc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(deleteProc)) { 0 0 0
1555 0 av_store(av, 4, newSVsv(deleteProc));
1556 }
1557 0 Tcl_CreateObjCommand(interp, cmdName, Tcl_PerlCallWrapper,
1558 (ClientData) av, Tcl_PerlCallDeleteProc);
1559 }
1560 0 ST(0) = &PL_sv_yes;
1561 0 XSRETURN(1);
1562  
1563 void
1564 Tcl_SetResult(interp, sv)
1565 Tcl interp
1566 SV * sv
1567 CODE:
1568 0 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 0 0 0
1569 {
1570 0 Tcl_Obj *objPtr = TclObjFromSv(aTHX_ sv);
1571 /* Tcl_SetObjResult will incr refcount */
1572 0 Tcl_SetObjResult(interp, objPtr);
1573 0 ST(0) = ST(1);
1574 0 XSRETURN(1);
1575 }
1576  
1577 void
1578 Tcl_AppendElement(interp, str)
1579 Tcl interp
1580 char * str
1581  
1582 void
1583 Tcl_ResetResult(interp)
1584 Tcl interp
1585  
1586 SV *
1587 Tcl_AppendResult(interp, ...)
1588 Tcl interp
1589 int i = NO_INIT
1590 CODE:
1591 0 ▶ if (initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (initialized) { 0 0 0
1592 0 Tcl_Obj *objPtr = Tcl_GetObjResult(interp);
1593 0 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 0 0 0
1594 0 Tcl_AppendObjToObj(objPtr, TclObjFromSv(aTHX_ ST(i)));
1595 }
1596 0 RETVAL = SvFromTclObj(aTHX_ objPtr);
1597 } else {
1598 0 RETVAL = &PL_sv_undef;
1599 }
1600 OUTPUT:
1601 RETVAL
1602  
1603 SV *
1604 Tcl_DeleteCommand(interp, cmdName)
1605 Tcl interp
1606 char * cmdName
1607 CODE:
1608 0 ▶ RETVAL = boolSV(initialized ? Tcl_DeleteCommand(interp, cmdName) == TCL_OK:TRUE);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = boolSV(initialized ? Tcl_DeleteCommand(interp, cmdName) == TCL_OK:TRUE); 0 0 0
RETVAL = boolSV(initialized ? Tcl_DeleteCommand(interp, cmdName) == TCL_OK:TRUE); 0 0 0
1609 OUTPUT:
1610 RETVAL
1611  
1612 void
1613 Tcl_SplitList(interp, str)
1614 Tcl interp
1615 char * str
1616 int argc = NO_INIT
1617 char ** argv = NO_INIT
1618 char ** tofree = NO_INIT
1619 PPCODE:
1620 0 ▶ if (Tcl_SplitList(interp, str, &argc, &argv) == TCL_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (Tcl_SplitList(interp, str, &argc, &argv) == TCL_OK) 0 0 0
1621 {
1622 0 tofree = argv;
1623 0 ▶ EXTEND(sp, argc);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(sp, argc); 0 0 0
EXTEND(sp, argc); 0 0 0
1624 0 ▶ while (argc--)
Branch: 1 branch
BranchTrueFalseTotal
while (argc--) 0 0 0
1625 0 PUSHs(sv_2mortal(newSVpv(*argv++, 0)));
1626 0 ckfree((char *) tofree);
1627 }
1628  
1629 SV *
1630 Tcl_SetVar(interp, varname, value, flags = 0)
1631 Tcl interp
1632 char * varname
1633 SV * value
1634 int flags
1635 CODE:
1636 0 RETVAL = SvFromTclObj(aTHX_ Tcl_SetVar2Ex(interp, varname, NULL,
1637 TclObjFromSv(aTHX_ value), flags));
1638 OUTPUT:
1639 RETVAL
1640  
1641 SV *
1642 Tcl_SetVar2(interp, varname1, varname2, value, flags = 0)
1643 Tcl interp
1644 char * varname1
1645 char * varname2
1646 SV * value
1647 int flags
1648 CODE:
1649 0 RETVAL = SvFromTclObj(aTHX_ Tcl_SetVar2Ex(interp, varname1, varname2,
1650 TclObjFromSv(aTHX_ value), flags));
1651 OUTPUT:
1652 RETVAL
1653  
1654 SV *
1655 Tcl_GetVar(interp, varname, flags = 0)
1656 Tcl interp
1657 char * varname
1658 int flags
1659 CODE:
1660 0 RETVAL = SvFromTclObj(aTHX_ Tcl_GetVar2Ex(interp, varname, NULL, flags));
1661 OUTPUT:
1662 RETVAL
1663  
1664 SV *
1665 Tcl_GetVar2(interp, varname1, varname2, flags = 0)
1666 Tcl interp
1667 char * varname1
1668 char * varname2
1669 int flags
1670 CODE:
1671 0 RETVAL = SvFromTclObj(aTHX_ Tcl_GetVar2Ex(interp, varname1, varname2, flags));
1672 OUTPUT:
1673 RETVAL
1674  
1675 SV *
1676 Tcl_UnsetVar(interp, varname, flags = 0)
1677 Tcl interp
1678 char * varname
1679 int flags
1680 CODE:
1681 0 ▶ RETVAL = boolSV(Tcl_UnsetVar2(interp, varname, NULL, flags) == TCL_OK);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = boolSV(Tcl_UnsetVar2(interp, varname, NULL, flags) == TCL_OK); 0 0 0
1682 OUTPUT:
1683 RETVAL
1684  
1685 SV *
1686 Tcl_UnsetVar2(interp, varname1, varname2, flags = 0)
1687 Tcl interp
1688 char * varname1
1689 char * varname2
1690 int flags
1691 CODE:
1692 0 ▶ RETVAL = boolSV(Tcl_UnsetVar2(interp, varname1, varname2, flags) == TCL_OK);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = boolSV(Tcl_UnsetVar2(interp, varname1, varname2, flags) == TCL_OK); 0 0 0
1693 OUTPUT:
1694 RETVAL
1695  
1696  
1697 MODULE = Tcl PACKAGE = Tcl::List
1698  
1699 SV*
1700 as_string(SV* sv,...)
1701 PREINIT:
1702 Tcl_Obj* objPtr;
1703 int len;
1704 char *str;
1705 CODE:
1706 0 objPtr = TclObjFromSv(aTHX_ sv);
1707 0 Tcl_IncrRefCount(objPtr);
1708 0 str = Tcl_GetStringFromObj(objPtr, &len);
1709 0 RETVAL = newSVpvn(str, len);
1710 /* should turn on, but let's check this first for efficiency */
1711 0 ▶ if (len && has_highbit(str, len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (len && has_highbit(str, len)) { 0 0 0
if (len && has_highbit(str, len)) { 0 0 0
1712 0 SvUTF8_on(RETVAL);
1713 }
1714 0 ▶ Tcl_DecrRefCount(objPtr);
Branch: 1 branch
BranchTrueFalseTotal
Tcl_DecrRefCount(objPtr); 0 0 0
1715 OUTPUT:
1716 RETVAL
1717  
1718  
1719 MODULE = Tcl PACKAGE = Tcl::Var
1720  
1721 SV *
1722 FETCH(av, key = NULL)
1723 Tcl::Var av
1724 char * key
1725 SV * sv = NO_INIT
1726 Tcl interp = NO_INIT
1727 char * varname1 = NO_INIT
1728 int flags = 0;
1729 CODE:
1730 /*
1731 * This handles both hash and scalar fetches. The blessed object
1732 * passed in is [$interp, $varname, $flags] ($flags optional).
1733 */
1734 0 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 0 0 0
1735 0 ▶ if (AvFILL(av) != 1 && AvFILL(av) != 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (AvFILL(av) != 1 && AvFILL(av) != 2) { 0 0 0
if (AvFILL(av) != 1 && AvFILL(av) != 2) { 0 0 0
1736 0 croak("bad object passed to Tcl::Var::FETCH");
1737 }
1738 0 sv = *av_fetch(av, 0, FALSE);
1739 0 ▶ if (sv_derived_from(sv, "Tcl")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(sv, "Tcl")) { 0 0 0
1740 0 IV tmp = SvIV((SV *) SvRV(sv));
1741 0 interp = INT2PTR(Tcl, tmp);
1742 }
1743 else {
1744 0 croak("bad object passed to Tcl::Var::FETCH");
1745 }
1746 0 ▶ if (AvFILL(av) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (AvFILL(av) == 2) { 0 0 0
1747 0 flags = (int) SvIV(*av_fetch(av, 2, FALSE));
1748 }
1749 0 varname1 = SvPV_nolen(*av_fetch(av, 1, FALSE));
1750 0 RETVAL = SvFromTclObj(aTHX_ Tcl_GetVar2Ex(interp, varname1, key, flags));
1751 OUTPUT:
1752 RETVAL
1753  
1754 void
1755 STORE(av, sv1, sv2 = NULL)
1756 Tcl::Var av
1757 SV * sv1
1758 SV * sv2
1759 SV * sv = NO_INIT
1760 Tcl interp = NO_INIT
1761 char * varname1 = NO_INIT
1762 Tcl_Obj * objPtr = NO_INIT
1763 int flags = 0;
1764 CODE:
1765 /*
1766 * This handles both hash and scalar stores. The blessed object
1767 * passed in is [$interp, $varname, $flags] ($flags optional).
1768 */
1769 0 ▶ if (!initialized) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!initialized) { return; } 0 0 0
1770 0 ▶ if (AvFILL(av) != 1 && AvFILL(av) != 2)
Branch: 2 branches
BranchTrueFalseTotal
if (AvFILL(av) != 1 && AvFILL(av) != 2) 0 0 0
if (AvFILL(av) != 1 && AvFILL(av) != 2) 0 0 0
1771 0 croak("bad object passed to Tcl::Var::STORE");
1772 0 sv = *av_fetch(av, 0, FALSE);
1773 0 ▶ if (sv_derived_from(sv, "Tcl")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(sv, "Tcl")) { 0 0 0
1774 0 IV tmp = SvIV((SV *) SvRV(sv));
1775 0 interp = INT2PTR(Tcl, tmp);
1776 }
1777 else
1778 0 croak("bad object passed to Tcl::Var::STORE");
1779 0 ▶ if (AvFILL(av) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (AvFILL(av) == 2) { 0 0 0
1780 0 flags = (int) SvIV(*av_fetch(av, 2, FALSE));
1781 }
1782 0 varname1 = SvPV_nolen(*av_fetch(av, 1, FALSE));
1783 /*
1784 * HASH: sv1 == key, sv2 == value
1785 * SCALAR: sv1 == value, sv2 NULL
1786 * Tcl_SetVar2Ex will incr refcount
1787 */
1788 0 ▶ if (sv2) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv2) { 0 0 0
1789 0 objPtr = TclObjFromSv(aTHX_ sv2);
1790 0 Tcl_SetVar2Ex(interp, varname1, SvPV_nolen(sv1), objPtr, flags);
1791 }
1792 else {
1793 0 objPtr = TclObjFromSv(aTHX_ sv1);
1794 0 Tcl_SetVar2Ex(interp, varname1, NULL, objPtr, flags);
1795 }
1796  
1797 MODULE = Tcl PACKAGE = Tcl
1798  
1799 BOOT:
1800 {
1801 20 SV *x = GvSV(gv_fetchpv("\030", TRUE, SVt_PV)); /* $^X */
1802 #ifdef USE_TCL_STUBS
1803 20 ▶ if (NpInitialize(aTHX_ x) == TCL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (NpInitialize(aTHX_ x) == TCL_ERROR) { 20 0 20
1804 20 croak("Unable to initialize Tcl");
1805 }
1806 #else
1807 /* Ideally this would be passed the dll instance location. */
1808 Tcl_FindExecutable(x && SvPOK(x) ? SvPV_nolen(x) : NULL);
1809 #if defined(HAVE_TKINIT) && defined(WIN32)
1810 /* HAVE_TKINIT means we're linking Tk statically with tcl.dll
1811 * so we need to perform same initialization as in
1812 * tk/win/tkWin32Dll.c
1813 * (unless all this goes statically into perl.dll; in this case
1814 * handle to perl.dll should be substituted TODO)
1815 * -- VKON
1816 */
1817 TkWinSetHINSTANCE(_hinst);
1818 #endif
1819 #endif
1820 0 initialized = 1;
1821 0 hvInterps = newHV();
1822 }
1823  
1824 0 tclBooleanTypePtr = Tcl_GetObjType("boolean");
1825 0 tclByteArrayTypePtr = Tcl_GetObjType("bytearray");
1826 0 tclDoubleTypePtr = Tcl_GetObjType("double");
1827 0 tclIntTypePtr = Tcl_GetObjType("int");
1828 0 tclListTypePtr = Tcl_GetObjType("list");
1829 0 tclStringTypePtr = Tcl_GetObjType("string");
1830 0 tclWideIntTypePtr = Tcl_GetObjType("wideInt");
1831  
1832 /* set up constant subs */
1833 {
1834 0 HV *stash = gv_stashpvn("Tcl", 3, TRUE);
1835 0 newCONSTSUB(stash, "OK", newSViv(TCL_OK));
1836 0 newCONSTSUB(stash, "ERROR", newSViv(TCL_ERROR));
1837 0 newCONSTSUB(stash, "RETURN", newSViv(TCL_RETURN));
1838 0 newCONSTSUB(stash, "BREAK", newSViv(TCL_BREAK));
1839 0 newCONSTSUB(stash, "CONTINUE", newSViv(TCL_CONTINUE));
1840  
1841 0 newCONSTSUB(stash, "GLOBAL_ONLY", newSViv(TCL_GLOBAL_ONLY));
1842 0 newCONSTSUB(stash, "NAMESPACE_ONLY", newSViv(TCL_NAMESPACE_ONLY));
1843 0 newCONSTSUB(stash, "APPEND_VALUE", newSViv(TCL_APPEND_VALUE));
1844 0 newCONSTSUB(stash, "LIST_ELEMENT", newSViv(TCL_LIST_ELEMENT));
1845 0 newCONSTSUB(stash, "TRACE_READS", newSViv(TCL_TRACE_READS));
1846 0 newCONSTSUB(stash, "TRACE_WRITES", newSViv(TCL_TRACE_WRITES));
1847 0 newCONSTSUB(stash, "TRACE_UNSETS", newSViv(TCL_TRACE_UNSETS));
1848 0 newCONSTSUB(stash, "TRACE_DESTROYED", newSViv(TCL_TRACE_DESTROYED));
1849 0 newCONSTSUB(stash, "INTERP_DESTROYED", newSViv(TCL_INTERP_DESTROYED));
1850 0 newCONSTSUB(stash, "LEAVE_ERR_MSG", newSViv(TCL_LEAVE_ERR_MSG));
1851 0 newCONSTSUB(stash, "TRACE_ARRAY", newSViv(TCL_TRACE_ARRAY));
1852  
1853 0 newCONSTSUB(stash, "LINK_INT", newSViv(TCL_LINK_INT));
1854 0 newCONSTSUB(stash, "LINK_DOUBLE", newSViv(TCL_LINK_DOUBLE));
1855 0 newCONSTSUB(stash, "LINK_BOOLEAN", newSViv(TCL_LINK_BOOLEAN));
1856 0 newCONSTSUB(stash, "LINK_STRING", newSViv(TCL_LINK_STRING));
1857 0 newCONSTSUB(stash, "LINK_READ_ONLY", newSViv(TCL_LINK_READ_ONLY));
1858  
1859 0 newCONSTSUB(stash, "WINDOW_EVENTS", newSViv(TCL_WINDOW_EVENTS));
1860 0 newCONSTSUB(stash, "FILE_EVENTS", newSViv(TCL_FILE_EVENTS));
1861 0 newCONSTSUB(stash, "TIMER_EVENTS", newSViv(TCL_TIMER_EVENTS));
1862 0 newCONSTSUB(stash, "IDLE_EVENTS", newSViv(TCL_IDLE_EVENTS));
1863 0 newCONSTSUB(stash, "ALL_EVENTS", newSViv(TCL_ALL_EVENTS));
1864 0 newCONSTSUB(stash, "DONT_WAIT", newSViv(TCL_DONT_WAIT));
1865  
1866 0 newCONSTSUB(stash, "EVAL_GLOBAL", newSViv(TCL_EVAL_GLOBAL));
1867 0 newCONSTSUB(stash, "EVAL_DIRECT", newSViv(TCL_EVAL_DIRECT));
1868 }
« blib/lib/Tcl.pm ↑ Summary