Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace GAP's ExecuteProcess by Julia code #726

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -177,6 +177,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 @@ -314,6 +315,7 @@ include("utils.jl")
include("help.jl")
include("packages.jl")
include("prompt.jl")
include("exec.jl")
include("doctestfilters.jl")

end
38 changes: 38 additions & 0 deletions src/exec.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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})
# Note: the GAP kernel function `ExecuteProcess` also handles so-called
# "window mode", for use in xgap and Gap.app -- we do not emulate this here.
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(ignorestatus(`$prg $args`), stdin=fin, stdout=fout))
return res.exitcode == 255 ? GAP.Globals.Fail : res.exitcode
end
end