interactive.c 6.9 KB

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