klish.c 26 KB

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