ansi_c.nim 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 include file 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}
  13. proc c_memchr(s: pointer, c: cint, n: csize): pointer {.
  14. importc: "memchr", header: "<string.h>".}
  15. proc c_memcmp(a, b: pointer, size: csize): cint {.
  16. importc: "memcmp", header: "<string.h>", noSideEffect.}
  17. proc c_memcpy(a, b: pointer, size: csize): pointer {.
  18. importc: "memcpy", header: "<string.h>", discardable.}
  19. proc c_memmove(a, b: pointer, size: csize): pointer {.
  20. importc: "memmove", header: "<string.h>",discardable.}
  21. proc c_memset(p: pointer, value: cint, size: csize): 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 {.
  26. importc: "strlen", header: "<string.h>", noSideEffect.}
  27. proc c_abort() {.
  28. importc: "abort", header: "<stdlib.h>", noSideEffect.}
  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. when defined(windows):
  37. const
  38. SIGABRT = cint(22)
  39. SIGFPE = cint(8)
  40. SIGILL = cint(4)
  41. SIGINT = cint(2)
  42. SIGSEGV = cint(11)
  43. SIGTERM = cint(15)
  44. elif defined(macosx) or defined(linux) or defined(freebsd) or
  45. defined(openbsd) or defined(netbsd) or defined(solaris) or
  46. defined(dragonfly) or defined(nintendoswitch) or defined(genode) or
  47. defined(aix) or hostOS == "standalone":
  48. const
  49. SIGABRT = cint(6)
  50. SIGFPE = cint(8)
  51. SIGILL = cint(4)
  52. SIGINT = cint(2)
  53. SIGSEGV = cint(11)
  54. SIGTERM = cint(15)
  55. SIGPIPE = cint(13)
  56. elif defined(haiku):
  57. const
  58. SIGABRT = cint(6)
  59. SIGFPE = cint(8)
  60. SIGILL = cint(4)
  61. SIGINT = cint(2)
  62. SIGSEGV = cint(11)
  63. SIGTERM = cint(15)
  64. SIGPIPE = cint(7)
  65. else:
  66. when NoFakeVars:
  67. {.error: "SIGABRT not ported to your platform".}
  68. else:
  69. var
  70. SIGINT {.importc: "SIGINT", nodecl.}: cint
  71. SIGSEGV {.importc: "SIGSEGV", nodecl.}: cint
  72. SIGABRT {.importc: "SIGABRT", nodecl.}: cint
  73. SIGFPE {.importc: "SIGFPE", nodecl.}: cint
  74. SIGILL {.importc: "SIGILL", nodecl.}: cint
  75. when defined(macosx) or defined(linux):
  76. var SIGPIPE {.importc: "SIGPIPE", nodecl.}: cint
  77. when defined(macosx):
  78. const SIGBUS = cint(10)
  79. elif defined(haiku):
  80. const SIGBUS = cint(30)
  81. else:
  82. template SIGBUS: untyped = SIGSEGV
  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 c_sighandler_t = proc (a: cint) {.noconv.}
  101. proc c_signal(sign: cint, handler: proc (a: cint) {.noconv.}): c_sighandler_t {.
  102. importc: "signal", header: "<signal.h>", discardable.}
  103. proc c_fprintf(f: File, frmt: cstring): cint {.
  104. importc: "fprintf", header: "<stdio.h>", varargs, discardable.}
  105. proc c_printf(frmt: cstring): cint {.
  106. importc: "printf", header: "<stdio.h>", varargs, discardable.}
  107. proc c_sprintf(buf, frmt: cstring): cint {.
  108. importc: "sprintf", header: "<stdio.h>", varargs, noSideEffect.}
  109. # we use it only in a way that cannot lead to security issues
  110. when defined(windows):
  111. proc c_fileno(f: File): cint {.
  112. importc: "_fileno", header: "<stdio.h>".}
  113. else:
  114. proc c_fileno(f: File): cint {.
  115. importc: "fileno", header: "<fcntl.h>".}
  116. proc c_malloc(size: csize): pointer {.
  117. importc: "malloc", header: "<stdlib.h>".}
  118. proc c_free(p: pointer) {.
  119. importc: "free", header: "<stdlib.h>".}
  120. proc c_realloc(p: pointer, newsize: csize): pointer {.
  121. importc: "realloc", header: "<stdlib.h>".}
  122. {.pop}