interactive.c 19 KB

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