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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
#! /usr/bin/perl
##
## testsyms -- test for function/variable symbols
##
## The symbols for which we test are listed in the file 'list.syms'
##
## Set the environment variable CURSES_VERBOSE to see the details of the
## testing.
##
## Copyright (c) 1994-2000 William Setzer
##
## You may distribute under the terms of either the Artistic License
## or the GNU General Public License, as specified in the README file.
##
## This program is modelled after parts of the dist-3.0 distribution.
## In many cases I just hand-converted the sh script to perl, so this
## program probably falls under the Artistic license. At the very least,
## it has the "look and feel". Will I be sued? :-)
##
## Thanks to Raphael Manfredi and the other contributors of dist-3.0.
##
## VMS patches thanks to Peter Prymmer <pvhp@forte.com>
use strict;
use warnings;
use English;
my $verbose;
sub makeCompileCommand($) {
my ($compileR) = @_;
#-----------------------------------------------------------------------------
# A template for a compile command that can test for existence of some
# symbol in this system's Curses library.
#
# The template is a shell command containing some variables
# to be substituted to make a real executable command.
#
# Those variables are:
#
# _C_SYM Value for -DSYM option, e.g. "addstr" to get
# -DSYM=addstr in the command.
#
# _C_FILE base of name of file to compile and executable output file
# e.g. "foo" to compile "foo.c" into "foo"
#
# Some of the compile command is determined by environment variables:
#
# CC, INC, CCFLAGS, LDLOADLIBS, LDDLFLAGS
#
#-----------------------------------------------------------------------------
my $compile;
$compile = '#CC# #DEFS# #INCS# #CFLAGS# #FILE# #LFLAGS# #LIBS#' .
($verbose ? '' : '#NULL#');
# Initial value
my $cc = $ENV{'CC'};
my $inc = $ENV{'INC'};
my $ccflags = $ENV{'CCFLAGS'};
my $ldloadlibs = $ENV{'LDLOADLIBS'};
my $lddlflags = $ENV{'LDDLFLAGS'};
if (defined($cc)) {
$compile =~ s{#CC#}{$cc};
}
if (defined($inc)) {
$compile =~ s{#INCS#}{$inc};
}
if (defined($ccflags)) {
$compile =~ s{#CFLAGS#}{$ccflags};
}
if (defined($ldloadlibs)) {
$compile =~ s{#LIBS#}{$ldloadlibs};
} else {
$compile =~ s{#LIBS#}{};
}
if (defined($lddlflags)) {
## Only get -L's. Other options can cause strange link behavior.
## (I shoulda stayed in bed.)
#
my $lflags;
$lflags = ''; # initial value
while ($lddlflags =~ m{(-L\S+)}g) {
$lflags .= " $1";
}
$compile =~ s{#LFLAGS#}{$lflags};
}
# Left to handle: DEFS/FILE/NULL
# DEFS => "cc" define of "SYM" to "_C_SYM_"
# FILE => "cc" compile of file _C_FILE_.c into executable _C_FILE_
# NULL => output of system call to dev null
#
# _C_SYM_ and _C_FILE_ will be filled in later
if ($OSNAME =~ m{VMS}i) {
$compile =~ s{#DEFS#}{DEFINE=SYM="_C_SYM_"};
$compile =~ s{#FILE#}{_C_FILE_.c};
$compile =~ s{#NULL#}{}; # no non-verbose way
}
elsif ($OSNAME eq 'MSWin32') {
$compile =~ s{#DEFS#}{-DSYM="_C_SYM_"};
$compile =~ s{#FILE#}{_C_FILE_.c};
$compile =~ s{#NULL#}{>nul 2>&1};
}
else {
$compile =~ s{#DEFS#}{-DSYM="_C_SYM_"};
$compile =~ s{#FILE#}{-o _C_FILE_ _C_FILE_.c};
$compile =~ s{#NULL#}{>/dev/null 2>&1};
}
if ($compile =~ m{#.+#}) {
die "OOPS: internal error constructing a compile command. " .
"We failed to substitute for a #xxx# substitution variable " .
"and thus ended up with this: '$compile'\n";
}
$$compileR = $compile;
}
sub completeCompileCmd($$$$) {
my ($template, $sym, $args, $file) = @_;
#-----------------------------------------------------------------------------
# The complete ready-to-execute compile command.
#
# $template is a template for the compile command, with some variables in it
# that need to be replaced based on the values of $sym, $args, and $file.
# -----------------------------------------------------------------------------
my $retval;
$retval = $template; # initial value
my $symargs = $sym . (defined($args) ? $args : '');
$retval =~ s{_C_SYM_}{$symargs}ge;
$retval =~ s{_C_FILE_}{$file}g;
return $retval;
}
sub writeHeader($) {
my ($cursesDefFh) = @_;
print $cursesDefFh <<'EOHDR';
/*============================================================================
CursesDef.h
==============================================================================
This file defines C macros that tell which Curses functions are available
(in the C Curses library) on the target platform and some information about
what variant is implemented.
==> This file was automatically generated by the program 'testsyms'; changes
==> will be lost the next time 'testsyms' runs.
If you need to edit this file because 'testsyms' didn't do a good job, be
sure to save a copy of your changes.
This method of determining what is implemented on this system (trying to
compile something and noting whether the compile succeeds or fails) is quite
sloppy and prone to error. If you are having problems compiling, check the
appropriate symbol to see if it was set correctly: For each line, if the
answer to the question is "no", that line should start with "#undef"; if the
answer is yes, it should start with "#define".
============================================================================*/
EOHDR
}
my %tstfile = qw( E testsym
I testint
V testsym
T testtyp);
sub testCompile($$$$$$) {
my ($action, $sym, $args, $compileCmdTemplate, $logFh, $compFailedR) = @_;
#-----------------------------------------------------------------------------
# Do a test compile to determine if symbol $sym exists in this system's
# Curses library.
#
# Use the compile command template $compileCmdTemplate to do this,
# filling in variables _C_SYM and _C_FILE in this template appropriately.
#
# The source file we compile is that given my %tstfile for the action
# $action.
#
# The symbol for which we test is $sym, with $args appended if $args
# is defined.
#
# Return $$compFailedR true iff the compile failed. Note that we don't
# supply any information as to how it failed; caller must simply assume
# that it failed because the symbol is not defined in this system's
# Curses library.
#-----------------------------------------------------------------------------
my $file = $tstfile{$action};
unless (defined $sym and defined $file) {
warn "WARNING: internal error on symbol $_\n";
}
my $cmd = completeCompileCmd($compileCmdTemplate, $sym, $args, $file);
print $logFh $cmd, "\n" if $verbose;
my $cmdStdout = qx{$cmd}; my $termStat = $?;
if ($verbose) {
print $logFh ("Stdout: '$cmdStdout' ");
print $logFh ("(term status = $termStat)");
print $logFh ("\n");
}
$$compFailedR = ($termStat != 0);
}
###############################################################################
# MAINLINE #
###############################################################################
print("Checking capabilities of the Ncurses libraries.\n");
print("Set CURSES_VERBOSE environment variable to see the details of the " .
"tests.\n");
print("\n");
if ($ENV{CURSES_VERBOSE}) {
$verbose = 1;
} else {
$verbose = 0;
}
open(my $listSymsFh, '<', 'list.syms')
or die "Can't open list.syms: $!";
open(my $cursesDefFh, '>', 'CursesDef.h')
or die "Can't open CursesDef.h for output: $!";
open(my $logFh, ">&STDERR")
or die "Can't redirect to STDERR: $!\n";
my $logFileNm;
while (@ARGV) {
my $arg = shift;
$arg =~ /^-h/ and Usage();
$arg =~ /^-v/ and ++$verbose and next;
$arg =~ /^-l/ and do {
$logFileNm = shift or Usage("<-l> needs a filename");
next;
};
$arg =~ /^-/ and Usage("Unknown option: $arg");
Usage("Unknown argument: $arg");
}
if (@ARGV) { Usage() }
if (defined($logFileNm)) {
open($logFh, '>', $logFileNm)
or die "Can't open file '$logFileNm': $!";
open STDERR, ">&$logFh"
or die "Can't redirect STDERR: $!";
} else {
open($logFh, ">&STDERR");
}
select $logFh;
$| = 1;
makeCompileCommand(\my $compile);
print $logFh ("Doing test compiles with the compile command '$compile'\n");
writeHeader($cursesDefFh);
while (<$listSymsFh>) {
next if /^\S*#/;
next unless /\S/;
my ($action, $sym, $args) = /^([A-Z])\s+(\w+)\s*(\(.*\))?/;
testCompile($action, $sym, $args, $compile, $logFh, \my $compFailed);
my $ssym = $sym;
$ssym =~ s/^w//;
my $c_sym;
my $comment;
if ($action eq 'E') {
print $logFh ("function '$sym' ",
($compFailed ? "NOT " : ""), "found\n");
$c_sym = uc "C_$ssym";
$comment = "Does function '$ssym' exist?";
} elsif ($action eq 'I') {
# Some functions return either int or void, depending on what compiler
# and libcurses.a you are using. For the int/void test, if the
# compiler doesn't complain about assigning the sym to an int
# variable, we assume the function returns int. Otherwise, we assume
# it returns void.
print $logFh ("function '$sym' returns ",
($compFailed ? "void" : "int"), "\n");
$c_sym = uc "C_INT$ssym";
$comment = "Does function '$ssym' return 'int'?";
} elsif ($action eq 'V') {
print $logFh ("variable '$sym' ",
($compFailed ? "NOT " : ""), "found\n");
$c_sym = uc "C_$ssym";
$comment = "Does variable '$ssym' exist?";
} elsif ($action eq 'T') {
print $logFh ("typedef '$sym' ",
($compFailed ? "NOT " : ""), "found\n");
$c_sym = uc "C_TYP$ssym";
$comment = "Does typedef '$ssym' exist?";
} else {
warn "WARNING: internal error on symbol $_\n";
}
my $pad1 = length($c_sym) < 24 ? " " x (24 - length($c_sym)) : "";
my $pad2 = length($comment) < 42 ? " " x (42 - length($comment)) : "";
print $cursesDefFh
$compFailed ? "#undef " : "#define ",
$c_sym, $pad1, "/* ", $comment, $pad2, "*/",
"\n";
}
unlink "testsym";
unlink "testint";
unlink "testtyp";
1 while unlink "testsym.obj"; # Possibly pointless VMSism
1 while unlink "testint.obj"; # Possibly pointless VMSism
1 while unlink "testtyp.obj"; # Possibly pointless VMSism
close($listSymsFh);
close($cursesDefFh);
close($logFh);
exit 0;
###
## Helper functions
#
sub Usage {
print LOG @_, "\n";
print LOG <<EOM;
Usage: testsyms [options]
where options are
-h This message.
-v Verbose. Tell you more than you want to know about
how the Curses symbols are being determined.
-l <file> Create file <file> and dump output into it.
EOM
}
__END__
|