klish.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. int retcode = 0;
  33. // Parse command line options
  34. opts = opts_init();
  35. if (opts_parse(argc, argv, opts)) {
  36. fprintf(stderr, "Error: Can't parse command line options\n");
  37. goto err;
  38. }
  39. // Connect to server
  40. unix_sock = ktp_connect_unix(opts->unix_socket_path);
  41. if (unix_sock < 0) {
  42. fprintf(stderr, "Error: Can't connect to server\n");
  43. goto err;
  44. }
  45. ktp = ktp_session_new(unix_sock);
  46. assert(ktp);
  47. if (!ktp) {
  48. fprintf(stderr, "Error: Can't create klish session\n");
  49. goto err;
  50. }
  51. ktp_session_set_stdout_cb(ktp, stdout_cb, NULL);
  52. ktp_session_set_stderr_cb(ktp, stderr_cb, NULL);
  53. // Commands from cmdline
  54. if (faux_list_len(opts->commands) > 0) {
  55. const char *line = NULL;
  56. faux_list_node_t *iter = faux_list_head(opts->commands);
  57. while ((line = faux_list_each(&iter))) {
  58. faux_error_t *error = faux_error_new();
  59. bool_t rc = BOOL_FALSE;
  60. // Echo command
  61. if (!opts->quiet)
  62. fprintf(stderr, "%s\n", line);
  63. // Request to server
  64. rc = ktp_session_req_cmd(ktp, line, &retcode,
  65. error, opts->dry_run);
  66. if (!rc)
  67. retcode = -1;
  68. if (faux_error_len(error) > 0) {
  69. fprintf(stderr, "Error:\n");
  70. faux_error_fshow(error, stderr);
  71. }
  72. faux_error_free(error);
  73. fprintf(stderr, "Retcode: %d\n", retcode);
  74. // Stop-on-error
  75. if (opts->stop_on_error && (!rc || retcode != 0))
  76. break;
  77. }
  78. // Commands from files
  79. } else if (faux_list_len(opts->files) > 0) {
  80. const char *filename = NULL;
  81. faux_list_node_t *iter = faux_list_head(opts->files);
  82. while ((filename = (const char *)faux_list_each(&iter))) {
  83. char *line = NULL;
  84. bool_t stop = BOOL_FALSE;
  85. faux_file_t *fd = faux_file_open(filename, O_RDONLY, 0);
  86. while ((line = faux_file_getline(fd))) {
  87. faux_error_t *error = faux_error_new();
  88. bool_t rc = BOOL_FALSE;
  89. // Echo command
  90. if (!opts->quiet)
  91. fprintf(stderr, "%s\n", line);
  92. // Request to server
  93. rc = ktp_session_req_cmd(ktp, line, &retcode,
  94. error, opts->dry_run);
  95. if (!rc)
  96. retcode = -1;
  97. if (faux_error_len(error) > 0) {
  98. fprintf(stderr, "Error:\n");
  99. faux_error_fshow(error, stderr);
  100. }
  101. faux_error_free(error);
  102. fprintf(stderr, "Retcode: %d\n", retcode);
  103. faux_str_free(line);
  104. // Stop-on-error
  105. if (opts->stop_on_error && (!rc || retcode != 0)) {
  106. stop = BOOL_TRUE;
  107. break;
  108. }
  109. }
  110. faux_file_close(fd);
  111. if (stop)
  112. break;
  113. }
  114. // Interactive shell
  115. } else {
  116. }
  117. retval = 0;
  118. err:
  119. ktp_session_free(ktp);
  120. ktp_disconnect(unix_sock);
  121. opts_free(opts);
  122. if ((retval < 0) || (retcode < 0))
  123. return -1;
  124. return 0;
  125. }
  126. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  127. void *user_data)
  128. {
  129. if (write(STDOUT_FILENO, line, len) < 0)
  130. return BOOL_FALSE;
  131. ktp = ktp;
  132. user_data = user_data;
  133. return BOOL_TRUE;
  134. }
  135. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  136. void *user_data)
  137. {
  138. if (write(STDERR_FILENO, line, len) < 0)
  139. return BOOL_FALSE;
  140. ktp = ktp;
  141. user_data = user_data;
  142. return BOOL_TRUE;
  143. }