ansi_c.nim 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. proc c_memchr*(s: pointer, c: cint, n: csize_t): pointer {.
  14. importc: "memchr", header: "<string.h>".}
  15. proc c_memcmp*(a, b: pointer, size: csize_t): cint {.
  16. importc: "memcmp", header: "<string.h>", noSideEffect.}
  17. proc c_memcpy*(a, b: pointer, size: csize_t): pointer {.
  18. importc: "memcpy", header: "<string.h>", discardable.}
  19. proc c_memmove*(a, b: pointer, size: csize_t): pointer {.
  20. importc: "memmove", header: "<string.h>",discardable.}
  21. proc c_memset*(p: pointer, value: cint, size: csize_t): pointer {.
  22. importc: "memset", header: "<string.h>", discardable.}
  23. proc c_strcmp*(a, b: cstring): cint {.
  24. importc: "strcmp", header: "<string.h>", noSideEffect.}
  25. proc c_strlen*(a: cstring): csize_t {.
  26. importc: "strlen", header: "<string.h>", noSideEffect.}
  27. proc c_abort*() {.
  28. importc: "abort", header: "<stdlib.h>", noSideEffect, noreturn.}
  29. when defined(linux) and defined(amd64):
  30. type
  31. C_JmpBuf* {.importc: "jmp_buf", header: "<setjmp.h>", bycopy.} = object
  32. abi: array[200 div sizeof(clong), clong]
  33. else:
  34. type
  35. C_JmpBuf* {.importc: "jmp_buf", header: "<setjmp.h>".} = object
  36. type CSighandlerT = proc (a: cint) {.noconv.}
  37. when defined(windows):
  38. const
  39. SIGABRT* = cint(22)
  40. SIGFPE* = cint(8)
  41. SIGILL* = cint(4)
  42. SIGINT* = cint(2)
  43. SIGSEGV* = cint(11)
  44. SIGTERM = cint(15)
  45. SIG_DFL* = cast[CSighandlerT](0)
  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. SIG_DFL* = cast[CSighandlerT](0)
  59. elif defined(haiku):
  60. const
  61. SIGABRT* = cint(6)
  62. SIGFPE* = cint(8)
  63. SIGILL* = cint(4)
  64. SIGINT* = cint(2)
  65. SIGSEGV* = cint(11)
  66. SIGTERM* = cint(15)
  67. SIGPIPE* = cint(7)
  68. SIG_DFL* = cast[CSighandlerT](0)
  69. else:
  70. when defined(nimscript):
  71. {.error: "SIGABRT not ported to your platform".}
  72. else:
  73. var
  74. SIGINT* {.importc: "SIGINT", nodecl.}: cint
  75. SIGSEGV* {.importc: "SIGSEGV", nodecl.}: cint
  76. SIGABRT* {.importc: "SIGABRT", nodecl.}: cint
  77. SIGFPE* {.importc: "SIGFPE", nodecl.}: cint
  78. SIGILL* {.importc: "SIGILL", nodecl.}: cint
  79. SIG_DFL* {.importc: "SIG_DFL", nodecl.}: CSighandlerT
  80. when defined(macosx) or defined(linux):
  81. var SIGPIPE* {.importc: "SIGPIPE", nodecl.}: cint
  82. when defined(macosx):
  83. const SIGBUS* = cint(10)
  84. elif defined(haiku):
  85. const SIGBUS* = cint(30)
  86. when defined(nimSigSetjmp) and not defined(nimStdSetjmp):
  87. proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {.
  88. header: "<setjmp.h>", importc: "siglongjmp".}
  89. template c_setjmp*(jmpb: C_JmpBuf): cint =
  90. proc c_sigsetjmp(jmpb: C_JmpBuf, savemask: cint): cint {.
  91. header: "<setjmp.h>", importc: "sigsetjmp".}
  92. c_sigsetjmp(jmpb, 0)
  93. elif defined(nimRawSetjmp) and not defined(nimStdSetjmp):
  94. proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {.
  95. header: "<setjmp.h>", importc: "_longjmp".}
  96. proc c_setjmp*(jmpb: C_JmpBuf): cint {.
  97. header: "<setjmp.h>", importc: "_setjmp".}
  98. else:
  99. proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {.
  100. header: "<setjmp.h>", importc: "longjmp".}
  101. proc c_setjmp*(jmpb: C_JmpBuf): cint {.
  102. header: "<setjmp.h>", importc: "setjmp".}
  103. proc c_signal*(sign: cint, handler: CSighandlerT): CSighandlerT {.
  104. importc: "signal", header: "<signal.h>", discardable.}
  105. proc c_raise*(sign: cint): cint {.importc: "raise", header: "<signal.h>".}
  106. type
  107. CFile {.importc: "FILE", header: "<stdio.h>",
  108. incompleteStruct.} = object
  109. CFilePtr* = ptr CFile ## The type representing a file handle.
  110. # duplicated between io and ansi_c
  111. const stdioUsesMacros = (defined(osx) or defined(freebsd) or defined(dragonfly)) and not defined(emscripten)
  112. const stderrName = when stdioUsesMacros: "__stderrp" else: "stderr"
  113. const stdoutName = when stdioUsesMacros: "__stdoutp" else: "stdout"
  114. const stdinName = when stdioUsesMacros: "__stdinp" else: "stdin"
  115. var
  116. cstderr* {.importc: stderrName, header: "<stdio.h>".}: CFilePtr
  117. cstdout* {.importc: stdoutName, header: "<stdio.h>".}: CFilePtr
  118. cstdin* {.importc: stdinName, header: "<stdio.h>".}: CFilePtr
  119. proc c_fprintf*(f: CFilePtr, frmt: cstring): cint {.
  120. importc: "fprintf", header: "<stdio.h>", varargs, discardable.}
  121. proc c_printf*(frmt: cstring): cint {.
  122. importc: "printf", header: "<stdio.h>", varargs, discardable.}
  123. proc c_fputs*(c: cstring, f: CFilePtr): cint {.
  124. importc: "fputs", header: "<stdio.h>", discardable.}
  125. proc c_sprintf*(buf, frmt: cstring): cint {.
  126. importc: "sprintf", header: "<stdio.h>", varargs, noSideEffect.}
  127. # we use it only in a way that cannot lead to security issues
  128. proc c_malloc*(size: csize_t): pointer {.
  129. importc: "malloc", header: "<stdlib.h>".}
  130. proc c_calloc*(nmemb, size: csize_t): pointer {.
  131. importc: "calloc", header: "<stdlib.h>".}
  132. proc c_free*(p: pointer) {.
  133. importc: "free", header: "<stdlib.h>".}
  134. proc c_realloc*(p: pointer, newsize: csize_t): pointer {.
  135. importc: "realloc", header: "<stdlib.h>".}
  136. proc c_fwrite*(buf: pointer, size, n: csize_t, f: CFilePtr): cint {.
  137. importc: "fwrite", header: "<stdio.h>".}
  138. proc c_fflush(f: CFilePtr): cint {.
  139. importc: "fflush", header: "<stdio.h>".}
  140. proc rawWriteString*(f: CFilePtr, s: cstring, length: int) {.compilerproc, nonReloadable, inline.} =
  141. # we cannot throw an exception here!
  142. discard c_fwrite(s, 1, cast[csize_t](length), f)
  143. discard c_fflush(f)
  144. proc rawWrite*(f: CFilePtr, s: cstring) {.compilerproc, nonReloadable, inline.} =
  145. # we cannot throw an exception here!
  146. discard c_fwrite(s, 1, cast[csize_t](s.len), f)
  147. discard c_fflush(f)
  148. {.pop.}