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