klish.c 26 KB

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