ansi_c.nim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module contains headers of Ansi C procs
  10. # and definitions of Ansi C types in Nim syntax
  11. # All symbols are prefixed with 'c_' to avoid ambiguities
  12. {.push hints:off, stack_trace: off, profiler: off.}
  13. when not defined(nimHasHotCodeReloading):
  14. {.pragma: nonReloadable.}
  15. proc c_memchr*(s: pointer, c: cint, n: csize_t): pointer {.
  16. importc: "memchr", header: "<string.h>".}
  17. proc c_memcmp*(a, b: pointer, size: csize_t): cint {.
  18. importc: "memcmp", header: "<string.h>", noSideEffect.}
  19. proc c_memcpy*(a, b: pointer, size: csize_t): pointer {.
  20. importc: "memcpy", header: "<string.h>", discardable.}
  21. proc c_memmove*(a, b: pointer, size: csize_t): pointer {.
  22. importc: "memmove", header: "<string.h>",discardable.}
  23. proc c_memset*(p: pointer, value: cint, size: csize_t): pointer {.
  24. importc: "memset", header: "<string.h>", discardable.}
  25. proc c_strcmp*(a, b: cstring): cint {.
  26. importc: "strcmp", header: "<string.h>", noSideEffect.}
  27. proc c_strlen*(a: cstring): csize_t {.
  28. importc: "strlen", header: "<string.h>", noSideEffect.}
  29. proc c_abort*() {.
  30. importc: "abort", header: "<stdlib.h>", noSideEffect, noreturn.}
  31. when defined(linux) and defined(amd64):
  32. type
  33. C_JmpBuf* {.importc: "jmp_buf", header: "<setjmp.h>", bycopy.} = object
  34. abi: array[200 div sizeof(clong), clong]
  35. else:
  36. type
  37. C_JmpBuf* {.importc: "jmp_buf", header: "<setjmp.h>".} = object
  38. when defined(windows):
  39. const
  40. SIGABRT* = cint(22)
  41. SIGFPE* = cint(8)
  42. SIGILL* = cint(4)
  43. SIGINT* = cint(2)
  44. SIGSEGV* = cint(11)
  45. SIGTERM = cint(15)
  46. elif defined(macosx) or defined(linux) or defined(freebsd) or
  47. defined(openbsd) or defined(netbsd) or defined(solaris) or
  48. defined(dragonfly) or defined(nintendoswitch) or defined(genode) or
  49. defined(aix) or hostOS == "standalone":
  50. const
  51. SIGABRT* = cint(6)
  52. SIGFPE* = cint(8)
  53. SIGILL* = cint(4)
  54. SIGINT* = cint(2)
  55. SIGSEGV* = cint(11)
  56. SIGTERM* = cint(15)
  57. SIGPIPE* = cint(13)
  58. elif defined(haiku):
  59. const
  60. SIGABRT* = cint(6)
  61. SIGFPE* = cint(8)
  62. SIGILL* = cint(4)
  63. SIGINT* = cint(2)
  64. SIGSEGV* = cint(11)
  65. SIGTERM* = cint(15)
  66. SIGPIPE* = cint(7)
  67. else:
  68. when NoFakeVars:
  69. {.error: "SIGABRT not ported to your platform".}
  70. else:
  71. var
  72. SIGINT* {.importc: "SIGINT", nodecl.}: cint
  73. SIGSEGV* {.importc: "SIGSEGV", nodecl.}: cint
  74. SIGABRT* {.importc: "SIGABRT", nodecl.}: cint
  75. SIGFPE* {.importc: "SIGFPE", nodecl.}: cint
  76. SIGILL* {.importc: "SIGILL", nodecl.}: cint
  77. when defined(macosx) or defined(linux):
  78. var SIGPIPE* {.importc: "SIGPIPE", nodecl.}: cint
  79. when defined(macosx):
  80. const SIGBUS* = cint(10)
  81. elif defined(haiku):
  82. const SIGBUS* = cint(30)
  83. when defined(nimSigSetjmp) and not defined(nimStdSetjmp):
  84. proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {.
  85. header: "<setjmp.h>", importc: "siglongjmp".}
  86. template c_setjmp*(jmpb: C_JmpBuf): cint =
  87. proc c_sigsetjmp(jmpb: C_JmpBuf, savemask: cint): cint {.
  88. header: "<setjmp.h>", importc: "sigsetjmp".}
  89. c_sigsetjmp(jmpb, 0)
  90. elif defined(nimRawSetjmp) and not defined(nimStdSetjmp):
  91. proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {.
  92. header: "<setjmp.h>", importc: "_longjmp".}
  93. proc c_setjmp*(jmpb: C_JmpBuf): cint {.
  94. header: "<setjmp.h>", importc: "_setjmp".}
  95. else:
  96. proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {.
  97. header: "<setjmp.h>", importc: "longjmp".}
  98. proc c_setjmp*(jmpb: C_JmpBuf): cint {.
  99. header: "<setjmp.h>", importc: "setjmp".}
  100. type CSighandlerT = proc (a: cint) {.noconv.}
  101. proc c_signal*(sign: cint, handler: proc (a: cint) {.noconv.}): CSighandlerT {.
  102. importc: "signal", header: "<signal.h>", discardable.}
  103. type
  104. CFile {.importc: "FILE", header: "<stdio.h>",
  105. incompleteStruct.} = object
  106. CFilePtr* = ptr CFile ## The type representing a file handle.
  107. var
  108. cstderr* {.importc: "stderr", header: "<stdio.h>".}: CFilePtr
  109. cstdout* {.importc: "stdout", header: "<stdio.h>".}: CFilePtr
  110. proc c_fprintf*(f: CFilePtr, frmt: cstring): cint {.
  111. importc: "fprintf", header: "<stdio.h>", varargs, discardable.}
  112. proc c_printf*(frmt: cstring): cint {.
  113. importc: "printf", header: "<stdio.h>", varargs, discardable.}
  114. proc c_fputs*(c: cstring, f: CFilePtr): cint {.
  115. importc: "fputs", header: "<stdio.h>", discardable.}
  116. proc c_sprintf*(buf, frmt: cstring): cint {.
  117. importc: "sprintf", header: "<stdio.h>", varargs, noSideEffect.}
  118. # we use it only in a way that cannot lead to security issues
  119. proc c_malloc*(size: csize_t): pointer {.
  120. importc: "malloc", header: "<stdlib.h>".}
  121. proc c_free*(p: pointer) {.
  122. importc: "free", header: "<stdlib.h>".}
  123. proc c_realloc*(p: pointer, newsize: csize_t): pointer {.
  124. importc: "realloc", header: "<stdlib.h>".}
  125. proc c_fwrite*(buf: pointer, size, n: csize_t, f: CFilePtr): cint {.
  126. importc: "fwrite", header: "<stdio.h>".}
  127. proc c_fflush(f: CFilePtr): cint {.
  128. importc: "fflush", header: "<stdio.h>".}
  129. proc rawWrite*(f: CFilePtr, s: cstring) {.compilerproc, nonReloadable, inline.} =
  130. # we cannot throw an exception here!
  131. discard c_fwrite(s, 1, cast[csize_t](s.len), f)
  132. discard c_fflush(f)
  133. {.pop.}