interactive.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 *completions,
  236. const char *prefix, size_t max)
  237. {
  238. size_t width = tinyrl_width(tinyrl);
  239. size_t cols = 0;
  240. faux_list_node_t *iter = NULL;
  241. faux_list_node_t *node = NULL;
  242. size_t prefix_len = 0;
  243. size_t cols_filled = 0;
  244. if (prefix)
  245. prefix_len = strlen(prefix);
  246. // Find out column and rows number
  247. if (max < width)
  248. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  249. else
  250. cols = 1;
  251. iter = faux_list_head(completions);
  252. while ((node = faux_list_each_node(&iter))) {
  253. char *compl = (char *)faux_list_data(node);
  254. tinyrl_printf(tinyrl, "%*s%s",
  255. (prefix_len + max + 1 - strlen(compl)),
  256. prefix ? prefix : "",
  257. compl);
  258. cols_filled++;
  259. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  260. cols_filled = 0;
  261. tinyrl_crlf(tinyrl);
  262. }
  263. }
  264. }
  265. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  266. {
  267. ctx_t *ctx = (ctx_t *)udata;
  268. faux_list_node_t *iter = NULL;
  269. uint32_t param_len = 0;
  270. char *param_data = NULL;
  271. uint16_t param_type = 0;
  272. char *prefix = NULL;
  273. faux_list_t *completions = NULL;
  274. size_t completions_num = 0;
  275. size_t max_compl_len = 0;
  276. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  277. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  278. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  279. NULL, NULL, (void (*)(void *))faux_str_free);
  280. iter = faux_msg_init_param_iter(msg);
  281. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  282. char *compl = NULL;
  283. if (KTP_PARAM_LINE != param_type)
  284. continue;
  285. compl = faux_str_dupn(param_data, param_len);
  286. faux_list_add(completions, compl);
  287. if (param_len > max_compl_len)
  288. max_compl_len = param_len;
  289. }
  290. completions_num = faux_list_len(completions);
  291. // Single possible completion
  292. if (1 == completions_num) {
  293. char *compl = (char *)faux_list_data(faux_list_head(completions));
  294. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  295. tinyrl_redisplay(ctx->tinyrl);
  296. // Multi possible completions
  297. } else if (completions_num > 1) {
  298. faux_list_node_t *eq_iter = NULL;
  299. size_t eq_part = 0;
  300. char *str = NULL;
  301. char *compl = NULL;
  302. // Try to find equal part for all possible completions
  303. eq_iter = faux_list_head(completions);
  304. str = (char *)faux_list_data(eq_iter);
  305. eq_part = strlen(str);
  306. eq_iter = faux_list_next_node(eq_iter);
  307. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  308. size_t cur_eq = 0;
  309. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  310. if (cur_eq < eq_part)
  311. eq_part = cur_eq;
  312. }
  313. // The equal part was found
  314. if (eq_part > 0) {
  315. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  316. tinyrl_redisplay(ctx->tinyrl);
  317. // There is no equal part for all completions
  318. } else {
  319. tinyrl_multi_crlf(ctx->tinyrl);
  320. tinyrl_reset_line_state(ctx->tinyrl);
  321. display_completions(ctx->tinyrl, completions,
  322. prefix, max_compl_len);
  323. tinyrl_redisplay(ctx->tinyrl);
  324. }
  325. }
  326. faux_list_free(completions);
  327. faux_str_free(prefix);
  328. // Happy compiler
  329. ktp = ktp;
  330. msg = msg;
  331. return BOOL_TRUE;
  332. }