Re: Time::HiRes qw(sleep) fails
[p5sagit/p5-mst-13.2.git] / ext / Errno / Errno_pm.PL
CommitLineData
eab60bb1 1use ExtUtils::MakeMaker;
2use Config;
3use strict;
4
76fbd8c4 5our $VERSION = "1.09_00";
eab60bb1 6
7my %err = ();
8
9unlink "Errno.pm" if -f "Errno.pm";
10open OUT, ">Errno.pm" or die "Cannot open Errno.pm: $!";
11select OUT;
12my $file;
13foreach $file (get_files()) {
14 process_file($file);
15}
16write_errno_pm();
17unlink "errno.c" if -f "errno.c";
18
19sub process_file {
20 my($file) = @_;
21
6683669a 22 return unless defined $file and -f $file;
eab60bb1 23
24 local *FH;
25 if (($^O eq 'VMS') && ($Config{vms_cc_type} ne 'gnuc')) {
26 unless(open(FH," LIBRARY/EXTRACT=ERRNO/OUTPUT=SYS\$OUTPUT $file |")) {
27 warn "Cannot open '$file'";
28 return;
29 }
7237d657 30 } elsif ($Config{gccversion} ne ''
8ff7320b 31 # OpenSTEP has gcc 2.7.2.1 which recognizes but
32 # doesn't implement the -dM flag.
7237d657 33 && $^O ne 'openstep' && $^O ne 'next'
8ff7320b 34 ) {
9ae2e8df 35 # With the -dM option, gcc outputs every #define it finds
ee1e7ac2 36 unless(open(FH,"$Config{cc} -E -dM $Config{cppflags} $file |")) {
9ae2e8df 37 warn "Cannot open '$file'";
38 return;
39 }
eab60bb1 40 } else {
41 unless(open(FH,"< $file")) {
def887e8 42 # This file could be a temporary file created by cppstdin
6ad8d9a8 43 # so only warn under -w, and return
44 warn "Cannot open '$file'" if $^W;
eab60bb1 45 return;
46 }
47 }
db5fd395 48
49 if ($^O eq 'MacOS') {
50 while(<FH>) {
51 $err{$1} = $2
52 if /^\s*#\s*define\s+(E\w+)\s+(\d+)/;
53 }
54 } else {
55 while(<FH>) {
56 $err{$1} = 1
57 if /^\s*#\s*define\s+(E\w+)\s+/;
58 }
59 }
60 close(FH);
eab60bb1 61}
62
def887e8 63my $cppstdin;
64
65sub default_cpp {
66 unless (defined $cppstdin) {
67 use File::Spec;
68 $cppstdin = $Config{cppstdin};
69 my $upup_cppstdin = File::Spec->catfile(File::Spec->updir,
70 File::Spec->updir,
71 "cppstdin");
72 my $cppstdin_is_wrapper =
73 ($cppstdin eq 'cppstdin'
74 and -f $upup_cppstdin
75 and -x $upup_cppstdin);
76 $cppstdin = $upup_cppstdin if $cppstdin_is_wrapper;
77 }
78 return "$cppstdin $Config{cppflags} $Config{cppminus}";
79}
80
eab60bb1 81sub get_files {
82 my %file = ();
83 # VMS keeps its include files in system libraries (well, except for Gcc)
84 if ($^O eq 'VMS') {
85 if ($Config{vms_cc_type} eq 'decc') {
86 $file{'Sys$Library:DECC$RTLDEF.TLB'} = 1;
87 } elsif ($Config{vms_cc_type} eq 'vaxc') {
88 $file{'Sys$Library:vaxcdef.tlb'} = 1;
89 } elsif ($Config{vms_cc_type} eq 'gcc') {
90 $file{'gnu_cc_include:[000000]errno.h'} = 1;
91 }
9d116dd7 92 } elsif ($^O eq 'os390') {
93 # OS/390 C compiler doesn't generate #file or #line directives
94 $file{'/usr/include/errno.h'} = 1;
092bebab 95 } elsif ($^O eq 'vmesa') {
96 # OS/390 C compiler doesn't generate #file or #line directives
97 $file{'../../vmesa/errno.h'} = 1;
a367e475 98 } elsif ($Config{archname} eq 'epoc') {
99 # Watch out for cross compiling for EPOC (usually done on linux)
100 $file{'/usr/local/epoc/include/libc/sys/errno.h'} = 1;
9ae2e8df 101 } elsif ($^O eq 'linux') {
102 # Some Linuxes have weird errno.hs which generate
103 # no #file or #line directives
104 $file{'/usr/include/errno.h'} = 1;
db5fd395 105 } elsif ($^O eq 'MacOS') {
106 # note that we are only getting the GUSI errno's here ...
107 # we might miss out on compiler-specific ones
108 $file{"$ENV{GUSI}include:sys:errno.h"} = 1;
109
eab60bb1 110 } else {
111 open(CPPI,"> errno.c") or
112 die "Cannot open errno.c";
113
2986a63f 114 if ($^O eq 'NetWare') {
115 print CPPI "#include <nwerrno.h>\n";
116 } else {
117 print CPPI "#include <errno.h>\n";
118 }
eab60bb1 119
120 close(CPPI);
121
122 # invoke CPP and read the output
2986a63f 123 if ($^O eq 'MSWin32' || $^O eq 'NetWare') {
6ad8d9a8 124 open(CPPO,"$Config{cpprun} $Config{cppflags} errno.c |") or
125 die "Cannot run '$Config{cpprun} $Config{cppflags} errno.c'";
126 } else {
def887e8 127 my $cpp = default_cpp();
6ad8d9a8 128 open(CPPO,"$cpp < errno.c |") or
129 die "Cannot exec $cpp";
130 }
eab60bb1 131
132 my $pat;
2986a63f 133 if (($^O eq 'MSWin32' || $^O eq 'NetWare') and $Config{cc} =~ /^bcc/i) {
eab60bb1 134 $pat = '^/\*\s+(.+)\s+\d+\s*:\s+\*/';
135 }
136 else {
d0d1d9b9 137 $pat = '^#\s*(?:line)?\s*\d+\s+"([^"]+)"';
eab60bb1 138 }
139 while(<CPPO>) {
2986a63f 140 if ($^O eq 'os2' or $^O eq 'MSWin32' or $^O eq 'NetWare') {
0196e43b 141 if (/$pat/o) {
142 my $f = $1;
143 $f =~ s,\\\\,/,g;
144 $file{$f} = 1;
145 }
146 }
147 else {
148 $file{$1} = 1 if /$pat/o;
149 }
eab60bb1 150 }
151 close(CPPO);
152 }
153 return keys %file;
154}
155
156sub write_errno_pm {
157 my $err;
158
def887e8 159 # quick sanity check
160
161 die "No error definitions found" unless keys %err;
162
eab60bb1 163 # create the CPP input
164
165 open(CPPI,"> errno.c") or
166 die "Cannot open errno.c";
167
2986a63f 168 if ($^O eq 'NetWare') {
169 print CPPI "#include <nwerrno.h>\n";
170 } else {
171 print CPPI "#include <errno.h>\n";
172 }
eab60bb1 173
174 foreach $err (keys %err) {
175 print CPPI '"',$err,'" [[',$err,']]',"\n";
176 }
177
178 close(CPPI);
179
db5fd395 180 unless ($^O eq 'MacOS') { # trust what we have
eab60bb1 181 # invoke CPP and read the output
dcbf900d 182
db5fd395 183 if ($^O eq 'VMS') {
184 my $cpp = "$Config{cppstdin} $Config{cppflags} $Config{cppminus}";
185 $cpp =~ s/sys\$input//i;
186 open(CPPO,"$cpp errno.c |") or
187 die "Cannot exec $Config{cppstdin}";
2986a63f 188 } elsif ($^O eq 'MSWin32' || $^O eq 'NetWare') {
db5fd395 189 open(CPPO,"$Config{cpprun} $Config{cppflags} errno.c |") or
190 die "Cannot run '$Config{cpprun} $Config{cppflags} errno.c'";
191 } else {
192 my $cpp = default_cpp();
193 open(CPPO,"$cpp < errno.c |")
194 or die "Cannot exec $cpp";
195 }
eab60bb1 196
db5fd395 197 %err = ();
eab60bb1 198
db5fd395 199 while(<CPPO>) {
200 my($name,$expr);
201 next unless ($name, $expr) = /"(.*?)"\s*\[\s*\[\s*(.*?)\s*\]\s*\]/;
202 next if $name eq $expr;
203 $err{$name} = eval $expr;
204 }
205 close(CPPO);
eab60bb1 206 }
eab60bb1 207
208 # Write Errno.pm
209
210 print <<"EDQ";
211#
212# This file is auto-generated. ***ANY*** changes here will be lost
213#
214
215package Errno;
f168a5e7 216our (\@EXPORT_OK,\%EXPORT_TAGS,\@ISA,\$VERSION,\%errno,\$AUTOLOAD);
eab60bb1 217use Exporter ();
218use Config;
219use strict;
220
8bc9d590 221"\$Config{'archname'}-\$Config{'osvers'}" eq
222"$Config{'archname'}-$Config{'osvers'}" or
81be85b8 223 die "Errno architecture ($Config{'archname'}-$Config{'osvers'}) does not match executable architecture (\$Config{'archname'}-\$Config{'osvers'})";
eab60bb1 224
225\$VERSION = "$VERSION";
226\@ISA = qw(Exporter);
227
228EDQ
229
230 my $len = 0;
231 my @err = sort { $err{$a} <=> $err{$b} } keys %err;
232 map { $len = length if length > $len } @err;
233
234 my $j = "\@EXPORT_OK = qw(" . join(" ",keys %err) . ");\n";
235 $j =~ s/(.{50,70})\s/$1\n\t/g;
236 print $j,"\n";
237
238print <<'ESQ';
239%EXPORT_TAGS = (
240 POSIX => [qw(
241ESQ
242
243 my $k = join(" ", grep { exists $err{$_} }
244 qw(E2BIG EACCES EADDRINUSE EADDRNOTAVAIL EAFNOSUPPORT
245 EAGAIN EALREADY EBADF EBUSY ECHILD ECONNABORTED
246 ECONNREFUSED ECONNRESET EDEADLK EDESTADDRREQ EDOM EDQUOT
247 EEXIST EFAULT EFBIG EHOSTDOWN EHOSTUNREACH EINPROGRESS
248 EINTR EINVAL EIO EISCONN EISDIR ELOOP EMFILE EMLINK
249 EMSGSIZE ENAMETOOLONG ENETDOWN ENETRESET ENETUNREACH
250 ENFILE ENOBUFS ENODEV ENOENT ENOEXEC ENOLCK ENOMEM
251 ENOPROTOOPT ENOSPC ENOSYS ENOTBLK ENOTCONN ENOTDIR
252 ENOTEMPTY ENOTSOCK ENOTTY ENXIO EOPNOTSUPP EPERM
253 EPFNOSUPPORT EPIPE EPROCLIM EPROTONOSUPPORT EPROTOTYPE
254 ERANGE EREMOTE ERESTART EROFS ESHUTDOWN ESOCKTNOSUPPORT
255 ESPIPE ESRCH ESTALE ETIMEDOUT ETOOMANYREFS ETXTBSY
256 EUSERS EWOULDBLOCK EXDEV));
257
258 $k =~ s/(.{50,70})\s/$1\n\t/g;
259 print "\t",$k,"\n )]\n);\n\n";
260
261 foreach $err (@err) {
262 printf "sub %s () { %d }\n",,$err,$err{$err};
263 }
264
265 print <<'ESQ';
266
267sub TIEHASH { bless [] }
268
269sub FETCH {
270 my ($self, $errname) = @_;
271 my $proto = prototype("Errno::$errname");
68301627 272 my $errno = "";
eab60bb1 273 if (defined($proto) && $proto eq "") {
274 no strict 'refs';
68301627 275 $errno = &$errname;
276 $errno = 0 unless $! == $errno;
eab60bb1 277 }
68301627 278 return $errno;
93014de6 279}
eab60bb1 280
281sub STORE {
282 require Carp;
283 Carp::confess("ERRNO hash is read only!");
284}
285
286*CLEAR = \&STORE;
287*DELETE = \&STORE;
288
289sub NEXTKEY {
290 my($k,$v);
291 while(($k,$v) = each %Errno::) {
292 my $proto = prototype("Errno::$k");
293 last if (defined($proto) && $proto eq "");
eab60bb1 294 }
295 $k
296}
297
298sub FIRSTKEY {
68301627 299 my $s = scalar keys %Errno::; # initialize iterator
eab60bb1 300 goto &NEXTKEY;
301}
302
303sub EXISTS {
304 my ($self, $errname) = @_;
305 my $proto = prototype($errname);
306 defined($proto) && $proto eq "";
307}
308
309tie %!, __PACKAGE__;
310
3111;
312__END__
313
314=head1 NAME
315
316Errno - System errno constants
317
318=head1 SYNOPSIS
319
320 use Errno qw(EINTR EIO :POSIX);
321
322=head1 DESCRIPTION
323
324C<Errno> defines and conditionally exports all the error constants
325defined in your system C<errno.h> include file. It has a single export
326tag, C<:POSIX>, which will export all POSIX defined error numbers.
327
93014de6 328C<Errno> also makes C<%!> magic such that each element of C<%!> has a
68301627 329non-zero value only if C<$!> is set to that value. For example:
eab60bb1 330
331 use Errno;
3cb6de81 332
eab60bb1 333 unless (open(FH, "/fangorn/spouse")) {
334 if ($!{ENOENT}) {
335 warn "Get a wife!\n";
336 } else {
337 warn "This path is barred: $!";
338 }
339 }
340
68301627 341If a specified constant C<EFOO> does not exist on the system, C<$!{EFOO}>
342returns C<"">. You may use C<exists $!{EFOO}> to check whether the
93014de6 343constant is available on the system.
344
68301627 345=head1 CAVEATS
346
347Importing a particular constant may not be very portable, because the
348import will fail on platforms that do not have that constant. A more
349portable way to set C<$!> to a valid value is to use:
350
351 if (exists &Errno::EFOO) {
352 $! = &Errno::EFOO;
353 }
354
eab60bb1 355=head1 AUTHOR
356
357Graham Barr <gbarr@pobox.com>
358
359=head1 COPYRIGHT
360
361Copyright (c) 1997-8 Graham Barr. All rights reserved.
362This program is free software; you can redistribute it and/or modify it
363under the same terms as Perl itself.
364
365=cut
366
367ESQ
368
369}