1
0

sym_misc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * sym_navy.c
  3. */
  4. #include "private.h"
  5. #include "lub/ctype.h"
  6. #include "lub/string.h"
  7. #include "lub/argv.h"
  8. #include "lub/conv.h"
  9. #include <assert.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <errno.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/file.h>
  18. #include <signal.h>
  19. #include <fcntl.h>
  20. /*----------------------------------------------------------- */
  21. /* Terminate the current shell session */
  22. CLISH_PLUGIN_SYM(clish_close)
  23. {
  24. clish_shell_t *this = clish_context__get_shell(clish_context);
  25. clish_shell__set_state(this, SHELL_STATE_CLOSING);
  26. script = script; /* Happy compiler */
  27. out = out; /* Happy compiler */
  28. return 0;
  29. }
  30. /*----------------------------------------------------------- */
  31. /*
  32. Open a file and interpret it as a script in the context of a new
  33. thread. Whether the script continues after command, but not script,
  34. errors depends on the value of the stop_on_error flag.
  35. */
  36. static int clish_source_internal(clish_context_t *context,
  37. const char *fn, int stop_on_error)
  38. {
  39. int result = -1;
  40. const char *filename = fn;
  41. struct stat fileStat;
  42. /* the exception proves the rule... */
  43. clish_shell_t *this = clish_context__get_shell(context);
  44. /*
  45. * Check file specified is not a directory
  46. */
  47. if ((0 == stat((char *)filename, &fileStat)) &&
  48. (!S_ISDIR(fileStat.st_mode))) {
  49. /*
  50. * push this file onto the file stack associated with this
  51. * session. This will be closed by clish_shell_pop_file()
  52. * when it is finished with.
  53. */
  54. result = clish_shell_push_file(this, filename,
  55. stop_on_error);
  56. }
  57. return result ? -1 : 0;
  58. }
  59. /*----------------------------------------------------------- */
  60. /*
  61. Open a file and interpret it as a script in the context of a new
  62. thread. Invoking a script in this way will cause the script to
  63. stop on the first error
  64. */
  65. CLISH_PLUGIN_SYM(clish_source)
  66. {
  67. clish_context_t *context = (clish_context_t *)clish_context;
  68. out = out; /* Happy compiler */
  69. return (clish_source_internal(context, script, 1));
  70. }
  71. /*----------------------------------------------------------- */
  72. /*
  73. Open a file and interpret it as a script in the context of a new
  74. thread. Invoking a script in this way will cause the script to
  75. continue after command, but not script, errors.
  76. */
  77. CLISH_PLUGIN_SYM(clish_source_nostop)
  78. {
  79. clish_context_t *context = (clish_context_t *)clish_context;
  80. out = out; /* Happy compiler */
  81. return (clish_source_internal(context, script, 0));
  82. }
  83. /*----------------------------------------------------------- */
  84. /*
  85. Show the shell overview
  86. */
  87. CLISH_PLUGIN_SYM(clish_overview)
  88. {
  89. clish_shell_t *this = clish_context__get_shell(clish_context);
  90. tinyrl_t *tinyrl = clish_shell__get_tinyrl(this);
  91. tinyrl_printf(tinyrl, "%s\n", clish_shell__get_overview(this));
  92. script = script; /* Happy compiler */
  93. out = out; /* Happy compiler */
  94. return 0;
  95. }
  96. /*----------------------------------------------------------- */
  97. CLISH_PLUGIN_SYM(clish_history)
  98. {
  99. clish_shell_t *this = clish_context__get_shell(clish_context);
  100. tinyrl_t *tinyrl = clish_shell__get_tinyrl(this);
  101. tinyrl_history_t *history = tinyrl__get_history(tinyrl);
  102. tinyrl_history_iterator_t iter;
  103. const tinyrl_history_entry_t *entry;
  104. unsigned int limit = 0;
  105. const char *arg = script;
  106. if (arg && ('\0' != *arg)) {
  107. lub_conv_atoui(arg, &limit, 0);
  108. if (0 == limit) {
  109. /* unlimit the history list */
  110. (void)tinyrl_history_unstifle(history);
  111. } else {
  112. /* limit the scope of the history list */
  113. tinyrl_history_stifle(history, limit);
  114. }
  115. }
  116. for (entry = tinyrl_history_getfirst(history, &iter);
  117. entry; entry = tinyrl_history_getnext(&iter)) {
  118. /* dump the details of this entry */
  119. tinyrl_printf(tinyrl,
  120. "%5d %s\n",
  121. tinyrl_history_entry__get_index(entry),
  122. tinyrl_history_entry__get_line(entry));
  123. }
  124. out = out; /* Happy compiler */
  125. return 0;
  126. }
  127. /*----------------------------------------------------------- */
  128. /*
  129. * Find out the previous view in the stack and go to it
  130. */
  131. CLISH_PLUGIN_SYM(clish_nested_up)
  132. {
  133. clish_shell_t *this = clish_context__get_shell(clish_context);
  134. unsigned int depth;
  135. if (!this)
  136. return -1;
  137. /* If depth=0 then exit */
  138. if (((depth = clish_shell__get_depth(this)) == 0) ||
  139. !clish_shell__set_depth(this, --depth)) {
  140. clish_shell__set_state(this, SHELL_STATE_CLOSING);
  141. return 0;
  142. }
  143. script = script; /* Happy compiler */
  144. out = out; /* Happy compiler */
  145. return 0;
  146. }
  147. /*----------------------------------------------------------- */
  148. /*
  149. * Builtin: NOP function
  150. */
  151. CLISH_PLUGIN_SYM(clish_nop)
  152. {
  153. script = script; /* Happy compiler */
  154. out = out; /* Happy compiler */
  155. clish_context = clish_context; /* Happy compiler */
  156. return 0;
  157. }
  158. /*----------------------------------------------------------- */
  159. /*
  160. * Builtin: Set watchdog timeout. The "0" to turn watchdog off.
  161. */
  162. CLISH_PLUGIN_SYM(clish_wdog)
  163. {
  164. const char *arg = script;
  165. clish_shell_t *this = clish_context__get_shell(clish_context);
  166. unsigned int wdto = 0;
  167. /* Turn off watchdog if no args */
  168. if (!arg || ('\0' == *arg)) {
  169. clish_shell__set_wdog_timeout(this, 0);
  170. return 0;
  171. }
  172. lub_conv_atoui(arg, &wdto, 0);
  173. clish_shell__set_wdog_timeout(this, wdto);
  174. out = out; /* Happy compiler */
  175. return 0;
  176. }
  177. /*--------------------------------------------------------- */
  178. /*
  179. * Get the ACTION context as a macros
  180. */
  181. CLISH_PLUGIN_SYM(clish_macros)
  182. {
  183. if (!script) /* Nothing to do */
  184. return 0;
  185. *out = lub_string_dup(script);
  186. clish_context = clish_context; /* Happy compiler */
  187. return 0;
  188. }
  189. /*----------------------------------------------------------- */
  190. CLISH_PLUGIN_SYM(clish_machine_interface)
  191. {
  192. clish_shell_t *this = clish_context__get_shell(clish_context);
  193. clish_shell_set_machine_interface(this);
  194. script = script; /* Happy compiler */
  195. out = out; /* Happy compiler */
  196. return 0;
  197. }
  198. /*----------------------------------------------------------- */
  199. CLISH_PLUGIN_SYM(clish_human_interface)
  200. {
  201. clish_shell_t *this = clish_context__get_shell(clish_context);
  202. clish_shell_set_human_interface(this);
  203. script = script; /* Happy compiler */
  204. out = out; /* Happy compiler */
  205. return 0;
  206. }
  207. /*----------------------------------------------------------- */
  208. /*
  209. * Builtin: Print script
  210. */
  211. CLISH_PLUGIN_SYM(clish_print_script)
  212. {
  213. if (!script)
  214. return 0;
  215. printf("%s\n", script);
  216. out = out; /* Happy compiler */
  217. clish_context = clish_context; /* Happy compiler */
  218. return 0;
  219. }
  220. /*----------------------------------------------------------- */
  221. /*
  222. * Builtin: Print param
  223. */
  224. CLISH_PLUGIN_SYM(clish_print_var)
  225. {
  226. char *str = NULL;
  227. char *copy = NULL;
  228. char *varname = NULL;
  229. char *t = NULL;
  230. // Script contains variable name
  231. if (!script)
  232. return 0;
  233. // Remove all spaces from var name
  234. copy = lub_string_dup(script);
  235. varname = copy;
  236. while (*varname && lub_ctype_isspace(*varname))
  237. varname++;
  238. t = varname;
  239. while (*t && !lub_ctype_isspace(*t))
  240. t++;
  241. *t = '\0';
  242. str = clish_shell_expand_var(varname, clish_context);
  243. lub_string_free(copy);
  244. if (!str)
  245. return 0;
  246. printf("%s\n", str);
  247. lub_string_free(str);
  248. out = out; /* Happy compiler */
  249. clish_context = clish_context; /* Happy compiler */
  250. return 0;
  251. }
  252. /*----------------------------------------------------------- */