Skip to content

Commit

Permalink
implement @static macro for replacing osutils macros
Browse files Browse the repository at this point in the history
implements #5892
closes #6674 and #4233
  • Loading branch information
vtjnash committed May 5, 2016
1 parent 280310d commit d5cc901
Show file tree
Hide file tree
Showing 46 changed files with 552 additions and 415 deletions.
6 changes: 3 additions & 3 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function refresh_multi_line(termbuf::TerminalBuffer, terminal::UnixTerminal, buf
write_prompt(termbuf, prompt)
prompt = prompt_string(prompt)
# Count the '\n' at the end of the line if the terminal emulator does (specific to DOS cmd prompt)
miscountnl = @windows ? (isa(Terminals.pipe_reader(terminal), Base.TTY) && !Base.ispty(Terminals.pipe_reader(terminal))) : false
miscountnl = @static is_windows() ? (isa(Terminals.pipe_reader(terminal), Base.TTY) && !Base.ispty(Terminals.pipe_reader(terminal))) : false
lindent = strwidth(prompt)

# Now go through the buffer line by line
Expand Down Expand Up @@ -1564,7 +1564,7 @@ function run_interface(terminal, m::ModalInterface)
p = s.current_mode
buf, ok, suspend = prompt!(terminal, m, s)
while suspend
@unix_only ccall(:jl_repl_raise_sigtstp, Cint, ())
@static if is_unix(); ccall(:jl_repl_raise_sigtstp, Cint, ()); end
buf, ok, suspend = prompt!(terminal, m, s)
end
mode(state(s, s.current_mode)).on_done(s, buf, ok)
Expand Down Expand Up @@ -1604,7 +1604,7 @@ function prompt!(term, prompt, s = init_state(term, prompt))
elseif state == :done
return buffer(s), true, false
elseif state == :suspend
@unix_only begin
if is_unix()
return buffer(s), true, true
end
else
Expand Down
4 changes: 2 additions & 2 deletions base/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ function complete_path(path::AbstractString, pos; use_envpath=false)
if startswith(file, prefix)
id = try isdir(joinpath(dir, file)) catch; false end
# joinpath is not used because windows needs to complete with double-backslash
push!(matches, id ? file * (@windows? "\\\\" : "/") : file)
push!(matches, id ? file * (@static is_windows() ? "\\\\" : "/") : file)
end
end

if use_envpath && length(dir) == 0
# Look for files in PATH as well
local pathdirs = split(ENV["PATH"], @unix? ":" : ";")
local pathdirs = split(ENV["PATH"], @static is_windows() ? ";" : ":")

for pathdir in pathdirs
local actualpath
Expand Down
21 changes: 12 additions & 9 deletions base/Terminals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ cmove_line_up(t::UnixTerminal, n) = (cmove_up(t, n); cmove_col(t, 0))
cmove_line_down(t::UnixTerminal, n) = (cmove_down(t, n); cmove_col(t, 0))
cmove_col(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)G")

@windows ? begin
if is_windows()
function raw!(t::TTYTerminal,raw::Bool)
check_open(t.in_stream)
if Base.ispty(t.in_stream)
Expand All @@ -132,7 +132,7 @@ cmove_col(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)G")
t.in_stream.handle, raw) != -1
end
end
end : begin
else
function raw!(t::TTYTerminal, raw::Bool)
check_open(t.in_stream)
ccall(:jl_tty_set_mode, Int32, (Ptr{Void},Int32), t.in_stream.handle, raw) != -1
Expand All @@ -151,14 +151,17 @@ clear(t::UnixTerminal) = write(t.out_stream, "\x1b[H\x1b[2J")
clear_line(t::UnixTerminal) = write(t.out_stream, "\x1b[0G\x1b[0K")
#beep(t::UnixTerminal) = write(t.err_stream,"\x7")

@unix_only function hascolor(t::TTYTerminal)
startswith(t.term_type, "xterm") && return true
try
return success(`tput setaf 0`)
catch
return false
if is_windows()
hascolor(t::TTYTerminal) = true
else
function hascolor(t::TTYTerminal)
startswith(t.term_type, "xterm") && return true
try
return success(`tput setaf 0`)
catch
return false
end
end
end
@windows_only hascolor(t::TTYTerminal) = true

end # module
10 changes: 6 additions & 4 deletions base/c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ convert(::Type{Cstring}, s::Symbol) = Cstring(unsafe_convert(Ptr{Cchar}, s))
# in string.jl: unsafe_convert(::Type{Cwstring}, s::WString)

# FIXME: this should be handled by implicit conversion to Cwstring, but good luck with that
@windows_only function cwstring(s::AbstractString)
bytes = bytestring(s).data
0 in bytes && throw(ArgumentError("embedded NULs are not allowed in C strings: $(repr(s))"))
return push!(utf8to16(bytes), 0)
if is_windows()
function cwstring(s::AbstractString)
bytes = bytestring(s).data
0 in bytes && throw(ArgumentError("embedded NULs are not allowed in C strings: $(repr(s))"))
return push!(utf8to16(bytes), 0)
end
end

