interactive.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  21. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  22. void *associated_data, void *user_data);
  23. // Keys
  24. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
  25. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key);
  26. int klish_interactive_shell(ktp_session_t *ktp, struct options *opts)
  27. {
  28. ctx_t ctx = {};
  29. faux_eloop_t *eloop = NULL;
  30. tinyrl_t *tinyrl = NULL;
  31. int stdin_flags = 0;
  32. char *hist_path = NULL;
  33. assert(ktp);
  34. if (!ktp)
  35. return -1;
  36. // Set stdin to O_NONBLOCK mode
  37. stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  38. fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
  39. hist_path = faux_expand_tilde("~/.klish_history");
  40. tinyrl = tinyrl_new(stdin, stdout, hist_path, 100);
  41. faux_str_free(hist_path);
  42. tinyrl_set_prompt(tinyrl, "$ ");
  43. tinyrl_set_udata(tinyrl, &ctx);
  44. tinyrl_bind_key(tinyrl, '\n', tinyrl_key_enter);
  45. tinyrl_bind_key(tinyrl, '\r', tinyrl_key_enter);
  46. tinyrl_bind_key(tinyrl, '\t', tinyrl_key_tab);
  47. tinyrl_redisplay(tinyrl);
  48. ctx.ktp = ktp;
  49. ctx.tinyrl = tinyrl;
  50. ctx.opts = opts;
  51. // Don't stop interactive loop on each answer
  52. ktp_session_set_stop_on_answer(ktp, BOOL_FALSE);
  53. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK, cmd_ack_cb, &ctx);
  54. ktp_session_set_cb(ktp, KTP_SESSION_CB_COMPLETION_ACK, completion_ack_cb, &ctx);
  55. eloop = ktp_session_eloop(ktp);
  56. faux_eloop_add_fd(eloop, STDIN_FILENO, POLLIN, stdin_cb, &ctx);
  57. faux_eloop_loop(eloop);
  58. // Cleanup
  59. if (tinyrl_busy(tinyrl))
  60. faux_error_free(ktp_session_error(ktp));
  61. tinyrl_free(tinyrl);
  62. // Restore stdin mode
  63. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  64. return 0;
  65. }
  66. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  67. {
  68. ctx_t *ctx = (ctx_t *)udata;
  69. int rc = -1;
  70. faux_error_t *error = NULL;
  71. if (!ktp_session_retcode(ktp, &rc))
  72. rc = -1;
  73. error = ktp_session_error(ktp);
  74. if ((rc < 0) && (faux_error_len(error) > 0)) {
  75. faux_error_node_t *err_iter = faux_error_iter(error);
  76. const char *err = NULL;
  77. while ((err = faux_error_each(&err_iter)))
  78. fprintf(stderr, "Error: %s\n", err);
  79. }
  80. faux_error_free(error);
  81. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  82. if (!ktp_session_done(ktp))
  83. tinyrl_redisplay(ctx->tinyrl);
  84. // Happy compiler
  85. msg = msg;
  86. return BOOL_TRUE;
  87. }
  88. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  89. void *associated_data, void *udata)
  90. {
  91. ctx_t *ctx = (ctx_t *)udata;
  92. tinyrl_read(ctx->tinyrl);
  93. return BOOL_TRUE;
  94. }
  95. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  96. {
  97. const char *line = NULL;
  98. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  99. faux_error_t *error = faux_error_new();
  100. tinyrl_line_to_hist(tinyrl);
  101. tinyrl_multi_crlf(tinyrl);
  102. tinyrl_reset_line_state(tinyrl);
  103. line = tinyrl_line(tinyrl);
  104. // Don't do anything on empty line
  105. if (faux_str_is_empty(line)) {
  106. faux_error_free(error);
  107. return BOOL_TRUE;
  108. }
  109. ktp_session_cmd(ctx->ktp, line, error, ctx->opts->dry_run);
  110. tinyrl_reset_line(tinyrl);
  111. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  112. return BOOL_TRUE;
  113. }
  114. static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  115. {
  116. const char *line = NULL;
  117. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  118. line = tinyrl_line(tinyrl);
  119. ktp_session_completion(ctx->ktp, line, ctx->opts->dry_run);
  120. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  121. return BOOL_TRUE;
  122. }
  123. bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  124. {
  125. ctx_t *ctx = (ctx_t *)udata;
  126. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  127. printf("\nCOMPLETION\n");
  128. // Happy compiler
  129. ktp = ktp;
  130. msg = msg;
  131. return BOOL_TRUE;
  132. }