klish.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. // Disable SIGINT caught for non-interactive commands.
  416. // Do it after pager exit. Else it can restore wrong tty mode after
  417. // ISIG disabling
  418. tinyrl_disable_isig(ctx->tinyrl);
  419. // Sometimes output stream from server doesn't contain final crlf so
  420. // goto newline itself
  421. if (ktp_session_last_stream(ktp) == STDERR_FILENO) {
  422. if (ktp_session_stderr_need_newline(ktp))
  423. fprintf(stderr, "\n");
  424. } else {
  425. // Pager adds newline itself
  426. if (ktp_session_stdout_need_newline(ktp) && !it_was_pager)
  427. tinyrl_crlf(ctx->tinyrl);
  428. }
  429. process_prompt_param(ctx->tinyrl, msg);
  430. process_hotkey_param(ctx, msg);
  431. if (!ktp_session_retcode(ktp, &rc))
  432. rc = -1;
  433. error = ktp_session_error(ktp);
  434. if (rc != 0) {
  435. if (faux_error_len(error) > 0) {
  436. faux_error_node_t *err_iter = faux_error_iter(error);
  437. const char *err = NULL;
  438. while ((err = faux_error_each(&err_iter)))
  439. fprintf(stderr, "Error: %s\n", err);
  440. }
  441. // Stop-on-error
  442. if (ctx->opts->stop_on_error) {
  443. faux_error_free(error);
  444. ktp_session_set_done(ktp, BOOL_TRUE);
  445. return BOOL_TRUE;
  446. }
  447. }
  448. faux_error_free(error);
  449. if (ctx->mode == MODE_INTERACTIVE) {
  450. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  451. if (!ktp_session_done(ktp))
  452. tinyrl_redisplay(ctx->tinyrl);
  453. // Operation is finished so restore stdin handler
  454. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  455. stdin_cb, ctx);
  456. }
  457. // Send next command for non-interactive modes
  458. send_next_command(ctx);
  459. // Happy compiler
  460. msg = msg;
  461. return BOOL_TRUE;
  462. }
  463. bool_t cmd_incompleted_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  464. {
  465. ctx_t *ctx = (ctx_t *)udata;
  466. // It can't get data from stdin, it gets commands from stdin instead.
  467. // So don't process incompleted ack but just return
  468. if (ctx->mode == MODE_STDIN)
  469. return BOOL_TRUE;
  470. if (ktp_session_state(ktp) == KTP_SESSION_STATE_WAIT_FOR_CMD) {
  471. // Cmd need stdin so restore stdin handler
  472. if (KTP_STATUS_IS_NEED_STDIN(ktp_session_cmd_features(ktp))) {
  473. // Disable SIGINT signal (it is used for commands that
  474. // don't need stdin. Commands with stdin can get ^C
  475. // themself interactively.)
  476. tinyrl_disable_isig(ctx->tinyrl);
  477. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  478. stdin_cb, ctx);
  479. }
  480. }
  481. // Happy compiler
  482. msg = msg;
  483. return BOOL_TRUE;
  484. }
  485. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  486. void *associated_data, void *udata)
  487. {
  488. bool_t rc = BOOL_TRUE;
  489. ctx_t *ctx = (ctx_t *)udata;
  490. ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
  491. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  492. bool_t close_stdin = BOOL_FALSE;
  493. size_t obuf_len = 0;
  494. if (!ctx)
  495. return BOOL_FALSE;
  496. // Some errors or fd is closed so stop interactive session
  497. // Non-interactive session just removes stdin callback
  498. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  499. close_stdin = BOOL_TRUE;
  500. // Temporarily stop stdin reading because too much data is buffered
  501. // and all data can't be sent to server yet
  502. obuf_len = faux_buf_len(faux_async_obuf(ktp_session_async(ctx->ktp)));
  503. if (obuf_len > OBUF_LIMIT) {
  504. faux_eloop_del_fd(eloop, STDIN_FILENO);
  505. return BOOL_TRUE;
  506. }
  507. state = ktp_session_state(ctx->ktp);
  508. // Standard klish command line
  509. if ((state == KTP_SESSION_STATE_IDLE) &&
  510. (ctx->mode == MODE_INTERACTIVE)) {
  511. tinyrl_read(ctx->tinyrl);
  512. if (close_stdin) {
  513. faux_eloop_del_fd(eloop, STDIN_FILENO);
  514. rc = BOOL_FALSE;
  515. }
  516. // Command needs stdin
  517. } else if ((state == KTP_SESSION_STATE_WAIT_FOR_CMD) &&
  518. KTP_STATUS_IS_NEED_STDIN(ktp_session_cmd_features(ctx->ktp))) {
  519. int fd = fileno(tinyrl_istream(ctx->tinyrl));
  520. char buf[1024] = {};
  521. ssize_t bytes_readed = 0;
  522. // Don't read all data from stdin to don't overfill out buffer.
  523. // Allow another handlers to push already received data to
  524. // server
  525. if ((bytes_readed = read(fd, buf, sizeof(buf))) > 0)
  526. ktp_session_stdin(ctx->ktp, buf, bytes_readed);
  527. // Actually close stdin only when all data is read
  528. if (close_stdin && (bytes_readed <= 0)) {
  529. ktp_session_stdin_close(ctx->ktp);
  530. faux_eloop_del_fd(eloop, STDIN_FILENO);
  531. }
  532. // Input is not needed
  533. } else {
  534. // Here the situation when input is not allowed. Remove stdin from
  535. // eloop waiting list. Else klish will get 100% CPU. Callbacks on
  536. // operation completions will restore this handler.
  537. faux_eloop_del_fd(eloop, STDIN_FILENO);
  538. }
  539. // Happy compiler
  540. type = type;
  541. return rc;
  542. }
  543. static bool_t async_stdin_sent_cb(ktp_session_t *ktp, size_t len,
  544. void *user_data)
  545. {
  546. ctx_t *ctx = (ctx_t *)user_data;
  547. assert(ktp);
  548. // This callbacks is executed when any number of bytes is really written
  549. // to server socket. So if stdin transmit was stopped due to obuf
  550. // overflow it's time to rearm transmission
  551. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  552. stdin_cb, ctx);
  553. len = len; // Happy compiler
  554. return BOOL_TRUE;
  555. }
  556. static bool_t send_winch_notification(ctx_t *ctx)
  557. {
  558. size_t width = 0;
  559. size_t height = 0;
  560. char *winsize = NULL;
  561. faux_msg_t *req = NULL;
  562. ktp_status_e status = KTP_STATUS_NONE;
  563. if (!ctx->tinyrl)
  564. return BOOL_FALSE;
  565. if (!ctx->ktp)
  566. return BOOL_FALSE;
  567. tinyrl_winsize(ctx->tinyrl, &width, &height);
  568. winsize = faux_str_sprintf("%lu %lu", width, height);
  569. req = ktp_msg_preform(KTP_NOTIFICATION, status);
  570. faux_msg_add_param(req, KTP_PARAM_WINCH, winsize, strlen(winsize));
  571. faux_str_free(winsize);
  572. faux_msg_send_async(req, ktp_session_async(ctx->ktp));
  573. faux_msg_free(req);
  574. return BOOL_TRUE;
  575. }
  576. static bool_t sigwinch_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  577. void *associated_data, void *udata)
  578. {
  579. ctx_t *ctx = (ctx_t *)udata;
  580. if (!ctx)
  581. return BOOL_FALSE;
  582. send_winch_notification(ctx);
  583. // Happy compiler
  584. eloop = eloop;
  585. type = type;
  586. associated_data = associated_data;
  587. return BOOL_TRUE;
  588. }
  589. static bool_t ctrl_c_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  590. void *associated_data, void *udata)
  591. {
  592. ctx_t *ctx = (ctx_t *)udata;
  593. char ctrl_c = KEY_ETX;
  594. ktp_session_state_e state = KTP_SESSION_STATE_ERROR;
  595. if (!ctx)
  596. return BOOL_FALSE;
  597. if (ctx->mode != MODE_INTERACTIVE)
  598. return BOOL_FALSE;
  599. state = ktp_session_state(ctx->ktp);
  600. if (state == KTP_SESSION_STATE_WAIT_FOR_CMD)
  601. ktp_session_stdin(ctx->ktp, &ctrl_c, sizeof(ctrl_c));
  602. // Happy compiler
  603. eloop = eloop;
  604. type = type;
  605. associated_data = associated_data;
  606. return BOOL_TRUE;
  607. }
  608. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  609. {
  610. const char *line = NULL;
  611. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  612. faux_error_t *error = faux_error_new();
  613. tinyrl_line_to_hist(tinyrl);
  614. tinyrl_multi_crlf(tinyrl);
  615. tinyrl_reset_line_state(tinyrl);
  616. line = tinyrl_line(tinyrl);
  617. // Don't do anything on empty line
  618. if (faux_str_is_empty(line)) {
  619. faux_error_free(error);
  620. return BOOL_TRUE;
  621. }
  622. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  623. tinyrl_reset_line(tinyrl);
  624. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  625. // Suppose non-interactive command by default
  626. // Caught SIGINT for non-interactive commands
  627. tinyrl_enable_isig(tinyrl);
  628. key = key; // Happy compiler
  629. return BOOL_TRUE;
  630. }
  631. static bool_t tinyrl_key_hotkey(tinyrl_t *tinyrl, unsigned char key)
  632. {
  633. const char *line = NULL;
  634. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  635. faux_error_t *error = NULL;
  636. if (key >= VT100_HOTKEY_MAP_LEN)
  637. return BOOL_TRUE;
  638. line = ctx->hotkeys[key];
  639. if (faux_str_is_empty(line))
  640. return BOOL_TRUE;
  641. error = faux_error_new();
  642. tinyrl_multi_crlf(tinyrl);
  643. tinyrl_reset_line_state(tinyrl);
  644. tinyrl_reset_line(tinyrl);
  645. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  646. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  647. return BOOL_TRUE;
  648. }
  649. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  650. {
  651. char *line = NULL;
  652. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  653. line = tinyrl_line_to_pos(tinyrl);
  654. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  655. faux_str_free(line);
  656. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  657. key = key; // Happy compiler
  658. return BOOL_TRUE;
  659. }
  660. static bool_t tinyrl_key_help(tinyrl_t *tinyrl, unsigned char key)
  661. {
  662. char *line = NULL;
  663. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  664. line = tinyrl_line_to_pos(tinyrl);
  665. // If "?" is quoted then it's not special hotkey.
  666. // Just insert it into the line.
  667. if (faux_str_unclosed_quotes(line, NULL)) {
  668. faux_str_free(line);
  669. return tinyrl_key_default(tinyrl, key);
  670. }
  671. ktp_session_help(ctx->ktp, line);
  672. faux_str_free(line);
  673. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  674. key = key; // Happy compiler
  675. return BOOL_TRUE;
  676. }
  677. static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
  678. const char *prefix, size_t max)
  679. {
  680. size_t width = tinyrl_width(tinyrl);
  681. size_t cols = 0;
  682. faux_list_node_t *iter = NULL;
  683. faux_list_node_t *node = NULL;
  684. size_t prefix_len = 0;
  685. size_t cols_filled = 0;
  686. if (prefix)
  687. prefix_len = strlen(prefix);
  688. // Find out column and rows number
  689. if (max < width)
  690. cols = (width + 1) / (prefix_len + max + 1); // For a space between words
  691. else
  692. cols = 1;
  693. iter = faux_list_head(completions);
  694. while ((node = faux_list_each_node(&iter))) {
  695. char *compl = (char *)faux_list_data(node);
  696. tinyrl_printf(tinyrl, "%*s%s",
  697. (prefix_len + max + 1 - strlen(compl)),
  698. prefix ? prefix : "",
  699. compl);
  700. cols_filled++;
  701. if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
  702. cols_filled = 0;
  703. tinyrl_crlf(tinyrl);
  704. }
  705. }
  706. }
  707. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  708. {
  709. ctx_t *ctx = (ctx_t *)udata;
  710. faux_list_node_t *iter = NULL;
  711. uint32_t param_len = 0;
  712. char *param_data = NULL;
  713. uint16_t param_type = 0;
  714. char *prefix = NULL;
  715. faux_list_t *completions = NULL;
  716. size_t completions_num = 0;
  717. size_t max_compl_len = 0;
  718. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  719. process_prompt_param(ctx->tinyrl, msg);
  720. prefix = faux_msg_get_str_param_by_type(msg, KTP_PARAM_PREFIX);
  721. completions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  722. NULL, NULL, (void (*)(void *))faux_str_free);
  723. iter = faux_msg_init_param_iter(msg);
  724. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data, &param_len)) {
  725. char *compl = NULL;
  726. if (KTP_PARAM_LINE != param_type)
  727. continue;
  728. compl = faux_str_dupn(param_data, param_len);
  729. faux_list_add(completions, compl);
  730. if (param_len > max_compl_len)
  731. max_compl_len = param_len;
  732. }
  733. completions_num = faux_list_len(completions);
  734. // Single possible completion
  735. if (1 == completions_num) {
  736. char *compl = (char *)faux_list_data(faux_list_head(completions));
  737. tinyrl_line_insert(ctx->tinyrl, compl, strlen(compl));
  738. // Add space after completion
  739. tinyrl_line_insert(ctx->tinyrl, " ", 1);
  740. tinyrl_redisplay(ctx->tinyrl);
  741. // Multi possible completions
  742. } else if (completions_num > 1) {
  743. faux_list_node_t *eq_iter = NULL;
  744. size_t eq_part = 0;
  745. char *str = NULL;
  746. char *compl = NULL;
  747. // Try to find equal part for all possible completions
  748. eq_iter = faux_list_head(completions);
  749. str = (char *)faux_list_data(eq_iter);
  750. eq_part = strlen(str);
  751. eq_iter = faux_list_next_node(eq_iter);
  752. while ((compl = (char *)faux_list_each(&eq_iter)) && (eq_part > 0)) {
  753. size_t cur_eq = 0;
  754. cur_eq = tinyrl_equal_part(ctx->tinyrl, str, compl);
  755. if (cur_eq < eq_part)
  756. eq_part = cur_eq;
  757. }
  758. // The equal part was found
  759. if (eq_part > 0) {
  760. tinyrl_line_insert(ctx->tinyrl, str, eq_part);
  761. tinyrl_redisplay(ctx->tinyrl);
  762. // There is no equal part for all completions
  763. } else {
  764. tinyrl_multi_crlf(ctx->tinyrl);
  765. tinyrl_reset_line_state(ctx->tinyrl);
  766. display_completions(ctx->tinyrl, completions,
  767. prefix, max_compl_len);
  768. tinyrl_redisplay(ctx->tinyrl);
  769. }
  770. }
  771. faux_list_free(completions);
  772. faux_str_free(prefix);
  773. // Operation is finished so restore stdin handler
  774. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  775. stdin_cb, ctx);
  776. // Happy compiler
  777. ktp = ktp;
  778. msg = msg;
  779. return BOOL_TRUE;
  780. }
  781. static void display_help(const tinyrl_t *tinyrl, faux_list_t *help_list,
  782. size_t max)
  783. {
  784. faux_list_node_t *iter = NULL;
  785. faux_list_node_t *node = NULL;
  786. iter = faux_list_head(help_list);
  787. while ((node = faux_list_each_node(&iter))) {
  788. help_t *help = (help_t *)faux_list_data(node);
  789. tinyrl_printf(tinyrl, " %s%*s%s\n",
  790. help->prefix,
  791. (max + 2 - strlen(help->prefix)),
  792. " ",
  793. help->line);
  794. }
  795. }
  796. bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  797. {
  798. ctx_t *ctx = (ctx_t *)udata;
  799. faux_list_t *help_list = NULL;
  800. faux_list_node_t *iter = NULL;
  801. uint32_t param_len = 0;
  802. char *param_data = NULL;
  803. uint16_t param_type = 0;
  804. size_t max_prefix_len = 0;
  805. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  806. process_prompt_param(ctx->tinyrl, msg);
  807. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  808. help_compare, NULL, help_free);
  809. // Wait for PREFIX - LINE pairs
  810. iter = faux_msg_init_param_iter(msg);
  811. while (faux_msg_get_param_each(&iter, &param_type, (void **)&param_data,
  812. &param_len)) {
  813. char *prefix_str = NULL;
  814. char *line_str = NULL;
  815. help_t *help = NULL;
  816. size_t prefix_len = 0;
  817. // Get PREFIX
  818. if (KTP_PARAM_PREFIX != param_type)
  819. continue;
  820. prefix_str = faux_str_dupn(param_data, param_len);
  821. prefix_len = param_len;
  822. // Get LINE
  823. if (!faux_msg_get_param_each(&iter, &param_type,
  824. (void **)&param_data, &param_len) ||
  825. (KTP_PARAM_LINE != param_type)) {
  826. faux_str_free(prefix_str);
  827. break;
  828. }
  829. line_str = faux_str_dupn(param_data, param_len);
  830. help = help_new(prefix_str, line_str);
  831. if (!faux_list_add(help_list, help)) {
  832. help_free(help);
  833. continue;
  834. }
  835. if (prefix_len > max_prefix_len)
  836. max_prefix_len = prefix_len;
  837. }
  838. if (faux_list_len(help_list) > 0) {
  839. tinyrl_multi_crlf(ctx->tinyrl);
  840. tinyrl_reset_line_state(ctx->tinyrl);
  841. display_help(ctx->tinyrl, help_list, max_prefix_len);
  842. tinyrl_redisplay(ctx->tinyrl);
  843. }
  844. faux_list_free(help_list);
  845. // Operation is finished so restore stdin handler
  846. faux_eloop_add_fd(ktp_session_eloop(ktp), STDIN_FILENO, POLLIN,
  847. stdin_cb, ctx);
  848. ktp = ktp; // happy compiler
  849. return BOOL_TRUE;
  850. }
  851. //size_t max_stdout_len = 0;
  852. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  853. void *udata)
  854. {
  855. ctx_t *ctx = (ctx_t *)udata;
  856. assert(ctx);
  857. //if (len > max_stdout_len) {
  858. //max_stdout_len = len;
  859. //fprintf(stderr, "max_stdout_len=%ld\n", max_stdout_len);
  860. //}
  861. // Start pager if necessary
  862. if (
  863. ctx->opts->pager_enabled && // Pager enabled within config file
  864. (ctx->pager_working == TRI_UNDEFINED) && // Pager is not working
  865. !KTP_STATUS_IS_INTERACTIVE(ktp_session_cmd_features(ktp)) // Non interactive command
  866. ) {
  867. ctx->pager_pipe = popen(ctx->opts->pager, "we");
  868. if (!ctx->pager_pipe)
  869. ctx->pager_working = TRI_FALSE; // Indicates can't start
  870. else
  871. ctx->pager_working = TRI_TRUE;
  872. }
  873. // Write to pager's pipe if pager is really working
  874. // Don't do anything if pager state is TRI_FALSE
  875. if (ctx->pager_working == TRI_TRUE) {
  876. if (faux_write_block(fileno(ctx->pager_pipe), line, len) <= 0) {
  877. // If we can't write to pager pipe then send
  878. // "SIGPIPE" to server. Pager is finished or broken.
  879. ktp_session_stdout_close(ktp);
  880. ctx->pager_working = TRI_FALSE;
  881. return BOOL_TRUE; // Don't break the loop
  882. }
  883. // TRI_UNDEFINED here means that pager is not needed
  884. } else if (ctx->pager_working == TRI_UNDEFINED) {
  885. if (faux_write_block(STDOUT_FILENO, line, len) < 0)
  886. return BOOL_FALSE;
  887. }
  888. return BOOL_TRUE;
  889. }
  890. static void signal_handler_empty(int signo)
  891. {
  892. signo = signo; // Happy compiler
  893. }