# conversions between UTF-8 and UTF-16 for Windows APIs
Expand Down
19 changes: 11 additions & 8 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ text_colors
have_color = false
default_color_warn = :red
default_color_info = :blue
@unix_only default_color_input = :bold
@unix_only default_color_answer = :bold
@windows_only default_color_input = :normal
@windows_only default_color_answer = :normal
if is_windows()
default_color_input = :normal
default_color_answer = :normal
else
default_color_input = :bold
default_color_answer = :bold
end
color_normal = text_colors[:normal]

function repl_color(key, default)
Expand Down Expand Up @@ -77,15 +80,15 @@ function repl_cmd(cmd, out)
end
cd(ENV["OLDPWD"])
else
cd(@windows? dir : readchomp(`$shell -c "echo $(shell_escape(dir))"`))
cd(@static is_windows() ? dir : readchomp(`$shell -c "echo $(shell_escape(dir))"`))
end
else
cd()
end
ENV["OLDPWD"] = new_oldpwd
println(out, pwd())
else
run(ignorestatus(@windows? cmd : (isa(STDIN, TTY) ? `$shell -i -c "($(shell_escape(cmd))) && true"` : `$shell -c "($(shell_escape(cmd))) && true"`)))
run(ignorestatus(@static is_windows() ? cmd : (isa(STDIN, TTY) ? `$shell -i -c "($(shell_escape(cmd))) && true"` : `$shell -c "($(shell_escape(cmd))) && true"`)))
end
nothing
end
Expand Down Expand Up @@ -323,10 +326,10 @@ function _start()
global active_repl_backend
if repl
if !isa(STDIN,TTY)
global is_interactive |= !isa(STDIN,Union{File,IOStream})
global is_interactive |= !isa(STDIN, Union{File, IOStream})
color_set || (global have_color = false)
else
term = Terminals.TTYTerminal(get(ENV,"TERM",@windows? "" : "dumb"),STDIN,STDOUT,STDERR)
term = Terminals.TTYTerminal(get(ENV, "TERM", @static is_windows() ? "" : "dumb"), STDIN, STDOUT, STDERR)
global is_interactive = true
color_set || (global have_color = Terminals.hascolor(term))
quiet || REPL.banner(term,term)
Expand Down
4 changes: 2 additions & 2 deletions base/dSFMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ end

## Windows entropy

@windows_only begin
if is_windows()
function win32_SystemFunction036!{T}(a::Array{T})
ccall((:SystemFunction036,:Advapi32),stdcall,UInt8,(Ptr{Void},UInt32),a,sizeof(a))
ccall((:SystemFunction036, :Advapi32), stdcall, UInt8, (Ptr{Void}, UInt32), a, sizeof(a))
end
end

Expand Down
2 changes: 1 addition & 1 deletion base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ readdlm(input, dlm::Char, T::Type, eol::Char; opts...) =

function readdlm_auto(input, dlm::Char, T::Type, eol::Char, auto::Bool; opts...)
optsd = val_opts(opts)
use_mmap = get(optsd, :use_mmap, @windows ? false : true)
use_mmap = get(optsd, :use_mmap, is_windows() ? false : true)
if isa(input, AbstractString)
fsz = filesize(input)
if use_mmap && fsz > 0 && fsz < typemax(Int)
Expand Down
11 changes: 7 additions & 4 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ function msync end
msync{T}(A::Array{T}) = msync(pointer(A), length(A)*sizeof(T))
msync(B::BitArray) = msync(pointer(B.chunks), length(B.chunks)*sizeof(UInt64))

@unix_only begin
if is_unix()
export mmap
@noinline function mmap(len::Integer, prot::Integer, flags::Integer, fd, offset::Integer)
depwarn("`mmap` is deprecated, use `Mmap.mmap(io, Array{T,N}, dims, offset)` instead to return an mmapped-array", :mmap)
Expand Down Expand Up @@ -619,7 +619,7 @@ end
end


@windows_only begin
if is_windows()
@noinline function munmap(viewhandle::Ptr, mmaphandle::Ptr)
depwarn("`munmap` is deprecated, `mmap` Arrays are automatically munmapped when finalized", :munmap)
status = ccall(:UnmapViewOfFile, stdcall, Cint, (Ptr{Void},), viewhandle)!=0
Expand All @@ -639,9 +639,11 @@ end

end

@unix_only @deprecate mmap_array{T,N}(::Type{T}, dims::NTuple{N,Integer}, s::IO, offset=position(s)) Mmap.mmap(s, Array{T,N}, dims, offset)
if is_unix()
@deprecate mmap_array{T,N}(::Type{T}, dims::NTuple{N,Integer}, s::IO, offset=position(s)) Mmap.mmap(s, Array{T,N}, dims, offset)
end

@windows_only begin
if is_windows()
type SharedMemSpec
name :: AbstractString
readonly :: Bool
Expand Down Expand Up @@ -1123,6 +1125,7 @@ end
@deprecate_binding UTF8String String
@deprecate_binding ByteString String


