klish.c 27 KB

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