# # # Nim's Runtime Library # (c) Copyright 2013 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # This module contains headers of Ansi C procs # and definitions of Ansi C types in Nim syntax # All symbols are prefixed with 'c_' to avoid ambiguities {.push hints:off, stack_trace: off, profiler: off.} when not defined(nimHasHotCodeReloading): {.pragma: nonReloadable.} proc c_memchr*(s: pointer, c: cint, n: csize_t): pointer {. importc: "memchr", header: "".} proc c_memcmp*(a, b: pointer, size: csize_t): cint {. importc: "memcmp", header: "", noSideEffect.} proc c_memcpy*(a, b: pointer, size: csize_t): pointer {. importc: "memcpy", header: "", discardable.} proc c_memmove*(a, b: pointer, size: csize_t): pointer {. importc: "memmove", header: "",discardable.} proc c_memset*(p: pointer, value: cint, size: csize_t): pointer {. importc: "memset", header: "", discardable.} proc c_strcmp*(a, b: cstring): cint {. importc: "strcmp", header: "", noSideEffect.} proc c_strlen*(a: cstring): csize_t {. importc: "strlen", header: "", noSideEffect.} proc c_abort*() {. importc: "abort", header: "", noSideEffect, noreturn.} when defined(linux) and defined(amd64): type C_JmpBuf* {.importc: "jmp_buf", header: "", bycopy.} = object abi: array[200 div sizeof(clong), clong] else: type C_JmpBuf* {.importc: "jmp_buf", header: "".} = object when defined(windows): 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) or defined(freebsd) or defined(openbsd) or defined(netbsd) or defined(solaris) or defined(dragonfly) or defined(nintendoswitch) or defined(genode) or defined(aix) or hostOS == "standalone": const SIGABRT* = cint(6) SIGFPE* = cint(8) SIGILL* = cint(4) SIGINT* = cint(2) SIGSEGV* = cint(11) SIGTERM* = cint(15) SIGPIPE* = cint(13) elif defined(haiku): const SIGABRT* = cint(6) SIGFPE* = cint(8) SIGILL* = cint(4) SIGINT* = cint(2) SIGSEGV* = cint(11) SIGTERM* = cint(15) SIGPIPE* = cint(7) else: when NoFakeVars: {.error: "SIGABRT not ported to your platform".} else: var SIGINT* {.importc: "SIGINT", nodecl.}: cint SIGSEGV* {.importc: "SIGSEGV", nodecl.}: cint SIGABRT* {.importc: "SIGABRT", nodecl.}: cint SIGFPE* {.importc: "SIGFPE", nodecl.}: cint SIGILL* {.importc: "SIGILL", nodecl.}: cint when defined(macosx) or defined(linux): var SIGPIPE* {.importc: "SIGPIPE", nodecl.}: cint when defined(macosx): const SIGBUS* = cint(10) elif defined(haiku): const SIGBUS* = cint(30) when defined(nimSigSetjmp) and not defined(nimStdSetjmp): proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {. header: "", importc: "siglongjmp".} template c_setjmp*(jmpb: C_JmpBuf): cint = proc c_sigsetjmp(jmpb: C_JmpBuf, savemask: cint): cint {. header: "", importc: "sigsetjmp".} c_sigsetjmp(jmpb, 0) elif defined(nimRawSetjmp) and not defined(nimStdSetjmp): proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {. header: "", importc: "_longjmp".} proc c_setjmp*(jmpb: C_JmpBuf): cint {. header: "", importc: "_setjmp".} else: proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {. header: "", importc: "longjmp".} proc c_setjmp*(jmpb: C_JmpBuf): cint {. header: "", importc: "setjmp".} type CSighandlerT = proc (a: cint) {.noconv.} proc c_signal*(sign: cint, handler: proc (a: cint) {.noconv.}): CSighandlerT {. importc: "signal", header: "", discardable.} type CFile {.importc: "FILE", header: "", incompleteStruct.} = object CFilePtr* = ptr CFile ## The type representing a file handle. # duplicated between io and ansi_c const stdioUsesMacros = (defined(osx) or defined(freebsd) or defined(dragonfly)) and not defined(emscripten) const stderrName = when stdioUsesMacros: "__stderrp" else: "stderr" const stdoutName = when stdioUsesMacros: "__stdoutp" else: "stdout" const stdinName = when stdioUsesMacros: "__stdinp" else: "stdin" var cstderr* {.importc: stderrName, header: "".}: CFilePtr cstdout* {.importc: stdoutName, header: "".}: CFilePtr cstdin* {.importc: stdinName, header: "".}: CFilePtr proc c_fprintf*(f: CFilePtr, frmt: cstring): cint {. importc: "fprintf", header: "", varargs, discardable.} proc c_printf*(frmt: cstring): cint {. importc: "printf", header: "", varargs, discardable.} proc c_fputs*(c: cstring, f: CFilePtr): cint {. importc: "fputs", header: "", discardable.} proc c_sprintf*(buf, frmt: cstring): cint {. importc: "sprintf", header: "", varargs, noSideEffect.} # we use it only in a way that cannot lead to security issues proc c_malloc*(size: csize_t): pointer {. importc: "malloc", header: "".} proc c_calloc*(nmemb, size: csize_t): pointer {. importc: "calloc", header: "".} proc c_free*(p: pointer) {. importc: "free", header: "".} proc c_realloc*(p: pointer, newsize: csize_t): pointer {. importc: "realloc", header: "".} proc c_fwrite*(buf: pointer, size, n: csize_t, f: CFilePtr): cint {. importc: "fwrite", header: "".} proc c_fflush(f: CFilePtr): cint {. importc: "fflush", header: "".} proc rawWrite*(f: CFilePtr, s: cstring) {.compilerproc, nonReloadable, inline.} = # we cannot throw an exception here! discard c_fwrite(s, 1, cast[csize_t](s.len), f) discard c_fflush(f) {.pop.}