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