]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: return error, not uintptr, when function returns error
authorAlex Brainman <alex.brainman@gmail.com>
Thu, 8 Dec 2011 01:07:21 +0000 (12:07 +1100)
committerAlex Brainman <alex.brainman@gmail.com>
Thu, 8 Dec 2011 01:07:21 +0000 (12:07 +1100)
R=rsc
CC=golang-dev
https://golang.org/cl/5450119

12 files changed:
src/pkg/exp/wingui/gui.go
src/pkg/exp/wingui/winapi.go
src/pkg/exp/wingui/zwinapi.go
src/pkg/mime/type_windows.go
src/pkg/net/fd_windows.go
src/pkg/net/interface_windows.go
src/pkg/net/lookup_windows.go
src/pkg/syscall/dll_windows.go
src/pkg/syscall/mksyscall_windows.pl
src/pkg/syscall/syscall_windows.go
src/pkg/syscall/zsyscall_windows_386.go
src/pkg/syscall/zsyscall_windows_amd64.go

index 5df2ee0faa141bae202604978429a2b6d61eab09..d58421bcfa740665255de8113261bfdf310d331f 100644 (file)
@@ -18,8 +18,9 @@ func abortf(format string, a ...interface{}) {
        os.Exit(1)
 }
 
-func abortErrNo(funcname string, err int) {
-       abortf("%s failed: %d %s\n", funcname, err, syscall.Errstr(err))
+func abortErrNo(funcname string, err error) {
+       errno, _ := err.(syscall.Errno)
+       abortf("%s failed: %d %s\n", funcname, uint32(errno), err)
 }
 
 // global vars
@@ -33,7 +34,7 @@ var (
 func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintptr) {
        switch msg {
        case WM_CREATE:
-               var e int
+               var e error
                // CreateWindowEx
                bh, e = CreateWindowEx(
                        0,
@@ -42,7 +43,7 @@ func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintpt
                        WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
                        75, 70, 140, 25,
                        hwnd, 1, mh, 0)
-               if e != 0 {
+               if e != nil {
                        abortErrNo("CreateWindowEx", e)
                }
                fmt.Printf("button handle is %x\n", bh)
@@ -51,7 +52,7 @@ func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintpt
                switch syscall.Handle(lparam) {
                case bh:
                        e := PostMessage(hwnd, WM_CLOSE, 0, 0)
-                       if e != 0 {
+                       if e != nil {
                                abortErrNo("PostMessage", e)
                        }
                default:
@@ -69,23 +70,23 @@ func WndProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintpt
 }
 
 func rungui() int {
-       var e int
+       var e error
 
        // GetModuleHandle
        mh, e = GetModuleHandle(nil)
-       if e != 0 {
+       if e != nil {
                abortErrNo("GetModuleHandle", e)
        }
 
        // Get icon we're going to use.
        myicon, e := LoadIcon(0, IDI_APPLICATION)
-       if e != 0 {
+       if e != nil {
                abortErrNo("LoadIcon", e)
        }
 
        // Get cursor we're going to use.
        mycursor, e := LoadCursor(0, IDC_ARROW)
-       if e != 0 {
+       if e != nil {
                abortErrNo("LoadCursor", e)
        }
 
@@ -104,7 +105,7 @@ func rungui() int {
        wc.MenuName = nil
        wc.ClassName = wcname
        wc.IconSm = myicon
-       if _, e := RegisterClassEx(&wc); e != 0 {
+       if _, e := RegisterClassEx(&wc); e != nil {
                abortErrNo("RegisterClassEx", e)
        }
 
@@ -116,7 +117,7 @@ func rungui() int {
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
                0, 0, mh, 0)
-       if e != 0 {
+       if e != nil {
                abortErrNo("CreateWindowEx", e)
        }
        fmt.Printf("main window handle is %x\n", wh)
@@ -125,7 +126,7 @@ func rungui() int {
        ShowWindow(wh, SW_SHOWDEFAULT)
 
        // UpdateWindow
-       if e := UpdateWindow(wh); e != 0 {
+       if e := UpdateWindow(wh); e != nil {
                abortErrNo("UpdateWindow", e)
        }
 
@@ -133,7 +134,7 @@ func rungui() int {
        var m Msg
        for {
                r, e := GetMessage(&m, 0, 0, 0)
-               if e != 0 {
+               if e != nil {
                        abortErrNo("GetMessage", e)
                }
                if r == 0 {
index 08059df2b9d9d428259754ed95aee7ae3b57d73f..24f3dd4d723e783d58d13fffd67d8c916b825d6c 100644 (file)
@@ -110,22 +110,22 @@ var (
        IDI_INFORMATION = IDI_ASTERISK
 )
 
-//sys  GetModuleHandle(modname *uint16) (handle syscall.Handle, errno int) = GetModuleHandleW
-//sys  RegisterClassEx(wndclass *Wndclassex) (atom uint16, errno int) = user32.RegisterClassExW
-//sys  CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent syscall.Handle, menu syscall.Handle, instance syscall.Handle, param uintptr) (hwnd syscall.Handle, errno int) = user32.CreateWindowExW
+//sys  GetModuleHandle(modname *uint16) (handle syscall.Handle, err error) = GetModuleHandleW
+//sys  RegisterClassEx(wndclass *Wndclassex) (atom uint16, err error) = user32.RegisterClassExW
+//sys  CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent syscall.Handle, menu syscall.Handle, instance syscall.Handle, param uintptr) (hwnd syscall.Handle, err error) = user32.CreateWindowExW
 //sys  DefWindowProc(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (lresult uintptr) = user32.DefWindowProcW
-//sys  DestroyWindow(hwnd syscall.Handle) (errno int) = user32.DestroyWindow
+//sys  DestroyWindow(hwnd syscall.Handle) (err error) = user32.DestroyWindow
 //sys  PostQuitMessage(exitcode int32) = user32.PostQuitMessage
 //sys  ShowWindow(hwnd syscall.Handle, cmdshow int32) (wasvisible bool) = user32.ShowWindow
-//sys  UpdateWindow(hwnd syscall.Handle) (errno int) = user32.UpdateWindow
-//sys  GetMessage(msg *Msg, hwnd syscall.Handle, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, errno int) [failretval==-1] = user32.GetMessageW
+//sys  UpdateWindow(hwnd syscall.Handle) (err error) = user32.UpdateWindow
+//sys  GetMessage(msg *Msg, hwnd syscall.Handle, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, err error) [failretval==-1] = user32.GetMessageW
 //sys  TranslateMessage(msg *Msg) (done bool) = user32.TranslateMessage
 //sys  DispatchMessage(msg *Msg) (ret int32) = user32.DispatchMessageW
-//sys  LoadIcon(instance syscall.Handle, iconname *uint16) (icon syscall.Handle, errno int) = user32.LoadIconW
-//sys  LoadCursor(instance syscall.Handle, cursorname *uint16) (cursor syscall.Handle, errno int) = user32.LoadCursorW
-//sys  SetCursor(cursor syscall.Handle) (precursor syscall.Handle, errno int) = user32.SetCursor
+//sys  LoadIcon(instance syscall.Handle, iconname *uint16) (icon syscall.Handle, err error) = user32.LoadIconW
+//sys  LoadCursor(instance syscall.Handle, cursorname *uint16) (cursor syscall.Handle, err error) = user32.LoadCursorW
+//sys  SetCursor(cursor syscall.Handle) (precursor syscall.Handle, err error) = user32.SetCursor
 //sys  SendMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (lresult uintptr) = user32.SendMessageW
-//sys  PostMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (errno int) = user32.PostMessageW
+//sys  PostMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (err error) = user32.PostMessageW
 
 func MakeIntResource(id uint16) *uint16 {
        return (*uint16)(unsafe.Pointer(uintptr(id)))
index 38e93eea717a7e07926656e1e5169329760aa163..b062ca3372e1bdca2dd19bcd8c3d918970c757fb 100644 (file)
@@ -28,47 +28,41 @@ var (
        procPostMessageW     = moduser32.NewProc("PostMessageW")
 )
 
-func GetModuleHandle(modname *uint16) (handle syscall.Handle, errno int) {
+func GetModuleHandle(modname *uint16) (handle syscall.Handle, err error) {
        r0, _, e1 := syscall.Syscall(procGetModuleHandleW.Addr(), 1, uintptr(unsafe.Pointer(modname)), 0, 0)
        handle = syscall.Handle(r0)
        if handle == 0 {
                if e1 != 0 {
-                       errno = int(e1)
+                       err = error(e1)
                } else {
-                       errno = syscall.EINVAL
+                       err = syscall.EINVAL
                }
-       } else {
-               errno = 0
        }
        return
 }
 
-func RegisterClassEx(wndclass *Wndclassex) (atom uint16, errno int) {
+func RegisterClassEx(wndclass *Wndclassex) (atom uint16, err error) {
        r0, _, e1 := syscall.Syscall(procRegisterClassExW.Addr(), 1, uintptr(unsafe.Pointer(wndclass)), 0, 0)
        atom = uint16(r0)
        if atom == 0 {
                if e1 != 0 {
-                       errno = int(e1)
+                       err = error(e1)
                } else {
-                       errno = syscall.EINVAL
+                       err = syscall.EINVAL
                }
-       } else {
-               errno = 0
        }
        return
 }
 
-func CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent syscall.Handle, menu syscall.Handle, instance syscall.Handle, param uintptr) (hwnd syscall.Handle, errno int) {
+func CreateWindowEx(exstyle uint32, classname *uint16, windowname *uint16, style uint32, x int32, y int32, width int32, height int32, wndparent syscall.Handle, menu syscall.Handle, instance syscall.Handle, param uintptr) (hwnd syscall.Handle, err error) {
        r0, _, e1 := syscall.Syscall12(procCreateWindowExW.Addr(), 12, uintptr(exstyle), uintptr(unsafe.Pointer(classname)), uintptr(unsafe.Pointer(windowname)), uintptr(style), uintptr(x), uintptr(y), uintptr(width), uintptr(height), uintptr(wndparent), uintptr(menu), uintptr(instance), uintptr(param))
        hwnd = syscall.Handle(r0)
        if hwnd == 0 {
                if e1 != 0 {
-                       errno = int(e1)
+                       err = error(e1)
                } else {
-                       errno = syscall.EINVAL
+                       err = syscall.EINVAL
                }
-       } else {
-               errno = 0
        }
        return
 }
@@ -79,16 +73,14 @@ func DefWindowProc(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintp
        return
 }
 
-func DestroyWindow(hwnd syscall.Handle) (errno int) {
+func DestroyWindow(hwnd syscall.Handle) (err error) {
        r1, _, e1 := syscall.Syscall(procDestroyWindow.Addr(), 1, uintptr(hwnd), 0, 0)
        if int(r1) == 0 {
                if e1 != 0 {
-                       errno = int(e1)
+                       err = error(e1)
                } else {
-                       errno = syscall.EINVAL
+                       err = syscall.EINVAL
                }
-       } else {
-               errno = 0
        }
        return
 }
@@ -104,31 +96,27 @@ func ShowWindow(hwnd syscall.Handle, cmdshow int32) (wasvisible bool) {
        return
 }
 
-func UpdateWindow(hwnd syscall.Handle) (errno int) {
+func UpdateWindow(hwnd syscall.Handle) (err error) {
        r1, _, e1 := syscall.Syscall(procUpdateWindow.Addr(), 1, uintptr(hwnd), 0, 0)
        if int(r1) == 0 {
                if e1 != 0 {
-                       errno = int(e1)
+                       err = error(e1)
                } else {
-                       errno = syscall.EINVAL
+                       err = syscall.EINVAL
                }
-       } else {
-               errno = 0
        }
        return
 }
 
-func GetMessage(msg *Msg, hwnd syscall.Handle, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, errno int) {
+func GetMessage(msg *Msg, hwnd syscall.Handle, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, err error) {
        r0, _, e1 := syscall.Syscall6(procGetMessageW.Addr(), 4, uintptr(unsafe.Pointer(msg)), uintptr(hwnd), uintptr(MsgFilterMin), uintptr(MsgFilterMax), 0, 0)
        ret = int32(r0)
        if ret == -1 {
                if e1 != 0 {
-                       errno = int(e1)
+                       err = error(e1)
                } else {
-                       errno = syscall.EINVAL
+                       err = syscall.EINVAL
                }
-       } else {
-               errno = 0
        }
        return
 }
@@ -145,47 +133,41 @@ func DispatchMessage(msg *Msg) (ret int32) {
        return
 }
 
-func LoadIcon(instance syscall.Handle, iconname *uint16) (icon syscall.Handle, errno int) {
+func LoadIcon(instance syscall.Handle, iconname *uint16) (icon syscall.Handle, err error) {
        r0, _, e1 := syscall.Syscall(procLoadIconW.Addr(), 2, uintptr(instance), uintptr(unsafe.Pointer(iconname)), 0)
        icon = syscall.Handle(r0)
        if icon == 0 {
                if e1 != 0 {
-                       errno = int(e1)
+                       err = error(e1)
                } else {
-                       errno = syscall.EINVAL
+                       err = syscall.EINVAL
                }
-       } else {
-               errno = 0
        }
        return
 }
 
-func LoadCursor(instance syscall.Handle, cursorname *uint16) (cursor syscall.Handle, errno int) {
+func LoadCursor(instance syscall.Handle, cursorname *uint16) (cursor syscall.Handle, err error) {
        r0, _, e1 := syscall.Syscall(procLoadCursorW.Addr(), 2, uintptr(instance), uintptr(unsafe.Pointer(cursorname)), 0)
        cursor = syscall.Handle(r0)
        if cursor == 0 {
                if e1 != 0 {
-                       errno = int(e1)
+                       err = error(e1)
                } else {
-                       errno = syscall.EINVAL
+                       err = syscall.EINVAL
                }
-       } else {
-               errno = 0
        }
        return
 }
 
-func SetCursor(cursor syscall.Handle) (precursor syscall.Handle, errno int) {
+func SetCursor(cursor syscall.Handle) (precursor syscall.Handle, err error) {
        r0, _, e1 := syscall.Syscall(procSetCursor.Addr(), 1, uintptr(cursor), 0, 0)
        precursor = syscall.Handle(r0)
        if precursor == 0 {
                if e1 != 0 {
-                       errno = int(e1)
+                       err = error(e1)
                } else {
-                       errno = syscall.EINVAL
+                       err = syscall.EINVAL
                }
-       } else {
-               errno = 0
        }
        return
 }
@@ -196,16 +178,14 @@ func SendMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr
        return
 }
 
-func PostMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (errno int) {
+func PostMessage(hwnd syscall.Handle, msg uint32, wparam uintptr, lparam uintptr) (err error) {
        r1, _, e1 := syscall.Syscall6(procPostMessageW.Addr(), 4, uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam), 0, 0)
        if int(r1) == 0 {
                if e1 != 0 {
-                       errno = int(e1)
+                       err = error(e1)
                } else {
-                       errno = syscall.EINVAL
+                       err = syscall.EINVAL
                }
-       } else {
-               errno = 0
        }
        return
 }
index 7cf2d3984b1e193a8fd52d2c3aab1e75ca768eb7..bc388893b48ab799583d988fb4dc365f817d2d0c 100644 (file)
@@ -12,18 +12,18 @@ import (
 func initMime() {
        var root syscall.Handle
        if syscall.RegOpenKeyEx(syscall.HKEY_CLASSES_ROOT, syscall.StringToUTF16Ptr(`\`),
-               0, syscall.KEY_READ, &root) != 0 {
+               0, syscall.KEY_READ, &root) != nil {
                return
        }
        defer syscall.RegCloseKey(root)
        var count uint32
-       if syscall.RegQueryInfoKey(root, nil, nil, nil, &count, nil, nil, nil, nil, nil, nil, nil) != 0 {
+       if syscall.RegQueryInfoKey(root, nil, nil, nil, &count, nil, nil, nil, nil, nil, nil, nil) != nil {
                return
        }
        var buf [1 << 10]uint16
        for i := uint32(0); i < count; i++ {
                n := uint32(len(buf))
-               if syscall.RegEnumKeyEx(root, i, &buf[0], &n, nil, nil, nil, nil) != 0 {
+               if syscall.RegEnumKeyEx(root, i, &buf[0], &n, nil, nil, nil, nil) != nil {
                        continue
                }
                ext := syscall.UTF16ToString(buf[:])
@@ -33,14 +33,14 @@ func initMime() {
                var h syscall.Handle
                if syscall.RegOpenKeyEx(
                        syscall.HKEY_CLASSES_ROOT, syscall.StringToUTF16Ptr(`\`+ext),
-                       0, syscall.KEY_READ, &h) != 0 {
+                       0, syscall.KEY_READ, &h) != nil {
                        continue
                }
                var typ uint32
                n = uint32(len(buf) * 2) // api expects array of bytes, not uint16
                if syscall.RegQueryValueEx(
                        h, syscall.StringToUTF16Ptr("Content Type"),
-                       nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != 0 {
+                       nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != nil {
                        syscall.RegCloseKey(h)
                        continue
                }
index 264b918c57dc147a2ad4f2cdb493d068aa3ad2ec..7bffd1ca2fb9751061d54c81cc15fce38362ecc3 100644 (file)
@@ -25,8 +25,8 @@ var initErr error
 func init() {
        var d syscall.WSAData
        e := syscall.WSAStartup(uint32(0x202), &d)
-       if e != 0 {
-               initErr = os.NewSyscallError("WSAStartup", syscall.Errno(e))
+       if e != nil {
+               initErr = os.NewSyscallError("WSAStartup", e)
        }
 }
 
index 2ed66cdce377a58e489f4cf4a5ded52419e2cfa9..add3dd3b9d9216fa510456cd1dff607219856f1f 100644 (file)
@@ -31,7 +31,7 @@ func getAdapterList() (*syscall.IpAdapterInfo, error) {
                a = (*syscall.IpAdapterInfo)(unsafe.Pointer(&b[0]))
                e = syscall.GetAdaptersInfo(a, &l)
        }
-       if e != 0 {
+       if e != nil {
                return nil, os.NewSyscallError("GetAdaptersInfo", e)
        }
        return a, nil
@@ -77,7 +77,7 @@ func interfaceTable(ifindex int) ([]Interface, error) {
 
                        row := syscall.MibIfRow{Index: index}
                        e := syscall.GetIfEntry(&row)
-                       if e != 0 {
+                       if e != nil {
                                return nil, os.NewSyscallError("GetIfEntry", e)
                        }
 
index 020871b46d6c88fd98f79b14ddf17e10e2f169af..51afbd4bb85a2dba27423f351c455f20d288b67f 100644 (file)
@@ -80,7 +80,7 @@ func LookupPort(network, service string) (port int, err error) {
 func LookupCNAME(name string) (cname string, err error) {
        var r *syscall.DNSRecord
        e := syscall.DnsQuery(name, syscall.DNS_TYPE_CNAME, 0, nil, &r, nil)
-       if e != 0 {
+       if e != nil {
                return "", os.NewSyscallError("LookupCNAME", e)
        }
        defer syscall.DnsRecordListFree(r, 1)
@@ -109,7 +109,7 @@ func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err
        }
        var r *syscall.DNSRecord
        e := syscall.DnsQuery(target, syscall.DNS_TYPE_SRV, 0, nil, &r, nil)
-       if e != 0 {
+       if e != nil {
                return "", nil, os.NewSyscallError("LookupSRV", e)
        }
        defer syscall.DnsRecordListFree(r, 1)
@@ -125,7 +125,7 @@ func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err err
 func LookupMX(name string) (mx []*MX, err error) {
        var r *syscall.DNSRecord
        e := syscall.DnsQuery(name, syscall.DNS_TYPE_MX, 0, nil, &r, nil)
-       if e != 0 {
+       if e != nil {
                return nil, os.NewSyscallError("LookupMX", e)
        }
        defer syscall.DnsRecordListFree(r, 1)
@@ -141,7 +141,7 @@ func LookupMX(name string) (mx []*MX, err error) {
 func LookupTXT(name string) (txt []string, err error) {
        var r *syscall.DNSRecord
        e := syscall.DnsQuery(name, syscall.DNS_TYPE_TEXT, 0, nil, &r, nil)
-       if e != 0 {
+       if e != nil {
                return nil, os.NewSyscallError("LookupTXT", e)
        }
        defer syscall.DnsRecordListFree(r, 1)
@@ -163,7 +163,7 @@ func LookupAddr(addr string) (name []string, err error) {
        }
        var r *syscall.DNSRecord
        e := syscall.DnsQuery(arpa, syscall.DNS_TYPE_PTR, 0, nil, &r, nil)
-       if e != 0 {
+       if e != nil {
                return nil, os.NewSyscallError("LookupAddr", e)
        }
        defer syscall.DnsRecordListFree(r, 1)
index 88f5a75777b6ac934b99c81079ef24a2e2fc70f4..2a45991dd7ea90f228befc192cdd9777c06579aa 100644 (file)
@@ -8,21 +8,6 @@ import (
        "sync"
 )
 
-// Errno is the Windows error number.
-type Errno uintptr
-
-func (e Errno) Error() string {
-       return errstr(e)
-}
-
-func (e Errno) Temporary() bool {
-       return e == EINTR || e == EMFILE || e.Timeout()
-}
-
-func (e Errno) Timeout() bool {
-       return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
-}
-
 // DLLError describes reasons for DLL load failures.
 type DLLError struct {
        Err     error
index 3b3df92bb7246ad8bfa31b03c303a70076ecb05c..0daca2eded2c4c2f2fd4c5bfed2f4e66991854a6 100755 (executable)
@@ -260,6 +260,11 @@ while(<>) {
                        $body .= "\t\t\t$name = ${syscalldot}EINVAL\n";
                        $body .= "\t\t}\n";
                        $body .= "\t}\n";
+               } elsif($rettype eq "error") {
+                       # Set $reg to "error" only if returned value indicate failure
+                       $body .= "\tif $reg != 0 {\n";
+                       $body .= "\t\t$name = Errno($reg)\n";
+                       $body .= "\t}\n";
                } else {
                        $body .= "\t$name = $rettype($reg)\n";
                }
index 8ee208a5c407ff75f5f916b902dad574f15fc818..f3025f11553aea8c07ac0bc51f436411a19f5a46 100644 (file)
@@ -76,6 +76,36 @@ func StringToUTF16Ptr(s string) *uint16 { return &StringToUTF16(s)[0] }
 
 func Getpagesize() int { return 4096 }
 
+// Errno is the Windows error number.
+type Errno uintptr
+
+func (e Errno) Error() string {
+       // deal with special go errors
+       idx := int(e - APPLICATION_ERROR)
+       if 0 <= idx && idx < len(errors) {
+               return errors[idx]
+       }
+       // ask windows for the remaining errors
+       var flags uint32 = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_IGNORE_INSERTS
+       b := make([]uint16, 300)
+       n, err := FormatMessage(flags, 0, uint32(e), 0, b, nil)
+       if err != nil {
+               return "error " + itoa(int(e)) + " (FormatMessage failed with err=" + itoa(int(err.(Errno))) + ")"
+       }
+       // trim terminating \r and \n
+       for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- {
+       }
+       return string(utf16.Decode(b[:n]))
+}
+
+func (e Errno) Temporary() bool {
+       return e == EINTR || e == EMFILE || e.Timeout()
+}
+
+func (e Errno) Timeout() bool {
+       return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
+}
+
 // Converts a Go function to a function pointer conforming
 // to the stdcall calling convention.  This is useful when
 // interoperating with Windows code requiring callbacks.
@@ -84,7 +114,7 @@ func NewCallback(fn interface{}) uintptr
 
 // windows api calls
 
-//sys  GetLastError() (lasterr uintptr)
+//sys  GetLastError() (lasterr error)
 //sys  LoadLibrary(libname string) (handle Handle, err error) = LoadLibraryW
 //sys  FreeLibrary(handle Handle) (err error)
 //sys  GetProcAddress(module Handle, procname string) (proc uintptr, err error)
@@ -154,34 +184,14 @@ func NewCallback(fn interface{}) uintptr
 //sys  CertOpenSystemStore(hprov Handle, name *uint16) (store Handle, err error) = crypt32.CertOpenSystemStoreW
 //sys  CertEnumCertificatesInStore(store Handle, prevContext *CertContext) (context *CertContext, err error) [failretval==nil] = crypt32.CertEnumCertificatesInStore
 //sys  CertCloseStore(store Handle, flags uint32) (err error) = crypt32.CertCloseStore
-//sys  RegOpenKeyEx(key Handle, subkey *uint16, options uint32, desiredAccess uint32, result *Handle) (regerrno uintptr) = advapi32.RegOpenKeyExW
-//sys  RegCloseKey(key Handle) (regerrno uintptr) = advapi32.RegCloseKey
-//sys  RegQueryInfoKey(key Handle, class *uint16, classLen *uint32, reserved *uint32, subkeysLen *uint32, maxSubkeyLen *uint32, maxClassLen *uint32, valuesLen *uint32, maxValueNameLen *uint32, maxValueLen *uint32, saLen *uint32, lastWriteTime *Filetime) (regerrno uintptr) = advapi32.RegQueryInfoKeyW
-//sys  RegEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno uintptr) = advapi32.RegEnumKeyExW
-//sys  RegQueryValueEx(key Handle, name *uint16, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno uintptr) = advapi32.RegQueryValueExW
+//sys  RegOpenKeyEx(key Handle, subkey *uint16, options uint32, desiredAccess uint32, result *Handle) (regerrno error) = advapi32.RegOpenKeyExW
+//sys  RegCloseKey(key Handle) (regerrno error) = advapi32.RegCloseKey
+//sys  RegQueryInfoKey(key Handle, class *uint16, classLen *uint32, reserved *uint32, subkeysLen *uint32, maxSubkeyLen *uint32, maxClassLen *uint32, valuesLen *uint32, maxValueNameLen *uint32, maxValueLen *uint32, saLen *uint32, lastWriteTime *Filetime) (regerrno error) = advapi32.RegQueryInfoKeyW
+//sys  RegEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno error) = advapi32.RegEnumKeyExW
+//sys  RegQueryValueEx(key Handle, name *uint16, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno error) = advapi32.RegQueryValueExW
 
 // syscall interface implementation for other packages
 
-
-func errstr(errno Errno) string {
-       // deal with special go errors
-       e := int(errno - APPLICATION_ERROR)
-       if 0 <= e && e < len(errors) {
-               return errors[e]
-       }
-       // ask windows for the remaining errors
-       var flags uint32 = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_IGNORE_INSERTS
-       b := make([]uint16, 300)
-       n, err := FormatMessage(flags, 0, uint32(errno), 0, b, nil)
-       if err != nil {
-               return "error " + itoa(int(errno)) + " (FormatMessage failed with err=" + itoa(int(err.(Errno))) + ")"
-       }
-       // trim terminating \r and \n
-       for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- {
-       }
-       return string(utf16.Decode(b[:n]))
-}
-
 func Exit(code int) { ExitProcess(uint32(code)) }
 
 func makeInheritSa() *SecurityAttributes {
@@ -415,7 +425,7 @@ func Chmod(path string, mode uint32) (err error) {
 
 // net api calls
 
-//sys  WSAStartup(verreq uint32, data *WSAData) (sockerr uintptr) = ws2_32.WSAStartup
+//sys  WSAStartup(verreq uint32, data *WSAData) (sockerr error) = ws2_32.WSAStartup
 //sys  WSACleanup() (err error) [failretval==-1] = ws2_32.WSACleanup
 //sys  WSAIoctl(s Handle, iocc uint32, inbuf *byte, cbif uint32, outbuf *byte, cbob uint32, cbbr *uint32, overlapped *Overlapped, completionRoutine uintptr) (err error) [failretval==-1] = ws2_32.WSAIoctl
 //sys  socket(af int32, typ int32, protocol int32) (handle Handle, err error) [failretval==InvalidHandle] = ws2_32.socket
@@ -437,10 +447,10 @@ func Chmod(path string, mode uint32) (err error) {
 //sys  GetServByName(name string, proto string) (s *Servent, err error) [failretval==nil] = ws2_32.getservbyname
 //sys  Ntohs(netshort uint16) (u uint16) = ws2_32.ntohs
 //sys  GetProtoByName(name string) (p *Protoent, err error) [failretval==nil] = ws2_32.getprotobyname
-//sys  DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status Errno) = dnsapi.DnsQuery_W
+//sys  DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status error) = dnsapi.DnsQuery_W
 //sys  DnsRecordListFree(rl *DNSRecord, freetype uint32) = dnsapi.DnsRecordListFree
-//sys  GetIfEntry(pIfRow *MibIfRow) (errcode Errno) = iphlpapi.GetIfEntry
-//sys  GetAdaptersInfo(ai *IpAdapterInfo, ol *uint32) (errcode Errno) = iphlpapi.GetAdaptersInfo
+//sys  GetIfEntry(pIfRow *MibIfRow) (errcode error) = iphlpapi.GetIfEntry
+//sys  GetAdaptersInfo(ai *IpAdapterInfo, ol *uint32) (errcode error) = iphlpapi.GetAdaptersInfo
 
 // For testing: clients can set this flag to force
 // creation of IPv6 sockets to return EAFNOSUPPORT.
index 25fa9c48b520f7bab4f35d1963d7e4a784acfce7..5550975c048ba2c8dd84345f93b57f78b41b0ce4 100644 (file)
@@ -118,9 +118,11 @@ var (
        procGetAdaptersInfo             = modiphlpapi.NewProc("GetAdaptersInfo")
 )
 
-func GetLastError() (lasterr uintptr) {
+func GetLastError() (lasterr error) {
        r0, _, _ := Syscall(procGetLastError.Addr(), 0, 0, 0, 0)
-       lasterr = uintptr(r0)
+       if r0 != 0 {
+               lasterr = Errno(r0)
+       }
        return
 }
 
@@ -994,39 +996,51 @@ func CertCloseStore(store Handle, flags uint32) (err error) {
        return
 }
 
-func RegOpenKeyEx(key Handle, subkey *uint16, options uint32, desiredAccess uint32, result *Handle) (regerrno uintptr) {
+func RegOpenKeyEx(key Handle, subkey *uint16, options uint32, desiredAccess uint32, result *Handle) (regerrno error) {
        r0, _, _ := Syscall6(procRegOpenKeyExW.Addr(), 5, uintptr(key), uintptr(unsafe.Pointer(subkey)), uintptr(options), uintptr(desiredAccess), uintptr(unsafe.Pointer(result)), 0)
-       regerrno = uintptr(r0)
+       if r0 != 0 {
+               regerrno = Errno(r0)
+       }
        return
 }
 
-func RegCloseKey(key Handle) (regerrno uintptr) {
+func RegCloseKey(key Handle) (regerrno error) {
        r0, _, _ := Syscall(procRegCloseKey.Addr(), 1, uintptr(key), 0, 0)
-       regerrno = uintptr(r0)
+       if r0 != 0 {
+               regerrno = Errno(r0)
+       }
        return
 }
 
-func RegQueryInfoKey(key Handle, class *uint16, classLen *uint32, reserved *uint32, subkeysLen *uint32, maxSubkeyLen *uint32, maxClassLen *uint32, valuesLen *uint32, maxValueNameLen *uint32, maxValueLen *uint32, saLen *uint32, lastWriteTime *Filetime) (regerrno uintptr) {
+func RegQueryInfoKey(key Handle, class *uint16, classLen *uint32, reserved *uint32, subkeysLen *uint32, maxSubkeyLen *uint32, maxClassLen *uint32, valuesLen *uint32, maxValueNameLen *uint32, maxValueLen *uint32, saLen *uint32, lastWriteTime *Filetime) (regerrno error) {
        r0, _, _ := Syscall12(procRegQueryInfoKeyW.Addr(), 12, uintptr(key), uintptr(unsafe.Pointer(class)), uintptr(unsafe.Pointer(classLen)), uintptr(unsafe.Pointer(reserved)), uintptr(unsafe.Pointer(subkeysLen)), uintptr(unsafe.Pointer(maxSubkeyLen)), uintptr(unsafe.Pointer(maxClassLen)), uintptr(unsafe.Pointer(valuesLen)), uintptr(unsafe.Pointer(maxValueNameLen)), uintptr(unsafe.Pointer(maxValueLen)), uintptr(unsafe.Pointer(saLen)), uintptr(unsafe.Pointer(lastWriteTime)))
-       regerrno = uintptr(r0)
+       if r0 != 0 {
+               regerrno = Errno(r0)
+       }
        return
 }
 
-func RegEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno uintptr) {
+func RegEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno error) {
        r0, _, _ := Syscall9(procRegEnumKeyExW.Addr(), 8, uintptr(key), uintptr(index), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(nameLen)), uintptr(unsafe.Pointer(reserved)), uintptr(unsafe.Pointer(class)), uintptr(unsafe.Pointer(classLen)), uintptr(unsafe.Pointer(lastWriteTime)), 0)
-       regerrno = uintptr(r0)
+       if r0 != 0 {
+               regerrno = Errno(r0)
+       }
        return
 }
 
-func RegQueryValueEx(key Handle, name *uint16, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno uintptr) {
+func RegQueryValueEx(key Handle, name *uint16, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno error) {
        r0, _, _ := Syscall6(procRegQueryValueExW.Addr(), 6, uintptr(key), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(reserved)), uintptr(unsafe.Pointer(valtype)), uintptr(unsafe.Pointer(buf)), uintptr(unsafe.Pointer(buflen)))
-       regerrno = uintptr(r0)
+       if r0 != 0 {
+               regerrno = Errno(r0)
+       }
        return
 }
 
-func WSAStartup(verreq uint32, data *WSAData) (sockerr uintptr) {
+func WSAStartup(verreq uint32, data *WSAData) (sockerr error) {
        r0, _, _ := Syscall(procWSAStartup.Addr(), 2, uintptr(verreq), uintptr(unsafe.Pointer(data)), 0)
-       sockerr = uintptr(r0)
+       if r0 != 0 {
+               sockerr = Errno(r0)
+       }
        return
 }
 
@@ -1273,9 +1287,11 @@ func GetProtoByName(name string) (p *Protoent, err error) {
        return
 }
 
-func DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status Errno) {
+func DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status error) {
        r0, _, _ := Syscall6(procDnsQuery_W.Addr(), 6, uintptr(unsafe.Pointer(StringToUTF16Ptr(name))), uintptr(qtype), uintptr(options), uintptr(unsafe.Pointer(extra)), uintptr(unsafe.Pointer(qrs)), uintptr(unsafe.Pointer(pr)))
-       status = Errno(r0)
+       if r0 != 0 {
+               status = Errno(r0)
+       }
        return
 }
 
@@ -1284,14 +1300,18 @@ func DnsRecordListFree(rl *DNSRecord, freetype uint32) {
        return
 }
 
-func GetIfEntry(pIfRow *MibIfRow) (errcode Errno) {
+func GetIfEntry(pIfRow *MibIfRow) (errcode error) {
        r0, _, _ := Syscall(procGetIfEntry.Addr(), 1, uintptr(unsafe.Pointer(pIfRow)), 0, 0)
-       errcode = Errno(r0)
+       if r0 != 0 {
+               errcode = Errno(r0)
+       }
        return
 }
 
-func GetAdaptersInfo(ai *IpAdapterInfo, ol *uint32) (errcode Errno) {
+func GetAdaptersInfo(ai *IpAdapterInfo, ol *uint32) (errcode error) {
        r0, _, _ := Syscall(procGetAdaptersInfo.Addr(), 2, uintptr(unsafe.Pointer(ai)), uintptr(unsafe.Pointer(ol)), 0)
-       errcode = Errno(r0)
+       if r0 != 0 {
+               errcode = Errno(r0)
+       }
        return
 }
index bba74623bdeebc84e1e0be4828e367638ddee6d1..df1c4f0a8191e789b15e174d4a2cafca44ca3543 100644 (file)
@@ -118,9 +118,11 @@ var (
        procGetAdaptersInfo             = modiphlpapi.NewProc("GetAdaptersInfo")
 )
 
-func GetLastError() (lasterr uintptr) {
+func GetLastError() (lasterr error) {
        r0, _, _ := Syscall(procGetLastError.Addr(), 0, 0, 0, 0)
-       lasterr = uintptr(r0)
+       if r0 != 0 {
+               lasterr = Errno(r0)
+       }
        return
 }
 
@@ -994,39 +996,51 @@ func CertCloseStore(store Handle, flags uint32) (err error) {
        return
 }
 
-func RegOpenKeyEx(key Handle, subkey *uint16, options uint32, desiredAccess uint32, result *Handle) (regerrno uintptr) {
+func RegOpenKeyEx(key Handle, subkey *uint16, options uint32, desiredAccess uint32, result *Handle) (regerrno error) {
        r0, _, _ := Syscall6(procRegOpenKeyExW.Addr(), 5, uintptr(key), uintptr(unsafe.Pointer(subkey)), uintptr(options), uintptr(desiredAccess), uintptr(unsafe.Pointer(result)), 0)
-       regerrno = uintptr(r0)
+       if r0 != 0 {
+               regerrno = Errno(r0)
+       }
        return
 }
 
-func RegCloseKey(key Handle) (regerrno uintptr) {
+func RegCloseKey(key Handle) (regerrno error) {
        r0, _, _ := Syscall(procRegCloseKey.Addr(), 1, uintptr(key), 0, 0)
-       regerrno = uintptr(r0)
+       if r0 != 0 {
+               regerrno = Errno(r0)
+       }
        return
 }
 
-func RegQueryInfoKey(key Handle, class *uint16, classLen *uint32, reserved *uint32, subkeysLen *uint32, maxSubkeyLen *uint32, maxClassLen *uint32, valuesLen *uint32, maxValueNameLen *uint32, maxValueLen *uint32, saLen *uint32, lastWriteTime *Filetime) (regerrno uintptr) {
+func RegQueryInfoKey(key Handle, class *uint16, classLen *uint32, reserved *uint32, subkeysLen *uint32, maxSubkeyLen *uint32, maxClassLen *uint32, valuesLen *uint32, maxValueNameLen *uint32, maxValueLen *uint32, saLen *uint32, lastWriteTime *Filetime) (regerrno error) {
        r0, _, _ := Syscall12(procRegQueryInfoKeyW.Addr(), 12, uintptr(key), uintptr(unsafe.Pointer(class)), uintptr(unsafe.Pointer(classLen)), uintptr(unsafe.Pointer(reserved)), uintptr(unsafe.Pointer(subkeysLen)), uintptr(unsafe.Pointer(maxSubkeyLen)), uintptr(unsafe.Pointer(maxClassLen)), uintptr(unsafe.Pointer(valuesLen)), uintptr(unsafe.Pointer(maxValueNameLen)), uintptr(unsafe.Pointer(maxValueLen)), uintptr(unsafe.Pointer(saLen)), uintptr(unsafe.Pointer(lastWriteTime)))
-       regerrno = uintptr(r0)
+       if r0 != 0 {
+               regerrno = Errno(r0)
+       }
        return
 }
 
-func RegEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno uintptr) {
+func RegEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno error) {
        r0, _, _ := Syscall9(procRegEnumKeyExW.Addr(), 8, uintptr(key), uintptr(index), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(nameLen)), uintptr(unsafe.Pointer(reserved)), uintptr(unsafe.Pointer(class)), uintptr(unsafe.Pointer(classLen)), uintptr(unsafe.Pointer(lastWriteTime)), 0)
-       regerrno = uintptr(r0)
+       if r0 != 0 {
+               regerrno = Errno(r0)
+       }
        return
 }
 
-func RegQueryValueEx(key Handle, name *uint16, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno uintptr) {
+func RegQueryValueEx(key Handle, name *uint16, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno error) {
        r0, _, _ := Syscall6(procRegQueryValueExW.Addr(), 6, uintptr(key), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(reserved)), uintptr(unsafe.Pointer(valtype)), uintptr(unsafe.Pointer(buf)), uintptr(unsafe.Pointer(buflen)))
-       regerrno = uintptr(r0)
+       if r0 != 0 {
+               regerrno = Errno(r0)
+       }
        return
 }
 
-func WSAStartup(verreq uint32, data *WSAData) (sockerr uintptr) {
+func WSAStartup(verreq uint32, data *WSAData) (sockerr error) {
        r0, _, _ := Syscall(procWSAStartup.Addr(), 2, uintptr(verreq), uintptr(unsafe.Pointer(data)), 0)
-       sockerr = uintptr(r0)
+       if r0 != 0 {
+               sockerr = Errno(r0)
+       }
        return
 }
 
@@ -1273,9 +1287,11 @@ func GetProtoByName(name string) (p *Protoent, err error) {
        return
 }
 
-func DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status Errno) {
+func DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status error) {
        r0, _, _ := Syscall6(procDnsQuery_W.Addr(), 6, uintptr(unsafe.Pointer(StringToUTF16Ptr(name))), uintptr(qtype), uintptr(options), uintptr(unsafe.Pointer(extra)), uintptr(unsafe.Pointer(qrs)), uintptr(unsafe.Pointer(pr)))
-       status = Errno(r0)
+       if r0 != 0 {
+               status = Errno(r0)
+       }
        return
 }
 
@@ -1284,14 +1300,18 @@ func DnsRecordListFree(rl *DNSRecord, freetype uint32) {
        return
 }
 
-func GetIfEntry(pIfRow *MibIfRow) (errcode Errno) {
+func GetIfEntry(pIfRow *MibIfRow) (errcode error) {
        r0, _, _ := Syscall(procGetIfEntry.Addr(), 1, uintptr(unsafe.Pointer(pIfRow)), 0, 0)
-       errcode = Errno(r0)
+       if r0 != 0 {
+               errcode = Errno(r0)
+       }
        return
 }
 
-func GetAdaptersInfo(ai *IpAdapterInfo, ol *uint32) (errcode Errno) {
+func GetAdaptersInfo(ai *IpAdapterInfo, ol *uint32) (errcode error) {
        r0, _, _ := Syscall(procGetAdaptersInfo.Addr(), 2, uintptr(unsafe.Pointer(ai)), uintptr(unsafe.Pointer(ol)), 0)
-       errcode = Errno(r0)
+       if r0 != 0 {
+               errcode = Errno(r0)
+       }
        return
 }