interactive.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. static bool_t process_prompt_param(tinyrl_t *tinyrl, const faux_msg_t *msg)
  72. {
  73. char *prompt = NULL;
  74. if (!tinyrl)
  75. return BOOL_FALSE;
  76. if (!msg)
  77. return BOOL_FALSE;
  78. prompt = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PROMPT);
  79. if (prompt) {
  80. tinyrl_set_prompt(tinyrl, prompt);
  81. faux_str_free(prompt);
  82. }
  83. return BOOL_TRUE;
  84. }
  85. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  86. {
  87. ctx_t *ctx = (ctx_t *)udata;
  88. int rc = -1;
  89. faux_error_t *error = NULL;
  90. process_prompt_param(ctx->tinyrl, msg);
  91. if (!ktp_session_retcode(ktp, &rc))
  92. rc = -1;
  93. error = ktp_session_error(ktp);
  94. if ((rc < 0) && (faux_error_len(error) > 0)) {
  95. faux_error_node_t *err_iter = faux_error_iter(error);
  96. const char *err = NULL;
  97. while ((err = faux_error_each(&err_iter)))
  98. fprintf(stderr, "Error: %s\n", err);
  99. }
  100. faux_error_free(error);
  101. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  102. if (!ktp_session_done(ktp))
  103. tinyrl_redisplay(ctx->tinyrl);
  104. // Happy compiler
  105. msg = msg;
  106. return BOOL_TRUE;
  107. }
  108. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  109. void *associated_data, void *udata)
  110. {
  111. ctx_t *ctx = (ctx_t *)udata;
  112. tinyrl_read(ctx->tinyrl);
  113. return BOOL_TRUE;
  114. }
  115. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  116. {
  117. const char *line = NULL;
  118. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  119. faux_error_t *error = faux_error_new();
  120. tinyrl_line_to_hist(tinyrl);
  121. tinyrl_multi_crlf(tinyrl);
  122. tinyrl_reset_line_state(tinyrl);
  123. line = tinyrl_line(tinyrl);
  124. // Don't do anything on empty line
  125. if (faux_str_is_empty(line)) {
  126. faux_error_free(error);
  127. return BOOL_TRUE;
  128. }
  129. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  130. tinyrl_reset_line(tinyrl);
  131. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  132. return BOOL_TRUE;
  133. }
  134. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  135. {
  136. const char *line = NULL;
  137. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  138. line = tinyrl_line(tinyrl);
  139. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  140. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  141. return BOOL_TRUE;
  142. }
  143. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  144. {
  145. const char *line = NULL;
  146. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  147. line = tinyrl_line(tinyrl);
  148. ktp_session_help(ctx->ktp, line);
  149. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  150. return BOOL_TRUE;
  151. }
  152. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  153. const char *prefix, size_t max)
  154. {
  155. size_t width = tinyrl_width(tinyrl);
  156. size_t cols = 0;
  157. faux_list_node_t *iter = NULL;
  158. faux_list_node_t *node = NULL;
  159. size_t prefix_len = 0;
  160. size_t cols_filled = 0;
  161. if (prefix)
  162. prefix_len = strlen(prefix);
  163. // Find out column and rows number
  164. if (max < width)
  165. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  166. else
  167. cols = 1;
  168. iter = faux_list_head(completions);
  169. while ((node = faux_list_each_node(&iter))) {
  170. char *compl = (char *)faux_list_data(node);
  171. tinyrl_printf(tinyrl, "%*s%s",
  172. (prefix_len + max + 1 - strlen(compl)),
  173. prefix ? prefix : "",
  174. compl);
  175. cols_filled++;
  176. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  177. cols_filled = 0;
  178. tinyrl_crlf(tinyrl);
  179. }
  180. }
  181. }
  182. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  183. {
  184. ctx_t *ctx = (ctx_t *)udata;
  185. faux_list_node_t *iter = NULL;
  186. uint32_t param_len = 0;
  187. char *param_data = NULL;
  188. uint16_t param_type = 0;
  189. char *prefix = NULL;
  190. faux_list_t *completions = NULL;
  191. size_t completions_num = 0;
  192. size_t max_compl_len = 0;
  193. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  194. process_prompt_param(ctx->tinyrl, msg);
  195. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  196. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  197. NULL, NULL, (void (*)(void *))faux_str_free);
  198. iter = faux_msg_init_param_iter(msg);
  199. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  200. char *compl = NULL;
  201. if (KTP_PARAM_LINE != param_type)
  202. continue;
  203. compl = faux_str_dupn(param_data, param_len);
  204. faux_list_add(completions, compl);
  205. if (param_len > max_compl_len)
  206. max_compl_len = param_len;
  207. }
  208. completions_num = faux_list_len(completions);
  209. // Single possible completion
  210. if (1 == completions_num) {
  211. char *compl = (char *)faux_list_data(faux_list_head(completions));
  212. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  213. tinyrl_redisplay(ctx->tinyrl);
  214. // Multi possible completions
  215. } else if (completions_num > 1) {
  216. faux_list_node_t *eq_iter = NULL;
  217. size_t eq_part = 0;
  218. char *str = NULL;
  219. char *compl = NULL;
  220. // Try to find equal part for all possible completions
  221. eq_iter = faux_list_head(completions);
  222. str = (char *)faux_list_data(eq_iter);
  223. eq_part = strlen(str);
  224. eq_iter = faux_list_next_node(eq_iter);
  225. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  226. size_t cur_eq = 0;
  227. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  228. if (cur_eq < eq_part)
  229. eq_part = cur_eq;
  230. }
  231. // The equal part was found
  232. if (eq_part > 0) {
  233. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  234. tinyrl_redisplay(ctx->tinyrl);
  235. // There is no equal part for all completions
  236. } else {
  237. tinyrl_multi_crlf(ctx->tinyrl);
  238. tinyrl_reset_line_state(ctx->tinyrl);
  239. display_completions(ctx->tinyrl, completions,
  240. prefix, max_compl_len);
  241. tinyrl_redisplay(ctx->tinyrl);
  242. }
  243. }
  244. faux_list_free(completions);
  245. faux_str_free(prefix);
  246. // Happy compiler
  247. ktp = ktp;
  248. msg = msg;
  249. return BOOL_TRUE;
  250. }
  251. static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
  252. size_t max)
  253. {
  254. faux_list_node_t *iter = NULL;
  255. faux_list_node_t *node = NULL;
  256. iter = faux_list_head(help_list);
  257. while ((node = faux_list_each_node(&iter))) {
  258. help_t *help = (help_t *)faux_list_data(node);
  259. tinyrl_printf(tinyrl, " %s%*s%s\n",
  260. help->prefix,
  261. (max + 1 - strlen(help->prefix)),
  262. " ",
  263. help->line);
  264. }
  265. }
  266. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  267. {
  268. ctx_t *ctx = (ctx_t *)udata;
  269. faux_list_t *help_list = NULL;
  270. faux_list_node_t *iter = NULL;
  271. uint32_t param_len = 0;
  272. char *param_data = NULL;
  273. uint16_t param_type = 0;
  274. size_t max_prefix_len = 0;
  275. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  276. process_prompt_param(ctx->tinyrl, msg);
  277. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
  278. help_compare, help_kcompare, help_free);
  279. // Wait for PREFIX - LINE pairs
  280. iter = faux_msg_init_param_iter(msg);
  281. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data,
  282. &param_len)) {
  283. char *prefix_str = NULL;
  284. char *line_str = NULL;
  285. help_t *help = NULL;
  286. size_t prefix_len = 0;
  287. // Get PREFIX
  288. if (KTP_PARAM_PREFIX != param_type)
  289. continue;
  290. prefix_str = faux_str_dupn(param_data, param_len);
  291. prefix_len = param_len;
  292. // Get LINE
  293. if (!faux_msg_get_param_each(&iter, &param_type,
  294. (void **)&param_data, &param_len) ||
  295. (KTP_PARAM_LINE != param_type)) {
  296. faux_str_free(prefix_str);
  297. break;
  298. }
  299. line_str = faux_str_dupn(param_data, param_len);
  300. help = help_new(prefix_str, line_str);
  301. faux_list_add(help_list, help);
  302. if (prefix_len > max_prefix_len)
  303. max_prefix_len = prefix_len;
  304. }
  305. if (faux_list_len(help_list) > 0) {
  306. tinyrl_multi_crlf(ctx->tinyrl);
  307. tinyrl_reset_line_state(ctx->tinyrl);
  308. display_help(ctx->tinyrl, help_list, max_prefix_len);
  309. tinyrl_redisplay(ctx->tinyrl);
  310. }
  311. faux_list_free(help_list);
  312. ktp = ktp; // happy compiler
  313. return BOOL_TRUE;
  314. }