1 (in-package :SB-BSD-SOCKETS-SYSTEM)
2 (defvar *export-symbols* nil)
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)
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)
12 (format stream "{ ~A t;printf(\"%d \",((unsigned long)&(t.~A)) - ((unsigned long)&(t)) ); }~%"
15 (format stream "{ ~A t;printf(\"%d\",(sizeof t.~A));}~%"
17 (format stream "printf(\")\\n\");~%")))))
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\");~%"
24 (princ "printf(\"(def-foreign-routine (" stream)
25 (princ "\\\"" stream) (princ c-name stream) (princ "\\\" " stream)
26 (princ lisp-name stream)
28 (dolist (d definition)
30 :right-margin nil :stream stream)
32 (format stream ")\\n\");")
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)
50 "printf(\"(defconstant ~A %d \\\"~A\\\")\\\n\",~A);~%"
54 "printf(\"(defvar ~A %S \\\"~A\\\")\\\n\",~A);~%"
57 (c-for-function stream lispname cname))
59 (c-for-structure stream lispname cname))
62 "printf(\";; Non hablo Espagnol, Monsieur~%")))))
63 (format stream "exit(0);~%}")))
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)))))