interactive.c 19 KB

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