interactive.c 14 KB

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