1
0

sym_misc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. return 0;
  25. }
  26. /*----------------------------------------------------------- */
  27. /*
  28. Open a file and interpret it as a script in the context of a new
  29. thread. Whether the script continues after command, but not script,
  30. errors depends on the value of the stop_on_error flag.
  31. */
  32. static int clish_source_internal(clish_context_t *context,
  33. const char *fn, bool_t stop_on_error)
  34. {
  35. int result = -1;
  36. const char *filename = fn;
  37. struct stat fileStat;
  38. /* the exception proves the rule... */
  39. clish_shell_t *this = clish_context__get_shell(context);
  40. /*
  41. * Check file specified is not a directory
  42. */
  43. if ((0 == stat((char *)filename, &fileStat)) &&
  44. (!S_ISDIR(fileStat.st_mode))) {
  45. /*
  46. * push this file onto the file stack associated with this
  47. * session. This will be closed by clish_shell_pop_file()
  48. * when it is finished with.
  49. */
  50. result = clish_shell_push_file(this, filename,
  51. stop_on_error);
  52. }
  53. return result ? -1 : 0;
  54. }
  55. /*----------------------------------------------------------- */
  56. /*
  57. Open a file and interpret it as a script in the context of a new
  58. thread. Invoking a script in this way will cause the script to
  59. stop on the first error
  60. */
  61. CLISH_PLUGIN_SYM(clish_source)
  62. {
  63. clish_context_t *context = (clish_context_t *)clish_context;
  64. return (clish_source_internal(context, script, BOOL_TRUE));
  65. }
  66. /*----------------------------------------------------------- */
  67. /*
  68. Open a file and interpret it as a script in the context of a new
  69. thread. Invoking a script in this way will cause the script to
  70. continue after command, but not script, errors.
  71. */
  72. CLISH_PLUGIN_SYM(clish_source_nostop)
  73. {
  74. clish_context_t *context = (clish_context_t *)clish_context;
  75. return (clish_source_internal(context, script, BOOL_FALSE));
  76. }
  77. /*----------------------------------------------------------- */
  78. /*
  79. Show the shell overview
  80. */
  81. CLISH_PLUGIN_SYM(clish_overview)
  82. {
  83. clish_shell_t *this = clish_context__get_shell(clish_context);
  84. tinyrl_t *tinyrl = clish_shell__get_tinyrl(this);
  85. tinyrl_printf(tinyrl, "%s\n", clish_shell__get_overview(this));
  86. return 0;
  87. }
  88. /*----------------------------------------------------------- */
  89. CLISH_PLUGIN_SYM(clish_history)
  90. {
  91. clish_shell_t *this = clish_context__get_shell(clish_context);
  92. tinyrl_t *tinyrl = clish_shell__get_tinyrl(this);
  93. tinyrl_history_t *history = tinyrl__get_history(tinyrl);
  94. tinyrl_history_iterator_t iter;
  95. const tinyrl_history_entry_t *entry;
  96. unsigned limit = 0;
  97. const char *arg = script;
  98. if (arg && ('\0' != *arg)) {
  99. limit = (unsigned)atoi(arg);
  100. if (0 == limit) {
  101. /* unlimit the history list */
  102. (void)tinyrl_history_unstifle(history);
  103. } else {
  104. /* limit the scope of the history list */
  105. tinyrl_history_stifle(history, limit);
  106. }
  107. }
  108. for (entry = tinyrl_history_getfirst(history, &iter);
  109. entry; entry = tinyrl_history_getnext(&iter)) {
  110. /* dump the details of this entry */
  111. tinyrl_printf(tinyrl,
  112. "%5d %s\n",
  113. tinyrl_history_entry__get_index(entry),
  114. tinyrl_history_entry__get_line(entry));
  115. }
  116. return 0;
  117. }
  118. /*----------------------------------------------------------- */
  119. /*
  120. * Find out the previous view in the stack and go to it
  121. */
  122. CLISH_PLUGIN_SYM(clish_nested_up)
  123. {
  124. clish_shell_t *this = clish_context__get_shell(clish_context);
  125. unsigned int depth;
  126. if (!this)
  127. return -1;
  128. /* If depth=0 than exit */
  129. if ((depth = clish_shell__get_depth(this)) == 0) {
  130. clish_shell__set_state(this, SHELL_STATE_CLOSING);
  131. return 0;
  132. }
  133. depth--;
  134. clish_shell__set_depth(this, depth);
  135. return 0;
  136. }
  137. /*----------------------------------------------------------- */
  138. /*
  139. * Builtin: NOP function
  140. */
  141. CLISH_PLUGIN_SYM(clish_nop)
  142. {
  143. return 0;
  144. }
  145. /*----------------------------------------------------------- */
  146. /*
  147. * Builtin: Set watchdog timeout. The "0" to turn watchdog off.
  148. */
  149. CLISH_PLUGIN_SYM(clish_wdog)
  150. {
  151. const char *arg = script;
  152. clish_shell_t *this = clish_context__get_shell(clish_context);
  153. /* Turn off watchdog if no args */
  154. if (!arg || ('\0' == *arg)) {
  155. clish_shell__set_wdog_timeout(this, 0);
  156. return 0;
  157. }
  158. clish_shell__set_wdog_timeout(this, (unsigned int)atoi(arg));
  159. return 0;
  160. }
  161. /*----------------------------------------------------------- */