childprocess.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. const char *expect_environment = NULL;
  10. SDL_bool expect_environment_match = SDL_FALSE;
  11. SDL_bool print_arguments = SDL_FALSE;
  12. SDL_bool print_environment = SDL_FALSE;
  13. SDL_bool stdin_to_stdout = SDL_FALSE;
  14. SDL_bool stdin_to_stderr = SDL_FALSE;
  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 (SDL_strcmp(argv[i], "--print-arguments") == 0) {
  20. print_arguments = SDL_TRUE;
  21. consumed = 1;
  22. } else if (SDL_strcmp(argv[i], "--print-environment") == 0) {
  23. print_environment = SDL_TRUE;
  24. consumed = 1;
  25. } else if (SDL_strcmp(argv[i], "--expect-env") == 0) {
  26. if (i + 1 < argc) {
  27. expect_environment = argv[i + 1];
  28. consumed = 2;
  29. }
  30. } else if (SDL_strcmp(argv[i], "--stdin-to-stdout") == 0) {
  31. stdin_to_stdout = SDL_TRUE;
  32. consumed = 1;
  33. } else if (SDL_strcmp(argv[i], "--stdin-to-stderr") == 0) {
  34. stdin_to_stderr = SDL_TRUE;
  35. consumed = 1;
  36. } else if (SDL_strcmp(argv[i], "--stdout") == 0) {
  37. if (i + 1 < argc) {
  38. fprintf(stdout, "%s", argv[i + 1]);
  39. consumed = 2;
  40. }
  41. } else if (SDL_strcmp(argv[i], "--stderr") == 0) {
  42. if (i + 1 < argc) {
  43. fprintf(stderr, "%s", argv[i + 1]);
  44. consumed = 2;
  45. }
  46. } else if (SDL_strcmp(argv[i], "--exit-code") == 0) {
  47. if (i + 1 < argc) {
  48. char *endptr = NULL;
  49. exit_code = SDL_strtol(argv[i + 1], &endptr, 0);
  50. if (endptr && *endptr == '\0') {
  51. consumed = 2;
  52. }
  53. }
  54. } else if (SDL_strcmp(argv[i], "--") == 0) {
  55. i++;
  56. break;
  57. }
  58. if (consumed <= 0) {
  59. const char *args[] = {
  60. "[--print-arguments]",
  61. "[--print-environment]",
  62. "[--expect-env KEY=VAL]",
  63. "[--stdin-to-stdout]",
  64. "[--stdout TEXT]",
  65. "[--stdin-to-stderr]",
  66. "[--stderr TEXT]",
  67. "[--exit-code EXIT_CODE]",
  68. "[--] [ARG [ARG ...]]",
  69. NULL
  70. };
  71. SDLTest_CommonLogUsage(state, argv[0], args);
  72. return 1;
  73. }
  74. i += consumed;
  75. }
  76. if (print_arguments) {
  77. int print_i;
  78. for (print_i = 0; i + print_i < argc; print_i++) {
  79. fprintf(stdout, "|%d=%s|\r\n", print_i, argv[i + print_i]);
  80. }
  81. }
  82. if (print_environment || expect_environment) {
  83. char **env = SDL_GetEnvironmentVariables(SDL_GetEnvironment());
  84. if (env) {
  85. for (i = 0; env[i]; ++i) {
  86. if (print_environment) {
  87. fprintf(stdout, "%s\n", env[i]);
  88. }
  89. if (expect_environment) {
  90. expect_environment_match |= SDL_strcmp(env[i], expect_environment) == 0;
  91. }
  92. }
  93. SDL_free(env);
  94. }
  95. }
  96. if (stdin_to_stdout || stdin_to_stderr) {
  97. for (;;) {
  98. int c;
  99. c = fgetc(stdin);
  100. if (c == EOF) {
  101. if (errno == EAGAIN) {
  102. clearerr(stdin);
  103. SDL_Delay(10);
  104. continue;
  105. }
  106. break;
  107. }
  108. if (stdin_to_stdout) {
  109. fputc(c, stdout);
  110. fflush(stdout);
  111. }
  112. if (stdin_to_stderr) {
  113. fputc(c, stderr);
  114. }
  115. }
  116. }
  117. SDLTest_CommonDestroyState(state);
  118. if (expect_environment && !expect_environment_match) {
  119. exit_code |= 0x1;
  120. }
  121. return exit_code;
  122. }