2 # Copyright (C) 2008 Stefan Hajnoczi <stefanha@gmail.com>.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of the
7 # License, or any later version.
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 pxenv_status_files
= ('../../src/include/errno.h', )
21 errfile_files
= ('../../src/include/gpxe/errfile.h',
22 '../../src/arch/i386/include/bits/errfile.h')
23 posix_errno_files
= ('../../src/include/errno.h', )
25 PXENV_STATUS_RE
= re
.compile(r
'^#define\s+(PXENV_STATUS_[^\s]+)\s+(.+)$', re
.M
)
26 ERRFILE_RE
= re
.compile(r
'^#define\s+(ERRFILE_[^\s]+)\s+(.+)$', re
.M
)
27 POSIX_ERRNO_RE
= re
.compile(r
'^#define\s+(E[A-Z0-9]+)\s+(?:\\\n)?.*(0x[0-9a-f]+).*$', re
.M
)
30 sys
.stderr
.write('%s: %s\n' % (sys
.argv
[0], msg
))
33 def to_pxenv_status(errno
):
36 def to_errfile(errno
):
37 return (errno
>> 13) & 0x7ff
39 def to_posix_errno(errno
):
40 return (errno
>> 24) & 0x7f
42 def load_header_file(filename
, regexp
):
44 data
= open(filename
, 'r').read()
45 for m
in regexp
.finditer(data
):
50 def evaluate(defines
, expr
):
52 for token
in expr
.split():
55 elif token
.startswith('/*') or token
.startswith('//'):
57 elif token
.startswith('0x') or token
== '|':
61 pyexpr
+= '0x%x' % defines
[token
]
64 if not re
.match(r
'^[0-9a-zA-Z_|]+$', pyexpr
):
65 err('invalid expression')
68 def build(filenames
, regexp
, selector
):
70 for filename
in filenames
:
71 unevaluated
.update(load_header_file(filename
, regexp
))
77 for key
in list(unevaluated
.keys()):
78 val
= evaluate(evaluated
, unevaluated
[key
])
84 err('unable to evaluate all #defines')
87 for key
, val
in evaluated
.iteritems():
88 lookup
[selector(val
)] = key
91 print 'pxenv_status =', repr(build(pxenv_status_files
, PXENV_STATUS_RE
, to_pxenv_status
))
92 print 'errfile =', repr(build(errfile_files
, ERRFILE_RE
, to_errfile
))
93 print 'posix_errno =', repr(build(posix_errno_files
, POSIX_ERRNO_RE
, to_posix_errno
))