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

Make Process::Status Windows-compatible #9021

Merged
merged 2 commits into from
Apr 12, 2020
Merged
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
59 changes: 43 additions & 16 deletions src/process/status.cr
Original file line number Diff line number Diff line change
@@ -1,38 +1,65 @@
# The status of a terminated process.
# The status of a terminated process. Returned by `Process#wait`.
class Process::Status
# Platform-specific exit status code, which usually contains either the exit code or a termination signal.
# The other `Process::Status` methods extract the values from `exit_status`.
getter exit_status : Int32

def initialize(@exit_status : Int32)
def exit_status : Int32
@exit_status.to_i32!
end

{% if flag?(:win32) %}
# :nodoc:
def initialize(@exit_status : UInt32)
end
{% else %}
# :nodoc:
def initialize(@exit_status : Int32)
end
{% end %}

# Returns `true` if the process was terminated by a signal.
def signal_exit?
# define __WIFSIGNALED(status) (((signed char) (((status) & 0x7f) + 1) >> 1) > 0)
((LibC::SChar.new(@exit_status & 0x7f) + 1) >> 1) > 0
def signal_exit? : Bool
{% if flag?(:unix) %}
# define __WIFSIGNALED(status) (((signed char) (((status) & 0x7f) + 1) >> 1) > 0)
((LibC::SChar.new(@exit_status & 0x7f) + 1) >> 1) > 0
{% else %}
false
{% end %}
end

# Returns `true` if the process terminated normally.
def normal_exit?
# define __WIFEXITED(status) (__WTERMSIG(status) == 0)
signal_code == 0
def normal_exit? : Bool
{% if flag?(:unix) %}
# define __WIFEXITED(status) (__WTERMSIG(status) == 0)
signal_code == 0
{% else %}
true
{% end %}
end

# If `signal_exit?` is `true`, returns the *Signal* the process
# received and didn't handle. Will raise if `signal_exit?` is `false`.
def exit_signal
Signal.from_value(signal_code)
#
# Available only on Unix-like operating systems.
def exit_signal : Signal
{% if flag?(:unix) %}
Signal.from_value(signal_code)
{% else %}
raise NotImplementedError.new("Process::Status#exit_signal")
{% end %}
end

# If `normal_exit?` is `true`, returns the exit code of the process.
def exit_code
# define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)
(@exit_status & 0xff00) >> 8
def exit_code : Int32
{% if flag?(:unix) %}
# define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)
(@exit_status & 0xff00) >> 8
{% else %}
exit_status
{% end %}
end

# Returns `true` if the process exited normally with an exit code of `0`.
def success?
def success? : Bool
normal_exit? && exit_code == 0
end

Expand Down