+(defvar *default-c-stream* nil)
+
+(defun escape-for-string (string)
+ (c-escape string))
+
+(defun split-cflags (string)
+ (remove-if (lambda (flag)
+ (zerop (length flag)))
+ (loop
+ for start = 0 then (if end (1+ end) nil)
+ for end = (and start (position #\Space string :start start))
+ while start
+ collect (subseq string start end))))
+
+(defun c-escape (string &optional (dangerous-chars '(#\")) (escape-char #\\))
+ "Escape DANGEROUS-CHARS in STRING, with ESCAPE-CHAR."
+ (declare (simple-string string))
+ (coerce (loop for c across string
+ if (member c dangerous-chars) collect escape-char
+ collect c)
+ 'string))
+
+(defun as-c (&rest args)
+ "Pretty-print ARGS into the C source file, separated by #\Space"
+ (format *default-c-stream* "~A~{ ~A~}~%" (first args) (rest args)))
+
+(defun printf (formatter &rest args)
+ "Emit C code to fprintf the quoted code, via FORMAT.
+The first argument is the C string that should be passed to
+printf.
+
+The rest of the arguments are consumed by FORMAT clauses, until
+there are no more FORMAT clauses to fill. If there are more
+arguments, they are emitted as printf arguments.
+
+There is no error checking done, unless you pass too few FORMAT
+clause args. I recommend using this formatting convention in
+code:
+
+ (printf \"string ~A ~S %d %d\" format-arg-1 format-arg-2
+ printf-arg-1 printf-arg-2)"
+ (let ((*print-pretty* nil))
+ (apply #'format *default-c-stream*
+ " fprintf (out, \"~@?\\n\"~@{, ~A~});~%"
+ (c-escape formatter)
+ args)))
+
+(defun c-for-enum (lispname elements export)
+ (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-alien:define-alien-type ~A (sb-alien:enum nil" lispname)
+ (dolist (element elements)
+ (destructuring-bind (lisp-element-name c-element-name) element
+ (printf " (~S %d)" lisp-element-name c-element-name)))
+ (printf ")))")
+ (when export
+ (dolist (element elements)
+ (destructuring-bind (lisp-element-name c-element-name) element
+ (declare (ignore c-element-name))
+ (unless (keywordp lisp-element-name)
+ (printf "(export '~S)" lisp-element-name))))))
+
+(defun c-for-structure (lispname cstruct)
+ (destructuring-bind (cname &rest elements) cstruct
+ (printf "(cl:eval-when (:compile-toplevel :load-toplevel :execute) (sb-grovel::define-c-struct ~A %d" lispname
+ (format nil "sizeof(~A)" cname))