klish.c 28 KB

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