771
|
1 : #-*- Perl -*-
|
|
2
|
|
3 ### make-mswin-unicode --- generate Unicode-encapsulation code for MS Windows
|
|
4
|
|
5 ## Copyright (C) 2001, 2002 Ben Wing.
|
|
6
|
|
7 ## Author: Ben Wing <ben@xemacs.org>
|
|
8 ## Maintainer: Ben Wing <ben@xemacs.org>
|
|
9 ## Current Version: 1.0, August 24, 2001
|
|
10
|
|
11 ## This file is part of XEmacs.
|
|
12
|
|
13 ## XEmacs is free software; you can redistribute it and/or modify it
|
|
14 ## under the terms of the GNU General Public License as published by
|
|
15 ## the Free Software Foundation; either version 2, or (at your option)
|
|
16 ## any later version.
|
|
17
|
|
18 ## XEmacs is distributed in the hope that it will be useful, but
|
|
19 ## WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 ## General Public License for more details.
|
|
22
|
|
23 ## You should have received a copy of the GNU General Public License
|
|
24 ## along with XEmacs; see the file COPYING. If not, write to the Free
|
|
25 ## Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
26 ## 02111-1307, USA.
|
|
27
|
|
28 eval 'exec perl -w -S $0 ${1+"$@"}'
|
|
29 if 0;
|
|
30
|
|
31 use strict;
|
|
32 use File::Basename;
|
|
33 use Getopt::Long;
|
|
34
|
|
35 my ($myName, $myPath) = fileparse ($0);
|
|
36
|
|
37 my $usage="
|
|
38 Usage: $myName [--c-output FILE] [--h-output FILE] [--help] [FILES ...]
|
|
39
|
|
40 The purpose of this script is to auto-generate Unicode-encapsulation
|
|
41 code for MS Windows library functions that come in two versions (ANSI
|
|
42 and Unicode). The MS Windows header files provide a way of
|
|
43 automatically calling the right version, but only at compile-time,
|
|
44 which is *NOT* sufficient for any real-world program. The solution is
|
|
45 run-time Unicode encapsulation, which is not conceptually difficult
|
|
46 but is time-consuming, and is not supported standardly only due to
|
|
47 evil marketing decisions made by Microsoft. See src/intl-win32.c
|
|
48 for more information.
|
|
49
|
|
50 This script processes the specified files, looking for commands
|
|
51 indicating library routines to Unicode-encapsulate, as follows:
|
|
52
|
|
53 Portions of the files that should be processed are enclosed in lines
|
|
54 consisting only of the words \"begin-unicode-encapsulation-script\"
|
|
55 and \"end-unicode-encapsulation-script\". More than one section can
|
|
56 occur in a single file. Processed lines begin with a command word,
|
|
57 followed by one or more args (no quotes are necessary for spaces):
|
|
58
|
|
59 file specifies a file to start reading from.
|
|
60 yes indicates a function to be automatically Unicode-encapsulated.
|
|
61 (All parameters either need no special processing or are LPTSTR or
|
|
62 LPCTSTR.)
|
|
63 soon indicates a function that should be automatically Unicode-encapsulated,
|
|
64 but we're not ready to process it yet.
|
|
65 no indicates a function we don't support (it will be #defined to cause
|
|
66 a compile error, with the text after the function included in the
|
|
67 erroneous definition to indicate why we don't support it).
|
|
68 skip indicates a function we support manually; only a comment about this
|
|
69 will be generated.
|
|
70 split indicates a function with a split structure (different versions
|
|
71 for Unicode and ANSI), but where the only difference is in pointer
|
|
72 types, and the actual size does not differ. The structure name
|
|
73 should follow the function name, and it will be automatically
|
|
74 Unicode-encapsulated with appropriate casts.
|
|
75 begin-bracket indicates a #if statement to be inserted here.
|
|
76 end-bracket indicates the corresponding #endif statement.
|
|
77 blank lines and lines beginning with // are ignored.
|
|
78 ";
|
|
79
|
|
80 # ------------------ process command-line options ------------------
|
|
81
|
|
82 my %options;
|
|
83 my @SAVE_ARGV = @ARGV;
|
|
84
|
|
85 $Getopt::Long::ignorecase = 0;
|
|
86 &GetOptions (
|
|
87 \%options,
|
|
88 'c-output=s',
|
|
89 'h-output=s',
|
778
|
90 'includedir=s',
|
771
|
91 'help',
|
|
92 );
|
|
93
|
|
94 die $usage if $options{"help"};
|
|
95
|
|
96 my $in_script;
|
|
97 my $slurp;
|
|
98
|
778
|
99 my ($cout, $hout, $dir) = ($options{"c-output"},
|
|
100 $options{"h-output"},
|
|
101 $options{"includedir"});
|
|
102 if (!$dir)
|
|
103 {
|
|
104 $dir=$ENV{"MSVCDIR"} or die "Environment variable MSVCDIR undefined - run vcvars32.bat from your MSVC installation";
|
|
105 $dir.='/include';
|
|
106 }
|
|
107 die "Can't find MSVC include files in \"$dir\"" unless (-f $dir.'/WINDOWS.H');
|
771
|
108
|
|
109 open (COUT, ">$cout") or die "Can't open C output file $cout: $!";
|
|
110 open (HOUT, ">$hout") or die "Can't open C output file $hout: $!";
|
|
111
|
|
112 select (STDOUT); $| = 1;
|
|
113
|
|
114 print COUT "/* Automatically-generated Unicode-encapsulation file,
|
|
115 using the command
|
|
116
|
|
117 $myPath$myName @SAVE_ARGV
|
|
118
|
|
119 Do not edit. See `$myName'.
|
|
120 */
|
|
121
|
|
122 #include <config.h>
|
|
123 #include \"lisp.h\"
|
|
124
|
|
125 #include \"syswindows.h\"
|
|
126
|
|
127 ";
|
|
128 print HOUT "/* Automatically-generated Unicode-encapsulation header file.
|
|
129 Do not edit. See `$myName'.
|
|
130 */\n\n";
|
|
131
|
|
132 my %files;
|
|
133 my %processed;
|
|
134 my %bracket;
|
|
135
|
|
136 my $current_file;
|
|
137 my @current_bracket;
|
|
138
|
|
139 while (<>)
|
|
140 {
|
|
141 chomp;
|
|
142
|
|
143 if (/^begin-unicode-encapsulation-script$/)
|
|
144 {
|
|
145 $in_script = 1;
|
|
146 }
|
|
147 elsif (/^end-unicode-encapsulation-script$/)
|
|
148 {
|
|
149 $in_script = 0;
|
|
150 }
|
|
151 elsif ($in_script)
|
|
152 {
|
|
153 next if (m!^//!);
|
|
154 next if (/^[ \t]*$/);
|
778
|
155 if (/(file|yes|soon|no|skip|split|begin-bracket|end-bracket)(?: (.*))?/)
|
771
|
156 {
|
|
157 my ($command, $parms) = ($1, $2);
|
778
|
158 if ($command eq "file")
|
771
|
159 {
|
|
160 $current_file = $parms;
|
|
161 }
|
|
162 elsif ($command eq "begin-bracket")
|
|
163 {
|
|
164 my $current_bracket = $current_bracket[$#current_bracket];
|
|
165 if (defined ($current_bracket))
|
|
166 {
|
|
167 $current_bracket .= "&& $parms";
|
|
168 }
|
|
169 else
|
|
170 {
|
|
171 $current_bracket = "$parms";
|
|
172 }
|
|
173 push @current_bracket, $current_bracket;
|
|
174 }
|
|
175 elsif ($command eq "end-bracket")
|
|
176 {
|
|
177 pop @current_bracket;
|
|
178 }
|
|
179 else
|
|
180 {
|
|
181 my ($fun, $reason) = split /\s+/, $parms, 2;
|
|
182 $files{$current_file}{$fun} = [$command, $reason];
|
|
183 $bracket{$current_file}{$fun} =
|
|
184 $current_bracket[$#current_bracket];
|
|
185 }
|
|
186 }
|
|
187 else
|
|
188 {
|
|
189 print "WARNING: Unknown line $_\n";
|
|
190 }
|
|
191 }
|
|
192 }
|
|
193
|
|
194
|
|
195 foreach my $file (keys %files)
|
|
196 {
|
|
197 $slurp = &FileContents ($file);
|
|
198 print "Processing file $file\n";
|
|
199 print HOUT "\n/* Processing file $file */\n\n";
|
|
200 my $totalspace = 70 - length ("Processing file $file");
|
|
201 $totalspace = 0 if $totalspace < 0;
|
|
202 my $alignspaceleft = $totalspace / 2;
|
|
203 my $alignspaceright = ($totalspace + 1) / 2;
|
|
204 print COUT "
|
|
205 /*----------------------------------------------------------------------*/
|
|
206 /*" . (" " x $alignspaceleft) . "Processing file $file" .
|
|
207 (" " x $alignspaceright) . "*/
|
|
208 /*----------------------------------------------------------------------*/
|
|
209
|
|
210 ";
|
|
211
|
|
212 my ($ws_re, $must_ws_re, $tok_ch) =
|
|
213 ("\\s*", "\\s+", "\\w");
|
|
214 # unfortunately there is no surefire way short of
|
|
215 # parsing all include files for typedefs to
|
|
216 # distinguish types from parameters, and prototypes
|
|
217 # appear in the include files both with and without
|
|
218 # parameters -- the latter kinds appear in a very
|
|
219 # different style and were obviously added later. so
|
|
220 # we rely on the fact that defined types are all
|
|
221 # upper-case, and parameters generally are not, and
|
|
222 # special-case the exceptions.
|
|
223 my $typeword_re =
|
|
224 # note the negative lookahead assertions: the first
|
|
225 # one excludes the words "X" and "Y" from type
|
|
226 # words, since they appear as parameter names in
|
|
227 # CreateWindowEx; the second prevents "void
|
|
228 # *Argument" from being parsed as a type "void *A"
|
|
229 # followed by a parameter "rgument".
|
|
230 "(?:(?!(?:X\\b|Y\\b))(?:unsigned|int|long|short|va_list|[A-Z_0-9]+)(?!${tok_ch}))";
|
|
231 my $typetoken_re = "(?:$typeword_re$ws_re\\**$ws_re)";
|
|
232 my $arg_re = "(?:($typetoken_re+)(${tok_ch}+)?(?: OPTIONAL)?)";
|
|
233 my $fun_re = "(SHSTDAPI_\\(${tok_ch}+\\)|${tok_ch}" . "[A-Za-z_0-9 \t\n\r\f]*?${tok_ch})${ws_re}(${tok_ch}+)W${ws_re}\\(((${ws_re}${arg_re}${ws_re},)*${ws_re}${arg_re}${ws_re})\\);";
|
|
234
|
|
235 # print "regexp: $fun_re\n";
|
|
236 while ($slurp =~ /$fun_re/g)
|
|
237 {
|
|
238 my ($rettype, $fun, $args) = ($1, $2, $3);
|
|
239 $processed{$fun} = 1;
|
|
240 print "Processing: $fun";
|
|
241
|
|
242 my ($command, $reason) = ($files{$file}{$fun}[0], $files{$file}{$fun}[1]);
|
|
243 if (!defined ($command))
|
|
244 {
|
|
245 print " (no command found)\n";
|
|
246 }
|
|
247 else
|
|
248 {
|
|
249 print "\n";
|
|
250 my $bracket = $bracket{$file}{$fun};
|
|
251 if (defined ($bracket))
|
|
252 {
|
|
253 print HOUT "#if $bracket\n";
|
|
254 print COUT "#if $bracket\n\n";
|
|
255 }
|
|
256 if ($command eq "no")
|
|
257 {
|
|
258 if (!defined ($reason))
|
|
259 {
|
|
260 print "WARNING: No reason given for `no' with function $fun\n";
|
|
261 $reason = "";
|
|
262 }
|
|
263
|
|
264 print HOUT "#undef $fun\n";
|
|
265 print HOUT "#define $fun error $reason\n";
|
|
266 print COUT "/* Error if $fun used: $reason */\n\n";
|
|
267 }
|
|
268 elsif ($command eq "skip")
|
|
269 {
|
|
270 if (!defined ($reason))
|
|
271 {
|
|
272 print "WARNING: No reason given for `skip' with function $fun\n";
|
|
273 $reason = "";
|
|
274 }
|
|
275
|
|
276 print HOUT "/* Skipping $fun because $reason */\n";
|
|
277 print COUT "/* Skipping $fun because $reason */\n\n";
|
|
278 }
|
|
279 elsif ($command eq "soon")
|
|
280 {
|
|
281 $reason = "" if !defined ($reason);
|
|
282
|
|
283 print HOUT "/* Not yet: $fun $reason */\n";
|
|
284 print COUT "/* Not yet: $fun $reason */\n\n";
|
|
285 }
|
|
286 else
|
|
287 {
|
|
288 my (@args, %argtype, %ansiarg, %xarg, $split_struct,
|
|
289 $split_rettype);
|
|
290 if ($command eq "split")
|
|
291 {
|
|
292 ($split_struct, $reason) = split /\s+/, $reason, 2;
|
|
293 }
|
|
294 my $argno = 0;
|
|
295 while ($args =~ /$arg_re/g)
|
|
296 {
|
|
297 $argno++;
|
|
298 my ($argtype, $argname) = ($1, $2);
|
|
299 $argtype =~ s/\s*$//;
|
|
300 next if $argtype eq "void" || $argtype eq "VOID";
|
|
301 $argname = "arg$argno" if !defined ($argname);
|
|
302 $argtype{$argname} = $argtype;
|
|
303 $ansiarg{$argname} = $argtype;
|
|
304 $ansiarg{$argname} =~ s/\bLPWSTR\b/LPSTR/;
|
|
305 $ansiarg{$argname} =~ s/\bLPCWSTR\b/LPCSTR/;
|
|
306 $xarg{$argname} = $argtype;
|
|
307 $xarg{$argname} =~ s/\bLPWSTR\b/Extbyte */;
|
|
308 $xarg{$argname} =~ s/\bLPCWSTR\b/const Extbyte */;
|
|
309 if (defined ($split_struct))
|
|
310 {
|
|
311 my $fuck_cperl1 = "\\b${split_struct}W\\b";
|
|
312 my $fuck_cperl2 = "${split_struct}A";
|
|
313 $ansiarg{$argname} =~ s/$fuck_cperl1/$fuck_cperl2/;
|
|
314 }
|
|
315 push @args, $argname;
|
|
316 }
|
|
317 $rettype =~ s/\bSHSTDAPI_\((.*)\)/$1/;
|
|
318 $rettype =~ s/\s*WIN\w*?API\s*//g;
|
|
319 $rettype =~ s/\bAPIENTRY\b//;
|
|
320 $rettype =~ s/\bSHSTDAPI\b/HRESULT/;
|
|
321 if ($rettype =~ /LPC?WSTR/)
|
|
322 {
|
|
323 $split_rettype = 1;
|
|
324 $rettype =~ s/\bLPWSTR\b/Extbyte */;
|
|
325 $rettype =~ s/\bLPCWSTR\b/const Extbyte */;
|
|
326 }
|
|
327 if (defined ($reason))
|
|
328 {
|
|
329 print COUT "/* NOTE: $reason */\n";
|
|
330 }
|
|
331 print COUT "$rettype\nqxe$fun (";
|
|
332 print HOUT "$rettype qxe$fun (";
|
|
333 my $first = 1;
|
|
334 if (!@args)
|
|
335 {
|
|
336 print COUT "void";
|
|
337 print HOUT "void";
|
|
338 }
|
|
339 else
|
|
340 {
|
|
341 foreach my $x (@args)
|
|
342 {
|
|
343 print COUT ", " if !$first;
|
|
344 print HOUT ", " if !$first;
|
|
345 $first = 0;
|
|
346 print COUT "$xarg{$x} $x";
|
|
347 print HOUT "$xarg{$x} $x";
|
|
348 }
|
|
349 }
|
|
350 print HOUT ");\n";
|
|
351 print COUT ")\n{\n if (XEUNICODE_P)\n ";
|
|
352 if ($rettype ne "void" && $rettype ne "VOID")
|
|
353 {
|
|
354 print COUT "return ";
|
|
355 print COUT "($rettype) " if $split_rettype;
|
|
356 }
|
|
357 print COUT "${fun}W (";
|
|
358 $first = 1;
|
|
359 foreach my $x (@args)
|
|
360 {
|
|
361 print COUT ", " if !$first;
|
|
362 $first = 0;
|
|
363 print COUT ($argtype{$x} eq $xarg{$x} ? $x :
|
|
364 "($argtype{$x}) $x");
|
|
365 }
|
|
366 print COUT ");\n else\n ";
|
|
367 if ($rettype ne "void" && $rettype ne "VOID")
|
|
368 {
|
|
369 print COUT "return ";
|
|
370 print COUT "($rettype) " if $split_rettype;
|
|
371 }
|
|
372 print COUT "${fun}A (";
|
|
373 $first = 1;
|
|
374 foreach my $x (@args)
|
|
375 {
|
|
376 print COUT ", " if !$first;
|
|
377 $first = 0;
|
|
378 print COUT ($argtype{$x} eq $ansiarg{$x} ? $x :
|
|
379 "($ansiarg{$x}) $x");
|
|
380 }
|
|
381 print COUT ");\n}\n\n";
|
|
382 }
|
|
383 if (defined ($bracket))
|
|
384 {
|
|
385 print HOUT "#endif /* $bracket */\n";
|
|
386 print COUT "#endif /* $bracket */\n\n";
|
|
387 }
|
|
388 }
|
|
389 }
|
|
390 }
|
|
391
|
|
392 foreach my $file (keys %files)
|
|
393 {
|
|
394 foreach my $fun (keys %{$files{$file}})
|
|
395 {
|
|
396 if (!$processed{$fun} && $files{$file}{$fun}[0] =~ /^(yes|soon|split)$/)
|
|
397 {
|
|
398 print "WARNING: Can't locate prototype for $fun\n";
|
|
399 }
|
|
400 }
|
|
401 }
|
|
402
|
|
403
|
|
404 sub FileContents
|
|
405 {
|
|
406 local $/ = undef;
|
778
|
407 open (FILE, "< $dir/$_[0]") or die "$dir/$_[0]: $!";
|
771
|
408 my $retval = scalar <FILE>;
|
|
409 # must hack away CRLF junk.
|
|
410 $retval =~ s/\r\n/\n/g;
|
|
411 return $retval;
|
|
412 }
|