chiark / gitweb /
[PATCH] fix klibc's broken strlcpy/strlcat
[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, $arch, $bits, $unistd, $havesyscall) = @args;
22
23 require "arch/$arch/sysstub.ph";
24
25 if (!open(UNISTD, '<', $unistd)) {
26     die "$0: $unistd: $!\n";
27 }
28 while ( defined($line = <UNISTD>) ) {
29     chomp $line;
30
31     if ( $line =~ /^\#\s*define\s+__NR_([A-Za-z0-9_]+)\s+(.*\S)\s*$/ ) {
32         $syscalls{$1} = $2;
33         print STDERR "SYSCALL FOUND: $1\n" unless ( $quiet );
34     }
35 }
36 close(UNISTD);
37
38 if (!open(HAVESYS, '>', $havesyscall)) {
39     die "$0: $havesyscall: $!\n";
40 }
41
42 print HAVESYS "#ifndef _KLIBC_HAVESYSCALL_H\n";
43 print HAVESYS "#define _KLIBC_HAVESYSCALL_H 1\n\n";
44
45 if (!open(FILE, '<', $file)) {
46     die "$0: $file: $!\n";
47 }
48
49 while ( defined($line = <FILE>) ) {
50     chomp $line;
51     $line =~ s/\s*(|[\#;].*)$//; # Strip comments and trailing blanks
52     next unless $line;
53
54     if ( $line =~ /^\s*(\<[^\>]+\>\s+|)([A-Za-z0-9_\*\s]+)\s+([A-Za-z0-9_,]+)(|\@[A-Za-z0-9_]+)(|\:\:[A-Za-z0-9_]+)\s*\(([^\:\)]*)\)\s*$/ ) {
55         $archs  = $1;
56         $type   = $2;
57         $snames = $3;
58         $stype  = $4;
59         $fname  = $5;
60         $argv   = $6;
61
62         $doit  = 1;
63         $maybe = 0;
64         if ( $archs ne '' ) {
65             die "$file:$.: Invalid architecture spec: <$archs>\n"
66                 unless ( $archs =~ /^\<(|\?)(|\!)([^\>\!\?]*)\>/ );
67             $maybe = $1 ne '';
68             $not = $2 ne '';
69             $list = $3;
70
71             $doit = $not || ($list eq '');
72
73             @list = split(/,/, $list);
74             foreach  $a ( @list ) {
75                 if ( $a eq $arch || $a eq $bits ) {
76                     $doit = !$not;
77                     last;
78                 }
79             }
80         }
81         next if ( ! $doit );
82
83         undef $sname;
84         foreach $sn ( split(/,/, $snames) ) {
85             if ( defined $syscalls{$sn} ) {
86                 $sname = $sn;
87                 last;
88             }
89         }
90         if ( !defined($sname) ) {
91             next if ( $maybe );
92             die "$file:$.: Undefined system call: $snames\n";
93         }
94
95         $type  =~ s/\s*$//;
96         $stype =~ s/^\@//;
97
98         if ( $fname eq '' ) {
99             $fname = $sname;
100         } else {
101             $fname =~ s/^\:\://;
102         }
103
104         @args = split(/\s*\,\s*/, $argv);
105
106         print HAVESYS "#define _KLIBC_HAVE_SYSCALL_${fname} ${sname}\n";
107         make_sysstub($fname, $type, $sname, $stype, @args);
108     } else {
109         die "$file:$.: Could not parse input: \"$line\"\n";
110     }
111 }
112
113 print HAVESYS "\n#endif\n";
114 close(HAVESYS);