klish.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. // Parse config file
  56. if (!access(opts->cfgfile, R_OK)) {
  57. if (!config_parse(opts->cfgfile, opts))
  58. goto err;
  59. } else if (opts->cfgfile_userdefined) {
  60. // User defined config must be found
  61. fprintf(stderr, "Error: Can't find config file %s\n",
  62. opts->cfgfile);
  63. goto err;
  64. }
  65. // Connect to server
  66. unix_sock = ktp_connect_unix(opts->unix_socket_path);
  67. if (unix_sock < 0) {
  68. fprintf(stderr, "Error: Can't connect to server\n");
  69. goto err;
  70. }
  71. // Eloop object
  72. eloop = faux_eloop_new(NULL);
  73. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, NULL);
  74. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, NULL);
  75. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, NULL);
  76. // KTP session
  77. ktp = ktp_session_new(unix_sock, eloop);
  78. assert(ktp);
  79. if (!ktp) {
  80. fprintf(stderr, "Error: Can't create klish session\n");
  81. goto err;
  82. }
  83. // These callback functions is only for batch mode. Interactive
  84. // mode can reassign them.
  85. ktp_session_set_cb(ktp, KTP_SESSION_CB_STDOUT, stdout_cb, NULL);
  86. ktp_session_set_cb(ktp, KTP_SESSION_CB_STDERR, stderr_cb, NULL);
  87. // Commands from cmdline
  88. if (faux_list_len(opts->commands) > 0) {
  89. const char *line = NULL;
  90. faux_list_node_t *iter = faux_list_head(opts->commands);
  91. while ((line = faux_list_each(&iter))) {
  92. faux_error_t *error = faux_error_new();
  93. bool_t rc = BOOL_FALSE;
  94. // Echo command
  95. if (!opts->quiet)
  96. fprintf(stderr, "%s\n", line);
  97. // Request to server
  98. rc = ktp_sync_cmd(ktp, line, &retcode,
  99. error, opts->dry_run);
  100. if (!rc)
  101. retcode = -1;
  102. if (faux_error_len(error) > 0) {
  103. fprintf(stderr, "Error:\n");
  104. faux_error_fshow(error, stderr);
  105. }
  106. faux_error_free(error);
  107. fprintf(stderr, "Retcode: %d\n", retcode);
  108. // Stop-on-error
  109. if (opts->stop_on_error && (!rc || retcode != 0))
  110. break;
  111. }
  112. // Commands from files
  113. } else if (faux_list_len(opts->files) > 0) {
  114. const char *filename = NULL;
  115. faux_list_node_t *iter = faux_list_head(opts->files);
  116. while ((filename = (const char *)faux_list_each(&iter))) {
  117. char *line = NULL;
  118. bool_t stop = BOOL_FALSE;
  119. faux_file_t *fd = faux_file_open(filename, O_RDONLY, 0);
  120. while ((line = faux_file_getline(fd))) {
  121. faux_error_t *error = faux_error_new();
  122. bool_t rc = BOOL_FALSE;
  123. // Echo command
  124. if (!opts->quiet)
  125. fprintf(stderr, "%s\n", line);
  126. // Request to server
  127. rc = ktp_sync_cmd(ktp, line, &retcode,
  128. error, opts->dry_run);
  129. if (!rc)
  130. retcode = -1;
  131. if (faux_error_len(error) > 0) {
  132. fprintf(stderr, "Error:\n");
  133. faux_error_fshow(error, stderr);
  134. }
  135. faux_error_free(error);
  136. fprintf(stderr, "Retcode: %d\n", retcode);
  137. faux_str_free(line);
  138. // Stop-on-error
  139. if (opts->stop_on_error && (!rc || retcode != 0)) {
  140. stop = BOOL_TRUE;
  141. break;
  142. }
  143. }
  144. faux_file_close(fd);
  145. if (stop)
  146. break;
  147. }
  148. // Interactive shell
  149. } else {
  150. // Interactive code is complex so move it to separate file
  151. retcode = klish_interactive_shell(ktp, opts);
  152. }
  153. retval = 0;
  154. err:
  155. ktp_session_free(ktp);
  156. faux_eloop_free(eloop);
  157. ktp_disconnect(unix_sock);
  158. opts_free(opts);
  159. if ((retval < 0) || (retcode < 0))
  160. return -1;
  161. return 0;
  162. }
  163. static bool_t ktp_sync_cmd(ktp_session_t *ktp, const char *line,
  164. int *retcode, faux_error_t *error, bool_t dry_run)
  165. {
  166. if (!ktp_session_cmd(ktp, line, error, dry_run))
  167. return BOOL_FALSE;
  168. faux_eloop_loop(ktp_session_eloop(ktp));
  169. return ktp_session_retcode(ktp, retcode);
  170. }
  171. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  172. void *user_data)
  173. {
  174. if (write(STDOUT_FILENO, line, len) < 0)
  175. return BOOL_FALSE;
  176. ktp = ktp;
  177. user_data = user_data;
  178. return BOOL_TRUE;
  179. }
  180. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  181. void *user_data)
  182. {
  183. if (write(STDERR_FILENO, line, len) < 0)
  184. return BOOL_FALSE;
  185. ktp = ktp;
  186. user_data = user_data;
  187. return BOOL_TRUE;
  188. }
  189. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  190. void *associated_data, void *user_data)
  191. {
  192. // Happy compiler
  193. eloop = eloop;
  194. type = type;
  195. associated_data = associated_data;
  196. user_data = user_data;
  197. return BOOL_FALSE; // Stop Event Loop
  198. }