tclish.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //-------------------------------------
  2. // tclish.cpp
  3. //
  4. // A simple client for libclish using
  5. // a TCL interpreter.
  6. //
  7. // (T)CL
  8. // (C)ommand
  9. // (L)ine
  10. // (I)nterface
  11. // (Sh)ell
  12. //
  13. // Pronounced: Ticklish
  14. //-------------------------------------
  15. #ifdef HAVE_CONFIG_H
  16. #include "config.h"
  17. #endif /* HAVE_CONFIG_H */
  18. #ifdef HAVE_LIBTCL
  19. #include <assert.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <tcl.h>
  23. #include "clish/shell.h"
  24. #include "clish/internal.h"
  25. #include "tcl_private.h"
  26. //---------------------------------------------------------
  27. static clish_shell_hooks_t my_hooks =
  28. {
  29. tclish_init_callback,
  30. clish_access_callback,
  31. NULL, /* don't worry about cmd_line callback */
  32. tclish_script_callback,
  33. tclish_fini_callback,
  34. NULL, /* don't worry about config callback */
  35. NULL
  36. };
  37. //---------------------------------------------------------
  38. static void
  39. tclish_cookie_init(tclish_cookie_t *cookie,
  40. const char *argv0)
  41. {
  42. /* make sure that the platform specific details are set up */
  43. Tcl_FindExecutable(argv0);
  44. cookie->interp = NULL;
  45. }
  46. //---------------------------------------------------------
  47. static tclish_cookie_t *
  48. tclish_cookie_new(const char *argv0)
  49. {
  50. tclish_cookie_t *cookie = (tclish_cookie_t *)malloc(sizeof(tclish_cookie_t));
  51. if(NULL != cookie)
  52. {
  53. tclish_cookie_init(cookie,argv0);
  54. }
  55. return cookie;
  56. }
  57. //---------------------------------------------------------
  58. int
  59. main(int argc, const char **argv)
  60. {
  61. tclish_cookie_t *cookie;
  62. bool_t running;
  63. int result = -1;
  64. clish_shell_t * shell;
  65. bool_t lockless = BOOL_FALSE;
  66. bool_t stop_on_error = BOOL_FALSE;
  67. const char *xml_path = getenv("CLISH_PATH");
  68. const char *view = getenv("CLISH_VIEW");
  69. const char *viewid = getenv("CLISH_VIEWID");
  70. cookie = tclish_cookie_new(argv[0]);
  71. /* Create shell instance */
  72. shell = clish_shell_new(&my_hooks, cookie, NULL, stdout, stop_on_error);
  73. if (!shell) {
  74. fprintf(stderr, "Cannot run clish.\n");
  75. return -1;
  76. }
  77. /* Load the XML files */
  78. clish_shell_load_scheme(shell, xml_path);
  79. /* Set communication to the konfd */
  80. // clish_shell__set_socket(shell, socket_path);
  81. /* Set lockless mode */
  82. if (lockless)
  83. clish_shell__set_lockfile(shell, NULL);
  84. /* Set startup view */
  85. if (view)
  86. clish_shell__set_startup_view(shell, view);
  87. /* Set startup viewid */
  88. if (viewid)
  89. clish_shell__set_startup_viewid(shell, viewid);
  90. /* Execute startup */
  91. running = clish_shell_startup(shell);
  92. if (!running) {
  93. fprintf(stderr, "Cannot startup clish.\n");
  94. clish_shell_delete(shell);
  95. return -1;
  96. }
  97. if(argc > 1) {
  98. int i;
  99. /* Run the commands from the files */
  100. for (i = argc - 1; i >= 1; i--)
  101. clish_shell_push_file(shell, argv[i], stop_on_error);
  102. } else {
  103. /* The interactive shell */
  104. clish_shell_push_fd(shell, fdopen(dup(fileno(stdin)), "r"), stop_on_error);
  105. }
  106. result = clish_shell_loop(shell);
  107. /* Cleanup */
  108. clish_shell_delete(shell);
  109. free(cookie);
  110. (void)Tcl_FinalizeThread();
  111. return result ? 0 : -1;
  112. }
  113. //---------------------------------------------------------
  114. #else /* not HAVE_LIBTCL */
  115. #include <stdio.h>
  116. int main(void)
  117. {
  118. printf("TCL not compiled in...\n");
  119. }
  120. #endif /* not HAVE_LIBTCL */