interactive.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. tinyrl_multi_crlf(tinyrl);
  49. tinyrl_free(tinyrl);
  50. // Restore stdin mode
  51. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  52. return 0;
  53. }
  54. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  55. {
  56. ctx_t *ctx = (ctx_t *)udata;
  57. int rc = -1;
  58. faux_error_t *error = NULL;
  59. if (!ktp_session_retcode(ktp, &rc))
  60. rc = -1;
  61. error = ktp_session_error(ktp);
  62. if ((rc < 0) && (faux_error_len(error) > 0)) {
  63. faux_error_node_t *err_iter = faux_error_iter(error);
  64. const char *err = NULL;
  65. while ((err = faux_error_each(&err_iter)))
  66. fprintf(stderr, "Error: %s\n", err);
  67. }
  68. faux_error_free(error);
  69. tinyrl_set_busy(ctx->tinyrl, BOOL_FALSE);
  70. tinyrl_redisplay(ctx->tinyrl);
  71. // Happy compiler
  72. ktp = ktp;
  73. msg = msg;
  74. udata = udata;
  75. return BOOL_TRUE;
  76. }
  77. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  78. void *associated_data, void *udata)
  79. {
  80. ctx_t *ctx = (ctx_t *)udata;
  81. tinyrl_read(ctx->tinyrl);
  82. return BOOL_TRUE;
  83. }
  84. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  85. {
  86. const char *line = NULL;
  87. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  88. faux_error_t *error = faux_error_new();
  89. tinyrl_multi_crlf(tinyrl);
  90. tinyrl_reset_line_state(tinyrl);
  91. line = tinyrl_line(tinyrl);
  92. // Don't do anything on empty line
  93. if (faux_str_is_empty(line))
  94. return BOOL_TRUE;
  95. ktp_session_cmd(ctx->ktp, line, error, BOOL_FALSE);
  96. tinyrl_reset_line(tinyrl);
  97. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  98. return BOOL_TRUE;
  99. }