summaryrefslogtreecommitdiffstats
path: root/xgen.scm
blob: 870c98be6a588bfe6bd4578f18586bb2db3b6671 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#! /usr/bin/scm \ %0 %*
- !#
;;;; "xgen.scm", Convert C Event structs to xevent.h and xevent.scm.
;; Copyright (C) 1991-2000 Free Software Foundation, Inc.
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Lesser General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with this program.  If not, see
;; <http://www.gnu.org/licenses/>.

;;; Author: Aubrey Jaffer.

(define (xgen.scm args)
  (cond ((= 1 (length args))
	 (xatoms)
	 (apply xgen args)
	 #t)
	(else (xgen.usage))))

(define (xgen.usage)
  (display "\
\
Usage: xgen.scm /usr/include/X11/Xlib.h
\
  Creates xevent.h and xevent.scm, from the `typedef struct's
  in /usr/include/X11/xlib.h.

http://swiss.csail.mit.edu/~jaffer/SCM
"
	   (current-error-port))
  #f)

(require 'common-list-functions)
(require 'string-search)
(require 'string-case)
(require 'line-i/o)
(require 'printf)
(require 'scanf)

(define progname (if (defined? *optind*)
		     (list-ref *argv* (+ -1 *optind*))
		     (car (program-arguments))))

;; SCHEMEIFY-NAME:
;; * Changes _ to -
;; * Changes the first - to : if it is within the first 3 characters.
;; * inserts dashes between `StudlyCaps'

(define (schemeify-name pre name)
  (define nstr (string-subst name "_" "-"))
  (let ((sid (string-index nstr #\-)))
    (cond ((and pre sid (< sid 3)) (string-set! nstr sid #\:)
	   nstr)
	  (pre (string-append pre (StudlyCapsExpand nstr)))
	  (else (StudlyCapsExpand nstr)))))

(define (extract-structs port)
  (define typedef-struct (string-append (string #\newline) "typedef struct {"))
  (define structs '())
  (do ((find? (find-string-from-port? typedef-struct port)
	      (find-string-from-port? typedef-struct port)))
      ((not find?) (reverse structs))
    (set! structs (cons (extract-struct port) structs))))

(define (extract-struct port)
  (define elts '())
  (do ((typ (read-token port) (read-token port)))
      ((or (eof-object? typ) (eq? #\} typ))
       (let ((name (read-token port)))
	 (let ((chr (read-token port)))
	   (cond ((eqv? #\; chr))
		 (else (slib:error 'expected #\; 'but-read chr)))
	   (cons name (reverse elts)))))
    (letrec ((loop
	      (lambda (name)
		;;(print 'typ= typ 'name= name)
		(case name
		  ((#\*)
		   (case (string->symbol typ)
		     ((char) (set! typ "string"))
		     (else (set! typ (string-append typ "*"))))
		   (loop (read-token port)))
		  (else
		   (let loop2 ((chr (read-token port)))
		     (case chr
		       ((#\;)
			(set! elts (cons (list typ name) elts)))
		       ((#\,)
			(set! elts (cons (list typ name) elts))
			(loop (read-token port)))
		       ((#\[)
			(find-string-from-port? "]" port)
			(case (string->symbol typ)
			  ((char) (set! typ "string"))
			  (else (set! typ (string-append typ "*"))))
			(loop2 (read-token port)))
		       (else (slib:error 'expected #\; 'read chr))))))
		)))
      (case (string->symbol typ)
	((unsigned)
	 (set! typ (read-token port))
	 (case (string->symbol typ)
	   ((long short char int) (set! typ "int")
	    (loop (read-token port)))
	   (else (loop typ))))
	((struct)
	 (set! typ (read-token port))
	 (loop (read-token port)))
	((union)
	 (find-string-from-port? close-brace-string port)
	 ;;(set! typ "union")
	 (loop (read-token port)))
	(else (loop (read-token port)))))))

(define close-brace-string (string #\}))

(define (read-token port)
  (let ((chr (peek-char port)))
    (cond ((eqv? chr #\newline)
	   (read-char port)
	   (do ((fchr (peek-char port) (peek-char port)))
	       ((not (eqv? #\# fchr)))
	     (read-char port)
	     (if (eq? 'if (read port))
		 (do ((fchr (peek-char port) (peek-char port)))
		     ((eqv? #\# fchr))
		   (read-line port)))
	     (read-line port))
	   (read-token port))
	  ((char-whitespace? chr)
	   (read-char port)
	   (read-token port))
	  ((eqv? #\/ chr)
	   (cond ((and (find-string-from-port? "/*" port)
		       (find-string-from-port? "*/" port
					       ;;(lambda (chr) (display chr) #f)
					       ))
		  ;;(newline)
		  (read-token port))
		 (else
		  (slib:error 'botched-comment (read-line port)))))
	  ((or (char-alphabetic? chr) (eqv? #\_ chr))
	   (car (scanf-read-list "%[a-zA-Z_0-9]" port)))
	  ;;((string-index "[]*" chr) (string->symbol (string chr)))
	  (else (read-char port)))))

(defconst Bool (string->symbol "Bool"))
(defconst Time (string->symbol "Time"))

(define event-map
  '(
    ("XMotionEvent"		"MotionNotify")
    ("XKeyEvent"		"KeyPress"	"KeyRelease")
    ("XButtonEvent"		"ButtonPress"	"ButtonRelease")
    ("XPointerMovedEvent"	"MotionNotify")
    ("XCrossingEvent"		"EnterNotify"	"LeaveNotify")
    ("XFocusChangeEvent"	"FocusIn"	"FocusOut")
    ("XKeymapEvent"		"KeymapNotify")
    ("XExposeEvent"		"Expose")
    ("XGraphicsExposeEvent"	"GraphicsExpose")
    ("XNoExposeEvent"		"NoExpose")
    ("XVisibilityEvent"		"VisibilityNotify")
    ("XCreateWindowEvent"	"CreateNotify")
    ("XDestroyWindowEvent"	"DestroyNotify")
    ("XUnmapEvent"		"UnmapNotify")
    ("XMapEvent"		"MapNotify")
    ("XMapRequestEvent"		"MapRequest")
    ("XReparentEvent"		"ReparentNotify")
    ("XConfigureEvent"		"ConfigureNotify")
    ("XConfigureRequestEvent"	"ConfigureRequest")
    ("XGravityEvent"		"GravityNotify")
    ("XResizeRequestEvent"	"ResizeRequest")
    ("XCirculateEvent"		"CirculateNotify")
    ("XCirculateRequestEvent"	"CirculateRequest")
    ("XPropertyEvent"		"PropertyNotify")
    ("XSelectionClearEvent"	"SelectionClear")
    ("XSelectionRequestEvent"	"SelectionRequest")
    ("XSelectionEvent"		"SelectionNotify")
    ("XColormapEvent"		"ColormapNotify")
    ("XClientMessageEvent"	"ClientMessage")
    ("XMappingEvent"		"MappingNotify")
    ))

(define event-fields '())
(define event-field-idx #x10)
(define (do-field xevent.scm fname)
  (define apr (assoc fname event-fields))
  (cond (apr (cdr apr))
	(else
	 (set! event-fields (acons fname event-field-idx event-fields))
	 (fprintf xevent.scm "(define X-event:%s #x%02x)\n"
		  (schemeify-name #f fname)
		  event-field-idx)
	 (set! event-field-idx (+ 1 event-field-idx))
	 (+ -1 event-field-idx))))

(define (xgen filename)
  (let ((structs (remove-if-not
		  (lambda (struct) (substring? "Event" (car struct)))
		  (call-with-input-file filename extract-structs))))
    (call-with-open-ports
     (open-file "xevent.h" "w")
     (open-file "xevent.scm" "w")
     (lambda (xevent.h xevent.scm)
       (define evs #f)
       (fprintf xevent.h "/* %s extracted typedef structs from %s */\n"
		progname filename)
       (fprintf xevent.h
		"#ifdef SCM_EVENT_FIELDS\n")
       (fprintf xevent.scm ";; %s extracted typedef structs from %s\n"
		progname filename)
       (for-each
	(lambda (struct)
	  (define name (car struct))
	  (set! evs (assoc name event-map))
	  (and
	   evs
	   (for-each
	    (lambda (decl)
	      (define typ (string->symbol (car decl)))
	      (qase typ
		((,Bool ,Time int char)
		 (fprintf xevent.h "  ")
		 (for-each (lambda (event-name)
			     (fprintf xevent.h "case (%s<<8)+0x%02x: "
				      event-name
				      (do-field xevent.scm (cadr decl))))
			   (cdr evs))
		 (fprintf xevent.h "return %s(((%s *) x)->%s);\n"
			  (qase typ
			    ((,Bool) "x_make_bool")
			    ((,Time) "ulong2num")
			    ((int char) "MAKINUM"))
			  name
			  (cadr decl)))
		;;(else (print 'typ typ))
		))
	    (cdr struct))))
	structs)
       (fprintf xevent.h "#else\n")
       (for-each (lambda (apr)
		   (for-each (lambda (evnt)
			       (fprintf xevent.h
					"  {%-20s \"%s\"},\n"
					(string-append evnt ",") evnt))
			     (cdr apr)))
		 event-map)
       (fprintf xevent.h "#endif\n")))))

(define (xatoms)
  (define /usr/include/X11/Xatom.h "/usr/include/X11/Xatom.h")
  (define /usr/include/X11/Xcms.h "/usr/include/X11/Xcms.h")
  (call-with-open-ports
   (open-file /usr/include/X11/Xatom.h "r")
   (open-file "/usr/include/X11/Xcms.h" "r")
   (open-file "xatoms.scm" "w")
   (lambda (xatom.h xcms.h xatoms.scm)
     (fprintf xatoms.scm ";; %s extracted definitions from %s\n"
	      progname /usr/include/X11/Xatom.h)
     (do ((line (read-line xatom.h) (read-line xatom.h)))
	 ((eof-object? line))
       (let ((lst (scanf-read-list "#define XA_%s ((Atom) %d)" line)))
	 (and (list? lst)
	      (case (length lst)
		((2) (fprintf xatoms.scm "(define %s %d)\n"
			      (string-subst (car lst) "_" "-")
			      (cadr lst)))
		((0) #f)		;(write-line line)
		(else (slib:error 'xatom.h 'line line))))))
     (fprintf xatoms.scm ";; %s extracted definitions from %s\n"
	      progname /usr/include/X11/Xcms.h)
     (do ((line (read-line xcms.h) (read-line xcms.h)))
	 ((eof-object? line))
       (let ((lst (scanf-read-list "#define Xcms%s (XcmsColorFormat)0x%4x%4x"
				   line)))
	 (and (list? lst)
	      (case (length lst)
		((3) (apply fprintf xatoms.scm "(define\tX:%s\t#x%04x%04x)\n"
			    (string-subst (car lst) "Format" "")
			    (cdr lst)))
		((2) (fprintf xatoms.scm "(define\tX:%s\t#x%08x)\n"
			      (string-subst (car lst) "Format" "")
			      (cadr lst)))
		((0 1) #f)
		(else (slib:error 'xcms.h 'line line)))))))))

;;; Local Variables:
;;; mode:scheme
;;; End:
(exit (xgen.scm (list-tail *argv* *optind*))) ;(and *script* )