Skip to content

Commit

Permalink
Replace GAP's ExecuteProcess by Julia code
Browse files Browse the repository at this point in the history
This is a step towards getting rid of the GAP SIGCHLD handler
  • Loading branch information
fingolfin committed Oct 27, 2021
1 parent 998bedb commit 68d5847
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gap/exec.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BindGlobal("_ORIG_ExecuteProcess", ExecuteProcess);
MakeReadWriteGlobal("ExecuteProcess");
ExecuteProcess := Julia.GAP.GAP_ExecuteProcess;
MakeReadOnlyGlobal("ExecuteProcess");
2 changes: 2 additions & 0 deletions src/GAP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ function initialize(argv::Vector{String})
end

GAP.Globals.Read(GapObj(joinpath(@__DIR__, "..", "gap", "pkg.g")))
GAP.Globals.Read(GapObj(joinpath(@__DIR__, "..", "gap", "exec.g")))

# If we are in "stand-alone mode", stop here
if handle_signals
Expand Down Expand Up @@ -287,6 +288,7 @@ include("utils.jl")
include("help.jl")
include("packages.jl")
include("prompt.jl")
include("exec.jl")
include("doctestfilters.jl")

end
40 changes: 40 additions & 0 deletions src/exec.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Replacement for the GAP kernel function ExecuteProcess
const use_orig_ExecuteProcess = Ref{Bool}(false)
function GAP_ExecuteProcess(dir::GapObj, prg::GapObj, in::Int, out::Int, args::GapObj)
if use_orig_ExecuteProcess[]
return GAP.Globals._ORIG_ExecuteProcess(dir, prg, in, out, args)
end
return GAP_ExecuteProcess(String(dir), String(prg), in, out, Vector{String}(args))
end

function GAP_ExecuteProcess(dir::String, prg::String, fin::Int, fout::Int, args::Vector{String})
# TODO: the GAP kernel function `ExecuteProcess` also handles so-called
# "window mode" but this function currently does not

if fin < 0
fin = Base.devnull
else
fin = ccall((:SyBufFileno, libgap), Int, (Culong, ), fin)
if fin == -1
error("fin invalid")
end
fin = RawFD(fin)
end

if fout < 0
fout = Base.devnull
else
fout = ccall((:SyBufFileno, libgap), Int, (Culong, ), fout)
if fout == -1
error("fout invalid")
end
fout = RawFD(fout)
end


# TODO: verify `dir` is a valid dir?
cd(dir) do
res = run(pipeline(`$prg $args`, stdin=fin, stdout=fout))
return res == 255 ? GAP.Globals.Fail : res
end
end

0 comments on commit 68d5847

Please sign in to comment.