interactive.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. #include <faux/faux.h>
  8. #include <faux/str.h>
  9. #include <faux/eloop.h>
  10. #include <klish/ktp.h>
  11. #include <klish/ktp_session.h>
  12. #include <tinyrl/tinyrl.h>
  13. #include "private.h"
  14. // Context for main loop
  15. typedef struct ctx_s {
  16. ktp_session_t *ktp;
  17. tinyrl_t *tinyrl;
  18. struct options *opts;
  19. } ctx_t;
  20. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  21. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  22. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  23. void *associated_data, void *user_data);
  24. // Keys
  25. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
  26. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key);
  27. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key);
  28. int klish_interactive_shell(ktp_session_t *ktp, struct options *opts)
  29. {
  30. ctx_t ctx = {};
  31. faux_eloop_t *eloop = NULL;
  32. tinyrl_t *tinyrl = NULL;
  33. int stdin_flags = 0;
  34. char *hist_path = NULL;
  35. assert(ktp);
  36. if (!ktp)
  37. return -1;
  38. // Set stdin to O_NONBLOCK mode
  39. stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  40. fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
  41. hist_path = faux_expand_tilde("~/.klish_history");
  42. tinyrl = tinyrl_new(stdin, stdout, hist_path, 100);
  43. faux_str_free(hist_path);
  44. tinyrl_set_prompt(tinyrl, "$ ");
  45. tinyrl_set_udata(tinyrl, &ctx);
  46. tinyrl_bind_key(tinyrl, '\n', tinyrl_key_enter);
  47. tinyrl_bind_key(tinyrl, '\r', tinyrl_key_enter);
  48. tinyrl_bind_key(tinyrl, '\t', tinyrl_key_tab);
  49. tinyrl_bind_key(tinyrl, '?', tinyrl_key_help);
  50. tinyrl_redisplay(tinyrl);
  51. ctx.ktp = ktp;
  52. ctx.tinyrl = tinyrl;
  53. ctx.opts = opts;
  54. // Don't stop interactive loop on each answer
  55. ktp_session_set_stop_on_answer(ktp, BOOL_FALSE);
  56. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK, cmd_ack_cb, &ctx);
  57. ktp_session_set_cb(ktp, KTP_SESSION_CB_COMPLETION_ACK, completion_ack_cb, &ctx);
  58. eloop = ktp_session_eloop(ktp);
  59. faux_eloop_add_fd(eloop, STDIN_FILENO, POLLIN, stdin_cb, &ctx);
  60. faux_eloop_loop(eloop);
  61. // Cleanup
  62. if (tinyrl_busy(tinyrl))
  63. faux_error_free(ktp_session_error(ktp));
  64. tinyrl_free(tinyrl);
  65. // Restore stdin mode
  66. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  67. return 0;
  68. }
  69. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  70. {
  71. ctx_t *ctx = (ctx_t *)udata;
  72. int rc = -1;
  73. faux_error_t *error = NULL;
  74. if (!ktp_session_retcode(ktp, &rc))
  75. rc = -1;
  76. error = ktp_session_error(ktp);
  77. if ((rc < 0) && (faux_error_len(error) > 0)) {
  78. faux_error_node_t *err_iter = faux_error_iter(error);
  79. const char *err = NULL;
  80. while ((err = faux_error_each(&err_iter)))
  81. fprintf(stderr, "Error: %s\n", err);
  82. }
  83. faux_error_free(error);
  84. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  85. if (!ktp_session_done(ktp))
  86. tinyrl_redisplay(ctx->tinyrl);
  87. // Happy compiler
  88. msg = msg;
  89. return BOOL_TRUE;
  90. }
  91. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  92. void *associated_data, void *udata)
  93. {
  94. ctx_t *ctx = (ctx_t *)udata;
  95. tinyrl_read(ctx->tinyrl);
  96. return BOOL_TRUE;
  97. }
  98. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  99. {
  100. const char *line = NULL;
  101. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  102. faux_error_t *error = faux_error_new();
  103. tinyrl_line_to_hist(tinyrl);
  104. tinyrl_multi_crlf(tinyrl);
  105. tinyrl_reset_line_state(tinyrl);
  106. line = tinyrl_line(tinyrl);
  107. // Don't do anything on empty line
  108. if (faux_str_is_empty(line)) {
  109. faux_error_free(error);
  110. return BOOL_TRUE;
  111. }
  112. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  113. tinyrl_reset_line(tinyrl);
  114. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  115. return BOOL_TRUE;
  116. }
  117. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  118. {
  119. const char *line = NULL;
  120. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  121. line = tinyrl_line(tinyrl);
  122. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  123. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  124. return BOOL_TRUE;
  125. }
  126. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  127. {
  128. const char *line = NULL;
  129. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  130. line = tinyrl_line(tinyrl);
  131. ktp_session_help(ctx->ktp, line);
  132. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  133. return BOOL_TRUE;
  134. }
  135. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  136. const char *prefix, size_t max)
  137. {
  138. size_t width = tinyrl_width(tinyrl);
  139. size_t cols = 0;
  140. faux_list_node_t *iter = NULL;
  141. faux_list_node_t *node = NULL;
  142. size_t prefix_len = 0;
  143. size_t cols_filled = 0;
  144. if (prefix)
  145. prefix_len = strlen(prefix);
  146. // Find out column and rows number
  147. if (max < width)
  148. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  149. else
  150. cols = 1;
  151. iter = faux_list_head(completions);
  152. while ((node = faux_list_each_node(&iter))) {
  153. char *compl = (char *)faux_list_data(node);
  154. tinyrl_printf(tinyrl, "%*s%s",
  155. (prefix_len + max + 1 - strlen(compl)),
  156. prefix ? prefix : "",
  157. compl);
  158. cols_filled++;
  159. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  160. cols_filled = 0;
  161. tinyrl_crlf(tinyrl);
  162. }
  163. }
  164. }
  165. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  166. {
  167. ctx_t *ctx = (ctx_t *)udata;
  168. faux_list_node_t *iter = NULL;
  169. uint32_t param_len = 0;
  170. char *param_data = NULL;
  171. uint16_t param_type = 0;
  172. char *prefix = NULL;
  173. faux_list_t *completions = NULL;
  174. size_t completions_num = 0;
  175. size_t max_compl_len = 0;
  176. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  177. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  178. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  179. NULL, NULL, (void (*)(void *))faux_str_free);
  180. iter = faux_msg_init_param_iter(msg);
  181. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  182. char *compl = NULL;
  183. if (KTP_PARAM_LINE != param_type)
  184. continue;
  185. compl = faux_str_dupn(param_data, param_len);
  186. faux_list_add(completions, compl);
  187. if (param_len > max_compl_len)
  188. max_compl_len = param_len;
  189. }
  190. completions_num = faux_list_len(completions);
  191. // Single possible completion
  192. if (1 == completions_num) {
  193. char *compl = (char *)faux_list_data(faux_list_head(completions));
  194. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  195. tinyrl_redisplay(ctx->tinyrl);
  196. // Multi possible completions
  197. } else if (completions_num > 1) {
  198. faux_list_node_t *eq_iter = NULL;
  199. size_t eq_part = 0;
  200. char *str = NULL;
  201. char *compl = NULL;
  202. // Try to find equal part for all possible completions
  203. eq_iter = faux_list_head(completions);
  204. str = (char *)faux_list_data(eq_iter);
  205. eq_part = strlen(str);
  206. eq_iter = faux_list_next_node(eq_iter);
  207. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  208. size_t cur_eq = 0;
  209. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  210. if (cur_eq < eq_part)
  211. eq_part = cur_eq;
  212. }
  213. // The equal part was found
  214. if (eq_part > 0) {
  215. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  216. tinyrl_redisplay(ctx->tinyrl);
  217. // There is no equal part for all completions
  218. } else {
  219. tinyrl_multi_crlf(ctx->tinyrl);
  220. tinyrl_reset_line_state(ctx->tinyrl);
  221. display_completions(ctx->tinyrl, completions,
  222. prefix, max_compl_len);
  223. tinyrl_redisplay(ctx->tinyrl);
  224. }
  225. }
  226. faux_list_free(completions);
  227. faux_str_free(prefix);
  228. // Happy compiler
  229. ktp = ktp;
  230. msg = msg;
  231. return BOOL_TRUE;
  232. }