0.7.12.24
[sbcl.git] / contrib / bsd-sockets / def-to-lisp.lisp
1 (in-package :BSD-SOCKETS-SYSTEM)
2 (defvar *export-symbols* nil)
3
4 (defun c-for-structure (stream lisp-name c-struct)
5   (destructuring-bind (c-name &rest elements) c-struct
6     (format stream "printf(\"(define-c-struct ~A %d)\\n\",sizeof (~A));~%" lisp-name c-name)
7     (dolist (e elements)
8       (destructuring-bind (lisp-type lisp-el-name c-type c-el-name) e
9         (format stream "printf(\"(define-c-accessor ~A-~A ~A ~A \");~%"
10                 lisp-name lisp-el-name lisp-name lisp-type)
11         ;; offset
12         (format stream "{ ~A t;printf(\"%d \",((unsigned long)&(t.~A)) - ((unsigned long)&(t)) ); }~%"
13                 c-name c-el-name)
14         ;; length
15         (format stream "{ ~A t;printf(\"%d\",(sizeof t.~A));}~%"
16                 c-name c-el-name)
17         (format stream "printf(\")\\n\");~%")))))
18
19 (defun c-for-function (stream lisp-name alien-defn)
20   (destructuring-bind (c-name &rest definition) alien-defn
21     (let ((*print-right-margin* nil))
22       (format stream "printf(\"(declaim (inline ~A))\\n\");~%"
23               lisp-name)
24       (princ "printf(\"(def-foreign-routine (" stream)
25       (princ "\\\"" stream) (princ c-name stream) (princ "\\\" " stream)
26       (princ lisp-name stream)
27       (princ " ) " stream)
28       (dolist (d definition)
29         (write d :length nil
30                :right-margin nil :stream stream)
31         (princ " " stream))
32       (format stream ")\\n\");")
33       (terpri stream))))
34
35
36 (defun print-c-source (stream headers definitions package-name)
37   ;(format stream "#include \"struct.h\"~%")
38   (let ((*print-right-margin* nil))
39     (loop for i in headers
40           do (format stream "#include <~A>~%" i))
41     (format stream "main() { ~%
42 printf(\"(in-package ~S)\\\n\");~%" package-name)  
43     (format stream "printf(\"(defconstant size-of-int %d)\\\n\",sizeof (int));~%")
44     (format stream "printf(\"(defconstant size-of-char %d)\\\n\",sizeof (char));~%")
45     (format stream "printf(\"(defconstant size-of-long %d)\\\n\",sizeof (long));~%")
46     (dolist (def definitions)
47       (destructuring-bind (type lispname cname &optional doc) def
48         (cond ((eq type :integer)
49                (format stream
50                        "printf(\"(defconstant ~A %d \\\"~A\\\")\\\n\",~A);~%"
51                        lispname doc cname))
52               ((eq type :string)
53                (format stream
54                        "printf(\"(defvar ~A %S \\\"~A\\\")\\\n\",~A);~%"
55                      lispname doc cname))
56               ((eq type :function)
57                (c-for-function stream lispname cname))
58               ((eq type :structure)
59                (c-for-structure stream lispname cname))
60               (t
61                (format stream
62                        "printf(\";; Non hablo Espagnol, Monsieur~%")))))
63     (format stream "exit(0);~%}")))
64
65 (defun c-constants-extract  (filename output-file package)
66   (with-open-file (f output-file :direction :output)
67     (with-open-file (i filename :direction :input)
68       (let* ((headers (read i))
69              (definitions (read i)))
70         (print-c-source  f headers definitions package)))))