libtcl.cc 3.7 KB

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