libtcl.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * tcl.c -- handles:
  22. * libtcl handling
  23. *
  24. */
  25. #include "buildinfo.h"
  26. #include "common.h"
  27. #include "main.h"
  28. #include "dl.h"
  29. #include <bdlib/src/String.h>
  30. #include <bdlib/src/Array.h>
  31. #include "libtcl.h"
  32. #ifndef GENERATING_DEFS
  33. #include ".defs/libtcl_defs.cc"
  34. #endif
  35. #ifdef USE_SCRIPT_TCL
  36. Tcl_Interp *global_interp = NULL;
  37. #endif
  38. void *libtcl_handle = NULL;
  39. static bd::Array<bd::String> my_symbols;
  40. void initialize_binds_tcl();
  41. static int load_symbols(void *handle) {
  42. #ifdef USE_SCRIPT_TCL
  43. DLSYM_GLOBAL(handle, Tcl_Eval);
  44. DLSYM_GLOBAL(handle, Tcl_GetStringResult);
  45. DLSYM_GLOBAL(handle, Tcl_DeleteInterp);
  46. DLSYM_GLOBAL(handle, Tcl_CreateCommand);
  47. DLSYM_GLOBAL(handle, Tcl_CreateInterp);
  48. DLSYM_GLOBAL(handle, Tcl_FindExecutable);
  49. DLSYM_GLOBAL(handle, Tcl_Init);
  50. #endif
  51. return 0;
  52. }
  53. int load_libtcl() {
  54. #ifndef USE_SCRIPT_TCL
  55. sdprintf("Not compiled with TCL support");
  56. return 1;
  57. #else
  58. if (global_interp) {
  59. return 0;
  60. }
  61. #endif
  62. const auto& libs_list(bd::String(
  63. TCLLIB "/lib" TCLLIBFN " "
  64. "libtcl.so "
  65. "libtcl83.so "
  66. "libtcl8.3.so "
  67. "libtcl84.so "
  68. "libtcl8.4.so "
  69. "libtcl85.so "
  70. "libtcl8.5.so "
  71. "libtcl86.so "
  72. "libtcl8.6.so").split(' '));
  73. for (const auto& lib : libs_list) {
  74. dlerror(); // Clear Errors
  75. libtcl_handle = dlopen(lib.c_str(), RTLD_LAZY);
  76. if (libtcl_handle) break;
  77. }
  78. if (!libtcl_handle) {
  79. sdprintf("Unable to find libtcl");
  80. return 1;
  81. }
  82. if (load_symbols(libtcl_handle)) {
  83. fprintf(stderr, STR("\nMissing symbols for libtcl (likely too old)\n"));
  84. return(1);
  85. }
  86. #ifdef USE_SCRIPT_TCL
  87. // create interp
  88. global_interp = Tcl_CreateInterp();
  89. Tcl_FindExecutable(binname);
  90. if (Tcl_Init(global_interp) != TCL_OK) {
  91. sdprintf("Tcl_Init error: %s", Tcl_GetStringResult(global_interp));
  92. return 1;
  93. }
  94. initialize_binds_tcl();
  95. #endif
  96. return 0;
  97. }
  98. #ifdef USE_SCRIPT_TCL
  99. #include "chanprog.h"
  100. static int cmd_privmsg STDVAR {
  101. bd::String str = argv[2];
  102. for (int i = 3; i < argc; ++i)
  103. str += " " + bd::String(argv[i]);
  104. privmsg(argv[1], str, DP_SERVER);
  105. return TCL_OK;
  106. }
  107. void initialize_binds_tcl() {
  108. Tcl_CreateCommand(global_interp, "privmsg", (Tcl_CmdProc*) cmd_privmsg, NULL, NULL);
  109. }
  110. #endif
  111. int unload_libtcl() {
  112. if (libtcl_handle) {
  113. #ifdef USE_SCRIPT_TCL
  114. if (global_interp) {
  115. Tcl_DeleteInterp(global_interp);
  116. global_interp = NULL;
  117. }
  118. #endif
  119. // Cleanup symbol table
  120. for (const auto& symbol : my_symbols) {
  121. dl_symbol_table.remove(symbol);
  122. }
  123. my_symbols.clear();
  124. dlclose(libtcl_handle);
  125. libtcl_handle = NULL;
  126. return 0;
  127. }
  128. return 1;
  129. }
  130. #ifdef USE_SCRIPT_TCL
  131. bd::String tcl_eval(const bd::String& str) {
  132. load_libtcl();
  133. if (!global_interp) return bd::String();
  134. if (Tcl_Eval(global_interp, str.c_str()) == TCL_OK) {
  135. return Tcl_GetStringResult(global_interp);
  136. } else
  137. return tcl_eval("set errorInfo");
  138. return bd::String();
  139. }
  140. #endif
  141. /* vim: set sts=2 sw=2 ts=8 et: */