interactive.c 11 KB

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