interactive.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 <errno.h>
  10. #include <syslog.h>
  11. #include <faux/faux.h>
  12. #include <faux/str.h>
  13. #include <faux/eloop.h>
  14. #include <klish/ktp.h>
  15. #include <klish/ktp_session.h>
  16. #include <tinyrl/tinyrl.h>
  17. #include "private.h"
  18. // Context for main loop
  19. typedef struct ctx_s {
  20. ktp_session_t *ktp;
  21. tinyrl_t *tinyrl;
  22. struct options *opts;
  23. char *hotkeys[VT100_HOTKEY_MAP_LEN];
  24. tri_t pager_working;
  25. FILE *pager_pipe;
  26. } ctx_t;
  27. bool_t auth_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  28. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  29. bool_t cmd_incompleted_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  30. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  31. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  32. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  33. void *associated_data, void *user_data);
  34. static bool_t sigwinch_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  35. void *associated_data, void *user_data);
  36. static void reset_hotkey_table(ctx_t *ctx);
  37. static bool_t interactive_stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  38. void *user_data);
  39. static bool_t send_winch_notification(ctx_t *ctx);
  40. // Keys
  41. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
  42. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key);
  43. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key);
  44. static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key);
  45. int klish_interactive_shell(ktp_session_t *ktp, struct options *opts)
  46. {
  47. ctx_t ctx = {};
  48. faux_eloop_t *eloop = NULL;
  49. tinyrl_t *tinyrl = NULL;
  50. int stdin_flags = 0;
  51. char *hist_path = NULL;
  52. int auth_rc = -1;
  53. assert(ktp);
  54. if (!ktp)
  55. return -1;
  56. // Set stdin to O_NONBLOCK mode
  57. stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  58. fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
  59. hist_path = faux_expand_tilde("~/.klish_history");
  60. tinyrl = tinyrl_new(stdin, stdout, hist_path, 100);
  61. faux_str_free(hist_path);
  62. tinyrl_set_prompt(tinyrl, "$ ");
  63. tinyrl_set_udata(tinyrl, &ctx);
  64. tinyrl_set_hotkey_fn(tinyrl, tinyrl_key_hotkey);
  65. tinyrl_bind_key(tinyrl, '\n', tinyrl_key_enter);
  66. tinyrl_bind_key(tinyrl, '\r', tinyrl_key_enter);
  67. tinyrl_bind_key(tinyrl, '\t', tinyrl_key_tab);
  68. tinyrl_bind_key(tinyrl, '?', tinyrl_key_help);
  69. ctx.ktp = ktp;
  70. ctx.tinyrl = tinyrl;
  71. ctx.opts = opts;
  72. faux_bzero(ctx.hotkeys, sizeof(ctx.hotkeys));
  73. ctx.pager_working = TRI_UNDEFINED;
  74. ctx.pager_pipe = NULL;
  75. // Now AUTH command is used only for starting hand-shake and getting
  76. // prompt from the server. Generally it must be necessary for
  77. // non-interactive session too but for now is not implemented.
  78. ktp_session_set_cb(ktp, KTP_SESSION_CB_AUTH_ACK, auth_ack_cb, &ctx);
  79. if (!ktp_sync_auth(ktp, &auth_rc))
  80. goto cleanup;
  81. if (auth_rc < 0)
  82. goto cleanup;
  83. // Replace common stdout callback by interactive-specific one.
  84. // Place it after auth to make auth use standard stdout callback.
  85. ktp_session_set_cb(ktp, KTP_SESSION_CB_STDOUT, interactive_stdout_cb, &ctx);
  86. // Don't stop interactive loop on each answer
  87. ktp_session_set_stop_on_answer(ktp, BOOL_FALSE);
  88. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK, cmd_ack_cb, &ctx);
  89. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK_INCOMPLETED,
  90. cmd_incompleted_ack_cb, &ctx);
  91. ktp_session_set_cb(ktp, KTP_SESSION_CB_COMPLETION_ACK,
  92. completion_ack_cb, &ctx);
  93. ktp_session_set_cb(ktp, KTP_SESSION_CB_HELP_ACK, help_ack_cb, &ctx);
  94. tinyrl_redisplay(tinyrl);
  95. eloop = ktp_session_eloop(ktp);
  96. // Notify server about terminal window size change
  97. faux_eloop_add_signal(eloop, SIGWINCH, sigwinch_cb, &ctx);
  98. faux_eloop_add_fd(eloop, STDIN_FILENO, POLLIN, stdin_cb, &ctx);
  99. send_winch_notification(&ctx);
  100. faux_eloop_loop(eloop);
  101. cleanup:
  102. // Cleanup
  103. reset_hotkey_table(&ctx);
  104. if (tinyrl_busy(tinyrl))
  105. faux_error_free(ktp_session_error(ktp));
  106. tinyrl_free(tinyrl);
  107. // Restore stdin mode
  108. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  109. return 0;
  110. }
  111. static bool_t process_prompt_param(tinyrl_t *tinyrl, const faux_msg_t *msg)
  112. {
  113. char *prompt = NULL;
  114. if (!tinyrl)
  115. return BOOL_FALSE;
  116. if (!msg)
  117. return BOOL_FALSE;
  118. prompt = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PROMPT);
  119. if (prompt) {
  120. tinyrl_set_prompt(tinyrl, prompt);
  121. faux_str_free(prompt);
  122. }
  123. return BOOL_TRUE;
  124. }
  125. static void reset_hotkey_table(ctx_t *ctx)
  126. {
  127. size_t i = 0;
  128. assert(ctx);
  129. for (i = 0; i < VT100_HOTKEY_MAP_LEN; i++)
  130. faux_str_free(ctx->hotkeys[i]);
  131. faux_bzero(ctx->hotkeys, sizeof(ctx->hotkeys));
  132. }
  133. static bool_t process_hotkey_param(ctx_t *ctx, const faux_msg_t *msg)
  134. {
  135. faux_list_node_t *iter = NULL;
  136. uint32_t param_len = 0;
  137. char *param_data = NULL;
  138. uint16_t param_type = 0;
  139. if (!ctx)
  140. return BOOL_FALSE;
  141. if (!msg)
  142. return BOOL_FALSE;
  143. if (!faux_msg_get_param_by_type(msg, KTP_PARAM_HOTKEY,
  144. (void **)&param_data, &param_len))
  145. return BOOL_TRUE;
  146. // If there is HOTKEY parameter then reinitialize whole hotkey table
  147. reset_hotkey_table(ctx);
  148. iter = faux_msg_init_param_iter(msg);
  149. while (faux_msg_get_param_each(
  150. &iter, &param_type, (void **)&param_data, &param_len)) {
  151. char *cmd = NULL;
  152. ssize_t code = -1;
  153. size_t key_len = 0;
  154. if (param_len < 3) // <key>'\0'<cmd>
  155. continue;
  156. if (KTP_PARAM_HOTKEY != param_type)
  157. continue;
  158. key_len = strlen(param_data); // Length of <key>
  159. if (key_len < 1)
  160. continue;
  161. code = vt100_hotkey_decode(param_data);
  162. if ((code < 0) || (code > VT100_HOTKEY_MAP_LEN))
  163. continue;
  164. cmd = faux_str_dupn(param_data + key_len + 1,
  165. param_len - key_len - 1);
  166. if (!cmd)
  167. continue;
  168. ctx->hotkeys[code] = cmd;
  169. }
  170. return BOOL_TRUE;
  171. }
  172. bool_t auth_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: auth: %s\n", err);
  187. }
  188. // Operation is finished so restore stdin handler
  189. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  190. stdin_cb, ctx);
  191. // Happy compiler
  192. msg = msg;
  193. return BOOL_TRUE;
  194. }
  195. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  196. {
  197. ctx_t *ctx = (ctx_t *)udata;
  198. int rc = -1;
  199. faux_error_t *error = NULL;
  200. process_prompt_param(ctx->tinyrl, msg);
  201. process_hotkey_param(ctx, msg);
  202. if (!ktp_session_retcode(ktp, &rc))
  203. rc = -1;
  204. error = ktp_session_error(ktp);
  205. if ((rc < 0) && (faux_error_len(error) > 0)) {
  206. faux_error_node_t *err_iter = faux_error_iter(error);
  207. const char *err = NULL;
  208. while ((err = faux_error_each(&err_iter)))
  209. fprintf(stderr, "Error: %s\n", err);
  210. }
  211. faux_error_free(error);
  212. // Wait for pager
  213. if (ctx->pager_working == TRI_TRUE) {
  214. pclose(ctx->pager_pipe);
  215. ctx->pager_working = TRI_UNDEFINED;
  216. ctx->pager_pipe = NULL;
  217. }
  218. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  219. if (!ktp_session_done(ktp))
  220. tinyrl_redisplay(ctx->tinyrl);
  221. // Operation is finished so restore stdin handler
  222. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  223. stdin_cb, ctx);
  224. // Happy compiler
  225. msg = msg;
  226. return BOOL_TRUE;
  227. }
  228. bool_t cmd_incompleted_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  229. {
  230. ctx_t *ctx = (ctx_t *)udata;
  231. // Interactive command. So restore stdin handler.
  232. if ((ktp_session_state(ktp) == KTP_SESSION_STATE_WAIT_FOR_CMD) &&
  233. KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ktp))) {
  234. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  235. stdin_cb, ctx);
  236. }
  237. // Happy compiler
  238. msg = msg;
  239. return BOOL_TRUE;
  240. }
  241. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  242. void *associated_data, void *udata)
  243. {
  244. bool_t rc = BOOL_TRUE;
  245. ctx_t *ctx = (ctx_t *)udata;
  246. ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
  247. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  248. if (!ctx)
  249. return BOOL_FALSE;
  250. // Some errors or fd is closed so stop session
  251. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  252. rc = BOOL_FALSE;
  253. state = ktp_session_state(ctx->ktp);
  254. // Standard klish command line
  255. if (state == KTP_SESSION_STATE_IDLE) {
  256. tinyrl_read(ctx->tinyrl);
  257. return rc;
  258. }
  259. // Interactive command
  260. if ((state == KTP_SESSION_STATE_WAIT_FOR_CMD) &&
  261. KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ctx->ktp))) {
  262. int fd = fileno(tinyrl_istream(ctx->tinyrl));
  263. char buf[1024] = {};
  264. ssize_t bytes_readed = 0;
  265. while ((bytes_readed = read(fd, buf, sizeof(buf))) > 0) {
  266. ktp_session_stdin(ctx->ktp, buf, bytes_readed);
  267. if (bytes_readed != sizeof(buf))
  268. break;
  269. }
  270. return rc;
  271. }
  272. // Here the situation when input is not allowed. Remove stdin from
  273. // eloop waiting list. Else klish will get 100% CPU. Callbacks on
  274. // operation completions will restore this handler.
  275. faux_eloop_del_fd(eloop, STDIN_FILENO);
  276. // Happy compiler
  277. eloop = eloop;
  278. type = type;
  279. return rc;
  280. }
  281. static bool_t send_winch_notification(ctx_t *ctx)
  282. {
  283. size_t width = 0;
  284. size_t height = 0;
  285. char *winsize = NULL;
  286. faux_msg_t *req = NULL;
  287. ktp_status_e status = KTP_STATUS_NONE;
  288. if (!ctx->tinyrl)
  289. return BOOL_FALSE;
  290. if (!ctx->ktp)
  291. return BOOL_FALSE;
  292. tinyrl_winsize(ctx->tinyrl, &width, &height);
  293. winsize = faux_str_sprintf("%lu %lu", width, height);
  294. req = ktp_msg_preform(KTP_NOTIFICATION, status);
  295. faux_msg_add_param(req, KTP_PARAM_WINCH, winsize, strlen(winsize));
  296. faux_str_free(winsize);
  297. faux_msg_send_async(req, ktp_session_async(ctx->ktp));
  298. faux_msg_free(req);
  299. return BOOL_TRUE;
  300. }
  301. static bool_t sigwinch_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  302. void *associated_data, void *udata)
  303. {
  304. ctx_t *ctx = (ctx_t *)udata;
  305. if (!ctx)
  306. return BOOL_FALSE;
  307. send_winch_notification(ctx);
  308. // Happy compiler
  309. eloop = eloop;
  310. type = type;
  311. associated_data = associated_data;
  312. return BOOL_TRUE;
  313. }
  314. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  315. {
  316. const char *line = NULL;
  317. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  318. faux_error_t *error = faux_error_new();
  319. tinyrl_line_to_hist(tinyrl);
  320. tinyrl_multi_crlf(tinyrl);
  321. tinyrl_reset_line_state(tinyrl);
  322. line = tinyrl_line(tinyrl);
  323. // Don't do anything on empty line
  324. if (faux_str_is_empty(line)) {
  325. faux_error_free(error);
  326. return BOOL_TRUE;
  327. }
  328. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  329. tinyrl_reset_line(tinyrl);
  330. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  331. key = key; // Happy compiler
  332. return BOOL_TRUE;
  333. }
  334. static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key)
  335. {
  336. const char *line = NULL;
  337. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  338. faux_error_t *error = NULL;
  339. if (key >= VT100_HOTKEY_MAP_LEN)
  340. return BOOL_TRUE;
  341. line = ctx->hotkeys[key];
  342. if (faux_str_is_empty(line))
  343. return BOOL_TRUE;
  344. error = faux_error_new();
  345. tinyrl_multi_crlf(tinyrl);
  346. tinyrl_reset_line_state(tinyrl);
  347. tinyrl_reset_line(tinyrl);
  348. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  349. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  350. return BOOL_TRUE;
  351. }
  352. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  353. {
  354. char *line = NULL;
  355. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  356. line = tinyrl_line_to_pos(tinyrl);
  357. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  358. faux_str_free(line);
  359. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  360. key = key; // Happy compiler
  361. return BOOL_TRUE;
  362. }
  363. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  364. {
  365. char *line = NULL;
  366. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  367. line = tinyrl_line_to_pos(tinyrl);
  368. // If "?" is quoted then it's not special hotkey.
  369. // Just insert it into the line.
  370. if (faux_str_unclosed_quotes(line, NULL)) {
  371. faux_str_free(line);
  372. return tinyrl_key_default(tinyrl, key);
  373. }
  374. ktp_session_help(ctx->ktp, line);
  375. faux_str_free(line);
  376. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  377. key = key; // Happy compiler
  378. return BOOL_TRUE;
  379. }
  380. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  381. const char *prefix, size_t max)
  382. {
  383. size_t width = tinyrl_width(tinyrl);
  384. size_t cols = 0;
  385. faux_list_node_t *iter = NULL;
  386. faux_list_node_t *node = NULL;
  387. size_t prefix_len = 0;
  388. size_t cols_filled = 0;
  389. if (prefix)
  390. prefix_len = strlen(prefix);
  391. // Find out column and rows number
  392. if (max < width)
  393. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  394. else
  395. cols = 1;
  396. iter = faux_list_head(completions);
  397. while ((node = faux_list_each_node(&iter))) {
  398. char *compl = (char *)faux_list_data(node);
  399. tinyrl_printf(tinyrl, "%*s%s",
  400. (prefix_len + max + 1 - strlen(compl)),
  401. prefix ? prefix : "",
  402. compl);
  403. cols_filled++;
  404. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  405. cols_filled = 0;
  406. tinyrl_crlf(tinyrl);
  407. }
  408. }
  409. }
  410. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  411. {
  412. ctx_t *ctx = (ctx_t *)udata;
  413. faux_list_node_t *iter = NULL;
  414. uint32_t param_len = 0;
  415. char *param_data = NULL;
  416. uint16_t param_type = 0;
  417. char *prefix = NULL;
  418. faux_list_t *completions = NULL;
  419. size_t completions_num = 0;
  420. size_t max_compl_len = 0;
  421. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  422. process_prompt_param(ctx->tinyrl, msg);
  423. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  424. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  425. NULL, NULL, (void (*)(void *))faux_str_free);
  426. iter = faux_msg_init_param_iter(msg);
  427. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  428. char *compl = NULL;
  429. if (KTP_PARAM_LINE != param_type)
  430. continue;
  431. compl = faux_str_dupn(param_data, param_len);
  432. faux_list_add(completions, compl);
  433. if (param_len > max_compl_len)
  434. max_compl_len = param_len;
  435. }
  436. completions_num = faux_list_len(completions);
  437. // Single possible completion
  438. if (1 == completions_num) {
  439. char *compl = (char *)faux_list_data(faux_list_head(completions));
  440. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  441. // Add space after completion
  442. tinyrl_line_insert(ctx->tinyrl, " ", 1);
  443. tinyrl_redisplay(ctx->tinyrl);
  444. // Multi possible completions
  445. } else if (completions_num > 1) {
  446. faux_list_node_t *eq_iter = NULL;
  447. size_t eq_part = 0;
  448. char *str = NULL;
  449. char *compl = NULL;
  450. // Try to find equal part for all possible completions
  451. eq_iter = faux_list_head(completions);
  452. str = (char *)faux_list_data(eq_iter);
  453. eq_part = strlen(str);
  454. eq_iter = faux_list_next_node(eq_iter);
  455. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  456. size_t cur_eq = 0;
  457. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  458. if (cur_eq < eq_part)
  459. eq_part = cur_eq;
  460. }
  461. // The equal part was found
  462. if (eq_part > 0) {
  463. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  464. tinyrl_redisplay(ctx->tinyrl);
  465. // There is no equal part for all completions
  466. } else {
  467. tinyrl_multi_crlf(ctx->tinyrl);
  468. tinyrl_reset_line_state(ctx->tinyrl);
  469. display_completions(ctx->tinyrl, completions,
  470. prefix, max_compl_len);
  471. tinyrl_redisplay(ctx->tinyrl);
  472. }
  473. }
  474. faux_list_free(completions);
  475. faux_str_free(prefix);
  476. // Operation is finished so restore stdin handler
  477. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  478. stdin_cb, ctx);
  479. // Happy compiler
  480. ktp = ktp;
  481. msg = msg;
  482. return BOOL_TRUE;
  483. }
  484. static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
  485. size_t max)
  486. {
  487. faux_list_node_t *iter = NULL;
  488. faux_list_node_t *node = NULL;
  489. iter = faux_list_head(help_list);
  490. while ((node = faux_list_each_node(&iter))) {
  491. help_t *help = (help_t *)faux_list_data(node);
  492. tinyrl_printf(tinyrl, " %s%*s%s\n",
  493. help->prefix,
  494. (max + 2 - strlen(help->prefix)),
  495. " ",
  496. help->line);
  497. }
  498. }
  499. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  500. {
  501. ctx_t *ctx = (ctx_t *)udata;
  502. faux_list_t *help_list = NULL;
  503. faux_list_node_t *iter = NULL;
  504. uint32_t param_len = 0;
  505. char *param_data = NULL;
  506. uint16_t param_type = 0;
  507. size_t max_prefix_len = 0;
  508. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  509. process_prompt_param(ctx->tinyrl, msg);
  510. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  511. help_compare, NULL, help_free);
  512. // Wait for PREFIX - LINE pairs
  513. iter = faux_msg_init_param_iter(msg);
  514. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data,
  515. &param_len)) {
  516. char *prefix_str = NULL;
  517. char *line_str = NULL;
  518. help_t *help = NULL;
  519. size_t prefix_len = 0;
  520. // Get PREFIX
  521. if (KTP_PARAM_PREFIX != param_type)
  522. continue;
  523. prefix_str = faux_str_dupn(param_data, param_len);
  524. prefix_len = param_len;
  525. // Get LINE
  526. if (!faux_msg_get_param_each(&iter, &param_type,
  527. (void **)&param_data, &param_len) ||
  528. (KTP_PARAM_LINE != param_type)) {
  529. faux_str_free(prefix_str);
  530. break;
  531. }
  532. line_str = faux_str_dupn(param_data, param_len);
  533. help = help_new(prefix_str, line_str);
  534. if (!faux_list_add(help_list, help)) {
  535. help_free(help);
  536. continue;
  537. }
  538. if (prefix_len > max_prefix_len)
  539. max_prefix_len = prefix_len;
  540. }
  541. if (faux_list_len(help_list) > 0) {
  542. tinyrl_multi_crlf(ctx->tinyrl);
  543. tinyrl_reset_line_state(ctx->tinyrl);
  544. display_help(ctx->tinyrl, help_list, max_prefix_len);
  545. tinyrl_redisplay(ctx->tinyrl);
  546. }
  547. faux_list_free(help_list);
  548. // Operation is finished so restore stdin handler
  549. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  550. stdin_cb, ctx);
  551. ktp = ktp; // happy compiler
  552. return BOOL_TRUE;
  553. }
  554. static bool_t interactive_stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  555. void *udata)
  556. {
  557. ctx_t *ctx = (ctx_t *)udata;
  558. assert(ctx);
  559. // Start pager if necessary
  560. if (
  561. ctx->opts->pager_enabled && // Pager enabled within config file
  562. (ctx->pager_working == TRI_UNDEFINED) && // Pager is not working
  563. !(ktp_session_cmd_features(ktp) & KTP_STATUS_INTERACTIVE) // Non interactive command
  564. ) {
  565. ctx->pager_pipe = popen(ctx->opts->pager, "we");
  566. if (!ctx->pager_pipe)
  567. ctx->pager_working = TRI_FALSE; // Indicates can't start
  568. else
  569. ctx->pager_working = TRI_TRUE;
  570. }
  571. // Write to pager's pipe if pager is really working
  572. if (ctx->pager_working == TRI_TRUE) {
  573. if (faux_write_block(fileno(ctx->pager_pipe), line, len) <= 0) {
  574. // If we can't write to pager pipe then try stdout.
  575. if (faux_write_block(STDOUT_FILENO, line, len) < 0)
  576. return BOOL_FALSE;
  577. return BOOL_TRUE; // Don't break the loop
  578. }
  579. } else {
  580. if (faux_write_block(STDOUT_FILENO, line, len) < 0)
  581. return BOOL_FALSE;
  582. }
  583. return BOOL_TRUE;
  584. }