interactive.c 20 KB

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