interactive.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/eloop.h>
  8. #include <klish/ktp.h>
  9. #include <klish/ktp_session.h>
  10. #include <tinyrl/tinyrl.h>
  11. // Context for main loop
  12. typedef struct ctx_s {
  13. ktp_session_t *ktp;
  14. tinyrl_t *tinyrl;
  15. } ctx_t;
  16. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
  17. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  18. void *associated_data, void *user_data);
  19. // Keys
  20. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
  21. int klish_interactive_shell(ktp_session_t *ktp)
  22. {
  23. ctx_t ctx = {};
  24. faux_eloop_t *eloop = NULL;
  25. tinyrl_t *tinyrl = NULL;
  26. int stdin_flags = 0;
  27. assert(ktp);
  28. if (!ktp)
  29. return -1;
  30. // Set stdin to O_NONBLOCK mode
  31. stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
  32. fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
  33. tinyrl = tinyrl_new(stdin, stdout, NULL, 0);
  34. tinyrl_set_prompt(tinyrl, "$ ");
  35. tinyrl_set_udata(tinyrl, &ctx);
  36. tinyrl_bind_key(tinyrl, KEY_CR, tinyrl_key_enter);
  37. tinyrl_redisplay(tinyrl);
  38. ctx.ktp = ktp;
  39. ctx.tinyrl = tinyrl;
  40. // Don't stop interactive loop on each answer
  41. ktp_session_set_stop_on_answer(ktp, BOOL_FALSE);
  42. ktp_session_set_cb(ktp, KTP_SESSION_CB_CMD_ACK, cmd_ack_cb, &ctx);
  43. eloop = ktp_session_eloop(ktp);
  44. faux_eloop_add_fd(eloop, STDIN_FILENO, POLLIN, stdin_cb, &ctx);
  45. faux_eloop_loop(eloop);
  46. // Cleanup
  47. tinyrl_free(tinyrl);
  48. // Restore stdin mode
  49. fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
  50. return 0;
  51. }
  52. bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
  53. {
  54. ctx_t *ctx = (ctx_t *)udata;
  55. // ktp_session_set_done(ktp, BOOL_TRUE);
  56. tinyrl_redisplay(ctx->tinyrl);
  57. // Happy compiler
  58. ktp = ktp;
  59. msg = msg;
  60. udata = udata;
  61. return BOOL_TRUE;
  62. }
  63. static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
  64. void *associated_data, void *udata)
  65. {
  66. ctx_t *ctx = (ctx_t *)udata;
  67. tinyrl_read(ctx->tinyrl);
  68. // ktp_session_cmd(ctx->ktp, "cmd", NULL, BOOL_FALSE);
  69. return BOOL_TRUE;
  70. }
  71. static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key)
  72. {
  73. const char *line = NULL;
  74. ctx_t *ctx = (ctx_t *)tinyrl_udata(tinyrl);
  75. tinyrl_multi_crlf(tinyrl);
  76. line = tinyrl_line(tinyrl);
  77. if (line) {
  78. printf("cmd = %s\n", line);
  79. ktp_session_cmd(ctx->ktp, line, NULL, BOOL_FALSE);
  80. }
  81. tinyrl_reset_line_state(tinyrl);
  82. tinyrl_reset_line(tinyrl);
  83. tinyrl_set_busy(tinyrl, BOOL_TRUE);
  84. return BOOL_TRUE;
  85. }