interactive.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 <faux/faux.h>
  11. #include <faux/str.h>
  12. #include <faux/eloop.h>
  13. #include <klish/ktp.h>
  14. #include <klish/ktp_session.h>
  15. #include <tinyrl/tinyrl.h>
  16. #include "private.h"
  17. // Context for main loop
  18. typedef struct ctx_s {
  19. ktp_session_t *ktp;
  20. tinyrl_t *tinyrl;
  21. struct options *opts;
  22. char *hotkeys[VT100_HOTKEY_MAP_LEN];
  23. tri_t pager_working;
  24. FILE *pager_pipe;
  25. } ctx_t;
  26. bool_t auth_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  27. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  28. bool_t cmd_incompleted_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  29. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  30. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  31. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  32. void *associated_data, void *user_data);
  33. static bool_t sigwinch_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  34. void *associated_data, void *user_data);
  35. static bool_t ktp_sync_auth(ktp_session_t *ktp, int *retcode,
  36. faux_error_t *error);
  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, ktp_session_error(ktp)))
  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: %s\n", err);
  188. }
  189. faux_error_free(error);
  190. // Operation is finished so restore stdin handler
  191. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  192. stdin_cb, ctx);
  193. // Happy compiler
  194. msg = msg;
  195. return BOOL_TRUE;
  196. }
  197. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  198. {
  199. ctx_t *ctx = (ctx_t *)udata;
  200. int rc = -1;
  201. faux_error_t *error = NULL;
  202. process_prompt_param(ctx->tinyrl, msg);
  203. process_hotkey_param(ctx, msg);
  204. if (!ktp_session_retcode(ktp, &rc))
  205. rc = -1;
  206. error = ktp_session_error(ktp);
  207. if ((rc < 0) && (faux_error_len(error) > 0)) {
  208. faux_error_node_t *err_iter = faux_error_iter(error);
  209. const char *err = NULL;
  210. while ((err = faux_error_each(&err_iter)))
  211. fprintf(stderr, "Error: %s\n", err);
  212. }
  213. faux_error_free(error);
  214. // Wait for pager
  215. if (ctx->pager_working == TRI_TRUE) {
  216. pclose(ctx->pager_pipe);
  217. ctx->pager_working = TRI_UNDEFINED;
  218. ctx->pager_pipe = NULL;
  219. }
  220. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  221. if (!ktp_session_done(ktp))
  222. tinyrl_redisplay(ctx->tinyrl);
  223. // Operation is finished so restore stdin handler
  224. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  225. stdin_cb, ctx);
  226. // Happy compiler
  227. msg = msg;
  228. return BOOL_TRUE;
  229. }
  230. bool_t cmd_incompleted_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  231. {
  232. ctx_t *ctx = (ctx_t *)udata;
  233. // Interactive command. So restore stdin handler.
  234. if ((ktp_session_state(ktp) == KTP_SESSION_STATE_WAIT_FOR_CMD) &&
  235. KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ktp))) {
  236. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  237. stdin_cb, ctx);
  238. }
  239. // Happy compiler
  240. msg = msg;
  241. return BOOL_TRUE;
  242. }
  243. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  244. void *associated_data, void *udata)
  245. {
  246. ctx_t *ctx = (ctx_t *)udata;
  247. ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
  248. if (!ctx)
  249. return BOOL_FALSE;
  250. state = ktp_session_state(ctx->ktp);
  251. // Standard klish command line
  252. if (state == KTP_SESSION_STATE_IDLE) {
  253. tinyrl_read(ctx->tinyrl);
  254. return BOOL_TRUE;
  255. }
  256. // Interactive command
  257. if ((state == KTP_SESSION_STATE_WAIT_FOR_CMD) &&
  258. KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ctx->ktp))) {
  259. int fd = fileno(tinyrl_istream(ctx->tinyrl));
  260. char buf[1024] = {};
  261. ssize_t bytes_readed = 0;
  262. while ((bytes_readed = read(fd, buf, sizeof(buf))) > 0) {
  263. ktp_session_stdin(ctx->ktp, buf, bytes_readed);
  264. if (bytes_readed != sizeof(buf))
  265. break;
  266. }
  267. return BOOL_TRUE;
  268. }
  269. // Here the situation when input is not allowed. Remove stdin from
  270. // eloop waiting list. Else klish will get 100% CPU. Callbacks on
  271. // operation completions will restore this handler.
  272. faux_eloop_del_fd(eloop, STDIN_FILENO);
  273. // Happy compiler
  274. eloop = eloop;
  275. type = type;
  276. associated_data = associated_data;
  277. return BOOL_TRUE;
  278. }
  279. static bool_t send_winch_notification(ctx_t *ctx)
  280. {
  281. size_t width = 0;
  282. size_t height = 0;
  283. char *winsize = NULL;
  284. faux_msg_t *req = NULL;
  285. ktp_status_e status = KTP_STATUS_NONE;
  286. if (!ctx->tinyrl)
  287. return BOOL_FALSE;
  288. if (!ctx->ktp)
  289. return BOOL_FALSE;
  290. tinyrl_winsize(ctx->tinyrl, &width, &height);
  291. winsize = faux_str_sprintf("%lu %lu", width, height);
  292. req = ktp_msg_preform(KTP_NOTIFICATION, status);
  293. faux_msg_add_param(req, KTP_PARAM_WINCH, winsize, strlen(winsize));
  294. faux_str_free(winsize);
  295. faux_msg_send_async(req, ktp_session_async(ctx->ktp));
  296. faux_msg_free(req);
  297. return BOOL_TRUE;
  298. }
  299. static bool_t sigwinch_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  300. void *associated_data, void *udata)
  301. {
  302. ctx_t *ctx = (ctx_t *)udata;
  303. if (!ctx)
  304. return BOOL_FALSE;
  305. send_winch_notification(ctx);
  306. // Happy compiler
  307. eloop = eloop;
  308. type = type;
  309. associated_data = associated_data;
  310. return BOOL_TRUE;
  311. }
  312. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  313. {
  314. const char *line = NULL;
  315. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  316. faux_error_t *error = faux_error_new();
  317. tinyrl_line_to_hist(tinyrl);
  318. tinyrl_multi_crlf(tinyrl);
  319. tinyrl_reset_line_state(tinyrl);
  320. line = tinyrl_line(tinyrl);
  321. // Don't do anything on empty line
  322. if (faux_str_is_empty(line)) {
  323. faux_error_free(error);
  324. return BOOL_TRUE;
  325. }
  326. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  327. tinyrl_reset_line(tinyrl);
  328. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  329. key = key; // Happy compiler
  330. return BOOL_TRUE;
  331. }
  332. static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key)
  333. {
  334. const char *line = NULL;
  335. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  336. faux_error_t *error = NULL;
  337. if (key >= VT100_HOTKEY_MAP_LEN)
  338. return BOOL_TRUE;
  339. line = ctx->hotkeys[key];
  340. if (faux_str_is_empty(line))
  341. return BOOL_TRUE;
  342. error = faux_error_new();
  343. tinyrl_multi_crlf(tinyrl);
  344. tinyrl_reset_line_state(tinyrl);
  345. tinyrl_reset_line(tinyrl);
  346. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  347. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  348. return BOOL_TRUE;
  349. }
  350. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  351. {
  352. const char *line = NULL;
  353. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  354. line = tinyrl_line(tinyrl);
  355. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  356. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  357. key = key; // Happy compiler
  358. return BOOL_TRUE;
  359. }
  360. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  361. {
  362. const char *line = NULL;
  363. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  364. line = tinyrl_line(tinyrl);
  365. ktp_session_help(ctx->ktp, line);
  366. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  367. key = key; // Happy compiler
  368. return BOOL_TRUE;
  369. }
  370. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  371. const char *prefix, size_t max)
  372. {
  373. size_t width = tinyrl_width(tinyrl);
  374. size_t cols = 0;
  375. faux_list_node_t *iter = NULL;
  376. faux_list_node_t *node = NULL;
  377. size_t prefix_len = 0;
  378. size_t cols_filled = 0;
  379. if (prefix)
  380. prefix_len = strlen(prefix);
  381. // Find out column and rows number
  382. if (max < width)
  383. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  384. else
  385. cols = 1;
  386. iter = faux_list_head(completions);
  387. while ((node = faux_list_each_node(&iter))) {
  388. char *compl = (char *)faux_list_data(node);
  389. tinyrl_printf(tinyrl, "%*s%s",
  390. (prefix_len + max + 1 - strlen(compl)),
  391. prefix ? prefix : "",
  392. compl);
  393. cols_filled++;
  394. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  395. cols_filled = 0;
  396. tinyrl_crlf(tinyrl);
  397. }
  398. }
  399. }
  400. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  401. {
  402. ctx_t *ctx = (ctx_t *)udata;
  403. faux_list_node_t *iter = NULL;
  404. uint32_t param_len = 0;
  405. char *param_data = NULL;
  406. uint16_t param_type = 0;
  407. char *prefix = NULL;
  408. faux_list_t *completions = NULL;
  409. size_t completions_num = 0;
  410. size_t max_compl_len = 0;
  411. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  412. process_prompt_param(ctx->tinyrl, msg);
  413. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  414. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  415. NULL, NULL, (void (*)(void *))faux_str_free);
  416. iter = faux_msg_init_param_iter(msg);
  417. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  418. char *compl = NULL;
  419. if (KTP_PARAM_LINE != param_type)
  420. continue;
  421. compl = faux_str_dupn(param_data, param_len);
  422. faux_list_add(completions, compl);
  423. if (param_len > max_compl_len)
  424. max_compl_len = param_len;
  425. }
  426. completions_num = faux_list_len(completions);
  427. // Single possible completion
  428. if (1 == completions_num) {
  429. char *compl = (char *)faux_list_data(faux_list_head(completions));
  430. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  431. // Add space after completion
  432. tinyrl_line_insert(ctx->tinyrl, " ", 1);
  433. tinyrl_redisplay(ctx->tinyrl);
  434. // Multi possible completions
  435. } else if (completions_num > 1) {
  436. faux_list_node_t *eq_iter = NULL;
  437. size_t eq_part = 0;
  438. char *str = NULL;
  439. char *compl = NULL;
  440. // Try to find equal part for all possible completions
  441. eq_iter = faux_list_head(completions);
  442. str = (char *)faux_list_data(eq_iter);
  443. eq_part = strlen(str);
  444. eq_iter = faux_list_next_node(eq_iter);
  445. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  446. size_t cur_eq = 0;
  447. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  448. if (cur_eq < eq_part)
  449. eq_part = cur_eq;
  450. }
  451. // The equal part was found
  452. if (eq_part > 0) {
  453. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  454. tinyrl_redisplay(ctx->tinyrl);
  455. // There is no equal part for all completions
  456. } else {
  457. tinyrl_multi_crlf(ctx->tinyrl);
  458. tinyrl_reset_line_state(ctx->tinyrl);
  459. display_completions(ctx->tinyrl, completions,
  460. prefix, max_compl_len);
  461. tinyrl_redisplay(ctx->tinyrl);
  462. }
  463. }
  464. faux_list_free(completions);
  465. faux_str_free(prefix);
  466. // Operation is finished so restore stdin handler
  467. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  468. stdin_cb, ctx);
  469. // Happy compiler
  470. ktp = ktp;
  471. msg = msg;
  472. return BOOL_TRUE;
  473. }
  474. static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
  475. size_t max)
  476. {
  477. faux_list_node_t *iter = NULL;
  478. faux_list_node_t *node = NULL;
  479. iter = faux_list_head(help_list);
  480. while ((node = faux_list_each_node(&iter))) {
  481. help_t *help = (help_t *)faux_list_data(node);
  482. tinyrl_printf(tinyrl, " %s%*s%s\n",
  483. help->prefix,
  484. (max + 2 - strlen(help->prefix)),
  485. " ",
  486. help->line);
  487. }
  488. }
  489. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  490. {
  491. ctx_t *ctx = (ctx_t *)udata;
  492. faux_list_t *help_list = NULL;
  493. faux_list_node_t *iter = NULL;
  494. uint32_t param_len = 0;
  495. char *param_data = NULL;
  496. uint16_t param_type = 0;
  497. size_t max_prefix_len = 0;
  498. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  499. process_prompt_param(ctx->tinyrl, msg);
  500. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_NONUNIQUE,
  501. help_compare, help_kcompare, help_free);
  502. // Wait for PREFIX - LINE pairs
  503. iter = faux_msg_init_param_iter(msg);
  504. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data,
  505. &param_len)) {
  506. char *prefix_str = NULL;
  507. char *line_str = NULL;
  508. help_t *help = NULL;
  509. size_t prefix_len = 0;
  510. // Get PREFIX
  511. if (KTP_PARAM_PREFIX != param_type)
  512. continue;
  513. prefix_str = faux_str_dupn(param_data, param_len);
  514. prefix_len = param_len;
  515. // Get LINE
  516. if (!faux_msg_get_param_each(&iter, &param_type,
  517. (void **)&param_data, &param_len) ||
  518. (KTP_PARAM_LINE != param_type)) {
  519. faux_str_free(prefix_str);
  520. break;
  521. }
  522. line_str = faux_str_dupn(param_data, param_len);
  523. help = help_new(prefix_str, line_str);
  524. faux_list_add(help_list, help);
  525. if (prefix_len > max_prefix_len)
  526. max_prefix_len = prefix_len;
  527. }
  528. if (faux_list_len(help_list) > 0) {
  529. tinyrl_multi_crlf(ctx->tinyrl);
  530. tinyrl_reset_line_state(ctx->tinyrl);
  531. display_help(ctx->tinyrl, help_list, max_prefix_len);
  532. tinyrl_redisplay(ctx->tinyrl);
  533. }
  534. faux_list_free(help_list);
  535. // Operation is finished so restore stdin handler
  536. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  537. stdin_cb, ctx);
  538. ktp = ktp; // happy compiler
  539. return BOOL_TRUE;
  540. }
  541. static bool_t ktp_sync_auth(ktp_session_t *ktp, int *retcode,
  542. faux_error_t *error)
  543. {
  544. if (!ktp_session_auth(ktp, error))
  545. return BOOL_FALSE;
  546. faux_eloop_loop(ktp_session_eloop(ktp));
  547. return ktp_session_retcode(ktp, retcode);
  548. }
  549. static bool_t interactive_stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  550. void *udata)
  551. {
  552. ctx_t *ctx = (ctx_t *)udata;
  553. assert(ctx);
  554. // Start pager if necessary
  555. if (
  556. ctx->opts->pager_enabled && // Pager enabled within config file
  557. (ctx->pager_working == TRI_UNDEFINED) && // Pager is not working
  558. !(ktp_session_cmd_features(ktp) & KTP_STATUS_INTERACTIVE) // Non interactive command
  559. ) {
  560. ctx->pager_pipe = popen(ctx->opts->pager, "we");
  561. if (!ctx->pager_pipe)
  562. ctx->pager_working = TRI_FALSE; // Indicates can't start
  563. else
  564. ctx->pager_working = TRI_TRUE;
  565. }
  566. // Write to pager's pipe if pager is really working
  567. if (ctx->pager_working == TRI_TRUE) {
  568. if (faux_write_block(fileno(ctx->pager_pipe), line, len) <= 0) {
  569. // If we can't write to pager pipe then try stdout.
  570. if (faux_write_block(STDOUT_FILENO, line, len) < 0)
  571. return BOOL_FALSE;
  572. return BOOL_TRUE; // Don't break the loop
  573. }
  574. } else {
  575. if (faux_write_block(STDOUT_FILENO, line, len) < 0)
  576. return BOOL_FALSE;
  577. }
  578. return BOOL_TRUE;
  579. }