tclish.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/private.h"
  24. //---------------------------------------------------------
  25. static clish_shell_hooks_t my_hooks =
  26. {
  27. tclish_init_callback,
  28. clish_access_callback,
  29. NULL, /* don't worry about cmd_line callback */
  30. tclish_script_callback,
  31. tclish_fini_callback,
  32. NULL
  33. };
  34. //---------------------------------------------------------
  35. static void
  36. tclish_cookie_init(tclish_cookie_t *cookie,
  37. const char *argv0)
  38. {
  39. /* make sure that the platform specific details are set up */
  40. Tcl_FindExecutable(argv0);
  41. cookie->interp = NULL;
  42. }
  43. //---------------------------------------------------------
  44. static tclish_cookie_t *
  45. tclish_cookie_new(const char *argv0)
  46. {
  47. tclish_cookie_t *cookie = (tclish_cookie_t *)malloc(sizeof(tclish_cookie_t));
  48. if(NULL != cookie)
  49. {
  50. tclish_cookie_init(cookie,argv0);
  51. }
  52. return cookie;
  53. }
  54. //---------------------------------------------------------
  55. int
  56. main(int argc, const char **argv)
  57. {
  58. int status = 0;
  59. tclish_cookie_t *cookie;
  60. clish_startup(argc,argv);
  61. if(argc > 1)
  62. {
  63. int i = 1;
  64. while((0 == status) && argc--)
  65. {
  66. cookie = tclish_cookie_new(argv[0]);
  67. /* run the commands in the file */
  68. status = clish_shell_spawn_from_file(&my_hooks,cookie,argv[i++]);
  69. }
  70. }
  71. else
  72. {
  73. pthread_t pthread;
  74. void *dummy;
  75. cookie = tclish_cookie_new(argv[0]);
  76. /* spawn the shell */
  77. status = clish_shell_spawn(&pthread,NULL,&my_hooks,cookie);
  78. if(-1 != status)
  79. {
  80. pthread_join(pthread,&dummy);
  81. }
  82. }
  83. if(-1 == status)
  84. {
  85. /* something went wrong */
  86. free(cookie);
  87. }
  88. (void)Tcl_FinalizeThread();
  89. clish_shutdown();
  90. return status;
  91. }
  92. //---------------------------------------------------------
  93. #else /* not HAVE_LIBTCL */
  94. #include <stdio.h>
  95. int main(void)
  96. {
  97. printf("TCL not compiled in...\n");
  98. }
  99. #endif /* not HAVE_LIBTCL */