klish.c 28 KB

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