interactive.c 20 KB

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