Mapping $? to text message

How do you go from $? (which refers to nil or a Process::Status
instance) to a text message, sorta like perror(3) does?

Apparently irb knows how to do it, or maybe it’s just connected to the
stderr of the sh process:

irb(main):001:0> system 'qwerty’
false
irb(main):002:0> p $?
32512
nil
irb(main):003:0> qwerty
(irb):3: command not found: qwerty
""

How do you go from $? (which refers to nil or a Process::Status
instance) to a text message, sorta like perror(3) does?

For me (running 1.6.8 under FreeBSD), $? is a Fixnum. It looks like it is
for you too:

irb(main):001:0> system ‘qwerty’
false
irb(main):002:0> p $?
32512

(try ‘p $?.class’ to be entirely sure)

Under Unix you have to split it into two parts:

 $? & 255    - 0 for normal exit, 127 for stopped, otherwise the signal
               number which caused it to die
 $? >> 8     - the exit status returned by the child (if it exited
               normally)

Under FreeBSD the signal number is also OR’d with 128 if core was dumped.

So in the above case that’s “normal exit, status 127” (returned by the
shell, which is a child process). For comparison, start a fresh session (so
$? is nil), do ‘exec “qwerty”’, and you’ll see $? is unchanged; it’s only
set when you wait(2) for a child.

Now, there are POSIX macros for all this: see ‘man 2 wait’ and look in
/usr/include/sys/wait.h

It would be really nice if this were all wrapped in an object. I raised this
a few weeks ago and didn’t hear anything, so I guess it’s not in 1.8.
Actually I’d volunteer to wrap the Unix macros myself; but I know nothing
about Windows (and I prefer to keep it that way).

It would be great to have:

p $?.exited? – true
p $?.exitstatus – 127

Note that none of this has anything to do with perror(3), which takes the
‘errno’ value set by certain system calls, and turns it into a string. Ruby
deals with ‘errno’ by raising exceptions, like Errno::ECONNREFUSED

Regards,

Brian.

···

On Sun, Mar 16, 2003 at 11:12:09AM +0900, Joel VanderWerf wrote:

Hi,

···

In message “mapping $? to text message” on 03/03/16, Joel VanderWerf vjoel@PATH.Berkeley.EDU writes:

How do you go from $? (which refers to nil or a Process::Status
instance) to a text message, sorta like perror(3) does?

IIRC, perror(3) prints message corresponding to errno, not return
status, right? If there’s a portable way to get text message from
return status, I’d be glad to add the feature.

						matz.

It would be great to have:
   p $?.exited? -- true
   p $?.exitstatus -- 127

pigeon% ruby -ve 'system("qw"); p $?.methods'
ruby 1.8.0 (2003-03-09) [i686-linux]
["stopsig", "==", "coredump?", "to_i", "signaled?", ">>", "termsig",
"inspect", "&", "to_int", "exited?", "pid", "to_s", "stopped?",
"exitstatus", "freeze", "nil?", "is_a?", "class", "instance_variable_set",
"methods", "extend", "__send__", "instance_eval", "instance_variables",
"=~", "instance_of?", "object_id", "copy_object", "id",
"protected_methods", "equal?", "respond_to?", "tainted?", "clone",
"public_methods", "untaint", "method", "type", "instance_variable_get",
"kind_of?", "frozen?", "taint", "send", "__id__", "private_methods",
"eql?", "to_a", "dup", "===", "display", "hash", "singleton_methods"]
pigeon%

Guy Decoux

It would be really nice if this were all wrapped in an object. I raised this
a few weeks ago and didn’t hear anything, so I guess it’s not in 1.8.

In 1.7 or later, Process.wait and related operations produce an object
of Process::Status class.
http://www.ruby-lang.org/ja/man-1.6/?cmd=view;name=Process%3A%3AStatus

On my 1.8.0:
class Process::Status
inherits: Object, Kernel
inherited by: (none)
class methods: (none)
instance methods: &, ==, >>, coredump?, exited?, exitstatus, inspect, pid,
signaled?, stopped?, stopsig, termsig, to_i, to_int, to_s

Unfortunately, Process::Status#to_s is equivalent to obj.to_i.to_s,
and does not produce descriptive string.

				       FUKUMOTO Atsushi
				       fukumoto@imasy.or.jp

In article 1047827648.000356.4358.nullmailer@picachu.netlab.jp,
matz@ruby-lang.org (Yukihiro Matsumoto) writes:

IIRC, perror(3) prints message corresponding to errno, not return
status, right? If there’s a portable way to get text message from
return status, I’d be glad to add the feature.

class Process::Status
def inspect
coredump = coredump? ? " (core dumped)" : “”
if exited?
“#<#{self.class}: exited: #{exitstatus}#{coredump}>”
elsif stopped?
“#<#{self.class}: stopped: #{Signal.list.invert[stopsig]}#{coredump}>”
elsif signaled?
“#<#{self.class}: signaled: #{Signal.list.invert[termsig]}#{coredump}>”
else
“#<#{self.class}: 0x#{to_i.to_s(16)}#{coredump}>”
end
end
end

···


Tanaka Akira

I had the need for the perror thing in the past, but could not make it
happen.

C:\ruby180>.\ruby -ve “system(‘qw’); p $?.methods;”
ruby 1.8.0 (2002-12-24) [i386-mswin32]
[“exited?”, “to_int”, “stopped?”, “==”, “exitstatus”, “stopsig”, “inspect”,
“>>”,
“&”, “coredump?”, “signaled?”, “pid”, “to_s”, “to_i”, “termsig”, “freeze”,
“nil?”,
“class”, “methods”, “method”, “instance_variables”, “is_a?”, “object_id”,
“=~”,
send”, “copy_object”, “instance_eval”, “extend”, “instance_of?”, “id”,
“protected_methods”, “equal?”, “tainted?”, “clone”, “public_methods”,
“untaint”,
“respond_to?”, “type”, “frozen?”, “taint”, “kind_of?”, “id”,
“private_methods”,
“eql?”, “to_a”, “display”, “dup”, “send”, “hash”, “singleton_methods”,
“===”]

I hope any steps to improve this situation is cross-platform.
Don’t forget us poor Windows programmers.
TIA,
– shanko

“ts” decoux@moulon.inra.fr wrote in message
news:200303161452.h2GEqFE11740@moulon.inra.fr

···

It would be great to have:
p $?.exited? – true
p $?.exitstatus – 127

pigeon% ruby -ve ‘system(“qw”); p $?.methods’
ruby 1.8.0 (2003-03-09) [i686-linux]
[“stopsig”, “==”, “coredump?”, “to_i”, “signaled?”, “>>”, “termsig”,
“inspect”, “&”, “to_int”, “exited?”, “pid”, “to_s”, “stopped?”,
“exitstatus”, “freeze”, “nil?”, “is_a?”, “class”, “instance_variable_set”,
“methods”, “extend”, “send”, “instance_eval”, “instance_variables”,
“=~”, “instance_of?”, “object_id”, “copy_object”, “id”,
“protected_methods”, “equal?”, “respond_to?”, “tainted?”, “clone”,
“public_methods”, “untaint”, “method”, “type”, “instance_variable_get”,
“kind_of?”, “frozen?”, “taint”, “send”, “id”, “private_methods”,
“eql?”, “to_a”, “dup”, “===”, “display”, “hash”, “singleton_methods”]
pigeon%

Guy Decoux