klish.c 29 KB

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