interactive.c 13 KB

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