Debian Codegraph @

contrib/genrpbindings

raw
1 #!/usr/bin/env perl
2 # genrpbindings -- generate ratpoison bindings for various languages
3 #
4 # Copyright (C) 2003, 2004 Ryan Yeske, Doug Kearns, Shawn Betts
5 #
6 # currently generates bindings for:
7 # * Perl (Ratpoison.pm) Ryan Yeske <[email protected]>
8 # * Emacs Lisp (ratpoison-cmd.el) Ryan Yeske <[email protected]>
9 # * Ruby (ratpoison.rb) Doug Kearns <[email protected]>
10 # * Common Lisp (ratpoison.lisp) Shawn Betts <[email protected]>
11 # * Python (ratpoison.py) Mike O'Connor <[email protected]>
12 # add more languages!
13 #
14 # Bindings are just very thin wrappers, no argument checking is done.
15 # All of the functions return a string.
16 #
17 # Example: ratpoison --command='echo hello world'
18 #
19 # #!perl
20 # use Ratpoison;
21 # Ratpoison::echo ("hello world")
22 #
23 # ;;; elisp
24 # (require 'ratpoison-cmd)
25 # (ratpoison-echo "hello world")
26 #
27 # #!ruby
28 # require "ratpoison"
29 # Ratpoison.echo ("hello world")
30 #
31 # ;;; Common Lisp
32 # (load "ratpoison.lisp")
33 # (ratpoison:rp-echo "hello world")
34 #
35 # #!python
36 # import ratpoison
37 # ratpoison.echo( "hello world" )
38
39 use warnings;
40
41 $\="\n";
42
43 # Feel free to hardcode this if you installed ratpoison in
44 # a non-standard path, but note that you can also use the RATPOISON
45 # environment variable
46 $RATPOISON = 'ratpoison';
47
48 # open source file
49 $ACTIONS_C="../src/actions.c";
50 open ACTIONS_C or die "Can't open $ACTIONS_C";
51
52 # open target files
53 $PERL_FILE="./Ratpoison.pm";
54 $ELISP_FILE="./ratpoison-cmd.el";
55 $RUBY_FILE="./ratpoison.rb";
56 $COMMONLISP_FILE="./ratpoison.lisp";
57 $PYTHON_FILE="./ratpoison.py";
58 open PERL, ">$PERL_FILE" or die "Can't create $PERL_FILE";
59 open ELISP, ">$ELISP_FILE" or die "Can't create $ELISP_FILE";
60 open RUBY, ">$RUBY_FILE" or die "Can't create $RUBY_FILE";
61 open COMMONLISP, ">$COMMONLISP_FILE" or die "Can't create $COMMONLISP_FILE";
62 open PYTHON, ">$PYTHON_FILE" or die "Can't create $PYTHON_FILE";
63
64 # PERL preamble
65 print PERL 'package Ratpoison;';
66 print PERL '$RATPOISON=$ENV{RATPOISON} ? $ENV{RATPOISON} : "',$RATPOISON,'";';
67 print PERL 'sub command { my $a = "@_"; $a =~ s/(\')/\'\\\\\'\'/g; return `$RATPOISON -c \'$a\'`; }';
68
69 # ELISP preamble
70 print ELISP <<PREAMBLE;
71 (defvar ratpoison-program (or (getenv "RATPOISON") "$RATPOISON"))
72
73 (defmacro defun-ratpoison (cmd)
74 `(progn (defun ,(intern (concat "ratpoison-" (symbol-name cmd))) (&rest args)
75 (apply 'ratpoison-cmd ,(symbol-name cmd) args))))
76
77 (defun ratpoison-cmd (cmd &rest args)
78 (with-temp-buffer
79 (call-process ratpoison-program nil (current-buffer) t
80 "-c" (format "%s %s"
81 cmd
82 (mapconcat (lambda (x)
83 (if (stringp x)
84 x
85 (prin1-to-string x)))
86 args " ")))
87 (buffer-substring (point-min) (if (> (point-max) 1)
88 (- (point-max) 1)
89 (point-max)))))
90 PREAMBLE
91
92 # RUBY preamble
93 print RUBY <<PREAMBLE;
94 module Ratpoison
95
96 RATPOISON=ENV.fetch('RATPOISON', "$RATPOISON")
97
98 def command(command, *args)
99 return `#{RATPOISON} -c "#{command} #{args.join(' ')}"`
100 end
101 module_function :command
102 PREAMBLE
103
104 # Common Lisp preamble
105
106 print COMMONLISP <<PREAMBLE;
107 (require :asdf :asdf-utils)
108 (defpackage :ratpoison
109 (:use :cl))
110
111 ;; Needs the CLOCC PORT package
112 (asdf:operate 'asdf:load-op :port)
113
114 (in-package :ratpoison)
115
116 (defvar ratpoison-program
117 (or (asdf-utils:getenv "RATPOISON")
118 "$RATPOISON"))
119
120 (defmacro defun-ratpoison (cmd)
121 (let ((sym (intern (concatenate 'string "RP-" (symbol-name cmd)))))
122 `(progn (defun ,sym (&rest args)
123 (apply 'ratpoison-cmd ,(string-downcase (symbol-name cmd)) args))
124 (export ',sym))))
125
126 (defun ratpoison-cmd (cmd &rest args)
127 (labels ((mapconcat (fn list sep)
128 (apply 'concatenate 'string
129 (loop for x on list
130 collect (if (cdr x)
131 (concatenate 'string (funcall fn (car x)) sep)
132 (funcall fn (car x))))))
133 (build-cmd (cmd args)
134 (mapconcat (lambda (x)
135 (if (stringp x)
136 x
137 (prin1-to-string x)))
138 (nconc (list cmd) args) " ")))
139 (let ((stream (port:pipe-input ratpoison-program
140 "-c" (build-cmd cmd args))))
141 (do ((line (read-line stream nil nil)
142 (read-line stream nil nil))
143 (accum nil (cons line accum)))
144 ((null line) accum)))))
145 PREAMBLE
146
147 # python preamble
148
149 print PYTHON <<PREAMBLE;
150 import os
151 ratpoison = os.getenv('RATPOISON') or '$RATPOISON'
152 def rp_command( *args ):
153 p = os.popen( ratpoison + ' -c ' + '"' + (' '.join( args ) ) + '"', 'r' )
154 r = p.readlines();
155 p.close();
156 return r
157
158 PREAMBLE
159
160 # bindings
161 while (<ACTIONS_C>) {
162 if (m!/\*\@begin !) {
163 while (<ACTIONS_C>)
164 {
165 last if (m!/\*\@end !);
166 if (/\s*add_command\s*\(\"([^\"]+)\",\s*[^\"]+,\s*([0-9]+),\s*[0-9]+,\s*([0-9]+)/) {
167 my $name = $1;
168 my $numargs = $2;
169 my $optargs = $3;
170
171 # Skip the arguments
172 for (my $i=0; $i<$numargs; $i++) {
173 <ACTIONS_C>;
174 }
175
176 $nbindings++;
177 print PERL "sub $name { return command (\"$name\", \@_); }";
178 print ELISP "(defun-ratpoison $name)";
179 print COMMONLISP "(defun-ratpoison $name)";
180 print RUBY " def $name(*args)";
181 print RUBY " return command(\"$name\", args)";
182 print RUBY " end";
183 print RUBY " module_function :$name\n";
184 print PYTHON "def rp_$name( *args ): return rp_command ( '$name ' + ' '.join( args ) )";
185 }
186 }
187 }
188 }
189 print "$nbindings bindings.";
190
191 # PERL postamble
192 # nothing
193
194 # ELISP postamble
195 print ELISP '(provide \'ratpoison-cmd)';
196
197 # RUBY postamble
198 print RUBY "end";
199
200 # COMMON-LISP postamble
201 # nothing
202
203 # PYTHON postamble
204 # nothing
205
206 close PERL;
207 print "Created $PERL_FILE";
208 close ELISP;
209 print "Created $ELISP_FILE";
210 close RUBY;
211 print "Created $RUBY_FILE";
212 close COMMONLISP;
213 print "Created $COMMONLISP_FILE";
214 close PYTHON;
215 print "Created $PYTHON_FILE";