-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathffi.ss
66 lines (56 loc) · 1.62 KB
/
ffi.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
;; -*- Gerbil -*-
;;;; FFI to standard C libraries
;; TODO:
;; * Make it a complete set of trivial wrappers around libc functions.
;; Automate import or at least steal the wrappers from another language implementation.
;; * Make it portable to Linux, Windows, macOS, FreeBSD, OpenBSD.
;; define and register stubs for functions not present.
;; * Write tests
;;
;;; XXX vyzo: kill/getpid are candidates for stdlib integration; perhaps std/os/process
(export #t)
(import
:gerbil/gambit
:std/misc/process :std/sugar
./base)
(begin-foreign
(c-declare #<<END-C
#define __USE_GNU 1
#include <unistd.h>
#include <signal.h>
#include <errno.h>
END-C
)
(define-macro (define-c-lambda id args ret #!optional (name #f))
(let ((name (or name (##symbol->string id))))
`(define ,id
(c-lambda ,args ,ret ,name))))
(define-macro (define-with-errno symbol ffi-symbol args)
`(define (,symbol ,@args)
(declare (not interrupts-enabled))
(let ((r (,ffi-symbol ,@args)))
(if (##fx< r 0)
(##fx- (__errno))
r))))
(define-c-lambda __errno () int
"___return (errno);")
(namespace ("clan/ffi#"
__kill _kill getpid
))
(define-c-lambda __kill (int int) int
"kill")
(define-with-errno _kill __kill (pid sig))
(define-c-lambda getpid () int
"getpid")
)
(extern _kill getpid)
(def (kill pid (sig SIGTERM)) (_kill pid sig))
;; Portable POSIX signal numbers according to https://en.wikipedia.org/wiki/Signal_(IPC)
(def SIGHUP 1)
(def SIGINT 2)
(def SIGQUIT 3)
(def SIGTRAP 5)
(def SIGABRT 6)
(def SIGKILL 9)
(def SIGALRM 14)
(def SIGTERM 15)