klish.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <faux/list.h>
  18. #include <faux/file.h>
  19. #include <klish/ktp.h>
  20. #include <klish/ktp_session.h>
  21. #include "private.h"
  22. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  23. void *user_data);
  24. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  25. void *user_data);
  26. int main(int argc, char **argv)
  27. {
  28. int retval = -1;
  29. struct options *opts = NULL;
  30. int unix_sock = -1;
  31. ktp_session_t *ktp = NULL;
  32. // Parse command line options
  33. opts = opts_init();
  34. if (opts_parse(argc, argv, opts)) {
  35. fprintf(stderr, "Error: Can't parse command line options\n");
  36. goto err;
  37. }
  38. // Connect to server
  39. unix_sock = ktp_connect_unix(opts->unix_socket_path);
  40. if (unix_sock < 0) {
  41. fprintf(stderr, "Error: Can't connect to server\n");
  42. goto err;
  43. }
  44. ktp = ktp_session_new(unix_sock);
  45. assert(ktp);
  46. if (!ktp) {
  47. fprintf(stderr, "Error: Can't create klish session\n");
  48. goto err;
  49. }
  50. ktp_session_set_stdout_cb(ktp, stdout_cb, NULL);
  51. ktp_session_set_stderr_cb(ktp, stderr_cb, NULL);
  52. // Commands from cmdline
  53. if (faux_list_len(opts->commands) > 0) {
  54. const char *line = NULL;
  55. faux_list_node_t *iter = faux_list_head(opts->commands);
  56. while ((line = faux_list_each(&iter))) {
  57. faux_error_t *error = faux_error_new();
  58. int retcode = -1;
  59. if (ktp_session_req_cmd(ktp, line, &retcode, error)) {
  60. fprintf(stderr, "Retcode: %d\n", retcode);
  61. } else {
  62. fprintf(stderr, "Error:\n");
  63. faux_error_fshow(error, stderr);
  64. }
  65. faux_error_free(error);
  66. }
  67. // Commands from files
  68. } else if (faux_list_len(opts->files) > 0) {
  69. const char *filename = NULL;
  70. faux_list_node_t *iter = faux_list_head(opts->files);
  71. while ((filename = (const char *)faux_list_each(&iter))) {
  72. char *line = NULL;
  73. faux_file_t *fd = NULL;
  74. fd = faux_file_open(filename, O_RDONLY, 0);
  75. while ((line = faux_file_getline(fd))) {
  76. faux_error_t *error = faux_error_new();
  77. int retcode = -1;
  78. if (ktp_session_req_cmd(ktp, line, &retcode, error)) {
  79. fprintf(stderr, "Retcode: %d\n", retcode);
  80. } else {
  81. fprintf(stderr, "Error:\n");
  82. faux_error_fshow(error, stderr);
  83. }
  84. faux_error_free(error);
  85. faux_str_free(line);
  86. }
  87. faux_file_close(fd);
  88. }
  89. // Interactive shell
  90. } else {
  91. }
  92. retval = 0;
  93. err:
  94. ktp_session_free(ktp);
  95. ktp_disconnect(unix_sock);
  96. opts_free(opts);
  97. return retval;
  98. }
  99. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  100. void *user_data)
  101. {
  102. if (write(STDOUT_FILENO, line, len) < 0)
  103. return BOOL_FALSE;
  104. ktp = ktp;
  105. user_data = user_data;
  106. return BOOL_TRUE;
  107. }
  108. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  109. void *user_data)
  110. {
  111. if (write(STDERR_FILENO, line, len) < 0)
  112. return BOOL_FALSE;
  113. ktp = ktp;
  114. user_data = user_data;
  115. return BOOL_TRUE;
  116. }