childprocess.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <SDL3/SDL.h>
  2. #include <SDL3/SDL_main.h>
  3. #include <SDL3/SDL_test.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. int main(int argc, char *argv[]) {
  7. SDLTest_CommonState *state;
  8. int i;
  9. bool print_arguments = false;
  10. bool print_environment = false;
  11. bool stdin_to_stdout = false;
  12. bool read_stdin = false;
  13. bool stdin_to_stderr = false;
  14. SDL_IOStream *log_stdin = NULL;
  15. int exit_code = 0;
  16. state = SDLTest_CommonCreateState(argv, 0);
  17. for (i = 1; i < argc;) {
  18. int consumed = SDLTest_CommonArg(state, i);
  19. if (!consumed) {
  20. if (SDL_strcmp(argv[i], "--print-arguments") == 0) {
  21. print_arguments = true;
  22. consumed = 1;
  23. } else if (SDL_strcmp(argv[i], "--print-environment") == 0) {
  24. print_environment = true;
  25. consumed = 1;
  26. } else if (SDL_strcmp(argv[i], "--stdin-to-stdout") == 0) {
  27. stdin_to_stdout = true;
  28. consumed = 1;
  29. } else if (SDL_strcmp(argv[i], "--stdin-to-stderr") == 0) {
  30. stdin_to_stderr = true;
  31. consumed = 1;
  32. } else if (SDL_strcmp(argv[i], "--stdin") == 0) {
  33. read_stdin = true;
  34. consumed = 1;
  35. } else if (SDL_strcmp(argv[i], "--stdout") == 0) {
  36. if (i + 1 < argc) {
  37. fprintf(stdout, "%s", argv[i + 1]);
  38. consumed = 2;
  39. }
  40. } else if (SDL_strcmp(argv[i], "--stderr") == 0) {
  41. if (i + 1 < argc) {
  42. fprintf(stderr, "%s", argv[i + 1]);
  43. consumed = 2;
  44. }
  45. } else if (SDL_strcmp(argv[i], "--log-stdin") == 0) {
  46. if (i + 1 < argc) {
  47. log_stdin = SDL_IOFromFile(argv[i + 1], "w");
  48. if (!log_stdin) {
  49. fprintf(stderr, "Couldn't open %s\n", argv[i + 1]);
  50. return 2;
  51. }
  52. consumed = 2;
  53. }
  54. } else if (SDL_strcmp(argv[i], "--exit-code") == 0) {
  55. if (i + 1 < argc) {
  56. char *endptr = NULL;
  57. exit_code = SDL_strtol(argv[i + 1], &endptr, 0);
  58. if (endptr && *endptr == '\0') {
  59. consumed = 2;
  60. }
  61. }
  62. } else if (SDL_strcmp(argv[i], "--version") == 0) {
  63. int version = SDL_GetVersion();
  64. fprintf(stdout, "SDL version %d.%d.%d",
  65. SDL_VERSIONNUM_MAJOR(version),
  66. SDL_VERSIONNUM_MINOR(version),
  67. SDL_VERSIONNUM_MICRO(version));
  68. fprintf(stderr, "SDL version %d.%d.%d",
  69. SDL_VERSIONNUM_MAJOR(version),
  70. SDL_VERSIONNUM_MINOR(version),
  71. SDL_VERSIONNUM_MICRO(version));
  72. consumed = 1;
  73. break;
  74. } else if (SDL_strcmp(argv[i], "--") == 0) {
  75. i++;
  76. break;
  77. }
  78. }
  79. if (consumed <= 0) {
  80. const char *args[] = {
  81. "[--print-arguments]",
  82. "[--print-environment]",
  83. "[--stdin]",
  84. "[--log-stdin FILE]",
  85. "[--stdin-to-stdout]",
  86. "[--stdout TEXT]",
  87. "[--stdin-to-stderr]",
  88. "[--stderr TEXT]",
  89. "[--exit-code EXIT_CODE]",
  90. "[--] [ARG [ARG ...]]",
  91. NULL
  92. };
  93. SDLTest_CommonLogUsage(state, argv[0], args);
  94. return 1;
  95. }
  96. i += consumed;
  97. }
  98. if (print_arguments) {
  99. int print_i;
  100. for (print_i = 0; i + print_i < argc; print_i++) {
  101. fprintf(stdout, "|%d=%s|\r\n", print_i, argv[i + print_i]);
  102. }
  103. fflush(stdout);
  104. }
  105. if (print_environment) {
  106. char **env = SDL_GetEnvironmentVariables(SDL_GetEnvironment());
  107. if (env) {
  108. for (i = 0; env[i]; ++i) {
  109. fprintf(stdout, "%s\n", env[i]);
  110. }
  111. SDL_free(env);
  112. }
  113. fflush(stdout);
  114. }
  115. if (stdin_to_stdout || stdin_to_stderr || read_stdin) {
  116. for (;;) {
  117. char buffer[4 * 4096];
  118. size_t result;
  119. result = fread(buffer, 1, sizeof(buffer), stdin);
  120. if (result == 0) {
  121. if (!feof(stdin)) {
  122. char error[128];
  123. if (errno == EAGAIN) {
  124. clearerr(stdin);
  125. SDL_Delay(20);
  126. continue;
  127. }
  128. #ifdef SDL_PLATFORM_WINDOWS
  129. if (strerror_s(error, sizeof(error), errno) != 0) {
  130. SDL_strlcpy(error, "Unknown error", sizeof(error));
  131. }
  132. #else
  133. SDL_strlcpy(error, strerror(errno), sizeof(error));
  134. #endif
  135. SDL_Log("Error reading from stdin: %s", error);
  136. }
  137. break;
  138. }
  139. if (log_stdin) {
  140. SDL_WriteIO(log_stdin, buffer, result);
  141. SDL_FlushIO(log_stdin);
  142. }
  143. if (stdin_to_stdout) {
  144. fwrite(buffer, 1, result, stdout);
  145. fflush(stdout);
  146. }
  147. if (stdin_to_stderr) {
  148. fwrite(buffer, 1, result, stderr);
  149. }
  150. }
  151. }
  152. if (log_stdin) {
  153. SDL_CloseIO(log_stdin);
  154. }
  155. SDLTest_CommonDestroyState(state);
  156. return exit_code;
  157. }