klish.c 25 KB

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