interactive.c 13 KB

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