1#!/usr/bin/perl
2#
3# This creates sys_errlist from <asm/errno.h> through somewhat
4# heuristic matching.  It presumes the relevant entries are of the form
5# #define Exxxx <integer> /* comment */
6#
7
8use FileHandle;
9
10%errors  = ();
11%errmsg  = ();
12$maxerr  = -1;
13@includelist = ();		# Include directories
14
15sub parse_file($) {
16    my($file) = @_;
17    my($fh) = new FileHandle;
18    my($line, $error, $msg);
19    my($kernelonly) = 0;
20    my($root);
21
22    print STDERR "opening $file\n" unless ( $quiet );
23
24    $ok = 0;
25    foreach $root ( @includelist ) {
26	if ( $fh->open($root.'//'.$file, '<') ) {
27	    $ok = 1;
28	    last;
29	}
30    }
31
32    if ( ! $ok ) {
33	die "$0: Cannot find file $file\n";
34    }
35
36    while ( defined($line = <$fh>) ) {
37	if ( $kernelonly ) {
38	    if ( $line =~ /^\#\s*endif/ ) {
39		$kernelonly--;
40	    } elsif ( $line =~ /^\#\sif/ ) {
41		$kernelonly++;
42	    }
43	} else {
44	    if ( $line =~ /^\#\s*define\s+([A-Z0-9_]+)\s+([0-9]+)\s*\/\*\s*(.*\S)\s*\*\// ) {
45		$error = $1;
46		$errno = $2+0;
47		$msg   = $3;
48		print STDERR "$error ($errno) => \"$msg\"\n" unless ( $quiet );
49		$errors{$errno} = $error;
50		$errmsg{$errno} = $msg;
51		$maxerr = $errno if ( $errno > $maxerr );
52	    } elsif ( $line =~ /^\#\s*include\s+[\<\"](.*)[\>\"]/ ) {
53		parse_file($1);
54	    } elsif ( $line =~ /^\#\s*ifdef\s+__KERNEL__/ ) {
55		$kernelonly++;
56	    }
57	}
58    }
59    close($fh);
60    print STDERR "closing $file\n" unless ( $quiet );
61}
62
63$v = $ENV{'KBUILD_VERBOSE'};
64$quiet = defined($v) ? !$v : 0;
65
66foreach $arg ( @ARGV ) {
67    if ( $arg eq '-q' ) {
68	$quiet = 1;
69    } elsif ( $arg =~ /^-(errlist|errnos|maxerr)$/ ) {
70	$type = $arg;
71    } elsif ( $arg =~ '^\-I' ) {
72	push(@includelist, "$'");
73    } else {
74	# Ignore
75    }
76}
77
78parse_file('errno.h');
79
80if ( $type eq '-errlist' ) {
81    print  "#include <errno.h>\n";
82    printf "const int sys_nerr = %d;\n", $maxerr+1;
83    printf "const char * const sys_errlist[%d] = {\n", $maxerr+1;
84    foreach $e ( sort(keys(%errors)) ) {
85	printf "  [%s] = \"%s\",\n", $errors{$e}, $errmsg{$e};
86    }
87    print "};\n";
88} elsif ( $type eq '-errnos' ) {
89    print  "#include <errno.h>\n";
90    printf "const int sys_nerr = %d;\n", $maxerr+1;
91    printf "const char * const sys_errlist[%d] = {\n", $maxerr+1;
92    foreach $e ( sort(keys(%errors)) ) {
93	printf "  [%s] = \"%s\",\n", $errors{$e}, $errors{$e};
94    }
95    print "};\n";
96} elsif ( $type eq '-maxerr' ) {
97    print $maxerr, "\n";
98}
99