klish.c 3.7 KB

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