klish.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. if (tinyrl_busy(tinyrl))
  240. faux_error_free(ktp_session_error(ktp));
  241. // Restore stdin mode
  242. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  243. reset_hotkey_table(&ctx);
  244. tinyrl_free(tinyrl);
  245. ktp_session_free(ktp);
  246. faux_eloop_free(eloop);
  247. ktp_disconnect(unix_sock);
  248. opts_free(opts);
  249. if ((retval < 0) || (retcode != 0))
  250. return -1;
  251. return 0;
  252. }
  253. static bool_t send_next_command(ctx_t *ctx)
  254. {
  255. const char *line = NULL;
  256. faux_error_t *error = NULL;
  257. // User must type next interactive command. So just return
  258. if (ctx->mode == MODE_INTERACTIVE)
  259. return BOOL_TRUE;
  260. // Commands from cmdline
  261. if (ctx->mode == MODE_CMDLINE)
  262. line = faux_list_each(&ctx->cmdline_iter);
  263. if (!line) {
  264. ktp_session_set_done(ctx->ktp, BOOL_TRUE);
  265. return BOOL_TRUE;
  266. }
  267. if (!ctx->opts->quiet) {
  268. const char *prompt = tinyrl_prompt(ctx->tinyrl);
  269. printf("%s%s\n", prompt ? prompt : "", line);
  270. }
  271. error = faux_error_new();
  272. if (!ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run)) {
  273. faux_error_free(error);
  274. return BOOL_FALSE;
  275. }
  276. // Suppose non-interactive command by default
  277. tinyrl_enable_isig(ctx->tinyrl);
  278. return BOOL_TRUE;
  279. }
  280. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  281. void *user_data)
  282. {
  283. if (faux_write_block(STDERR_FILENO, line, len) < 0)
  284. return BOOL_FALSE;
  285. ktp = ktp;
  286. user_data = user_data;
  287. return BOOL_TRUE;
  288. }
  289. /*
  290. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  291. void *associated_data, void *user_data)
  292. {
  293. // Happy compiler
  294. eloop = eloop;
  295. type = type;
  296. associated_data = associated_data;
  297. user_data = user_data;
  298. return BOOL_FALSE; // Stop Event Loop
  299. }
  300. */
  301. static bool_t process_prompt_param(tinyrl_t *tinyrl, const faux_msg_t *msg)
  302. {
  303. char *prompt = NULL;
  304. if (!tinyrl)
  305. return BOOL_FALSE;
  306. if (!msg)
  307. return BOOL_FALSE;
  308. prompt = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PROMPT);
  309. if (prompt) {
  310. tinyrl_set_prompt(tinyrl, prompt);
  311. faux_str_free(prompt);
  312. }
  313. return BOOL_TRUE;
  314. }
  315. static void reset_hotkey_table(ctx_t *ctx)
  316. {
  317. size_t i = 0;
  318. assert(ctx);
  319. for (i = 0; i < VT100_HOTKEY_MAP_LEN; i++)
  320. faux_str_free(ctx->hotkeys[i]);
  321. faux_bzero(ctx->hotkeys, sizeof(ctx->hotkeys));
  322. }
  323. static bool_t process_hotkey_param(ctx_t *ctx, const faux_msg_t *msg)
  324. {
  325. faux_list_node_t *iter = NULL;
  326. uint32_t param_len = 0;
  327. char *param_data = NULL;
  328. uint16_t param_type = 0;
  329. if (!ctx)
  330. return BOOL_FALSE;
  331. if (!msg)
  332. return BOOL_FALSE;
  333. if (!faux_msg_get_param_by_type(msg, KTP_PARAM_HOTKEY,
  334. (void **)&param_data, &param_len))
  335. return BOOL_TRUE;
  336. // If there is HOTKEY parameter then reinitialize whole hotkey table
  337. reset_hotkey_table(ctx);
  338. iter = faux_msg_init_param_iter(msg);
  339. while (faux_msg_get_param_each(
  340. &iter, &param_type, (void **)&param_data, &param_len)) {
  341. char *cmd = NULL;
  342. ssize_t code = -1;
  343. size_t key_len = 0;
  344. if (param_len < 3) // <key>'\0'<cmd>
  345. continue;
  346. if (KTP_PARAM_HOTKEY != param_type)
  347. continue;
  348. key_len = strlen(param_data); // Length of <key>
  349. if (key_len < 1)
  350. continue;
  351. code = vt100_hotkey_decode(param_data);
  352. if ((code < 0) || (code > VT100_HOTKEY_MAP_LEN))
  353. continue;
  354. cmd = faux_str_dupn(param_data + key_len + 1,
  355. param_len - key_len - 1);
  356. if (!cmd)
  357. continue;
  358. ctx->hotkeys[code] = cmd;
  359. }
  360. return BOOL_TRUE;
  361. }
  362. bool_t auth_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  363. {
  364. ctx_t *ctx = (ctx_t *)udata;
  365. int rc = -1;
  366. faux_error_t *error = NULL;
  367. process_prompt_param(ctx->tinyrl, msg);
  368. process_hotkey_param(ctx, msg);
  369. if (!ktp_session_retcode(ktp, &rc))
  370. rc = -1;
  371. error = ktp_session_error(ktp);
  372. if ((rc < 0) && (faux_error_len(error) > 0)) {
  373. faux_error_node_t *err_iter = faux_error_iter(error);
  374. const char *err = NULL;
  375. while ((err = faux_error_each(&err_iter)))
  376. fprintf(stderr, "Error: auth: %s\n", err);
  377. return BOOL_FALSE;
  378. }
  379. if (isatty(STDIN_FILENO))
  380. send_winch_notification(ctx);
  381. if (ctx->mode == MODE_INTERACTIVE) {
  382. // Print prompt for interactive command
  383. tinyrl_redisplay(ctx->tinyrl);
  384. } else {
  385. // Send first command for non-interactive modes
  386. send_next_command(ctx);
  387. }
  388. // Happy compiler
  389. msg = msg;
  390. return BOOL_TRUE;
  391. }
  392. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  393. {
  394. ctx_t *ctx = (ctx_t *)udata;
  395. int rc = -1;
  396. faux_error_t *error = NULL;
  397. bool_t it_was_pager = BOOL_FALSE;
  398. // Wait for pager
  399. if (ctx->pager_working != TRI_UNDEFINED) {
  400. pclose(ctx->pager_pipe);
  401. ctx->pager_working = TRI_UNDEFINED;
  402. ctx->pager_pipe = NULL;
  403. it_was_pager = BOOL_TRUE;
  404. }
  405. // Disable SIGINT caught for non-interactive commands.
  406. // Do it after pager exit. Else it can restore wrong tty mode after
  407. // ISIG disabling
  408. tinyrl_disable_isig(ctx->tinyrl);
  409. // Sometimes output stream from server doesn't contain final crlf so
  410. // goto newline itself
  411. if (ktp_session_last_stream(ktp) == STDERR_FILENO) {
  412. if (ktp_session_stderr_need_newline(ktp))
  413. fprintf(stderr, "\n");
  414. } else {
  415. // Pager adds newline itself
  416. if (ktp_session_stdout_need_newline(ktp) && !it_was_pager)
  417. tinyrl_crlf(ctx->tinyrl);
  418. }
  419. process_prompt_param(ctx->tinyrl, msg);
  420. process_hotkey_param(ctx, msg);
  421. if (!ktp_session_retcode(ktp, &rc))
  422. rc = -1;
  423. error = ktp_session_error(ktp);
  424. if (rc < 0) {
  425. if (faux_error_len(error) > 0) {
  426. faux_error_node_t *err_iter = faux_error_iter(error);
  427. const char *err = NULL;
  428. while ((err = faux_error_each(&err_iter)))
  429. fprintf(stderr, "Error: %s\n", err);
  430. }
  431. // Stop-on-error
  432. if (ctx->opts->stop_on_error) {
  433. faux_error_free(error);
  434. ktp_session_set_done(ktp, BOOL_TRUE);
  435. return BOOL_TRUE;
  436. }
  437. }
  438. faux_error_free(error);
  439. if (ctx->mode == MODE_INTERACTIVE) {
  440. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  441. if (!ktp_session_done(ktp))
  442. tinyrl_redisplay(ctx->tinyrl);
  443. }
  444. // Operation is finished so restore stdin handler
  445. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  446. stdin_cb, ctx);
  447. // Send next command for non-interactive modes
  448. send_next_command(ctx);
  449. // Happy compiler
  450. msg = msg;
  451. return BOOL_TRUE;
  452. }
  453. bool_t cmd_incompleted_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  454. {
  455. ctx_t *ctx = (ctx_t *)udata;
  456. if (ktp_session_state(ktp) == KTP_SESSION_STATE_WAIT_FOR_CMD) {
  457. // Cmd need stdin so restore stdin handler
  458. if (KTP_STATUS_IS_NEED_STDIN(ktp_session_cmd_features(ktp))) {
  459. // Disable SIGINT signal (it is used for commands that
  460. // don't need stdin. Commands with stdin can get ^C
  461. // themself interactively.)
  462. tinyrl_disable_isig(ctx->tinyrl);
  463. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  464. stdin_cb, ctx);
  465. }
  466. }
  467. // Happy compiler
  468. msg = msg;
  469. return BOOL_TRUE;
  470. }
  471. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  472. void *associated_data, void *udata)
  473. {
  474. bool_t rc = BOOL_TRUE;
  475. ctx_t *ctx = (ctx_t *)udata;
  476. ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
  477. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  478. if (!ctx)
  479. return BOOL_FALSE;
  480. // Some errors or fd is closed so stop session
  481. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  482. rc = BOOL_FALSE;
  483. state = ktp_session_state(ctx->ktp);
  484. // Standard klish command line
  485. if ((state == KTP_SESSION_STATE_IDLE) &&
  486. (ctx->mode == MODE_INTERACTIVE)) {
  487. tinyrl_read(ctx->tinyrl);
  488. return rc;
  489. }
  490. // Command needs stdin
  491. if ((state == KTP_SESSION_STATE_WAIT_FOR_CMD) &&
  492. KTP_STATUS_IS_NEED_STDIN(ktp_session_cmd_features(ctx->ktp))) {
  493. int fd = fileno(tinyrl_istream(ctx->tinyrl));
  494. char buf[1024] = {};
  495. ssize_t bytes_readed = 0;
  496. while ((bytes_readed = read(fd, buf, sizeof(buf))) > 0) {
  497. ktp_session_stdin(ctx->ktp, buf, bytes_readed);
  498. if (bytes_readed != sizeof(buf))
  499. break;
  500. }
  501. return rc;
  502. }
  503. // Here the situation when input is not allowed. Remove stdin from
  504. // eloop waiting list. Else klish will get 100% CPU. Callbacks on
  505. // operation completions will restore this handler.
  506. faux_eloop_del_fd(eloop, STDIN_FILENO);
  507. // Happy compiler
  508. eloop = eloop;
  509. type = type;
  510. return rc;
  511. }
  512. static bool_t send_winch_notification(ctx_t *ctx)
  513. {
  514. size_t width = 0;
  515. size_t height = 0;
  516. char *winsize = NULL;
  517. faux_msg_t *req = NULL;
  518. ktp_status_e status = KTP_STATUS_NONE;
  519. if (!ctx->tinyrl)
  520. return BOOL_FALSE;
  521. if (!ctx->ktp)
  522. return BOOL_FALSE;
  523. tinyrl_winsize(ctx->tinyrl, &width, &height);
  524. winsize = faux_str_sprintf("%lu %lu", width, height);
  525. req = ktp_msg_preform(KTP_NOTIFICATION, status);
  526. faux_msg_add_param(req, KTP_PARAM_WINCH, winsize, strlen(winsize));
  527. faux_str_free(winsize);
  528. faux_msg_send_async(req, ktp_session_async(ctx->ktp));
  529. faux_msg_free(req);
  530. return BOOL_TRUE;
  531. }
  532. static bool_t sigwinch_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  533. void *associated_data, void *udata)
  534. {
  535. ctx_t *ctx = (ctx_t *)udata;
  536. if (!ctx)
  537. return BOOL_FALSE;
  538. send_winch_notification(ctx);
  539. // Happy compiler
  540. eloop = eloop;
  541. type = type;
  542. associated_data = associated_data;
  543. return BOOL_TRUE;
  544. }
  545. static bool_t ctrl_c_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  546. void *associated_data, void *udata)
  547. {
  548. ctx_t *ctx = (ctx_t *)udata;
  549. char ctrl_c = KEY_ETX;
  550. ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
  551. if (!ctx)
  552. return BOOL_FALSE;
  553. state = ktp_session_state(ctx->ktp);
  554. if (state != KTP_SESSION_STATE_WAIT_FOR_CMD)
  555. return BOOL_TRUE;
  556. ktp_session_stdin(ctx->ktp, &ctrl_c, sizeof(ctrl_c));
  557. // Happy compiler
  558. eloop = eloop;
  559. type = type;
  560. associated_data = associated_data;
  561. return BOOL_TRUE;
  562. }
  563. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  564. {
  565. const char *line = NULL;
  566. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  567. faux_error_t *error = faux_error_new();
  568. tinyrl_line_to_hist(tinyrl);
  569. tinyrl_multi_crlf(tinyrl);
  570. tinyrl_reset_line_state(tinyrl);
  571. line = tinyrl_line(tinyrl);
  572. // Don't do anything on empty line
  573. if (faux_str_is_empty(line)) {
  574. faux_error_free(error);
  575. return BOOL_TRUE;
  576. }
  577. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  578. tinyrl_reset_line(tinyrl);
  579. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  580. // Suppose non-interactive command by default
  581. // Caught SIGINT for non-interactive commands
  582. tinyrl_enable_isig(tinyrl);
  583. key = key; // Happy compiler
  584. return BOOL_TRUE;
  585. }
  586. static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key)
  587. {
  588. const char *line = NULL;
  589. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  590. faux_error_t *error = NULL;
  591. if (key >= VT100_HOTKEY_MAP_LEN)
  592. return BOOL_TRUE;
  593. line = ctx->hotkeys[key];
  594. if (faux_str_is_empty(line))
  595. return BOOL_TRUE;
  596. error = faux_error_new();
  597. tinyrl_multi_crlf(tinyrl);
  598. tinyrl_reset_line_state(tinyrl);
  599. tinyrl_reset_line(tinyrl);
  600. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  601. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  602. return BOOL_TRUE;
  603. }
  604. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  605. {
  606. char *line = NULL;
  607. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  608. line = tinyrl_line_to_pos(tinyrl);
  609. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  610. faux_str_free(line);
  611. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  612. key = key; // Happy compiler
  613. return BOOL_TRUE;
  614. }
  615. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  616. {
  617. char *line = NULL;
  618. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  619. line = tinyrl_line_to_pos(tinyrl);
  620. // If "?" is quoted then it's not special hotkey.
  621. // Just insert it into the line.
  622. if (faux_str_unclosed_quotes(line, NULL)) {
  623. faux_str_free(line);
  624. return tinyrl_key_default(tinyrl, key);
  625. }
  626. ktp_session_help(ctx->ktp, line);
  627. faux_str_free(line);
  628. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  629. key = key; // Happy compiler
  630. return BOOL_TRUE;
  631. }
  632. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  633. const char *prefix, size_t max)
  634. {
  635. size_t width = tinyrl_width(tinyrl);
  636. size_t cols = 0;
  637. faux_list_node_t *iter = NULL;
  638. faux_list_node_t *node = NULL;
  639. size_t prefix_len = 0;
  640. size_t cols_filled = 0;
  641. if (prefix)
  642. prefix_len = strlen(prefix);
  643. // Find out column and rows number
  644. if (max < width)
  645. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  646. else
  647. cols = 1;
  648. iter = faux_list_head(completions);
  649. while ((node = faux_list_each_node(&iter))) {
  650. char *compl = (char *)faux_list_data(node);
  651. tinyrl_printf(tinyrl, "%*s%s",
  652. (prefix_len + max + 1 - strlen(compl)),
  653. prefix ? prefix : "",
  654. compl);
  655. cols_filled++;
  656. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  657. cols_filled = 0;
  658. tinyrl_crlf(tinyrl);
  659. }
  660. }
  661. }
  662. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  663. {
  664. ctx_t *ctx = (ctx_t *)udata;
  665. faux_list_node_t *iter = NULL;
  666. uint32_t param_len = 0;
  667. char *param_data = NULL;
  668. uint16_t param_type = 0;
  669. char *prefix = NULL;
  670. faux_list_t *completions = NULL;
  671. size_t completions_num = 0;
  672. size_t max_compl_len = 0;
  673. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  674. process_prompt_param(ctx->tinyrl, msg);
  675. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  676. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  677. NULL, NULL, (void (*)(void *))faux_str_free);
  678. iter = faux_msg_init_param_iter(msg);
  679. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  680. char *compl = NULL;
  681. if (KTP_PARAM_LINE != param_type)
  682. continue;
  683. compl = faux_str_dupn(param_data, param_len);
  684. faux_list_add(completions, compl);
  685. if (param_len > max_compl_len)
  686. max_compl_len = param_len;
  687. }
  688. completions_num = faux_list_len(completions);
  689. // Single possible completion
  690. if (1 == completions_num) {
  691. char *compl = (char *)faux_list_data(faux_list_head(completions));
  692. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  693. // Add space after completion
  694. tinyrl_line_insert(ctx->tinyrl, " ", 1);
  695. tinyrl_redisplay(ctx->tinyrl);
  696. // Multi possible completions
  697. } else if (completions_num > 1) {
  698. faux_list_node_t *eq_iter = NULL;
  699. size_t eq_part = 0;
  700. char *str = NULL;
  701. char *compl = NULL;
  702. // Try to find equal part for all possible completions
  703. eq_iter = faux_list_head(completions);
  704. str = (char *)faux_list_data(eq_iter);
  705. eq_part = strlen(str);
  706. eq_iter = faux_list_next_node(eq_iter);
  707. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  708. size_t cur_eq = 0;
  709. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  710. if (cur_eq < eq_part)
  711. eq_part = cur_eq;
  712. }
  713. // The equal part was found
  714. if (eq_part > 0) {
  715. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  716. tinyrl_redisplay(ctx->tinyrl);
  717. // There is no equal part for all completions
  718. } else {
  719. tinyrl_multi_crlf(ctx->tinyrl);
  720. tinyrl_reset_line_state(ctx->tinyrl);
  721. display_completions(ctx->tinyrl, completions,
  722. prefix, max_compl_len);
  723. tinyrl_redisplay(ctx->tinyrl);
  724. }
  725. }
  726. faux_list_free(completions);
  727. faux_str_free(prefix);
  728. // Operation is finished so restore stdin handler
  729. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  730. stdin_cb, ctx);
  731. // Happy compiler
  732. ktp = ktp;
  733. msg = msg;
  734. return BOOL_TRUE;
  735. }
  736. static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
  737. size_t max)
  738. {
  739. faux_list_node_t *iter = NULL;
  740. faux_list_node_t *node = NULL;
  741. iter = faux_list_head(help_list);
  742. while ((node = faux_list_each_node(&iter))) {
  743. help_t *help = (help_t *)faux_list_data(node);
  744. tinyrl_printf(tinyrl, " %s%*s%s\n",
  745. help->prefix,
  746. (max + 2 - strlen(help->prefix)),
  747. " ",
  748. help->line);
  749. }
  750. }
  751. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  752. {
  753. ctx_t *ctx = (ctx_t *)udata;
  754. faux_list_t *help_list = NULL;
  755. faux_list_node_t *iter = NULL;
  756. uint32_t param_len = 0;
  757. char *param_data = NULL;
  758. uint16_t param_type = 0;
  759. size_t max_prefix_len = 0;
  760. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  761. process_prompt_param(ctx->tinyrl, msg);
  762. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  763. help_compare, NULL, help_free);
  764. // Wait for PREFIX - LINE pairs
  765. iter = faux_msg_init_param_iter(msg);
  766. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data,
  767. &param_len)) {
  768. char *prefix_str = NULL;
  769. char *line_str = NULL;
  770. help_t *help = NULL;
  771. size_t prefix_len = 0;
  772. // Get PREFIX
  773. if (KTP_PARAM_PREFIX != param_type)
  774. continue;
  775. prefix_str = faux_str_dupn(param_data, param_len);
  776. prefix_len = param_len;
  777. // Get LINE
  778. if (!faux_msg_get_param_each(&iter, &param_type,
  779. (void **)&param_data, &param_len) ||
  780. (KTP_PARAM_LINE != param_type)) {
  781. faux_str_free(prefix_str);
  782. break;
  783. }
  784. line_str = faux_str_dupn(param_data, param_len);
  785. help = help_new(prefix_str, line_str);
  786. if (!faux_list_add(help_list, help)) {
  787. help_free(help);
  788. continue;
  789. }
  790. if (prefix_len > max_prefix_len)
  791. max_prefix_len = prefix_len;
  792. }
  793. if (faux_list_len(help_list) > 0) {
  794. tinyrl_multi_crlf(ctx->tinyrl);
  795. tinyrl_reset_line_state(ctx->tinyrl);
  796. display_help(ctx->tinyrl, help_list, max_prefix_len);
  797. tinyrl_redisplay(ctx->tinyrl);
  798. }
  799. faux_list_free(help_list);
  800. // Operation is finished so restore stdin handler
  801. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  802. stdin_cb, ctx);
  803. ktp = ktp; // happy compiler
  804. return BOOL_TRUE;
  805. }
  806. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  807. void *udata)
  808. {
  809. ctx_t *ctx = (ctx_t *)udata;
  810. assert(ctx);
  811. // Start pager if necessary
  812. if (
  813. ctx->opts->pager_enabled && // Pager enabled within config file
  814. (ctx->pager_working == TRI_UNDEFINED) && // Pager is not working
  815. !KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ktp)) // Non interactive command
  816. ) {
  817. ctx->pager_pipe = popen(ctx->opts->pager, "we");
  818. if (!ctx->pager_pipe)
  819. ctx->pager_working = TRI_FALSE; // Indicates can't start
  820. else
  821. ctx->pager_working = TRI_TRUE;
  822. }
  823. // Write to pager's pipe if pager is really working
  824. // Don't do anything if pager state is TRI_FALSE
  825. if (ctx->pager_working == TRI_TRUE) {
  826. if (faux_write_block(fileno(ctx->pager_pipe), line, len) <= 0) {
  827. // If we can't write to pager pipe then send
  828. // "SIGPIPE" to server. Pager is finished or broken.
  829. ktp_session_stdout_close(ktp);
  830. ctx->pager_working = TRI_FALSE;
  831. return BOOL_TRUE; // Don't break the loop
  832. }
  833. // TRI_UNDEFINED here means that pager is not needed
  834. } else if (ctx->pager_working == TRI_UNDEFINED) {
  835. if (faux_write_block(STDOUT_FILENO, line, len) < 0)
  836. return BOOL_FALSE;
  837. }
  838. return BOOL_TRUE;
  839. }