@@ -65,8 +65,9 @@ static void launch(const char *command, const char *file) {
6565 }
6666}
6767
68- void launchConsole (const char *file) {
68+ int launchConsole (const char *file) {
6969 launch (" sbasic" , file);
70+ return 0 ;
7071}
7172
7273void launchDebug (const char *file) {
@@ -99,10 +100,16 @@ void browseFile(SDL_Window *window, const char *url) {
99100}
100101
101102#else
102- #include < unistd.h>
103103#include < errno.h>
104+ #include < fcntl.h>
104105#include < limits.h>
105106#include < spawn.h>
107+ #include < sys/wait.h>
108+ #include < unistd.h>
109+
110+ #include < SDL3/SDL_events.h>
111+ #include < SDL3/SDL_timer.h>
112+
106113#include " icon.h"
107114#include " lib/lodepng/lodepng.h"
108115
@@ -140,15 +147,48 @@ static void launch(const char *command, const char *argument, const char *file)
140147 char *argv[] = {(char *)command, (char *)argument, (char *)file, nullptr };
141148 int status = posix_spawnp (&pid, command, nullptr , nullptr , argv, environ);
142149 if (status != 0 ) {
143- fprintf (stderr, " exec failed [%d] %s \n " , status, command );
150+ fprintf (stderr, " %s failed [%s] \n " , command, strerror (status) );
144151 }
145152}
146153
147154//
148155// invoke sbasic console build to run external GUI based modules
149156//
150- void launchConsole (const char *file) {
151- launch (" sbasic" , " " , file);
157+ int launchConsole (const char *file) {
158+ pid_t pid;
159+ char *argv[] = {(char *)" sbasic" , (char *)file, nullptr };
160+ int status = posix_spawnp (&pid, " sbasic" , nullptr , nullptr , argv, environ);
161+ if (status != 0 ) {
162+ fprintf (stderr, " sbasic failed [%s]\n " , strerror (status));
163+ return 1 ;
164+ }
165+
166+ bool running = true ;
167+ int result = 0 ;
168+ while (running) {
169+ SDL_Event event;
170+ while (SDL_PollEvent (&event)) {
171+ if (event.type == SDL_EVENT_QUIT) {
172+ kill (pid, SIGTERM);
173+ running = false ;
174+ break ;
175+ }
176+ }
177+ int wait_status;
178+ pid_t wait_result = waitpid (pid, &wait_status, WNOHANG);
179+ if (wait_result > 0 ) {
180+ running = false ;
181+ if (WIFEXITED (wait_status)) {
182+ // normal exit
183+ result = WEXITSTATUS (wait_status);
184+ } else if (WIFSIGNALED (wait_status)) {
185+ // crashed
186+ result = -1 ;
187+ }
188+ }
189+ SDL_Delay (16 );
190+ }
191+ return result;
152192}
153193
154194//
0 commit comments