klish.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <faux/eloop.h>
  20. #include <klish/ktp.h>
  21. #include <klish/ktp_session.h>
  22. #include "private.h"
  23. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  24. void *user_data);
  25. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  26. void *user_data);
  27. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  28. void *associated_data, void *user_data);
  29. int main(int argc, char **argv)
  30. {
  31. int retval = -1;
  32. struct options *opts = NULL;
  33. int unix_sock = -1;
  34. ktp_session_t *ktp = NULL;
  35. int retcode = 0;
  36. faux_eloop_t *eloop = NULL;
  37. // Parse command line options
  38. opts = opts_init();
  39. if (opts_parse(argc, argv, opts)) {
  40. fprintf(stderr, "Error: Can't parse command line options\n");
  41. goto err;
  42. }
  43. // Connect to server
  44. unix_sock = ktp_connect_unix(opts->unix_socket_path);
  45. if (unix_sock < 0) {
  46. fprintf(stderr, "Error: Can't connect to server\n");
  47. goto err;
  48. }
  49. // Eloop object
  50. eloop = faux_eloop_new(NULL);
  51. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  52. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  53. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  54. // KTP session
  55. ktp = ktp_session_new(unix_sock, eloop);
  56. assert(ktp);
  57. if (!ktp) {
  58. fprintf(stderr, "Error: Can't create klish session\n");
  59. goto err;
  60. }
  61. ktp_session_set_stdout_cb(ktp, stdout_cb, NULL);
  62. ktp_session_set_stderr_cb(ktp, stderr_cb, NULL);
  63. // Commands from cmdline
  64. if (faux_list_len(opts->commands) > 0) {
  65. const char *line = NULL;
  66. faux_list_node_t *iter = faux_list_head(opts->commands);
  67. while ((line = faux_list_each(&iter))) {
  68. faux_error_t *error = faux_error_new();
  69. bool_t rc = BOOL_FALSE;
  70. // Echo command
  71. if (!opts->quiet)
  72. fprintf(stderr, "%s\n", line);
  73. // Request to server
  74. rc = ktp_session_req_cmd(ktp, line, &retcode,
  75. error, opts->dry_run);
  76. if (!rc)
  77. retcode = -1;
  78. if (faux_error_len(error) > 0) {
  79. fprintf(stderr, "Error:\n");
  80. faux_error_fshow(error, stderr);
  81. }
  82. faux_error_free(error);
  83. fprintf(stderr, "Retcode: %d\n", retcode);
  84. // Stop-on-error
  85. if (opts->stop_on_error && (!rc || retcode != 0))
  86. break;
  87. }
  88. // Commands from files
  89. } else if (faux_list_len(opts->files) > 0) {
  90. const char *filename = NULL;
  91. faux_list_node_t *iter = faux_list_head(opts->files);
  92. while ((filename = (const char *)faux_list_each(&iter))) {
  93. char *line = NULL;
  94. bool_t stop = BOOL_FALSE;
  95. faux_file_t *fd = faux_file_open(filename, O_RDONLY, 0);
  96. while ((line = faux_file_getline(fd))) {
  97. faux_error_t *error = faux_error_new();
  98. bool_t rc = BOOL_FALSE;
  99. // Echo command
  100. if (!opts->quiet)
  101. fprintf(stderr, "%s\n", line);
  102. // Request to server
  103. rc = ktp_session_req_cmd(ktp, line, &retcode,
  104. error, opts->dry_run);
  105. if (!rc)
  106. retcode = -1;
  107. if (faux_error_len(error) > 0) {
  108. fprintf(stderr, "Error:\n");
  109. faux_error_fshow(error, stderr);
  110. }
  111. faux_error_free(error);
  112. fprintf(stderr, "Retcode: %d\n", retcode);
  113. faux_str_free(line);
  114. // Stop-on-error
  115. if (opts->stop_on_error && (!rc || retcode != 0)) {
  116. stop = BOOL_TRUE;
  117. break;
  118. }
  119. }
  120. faux_file_close(fd);
  121. if (stop)
  122. break;
  123. }
  124. // Interactive shell
  125. } else {
  126. }
  127. retval = 0;
  128. err:
  129. ktp_session_free(ktp);
  130. ktp_disconnect(unix_sock);
  131. opts_free(opts);
  132. if ((retval < 0) || (retcode < 0))
  133. return -1;
  134. return 0;
  135. }
  136. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  137. void *user_data)
  138. {
  139. if (write(STDOUT_FILENO, line, len) < 0)
  140. return BOOL_FALSE;
  141. ktp = ktp;
  142. user_data = user_data;
  143. return BOOL_TRUE;
  144. }
  145. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  146. void *user_data)
  147. {
  148. if (write(STDERR_FILENO, line, len) < 0)
  149. return BOOL_FALSE;
  150. ktp = ktp;
  151. user_data = user_data;
  152. return BOOL_TRUE;
  153. }
  154. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  155. void *associated_data, void *user_data)
  156. {
  157. // Happy compiler
  158. eloop = eloop;
  159. type = type;
  160. associated_data = associated_data;
  161. user_data = user_data;
  162. return BOOL_FALSE; // Stop Event Loop
  163. }