interactive.c 19 KB

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