🐛 subprocess::terminate return correct value

This commit is contained in:
ValKmjolnir 2024-06-13 01:00:18 +08:00
parent 40ca6c19bf
commit 9752b8823e
2 changed files with 14 additions and 10 deletions

View File

@ -158,7 +158,11 @@ var builtin_subprocess_terminate(context* ctx, gc* ngc) {
auto pi = &static_cast<subprocess_descriptor*>(obj.ghost().pointer)->pi; auto pi = &static_cast<subprocess_descriptor*>(obj.ghost().pointer)->pi;
WaitForSingleObject(pi->hProcess, 0); WaitForSingleObject(pi->hProcess, 0);
TerminateProcess(pi->hProcess, 0); TerminateProcess(pi->hProcess, -1);
DWORD res;
GetExitCodeProcess(pi->hProcess, &res);
CloseHandle(pi->hProcess); CloseHandle(pi->hProcess);
CloseHandle(pi->hThread); CloseHandle(pi->hThread);
#else #else
@ -171,9 +175,11 @@ var builtin_subprocess_terminate(context* ctx, gc* ngc) {
if (result==0) { if (result==0) {
kill(pid, SIGTERM); kill(pid, SIGTERM);
} }
auto res = WIFEXITED(status)? WEXITSTATUS(status):-1;
#endif #endif
return nil; return var::num(res);
} }
nasal_builtin_table subprocess_native[] = { nasal_builtin_table subprocess_native[] = {

View File

@ -70,7 +70,7 @@ if (size(argv)==2) {
var modified_time = io.fstat(filename).st_mtime; var modified_time = io.fstat(filename).st_mtime;
println(os_time(), info_hd(), "\e[1mwatching ", filename, " ..\e[0m"); println(os_time(), info_hd(), "\e[1mwatching ", filename, " ..\e[0m");
while(1) { while(1) {
unix.sleep(5); unix.sleep(1);
if (!io.exists(filename)) { if (!io.exists(filename)) {
println( println(
os_time(), os_time(),
@ -91,7 +91,6 @@ while(1) {
foreach(var i; args) { foreach(var i; args) {
cmd ~= " " ~ i; cmd ~= " " ~ i;
} }
cmd ~= " 2>&1";
println( println(
os_time(), os_time(),
info_hd(), info_hd(),
@ -100,12 +99,11 @@ while(1) {
"\"\e[0m" "\"\e[0m"
); );
var subproc = subprocess.popen(cmd); var subproc = subprocess.create(["nasal", filename]~args);
var stdout_info = subprocess.read(subproc);
var ret = subprocess.pclose(subproc); unix.sleep(2);
if (size(stdout_info)>0) { var ret = subprocess.terminate(subproc);
println(stdout_info);
}
println( println(
os_time(), os_time(),
ret!=0? err_hd():info_hd(), ret!=0? err_hd():info_hd(),