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