childprocess.c 4.9 KB

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