interactive.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  23. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  24. void *associated_data, void *user_data);
  25. // Keys
  26. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
  27. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key);
  28. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key);
  29. int klish_interactive_shell(ktp_session_t *ktp, struct options *opts)
  30. {
  31. ctx_t ctx = {};
  32. faux_eloop_t *eloop = NULL;
  33. tinyrl_t *tinyrl = NULL;
  34. int stdin_flags = 0;
  35. char *hist_path = NULL;
  36. assert(ktp);
  37. if (!ktp)
  38. return -1;
  39. // Set stdin to O_NONBLOCK mode
  40. stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  41. fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
  42. hist_path = faux_expand_tilde("~/.klish_history");
  43. tinyrl = tinyrl_new(stdin, stdout, hist_path, 100);
  44. faux_str_free(hist_path);
  45. tinyrl_set_prompt(tinyrl, "$ ");
  46. tinyrl_set_udata(tinyrl, &ctx);
  47. tinyrl_bind_key(tinyrl, '\n', tinyrl_key_enter);
  48. tinyrl_bind_key(tinyrl, '\r', tinyrl_key_enter);
  49. tinyrl_bind_key(tinyrl, '\t', tinyrl_key_tab);
  50. tinyrl_bind_key(tinyrl, '?', tinyrl_key_help);
  51. tinyrl_redisplay(tinyrl);
  52. ctx.ktp = ktp;
  53. ctx.tinyrl = tinyrl;
  54. ctx.opts = opts;
  55. // Don't stop interactive loop on each answer
  56. ktp_session_set_stop_on_answer(ktp, BOOL_FALSE);
  57. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK, cmd_ack_cb, &ctx);
  58. ktp_session_set_cb(ktp, KTP_SESSION_CB_COMPLETION_ACK, completion_ack_cb, &ctx);
  59. ktp_session_set_cb(ktp, KTP_SESSION_CB_HELP_ACK, help_ack_cb, &ctx);
  60. eloop = ktp_session_eloop(ktp);
  61. faux_eloop_add_fd(eloop, STDIN_FILENO, POLLIN, stdin_cb, &ctx);
  62. faux_eloop_loop(eloop);
  63. // Cleanup
  64. if (tinyrl_busy(tinyrl))
  65. faux_error_free(ktp_session_error(ktp));
  66. tinyrl_free(tinyrl);
  67. // Restore stdin mode
  68. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  69. return 0;
  70. }
  71. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  72. {
  73. ctx_t *ctx = (ctx_t *)udata;
  74. int rc = -1;
  75. faux_error_t *error = NULL;
  76. if (!ktp_session_retcode(ktp, &rc))
  77. rc = -1;
  78. error = ktp_session_error(ktp);
  79. if ((rc < 0) && (faux_error_len(error) > 0)) {
  80. faux_error_node_t *err_iter = faux_error_iter(error);
  81. const char *err = NULL;
  82. while ((err = faux_error_each(&err_iter)))
  83. fprintf(stderr, "Error: %s\n", err);
  84. }
  85. faux_error_free(error);
  86. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  87. if (!ktp_session_done(ktp))
  88. tinyrl_redisplay(ctx->tinyrl);
  89. // Happy compiler
  90. msg = msg;
  91. return BOOL_TRUE;
  92. }
  93. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  94. void *associated_data, void *udata)
  95. {
  96. ctx_t *ctx = (ctx_t *)udata;
  97. tinyrl_read(ctx->tinyrl);
  98. return BOOL_TRUE;
  99. }
  100. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  101. {
  102. const char *line = NULL;
  103. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  104. faux_error_t *error = faux_error_new();
  105. tinyrl_line_to_hist(tinyrl);
  106. tinyrl_multi_crlf(tinyrl);
  107. tinyrl_reset_line_state(tinyrl);
  108. line = tinyrl_line(tinyrl);
  109. // Don't do anything on empty line
  110. if (faux_str_is_empty(line)) {
  111. faux_error_free(error);
  112. return BOOL_TRUE;
  113. }
  114. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  115. tinyrl_reset_line(tinyrl);
  116. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  117. return BOOL_TRUE;
  118. }
  119. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  120. {
  121. const char *line = NULL;
  122. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  123. line = tinyrl_line(tinyrl);
  124. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  125. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  126. return BOOL_TRUE;
  127. }
  128. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  129. {
  130. const char *line = NULL;
  131. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  132. line = tinyrl_line(tinyrl);
  133. ktp_session_help(ctx->ktp, line);
  134. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  135. return BOOL_TRUE;
  136. }
  137. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  138. const char *prefix, size_t max)
  139. {
  140. size_t width = tinyrl_width(tinyrl);
  141. size_t cols = 0;
  142. faux_list_node_t *iter = NULL;
  143. faux_list_node_t *node = NULL;
  144. size_t prefix_len = 0;
  145. size_t cols_filled = 0;
  146. if (prefix)
  147. prefix_len = strlen(prefix);
  148. // Find out column and rows number
  149. if (max < width)
  150. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  151. else
  152. cols = 1;
  153. iter = faux_list_head(completions);
  154. while ((node = faux_list_each_node(&iter))) {
  155. char *compl = (char *)faux_list_data(node);
  156. tinyrl_printf(tinyrl, "%*s%s",
  157. (prefix_len + max + 1 - strlen(compl)),
  158. prefix ? prefix : "",
  159. compl);
  160. cols_filled++;
  161. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  162. cols_filled = 0;
  163. tinyrl_crlf(tinyrl);
  164. }
  165. }
  166. }
  167. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  168. {
  169. ctx_t *ctx = (ctx_t *)udata;
  170. faux_list_node_t *iter = NULL;
  171. uint32_t param_len = 0;
  172. char *param_data = NULL;
  173. uint16_t param_type = 0;
  174. char *prefix = NULL;
  175. faux_list_t *completions = NULL;
  176. size_t completions_num = 0;
  177. size_t max_compl_len = 0;
  178. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  179. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  180. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  181. NULL, NULL, (void (*)(void *))faux_str_free);
  182. iter = faux_msg_init_param_iter(msg);
  183. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  184. char *compl = NULL;
  185. if (KTP_PARAM_LINE != param_type)
  186. continue;
  187. compl = faux_str_dupn(param_data, param_len);
  188. faux_list_add(completions, compl);
  189. if (param_len > max_compl_len)
  190. max_compl_len = param_len;
  191. }
  192. completions_num = faux_list_len(completions);
  193. // Single possible completion
  194. if (1 == completions_num) {
  195. char *compl = (char *)faux_list_data(faux_list_head(completions));
  196. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  197. tinyrl_redisplay(ctx->tinyrl);
  198. // Multi possible completions
  199. } else if (completions_num > 1) {
  200. faux_list_node_t *eq_iter = NULL;
  201. size_t eq_part = 0;
  202. char *str = NULL;
  203. char *compl = NULL;
  204. // Try to find equal part for all possible completions
  205. eq_iter = faux_list_head(completions);
  206. str = (char *)faux_list_data(eq_iter);
  207. eq_part = strlen(str);
  208. eq_iter = faux_list_next_node(eq_iter);
  209. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  210. size_t cur_eq = 0;
  211. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  212. if (cur_eq < eq_part)
  213. eq_part = cur_eq;
  214. }
  215. // The equal part was found
  216. if (eq_part > 0) {
  217. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  218. tinyrl_redisplay(ctx->tinyrl);
  219. // There is no equal part for all completions
  220. } else {
  221. tinyrl_multi_crlf(ctx->tinyrl);
  222. tinyrl_reset_line_state(ctx->tinyrl);
  223. display_completions(ctx->tinyrl, completions,
  224. prefix, max_compl_len);
  225. tinyrl_redisplay(ctx->tinyrl);
  226. }
  227. }
  228. faux_list_free(completions);
  229. faux_str_free(prefix);
  230. // Happy compiler
  231. ktp = ktp;
  232. msg = msg;
  233. return BOOL_TRUE;
  234. }
  235. static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
  236. size_t max)
  237. {
  238. faux_list_node_t *iter = NULL;
  239. faux_list_node_t *node = NULL;
  240. iter = faux_list_head(help_list);
  241. while ((node = faux_list_each_node(&iter))) {
  242. help_t *help = (help_t *)faux_list_data(node);
  243. tinyrl_printf(tinyrl, " %s%*s%s\n",
  244. help->prefix,
  245. (max + 1 - strlen(help->prefix)),
  246. " ",
  247. help->line);
  248. }
  249. }
  250. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  251. {
  252. ctx_t *ctx = (ctx_t *)udata;
  253. faux_list_t *help_list = NULL;
  254. faux_list_node_t *iter = NULL;
  255. uint32_t param_len = 0;
  256. char *param_data = NULL;
  257. uint16_t param_type = 0;
  258. size_t max_prefix_len = 0;
  259. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  260. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
  261. help_compare, help_kcompare, help_free);
  262. // Wait for PREFIX - LINE pairs
  263. iter = faux_msg_init_param_iter(msg);
  264. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data,
  265. &param_len)) {
  266. char *prefix_str = NULL;
  267. char *line_str = NULL;
  268. help_t *help = NULL;
  269. size_t prefix_len = 0;
  270. // Get PREFIX
  271. if (KTP_PARAM_PREFIX != param_type)
  272. continue;
  273. prefix_str = faux_str_dupn(param_data, param_len);
  274. prefix_len = param_len;
  275. // Get LINE
  276. if (!faux_msg_get_param_each(&iter, &param_type,
  277. (void **)&param_data, &param_len) ||
  278. (KTP_PARAM_LINE != param_type)) {
  279. faux_str_free(prefix_str);
  280. break;
  281. }
  282. line_str = faux_str_dupn(param_data, param_len);
  283. help = help_new(prefix_str, line_str);
  284. faux_list_add(help_list, help);
  285. if (prefix_len > max_prefix_len)
  286. max_prefix_len = prefix_len;
  287. }
  288. if (faux_list_len(help_list) > 0) {
  289. tinyrl_multi_crlf(ctx->tinyrl);
  290. tinyrl_reset_line_state(ctx->tinyrl);
  291. display_help(ctx->tinyrl, help_list, max_prefix_len);
  292. tinyrl_redisplay(ctx->tinyrl);
  293. }
  294. faux_list_free(help_list);
  295. ktp = ktp; // happy compiler
  296. return BOOL_TRUE;
  297. }