summary refs log tree commit diff stats
path: root/lib/ansi_c.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <andreas@andi>2008-06-22 16:14:11 +0200
committerAndreas Rumpf <andreas@andi>2008-06-22 16:14:11 +0200
commit405b86068e6a3d39970b9129ceec0a9108464b28 (patch)
treec0449946f54baae6ea88baf453157ddd7faa8f86 /lib/ansi_c.nim
downloadNim-405b86068e6a3d39970b9129ceec0a9108464b28.tar.gz
Initial import
Diffstat (limited to 'lib/ansi_c.nim')
-rwxr-xr-xlib/ansi_c.nim84
1 files changed, 84 insertions, 0 deletions
diff --git a/lib/ansi_c.nim b/lib/ansi_c.nim
new file mode 100755
index 000000000..e667822a9
--- /dev/null
+++ b/lib/ansi_c.nim
@@ -0,0 +1,84 @@
+#
+#
+#            Nimrod's Runtime Library
+#        (c) Copyright 2006 Andreas Rumpf
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+# This include file contains headers of Ansi C procs
+# and definitions of Ansi C types in Nimrod syntax
+# All symbols are prefixed with 'c_' to avoid ambiguities
+
+proc c_strcmp(a, b: CString): cint {.nodecl, importc: "strcmp".}
+proc c_memcmp(a, b: CString, size: cint): cint {.nodecl, importc: "memcmp".}
+proc c_memcpy(a, b: CString, size: cint) {.nodecl, importc: "memcpy".}
+proc c_strlen(a: CString): cint {.nodecl, importc: "strlen".}
+
+type
+  C_TextFile {.importc: "FILE", nodecl.} = record   # empty record for
+                                                   # data hiding
+  C_BinaryFile {.importc: "FILE", nodecl.} = record
+  C_TextFileStar = ptr CTextFile
+  C_BinaryFileStar = ptr CBinaryFile
+
+  C_JmpBuf {.importc: "jmp_buf".} = array[0..31, int]
+
+var
+  c_stdin {.importc: "stdin", noDecl.}: C_TextFileStar
+  c_stdout {.importc: "stdout", noDecl.}: C_TextFileStar
+  c_stderr {.importc: "stderr", noDecl.}: C_TextFileStar
+
+var # constants faked as variables:
+  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):
+  var
+    SIGBUS {.importc: "SIGBUS", nodecl.}: cint
+      # hopefully this does not lead to new bugs
+else:
+  var
+    SIGBUS {.importc: "SIGSEGV", nodecl.}: cint
+      # only Mac OS X has this shit
+
+proc c_longjmp(jmpb: C_JmpBuf, retval: cint) {.nodecl, importc: "longjmp".}
+proc c_setjmp(jmpb: var C_JmpBuf) {.nodecl, importc: "setjmp".}
+
+proc c_signal(sig: cint, handler: proc (a: cint) {.noconv.}) {.
+  importc: "signal", header: "<signal.h>".}
+proc c_raise(sig: cint) {.importc: "raise", header: "<signal.h>".}
+
+proc c_fputs(c: cstring, f: C_TextFileStar) {.importc: "fputs", noDecl.}
+proc c_fgets(c: cstring, n: int, f: C_TextFileStar): cstring  {.
+  importc: "fgets", noDecl.}
+proc c_fgetc(stream: C_TextFileStar): int {.importc: "fgetc", nodecl.}
+proc c_ungetc(c: int, f: C_TextFileStar) {.importc: "ungetc", nodecl.}
+proc c_putc(c: Char, stream: C_TextFileStar) {.importc: "putc", nodecl.}
+proc c_fprintf(f: C_TextFileStar, frmt: CString) {.
+  importc: "fprintf", nodecl, varargs.}
+
+proc c_fopen(filename, mode: cstring): C_TextFileStar {.
+  importc: "fopen", nodecl.}
+proc c_fclose(f: C_TextFileStar) {.importc: "fclose", nodecl.}
+
+proc c_sprintf(buf, frmt: CString) {.nodecl, importc: "sprintf", varargs.}
+  # we use it only in a way that cannot lead to security issues
+
+proc c_fread(buf: Pointer, size, n: int, f: C_BinaryFileStar): int {.
+  importc: "fread", noDecl.}
+proc c_fseek(f: C_BinaryFileStar, offset: clong, whence: int): int {.
+  importc: "fseek", noDecl.}
+
+proc c_fwrite(buf: Pointer, size, n: int, f: C_BinaryFileStar): int {.
+  importc: "fwrite", noDecl.}
+
+proc c_exit(errorcode: cint) {.importc: "exit", nodecl.}
+proc c_ferror(stream: C_TextFileStar): bool {.importc: "ferror", nodecl.}
+proc c_fflush(stream: C_TextFileStar) {.importc: "fflush", nodecl.}
+proc c_abort() {.importc: "abort", nodecl.}
+proc c_feof(stream: C_TextFileStar): bool {.importc: "feof", nodecl.}