ansi_c.nim 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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):
  47. const
  48. SIGABRT = cint(6)
  49. SIGFPE = cint(8)
  50. SIGILL = cint(4)
  51. SIGINT = cint(2)
  52. SIGSEGV = cint(11)
  53. SIGTERM = cint(15)
  54. SIGPIPE = cint(13)
  55. elif defined(haiku):
  56. const
  57. SIGABRT = cint(6)
  58. SIGFPE = cint(8)
  59. SIGILL = cint(4)
  60. SIGINT = cint(2)
  61. SIGSEGV = cint(11)
  62. SIGTERM = cint(15)
  63. SIGPIPE = cint(7)
  64. else:
  65. when NoFakeVars:
  66. {.error: "SIGABRT not ported to your platform".}
  67. else:
  68. var
  69. SIGINT {.importc: "SIGINT", nodecl.}: cint
  70. SIGSEGV {.importc: "SIGSEGV", nodecl.}: cint
  71. SIGABRT {.importc: "SIGABRT", nodecl.}: cint
  72. SIGFPE {.importc: "SIGFPE", nodecl.}: cint
  73. SIGILL {.importc: "SIGILL", nodecl.}: cint
  74. when defined(macosx) or defined(linux):
  75. var SIGPIPE {.importc: "SIGPIPE", nodecl.}: cint
  76. when defined(macosx):
  77. const SIGBUS = cint(10)
  78. elif defined(haiku):
  79. const SIGBUS = cint(30)
  80. else:
  81. template SIGBUS: untyped = SIGSEGV
  82. when defined(nimSigSetjmp) and not defined(nimStdSetjmp):
  83. proc c_longjmp(jmpb: C_JmpBuf, retval: cint) {.
  84. header: "<setjmp.h>", importc: "siglongjmp".}
  85. template c_setjmp(jmpb: C_JmpBuf): cint =
  86. proc c_sigsetjmp(jmpb: C_JmpBuf, savemask: cint): cint {.
  87. header: "<setjmp.h>", importc: "sigsetjmp".}
  88. c_sigsetjmp(jmpb, 0)
  89. elif defined(nimRawSetjmp) and not defined(nimStdSetjmp):
  90. proc c_longjmp(jmpb: C_JmpBuf, retval: cint) {.
  91. header: "<setjmp.h>", importc: "_longjmp".}
  92. proc c_setjmp(jmpb: C_JmpBuf): cint {.
  93. header: "<setjmp.h>", importc: "_setjmp".}
  94. else:
  95. proc c_longjmp(jmpb: C_JmpBuf, retval: cint) {.
  96. header: "<setjmp.h>", importc: "longjmp".}
  97. proc c_setjmp(jmpb: C_JmpBuf): cint {.
  98. header: "<setjmp.h>", importc: "setjmp".}
  99. type c_sighandler_t = proc (a: cint) {.noconv.}
  100. proc c_signal(sign: cint, handler: proc (a: cint) {.noconv.}): c_sighandler_t {.
  101. importc: "signal", header: "<signal.h>", discardable.}
  102. proc c_fprintf(f: File, frmt: cstring): cint {.
  103. importc: "fprintf", header: "<stdio.h>", varargs, discardable.}
  104. proc c_printf(frmt: cstring): cint {.
  105. importc: "printf", header: "<stdio.h>", varargs, discardable.}
  106. proc c_sprintf(buf, frmt: cstring): cint {.
  107. importc: "sprintf", header: "<stdio.h>", varargs, noSideEffect.}
  108. # we use it only in a way that cannot lead to security issues
  109. when defined(windows):
  110. proc c_fileno(f: File): cint {.
  111. importc: "_fileno", header: "<stdio.h>".}
  112. else:
  113. proc c_fileno(f: File): cint {.
  114. importc: "fileno", header: "<fcntl.h>".}
  115. proc c_malloc(size: csize): pointer {.
  116. importc: "malloc", header: "<stdlib.h>".}
  117. proc c_free(p: pointer) {.
  118. importc: "free", header: "<stdlib.h>".}
  119. proc c_realloc(p: pointer, newsize: csize): pointer {.
  120. importc: "realloc", header: "<stdlib.h>".}
  121. {.pop}