# During the 0.5 development cycle, do not add any deprecations below this line
# To be deprecated in 0.6

Expand Down
83 changes: 40 additions & 43 deletions base/env.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

@unix_only begin

_getenv(var::AbstractString) = ccall(:getenv, Cstring, (Cstring,), var)
_hasenv(s::AbstractString) = _getenv(s) != C_NULL

function access_env(onError::Function, var::AbstractString)
val = _getenv(var)
val == C_NULL ? onError(var) : bytestring(val)
end

function _setenv(var::AbstractString, val::AbstractString, overwrite::Bool=true)
ret = ccall(:setenv, Int32, (Cstring,Cstring,Int32), var, val, overwrite)
systemerror(:setenv, ret != 0)
end

function _unsetenv(var::AbstractString)
ret = ccall(:unsetenv, Int32, (Cstring,), var)
systemerror(:unsetenv, ret != 0)
end

end # @unix_only

@windows_only begin
if is_windows()

const ERROR_ENVVAR_NOT_FOUND = UInt32(203)

Expand Down Expand Up @@ -60,7 +38,26 @@ function _unsetenv(svar::AbstractString)
systemerror(:setenv, ret == 0)
end

end # @windows_only
else # !windows
_getenv(var::AbstractString) = ccall(:getenv, Cstring, (Cstring,), var)
_hasenv(s::AbstractString) = _getenv(s) != C_NULL

function access_env(onError::Function, var::AbstractString)
val = _getenv(var)
val == C_NULL ? onError(var) : bytestring(val)
end

function _setenv(var::AbstractString, val::AbstractString, overwrite::Bool=true)
ret = ccall(:setenv, Int32, (Cstring,Cstring,Int32), var, val, overwrite)
systemerror(:setenv, ret != 0)
end

function _unsetenv(var::AbstractString)
ret = ccall(:unsetenv, Int32, (Cstring,), var)
systemerror(:unsetenv, ret != 0)
end

end # os test

## ENV: hash interface ##

Expand All @@ -79,25 +76,7 @@ delete!(::EnvHash, k::AbstractString, def) = haskey(ENV,k) ? delete!(ENV,k) : de
setindex!(::EnvHash, v, k::AbstractString) = _setenv(k,string(v))
push!(::EnvHash, k::AbstractString, v) = setindex!(ENV, v, k)

@unix_only begin
start(::EnvHash) = 0
done(::EnvHash, i) = (ccall(:jl_environ, Any, (Int32,), i) === nothing)

function next(::EnvHash, i)
env = ccall(:jl_environ, Any, (Int32,), i)
if env === nothing
throw(BoundsError())
end
env::String
m = match(r"^(.*?)=(.*)$"s, env)
if m === nothing
error("malformed environment entry: $env")
end
(Pair{String,String}(m.captures[1], m.captures[2]), i+1)
end
end

@windows_only begin
if is_windows()
start(hash::EnvHash) = (pos = ccall(:GetEnvironmentStringsW,stdcall,Ptr{UInt16},()); (pos,pos))
function done(hash::EnvHash, block::Tuple{Ptr{UInt16},Ptr{UInt16}})
if unsafe_load(block[1]) == 0
Expand All @@ -119,8 +98,26 @@ function next(hash::EnvHash, block::Tuple{Ptr{UInt16},Ptr{UInt16}})
end
(Pair{String,String}(m.captures[1], m.captures[2]), (pos+len*2, blk))
end

else # !windows
start(::EnvHash) = 0
done(::EnvHash, i) = (ccall(:jl_environ, Any, (Int32,), i) === nothing)

function next(::EnvHash, i)
env = ccall(:jl_environ, Any, (Int32,), i)
if env === nothing
throw(BoundsError())
end
env::String
m = match(r"^(.*?)=(.*)$"s, env)
if m === nothing
error("malformed environment entry: $env")
end
(Pair{String,String}(m.captures[1], m.captures[2]), i+1)
end

end # os-test

#TODO: Make these more efficent
function length(::EnvHash)
i = 0
Expand Down
7 changes: 4 additions & 3 deletions base/event.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ function wait()
assert(false)
end

function pause()
@unix_only ccall(:pause, Void, ())
@windows_only ccall(:Sleep,stdcall, Void, (UInt32,), 0xffffffff)
if is_windows()
pause() = ccall(:Sleep, stdcall, Void, (UInt32,), 0xffffffff)
else
pause() = ccall(:pause, Void, ())
end


Expand Down
14 changes: 6 additions & 8 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1391,14 +1391,12 @@ export
@code_native,

# platform-conditional code
@windows,
@unix,
@osx,
@linux,
@windows_only,
@unix_only,
@osx_only,
@linux_only,
@static,
is_windows,
is_linux,
is_apple,
is_bsd,
is_unix,

# tasks
@schedule,
Expand Down
Loading

0 comments on commit d5cc901

Please sign in to comment.