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