| 1 |
#! ./scmlit \ |
| 2 |
- !# |
| 3 |
;;;; "inc2scm", Convert numeric C #defines to Scheme definitions. |
| 4 |
;; Copyright (C) 1991-1999 Free Software Foundation, Inc. |
| 5 |
;; |
| 6 |
;; This program is free software: you can redistribute it and/or modify |
| 7 |
;; it under the terms of the GNU Lesser General Public License as |
| 8 |
;; published by the Free Software Foundation, either version 3 of the |
| 9 |
;; License, or (at your option) any later version. |
| 10 |
;; |
| 11 |
;; This program is distributed in the hope that it will be useful, but |
| 12 |
;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
;; Lesser General Public License for more details. |
| 15 |
;; |
| 16 |
;; You should have received a copy of the GNU Lesser General Public |
| 17 |
;; License along with this program. If not, see |
| 18 |
;; <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
;;; Author: Aubrey Jaffer. |
| 21 |
|
| 22 |
(define (inc2scm.script args) |
| 23 |
(cond ((< 1 (length args)) |
| 24 |
(apply scm<-usr/includes args)) |
| 25 |
(else (inc2scm.usage)))) |
| 26 |
|
| 27 |
(define (inc2scm.usage) |
| 28 |
(display "\ |
| 29 |
\ |
| 30 |
Usage: inc2scm defines.scm [pre:] [/usr/include/] file1.h file2.h ... |
| 31 |
\ |
| 32 |
Appends to DEFINES.SCM the Scheme translations of the numeric |
| 33 |
#define statements in /USR/INCLUDE/FILE1.H, /USR/INCLUDE/FILE2.H, ... |
| 34 |
|
| 35 |
PRE: is prepended to those scheme names lacking a prefix. |
| 36 |
|
| 37 |
/USR/INCLUDE/ defaults to /usr/include/. |
| 38 |
|
| 39 |
http://people.csail.mit.edu/jaffer/SCM |
| 40 |
" |
| 41 |
(current-error-port)) |
| 42 |
#f) |
| 43 |
|
| 44 |
(require 'string-search) |
| 45 |
(require 'printf) |
| 46 |
(require 'scanf) |
| 47 |
|
| 48 |
(define (StudlyCaps->dashed-name nstr) |
| 49 |
(do ((idx (+ -1 (string-length nstr)) (+ -1 idx))) |
| 50 |
((> 2 idx)) |
| 51 |
(cond ((and (char-upper-case? (string-ref nstr (+ -1 idx))) |
| 52 |
(char-lower-case? (string-ref nstr idx))) |
| 53 |
(set! nstr |
| 54 |
(string-append (substring nstr 0 (+ -1 idx)) |
| 55 |
"-" |
| 56 |
(substring nstr (+ -1 idx) |
| 57 |
(string-length nstr))))) |
| 58 |
((and (char-lower-case? (string-ref nstr (+ -1 idx))) |
| 59 |
(char-upper-case? (string-ref nstr idx))) |
| 60 |
(set! nstr |
| 61 |
(string-append (substring nstr 0 idx) |
| 62 |
"-" |
| 63 |
(substring nstr idx |
| 64 |
(string-length nstr))))))) |
| 65 |
nstr) |
| 66 |
|
| 67 |
;; SCHEMEIFY-NAME: |
| 68 |
;; * Changes _ to - |
| 69 |
;; * Changes the first - to : if it is within the first 3 characters. |
| 70 |
;; * inserts dashes between `StudlyCaps' |
| 71 |
|
| 72 |
(define (schemeify-name pre name) |
| 73 |
(define nstr (string-subst name "_" "-")) |
| 74 |
(let ((sid (string-index nstr #\-))) |
| 75 |
(cond ((and sid (< sid 3)) (string-set! nstr sid #\:) |
| 76 |
nstr) |
| 77 |
(pre (string-append pre (StudlyCaps->dashed-name nstr))) |
| 78 |
(else (StudlyCaps->dashed-name nstr))))) |
| 79 |
|
| 80 |
(define (extract-defineds port) |
| 81 |
(define sharp (string #\newline #\#)) |
| 82 |
(define defineds '()) |
| 83 |
(do ((find? (find-string-from-port? sharp port) |
| 84 |
(find-string-from-port? sharp port))) |
| 85 |
((not find?) (reverse defineds)) |
| 86 |
(do ((chr (read-char port) (read-char port))) |
| 87 |
((or (eof-object? chr) (not (char-whitespace? chr))) |
| 88 |
(and (eqv? chr #\d) |
| 89 |
(let ((op #f) (va #f)) |
| 90 |
(fscanf port "efine%*[ \t]%s%*[ \t]%s" op va) |
| 91 |
(if (and op va |
| 92 |
(not (string-index op #\()) |
| 93 |
(not (eqv? #\_ (string-ref op 0))) |
| 94 |
(not (equal? "int" va))) |
| 95 |
(set! defineds (cons op defineds))))))))) |
| 96 |
|
| 97 |
(define (scm<-includes scmname pre non-local? . filenames) |
| 98 |
(define tmpprog "tmpprog") |
| 99 |
(call-with-output-file (string-append tmpprog ".c") |
| 100 |
(lambda (cport) |
| 101 |
(for-each (lambda (filename) |
| 102 |
(fprintf cport |
| 103 |
(if non-local? |
| 104 |
"#include <%s>\\n" |
| 105 |
"#include \"%s\"\\n") |
| 106 |
filename)) |
| 107 |
filenames) |
| 108 |
(for-each |
| 109 |
(lambda (args) (apply fprintf cport args)) |
| 110 |
`(("#include <stdio.h>\\n") |
| 111 |
("void pSl(sname, value)\\n") |
| 112 |
(" char sname[];\\n") |
| 113 |
(" int value;\\n") |
| 114 |
("{\\n") |
| 115 |
("%s\\n" " printf(\"(define %s %d)\\n\", sname, value);") |
| 116 |
("}\\n") |
| 117 |
("\\n") |
| 118 |
("int main(argc, argv)\\n") |
| 119 |
(" int argc;\\n") |
| 120 |
(" char *argv[];\\n") |
| 121 |
("{\\n") |
| 122 |
)) |
| 123 |
(for-each |
| 124 |
(lambda (filename) |
| 125 |
(if non-local? |
| 126 |
(set! filename (string-append non-local? filename))) |
| 127 |
(fprintf cport "/* Extract #define values from %s */\\n" filename) |
| 128 |
(fprintf cport "%s %s%s\\n" |
| 129 |
" printf(\";;inc2scm extracted #define values from" |
| 130 |
filename |
| 131 |
"\\n\");") |
| 132 |
(for-each |
| 133 |
(lambda (name) |
| 134 |
(fprintf cport "#ifdef %s\\n pSl(\"%s\", %s);\\n#endif\\n" |
| 135 |
name (schemeify-name pre name) name)) |
| 136 |
(call-with-input-file filename extract-defineds))) |
| 137 |
filenames) |
| 138 |
(fprintf cport "}\\n"))) |
| 139 |
(cond |
| 140 |
((not (zero? (system (sprintf #f "cc -o %s %s.c" tmpprog tmpprog))))) |
| 141 |
((not (zero? (system (sprintf #f "./%s >> %s" tmpprog scmname))))))) |
| 142 |
|
| 143 |
(define (scm<-usr/includes scmname . filenames) |
| 144 |
(define pre (let ((first (car filenames))) |
| 145 |
(cond ((substring-ci? ".h" first) #f) |
| 146 |
(else (set! filenames (cdr filenames)) first)))) |
| 147 |
(define include-path "/usr/include/") |
| 148 |
(let* ((first (car filenames))) |
| 149 |
(cond ((memv (string-ref first (+ -1 (string-length first))) '(#\\ #\/)) |
| 150 |
(set! include-path first) |
| 151 |
(set! filenames (cdr filenames))))) |
| 152 |
(apply scm<-includes scmname pre include-path filenames) |
| 153 |
(delete-file "tmpprog.c") |
| 154 |
(delete-file "tmpprog")) |
| 155 |
|
| 156 |
(define (scm<-h* scmname . filenames) |
| 157 |
(define pre (let ((first (car filenames))) |
| 158 |
(cond ((substring-ci? ".h" first) first) |
| 159 |
(else (set! filenames (cdr filenames)) #f)))) |
| 160 |
(apply scm<-includes scmname pre #f filenames) |
| 161 |
(delete-file "tmpprog.c") |
| 162 |
(delete-file "tmpprog")) |
| 163 |
(define h2scm scm<-h*) |
| 164 |
|
| 165 |
;;; Local Variables: |
| 166 |
;;; mode:scheme |
| 167 |
;;; End: |
| 168 |
(exit (inc2scm.script (list-tail *argv* *optind*))) |