klish.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. if (KTP_STATUS_IS_ERROR(faux_msg_get_status(msg))) {
  59. char *error = faux_msg_get_str_param_by_type(msg, KTP_PARAM_ERROR);
  60. if (error) {
  61. printf("Error: %s\n", error);
  62. faux_str_free(error);
  63. }
  64. }
  65. faux_msg_free(msg);
  66. faux_net_free(net);
  67. retval = 0;
  68. err:
  69. ktp_session_free(session);
  70. ktp_disconnect(unix_sock);
  71. opts_free(opts);
  72. return retval;
  73. }