1
0

sym_misc.c 5.4 KB

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