1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
/************************************************
stubs.c - Tcl/Tk stubs support
************************************************/
#include "ruby.h"
#include "stubs.h"
#if !defined(RSTRING_PTR)
#define RSTRING_PTR(s) (RSTRING(s)->ptr)
#define RSTRING_LEN(s) (RSTRING(s)->len)
#endif
#include <tcl.h>
#include <tk.h>
/*------------------------------*/
#ifdef __MACOS__
# include <tkMac.h>
# include <Quickdraw.h>
static int call_macinit = 0;
static void
_macinit()
{
if (!call_macinit) {
tcl_macQdPtr = &qd; /* setup QuickDraw globals */
Tcl_MacSetEventProc(TkMacConvertEvent); /* setup event handler */
call_macinit = 1;
}
}
#endif
/*------------------------------*/
static int nativethread_checked = 0;
static void
_nativethread_consistency_check(ip)
Tcl_Interp *ip;
{
if (nativethread_checked || ip == (Tcl_Interp *)NULL) {
return;
}
/* If the variable "tcl_platform(threaded)" exists,
then the Tcl interpreter was compiled with threads enabled. */
if (Tcl_GetVar2(ip, "tcl_platform", "threaded", TCL_GLOBAL_ONLY) != (char*)NULL) {
#ifdef HAVE_NATIVETHREAD
/* consistent */
#else
rb_warn("Inconsistency. Loaded Tcl/Tk libraries are enabled nativethread-support. But `tcltklib' is not. The inconsistency causes SEGV or other troubles frequently.");
#endif
} else {
#ifdef HAVE_NATIVETHREAD
rb_warning("Inconsistency.`tcltklib' is enabled nativethread-support. But loaded Tcl/Tk libraries are not. (Probably, the inconsistency doesn't cause any troubles.)");
#else
/* consistent */
#endif
}
Tcl_ResetResult(ip);
nativethread_checked = 1;
}
/*------------------------------*/
#if defined USE_TCL_STUBS && defined USE_TK_STUBS
#if defined _WIN32 || defined __CYGWIN__
# ifdef HAVE_RUBY_RUBY_H
# include "ruby/util.h"
# else
# include "util.h"
# endif
# include <windows.h>
typedef HINSTANCE DL_HANDLE;
# define DL_OPEN LoadLibrary
# define DL_SYM GetProcAddress
# define TCL_INDEX 4
# define TK_INDEX 3
# define TCL_NAME "tcl89%s"
# define TK_NAME "tk89%s"
# undef DLEXT
# define DLEXT ".dll"
#elif defined HAVE_DLOPEN
# include <dlfcn.h>
typedef void *DL_HANDLE;
# define DL_OPEN(file) dlopen(file, RTLD_LAZY|RTLD_GLOBAL)
# define DL_SYM dlsym
# define TCL_INDEX 8
# define TK_INDEX 7
# define TCL_NAME "libtcl8.9%s"
# define TK_NAME "libtk8.9%s"
# ifdef __APPLE__
# undef DLEXT
# define DLEXT ".dylib"
# endif
#endif
static DL_HANDLE tcl_dll = (DL_HANDLE)0;
static DL_HANDLE tk_dll = (DL_HANDLE)0;
int
#ifdef HAVE_PROTOTYPES
ruby_open_tcl_dll(char *appname)
#else
ruby_open_tcl_dll(appname)
char *appname;
#endif
{
void (*p_Tcl_FindExecutable)(const char *);
int n;
char *ruby_tcl_dll = 0;
char tcl_name[20];
if (tcl_dll) return TCLTK_STUBS_OK;
ruby_tcl_dll = getenv("RUBY_TCL_DLL");
#if defined _WIN32
if (ruby_tcl_dll) ruby_tcl_dll = ruby_strdup(ruby_tcl_dll);
#endif
if (ruby_tcl_dll) {
tcl_dll = (DL_HANDLE)DL_OPEN(ruby_tcl_dll);
} else {
snprintf(tcl_name, sizeof tcl_name, TCL_NAME, DLEXT);
/* examine from 8.9 to 8.1 */
for (n = '9'; n > '0'; n--) {
tcl_name[TCL_INDEX] = n;
tcl_dll = (DL_HANDLE)DL_OPEN(tcl_name);
if (tcl_dll)
break;
}
}
#if defined _WIN32
if (ruby_tcl_dll) ruby_xfree(ruby_tcl_dll);
#endif
if (!tcl_dll)
return NO_TCL_DLL;
p_Tcl_FindExecutable = (void (*)(const char *))DL_SYM(tcl_dll, "Tcl_FindExecutable");
if (!p_Tcl_FindExecutable)
return NO_FindExecutable;
if (appname) {
p_Tcl_FindExecutable(appname);
} else {
p_Tcl_FindExecutable("ruby");
}
return TCLTK_STUBS_OK;
}
int
ruby_open_tk_dll()
{
int n;
char *ruby_tk_dll = 0;
char tk_name[20];
if (!tcl_dll) {
/* int ret = ruby_open_tcl_dll(RSTRING_PTR(rb_argv0)); */
int ret = ruby_open_tcl_dll(rb_argv0 ? RSTRING_PTR(rb_argv0) : 0);
if (ret != TCLTK_STUBS_OK) return ret;
}
if (tk_dll) return TCLTK_STUBS_OK;
ruby_tk_dll = getenv("RUBY_TK_DLL");
if (ruby_tk_dll) {
tk_dll = (DL_HANDLE)DL_OPEN(ruby_tk_dll);
} else {
snprintf(tk_name, sizeof tk_name, TK_NAME, DLEXT);
/* examine from 8.9 to 8.1 */
for (n = '9'; n > '0'; n--) {
tk_name[TK_INDEX] = n;
tk_dll = (DL_HANDLE)DL_OPEN(tk_name);
if (tk_dll)
break;
}
}
if (!tk_dll)
return NO_TK_DLL;
return TCLTK_STUBS_OK;
}
int
#ifdef HAVE_PROTOTYPES
ruby_open_tcltk_dll(char *appname)
#else
ruby_open_tcltk_dll(appname)
char *appname;
#endif
{
return( ruby_open_tcl_dll(appname) || ruby_open_tk_dll() );
}
int
tcl_stubs_init_p()
{
return(tclStubsPtr != (TclStubs*)NULL);
}
int
tk_stubs_init_p()
{
return(tkStubsPtr != (TkStubs*)NULL);
}
Tcl_Interp *
#ifdef HAVE_PROTOTYPES
ruby_tcl_create_ip_and_stubs_init(int *st)
#else
ruby_tcl_create_ip_and_stubs_init(st)
int *st;
#endif
{
Tcl_Interp *tcl_ip;
if (st) *st = 0;
if (tcl_stubs_init_p()) {
tcl_ip = Tcl_CreateInterp();
if (!tcl_ip) {
if (st) *st = FAIL_CreateInterp;
return (Tcl_Interp*)NULL;
}
_nativethread_consistency_check(tcl_ip);
return tcl_ip;
} else {
Tcl_Interp *(*p_Tcl_CreateInterp)();
Tcl_Interp *(*p_Tcl_DeleteInterp)();
if (!tcl_dll) {
/* int ret = ruby_open_tcl_dll(RSTRING_PTR(rb_argv0)); */
int ret = ruby_open_tcl_dll(rb_argv0 ? RSTRING_PTR(rb_argv0) : 0);
if (ret != TCLTK_STUBS_OK) {
if (st) *st = ret;
return (Tcl_Interp*)NULL;
}
}
p_Tcl_CreateInterp
= (Tcl_Interp *(*)())DL_SYM(tcl_dll, "Tcl_CreateInterp");
if (!p_Tcl_CreateInterp) {
if (st) *st = NO_CreateInterp;
return (Tcl_Interp*)NULL;
}
p_Tcl_DeleteInterp
= (Tcl_Interp *(*)())DL_SYM(tcl_dll, "Tcl_DeleteInterp");
if (!p_Tcl_DeleteInterp) {
if (st) *st = NO_DeleteInterp;
return (Tcl_Interp*)NULL;
}
tcl_ip = (*p_Tcl_CreateInterp)();
if (!tcl_ip) {
if (st) *st = FAIL_CreateInterp;
return (Tcl_Interp*)NULL;
}
if (!Tcl_InitStubs(tcl_ip, "8.1", 0)) {
if (st) *st = FAIL_Tcl_InitStubs;
(*p_Tcl_DeleteInterp)(tcl_ip);
return (Tcl_Interp*)NULL;
}
_nativethread_consistency_check(tcl_ip);
return tcl_ip;
}
}
int
ruby_tcl_stubs_init()
{
int st;
Tcl_Interp *tcl_ip;
if (!tcl_stubs_init_p()) {
tcl_ip = ruby_tcl_create_ip_and_stubs_init(&st);
if (!tcl_ip) return st;
Tcl_DeleteInterp(tcl_ip);
}
return TCLTK_STUBS_OK;
}
int
#ifdef HAVE_PROTOTYPES
ruby_tk_stubs_init(Tcl_Interp *tcl_ip)
#else
ruby_tk_stubs_init(tcl_ip)
Tcl_Interp *tcl_ip;
#endif
{
Tcl_ResetResult(tcl_ip);
if (tk_stubs_init_p()) {
if (Tk_Init(tcl_ip) == TCL_ERROR) {
return FAIL_Tk_Init;
}
} else {
int (*p_Tk_Init)(Tcl_Interp *);
if (!tk_dll) {
int ret = ruby_open_tk_dll();
if (ret != TCLTK_STUBS_OK) return ret;
}
p_Tk_Init = (int (*)(Tcl_Interp *))DL_SYM(tk_dll, "Tk_Init");
if (!p_Tk_Init)
return NO_Tk_Init;
#if defined USE_TK_STUBS && defined TK_FRAMEWORK && defined(__APPLE__)
/*
FIX ME : dirty hack for Mac OS X frameworks.
With stubs, fails to find Resource/Script directory of Tk.framework.
So, teach it to a Tcl interpreter by an environment variable.
e.g. when $tcl_library ==
/Library/Frameworks/Tcl.framwwork/8.5/Resources/Scripts
==> /Library/Frameworks/Tk.framwwork/8.5/Resources/Scripts
*/
if (Tcl_Eval(tcl_ip,
"if {[array get env TK_LIBRARY] == {}} { set env(TK_LIBRARY) [regsub -all -nocase {(t)cl} $tcl_library {\\1k}] }"
) != TCL_OK) {
return FAIL_Tk_Init;
}
#endif
if ((*p_Tk_Init)(tcl_ip) == TCL_ERROR)
return FAIL_Tk_Init;
if (!Tk_InitStubs(tcl_ip, (char *)"8.1", 0))
return FAIL_Tk_InitStubs;
#ifdef __MACOS__
_macinit();
#endif
}
return TCLTK_STUBS_OK;
}
int
#ifdef HAVE_PROTOTYPES
ruby_tk_stubs_safeinit(Tcl_Interp *tcl_ip)
#else
ruby_tk_stubs_safeinit(tcl_ip)
Tcl_Interp *tcl_ip;
#endif
{
Tcl_ResetResult(tcl_ip);
if (tk_stubs_init_p()) {
if (Tk_SafeInit(tcl_ip) == TCL_ERROR)
return FAIL_Tk_Init;
} else {
int (*p_Tk_SafeInit)(Tcl_Interp *);
if (!tk_dll) {
int ret = ruby_open_tk_dll();
if (ret != TCLTK_STUBS_OK) return ret;
}
p_Tk_SafeInit = (int (*)(Tcl_Interp *))DL_SYM(tk_dll, "Tk_SafeInit");
if (!p_Tk_SafeInit)
return NO_Tk_Init;
if ((*p_Tk_SafeInit)(tcl_ip) == TCL_ERROR)
return FAIL_Tk_Init;
if (!Tk_InitStubs(tcl_ip, (char *)"8.1", 0))
return FAIL_Tk_InitStubs;
#ifdef __MACOS__
_macinit();
#endif
}
return TCLTK_STUBS_OK;
}
int
ruby_tcltk_stubs()
{
int st;
Tcl_Interp *tcl_ip;
/* st = ruby_open_tcltk_dll(RSTRING_PTR(rb_argv0)); */
st = ruby_open_tcltk_dll(rb_argv0 ? RSTRING_PTR(rb_argv0) : 0);
switch(st) {
case NO_FindExecutable:
return -7;
case NO_TCL_DLL:
case NO_TK_DLL:
return -1;
}
tcl_ip = ruby_tcl_create_ip_and_stubs_init(&st);
if (!tcl_ip) {
switch(st) {
case NO_CreateInterp:
case NO_DeleteInterp:
return -2;
case FAIL_CreateInterp:
return -3;
case FAIL_Tcl_InitStubs:
return -5;
}
}
st = ruby_tk_stubs_init(tcl_ip);
switch(st) {
case NO_Tk_Init:
Tcl_DeleteInterp(tcl_ip);
return -4;
case FAIL_Tk_Init:
case FAIL_Tk_InitStubs:
Tcl_DeleteInterp(tcl_ip);
return -6;
}
Tcl_DeleteInterp(tcl_ip);
return 0;
}
/*###################################################*/
#else /* ! USE_TCL_STUBS || ! USE_TK_STUBS) */
/*###################################################*/
static int open_tcl_dll = 0;
static int call_tk_stubs_init = 0;
int
#ifdef HAVE_PROTOTYPES
ruby_open_tcl_dll(char *appname)
#else
ruby_open_tcl_dll(appname)
char *appname;
#endif
{
if (appname) {
Tcl_FindExecutable(appname);
} else {
Tcl_FindExecutable("ruby");
}
open_tcl_dll = 1;
return TCLTK_STUBS_OK;
}
int
ruby_open_tk_dll()
{
if (!open_tcl_dll) {
/* ruby_open_tcl_dll(RSTRING_PTR(rb_argv0)); */
ruby_open_tcl_dll(rb_argv0 ? RSTRING_PTR(rb_argv0) : 0);
}
return TCLTK_STUBS_OK;
}
int
#ifdef HAVE_PROTOTYPES
ruby_open_tcltk_dll(char *appname)
#else
ruby_open_tcltk_dll(appname)
char *appname;
#endif
{
return( ruby_open_tcl_dll(appname) || ruby_open_tk_dll() );
}
int
tcl_stubs_init_p()
{
return 1;
}
int
tk_stubs_init_p()
{
return call_tk_stubs_init;
}
Tcl_Interp *
#ifdef HAVE_PROTOTYPES
ruby_tcl_create_ip_and_stubs_init(int *st)
#else
ruby_tcl_create_ip_and_stubs_init(st)
int *st;
#endif
{
Tcl_Interp *tcl_ip;
if (!open_tcl_dll) {
/* ruby_open_tcl_dll(RSTRING_PTR(rb_argv0)); */
ruby_open_tcl_dll(rb_argv0 ? RSTRING_PTR(rb_argv0) : 0);
}
if (st) *st = 0;
tcl_ip = Tcl_CreateInterp();
if (!tcl_ip) {
if (st) *st = FAIL_CreateInterp;
return (Tcl_Interp*)NULL;
}
_nativethread_consistency_check(tcl_ip);
return tcl_ip;
}
int
ruby_tcl_stubs_init()
{
return TCLTK_STUBS_OK;
}
int
#ifdef HAVE_PROTOTYPES
ruby_tk_stubs_init(Tcl_Interp *tcl_ip)
#else
ruby_tk_stubs_init(tcl_ip)
Tcl_Interp *tcl_ip;
#endif
{
if (Tk_Init(tcl_ip) == TCL_ERROR)
return FAIL_Tk_Init;
if (!call_tk_stubs_init) {
#ifdef __MACOS__
_macinit();
#endif
call_tk_stubs_init = 1;
}
return TCLTK_STUBS_OK;
}
int
#ifdef HAVE_PROTOTYPES
ruby_tk_stubs_safeinit(Tcl_Interp *tcl_ip)
#else
ruby_tk_stubs_safeinit(tcl_ip)
Tcl_Interp *tcl_ip;
#endif
{
#if TCL_MAJOR_VERSION >= 8
if (Tk_SafeInit(tcl_ip) == TCL_ERROR)
return FAIL_Tk_Init;
if (!call_tk_stubs_init) {
#ifdef __MACOS__
_macinit();
#endif
call_tk_stubs_init = 1;
}
return TCLTK_STUBS_OK;
#else /* TCL_MAJOR_VERSION < 8 */
return FAIL_Tk_Init;
#endif
}
int
ruby_tcltk_stubs()
{
/* Tcl_FindExecutable(RSTRING_PTR(rb_argv0)); */
Tcl_FindExecutable(rb_argv0 ? RSTRING_PTR(rb_argv0) : 0);
return 0;
}
#endif
|