libtcl.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "common.h"
  26. #include "main.h"
  27. #include "dl.h"
  28. #include <bdlib/src/String.h>
  29. #include <bdlib/src/Array.h>
  30. #include "libtcl.h"
  31. #include ".defs/libtcl_defs.cc"
  32. #ifdef USE_SCRIPT_TCL
  33. Tcl_Interp *global_interp = NULL;
  34. #endif
  35. void *libtcl_handle = NULL;
  36. static bd::Array<bd::String> my_symbols;
  37. void initialize_binds_tcl();
  38. static int load_symbols(void *handle) {
  39. #ifdef USE_SCRIPT_TCL
  40. const char *dlsym_error = NULL;
  41. DLSYM_GLOBAL(handle, Tcl_Eval);
  42. DLSYM_GLOBAL(handle, Tcl_GetStringResult);
  43. DLSYM_GLOBAL(handle, Tcl_DeleteInterp);
  44. DLSYM_GLOBAL(handle, Tcl_CreateCommand);
  45. DLSYM_GLOBAL(handle, Tcl_CreateInterp);
  46. DLSYM_GLOBAL(handle, Tcl_FindExecutable);
  47. DLSYM_GLOBAL(handle, Tcl_Init);
  48. #endif
  49. return 0;
  50. }
  51. int load_libtcl() {
  52. #ifndef USE_SCRIPT_TCL
  53. sdprintf("Not compiled with TCL support");
  54. return 1;
  55. #else
  56. if (global_interp) {
  57. return 0;
  58. }
  59. #endif
  60. bd::Array<bd::String> libs_list(bd::String("libtcl.so libtcl83.so libtcl8.3.so libtcl84.so libtcl8.4.so libtcl85.so libtcl8.5.so libtcl86.so libtcl8.6.so").split(' '));
  61. for (size_t i = 0; i < libs_list.length(); ++i) {
  62. dlerror(); // Clear Errors
  63. libtcl_handle = dlopen(bd::String(libs_list[i]).c_str(), RTLD_LAZY);
  64. if (libtcl_handle) break;
  65. }
  66. if (!libtcl_handle) {
  67. sdprintf("Unable to find libtcl");
  68. return 1;
  69. }
  70. if (load_symbols(libtcl_handle)) {
  71. fprintf(stderr, STR("Missing symbols for libtcl (likely too old)\n"));
  72. return(1);
  73. }
  74. #ifdef USE_SCRIPT_TCL
  75. // create interp
  76. global_interp = Tcl_CreateInterp();
  77. Tcl_FindExecutable(binname);
  78. if (Tcl_Init(global_interp) != TCL_OK) {
  79. sdprintf("Tcl_Init error: %s", Tcl_GetStringResult(global_interp));
  80. return 1;
  81. }
  82. initialize_binds_tcl();
  83. #endif
  84. return 0;
  85. }
  86. #ifdef USE_SCRIPT_TCL
  87. #include "chanprog.h"
  88. static int cmd_privmsg STDVAR {
  89. bd::String str = argv[2];
  90. for (int i = 3; i < argc; ++i)
  91. str += " " + bd::String(argv[i]);
  92. privmsg(argv[1], str, DP_SERVER);
  93. return TCL_OK;
  94. }
  95. void initialize_binds_tcl() {
  96. Tcl_CreateCommand(global_interp, "privmsg", (Tcl_CmdProc*) cmd_privmsg, NULL, NULL);
  97. }
  98. #endif
  99. int unload_libtcl() {
  100. if (libtcl_handle) {
  101. #ifdef USE_SCRIPT_TCL
  102. if (global_interp) {
  103. Tcl_DeleteInterp(global_interp);
  104. global_interp = NULL;
  105. }
  106. #endif
  107. // Cleanup symbol table
  108. for (size_t i = 0; i < my_symbols.length(); ++i) {
  109. dl_symbol_table.remove(my_symbols[i]);
  110. static_cast<bd::String>(my_symbols[i]).clear();
  111. }
  112. my_symbols.clear();
  113. dlclose(libtcl_handle);
  114. libtcl_handle = NULL;
  115. return 0;
  116. }
  117. return 1;
  118. }
  119. #ifdef USE_SCRIPT_TCL
  120. bd::String tcl_eval(const bd::String& str) {
  121. load_libtcl();
  122. if (!global_interp) return bd::String();
  123. if (Tcl_Eval(global_interp, str.c_str()) == TCL_OK) {
  124. return Tcl_GetStringResult(global_interp);
  125. } else
  126. return tcl_eval("set errorInfo");
  127. return bd::String();
  128. }
  129. #endif
  130. /* vim: set sts=2 sw=2 ts=8 et: */