klish.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 int ktp_sync_cmd(ktp_session_t *ktp, const char *line,
  36. const struct options *opts);
  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. // Ignore SIGPIPE from server
  88. signal(SIGPIPE, SIG_IGN);
  89. // Commands from cmdline
  90. if (faux_list_len(opts->commands) > 0) {
  91. const char *line = NULL;
  92. faux_list_node_t *iter = faux_list_head(opts->commands);
  93. while ((line = faux_list_each(&iter))) {
  94. // Request to server
  95. retcode = ktp_sync_cmd(ktp, line, opts);
  96. // Stop-on-error
  97. if (opts->stop_on_error && (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. // Request to server
  110. retcode = ktp_sync_cmd(ktp, line, opts);
  111. faux_str_free(line);
  112. // Stop-on-error
  113. if (opts->stop_on_error && (retcode != 0)) {
  114. stop = BOOL_TRUE;
  115. break;
  116. }
  117. }
  118. faux_file_close(fd);
  119. if (stop)
  120. break;
  121. }
  122. // Commands from non-interactive STDIN
  123. } else if (!isatty(STDIN_FILENO)) {
  124. char *line = NULL;
  125. faux_file_t *fd = faux_file_fdopen(STDIN_FILENO);
  126. while ((line = faux_file_getline(fd))) {
  127. // Request to server
  128. retcode = ktp_sync_cmd(ktp, line, opts);
  129. faux_str_free(line);
  130. // Stop-on-error
  131. if (opts->stop_on_error && (retcode != 0))
  132. break;
  133. }
  134. faux_file_close(fd);
  135. // Interactive shell
  136. } else {
  137. // Interactive code is complex so move it to separate file
  138. retcode = klish_interactive_shell(ktp, opts);
  139. }
  140. retval = 0;
  141. err:
  142. ktp_session_free(ktp);
  143. faux_eloop_free(eloop);
  144. ktp_disconnect(unix_sock);
  145. opts_free(opts);
  146. if ((retval < 0) || (retcode != 0))
  147. return -1;
  148. return 0;
  149. }
  150. static int ktp_sync_cmd(ktp_session_t *ktp, const char *line,
  151. const struct options *opts)
  152. {
  153. faux_error_t *error = NULL;
  154. int retcode = -1;
  155. if (faux_str_is_empty(line))
  156. return 0;
  157. // Echo command
  158. if (!opts->quiet)
  159. fprintf(stderr, "%s\n", line);
  160. error = faux_error_new();
  161. if (!ktp_session_cmd(ktp, line, error, opts->dry_run)) {
  162. faux_error_free(error);
  163. return -1;
  164. }
  165. faux_eloop_loop(ktp_session_eloop(ktp));
  166. // If retcode is not available then variable will not be changed
  167. ktp_session_retcode(ktp, &retcode);
  168. if (faux_error_len(error) > 0) {
  169. fprintf(stderr, "Error:\n");
  170. faux_error_fshow(error, stderr);
  171. }
  172. faux_error_free(error);
  173. return retcode;
  174. }
  175. static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
  176. void *user_data)
  177. {
  178. if (faux_write_block(STDOUT_FILENO, line, len) < 0)
  179. return BOOL_FALSE;
  180. ktp = ktp;
  181. user_data = user_data;
  182. return BOOL_TRUE;
  183. }
  184. static bool_t stderr_cb(ktp_session_t *ktp, const char *line, size_t len,
  185. void *user_data)
  186. {
  187. if (faux_write_block(STDERR_FILENO, line, len) < 0)
  188. return BOOL_FALSE;
  189. ktp = ktp;
  190. user_data = user_data;
  191. return BOOL_TRUE;
  192. }
  193. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  194. void *associated_data, void *user_data)
  195. {
  196. // Happy compiler
  197. eloop = eloop;
  198. type = type;
  199. associated_data = associated_data;
  200. user_data = user_data;
  201. return BOOL_FALSE; // Stop Event Loop
  202. }