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 e0e93fc commit f342c7d
Show file tree
Hide file tree
Showing 57 changed files with 824 additions and 666 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
53 changes: 49 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,49 @@ end
@deprecate_binding UTF8String String
@deprecate_binding ByteString String

#6674 and #4233
macro windows(qm,ex)
depwarn("`@windows` is deprecated, use `@static is_windows()` instead", Symbol("@windows"))
return @static is_windows() ? esc(ex.args[1]) : esc(ex.args[2])
end
macro unix(qm,ex)
depwarn("`@unix` is deprecated, use `@static is_unix()` instead", Symbol("@unix"))
return @static is_unix() ? esc(ex.args[1]) : esc(ex.args[2])
end
macro osx(qm,ex)
depwarn("`@osx` is deprecated, use `@static is_apple()` instead", Symbol("@osx"))
return @static is_apple() ? esc(ex.args[1]) : esc(ex.args[2])
end
macro linux(qm,ex)
depwarn("`@linux` is deprecated, use `@static is_linux()` instead", Symbol("@linux"))
return @static is_linux() ? esc(ex.args[1]) : esc(ex.args[2])
end
macro windows_only(ex)
depwarn("`@windows_only` is deprecated, use `@static is_windows()` instead", Symbol("@windows_only"))
return @static if is_windows() esc(ex) end
end
macro unix_only(ex)
depwarn("`@unix_only` is deprecated, use `@static is_unix()` instead", Symbol("@unix_only"))
return @static if is_unix() esc(ex) end
end
macro osx_only(ex)
depwarn("`@osx_only` is deprecated, use `@static is_apple()` instead", Symbol("@osx_only"))
return @static if is_apple() esc(ex) end
end
macro linux_only(ex)
depwarn("`@linux_only` is deprecated, use `@static is_linux()` instead", Symbol("@linux_only"))
return @static if is_linux() esc(ex) end
end
export
@windows,
@unix,
@osx,
@linux,
@windows_only,
@unix_only,
@osx_only,
@linux_only

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

Expand Down
58 changes: 0 additions & 58 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2055,36 +2055,6 @@ of a string.
"""
isdigit

"""
@windows
Given `@windows? a : b`, do `a` on Windows and `b` elsewhere. See documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
:@windows

"""
@unix
Given `@unix? a : b`, do `a` on Unix systems (including Linux and OS X) and `b` elsewhere.
See documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
:@unix

"""
@windows_only
A macro that evaluates the given expression only on Windows systems. See documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
:@windows_only

"""
@unix_only
A macro that evaluates the given expression only on Unix systems (including Linux and OS X). See
documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
:@unix_only

"""
num2hex(f)
Expand Down Expand Up @@ -6407,20 +6377,6 @@ Returns the number of dimensions of `A`.
"""
ndims

"""
@osx
Given `@osx? a : b`, do `a` on OS X and `b` elsewhere. See documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
:@osx

"""
@osx_only
A macro that evaluates the given expression only on OS X systems. See documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
:@osx_only

"""
ishermitian(A) -> Bool
Expand Down Expand Up @@ -8063,20 +8019,6 @@ Cumulative product of `A` along a dimension, storing the result in `B`. The dime
"""
cumprod!

"""
@linux
Given `@linux? a : b`, do `a` on Linux and `b` elsewhere. See documentation [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
:@linux

"""
@linux_only
A macro that evaluates the given expression only on Linux systems. See documentation in [Handling Operating System Variation](:ref:`Handling Operating System Variation <man-handling-operating-system-variation>`).
"""
:@linux_only

"""
complement(s)
Expand Down
Loading

0 comments on commit f342c7d

Please sign in to comment.