klish.c 26 KB

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