1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | # Selectively preprocess #ifdef / #ifndef statements. |
---|
4 | n/a | # Usage: |
---|
5 | n/a | # ifdef [-Dname] ... [-Uname] ... [file] ... |
---|
6 | n/a | # |
---|
7 | n/a | # This scans the file(s), looking for #ifdef and #ifndef preprocessor |
---|
8 | n/a | # commands that test for one of the names mentioned in the -D and -U |
---|
9 | n/a | # options. On standard output it writes a copy of the input file(s) |
---|
10 | n/a | # minus those code sections that are suppressed by the selected |
---|
11 | n/a | # combination of defined/undefined symbols. The #if(n)def/#else/#else |
---|
12 | n/a | # lines themselves (if the #if(n)def tests for one of the mentioned |
---|
13 | n/a | # names) are removed as well. |
---|
14 | n/a | |
---|
15 | n/a | # Features: Arbitrary nesting of recognized and unrecognized |
---|
16 | n/a | # preprocessor statements works correctly. Unrecognized #if* commands |
---|
17 | n/a | # are left in place, so it will never remove too much, only too |
---|
18 | n/a | # little. It does accept whitespace around the '#' character. |
---|
19 | n/a | |
---|
20 | n/a | # Restrictions: There should be no comments or other symbols on the |
---|
21 | n/a | # #if(n)def lines. The effect of #define/#undef commands in the input |
---|
22 | n/a | # file or in included files is not taken into account. Tests using |
---|
23 | n/a | # #if and the defined() pseudo function are not recognized. The #elif |
---|
24 | n/a | # command is not recognized. Improperly nesting is not detected. |
---|
25 | n/a | # Lines that look like preprocessor commands but which are actually |
---|
26 | n/a | # part of comments or string literals will be mistaken for |
---|
27 | n/a | # preprocessor commands. |
---|
28 | n/a | |
---|
29 | n/a | import sys |
---|
30 | n/a | import getopt |
---|
31 | n/a | |
---|
32 | n/a | defs = [] |
---|
33 | n/a | undefs = [] |
---|
34 | n/a | |
---|
35 | n/a | def main(): |
---|
36 | n/a | opts, args = getopt.getopt(sys.argv[1:], 'D:U:') |
---|
37 | n/a | for o, a in opts: |
---|
38 | n/a | if o == '-D': |
---|
39 | n/a | defs.append(a) |
---|
40 | n/a | if o == '-U': |
---|
41 | n/a | undefs.append(a) |
---|
42 | n/a | if not args: |
---|
43 | n/a | args = ['-'] |
---|
44 | n/a | for filename in args: |
---|
45 | n/a | if filename == '-': |
---|
46 | n/a | process(sys.stdin, sys.stdout) |
---|
47 | n/a | else: |
---|
48 | n/a | f = open(filename, 'r') |
---|
49 | n/a | process(f, sys.stdout) |
---|
50 | n/a | f.close() |
---|
51 | n/a | |
---|
52 | n/a | def process(fpi, fpo): |
---|
53 | n/a | keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif') |
---|
54 | n/a | ok = 1 |
---|
55 | n/a | stack = [] |
---|
56 | n/a | while 1: |
---|
57 | n/a | line = fpi.readline() |
---|
58 | n/a | if not line: break |
---|
59 | n/a | while line[-2:] == '\\\n': |
---|
60 | n/a | nextline = fpi.readline() |
---|
61 | n/a | if not nextline: break |
---|
62 | n/a | line = line + nextline |
---|
63 | n/a | tmp = line.strip() |
---|
64 | n/a | if tmp[:1] != '#': |
---|
65 | n/a | if ok: fpo.write(line) |
---|
66 | n/a | continue |
---|
67 | n/a | tmp = tmp[1:].strip() |
---|
68 | n/a | words = tmp.split() |
---|
69 | n/a | keyword = words[0] |
---|
70 | n/a | if keyword not in keywords: |
---|
71 | n/a | if ok: fpo.write(line) |
---|
72 | n/a | continue |
---|
73 | n/a | if keyword in ('ifdef', 'ifndef') and len(words) == 2: |
---|
74 | n/a | if keyword == 'ifdef': |
---|
75 | n/a | ko = 1 |
---|
76 | n/a | else: |
---|
77 | n/a | ko = 0 |
---|
78 | n/a | word = words[1] |
---|
79 | n/a | if word in defs: |
---|
80 | n/a | stack.append((ok, ko, word)) |
---|
81 | n/a | if not ko: ok = 0 |
---|
82 | n/a | elif word in undefs: |
---|
83 | n/a | stack.append((ok, not ko, word)) |
---|
84 | n/a | if ko: ok = 0 |
---|
85 | n/a | else: |
---|
86 | n/a | stack.append((ok, -1, word)) |
---|
87 | n/a | if ok: fpo.write(line) |
---|
88 | n/a | elif keyword == 'if': |
---|
89 | n/a | stack.append((ok, -1, '')) |
---|
90 | n/a | if ok: fpo.write(line) |
---|
91 | n/a | elif keyword == 'else' and stack: |
---|
92 | n/a | s_ok, s_ko, s_word = stack[-1] |
---|
93 | n/a | if s_ko < 0: |
---|
94 | n/a | if ok: fpo.write(line) |
---|
95 | n/a | else: |
---|
96 | n/a | s_ko = not s_ko |
---|
97 | n/a | ok = s_ok |
---|
98 | n/a | if not s_ko: ok = 0 |
---|
99 | n/a | stack[-1] = s_ok, s_ko, s_word |
---|
100 | n/a | elif keyword == 'endif' and stack: |
---|
101 | n/a | s_ok, s_ko, s_word = stack[-1] |
---|
102 | n/a | if s_ko < 0: |
---|
103 | n/a | if ok: fpo.write(line) |
---|
104 | n/a | del stack[-1] |
---|
105 | n/a | ok = s_ok |
---|
106 | n/a | else: |
---|
107 | n/a | sys.stderr.write('Unknown keyword %s\n' % keyword) |
---|
108 | n/a | if stack: |
---|
109 | n/a | sys.stderr.write('stack: %s\n' % stack) |
---|
110 | n/a | |
---|
111 | n/a | if __name__ == '__main__': |
---|
112 | n/a | main() |
---|