Atlas - SDL_process.c

Home / ext / SDL / src / process Lines: 1 | Size: 5939 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#include "SDL_sysprocess.h" 24 25 26SDL_Process *SDL_CreateProcess(const char * const *args, bool pipe_stdio) 27{ 28 CHECK_PARAM(!args || !args[0] || !args[0][0]) { 29 SDL_InvalidParamError("args"); 30 return NULL; 31 } 32 33 SDL_Process *process; 34 SDL_PropertiesID props = SDL_CreateProperties(); 35 SDL_SetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, (void *)args); 36 if (pipe_stdio) { 37 SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDIN_NUMBER, SDL_PROCESS_STDIO_APP); 38 SDL_SetNumberProperty(props, SDL_PROP_PROCESS_CREATE_STDOUT_NUMBER, SDL_PROCESS_STDIO_APP); 39 } 40 process = SDL_CreateProcessWithProperties(props); 41 SDL_DestroyProperties(props); 42 return process; 43} 44 45SDL_Process *SDL_CreateProcessWithProperties(SDL_PropertiesID props) 46{ 47 const char * const *args = SDL_GetPointerProperty(props, SDL_PROP_PROCESS_CREATE_ARGS_POINTER, NULL); 48#if defined(SDL_PLATFORM_WINDOWS) 49 const char *cmdline = SDL_GetStringProperty(props, SDL_PROP_PROCESS_CREATE_CMDLINE_STRING, NULL); 50 CHECK_PARAM((!args || !args[0] || !args[0][0]) && (!cmdline || !cmdline[0])) { 51 SDL_SetError("Either SDL_PROP_PROCESS_CREATE_ARGS_POINTER or SDL_PROP_PROCESS_CREATE_CMDLINE_STRING must be valid"); 52 return NULL; 53 } 54#else 55 CHECK_PARAM(!args || !args[0] || !args[0][0]) { 56 SDL_InvalidParamError("SDL_PROP_PROCESS_CREATE_ARGS_POINTER"); 57 return NULL; 58 } 59#endif 60 61 SDL_Process *process = (SDL_Process *)SDL_calloc(1, sizeof(*process)); 62 if (!process) { 63 return NULL; 64 } 65 process->background = SDL_GetBooleanProperty(props, SDL_PROP_PROCESS_CREATE_BACKGROUND_BOOLEAN, false); 66 67 process->props = SDL_CreateProperties(); 68 if (!process->props) { 69 SDL_DestroyProcess(process); 70 return NULL; 71 } 72 SDL_SetBooleanProperty(process->props, SDL_PROP_PROCESS_BACKGROUND_BOOLEAN, process->background); 73 74 if (!SDL_SYS_CreateProcessWithProperties(process, props)) { 75 SDL_DestroyProcess(process); 76 return NULL; 77 } 78 process->alive = true; 79 return process; 80} 81 82SDL_PropertiesID SDL_GetProcessProperties(SDL_Process *process) 83{ 84 CHECK_PARAM(!process) { 85 return SDL_InvalidParamError("process"); 86 } 87 return process->props; 88} 89 90void *SDL_ReadProcess(SDL_Process *process, size_t *datasize, int *exitcode) 91{ 92 void *result; 93 94 if (datasize) { 95 *datasize = 0; 96 } 97 if (exitcode) { 98 *exitcode = -1; 99 } 100 101 CHECK_PARAM(!process) { 102 SDL_InvalidParamError("process"); 103 return NULL; 104 } 105 106 SDL_IOStream *io = (SDL_IOStream *)SDL_GetPointerProperty(process->props, SDL_PROP_PROCESS_STDOUT_POINTER, NULL); 107 if (!io) { 108 SDL_SetError("Process not created with I/O enabled"); 109 return NULL; 110 } 111 112 result = SDL_LoadFile_IO(io, datasize, false); 113 114 SDL_WaitProcess(process, true, exitcode); 115 116 return result; 117} 118 119SDL_IOStream *SDL_GetProcessInput(SDL_Process *process) 120{ 121 CHECK_PARAM(!process) { 122 SDL_InvalidParamError("process"); 123 return NULL; 124 } 125 126 SDL_IOStream *io = (SDL_IOStream *)SDL_GetPointerProperty(process->props, SDL_PROP_PROCESS_STDIN_POINTER, NULL); 127 if (!io) { 128 SDL_SetError("Process not created with standard input available"); 129 return NULL; 130 } 131 132 return io; 133} 134 135SDL_IOStream *SDL_GetProcessOutput(SDL_Process *process) 136{ 137 CHECK_PARAM(!process) { 138 SDL_InvalidParamError("process"); 139 return NULL; 140 } 141 142 SDL_IOStream *io = (SDL_IOStream *)SDL_GetPointerProperty(process->props, SDL_PROP_PROCESS_STDOUT_POINTER, NULL); 143 if (!io) { 144 SDL_SetError("Process not created with standard output available"); 145 return NULL; 146 } 147 148 return io; 149} 150 151bool SDL_KillProcess(SDL_Process *process, bool force) 152{ 153 CHECK_PARAM(!process) { 154 return SDL_InvalidParamError("process"); 155 } 156 157 if (!process->alive) { 158 return SDL_SetError("Process isn't running"); 159 } 160 161 return SDL_SYS_KillProcess(process, force); 162} 163 164bool SDL_WaitProcess(SDL_Process *process, bool block, int *exitcode) 165{ 166 CHECK_PARAM(!process) { 167 return SDL_InvalidParamError("process"); 168 } 169 170 if (!process->alive) { 171 if (exitcode) { 172 *exitcode = process->exitcode; 173 } 174 return true; 175 } 176 177 if (SDL_SYS_WaitProcess(process, block, &process->exitcode)) { 178 process->alive = false; 179 if (exitcode) { 180 if (process->background) { 181 process->exitcode = 0; 182 } 183 *exitcode = process->exitcode; 184 } 185 return true; 186 } 187 return false; 188} 189 190void SDL_DestroyProcess(SDL_Process *process) 191{ 192 if (!process) { 193 return; 194 } 195 196 // Check to see if the process has exited, will reap zombies on POSIX platforms 197 if (process->alive) { 198 SDL_WaitProcess(process, false, NULL); 199 } 200 201 SDL_SYS_DestroyProcess(process); 202 SDL_DestroyProperties(process->props); 203 SDL_free(process); 204} 205
[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.