1
0

sym_misc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * sym_navy.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include "lub/argv.h"
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <sys/file.h>
  16. #include <signal.h>
  17. #include <fcntl.h>
  18. /*----------------------------------------------------------- */
  19. /* Terminate the current shell session */
  20. CLISH_PLUGIN_SYM(clish_close)
  21. {
  22. clish_shell_t *this = clish_context__get_shell(clish_context);
  23. clish_shell__set_state(this, SHELL_STATE_CLOSING);
  24. script = script; /* Happy compiler */
  25. out = out; /* Happy compiler */
  26. return 0;
  27. }
  28. /*----------------------------------------------------------- */
  29. /*
  30. Open a file and interpret it as a script in the context of a new
  31. thread. Whether the script continues after command, but not script,
  32. errors depends on the value of the stop_on_error flag.
  33. */
  34. static int clish_source_internal(clish_context_t *context,
  35. const char *fn, bool_t stop_on_error)
  36. {
  37. int result = -1;
  38. const char *filename = fn;
  39. struct stat fileStat;
  40. /* the exception proves the rule... */
  41. clish_shell_t *this = clish_context__get_shell(context);
  42. /*
  43. * Check file specified is not a directory
  44. */
  45. if ((0 == stat((char *)filename, &fileStat)) &&
  46. (!S_ISDIR(fileStat.st_mode))) {
  47. /*
  48. * push this file onto the file stack associated with this
  49. * session. This will be closed by clish_shell_pop_file()
  50. * when it is finished with.
  51. */
  52. result = clish_shell_push_file(this, filename,
  53. stop_on_error);
  54. }
  55. return result ? -1 : 0;
  56. }
  57. /*----------------------------------------------------------- */
  58. /*
  59. Open a file and interpret it as a script in the context of a new
  60. thread. Invoking a script in this way will cause the script to
  61. stop on the first error
  62. */
  63. CLISH_PLUGIN_SYM(clish_source)
  64. {
  65. clish_context_t *context = (clish_context_t *)clish_context;
  66. out = out; /* Happy compiler */
  67. return (clish_source_internal(context, script, BOOL_TRUE));
  68. }
  69. /*----------------------------------------------------------- */
  70. /*
  71. Open a file and interpret it as a script in the context of a new
  72. thread. Invoking a script in this way will cause the script to
  73. continue after command, but not script, errors.
  74. */
  75. CLISH_PLUGIN_SYM(clish_source_nostop)
  76. {
  77. clish_context_t *context = (clish_context_t *)clish_context;
  78. out = out; /* Happy compiler */
  79. return (clish_source_internal(context, script, BOOL_FALSE));
  80. }
  81. /*----------------------------------------------------------- */
  82. /*
  83. Show the shell overview
  84. */
  85. CLISH_PLUGIN_SYM(clish_overview)
  86. {
  87. clish_shell_t *this = clish_context__get_shell(clish_context);
  88. tinyrl_t *tinyrl = clish_shell__get_tinyrl(this);
  89. tinyrl_printf(tinyrl, "%s\n", clish_shell__get_overview(this));
  90. script = script; /* Happy compiler */
  91. out = out; /* Happy compiler */
  92. return 0;
  93. }
  94. /*----------------------------------------------------------- */
  95. CLISH_PLUGIN_SYM(clish_history)
  96. {
  97. clish_shell_t *this = clish_context__get_shell(clish_context);
  98. tinyrl_t *tinyrl = clish_shell__get_tinyrl(this);
  99. tinyrl_history_t *history = tinyrl__get_history(tinyrl);
  100. tinyrl_history_iterator_t iter;
  101. const tinyrl_history_entry_t *entry;
  102. unsigned limit = 0;
  103. const char *arg = script;
  104. if (arg && ('\0' != *arg)) {
  105. limit = (unsigned)atoi(arg);
  106. if (0 == limit) {
  107. /* unlimit the history list */
  108. (void)tinyrl_history_unstifle(history);
  109. } else {
  110. /* limit the scope of the history list */
  111. tinyrl_history_stifle(history, limit);
  112. }
  113. }
  114. for (entry = tinyrl_history_getfirst(history, &iter);
  115. entry; entry = tinyrl_history_getnext(&iter)) {
  116. /* dump the details of this entry */
  117. tinyrl_printf(tinyrl,
  118. "%5d %s\n",
  119. tinyrl_history_entry__get_index(entry),
  120. tinyrl_history_entry__get_line(entry));
  121. }
  122. out = out; /* Happy compiler */
  123. return 0;
  124. }
  125. /*----------------------------------------------------------- */
  126. /*
  127. * Find out the previous view in the stack and go to it
  128. */
  129. CLISH_PLUGIN_SYM(clish_nested_up)
  130. {
  131. clish_shell_t *this = clish_context__get_shell(clish_context);
  132. unsigned int depth;
  133. if (!this)
  134. return -1;
  135. /* If depth=0 than exit */
  136. if ((depth = clish_shell__get_depth(this)) == 0) {
  137. clish_shell__set_state(this, SHELL_STATE_CLOSING);
  138. return 0;
  139. }
  140. depth--;
  141. clish_shell__set_depth(this, depth);
  142. script = script; /* Happy compiler */
  143. out = out; /* Happy compiler */
  144. return 0;
  145. }
  146. /*----------------------------------------------------------- */
  147. /*
  148. * Builtin: NOP function
  149. */
  150. CLISH_PLUGIN_SYM(clish_nop)
  151. {
  152. script = script; /* Happy compiler */
  153. out = out; /* Happy compiler */
  154. clish_context = clish_context; /* Happy compiler */
  155. return 0;
  156. }
  157. /*----------------------------------------------------------- */
  158. /*
  159. * Builtin: Set watchdog timeout. The "0" to turn watchdog off.
  160. */
  161. CLISH_PLUGIN_SYM(clish_wdog)
  162. {
  163. const char *arg = script;
  164. clish_shell_t *this = clish_context__get_shell(clish_context);
  165. /* Turn off watchdog if no args */
  166. if (!arg || ('\0' == *arg)) {
  167. clish_shell__set_wdog_timeout(this, 0);
  168. return 0;
  169. }
  170. clish_shell__set_wdog_timeout(this, (unsigned int)atoi(arg));
  171. out = out; /* Happy compiler */
  172. return 0;
  173. }
  174. /*--------------------------------------------------------- */
  175. /*
  176. * Get the ACTION context as a macros
  177. */
  178. CLISH_PLUGIN_SYM(clish_macros)
  179. {
  180. if (!script) /* Nothing to do */
  181. return 0;
  182. *out = lub_string_dup(script);
  183. clish_context = clish_context; /* Happy compiler */
  184. return 0;
  185. }
  186. /*----------------------------------------------------------- */