interactive.c 2.8 KB

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