Atlas - childprocess.c

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