interactive.c 17 KB

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