mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 09:24:36 +00:00
prefer consts to importing #defines from headers
to be completed - better would be to have a libc wrapper that deals with all pesky C ABI details
This commit is contained in:
@@ -29,28 +29,26 @@ proc c_strcmp(a, b: cstring): cint {.
|
|||||||
type
|
type
|
||||||
C_JmpBuf {.importc: "jmp_buf", header: "<setjmp.h>".} = object
|
C_JmpBuf {.importc: "jmp_buf", header: "<setjmp.h>".} = object
|
||||||
|
|
||||||
# constants faked as variables:
|
when defined(windows):
|
||||||
when not declared(SIGINT):
|
const
|
||||||
|
SIGABRT = cint(22)
|
||||||
|
SIGFPE = cint(8)
|
||||||
|
SIGILL = cint(4)
|
||||||
|
SIGINT = cint(2)
|
||||||
|
SIGSEGV = cint(11)
|
||||||
|
SIGTERM = cint(15)
|
||||||
|
elif defined(macosx) or defined(linux):
|
||||||
|
const
|
||||||
|
SIGABRT = cint(6)
|
||||||
|
SIGFPE = cint(8)
|
||||||
|
SIGILL = cint(4)
|
||||||
|
SIGINT = cint(2)
|
||||||
|
SIGSEGV = cint(11)
|
||||||
|
SIGTERM = cint(15)
|
||||||
|
SIGPIPE = cint(13)
|
||||||
|
else:
|
||||||
when NoFakeVars:
|
when NoFakeVars:
|
||||||
when defined(windows):
|
{.error: "SIGABRT not ported to your platform".}
|
||||||
const
|
|
||||||
SIGABRT = cint(22)
|
|
||||||
SIGFPE = cint(8)
|
|
||||||
SIGILL = cint(4)
|
|
||||||
SIGINT = cint(2)
|
|
||||||
SIGSEGV = cint(11)
|
|
||||||
SIGTERM = cint(15)
|
|
||||||
elif defined(macosx) or defined(linux):
|
|
||||||
const
|
|
||||||
SIGABRT = cint(6)
|
|
||||||
SIGFPE = cint(8)
|
|
||||||
SIGILL = cint(4)
|
|
||||||
SIGINT = cint(2)
|
|
||||||
SIGSEGV = cint(11)
|
|
||||||
SIGTERM = cint(15)
|
|
||||||
SIGPIPE = cint(13)
|
|
||||||
else:
|
|
||||||
{.error: "SIGABRT not ported to your platform".}
|
|
||||||
else:
|
else:
|
||||||
var
|
var
|
||||||
SIGINT {.importc: "SIGINT", nodecl.}: cint
|
SIGINT {.importc: "SIGINT", nodecl.}: cint
|
||||||
@@ -62,10 +60,7 @@ when not declared(SIGINT):
|
|||||||
var SIGPIPE {.importc: "SIGPIPE", nodecl.}: cint
|
var SIGPIPE {.importc: "SIGPIPE", nodecl.}: cint
|
||||||
|
|
||||||
when defined(macosx):
|
when defined(macosx):
|
||||||
when NoFakeVars:
|
const SIGBUS = cint(10)
|
||||||
const SIGBUS = cint(10)
|
|
||||||
else:
|
|
||||||
var SIGBUS {.importc: "SIGBUS", nodecl.}: cint
|
|
||||||
else:
|
else:
|
||||||
template SIGBUS: expr = SIGSEGV
|
template SIGBUS: expr = SIGSEGV
|
||||||
|
|
||||||
|
|||||||
@@ -52,11 +52,14 @@ when defined(posix):
|
|||||||
#
|
#
|
||||||
|
|
||||||
# c stuff:
|
# c stuff:
|
||||||
var
|
when defined(linux) or defined(macosx):
|
||||||
RTLD_NOW {.importc: "RTLD_NOW", header: "<dlfcn.h>".}: int
|
const RTLD_NOW = cint(2)
|
||||||
|
else:
|
||||||
|
var
|
||||||
|
RTLD_NOW {.importc: "RTLD_NOW", header: "<dlfcn.h>".}: cint
|
||||||
|
|
||||||
proc dlclose(lib: LibHandle) {.importc, header: "<dlfcn.h>".}
|
proc dlclose(lib: LibHandle) {.importc, header: "<dlfcn.h>".}
|
||||||
proc dlopen(path: cstring, mode: int): LibHandle {.
|
proc dlopen(path: cstring, mode: cint): LibHandle {.
|
||||||
importc, header: "<dlfcn.h>".}
|
importc, header: "<dlfcn.h>".}
|
||||||
proc dlsym(lib: LibHandle, name: cstring): ProcAddr {.
|
proc dlsym(lib: LibHandle, name: cstring): ProcAddr {.
|
||||||
importc, header: "<dlfcn.h>".}
|
importc, header: "<dlfcn.h>".}
|
||||||
|
|||||||
@@ -87,6 +87,8 @@ elif defined(posix):
|
|||||||
const MAP_ANONYMOUS = 0x1000
|
const MAP_ANONYMOUS = 0x1000
|
||||||
elif defined(solaris):
|
elif defined(solaris):
|
||||||
const MAP_ANONYMOUS = 0x100
|
const MAP_ANONYMOUS = 0x100
|
||||||
|
elif defined(linux):
|
||||||
|
const MAP_ANONYMOUS = 0x20
|
||||||
else:
|
else:
|
||||||
var
|
var
|
||||||
MAP_ANONYMOUS {.importc: "MAP_ANONYMOUS", header: "<sys/mman.h>".}: cint
|
MAP_ANONYMOUS {.importc: "MAP_ANONYMOUS", header: "<sys/mman.h>".}: cint
|
||||||
|
|||||||
@@ -277,15 +277,31 @@ const
|
|||||||
# should not be translated.
|
# should not be translated.
|
||||||
|
|
||||||
when defined(posix) and not defined(nimscript):
|
when defined(posix) and not defined(nimscript):
|
||||||
type
|
when defined(linux) and defined(amd64):
|
||||||
Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint
|
type
|
||||||
|
Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint
|
||||||
|
|
||||||
Stat {.importc: "struct stat",
|
# fillers ensure correct size & offsets
|
||||||
header: "<sys/stat.h>", final, pure.} = object ## struct stat
|
Stat {.importc: "struct stat",
|
||||||
st_mode: Mode ## Mode of file
|
header: "<sys/stat.h>", final, pure.} = object ## struct stat
|
||||||
|
filler_1: array[24, char]
|
||||||
|
st_mode: Mode ## Mode of file
|
||||||
|
filler_2: array[144 - 24 - 4, char]
|
||||||
|
|
||||||
proc S_ISDIR(m: Mode): bool {.importc, header: "<sys/stat.h>".}
|
proc S_ISDIR(m: Mode): bool =
|
||||||
## Test for a directory.
|
## Test for a directory.
|
||||||
|
(m and 0o170000) == 0o40000
|
||||||
|
|
||||||
|
else:
|
||||||
|
type
|
||||||
|
Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint
|
||||||
|
|
||||||
|
Stat {.importc: "struct stat",
|
||||||
|
header: "<sys/stat.h>", final, pure.} = object ## struct stat
|
||||||
|
st_mode: Mode ## Mode of file
|
||||||
|
|
||||||
|
proc S_ISDIR(m: Mode): bool {.importc, header: "<sys/stat.h>".}
|
||||||
|
## Test for a directory.
|
||||||
|
|
||||||
proc c_fstat(a1: cint, a2: var Stat): cint {.
|
proc c_fstat(a1: cint, a2: var Stat): cint {.
|
||||||
importc: "fstat", header: "<sys/stat.h>".}
|
importc: "fstat", header: "<sys/stat.h>".}
|
||||||
|
|||||||
@@ -117,6 +117,11 @@ else:
|
|||||||
schedh = "#define _GNU_SOURCE\n#include <sched.h>"
|
schedh = "#define _GNU_SOURCE\n#include <sched.h>"
|
||||||
pthreadh = "#define _GNU_SOURCE\n#include <pthread.h>"
|
pthreadh = "#define _GNU_SOURCE\n#include <pthread.h>"
|
||||||
|
|
||||||
|
when defined(linux):
|
||||||
|
type Time = clong
|
||||||
|
else:
|
||||||
|
type Time = int
|
||||||
|
|
||||||
type
|
type
|
||||||
SysThread {.importc: "pthread_t", header: "<sys/types.h>",
|
SysThread {.importc: "pthread_t", header: "<sys/types.h>",
|
||||||
final, pure.} = object
|
final, pure.} = object
|
||||||
@@ -125,8 +130,8 @@ else:
|
|||||||
|
|
||||||
Timespec {.importc: "struct timespec",
|
Timespec {.importc: "struct timespec",
|
||||||
header: "<time.h>", final, pure.} = object
|
header: "<time.h>", final, pure.} = object
|
||||||
tv_sec: int
|
tv_sec: Time
|
||||||
tv_nsec: int
|
tv_nsec: clong
|
||||||
{.deprecated: [TSysThread: SysThread, Tpthread_attr: PThreadAttr,
|
{.deprecated: [TSysThread: SysThread, Tpthread_attr: PThreadAttr,
|
||||||
Ttimespec: Timespec].}
|
Ttimespec: Timespec].}
|
||||||
|
|
||||||
|
|||||||
@@ -78,11 +78,16 @@ elif defined(posixRealtime):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
# fallback Posix implementation:
|
# fallback Posix implementation:
|
||||||
|
when defined(linux):
|
||||||
|
type Time = clong
|
||||||
|
else:
|
||||||
|
type Time = int
|
||||||
|
|
||||||
type
|
type
|
||||||
Timeval {.importc: "struct timeval", header: "<sys/select.h>",
|
Timeval {.importc: "struct timeval", header: "<sys/select.h>",
|
||||||
final, pure.} = object ## struct timeval
|
final, pure.} = object ## struct timeval
|
||||||
tv_sec: int ## Seconds.
|
tv_sec: Time ## Seconds.
|
||||||
tv_usec: int ## Microseconds.
|
tv_usec: clong ## Microseconds.
|
||||||
{.deprecated: [Ttimeval: Timeval].}
|
{.deprecated: [Ttimeval: Timeval].}
|
||||||
proc posix_gettimeofday(tp: var Timeval, unused: pointer = nil) {.
|
proc posix_gettimeofday(tp: var Timeval, unused: pointer = nil) {.
|
||||||
importc: "gettimeofday", header: "<sys/time.h>".}
|
importc: "gettimeofday", header: "<sys/time.h>".}
|
||||||
|
|||||||
Reference in New Issue
Block a user