interactive.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. assert(ktp);
  31. if (!ktp)
  32. return -1;
  33. // Set stdin to O_NONBLOCK mode
  34. stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  35. fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
  36. tinyrl = tinyrl_new(stdin, stdout, NULL, 0);
  37. tinyrl_set_prompt(tinyrl, "$ ");
  38. tinyrl_set_udata(tinyrl, &ctx);
  39. tinyrl_bind_key(tinyrl, KEY_CR, tinyrl_key_enter);
  40. tinyrl_redisplay(tinyrl);
  41. ctx.ktp = ktp;
  42. ctx.tinyrl = tinyrl;
  43. ctx.opts = opts;
  44. // Don't stop interactive loop on each answer
  45. ktp_session_set_stop_on_answer(ktp, BOOL_FALSE);
  46. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK, cmd_ack_cb, &ctx);
  47. eloop = ktp_session_eloop(ktp);
  48. faux_eloop_add_fd(eloop, STDIN_FILENO, POLLIN, stdin_cb, &ctx);
  49. faux_eloop_loop(eloop);
  50. // Cleanup
  51. if (tinyrl_busy(tinyrl))
  52. faux_error_free(ktp_session_error(ktp));
  53. tinyrl_free(tinyrl);
  54. // Restore stdin mode
  55. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  56. return 0;
  57. }
  58. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  59. {
  60. ctx_t *ctx = (ctx_t *)udata;
  61. int rc = -1;
  62. faux_error_t *error = NULL;
  63. if (!ktp_session_retcode(ktp, &rc))
  64. rc = -1;
  65. error = ktp_session_error(ktp);
  66. if ((rc < 0) && (faux_error_len(error) > 0)) {
  67. faux_error_node_t *err_iter = faux_error_iter(error);
  68. const char *err = NULL;
  69. while ((err = faux_error_each(&err_iter)))
  70. fprintf(stderr, "Error: %s\n", err);
  71. }
  72. faux_error_free(error);
  73. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  74. if (!ktp_session_done(ktp))
  75. tinyrl_redisplay(ctx->tinyrl);
  76. // Happy compiler
  77. ktp = ktp;
  78. msg = msg;
  79. udata = udata;
  80. return BOOL_TRUE;
  81. }
  82. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  83. void *associated_data, void *udata)
  84. {
  85. ctx_t *ctx = (ctx_t *)udata;
  86. tinyrl_read(ctx->tinyrl);
  87. return BOOL_TRUE;
  88. }
  89. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  90. {
  91. const char *line = NULL;
  92. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  93. faux_error_t *error = faux_error_new();
  94. tinyrl_multi_crlf(tinyrl);
  95. tinyrl_reset_line_state(tinyrl);
  96. line = tinyrl_line(tinyrl);
  97. // Don't do anything on empty line
  98. if (faux_str_is_empty(line)) {
  99. faux_error_free(error);
  100. return BOOL_TRUE;
  101. }
  102. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  103. tinyrl_reset_line(tinyrl);
  104. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  105. return BOOL_TRUE;
  106. }