interactive.c 20 KB

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