klish.c 4.9 KB

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