klish.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /*
  47. net = faux_net_new();
  48. faux_net_set_fd(net, ktp_session_fd(session));
  49. */
  50. ktp_session_req_cmd(session, opts->line, NULL);
  51. /*
  52. msg = faux_msg_new(KTP_MAGIC, KTP_MAJOR, KTP_MINOR);
  53. faux_msg_set_cmd(msg, KTP_CMD);
  54. if (opts->line)
  55. faux_msg_add_param(msg, KTP_PARAM_LINE,
  56. opts->line, strlen(opts->line));
  57. faux_msg_debug(msg);
  58. faux_msg_send(msg, net);
  59. faux_msg_free(msg);
  60. msg = faux_msg_recv(net);
  61. if (msg) {
  62. faux_msg_debug(msg);
  63. if (KTP_STATUS_IS_ERROR(faux_msg_get_status(msg))) {
  64. char *error = faux_msg_get_str_param_by_type(msg, KTP_PARAM_ERROR);
  65. if (error) {
  66. printf("Error: %s\n", error);
  67. faux_str_free(error);
  68. }
  69. }
  70. {
  71. int retcode = -1;
  72. uint8_t *retcode8bit = NULL;
  73. if (faux_msg_get_param_by_type(msg, KTP_PARAM_RETCODE,
  74. (void **)&retcode8bit, NULL)) {
  75. retcode = (int)(*retcode8bit);
  76. printf("Retcode: %d\n", retcode);
  77. }
  78. }
  79. faux_msg_free(msg);
  80. }
  81. faux_net_free(net);
  82. */
  83. retval = 0;
  84. err:
  85. ktp_session_free(session);
  86. ktp_disconnect(unix_sock);
  87. opts_free(opts);
  88. return retval;
  89. }