1#!/usr/bin/env python
2
3import re,sys
4
5try:
6    maketrans = str.maketrans
7except AttributeError:
8    # For python2
9    from string import maketrans
10
11pats = [
12 [ r"__InClUdE__(.*)", r"#include\1" ],
13 [ r"__IfDeF__ (XEN_HAVE.*)", r"#ifdef \1" ],
14 [ r"__ElSe__", r"#else" ],
15 [ r"__EnDif__", r"#endif" ],
16 [ r"__DeFiNe__", r"#define" ],
17 [ r"__UnDeF__", r"#undef" ],
18 [ r"\"xen-compat.h\"", r"<public/xen-compat.h>" ],
19 [ r"(struct|union|enum)\s+(xen_?)?(\w)", r"\1 compat_\3" ],
20 [ r"typedef(.*)@KeeP@((xen_?)?)([\w]+)([^\w])",
21   r"typedef\1\2\4 __attribute__((__aligned__(__alignof(\1compat_\4))))\5" ],
22 [ r"_t([^\w]|$)", r"_compat_t\1" ],
23 [ r"int(8|16|32|64_aligned)_compat_t([^\w]|$)", r"int\1_t\2" ],
24 [ r"(\su?int64(_compat)?)_T([^\w]|$)", r"\1_t\3" ],
25 [ r"(^|[^\w])xen_?(\w*)_compat_t([^\w]|$$)", r"\1compat_\2_t\3" ],
26 [ r"(^|[^\w])XEN_?", r"\1COMPAT_" ],
27 [ r"(^|[^\w])Xen_?", r"\1Compat_" ],
28 [ r"(^|[^\w])COMPAT_HANDLE_64\(", r"\1XEN_GUEST_HANDLE_64(" ],
29 [ r"(^|[^\w])long([^\w]|$$)", r"\1int\2" ]
30];
31
32output_filename = sys.argv[1]
33
34# tr '[:lower:]-/.' '[:upper:]___'
35header_id = '_' + \
36    output_filename.upper().translate(maketrans('-/.','___'))
37
38header = """#ifndef {0}
39#define {0}
40#include <xen/compat.h>""".format(header_id)
41
42print(header)
43
44if not re.match("compat/arch-.*.h$", output_filename):
45    x = output_filename.replace("compat/","public/")
46    print('#include <%s>' % x)
47
48last_line_empty = False
49for line in sys.stdin.readlines():
50    line = line.rstrip()
51
52    # Remove linemarkers generated by the preprocessor.
53    if re.match(r"^# \d", line):
54        continue
55
56    # De-duplicate empty lines.
57    if len(line) == 0:
58        if not last_line_empty:
59            print(line)
60            last_line_empty = True
61        continue
62    else:
63        last_line_empty = False
64
65    for pat in pats:
66        line = re.sub(pat[0], pat[1], line)
67    print(line)
68
69print("#endif /* %s */" % header_id)
70