klish.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_CMD);
  50. if (opts->line)
  51. faux_msg_add_param(msg, KTP_PARAM_LINE,
  52. opts->line, strlen(opts->line));
  53. faux_msg_debug(msg);
  54. faux_msg_send(msg, net);
  55. faux_msg_free(msg);
  56. msg = faux_msg_recv(net);
  57. faux_msg_debug(msg);
  58. faux_msg_free(msg);
  59. faux_net_free(net);
  60. retval = 0;
  61. err:
  62. ktp_session_free(session);
  63. ktp_disconnect(unix_sock);
  64. opts_free(opts);
  65. return retval;
  66. }