interactive.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <faux/faux.h>
  7. #include <faux/str.h>
  8. #include <faux/eloop.h>
  9. #include <klish/ktp.h>
  10. #include <klish/ktp_session.h>
  11. #include <tinyrl/tinyrl.h>
  12. #include "private.h"
  13. // Context for main loop
  14. typedef struct ctx_s {
  15. ktp_session_t *ktp;
  16. tinyrl_t *tinyrl;
  17. struct options *opts;
  18. } ctx_t;
  19. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  20. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  21. void *associated_data, void *user_data);
  22. // Keys
  23. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
  24. int klish_interactive_shell(ktp_session_t *ktp, struct options *opts)
  25. {
  26. ctx_t ctx = {};
  27. faux_eloop_t *eloop = NULL;
  28. tinyrl_t *tinyrl = NULL;
  29. int stdin_flags = 0;
  30. char *hist_path = NULL;
  31. assert(ktp);
  32. if (!ktp)
  33. return -1;
  34. // Set stdin to O_NONBLOCK mode
  35. stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  36. fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
  37. hist_path = faux_expand_tilde("~/.klish_history");
  38. tinyrl = tinyrl_new(stdin, stdout, hist_path, 100);
  39. faux_str_free(hist_path);
  40. tinyrl_set_prompt(tinyrl, "$ ");
  41. tinyrl_set_udata(tinyrl, &ctx);
  42. tinyrl_bind_key(tinyrl, KEY_CR, tinyrl_key_enter);
  43. tinyrl_redisplay(tinyrl);
  44. ctx.ktp = ktp;
  45. ctx.tinyrl = tinyrl;
  46. ctx.opts = opts;
  47. // Don't stop interactive loop on each answer
  48. ktp_session_set_stop_on_answer(ktp, BOOL_FALSE);
  49. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK, cmd_ack_cb, &ctx);
  50. eloop = ktp_session_eloop(ktp);
  51. faux_eloop_add_fd(eloop, STDIN_FILENO, POLLIN, stdin_cb, &ctx);
  52. faux_eloop_loop(eloop);
  53. // Cleanup
  54. if (tinyrl_busy(tinyrl))
  55. faux_error_free(ktp_session_error(ktp));
  56. tinyrl_free(tinyrl);
  57. // Restore stdin mode
  58. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  59. return 0;
  60. }
  61. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  62. {
  63. ctx_t *ctx = (ctx_t *)udata;
  64. int rc = -1;
  65. faux_error_t *error = NULL;
  66. if (!ktp_session_retcode(ktp, &rc))
  67. rc = -1;
  68. error = ktp_session_error(ktp);
  69. if ((rc < 0) && (faux_error_len(error) > 0)) {
  70. faux_error_node_t *err_iter = faux_error_iter(error);
  71. const char *err = NULL;
  72. while ((err = faux_error_each(&err_iter)))
  73. fprintf(stderr, "Error: %s\n", err);
  74. }
  75. faux_error_free(error);
  76. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  77. if (!ktp_session_done(ktp))
  78. tinyrl_redisplay(ctx->tinyrl);
  79. // Happy compiler
  80. ktp = ktp;
  81. msg = msg;
  82. udata = udata;
  83. return BOOL_TRUE;
  84. }
  85. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  86. void *associated_data, void *udata)
  87. {
  88. ctx_t *ctx = (ctx_t *)udata;
  89. tinyrl_read(ctx->tinyrl);
  90. return BOOL_TRUE;
  91. }
  92. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  93. {
  94. const char *line = NULL;
  95. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  96. faux_error_t *error = faux_error_new();
  97. tinyrl_line_to_hist(tinyrl);
  98. tinyrl_multi_crlf(tinyrl);
  99. tinyrl_reset_line_state(tinyrl);
  100. line = tinyrl_line(tinyrl);
  101. // Don't do anything on empty line
  102. if (faux_str_is_empty(line)) {
  103. faux_error_free(error);
  104. return BOOL_TRUE;
  105. }
  106. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  107. tinyrl_reset_line(tinyrl);
  108. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  109. return BOOL_TRUE;
  110. }