interactive.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. // Happy compiler
  147. eloop = eloop;
  148. type = type;
  149. associated_data = associated_data;
  150. return BOOL_TRUE;
  151. }
  152. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  153. {
  154. const char *line = NULL;
  155. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  156. faux_error_t *error = faux_error_new();
  157. tinyrl_line_to_hist(tinyrl);
  158. tinyrl_multi_crlf(tinyrl);
  159. tinyrl_reset_line_state(tinyrl);
  160. line = tinyrl_line(tinyrl);
  161. // Don't do anything on empty line
  162. if (faux_str_is_empty(line)) {
  163. faux_error_free(error);
  164. return BOOL_TRUE;
  165. }
  166. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  167. tinyrl_reset_line(tinyrl);
  168. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  169. key = key; // Happy compiler
  170. return BOOL_TRUE;
  171. }
  172. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  173. {
  174. const char *line = NULL;
  175. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  176. line = tinyrl_line(tinyrl);
  177. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  178. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  179. key = key; // Happy compiler
  180. return BOOL_TRUE;
  181. }
  182. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  183. {
  184. const char *line = NULL;
  185. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  186. line = tinyrl_line(tinyrl);
  187. ktp_session_help(ctx->ktp, line);
  188. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  189. key = key; // Happy compiler
  190. return BOOL_TRUE;
  191. }
  192. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  193. const char *prefix, size_t max)
  194. {
  195. size_t width = tinyrl_width(tinyrl);
  196. size_t cols = 0;
  197. faux_list_node_t *iter = NULL;
  198. faux_list_node_t *node = NULL;
  199. size_t prefix_len = 0;
  200. size_t cols_filled = 0;
  201. if (prefix)
  202. prefix_len = strlen(prefix);
  203. // Find out column and rows number
  204. if (max < width)
  205. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  206. else
  207. cols = 1;
  208. iter = faux_list_head(completions);
  209. while ((node = faux_list_each_node(&iter))) {
  210. char *compl = (char *)faux_list_data(node);
  211. tinyrl_printf(tinyrl, "%*s%s",
  212. (prefix_len + max + 1 - strlen(compl)),
  213. prefix ? prefix : "",
  214. compl);
  215. cols_filled++;
  216. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  217. cols_filled = 0;
  218. tinyrl_crlf(tinyrl);
  219. }
  220. }
  221. }
  222. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  223. {
  224. ctx_t *ctx = (ctx_t *)udata;
  225. faux_list_node_t *iter = NULL;
  226. uint32_t param_len = 0;
  227. char *param_data = NULL;
  228. uint16_t param_type = 0;
  229. char *prefix = NULL;
  230. faux_list_t *completions = NULL;
  231. size_t completions_num = 0;
  232. size_t max_compl_len = 0;
  233. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  234. process_prompt_param(ctx->tinyrl, msg);
  235. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  236. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  237. NULL, NULL, (void (*)(void *))faux_str_free);
  238. iter = faux_msg_init_param_iter(msg);
  239. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  240. char *compl = NULL;
  241. if (KTP_PARAM_LINE != param_type)
  242. continue;
  243. compl = faux_str_dupn(param_data, param_len);
  244. faux_list_add(completions, compl);
  245. if (param_len > max_compl_len)
  246. max_compl_len = param_len;
  247. }
  248. completions_num = faux_list_len(completions);
  249. // Single possible completion
  250. if (1 == completions_num) {
  251. char *compl = (char *)faux_list_data(faux_list_head(completions));
  252. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  253. tinyrl_redisplay(ctx->tinyrl);
  254. // Multi possible completions
  255. } else if (completions_num > 1) {
  256. faux_list_node_t *eq_iter = NULL;
  257. size_t eq_part = 0;
  258. char *str = NULL;
  259. char *compl = NULL;
  260. // Try to find equal part for all possible completions
  261. eq_iter = faux_list_head(completions);
  262. str = (char *)faux_list_data(eq_iter);
  263. eq_part = strlen(str);
  264. eq_iter = faux_list_next_node(eq_iter);
  265. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  266. size_t cur_eq = 0;
  267. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  268. if (cur_eq < eq_part)
  269. eq_part = cur_eq;
  270. }
  271. // The equal part was found
  272. if (eq_part > 0) {
  273. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  274. tinyrl_redisplay(ctx->tinyrl);
  275. // There is no equal part for all completions
  276. } else {
  277. tinyrl_multi_crlf(ctx->tinyrl);
  278. tinyrl_reset_line_state(ctx->tinyrl);
  279. display_completions(ctx->tinyrl, completions,
  280. prefix, max_compl_len);
  281. tinyrl_redisplay(ctx->tinyrl);
  282. }
  283. }
  284. faux_list_free(completions);
  285. faux_str_free(prefix);
  286. // Happy compiler
  287. ktp = ktp;
  288. msg = msg;
  289. return BOOL_TRUE;
  290. }
  291. static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
  292. size_t max)
  293. {
  294. faux_list_node_t *iter = NULL;
  295. faux_list_node_t *node = NULL;
  296. iter = faux_list_head(help_list);
  297. while ((node = faux_list_each_node(&iter))) {
  298. help_t *help = (help_t *)faux_list_data(node);
  299. tinyrl_printf(tinyrl, " %s%*s%s\n",
  300. help->prefix,
  301. (max + 2 - strlen(help->prefix)),
  302. " ",
  303. help->line);
  304. }
  305. }
  306. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  307. {
  308. ctx_t *ctx = (ctx_t *)udata;
  309. faux_list_t *help_list = NULL;
  310. faux_list_node_t *iter = NULL;
  311. uint32_t param_len = 0;
  312. char *param_data = NULL;
  313. uint16_t param_type = 0;
  314. size_t max_prefix_len = 0;
  315. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  316. process_prompt_param(ctx->tinyrl, msg);
  317. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
  318. help_compare, help_kcompare, help_free);
  319. // Wait for PREFIX - LINE pairs
  320. iter = faux_msg_init_param_iter(msg);
  321. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data,
  322. &param_len)) {
  323. char *prefix_str = NULL;
  324. char *line_str = NULL;
  325. help_t *help = NULL;
  326. size_t prefix_len = 0;
  327. // Get PREFIX
  328. if (KTP_PARAM_PREFIX != param_type)
  329. continue;
  330. prefix_str = faux_str_dupn(param_data, param_len);
  331. prefix_len = param_len;
  332. // Get LINE
  333. if (!faux_msg_get_param_each(&iter, &param_type,
  334. (void **)&param_data, &param_len) ||
  335. (KTP_PARAM_LINE != param_type)) {
  336. faux_str_free(prefix_str);
  337. break;
  338. }
  339. line_str = faux_str_dupn(param_data, param_len);
  340. help = help_new(prefix_str, line_str);
  341. faux_list_add(help_list, help);
  342. if (prefix_len > max_prefix_len)
  343. max_prefix_len = prefix_len;
  344. }
  345. if (faux_list_len(help_list) > 0) {
  346. tinyrl_multi_crlf(ctx->tinyrl);
  347. tinyrl_reset_line_state(ctx->tinyrl);
  348. display_help(ctx->tinyrl, help_list, max_prefix_len);
  349. tinyrl_redisplay(ctx->tinyrl);
  350. }
  351. faux_list_free(help_list);
  352. ktp = ktp; // happy compiler
  353. return BOOL_TRUE;
  354. }
  355. static bool_t ktp_sync_auth(ktp_session_t *ktp, int *retcode,
  356. faux_error_t *error)
  357. {
  358. if (!ktp_session_auth(ktp, error))
  359. return BOOL_FALSE;
  360. faux_eloop_loop(ktp_session_eloop(ktp));
  361. return ktp_session_retcode(ktp, retcode);
  362. }