interactive.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. process_prompt_param(ctx->tinyrl, msg);
  212. process_hotkey_param(ctx, msg);
  213. if (!ktp_session_retcode(ktp, &rc))
  214. rc = -1;
  215. error = ktp_session_error(ktp);
  216. if ((rc < 0) && (faux_error_len(error) > 0)) {
  217. faux_error_node_t *err_iter = faux_error_iter(error);
  218. const char *err = NULL;
  219. while ((err = faux_error_each(&err_iter)))
  220. fprintf(stderr, "Error: %s\n", err);
  221. }
  222. faux_error_free(error);
  223. // Wait for pager
  224. if (ctx->pager_working != TRI_UNDEFINED) {
  225. pclose(ctx->pager_pipe);
  226. ctx->pager_working = TRI_UNDEFINED;
  227. ctx->pager_pipe = NULL;
  228. }
  229. // Disable SIGINT caught for non-interactive commands.
  230. // Do it after pager exit. Else it can restore wrong tty mode after
  231. // ISIG disabling
  232. tinyrl_disable_isig(ctx->tinyrl);
  233. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  234. if (!ktp_session_done(ktp))
  235. tinyrl_redisplay(ctx->tinyrl);
  236. // Operation is finished so restore stdin handler
  237. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  238. stdin_cb, ctx);
  239. // Happy compiler
  240. msg = msg;
  241. return BOOL_TRUE;
  242. }
  243. bool_t cmd_incompleted_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  244. {
  245. ctx_t *ctx = (ctx_t *)udata;
  246. if (ktp_session_state(ktp) == KTP_SESSION_STATE_WAIT_FOR_CMD) {
  247. // Interactive command. So restore stdin handler.
  248. if (KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ktp))) {
  249. // Disable SIGINT signal
  250. tinyrl_disable_isig(ctx->tinyrl);
  251. // Interactive command. So restore stdin handler.
  252. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  253. stdin_cb, ctx);
  254. }
  255. }
  256. // Happy compiler
  257. msg = msg;
  258. return BOOL_TRUE;
  259. }
  260. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  261. void *associated_data, void *udata)
  262. {
  263. bool_t rc = BOOL_TRUE;
  264. ctx_t *ctx = (ctx_t *)udata;
  265. ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
  266. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  267. if (!ctx)
  268. return BOOL_FALSE;
  269. // Some errors or fd is closed so stop session
  270. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  271. rc = BOOL_FALSE;
  272. state = ktp_session_state(ctx->ktp);
  273. // Standard klish command line
  274. if (state == KTP_SESSION_STATE_IDLE) {
  275. tinyrl_read(ctx->tinyrl);
  276. return rc;
  277. }
  278. // Interactive command
  279. if ((state == KTP_SESSION_STATE_WAIT_FOR_CMD) &&
  280. KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ctx->ktp))) {
  281. int fd = fileno(tinyrl_istream(ctx->tinyrl));
  282. char buf[1024] = {};
  283. ssize_t bytes_readed = 0;
  284. while ((bytes_readed = read(fd, buf, sizeof(buf))) > 0) {
  285. ktp_session_stdin(ctx->ktp, buf, bytes_readed);
  286. if (bytes_readed != sizeof(buf))
  287. break;
  288. }
  289. return rc;
  290. }
  291. // Here the situation when input is not allowed. Remove stdin from
  292. // eloop waiting list. Else klish will get 100% CPU. Callbacks on
  293. // operation completions will restore this handler.
  294. faux_eloop_del_fd(eloop, STDIN_FILENO);
  295. // Happy compiler
  296. eloop = eloop;
  297. type = type;
  298. return rc;
  299. }
  300. static bool_t send_winch_notification(ctx_t *ctx)
  301. {
  302. size_t width = 0;
  303. size_t height = 0;
  304. char *winsize = NULL;
  305. faux_msg_t *req = NULL;
  306. ktp_status_e status = KTP_STATUS_NONE;
  307. if (!ctx->tinyrl)
  308. return BOOL_FALSE;
  309. if (!ctx->ktp)
  310. return BOOL_FALSE;
  311. tinyrl_winsize(ctx->tinyrl, &width, &height);
  312. winsize = faux_str_sprintf("%lu %lu", width, height);
  313. req = ktp_msg_preform(KTP_NOTIFICATION, status);
  314. faux_msg_add_param(req, KTP_PARAM_WINCH, winsize, strlen(winsize));
  315. faux_str_free(winsize);
  316. faux_msg_send_async(req, ktp_session_async(ctx->ktp));
  317. faux_msg_free(req);
  318. return BOOL_TRUE;
  319. }
  320. static bool_t sigwinch_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  321. void *associated_data, void *udata)
  322. {
  323. ctx_t *ctx = (ctx_t *)udata;
  324. if (!ctx)
  325. return BOOL_FALSE;
  326. send_winch_notification(ctx);
  327. // Happy compiler
  328. eloop = eloop;
  329. type = type;
  330. associated_data = associated_data;
  331. return BOOL_TRUE;
  332. }
  333. static bool_t ctrl_c_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. char ctrl_c = KEY_ETX;
  338. if (!ctx)
  339. return BOOL_FALSE;
  340. ktp_session_stdin(ctx->ktp, &ctrl_c, sizeof(ctrl_c));
  341. // Happy compiler
  342. eloop = eloop;
  343. type = type;
  344. associated_data = associated_data;
  345. return BOOL_TRUE;
  346. }
  347. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  348. {
  349. const char *line = NULL;
  350. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  351. faux_error_t *error = faux_error_new();
  352. tinyrl_line_to_hist(tinyrl);
  353. tinyrl_multi_crlf(tinyrl);
  354. tinyrl_reset_line_state(tinyrl);
  355. line = tinyrl_line(tinyrl);
  356. // Don't do anything on empty line
  357. if (faux_str_is_empty(line)) {
  358. faux_error_free(error);
  359. return BOOL_TRUE;
  360. }
  361. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  362. tinyrl_reset_line(tinyrl);
  363. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  364. // Suppose non-interactive command
  365. // Caught SIGINT for non-interactive commands
  366. tinyrl_enable_isig(tinyrl);
  367. key = key; // Happy compiler
  368. return BOOL_TRUE;
  369. }
  370. static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key)
  371. {
  372. const char *line = NULL;
  373. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  374. faux_error_t *error = NULL;
  375. if (key >= VT100_HOTKEY_MAP_LEN)
  376. return BOOL_TRUE;
  377. line = ctx->hotkeys[key];
  378. if (faux_str_is_empty(line))
  379. return BOOL_TRUE;
  380. error = faux_error_new();
  381. tinyrl_multi_crlf(tinyrl);
  382. tinyrl_reset_line_state(tinyrl);
  383. tinyrl_reset_line(tinyrl);
  384. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  385. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  386. return BOOL_TRUE;
  387. }
  388. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  389. {
  390. char *line = NULL;
  391. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  392. line = tinyrl_line_to_pos(tinyrl);
  393. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  394. faux_str_free(line);
  395. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  396. key = key; // Happy compiler
  397. return BOOL_TRUE;
  398. }
  399. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  400. {
  401. char *line = NULL;
  402. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  403. line = tinyrl_line_to_pos(tinyrl);
  404. // If "?" is quoted then it's not special hotkey.
  405. // Just insert it into the line.
  406. if (faux_str_unclosed_quotes(line, NULL)) {
  407. faux_str_free(line);
  408. return tinyrl_key_default(tinyrl, key);
  409. }
  410. ktp_session_help(ctx->ktp, line);
  411. faux_str_free(line);
  412. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  413. key = key; // Happy compiler
  414. return BOOL_TRUE;
  415. }
  416. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  417. const char *prefix, size_t max)
  418. {
  419. size_t width = tinyrl_width(tinyrl);
  420. size_t cols = 0;
  421. faux_list_node_t *iter = NULL;
  422. faux_list_node_t *node = NULL;
  423. size_t prefix_len = 0;
  424. size_t cols_filled = 0;
  425. if (prefix)
  426. prefix_len = strlen(prefix);
  427. // Find out column and rows number
  428. if (max < width)
  429. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  430. else
  431. cols = 1;
  432. iter = faux_list_head(completions);
  433. while ((node = faux_list_each_node(&iter))) {
  434. char *compl = (char *)faux_list_data(node);
  435. tinyrl_printf(tinyrl, "%*s%s",
  436. (prefix_len + max + 1 - strlen(compl)),
  437. prefix ? prefix : "",
  438. compl);
  439. cols_filled++;
  440. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  441. cols_filled = 0;
  442. tinyrl_crlf(tinyrl);
  443. }
  444. }
  445. }
  446. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  447. {
  448. ctx_t *ctx = (ctx_t *)udata;
  449. faux_list_node_t *iter = NULL;
  450. uint32_t param_len = 0;
  451. char *param_data = NULL;
  452. uint16_t param_type = 0;
  453. char *prefix = NULL;
  454. faux_list_t *completions = NULL;
  455. size_t completions_num = 0;
  456. size_t max_compl_len = 0;
  457. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  458. process_prompt_param(ctx->tinyrl, msg);
  459. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  460. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  461. NULL, NULL, (void (*)(void *))faux_str_free);
  462. iter = faux_msg_init_param_iter(msg);
  463. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  464. char *compl = NULL;
  465. if (KTP_PARAM_LINE != param_type)
  466. continue;
  467. compl = faux_str_dupn(param_data, param_len);
  468. faux_list_add(completions, compl);
  469. if (param_len > max_compl_len)
  470. max_compl_len = param_len;
  471. }
  472. completions_num = faux_list_len(completions);
  473. // Single possible completion
  474. if (1 == completions_num) {
  475. char *compl = (char *)faux_list_data(faux_list_head(completions));
  476. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  477. // Add space after completion
  478. tinyrl_line_insert(ctx->tinyrl, " ", 1);
  479. tinyrl_redisplay(ctx->tinyrl);
  480. // Multi possible completions
  481. } else if (completions_num > 1) {
  482. faux_list_node_t *eq_iter = NULL;
  483. size_t eq_part = 0;
  484. char *str = NULL;
  485. char *compl = NULL;
  486. // Try to find equal part for all possible completions
  487. eq_iter = faux_list_head(completions);
  488. str = (char *)faux_list_data(eq_iter);
  489. eq_part = strlen(str);
  490. eq_iter = faux_list_next_node(eq_iter);
  491. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  492. size_t cur_eq = 0;
  493. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  494. if (cur_eq < eq_part)
  495. eq_part = cur_eq;
  496. }
  497. // The equal part was found
  498. if (eq_part > 0) {
  499. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  500. tinyrl_redisplay(ctx->tinyrl);
  501. // There is no equal part for all completions
  502. } else {
  503. tinyrl_multi_crlf(ctx->tinyrl);
  504. tinyrl_reset_line_state(ctx->tinyrl);
  505. display_completions(ctx->tinyrl, completions,
  506. prefix, max_compl_len);
  507. tinyrl_redisplay(ctx->tinyrl);
  508. }
  509. }
  510. faux_list_free(completions);
  511. faux_str_free(prefix);
  512. // Operation is finished so restore stdin handler
  513. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  514. stdin_cb, ctx);
  515. // Happy compiler
  516. ktp = ktp;
  517. msg = msg;
  518. return BOOL_TRUE;
  519. }
  520. static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
  521. size_t max)
  522. {
  523. faux_list_node_t *iter = NULL;
  524. faux_list_node_t *node = NULL;
  525. iter = faux_list_head(help_list);
  526. while ((node = faux_list_each_node(&iter))) {
  527. help_t *help = (help_t *)faux_list_data(node);
  528. tinyrl_printf(tinyrl, " %s%*s%s\n",
  529. help->prefix,
  530. (max + 2 - strlen(help->prefix)),
  531. " ",
  532. help->line);
  533. }
  534. }
  535. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  536. {
  537. ctx_t *ctx = (ctx_t *)udata;
  538. faux_list_t *help_list = NULL;
  539. faux_list_node_t *iter = NULL;
  540. uint32_t param_len = 0;
  541. char *param_data = NULL;
  542. uint16_t param_type = 0;
  543. size_t max_prefix_len = 0;
  544. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  545. process_prompt_param(ctx->tinyrl, msg);
  546. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  547. help_compare, NULL, help_free);
  548. // Wait for PREFIX - LINE pairs
  549. iter = faux_msg_init_param_iter(msg);
  550. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data,
  551. &param_len)) {
  552. char *prefix_str = NULL;
  553. char *line_str = NULL;
  554. help_t *help = NULL;
  555. size_t prefix_len = 0;
  556. // Get PREFIX
  557. if (KTP_PARAM_PREFIX != param_type)
  558. continue;
  559. prefix_str = faux_str_dupn(param_data, param_len);
  560. prefix_len = param_len;
  561. // Get LINE
  562. if (!faux_msg_get_param_each(&iter, &param_type,
  563. (void **)&param_data, &param_len) ||
  564. (KTP_PARAM_LINE != param_type)) {
  565. faux_str_free(prefix_str);
  566. break;
  567. }
  568. line_str = faux_str_dupn(param_data, param_len);
  569. help = help_new(prefix_str, line_str);
  570. if (!faux_list_add(help_list, help)) {
  571. help_free(help);
  572. continue;
  573. }
  574. if (prefix_len > max_prefix_len)
  575. max_prefix_len = prefix_len;
  576. }
  577. if (faux_list_len(help_list) > 0) {
  578. tinyrl_multi_crlf(ctx->tinyrl);
  579. tinyrl_reset_line_state(ctx->tinyrl);
  580. display_help(ctx->tinyrl, help_list, max_prefix_len);
  581. tinyrl_redisplay(ctx->tinyrl);
  582. }
  583. faux_list_free(help_list);
  584. // Operation is finished so restore stdin handler
  585. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  586. stdin_cb, ctx);
  587. ktp = ktp; // happy compiler
  588. return BOOL_TRUE;
  589. }
  590. static bool_t interactive_stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  591. void *udata)
  592. {
  593. ctx_t *ctx = (ctx_t *)udata;
  594. assert(ctx);
  595. // Start pager if necessary
  596. if (
  597. ctx->opts->pager_enabled && // Pager enabled within config file
  598. (ctx->pager_working == TRI_UNDEFINED) && // Pager is not working
  599. !(ktp_session_cmd_features(ktp) & KTP_STATUS_INTERACTIVE) // Non interactive command
  600. ) {
  601. ctx->pager_pipe = popen(ctx->opts->pager, "we");
  602. if (!ctx->pager_pipe)
  603. ctx->pager_working = TRI_FALSE; // Indicates can't start
  604. else
  605. ctx->pager_working = TRI_TRUE;
  606. }
  607. // Write to pager's pipe if pager is really working
  608. // Don't do anything if pager state is TRI_FALSE
  609. if (ctx->pager_working == TRI_TRUE) {
  610. if (faux_write_block(fileno(ctx->pager_pipe), line, len) <= 0) {
  611. // If we can't write to pager pipe then send
  612. // "SIGPIPE" to server. Pager is finished or broken.
  613. ktp_session_stdout_close(ktp);
  614. ctx->pager_working = TRI_FALSE;
  615. return BOOL_TRUE; // Don't break the loop
  616. }
  617. // TRI_UNDEFINED here means that pager is not needed
  618. } else if (ctx->pager_working == TRI_UNDEFINED) {
  619. if (faux_write_block(STDOUT_FILENO, line, len) < 0)
  620. return BOOL_FALSE;
  621. }
  622. return BOOL_TRUE;
  623. }