interactive.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // Context for main loop
  13. typedef struct ctx_s {
  14. ktp_session_t *ktp;
  15. tinyrl_t *tinyrl;
  16. } ctx_t;
  17. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  18. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  19. void *associated_data, void *user_data);
  20. // Keys
  21. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
  22. int klish_interactive_shell(ktp_session_t *ktp)
  23. {
  24. ctx_t ctx = {};
  25. faux_eloop_t *eloop = NULL;
  26. tinyrl_t *tinyrl = NULL;
  27. int stdin_flags = 0;
  28. assert(ktp);
  29. if (!ktp)
  30. return -1;
  31. // Set stdin to O_NONBLOCK mode
  32. stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  33. fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
  34. tinyrl = tinyrl_new(stdin, stdout, NULL, 0);
  35. tinyrl_set_prompt(tinyrl, "$ ");
  36. tinyrl_set_udata(tinyrl, &ctx);
  37. tinyrl_bind_key(tinyrl, KEY_CR, tinyrl_key_enter);
  38. tinyrl_redisplay(tinyrl);
  39. ctx.ktp = ktp;
  40. ctx.tinyrl = tinyrl;
  41. // Don't stop interactive loop on each answer
  42. ktp_session_set_stop_on_answer(ktp, BOOL_FALSE);
  43. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK, cmd_ack_cb, &ctx);
  44. eloop = ktp_session_eloop(ktp);
  45. faux_eloop_add_fd(eloop, STDIN_FILENO, POLLIN, stdin_cb, &ctx);
  46. faux_eloop_loop(eloop);
  47. // Cleanup
  48. if (tinyrl_busy(tinyrl)) {
  49. faux_error_free(ktp_session_error(ktp));
  50. } else {
  51. tinyrl_multi_crlf(tinyrl);
  52. }
  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. tinyrl_redisplay(ctx->tinyrl);
  75. // Happy compiler
  76. ktp = ktp;
  77. msg = msg;
  78. udata = udata;
  79. return BOOL_TRUE;
  80. }
  81. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  82. void *associated_data, void *udata)
  83. {
  84. ctx_t *ctx = (ctx_t *)udata;
  85. tinyrl_read(ctx->tinyrl);
  86. return BOOL_TRUE;
  87. }
  88. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  89. {
  90. const char *line = NULL;
  91. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  92. faux_error_t *error = faux_error_new();
  93. tinyrl_multi_crlf(tinyrl);
  94. tinyrl_reset_line_state(tinyrl);
  95. line = tinyrl_line(tinyrl);
  96. // Don't do anything on empty line
  97. if (faux_str_is_empty(line))
  98. return BOOL_TRUE;
  99. ktp_session_cmd(ctx->ktp, line, error, BOOL_FALSE);
  100. tinyrl_reset_line(tinyrl);
  101. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  102. return BOOL_TRUE;
  103. }