lib/standard: Added errno and strerror bindings to stdlib.
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 7 Apr 2014 17:56:57 +0000 (13:56 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Mon, 7 Apr 2014 17:56:57 +0000 (13:56 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

examples/socket_server.nit
lib/socket/socket.nit
lib/socket/socket_c.nit
lib/standard/kernel.nit
lib/standard/string.nit
src/naive_interpreter.nit

index 574d0a2..9df9d79 100644 (file)
@@ -38,7 +38,7 @@ loop
        for c in clients do fs.readset.set(c)
 
        if fs.select(max, 4, 0) == 0 then
-               print "Error occured in select {socket.errno.to_s}"
+               print "Error occured in select {sys.errno.strerror}"
                break
        end
 
index b8f4acc..52094cc 100644 (file)
@@ -146,7 +146,6 @@ class Socket
                return new Socket.primitive_init(socket.accept)
        end
 
-       fun errno: Int do return socket.errno
 end
 
 class SocketSet
index d576332..11bda16 100644 (file)
@@ -28,7 +28,6 @@ in "C Header" `{
        #include <arpa/inet.h>
        #include <netdb.h>
        #include <sys/poll.h>
-       #include <errno.h>
 
        typedef int S_DESCRIPTOR;
        typedef struct sockaddr_in S_ADDR_IN;
@@ -116,7 +115,6 @@ extern FFSocket `{ S_DESCRIPTOR* `}
        fun destroy `{ free(recv); `}
        fun close: Int `{ return close( *recv ); `}
        fun descriptor: Int `{ return *recv; `}
-       fun errno: Int `{ return errno; `}
 
        fun gethostbyname(n: String): FFSocketHostent import String.to_cstring `{ return gethostbyname(String_to_cstring(n)); `}
        fun connect(addrIn: FFSocketAddrIn): Int `{ return connect( *recv, (S_ADDR*)addrIn, sizeof(*addrIn) ); `}
index 19da7f8..e2f8f1f 100644 (file)
@@ -17,6 +17,10 @@ module kernel
 
 import end # Mark this module is a top level one. (must be only one)
 
+`{
+#include <errno.h>
+`}
+
 ###############################################################################
 # System Classes                                                              #
 ###############################################################################
@@ -84,6 +88,11 @@ end
 class Sys
        # Instructions outside classes implicitly redefine this method.
        fun main do end
+
+       # Number of the last error
+       fun errno: Int is extern `{
+               return errno;
+       `}
 end
 
 ###############################################################################
index 29a25ff..2ba5772 100644 (file)
@@ -19,6 +19,7 @@ intrude import collection # FIXME should be collection::array
 
 `{
 #include <stdio.h>
+#include <string.h>
 `}
 
 ###############################################################################
@@ -1341,6 +1342,15 @@ redef class Bool
 end
 
 redef class Int
+
+       # Wrapper of strerror C function
+       private fun strerror_ext: NativeString is extern `{
+               return strerror(recv);
+       `}
+
+       # Returns a string describing error number
+       fun strerror: String do return strerror_ext.to_s
+
        # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative).
        # assume < to_c max const of char
        fun fill_buffer(s: Buffer, base: Int, signed: Bool)
index 4bd3267..7b42204 100644 (file)
@@ -869,6 +869,8 @@ redef class AExternMethPropdef
                                return v.int_instance(res)
                        else if pname == "native_int_to_s" then
                                return v.native_string_instance(recvval.to_s)
+                       else if pname == "strerror_ext" then
+                               return v.native_string_instance(recvval.strerror)
                        end
                else if cname == "NativeFile" then
                        var recvval = args.first.val
@@ -966,6 +968,8 @@ redef class AExternMethPropdef
                        return v.int_instance(parser_action(args[1].to_i, args[2].to_i))
                else if pname == "file_getcwd" then
                        return v.native_string_instance(getcwd)
+               else if pname == "errno" then
+                       return v.int_instance(sys.errno)
                end
                fatal(v, "NOT YET IMPLEMENTED extern {mpropdef}")
                abort