chiark / gitweb /
[PATCH] klibc: version 1.0.7
[elogind.git] / klibc / klibc / syscalls.pl
1 #!/usr/bin/perl
2 #
3 # Script to parse the SYSCALLS file and generate appropriate
4 # stubs.
5
6 $v = $ENV{'KBUILD_VERBOSE'};
7 $quiet = defined($v) ? !$v : 0;
8
9 @args = ();
10 for $arg ( @ARGV ) {
11     if ( $arg =~ /^-/ ) {
12         if ( $arg eq '-q' ) {
13             $quiet = 1;
14         } else {
15             die "$0: Unknown option: $arg\n";
16         }
17     } else {
18         push(@args, $arg);
19     }
20 }
21 ($file, $sysstub, $arch, $bits, $unistd, $outputdir, $havesyscall) = @args;
22
23 require "$sysstub";
24
25 if (!open(UNISTD, '<', $unistd)) {
26     die "$0: $unistd: $!\n";
27 }
28
29 while ( defined($line = <UNISTD>) ) {
30     chomp $line;
31
32     if ( $line =~ /^\#\s*define\s+__NR_([A-Za-z0-9_]+)\s+(.*\S)\s*$/ ) {
33         $syscalls{$1} = $2;
34         print STDERR "SYSCALL FOUND: $1\n" unless ( $quiet );
35     }
36 }
37 close(UNISTD);
38
39 if (!open(HAVESYS, '>', $havesyscall)) {
40     die "$0: $havesyscall: $!\n";
41 }
42
43 print HAVESYS "#ifndef _KLIBC_HAVESYSCALL_H\n";
44 print HAVESYS "#define _KLIBC_HAVESYSCALL_H 1\n\n";
45
46 if (!open(FILE, '<', $file)) {
47     die "$0: $file: $!\n";
48 }
49
50 print "syscall-objs := ";
51
52 while ( defined($line = <FILE>) ) {
53     chomp $line;
54     $line =~ s/\s*(|[\#;].*)$//; # Strip comments and trailing blanks
55     next unless $line;
56
57     if ( $line =~ /^\s*(\<[^\>]+\>\s+|)([A-Za-z0-9_\*\s]+)\s+([A-Za-z0-9_,]+)(|\@[A-Za-z0-9_]+)(|\:\:[A-Za-z0-9_]+)\s*\(([^\:\)]*)\)\s*$/ ) {
58         $archs  = $1;
59         $type   = $2;
60         $snames = $3;
61         $stype  = $4;
62         $fname  = $5;
63         $argv   = $6;
64
65         $doit  = 1;
66         $maybe = 0;
67         if ( $archs ne '' ) {
68             die "$file:$.: Invalid architecture spec: <$archs>\n"
69                 unless ( $archs =~ /^\<(|\?)(|\!)([^\>\!\?]*)\>/ );
70             $maybe = $1 ne '';
71             $not = $2 ne '';
72             $list = $3;
73
74             $doit = $not || ($list eq '');
75
76             @list = split(/,/, $list);
77             foreach  $a ( @list ) {
78                 if ( $a eq $arch || $a eq $bits ) {
79                     $doit = !$not;
80                     last;
81                 }
82             }
83         }
84         next if ( ! $doit );
85
86         undef $sname;
87         foreach $sn ( split(/,/, $snames) ) {
88             if ( defined $syscalls{$sn} ) {
89                 $sname = $sn;
90                 last;
91             }
92         }
93         if ( !defined($sname) ) {
94             next if ( $maybe );
95             die "$file:$.: Undefined system call: $snames\n";
96         }
97
98         $type  =~ s/\s*$//;
99         $stype =~ s/^\@//;
100
101         if ( $fname eq '' ) {
102             $fname = $sname;
103         } else {
104             $fname =~ s/^\:\://;
105         }
106
107         @args = split(/\s*\,\s*/, $argv);
108
109         print HAVESYS "#define _KLIBC_HAVE_SYSCALL_${fname} ${sname}\n";
110         print " \\\n\t${fname}.o";
111         make_sysstub($outputdir, $fname, $type, $sname, $stype, @args);
112     } else {
113         die "$file:$.: Could not parse input: \"$line\"\n";
114     }
115 }
116
117 print "\n";
118
119 print HAVESYS "\n#endif\n";
120 close(HAVESYS);