Debian Codegraph @

testsyms

raw
1 #! /usr/bin/perl
2 ##
3 ## testsyms -- test for function/variable symbols
4 ##
5 ## The symbols for which we test are listed in the file 'list.syms'
6 ##
7 ## Set the environment variable CURSES_VERBOSE to see the details of the
8 ## testing.
9 ##
10 ## Copyright (c) 1994-2000 William Setzer
11 ##
12 ## You may distribute under the terms of either the Artistic License
13 ## or the GNU General Public License, as specified in the README file.
14 ##
15 ## This program is modelled after parts of the dist-3.0 distribution.
16 ## In many cases I just hand-converted the sh script to perl, so this
17 ## program probably falls under the Artistic license. At the very least,
18 ## it has the "look and feel". Will I be sued? :-)
19 ##
20 ## Thanks to Raphael Manfredi and the other contributors of dist-3.0.
21 ##
22 ## VMS patches thanks to Peter Prymmer <[email protected]>
23
24 use strict;
25 use warnings;
26 use English;
27
28 my $verbose;
29
30 sub makeCompileCommand($) {
31 my ($compileR) = @_;
32 #-----------------------------------------------------------------------------
33 # A template for a compile command that can test for existence of some
34 # symbol in this system's Curses library.
35 #
36 # The template is a shell command containing some variables
37 # to be substituted to make a real executable command.
38 #
39 # Those variables are:
40 #
41 # _C_SYM Value for -DSYM option, e.g. "addstr" to get
42 # -DSYM=addstr in the command.
43 #
44 # _C_FILE base of name of file to compile and executable output file
45 # e.g. "foo" to compile "foo.c" into "foo"
46 #
47 # Some of the compile command is determined by environment variables:
48 #
49 # CC, INC, CCFLAGS, LDLOADLIBS, LDDLFLAGS
50 #
51 #-----------------------------------------------------------------------------
52 my $compile;
53
54 $compile = '#CC# #DEFS# #INCS# #CFLAGS# #FILE# #LFLAGS# #LIBS#' .
55 ($verbose ? '' : '#NULL#');
56 # Initial value
57
58 my $cc = $ENV{'CC'};
59 my $inc = $ENV{'INC'};
60 my $ccflags = $ENV{'CCFLAGS'};
61 my $ldloadlibs = $ENV{'LDLOADLIBS'};
62 my $lddlflags = $ENV{'LDDLFLAGS'};
63
64 if (defined($cc)) {
65 $compile =~ s{#CC#}{$cc};
66 }
67 if (defined($inc)) {
68 $compile =~ s{#INCS#}{$inc};
69 }
70 if (defined($ccflags)) {
71 $compile =~ s{#CFLAGS#}{$ccflags};
72 }
73 if (defined($ldloadlibs)) {
74 $compile =~ s{#LIBS#}{$ldloadlibs};
75 } else {
76 $compile =~ s{#LIBS#}{};
77 }
78 if (defined($lddlflags)) {
79 ## Only get -L's. Other options can cause strange link behavior.
80 ## (I shoulda stayed in bed.)
81 #
82 my $lflags;
83 $lflags = ''; # initial value
84 while ($lddlflags =~ m{(-L\S+)}g) {
85 $lflags .= " $1";
86 }
87 $compile =~ s{#LFLAGS#}{$lflags};
88 }
89
90 # Left to handle: DEFS/FILE/NULL
91 # DEFS => "cc" define of "SYM" to "_C_SYM_"
92 # FILE => "cc" compile of file _C_FILE_.c into executable _C_FILE_
93 # NULL => output of system call to dev null
94 #
95 # _C_SYM_ and _C_FILE_ will be filled in later
96
97 if ($OSNAME =~ m{VMS}i) {
98 $compile =~ s{#DEFS#}{DEFINE=SYM="_C_SYM_"};
99 $compile =~ s{#FILE#}{_C_FILE_.c};
100 $compile =~ s{#NULL#}{}; # no non-verbose way
101 }
102 elsif ($OSNAME eq 'MSWin32') {
103 $compile =~ s{#DEFS#}{-DSYM="_C_SYM_"};
104 $compile =~ s{#FILE#}{_C_FILE_.c};
105 $compile =~ s{#NULL#}{>nul 2>&1};
106 }
107 else {
108 $compile =~ s{#DEFS#}{-DSYM="_C_SYM_"};
109 $compile =~ s{#FILE#}{-o _C_FILE_ _C_FILE_.c};
110 $compile =~ s{#NULL#}{>/dev/null 2>&1};
111 }
112
113 if ($compile =~ m{#.+#}) {
114 die "OOPS: internal error constructing a compile command. " .
115 "We failed to substitute for a #xxx# substitution variable " .
116 "and thus ended up with this: '$compile'\n";
117 }
118 $$compileR = $compile;
119 }
120
121
122
123 sub completeCompileCmd($$$$) {
124 my ($template, $sym, $args, $file) = @_;
125 #-----------------------------------------------------------------------------
126 # The complete ready-to-execute compile command.
127 #
128 # $template is a template for the compile command, with some variables in it
129 # that need to be replaced based on the values of $sym, $args, and $file.
130 # -----------------------------------------------------------------------------
131 my $retval;
132
133 $retval = $template; # initial value
134
135 my $symargs = $sym . (defined($args) ? $args : '');
136
137 $retval =~ s{_C_SYM_}{$symargs}ge;
138 $retval =~ s{_C_FILE_}{$file}g;
139
140 return $retval;
141 }
142
143
144
145 sub writeHeader($) {
146 my ($cursesDefFh) = @_;
147
148 print $cursesDefFh <<'EOHDR';
149 /*============================================================================
150 CursesDef.h
151 ==============================================================================
152
153 This file defines C macros that tell which Curses functions are available
154 (in the C Curses library) on the target platform and some information about
155 what variant is implemented.
156
157 ==> This file was automatically generated by the program 'testsyms'; changes
158 ==> will be lost the next time 'testsyms' runs.
159
160 If you need to edit this file because 'testsyms' didn't do a good job, be
161 sure to save a copy of your changes.
162
163 This method of determining what is implemented on this system (trying to
164 compile something and noting whether the compile succeeds or fails) is quite
165 sloppy and prone to error. If you are having problems compiling, check the
166 appropriate symbol to see if it was set correctly: For each line, if the
167 answer to the question is "no", that line should start with "#undef"; if the
168 answer is yes, it should start with "#define".
169 ============================================================================*/
170 EOHDR
171 }
172
173
174 my %tstfile = qw( E testsym
175 I testint
176 V testsym
177 T testtyp);
178
179
180
181 sub testCompile($$$$$$) {
182 my ($action, $sym, $args, $compileCmdTemplate, $logFh, $compFailedR) = @_;
183 #-----------------------------------------------------------------------------
184 # Do a test compile to determine if symbol $sym exists in this system's
185 # Curses library.
186 #
187 # Use the compile command template $compileCmdTemplate to do this,
188 # filling in variables _C_SYM and _C_FILE in this template appropriately.
189 #
190 # The source file we compile is that given my %tstfile for the action
191 # $action.
192 #
193 # The symbol for which we test is $sym, with $args appended if $args
194 # is defined.
195 #
196 # Return $$compFailedR true iff the compile failed. Note that we don't
197 # supply any information as to how it failed; caller must simply assume
198 # that it failed because the symbol is not defined in this system's
199 # Curses library.
200 #-----------------------------------------------------------------------------
201 my $file = $tstfile{$action};
202
203 unless (defined $sym and defined $file) {
204 warn "WARNING: internal error on symbol $_\n";
205 }
206
207 my $cmd = completeCompileCmd($compileCmdTemplate, $sym, $args, $file);
208
209 print $logFh $cmd, "\n" if $verbose;
210
211 my $cmdStdout = qx{$cmd}; my $termStat = $?;
212
213 if ($verbose) {
214 print $logFh ("Stdout: '$cmdStdout' ");
215 print $logFh ("(term status = $termStat)");
216 print $logFh ("\n");
217 }
218
219 $$compFailedR = ($termStat != 0);
220 }
221
222
223
224 ###############################################################################
225 # MAINLINE #
226 ###############################################################################
227
228 print("Checking capabilities of the Ncurses libraries.\n");
229 print("Set CURSES_VERBOSE environment variable to see the details of the " .
230 "tests.\n");
231 print("\n");
232
233 if ($ENV{CURSES_VERBOSE}) {
234 $verbose = 1;
235 } else {
236 $verbose = 0;
237 }
238
239 open(my $listSymsFh, '<', 'list.syms')
240 or die "Can't open list.syms: $!";
241 open(my $cursesDefFh, '>', 'CursesDef.h')
242 or die "Can't open CursesDef.h for output: $!";
243 open(my $logFh, ">&STDERR")
244 or die "Can't redirect to STDERR: $!\n";
245
246 my $logFileNm;
247
248 while (@ARGV) {
249 my $arg = shift;
250
251 $arg =~ /^-h/ and Usage();
252 $arg =~ /^-v/ and ++$verbose and next;
253 $arg =~ /^-l/ and do {
254 $logFileNm = shift or Usage("<-l> needs a filename");
255 next;
256 };
257 $arg =~ /^-/ and Usage("Unknown option: $arg");
258 Usage("Unknown argument: $arg");
259 }
260 if (@ARGV) { Usage() }
261
262 if (defined($logFileNm)) {
263 open($logFh, '>', $logFileNm)
264 or die "Can't open file '$logFileNm': $!";
265 open STDERR, ">&$logFh"
266 or die "Can't redirect STDERR: $!";
267 } else {
268 open($logFh, ">&STDERR");
269 }
270
271
272 select $logFh;
273 $| = 1;
274
275
276 makeCompileCommand(\my $compile);
277
278 print $logFh ("Doing test compiles with the compile command '$compile'\n");
279
280 writeHeader($cursesDefFh);
281
282 while (<$listSymsFh>) {
283 next if /^\S*#/;
284 next unless /\S/;
285
286 my ($action, $sym, $args) = /^([A-Z])\s+(\w+)\s*(\(.*\))?/;
287
288 testCompile($action, $sym, $args, $compile, $logFh, \my $compFailed);
289
290 my $ssym = $sym;
291 $ssym =~ s/^w//;
292
293 my $c_sym;
294 my $comment;
295
296 if ($action eq 'E') {
297 print $logFh ("function '$sym' ",
298 ($compFailed ? "NOT " : ""), "found\n");
299
300 $c_sym = uc "C_$ssym";
301 $comment = "Does function '$ssym' exist?";
302 } elsif ($action eq 'I') {
303 # Some functions return either int or void, depending on what compiler
304 # and libcurses.a you are using. For the int/void test, if the
305 # compiler doesn't complain about assigning the sym to an int
306 # variable, we assume the function returns int. Otherwise, we assume
307 # it returns void.
308
309 print $logFh ("function '$sym' returns ",
310 ($compFailed ? "void" : "int"), "\n");
311
312 $c_sym = uc "C_INT$ssym";
313 $comment = "Does function '$ssym' return 'int'?";
314 } elsif ($action eq 'V') {
315 print $logFh ("variable '$sym' ",
316 ($compFailed ? "NOT " : ""), "found\n");
317
318 $c_sym = uc "C_$ssym";
319 $comment = "Does variable '$ssym' exist?";
320 } elsif ($action eq 'T') {
321 print $logFh ("typedef '$sym' ",
322 ($compFailed ? "NOT " : ""), "found\n");
323
324 $c_sym = uc "C_TYP$ssym";
325 $comment = "Does typedef '$ssym' exist?";
326 } else {
327 warn "WARNING: internal error on symbol $_\n";
328 }
329
330 my $pad1 = length($c_sym) < 24 ? " " x (24 - length($c_sym)) : "";
331 my $pad2 = length($comment) < 42 ? " " x (42 - length($comment)) : "";
332
333 print $cursesDefFh
334 $compFailed ? "#undef " : "#define ",
335 $c_sym, $pad1, "/* ", $comment, $pad2, "*/",
336 "\n";
337 }
338
339 unlink "testsym";
340 unlink "testint";
341 unlink "testtyp";
342
343 1 while unlink "testsym.obj"; # Possibly pointless VMSism
344 1 while unlink "testint.obj"; # Possibly pointless VMSism
345 1 while unlink "testtyp.obj"; # Possibly pointless VMSism
346
347 close($listSymsFh);
348 close($cursesDefFh);
349 close($logFh);
350 exit 0;
351
352 ###
353 ## Helper functions
354 #
355
356 sub Usage {
357 print LOG @_, "\n";
358 print LOG <<EOM;
359 Usage: testsyms [options]
360 where options are
361
362 -h This message.
363 -v Verbose. Tell you more than you want to know about
364 how the Curses symbols are being determined.
365 -l <file> Create file <file> and dump output into it.
366 EOM
367 }
368
369 __END__