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