interactive.c 15 KB

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