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

REPL shell mode for Windows #23703

Closed
wants to merge 8 commits into from
Closed
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
69 changes: 48 additions & 21 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,64 @@ answer_color() = text_colors[repl_color("JULIA_ANSWER_COLOR", default_color_answ
stackframe_lineinfo_color() = repl_color("JULIA_STACKFRAME_LINEINFO_COLOR", :bold)
stackframe_function_color() = repl_color("JULIA_STACKFRAME_FUNCTION_COLOR", :bold)

function repl_cmd(cmd, out)
shell = shell_split(get(ENV,"JULIA_SHELL",get(ENV,"SHELL","/bin/sh")))
shell_name = Base.basename(shell[1])
function repl_cmd(line::AbstractString, out)
if Sys.iswindows()
shell = shell_split(get(ENV, "JULIA_SHELL", "cmd"))
shell_name = isempty(shell) ? "" : lowercase(splitext(basename(shell[1]))[1])
else
shell = shell_split(get(ENV, "JULIA_SHELL", get(ENV,"SHELL","/bin/sh")))
shell_name = basename(shell[1])
end

if isempty(cmd.exec)
cmd = cmd_gen(eval(Main, shell_parse(line)[1]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there shouldn't be any calls to eval

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eliminated in the updated PR


if isempty(cmd)
throw(ArgumentError("no cmd to execute"))
elseif cmd.exec[1] == "cd"
new_oldpwd = pwd()
if length(cmd.exec) > 2
throw(ArgumentError("cd method only takes one argument"))
elseif length(cmd.exec) == 2
dir = cmd.exec[2]
if dir == "-"
if !haskey(ENV, "OLDPWD")
error("cd: OLDPWD not set")
end
cd(ENV["OLDPWD"])
elseif cmd[1] == "cd" && length(cmd) <= 2
repl_cd(cmd, shell, out)
else
local command::Cmd
if Sys.iswindows()
interpolated_line = eval(Main, parse(string('"', escape_string(line), '"')))::String
if shell_name == ""
command = cmd
elseif shell_name == "cmd"
command = Cmd(`$shell /s /c $(string('"', interpolated_line, '"'))`, windows_verbatim=true)
elseif shell_name == "powershell"
command = `$shell -Command $interpolated_line`
elseif shell_name == "busybox"
command = `$shell sh -c $interpolated_line`
else
cd(@static Sys.iswindows() ? dir : readchomp(`$shell -c "echo $(shell_escape(dir))"`))
command = `$shell $interpolated_line`
end
else
cd()
ttyopt = STDIN isa TTY ? `-i` : ``
command = `$shell $ttyopt -c "$(shell_wrap_true(shell_name, cmd))"`
end
ENV["OLDPWD"] = new_oldpwd
println(out, pwd())
else
run(ignorestatus(@static Sys.iswindows() ? cmd : (isa(STDIN, TTY) ? `$shell -i -c "$(shell_wrap_true(shell_name, cmd))"` : `$shell -c "$(shell_wrap_true(shell_name, cmd))"`)))
run(ignorestatus(command))
end
nothing
end

function repl_cd(cmd::Cmd, shell, out)
new_oldpwd = pwd()
if length(cmd) == 2
dir = cmd[2]
if dir == "-"
if !haskey(ENV, "OLDPWD")
error("cd: OLDPWD not set")
end
cd(ENV["OLDPWD"])
else
cd(@static Sys.iswindows() ? dir : readchomp(`$shell -c "echo $(shell_escape(dir))"`))
end
else
cd()
end
ENV["OLDPWD"] = new_oldpwd
println(out, pwd())
end

function shell_wrap_true(shell_name, cmd)
if shell_name == "fish"
"begin; $(shell_escape(cmd)); and true; end"
Expand Down
8 changes: 2 additions & 6 deletions base/repl/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,9 @@ function setup_interface(
(repl.envcolors ? Base.input_color : repl.input_color) : "",
repl = repl,
complete = ShellCompletionProvider(),
# Transform "foo bar baz" into `foo bar baz` (shell quoting)
# and pass into Base.repl_cmd for processing (handles `ls` and `cd`
# special)
# Pass entered line into Base.repl_cmd for processing (handles `cd` special)
on_done = respond(repl, julia_prompt) do line
Expr(:call, :(Base.repl_cmd),
:(Base.cmd_gen($(Base.shell_parse(line)[1]))),
outstream(repl))
Expr(:call, :(Base.repl_cmd), line, outstream(repl))
end)


Expand Down
7 changes: 3 additions & 4 deletions doc/src/manual/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ The absolute path of the shell with which Julia should execute external commands
(via `Base.repl_cmd()`). Defaults to the environment variable `$SHELL`, and
falls back to `/bin/sh` if `$SHELL` is unset.

!!! note

On Windows, this environment variable is ignored, and external commands are
executed directly.
On Windows, `$JULIA_SHELL` can be set to `cmd`, `powershell`, `busybox` or `""`.
If set to `""` external commands are executed directly. Defaults to `cmd` if
`$JULIA_SHELL` is not set.

### `JULIA_EDITOR`

Expand Down
1 change: 1 addition & 0 deletions doc/src/manual/interacting-with-julia.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ julia> ; # upon typing ;, the prompt changes (in place) to: shell>
shell> echo hello
hello
```
See [JULIA_SHELL](@ref) in the Environment Variables section of the manual.

### Search modes

Expand Down