klish.c 5.1 KB

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