klish.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <getopt.h>
  12. #include <sys/socket.h>
  13. #include <sys/un.h>
  14. #include <syslog.h>
  15. #include <sys/wait.h>
  16. #include <errno.h>
  17. #ifdef HAVE_LOCALE_H
  18. #include <locale.h>
  19. #endif
  20. #ifdef HAVE_LANGINFO_CODESET
  21. #include <langinfo.h>
  22. #endif
  23. #include <faux/faux.h>
  24. #include <faux/str.h>
  25. #include <faux/msg.h>
  26. #include <faux/list.h>
  27. #include <faux/file.h>
  28. #include <faux/eloop.h>
  29. #include <klish/ktp.h>
  30. #include <klish/ktp_session.h>
  31. #include <tinyrl/tinyrl.h>
  32. #include "private.h"
  33. // Client mode
  34. typedef enum {
  35. MODE_CMDLINE,
  36. MODE_FILES,
  37. MODE_STDIN,
  38. MODE_INTERACTIVE
  39. } client_mode_e;
  40. // Context for main loop
  41. typedef struct ctx_s {
  42. ktp_session_t *ktp;
  43. tinyrl_t *tinyrl;
  44. struct options *opts;
  45. char *hotkeys[VT100_HOTKEY_MAP_LEN]; // MODE_INTERACTIVE
  46. // pager_working flag values:
  47. // TRI_UNDEFINED - Not started yet or not necessary
  48. // TRI_TRUE - Pager is working
  49. // TRI_FALSE - Can't start pager or pager has exited
  50. tri_t pager_working;
  51. FILE *pager_pipe;
  52. client_mode_e mode;
  53. faux_list_node_t *cmdline_iter; // MODE_CMDLINE
  54. } ctx_t;
  55. // KTP session static functions
  56. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  57. void *user_data);
  58. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  59. void *user_data);
  60. static bool_t auth_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  61. static bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  62. static bool_t cmd_incompleted_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  63. static bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  64. static bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  65. // Eloop callbacks
  66. //static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  67. // void *associated_data, void *user_data);
  68. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  69. void *associated_data, void *user_data);
  70. static bool_t sigwinch_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  71. void *associated_data, void *user_data);
  72. static bool_t ctrl_c_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  73. void *associated_data, void *user_data);
  74. // Service functions
  75. static void reset_hotkey_table(ctx_t *ctx);
  76. static bool_t send_winch_notification(ctx_t *ctx);
  77. static bool_t send_next_command(ctx_t *ctx);
  78. // Keys
  79. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
  80. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key);
  81. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key);
  82. static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key);
  83. int main(int argc, char **argv)
  84. {
  85. int retval = -1;
  86. struct options *opts = NULL;
  87. int unix_sock = -1;
  88. ktp_session_t *ktp = NULL;
  89. int retcode = 0;
  90. faux_eloop_t *eloop = NULL;
  91. ctx_t ctx = {};
  92. tinyrl_t *tinyrl = NULL;
  93. char *hist_path = NULL;
  94. int stdin_flags = 0;
  95. #ifdef HAVE_LOCALE_H
  96. // Set current locale
  97. setlocale(LC_ALL, "");
  98. #endif
  99. // Parse command line options
  100. opts = opts_init();
  101. if (opts_parse(argc, argv, opts)) {
  102. fprintf(stderr, "Error: Can't parse command line options\n");
  103. goto err;
  104. }
  105. // Parse config file
  106. if (!access(opts->cfgfile, R_OK)) {
  107. if (!config_parse(opts->cfgfile, opts))
  108. goto err;
  109. } else if (opts->cfgfile_userdefined) {
  110. // User defined config must be found
  111. fprintf(stderr, "Error: Can't find config file %s\n",
  112. opts->cfgfile);
  113. goto err;
  114. }
  115. // Init context
  116. faux_bzero(&ctx, sizeof(ctx));
  117. // Find out client mode
  118. ctx.mode = MODE_INTERACTIVE; // Default
  119. if (faux_list_len(opts->commands) > 0)
  120. ctx.mode = MODE_CMDLINE;
  121. else if (faux_list_len(opts->files) > 0)
  122. ctx.mode = MODE_FILES;
  123. else if (!isatty(STDIN_FILENO))
  124. ctx.mode = MODE_STDIN;
  125. // Connect to server
  126. unix_sock = ktp_connect_unix(opts->unix_socket_path);
  127. if (unix_sock < 0) {
  128. fprintf(stderr, "Error: Can't connect to server\n");
  129. goto err;
  130. }
  131. // Eloop object
  132. eloop = faux_eloop_new(NULL);
  133. // faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  134. // faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  135. // faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  136. // Handlers are used to send SIGINT
  137. // to non-interactive commands
  138. faux_eloop_add_signal(eloop, SIGINT, ctrl_c_cb, &ctx);
  139. faux_eloop_add_signal(eloop, SIGTERM, ctrl_c_cb, &ctx);
  140. faux_eloop_add_signal(eloop, SIGQUIT, ctrl_c_cb, &ctx);
  141. // To don't stop klish client on exit. SIGTSTP can be pended
  142. faux_eloop_add_signal(eloop, SIGTSTP, ctrl_c_cb, &ctx);
  143. // Notify server about terminal window size change
  144. faux_eloop_add_signal(eloop, SIGWINCH, sigwinch_cb, &ctx);
  145. // KTP session
  146. ktp = ktp_session_new(unix_sock, eloop);
  147. assert(ktp);
  148. if (!ktp) {
  149. fprintf(stderr, "Error: Can't create klish session\n");
  150. goto err;
  151. }
  152. // Don't stop loop on each answer
  153. ktp_session_set_stop_on_answer(ktp, BOOL_FALSE);
  154. // Ignore SIGPIPE from server
  155. signal(SIGPIPE, SIG_IGN);
  156. // Set stdin to O_NONBLOCK mode
  157. stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  158. fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
  159. // TiniRL
  160. if (ctx.mode == MODE_INTERACTIVE)
  161. hist_path = faux_expand_tilde("~/.klish_history");
  162. tinyrl = tinyrl_new(stdin, stdout, hist_path, 100);
  163. if (ctx.mode == MODE_INTERACTIVE)
  164. faux_str_free(hist_path);
  165. tinyrl_set_prompt(tinyrl, "$ ");
  166. tinyrl_set_udata(tinyrl, &ctx);
  167. // Populate context
  168. ctx.ktp = ktp;
  169. ctx.tinyrl = tinyrl;
  170. ctx.opts = opts;
  171. faux_bzero(ctx.hotkeys, sizeof(ctx.hotkeys));
  172. ctx.pager_working = TRI_UNDEFINED;
  173. ctx.pager_pipe = NULL;
  174. ktp_session_set_cb(ktp, KTP_SESSION_CB_STDOUT, stdout_cb, &ctx);
  175. ktp_session_set_cb(ktp, KTP_SESSION_CB_STDERR, stderr_cb, &ctx);
  176. ktp_session_set_cb(ktp, KTP_SESSION_CB_AUTH_ACK, auth_ack_cb, &ctx);
  177. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK, cmd_ack_cb, &ctx);
  178. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK_INCOMPLETED,
  179. cmd_incompleted_ack_cb, &ctx);
  180. ktp_session_set_cb(ktp, KTP_SESSION_CB_COMPLETION_ACK,
  181. completion_ack_cb, &ctx);
  182. ktp_session_set_cb(ktp, KTP_SESSION_CB_HELP_ACK, help_ack_cb, &ctx);
  183. // Commands from cmdline
  184. if (ctx.mode == MODE_CMDLINE) {
  185. // "-c" options iterator
  186. ctx.cmdline_iter = faux_list_head(opts->commands);
  187. // Commands from files
  188. } else if (ctx.mode == MODE_FILES) {
  189. const char *filename = NULL;
  190. faux_list_node_t *iter = faux_list_head(opts->files);
  191. while ((filename = (const char *)faux_list_each(&iter))) {
  192. char *line = NULL;
  193. bool_t stop = BOOL_FALSE;
  194. faux_file_t *fd = faux_file_open(filename, O_RDONLY, 0);
  195. while ((line = faux_file_getline(fd))) {
  196. // Request to server
  197. // retcode = ktp_sync_cmd(ktp, line, opts);
  198. faux_str_free(line);
  199. // Stop-on-error
  200. if (opts->stop_on_error && (retcode != 0)) {
  201. stop = BOOL_TRUE;
  202. break;
  203. }
  204. }
  205. faux_file_close(fd);
  206. if (stop)
  207. break;
  208. }
  209. // Commands from non-interactive STDIN
  210. } else if (ctx.mode == MODE_STDIN) {
  211. char *line = NULL;
  212. faux_file_t *fd = faux_file_fdopen(STDIN_FILENO);
  213. while ((line = faux_file_getline(fd))) {
  214. // Request to server
  215. // retcode = ktp_sync_cmd(ktp, line, opts);
  216. faux_str_free(line);
  217. // Stop-on-error
  218. if (opts->stop_on_error && (retcode != 0))
  219. break;
  220. }
  221. faux_file_close(fd);
  222. // Interactive shell
  223. } else {
  224. // Interactive keys
  225. tinyrl_set_hotkey_fn(tinyrl, tinyrl_key_hotkey);
  226. tinyrl_bind_key(tinyrl, '\n', tinyrl_key_enter);
  227. tinyrl_bind_key(tinyrl, '\r', tinyrl_key_enter);
  228. tinyrl_bind_key(tinyrl, '\t', tinyrl_key_tab);
  229. tinyrl_bind_key(tinyrl, '?', tinyrl_key_help);
  230. faux_eloop_add_fd(eloop, STDIN_FILENO, POLLIN, stdin_cb, &ctx);
  231. }
  232. // Send AUTH message to server
  233. if (!ktp_session_auth(ktp, NULL))
  234. goto err;
  235. // Main loop
  236. faux_eloop_loop(eloop);
  237. retval = 0;
  238. err:
  239. // Restore stdin mode
  240. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  241. reset_hotkey_table(&ctx);
  242. if (tinyrl) {
  243. if (tinyrl_busy(tinyrl))
  244. faux_error_free(ktp_session_error(ktp));
  245. tinyrl_free(tinyrl);
  246. }
  247. ktp_session_free(ktp);
  248. faux_eloop_free(eloop);
  249. ktp_disconnect(unix_sock);
  250. opts_free(opts);
  251. if ((retval < 0) || (retcode != 0))
  252. return -1;
  253. return 0;
  254. }
  255. static bool_t send_next_command(ctx_t *ctx)
  256. {
  257. const char *line = NULL;
  258. faux_error_t *error = NULL;
  259. // User must type next interactive command. So just return
  260. if (ctx->mode == MODE_INTERACTIVE)
  261. return BOOL_TRUE;
  262. // Commands from cmdline
  263. if (ctx->mode == MODE_CMDLINE)
  264. line = faux_list_each(&ctx->cmdline_iter);
  265. if (!line) {
  266. ktp_session_set_done(ctx->ktp, BOOL_TRUE);
  267. return BOOL_TRUE;
  268. }
  269. if (!ctx->opts->quiet) {
  270. const char *prompt = tinyrl_prompt(ctx->tinyrl);
  271. printf("%s%s\n", prompt ? prompt : "", line);
  272. }
  273. error = faux_error_new();
  274. if (!ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run)) {
  275. faux_error_free(error);
  276. return BOOL_FALSE;
  277. }
  278. // Suppose non-interactive command by default
  279. tinyrl_enable_isig(ctx->tinyrl);
  280. return BOOL_TRUE;
  281. }
  282. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  283. void *user_data)
  284. {
  285. if (faux_write_block(STDERR_FILENO, line, len) < 0)
  286. return BOOL_FALSE;
  287. ktp = ktp;
  288. user_data = user_data;
  289. return BOOL_TRUE;
  290. }
  291. /*
  292. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  293. void *associated_data, void *user_data)
  294. {
  295. // Happy compiler
  296. eloop = eloop;
  297. type = type;
  298. associated_data = associated_data;
  299. user_data = user_data;
  300. return BOOL_FALSE; // Stop Event Loop
  301. }
  302. */
  303. static bool_t process_prompt_param(tinyrl_t *tinyrl, const faux_msg_t *msg)
  304. {
  305. char *prompt = NULL;
  306. if (!tinyrl)
  307. return BOOL_FALSE;
  308. if (!msg)
  309. return BOOL_FALSE;
  310. prompt = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PROMPT);
  311. if (prompt) {
  312. tinyrl_set_prompt(tinyrl, prompt);
  313. faux_str_free(prompt);
  314. }
  315. return BOOL_TRUE;
  316. }
  317. static void reset_hotkey_table(ctx_t *ctx)
  318. {
  319. size_t i = 0;
  320. assert(ctx);
  321. for (i = 0; i < VT100_HOTKEY_MAP_LEN; i++)
  322. faux_str_free(ctx->hotkeys[i]);
  323. faux_bzero(ctx->hotkeys, sizeof(ctx->hotkeys));
  324. }
  325. static bool_t process_hotkey_param(ctx_t *ctx, const faux_msg_t *msg)
  326. {
  327. faux_list_node_t *iter = NULL;
  328. uint32_t param_len = 0;
  329. char *param_data = NULL;
  330. uint16_t param_type = 0;
  331. if (!ctx)
  332. return BOOL_FALSE;
  333. if (!msg)
  334. return BOOL_FALSE;
  335. if (!faux_msg_get_param_by_type(msg, KTP_PARAM_HOTKEY,
  336. (void **)&param_data, &param_len))
  337. return BOOL_TRUE;
  338. // If there is HOTKEY parameter then reinitialize whole hotkey table
  339. reset_hotkey_table(ctx);
  340. iter = faux_msg_init_param_iter(msg);
  341. while (faux_msg_get_param_each(
  342. &iter, &param_type, (void **)&param_data, &param_len)) {
  343. char *cmd = NULL;
  344. ssize_t code = -1;
  345. size_t key_len = 0;
  346. if (param_len < 3) // <key>'\0'<cmd>
  347. continue;
  348. if (KTP_PARAM_HOTKEY != param_type)
  349. continue;
  350. key_len = strlen(param_data); // Length of <key>
  351. if (key_len < 1)
  352. continue;
  353. code = vt100_hotkey_decode(param_data);
  354. if ((code < 0) || (code > VT100_HOTKEY_MAP_LEN))
  355. continue;
  356. cmd = faux_str_dupn(param_data + key_len + 1,
  357. param_len - key_len - 1);
  358. if (!cmd)
  359. continue;
  360. ctx->hotkeys[code] = cmd;
  361. }
  362. return BOOL_TRUE;
  363. }
  364. bool_t auth_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  365. {
  366. ctx_t *ctx = (ctx_t *)udata;
  367. int rc = -1;
  368. faux_error_t *error = NULL;
  369. process_prompt_param(ctx->tinyrl, msg);
  370. process_hotkey_param(ctx, msg);
  371. if (!ktp_session_retcode(ktp, &rc))
  372. rc = -1;
  373. error = ktp_session_error(ktp);
  374. if ((rc < 0) && (faux_error_len(error) > 0)) {
  375. faux_error_node_t *err_iter = faux_error_iter(error);
  376. const char *err = NULL;
  377. while ((err = faux_error_each(&err_iter)))
  378. fprintf(stderr, "Error: auth: %s\n", err);
  379. return BOOL_FALSE;
  380. }
  381. if (isatty(STDIN_FILENO))
  382. send_winch_notification(ctx);
  383. if (ctx->mode == MODE_INTERACTIVE) {
  384. // Print prompt for interactive command
  385. tinyrl_redisplay(ctx->tinyrl);
  386. } else {
  387. // Send first command for non-interactive modes
  388. send_next_command(ctx);
  389. }
  390. // Happy compiler
  391. msg = msg;
  392. return BOOL_TRUE;
  393. }
  394. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  395. {
  396. ctx_t *ctx = (ctx_t *)udata;
  397. int rc = -1;
  398. faux_error_t *error = NULL;
  399. bool_t it_was_pager = BOOL_FALSE;
  400. // Wait for pager
  401. if (ctx->pager_working != TRI_UNDEFINED) {
  402. pclose(ctx->pager_pipe);
  403. ctx->pager_working = TRI_UNDEFINED;
  404. ctx->pager_pipe = NULL;
  405. it_was_pager = BOOL_TRUE;
  406. }
  407. // Disable SIGINT caught for non-interactive commands.
  408. // Do it after pager exit. Else it can restore wrong tty mode after
  409. // ISIG disabling
  410. tinyrl_disable_isig(ctx->tinyrl);
  411. // Sometimes output stream from server doesn't contain final crlf so
  412. // goto newline itself
  413. if (ktp_session_last_stream(ktp) == STDERR_FILENO) {
  414. if (ktp_session_stderr_need_newline(ktp))
  415. fprintf(stderr, "\n");
  416. } else {
  417. // Pager adds newline itself
  418. if (ktp_session_stdout_need_newline(ktp) && !it_was_pager)
  419. tinyrl_crlf(ctx->tinyrl);
  420. }
  421. process_prompt_param(ctx->tinyrl, msg);
  422. process_hotkey_param(ctx, msg);
  423. if (!ktp_session_retcode(ktp, &rc))
  424. rc = -1;
  425. error = ktp_session_error(ktp);
  426. if (rc < 0) {
  427. if (faux_error_len(error) > 0) {
  428. faux_error_node_t *err_iter = faux_error_iter(error);
  429. const char *err = NULL;
  430. while ((err = faux_error_each(&err_iter)))
  431. fprintf(stderr, "Error: %s\n", err);
  432. }
  433. // Stop-on-error
  434. if (ctx->opts->stop_on_error) {
  435. faux_error_free(error);
  436. ktp_session_set_done(ktp, BOOL_TRUE);
  437. return BOOL_TRUE;
  438. }
  439. }
  440. faux_error_free(error);
  441. if (ctx->mode == MODE_INTERACTIVE) {
  442. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  443. if (!ktp_session_done(ktp))
  444. tinyrl_redisplay(ctx->tinyrl);
  445. }
  446. // Operation is finished so restore stdin handler
  447. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  448. stdin_cb, ctx);
  449. // Send next command for non-interactive modes
  450. send_next_command(ctx);
  451. // Happy compiler
  452. msg = msg;
  453. return BOOL_TRUE;
  454. }
  455. bool_t cmd_incompleted_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  456. {
  457. ctx_t *ctx = (ctx_t *)udata;
  458. if (ktp_session_state(ktp) == KTP_SESSION_STATE_WAIT_FOR_CMD) {
  459. // Cmd need stdin so restore stdin handler
  460. if (KTP_STATUS_IS_NEED_STDIN(ktp_session_cmd_features(ktp))) {
  461. // Disable SIGINT signal (it is used for commands that
  462. // don't need stdin. Commands with stdin can get ^C
  463. // themself interactively.)
  464. tinyrl_disable_isig(ctx->tinyrl);
  465. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  466. stdin_cb, ctx);
  467. }
  468. }
  469. // Happy compiler
  470. msg = msg;
  471. return BOOL_TRUE;
  472. }
  473. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  474. void *associated_data, void *udata)
  475. {
  476. bool_t rc = BOOL_TRUE;
  477. ctx_t *ctx = (ctx_t *)udata;
  478. ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
  479. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  480. bool_t close_stdin = BOOL_FALSE;
  481. if (!ctx)
  482. return BOOL_FALSE;
  483. // Some errors or fd is closed so stop interactive session
  484. // Non-interactive session just removes stdin callback
  485. if (info->revents & (POLLHUP | POLLERR | POLLNVAL)) {
  486. if (ctx->mode == MODE_INTERACTIVE)
  487. rc = BOOL_FALSE;
  488. faux_eloop_del_fd(eloop, STDIN_FILENO);
  489. close_stdin = BOOL_TRUE;
  490. }
  491. state = ktp_session_state(ctx->ktp);
  492. // Standard klish command line
  493. if ((state == KTP_SESSION_STATE_IDLE) &&
  494. (ctx->mode == MODE_INTERACTIVE)) {
  495. tinyrl_read(ctx->tinyrl);
  496. // Command needs stdin
  497. } else if ((state == KTP_SESSION_STATE_WAIT_FOR_CMD) &&
  498. KTP_STATUS_IS_NEED_STDIN(ktp_session_cmd_features(ctx->ktp))) {
  499. int fd = fileno(tinyrl_istream(ctx->tinyrl));
  500. char buf[1024] = {};
  501. ssize_t bytes_readed = 0;
  502. while ((bytes_readed = read(fd, buf, sizeof(buf))) > 0) {
  503. ktp_session_stdin(ctx->ktp, buf, bytes_readed);
  504. if (bytes_readed != sizeof(buf))
  505. break;
  506. }
  507. if (close_stdin)
  508. ktp_session_stdin_close(ctx->ktp);
  509. // Input is not needed
  510. } else {
  511. // Here the situation when input is not allowed. Remove stdin from
  512. // eloop waiting list. Else klish will get 100% CPU. Callbacks on
  513. // operation completions will restore this handler.
  514. faux_eloop_del_fd(eloop, STDIN_FILENO);
  515. }
  516. // Happy compiler
  517. type = type;
  518. return rc;
  519. }
  520. static bool_t send_winch_notification(ctx_t *ctx)
  521. {
  522. size_t width = 0;
  523. size_t height = 0;
  524. char *winsize = NULL;
  525. faux_msg_t *req = NULL;
  526. ktp_status_e status = KTP_STATUS_NONE;
  527. if (!ctx->tinyrl)
  528. return BOOL_FALSE;
  529. if (!ctx->ktp)
  530. return BOOL_FALSE;
  531. tinyrl_winsize(ctx->tinyrl, &width, &height);
  532. winsize = faux_str_sprintf("%lu %lu", width, height);
  533. req = ktp_msg_preform(KTP_NOTIFICATION, status);
  534. faux_msg_add_param(req, KTP_PARAM_WINCH, winsize, strlen(winsize));
  535. faux_str_free(winsize);
  536. faux_msg_send_async(req, ktp_session_async(ctx->ktp));
  537. faux_msg_free(req);
  538. return BOOL_TRUE;
  539. }
  540. static bool_t sigwinch_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  541. void *associated_data, void *udata)
  542. {
  543. ctx_t *ctx = (ctx_t *)udata;
  544. if (!ctx)
  545. return BOOL_FALSE;
  546. send_winch_notification(ctx);
  547. // Happy compiler
  548. eloop = eloop;
  549. type = type;
  550. associated_data = associated_data;
  551. return BOOL_TRUE;
  552. }
  553. static bool_t ctrl_c_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  554. void *associated_data, void *udata)
  555. {
  556. ctx_t *ctx = (ctx_t *)udata;
  557. char ctrl_c = KEY_ETX;
  558. ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
  559. if (!ctx)
  560. return BOOL_FALSE;
  561. state = ktp_session_state(ctx->ktp);
  562. if (state != KTP_SESSION_STATE_WAIT_FOR_CMD)
  563. return BOOL_TRUE;
  564. ktp_session_stdin(ctx->ktp, &ctrl_c, sizeof(ctrl_c));
  565. // Happy compiler
  566. eloop = eloop;
  567. type = type;
  568. associated_data = associated_data;
  569. return BOOL_TRUE;
  570. }
  571. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  572. {
  573. const char *line = NULL;
  574. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  575. faux_error_t *error = faux_error_new();
  576. tinyrl_line_to_hist(tinyrl);
  577. tinyrl_multi_crlf(tinyrl);
  578. tinyrl_reset_line_state(tinyrl);
  579. line = tinyrl_line(tinyrl);
  580. // Don't do anything on empty line
  581. if (faux_str_is_empty(line)) {
  582. faux_error_free(error);
  583. return BOOL_TRUE;
  584. }
  585. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  586. tinyrl_reset_line(tinyrl);
  587. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  588. // Suppose non-interactive command by default
  589. // Caught SIGINT for non-interactive commands
  590. tinyrl_enable_isig(tinyrl);
  591. key = key; // Happy compiler
  592. return BOOL_TRUE;
  593. }
  594. static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key)
  595. {
  596. const char *line = NULL;
  597. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  598. faux_error_t *error = NULL;
  599. if (key >= VT100_HOTKEY_MAP_LEN)
  600. return BOOL_TRUE;
  601. line = ctx->hotkeys[key];
  602. if (faux_str_is_empty(line))
  603. return BOOL_TRUE;
  604. error = faux_error_new();
  605. tinyrl_multi_crlf(tinyrl);
  606. tinyrl_reset_line_state(tinyrl);
  607. tinyrl_reset_line(tinyrl);
  608. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  609. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  610. return BOOL_TRUE;
  611. }
  612. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  613. {
  614. char *line = NULL;
  615. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  616. line = tinyrl_line_to_pos(tinyrl);
  617. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  618. faux_str_free(line);
  619. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  620. key = key; // Happy compiler
  621. return BOOL_TRUE;
  622. }
  623. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  624. {
  625. char *line = NULL;
  626. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  627. line = tinyrl_line_to_pos(tinyrl);
  628. // If "?" is quoted then it's not special hotkey.
  629. // Just insert it into the line.
  630. if (faux_str_unclosed_quotes(line, NULL)) {
  631. faux_str_free(line);
  632. return tinyrl_key_default(tinyrl, key);
  633. }
  634. ktp_session_help(ctx->ktp, line);
  635. faux_str_free(line);
  636. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  637. key = key; // Happy compiler
  638. return BOOL_TRUE;
  639. }
  640. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  641. const char *prefix, size_t max)
  642. {
  643. size_t width = tinyrl_width(tinyrl);
  644. size_t cols = 0;
  645. faux_list_node_t *iter = NULL;
  646. faux_list_node_t *node = NULL;
  647. size_t prefix_len = 0;
  648. size_t cols_filled = 0;
  649. if (prefix)
  650. prefix_len = strlen(prefix);
  651. // Find out column and rows number
  652. if (max < width)
  653. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  654. else
  655. cols = 1;
  656. iter = faux_list_head(completions);
  657. while ((node = faux_list_each_node(&iter))) {
  658. char *compl = (char *)faux_list_data(node);
  659. tinyrl_printf(tinyrl, "%*s%s",
  660. (prefix_len + max + 1 - strlen(compl)),
  661. prefix ? prefix : "",
  662. compl);
  663. cols_filled++;
  664. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  665. cols_filled = 0;
  666. tinyrl_crlf(tinyrl);
  667. }
  668. }
  669. }
  670. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  671. {
  672. ctx_t *ctx = (ctx_t *)udata;
  673. faux_list_node_t *iter = NULL;
  674. uint32_t param_len = 0;
  675. char *param_data = NULL;
  676. uint16_t param_type = 0;
  677. char *prefix = NULL;
  678. faux_list_t *completions = NULL;
  679. size_t completions_num = 0;
  680. size_t max_compl_len = 0;
  681. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  682. process_prompt_param(ctx->tinyrl, msg);
  683. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  684. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  685. NULL, NULL, (void (*)(void *))faux_str_free);
  686. iter = faux_msg_init_param_iter(msg);
  687. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  688. char *compl = NULL;
  689. if (KTP_PARAM_LINE != param_type)
  690. continue;
  691. compl = faux_str_dupn(param_data, param_len);
  692. faux_list_add(completions, compl);
  693. if (param_len > max_compl_len)
  694. max_compl_len = param_len;
  695. }
  696. completions_num = faux_list_len(completions);
  697. // Single possible completion
  698. if (1 == completions_num) {
  699. char *compl = (char *)faux_list_data(faux_list_head(completions));
  700. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  701. // Add space after completion
  702. tinyrl_line_insert(ctx->tinyrl, " ", 1);
  703. tinyrl_redisplay(ctx->tinyrl);
  704. // Multi possible completions
  705. } else if (completions_num > 1) {
  706. faux_list_node_t *eq_iter = NULL;
  707. size_t eq_part = 0;
  708. char *str = NULL;
  709. char *compl = NULL;
  710. // Try to find equal part for all possible completions
  711. eq_iter = faux_list_head(completions);
  712. str = (char *)faux_list_data(eq_iter);
  713. eq_part = strlen(str);
  714. eq_iter = faux_list_next_node(eq_iter);
  715. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  716. size_t cur_eq = 0;
  717. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  718. if (cur_eq < eq_part)
  719. eq_part = cur_eq;
  720. }
  721. // The equal part was found
  722. if (eq_part > 0) {
  723. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  724. tinyrl_redisplay(ctx->tinyrl);
  725. // There is no equal part for all completions
  726. } else {
  727. tinyrl_multi_crlf(ctx->tinyrl);
  728. tinyrl_reset_line_state(ctx->tinyrl);
  729. display_completions(ctx->tinyrl, completions,
  730. prefix, max_compl_len);
  731. tinyrl_redisplay(ctx->tinyrl);
  732. }
  733. }
  734. faux_list_free(completions);
  735. faux_str_free(prefix);
  736. // Operation is finished so restore stdin handler
  737. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  738. stdin_cb, ctx);
  739. // Happy compiler
  740. ktp = ktp;
  741. msg = msg;
  742. return BOOL_TRUE;
  743. }
  744. static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
  745. size_t max)
  746. {
  747. faux_list_node_t *iter = NULL;
  748. faux_list_node_t *node = NULL;
  749. iter = faux_list_head(help_list);
  750. while ((node = faux_list_each_node(&iter))) {
  751. help_t *help = (help_t *)faux_list_data(node);
  752. tinyrl_printf(tinyrl, " %s%*s%s\n",
  753. help->prefix,
  754. (max + 2 - strlen(help->prefix)),
  755. " ",
  756. help->line);
  757. }
  758. }
  759. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  760. {
  761. ctx_t *ctx = (ctx_t *)udata;
  762. faux_list_t *help_list = NULL;
  763. faux_list_node_t *iter = NULL;
  764. uint32_t param_len = 0;
  765. char *param_data = NULL;
  766. uint16_t param_type = 0;
  767. size_t max_prefix_len = 0;
  768. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  769. process_prompt_param(ctx->tinyrl, msg);
  770. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  771. help_compare, NULL, help_free);
  772. // Wait for PREFIX - LINE pairs
  773. iter = faux_msg_init_param_iter(msg);
  774. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data,
  775. &param_len)) {
  776. char *prefix_str = NULL;
  777. char *line_str = NULL;
  778. help_t *help = NULL;
  779. size_t prefix_len = 0;
  780. // Get PREFIX
  781. if (KTP_PARAM_PREFIX != param_type)
  782. continue;
  783. prefix_str = faux_str_dupn(param_data, param_len);
  784. prefix_len = param_len;
  785. // Get LINE
  786. if (!faux_msg_get_param_each(&iter, &param_type,
  787. (void **)&param_data, &param_len) ||
  788. (KTP_PARAM_LINE != param_type)) {
  789. faux_str_free(prefix_str);
  790. break;
  791. }
  792. line_str = faux_str_dupn(param_data, param_len);
  793. help = help_new(prefix_str, line_str);
  794. if (!faux_list_add(help_list, help)) {
  795. help_free(help);
  796. continue;
  797. }
  798. if (prefix_len > max_prefix_len)
  799. max_prefix_len = prefix_len;
  800. }
  801. if (faux_list_len(help_list) > 0) {
  802. tinyrl_multi_crlf(ctx->tinyrl);
  803. tinyrl_reset_line_state(ctx->tinyrl);
  804. display_help(ctx->tinyrl, help_list, max_prefix_len);
  805. tinyrl_redisplay(ctx->tinyrl);
  806. }
  807. faux_list_free(help_list);
  808. // Operation is finished so restore stdin handler
  809. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  810. stdin_cb, ctx);
  811. ktp = ktp; // happy compiler
  812. return BOOL_TRUE;
  813. }
  814. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  815. void *udata)
  816. {
  817. ctx_t *ctx = (ctx_t *)udata;
  818. assert(ctx);
  819. // Start pager if necessary
  820. if (
  821. ctx->opts->pager_enabled && // Pager enabled within config file
  822. (ctx->pager_working == TRI_UNDEFINED) && // Pager is not working
  823. !KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ktp)) // Non interactive command
  824. ) {
  825. ctx->pager_pipe = popen(ctx->opts->pager, "we");
  826. if (!ctx->pager_pipe)
  827. ctx->pager_working = TRI_FALSE; // Indicates can't start
  828. else
  829. ctx->pager_working = TRI_TRUE;
  830. }
  831. // Write to pager's pipe if pager is really working
  832. // Don't do anything if pager state is TRI_FALSE
  833. if (ctx->pager_working == TRI_TRUE) {
  834. if (faux_write_block(fileno(ctx->pager_pipe), line, len) <= 0) {
  835. // If we can't write to pager pipe then send
  836. // "SIGPIPE" to server. Pager is finished or broken.
  837. ktp_session_stdout_close(ktp);
  838. ctx->pager_working = TRI_FALSE;
  839. return BOOL_TRUE; // Don't break the loop
  840. }
  841. // TRI_UNDEFINED here means that pager is not needed
  842. } else if (ctx->pager_working == TRI_UNDEFINED) {
  843. if (faux_write_block(STDOUT_FILENO, line, len) < 0)
  844. return BOOL_FALSE;
  845. }
  846. return BOOL_TRUE;
  847. }