klish.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <getopt.h>
  12. #include <sys/socket.h>
  13. #include <sys/un.h>
  14. #include <faux/faux.h>
  15. #include <faux/str.h>
  16. #include <faux/msg.h>
  17. #include <klish/ktp.h>
  18. #include <klish/ktp_session.h>
  19. #include "private.h"
  20. int main(int argc, char **argv)
  21. {
  22. int retval = -1;
  23. struct options *opts = NULL;
  24. int unix_sock = -1;
  25. ktp_session_t *session = NULL;
  26. faux_msg_t *msg = NULL;
  27. faux_net_t *net = NULL;
  28. // Parse command line options
  29. opts = opts_init();
  30. if (opts_parse(argc, argv, opts)) {
  31. fprintf(stderr, "Error: Can't parse command line options\n");
  32. goto err;
  33. }
  34. // Connect to server
  35. unix_sock = ktp_connect_unix(opts->unix_socket_path);
  36. if (unix_sock < 0) {
  37. fprintf(stderr, "Error: Can't connect to server\n");
  38. goto err;
  39. }
  40. session = ktp_session_new(unix_sock);
  41. assert(session);
  42. if (!session) {
  43. fprintf(stderr, "Error: Can't create klish session\n");
  44. goto err;
  45. }
  46. net = faux_net_new();
  47. faux_net_set_fd(net, ktp_session_fd(session));
  48. msg = faux_msg_new(KTP_MAGIC, KTP_MAJOR, KTP_MINOR);
  49. faux_msg_set_cmd(msg, KTP_AUTH);
  50. faux_msg_send(msg, net);
  51. // write(ktp_session_fd(session), "hello", 5);
  52. faux_msg_free(msg);
  53. faux_net_free(net);
  54. retval = 0;
  55. err:
  56. ktp_session_free(session);
  57. ktp_disconnect(unix_sock);
  58. opts_free(opts);
  59. return retval;
  60. }