klish.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. bool_t rc = BOOL_FALSE;
  60. if (!opts->quiet)
  61. fprintf(stderr, "%s\n", line);
  62. rc = ktp_session_req_cmd(ktp, line, &retcode, error);
  63. if (!rc || (faux_error_len(error) > 0)) {
  64. fprintf(stderr, "Error:\n");
  65. faux_error_fshow(error, stderr);
  66. }
  67. if (rc)
  68. fprintf(stderr, "Retcode: %d\n", retcode);
  69. faux_error_free(error);
  70. }
  71. // Commands from files
  72. } else if (faux_list_len(opts->files) > 0) {
  73. const char *filename = NULL;
  74. faux_list_node_t *iter = faux_list_head(opts->files);
  75. while ((filename = (const char *)faux_list_each(&iter))) {
  76. char *line = NULL;
  77. faux_file_t *fd = NULL;
  78. fd = faux_file_open(filename, O_RDONLY, 0);
  79. while ((line = faux_file_getline(fd))) {
  80. faux_error_t *error = faux_error_new();
  81. int retcode = -1;
  82. bool_t rc = BOOL_FALSE;
  83. if (!opts->quiet)
  84. fprintf(stderr, "%s\n", line);
  85. rc = ktp_session_req_cmd(ktp, line, &retcode, error);
  86. if (!rc || (faux_error_len(error) > 0)) {
  87. fprintf(stderr, "Error:\n");
  88. faux_error_fshow(error, stderr);
  89. }
  90. if (rc)
  91. fprintf(stderr, "Retcode: %d\n", retcode);
  92. faux_error_free(error);
  93. faux_str_free(line);
  94. }
  95. faux_file_close(fd);
  96. }
  97. // Interactive shell
  98. } else {
  99. }
  100. retval = 0;
  101. err:
  102. ktp_session_free(ktp);
  103. ktp_disconnect(unix_sock);
  104. opts_free(opts);
  105. return retval;
  106. }
  107. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  108. void *user_data)
  109. {
  110. if (write(STDOUT_FILENO, line, len) < 0)
  111. return BOOL_FALSE;
  112. ktp = ktp;
  113. user_data = user_data;
  114. return BOOL_TRUE;
  115. }
  116. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  117. void *user_data)
  118. {
  119. if (write(STDERR_FILENO, line, len) < 0)
  120. return BOOL_FALSE;
  121. ktp = ktp;
  122. user_data = user_data;
  123. return BOOL_TRUE;
  124. }