1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | # Perform massive identifier substitution on C source files. |
---|
4 | n/a | # This actually tokenizes the files (to some extent) so it can |
---|
5 | n/a | # avoid making substitutions inside strings or comments. |
---|
6 | n/a | # Inside strings, substitutions are never made; inside comments, |
---|
7 | n/a | # it is a user option (off by default). |
---|
8 | n/a | # |
---|
9 | n/a | # The substitutions are read from one or more files whose lines, |
---|
10 | n/a | # when not empty, after stripping comments starting with #, |
---|
11 | n/a | # must contain exactly two words separated by whitespace: the |
---|
12 | n/a | # old identifier and its replacement. |
---|
13 | n/a | # |
---|
14 | n/a | # The option -r reverses the sense of the substitutions (this may be |
---|
15 | n/a | # useful to undo a particular substitution). |
---|
16 | n/a | # |
---|
17 | n/a | # If the old identifier is prefixed with a '*' (with no intervening |
---|
18 | n/a | # whitespace), then it will not be substituted inside comments. |
---|
19 | n/a | # |
---|
20 | n/a | # Command line arguments are files or directories to be processed. |
---|
21 | n/a | # Directories are searched recursively for files whose name looks |
---|
22 | n/a | # like a C file (ends in .h or .c). The special filename '-' means |
---|
23 | n/a | # operate in filter mode: read stdin, write stdout. |
---|
24 | n/a | # |
---|
25 | n/a | # Symbolic links are always ignored (except as explicit directory |
---|
26 | n/a | # arguments). |
---|
27 | n/a | # |
---|
28 | n/a | # The original files are kept as back-up with a "~" suffix. |
---|
29 | n/a | # |
---|
30 | n/a | # Changes made are reported to stdout in a diff-like format. |
---|
31 | n/a | # |
---|
32 | n/a | # NB: by changing only the function fixline() you can turn this |
---|
33 | n/a | # into a program for different changes to C source files; by |
---|
34 | n/a | # changing the function wanted() you can make a different selection of |
---|
35 | n/a | # files. |
---|
36 | n/a | |
---|
37 | n/a | import sys |
---|
38 | n/a | import re |
---|
39 | n/a | import os |
---|
40 | n/a | from stat import * |
---|
41 | n/a | import getopt |
---|
42 | n/a | |
---|
43 | n/a | err = sys.stderr.write |
---|
44 | n/a | dbg = err |
---|
45 | n/a | rep = sys.stdout.write |
---|
46 | n/a | |
---|
47 | n/a | def usage(): |
---|
48 | n/a | progname = sys.argv[0] |
---|
49 | n/a | err('Usage: ' + progname + |
---|
50 | n/a | ' [-c] [-r] [-s file] ... file-or-directory ...\n') |
---|
51 | n/a | err('\n') |
---|
52 | n/a | err('-c : substitute inside comments\n') |
---|
53 | n/a | err('-r : reverse direction for following -s options\n') |
---|
54 | n/a | err('-s substfile : add a file of substitutions\n') |
---|
55 | n/a | err('\n') |
---|
56 | n/a | err('Each non-empty non-comment line in a substitution file must\n') |
---|
57 | n/a | err('contain exactly two words: an identifier and its replacement.\n') |
---|
58 | n/a | err('Comments start with a # character and end at end of line.\n') |
---|
59 | n/a | err('If an identifier is preceded with a *, it is not substituted\n') |
---|
60 | n/a | err('inside a comment even when -c is specified.\n') |
---|
61 | n/a | |
---|
62 | n/a | def main(): |
---|
63 | n/a | try: |
---|
64 | n/a | opts, args = getopt.getopt(sys.argv[1:], 'crs:') |
---|
65 | n/a | except getopt.error as msg: |
---|
66 | n/a | err('Options error: ' + str(msg) + '\n') |
---|
67 | n/a | usage() |
---|
68 | n/a | sys.exit(2) |
---|
69 | n/a | bad = 0 |
---|
70 | n/a | if not args: # No arguments |
---|
71 | n/a | usage() |
---|
72 | n/a | sys.exit(2) |
---|
73 | n/a | for opt, arg in opts: |
---|
74 | n/a | if opt == '-c': |
---|
75 | n/a | setdocomments() |
---|
76 | n/a | if opt == '-r': |
---|
77 | n/a | setreverse() |
---|
78 | n/a | if opt == '-s': |
---|
79 | n/a | addsubst(arg) |
---|
80 | n/a | for arg in args: |
---|
81 | n/a | if os.path.isdir(arg): |
---|
82 | n/a | if recursedown(arg): bad = 1 |
---|
83 | n/a | elif os.path.islink(arg): |
---|
84 | n/a | err(arg + ': will not process symbolic links\n') |
---|
85 | n/a | bad = 1 |
---|
86 | n/a | else: |
---|
87 | n/a | if fix(arg): bad = 1 |
---|
88 | n/a | sys.exit(bad) |
---|
89 | n/a | |
---|
90 | n/a | # Change this regular expression to select a different set of files |
---|
91 | n/a | Wanted = r'^[a-zA-Z0-9_]+\.[ch]$' |
---|
92 | n/a | def wanted(name): |
---|
93 | n/a | return re.match(Wanted, name) |
---|
94 | n/a | |
---|
95 | n/a | def recursedown(dirname): |
---|
96 | n/a | dbg('recursedown(%r)\n' % (dirname,)) |
---|
97 | n/a | bad = 0 |
---|
98 | n/a | try: |
---|
99 | n/a | names = os.listdir(dirname) |
---|
100 | n/a | except OSError as msg: |
---|
101 | n/a | err(dirname + ': cannot list directory: ' + str(msg) + '\n') |
---|
102 | n/a | return 1 |
---|
103 | n/a | names.sort() |
---|
104 | n/a | subdirs = [] |
---|
105 | n/a | for name in names: |
---|
106 | n/a | if name in (os.curdir, os.pardir): continue |
---|
107 | n/a | fullname = os.path.join(dirname, name) |
---|
108 | n/a | if os.path.islink(fullname): pass |
---|
109 | n/a | elif os.path.isdir(fullname): |
---|
110 | n/a | subdirs.append(fullname) |
---|
111 | n/a | elif wanted(name): |
---|
112 | n/a | if fix(fullname): bad = 1 |
---|
113 | n/a | for fullname in subdirs: |
---|
114 | n/a | if recursedown(fullname): bad = 1 |
---|
115 | n/a | return bad |
---|
116 | n/a | |
---|
117 | n/a | def fix(filename): |
---|
118 | n/a | ## dbg('fix(%r)\n' % (filename,)) |
---|
119 | n/a | if filename == '-': |
---|
120 | n/a | # Filter mode |
---|
121 | n/a | f = sys.stdin |
---|
122 | n/a | g = sys.stdout |
---|
123 | n/a | else: |
---|
124 | n/a | # File replacement mode |
---|
125 | n/a | try: |
---|
126 | n/a | f = open(filename, 'r') |
---|
127 | n/a | except IOError as msg: |
---|
128 | n/a | err(filename + ': cannot open: ' + str(msg) + '\n') |
---|
129 | n/a | return 1 |
---|
130 | n/a | head, tail = os.path.split(filename) |
---|
131 | n/a | tempname = os.path.join(head, '@' + tail) |
---|
132 | n/a | g = None |
---|
133 | n/a | # If we find a match, we rewind the file and start over but |
---|
134 | n/a | # now copy everything to a temp file. |
---|
135 | n/a | lineno = 0 |
---|
136 | n/a | initfixline() |
---|
137 | n/a | while 1: |
---|
138 | n/a | line = f.readline() |
---|
139 | n/a | if not line: break |
---|
140 | n/a | lineno = lineno + 1 |
---|
141 | n/a | while line[-2:] == '\\\n': |
---|
142 | n/a | nextline = f.readline() |
---|
143 | n/a | if not nextline: break |
---|
144 | n/a | line = line + nextline |
---|
145 | n/a | lineno = lineno + 1 |
---|
146 | n/a | newline = fixline(line) |
---|
147 | n/a | if newline != line: |
---|
148 | n/a | if g is None: |
---|
149 | n/a | try: |
---|
150 | n/a | g = open(tempname, 'w') |
---|
151 | n/a | except IOError as msg: |
---|
152 | n/a | f.close() |
---|
153 | n/a | err(tempname+': cannot create: '+ |
---|
154 | n/a | str(msg)+'\n') |
---|
155 | n/a | return 1 |
---|
156 | n/a | f.seek(0) |
---|
157 | n/a | lineno = 0 |
---|
158 | n/a | initfixline() |
---|
159 | n/a | rep(filename + ':\n') |
---|
160 | n/a | continue # restart from the beginning |
---|
161 | n/a | rep(repr(lineno) + '\n') |
---|
162 | n/a | rep('< ' + line) |
---|
163 | n/a | rep('> ' + newline) |
---|
164 | n/a | if g is not None: |
---|
165 | n/a | g.write(newline) |
---|
166 | n/a | |
---|
167 | n/a | # End of file |
---|
168 | n/a | if filename == '-': return 0 # Done in filter mode |
---|
169 | n/a | f.close() |
---|
170 | n/a | if not g: return 0 # No changes |
---|
171 | n/a | g.close() |
---|
172 | n/a | |
---|
173 | n/a | # Finishing touch -- move files |
---|
174 | n/a | |
---|
175 | n/a | # First copy the file's mode to the temp file |
---|
176 | n/a | try: |
---|
177 | n/a | statbuf = os.stat(filename) |
---|
178 | n/a | os.chmod(tempname, statbuf[ST_MODE] & 0o7777) |
---|
179 | n/a | except OSError as msg: |
---|
180 | n/a | err(tempname + ': warning: chmod failed (' + str(msg) + ')\n') |
---|
181 | n/a | # Then make a backup of the original file as filename~ |
---|
182 | n/a | try: |
---|
183 | n/a | os.rename(filename, filename + '~') |
---|
184 | n/a | except OSError as msg: |
---|
185 | n/a | err(filename + ': warning: backup failed (' + str(msg) + ')\n') |
---|
186 | n/a | # Now move the temp file to the original file |
---|
187 | n/a | try: |
---|
188 | n/a | os.rename(tempname, filename) |
---|
189 | n/a | except OSError as msg: |
---|
190 | n/a | err(filename + ': rename failed (' + str(msg) + ')\n') |
---|
191 | n/a | return 1 |
---|
192 | n/a | # Return success |
---|
193 | n/a | return 0 |
---|
194 | n/a | |
---|
195 | n/a | # Tokenizing ANSI C (partly) |
---|
196 | n/a | |
---|
197 | n/a | Identifier = '(struct )?[a-zA-Z_][a-zA-Z0-9_]+' |
---|
198 | n/a | String = r'"([^\n\\"]|\\.)*"' |
---|
199 | n/a | Char = r"'([^\n\\']|\\.)*'" |
---|
200 | n/a | CommentStart = r'/\*' |
---|
201 | n/a | CommentEnd = r'\*/' |
---|
202 | n/a | |
---|
203 | n/a | Hexnumber = '0[xX][0-9a-fA-F]*[uUlL]*' |
---|
204 | n/a | Octnumber = '0[0-7]*[uUlL]*' |
---|
205 | n/a | Decnumber = '[1-9][0-9]*[uUlL]*' |
---|
206 | n/a | Intnumber = Hexnumber + '|' + Octnumber + '|' + Decnumber |
---|
207 | n/a | Exponent = '[eE][-+]?[0-9]+' |
---|
208 | n/a | Pointfloat = r'([0-9]+\.[0-9]*|\.[0-9]+)(' + Exponent + r')?' |
---|
209 | n/a | Expfloat = '[0-9]+' + Exponent |
---|
210 | n/a | Floatnumber = Pointfloat + '|' + Expfloat |
---|
211 | n/a | Number = Floatnumber + '|' + Intnumber |
---|
212 | n/a | |
---|
213 | n/a | # Anything else is an operator -- don't list this explicitly because of '/*' |
---|
214 | n/a | |
---|
215 | n/a | OutsideComment = (Identifier, Number, String, Char, CommentStart) |
---|
216 | n/a | OutsideCommentPattern = '(' + '|'.join(OutsideComment) + ')' |
---|
217 | n/a | OutsideCommentProgram = re.compile(OutsideCommentPattern) |
---|
218 | n/a | |
---|
219 | n/a | InsideComment = (Identifier, Number, CommentEnd) |
---|
220 | n/a | InsideCommentPattern = '(' + '|'.join(InsideComment) + ')' |
---|
221 | n/a | InsideCommentProgram = re.compile(InsideCommentPattern) |
---|
222 | n/a | |
---|
223 | n/a | def initfixline(): |
---|
224 | n/a | global Program |
---|
225 | n/a | Program = OutsideCommentProgram |
---|
226 | n/a | |
---|
227 | n/a | def fixline(line): |
---|
228 | n/a | global Program |
---|
229 | n/a | ## print('-->', repr(line)) |
---|
230 | n/a | i = 0 |
---|
231 | n/a | while i < len(line): |
---|
232 | n/a | match = Program.search(line, i) |
---|
233 | n/a | if match is None: break |
---|
234 | n/a | i = match.start() |
---|
235 | n/a | found = match.group(0) |
---|
236 | n/a | ## if Program is InsideCommentProgram: print(end='... ') |
---|
237 | n/a | ## else: print(end=' ') |
---|
238 | n/a | ## print(found) |
---|
239 | n/a | if len(found) == 2: |
---|
240 | n/a | if found == '/*': |
---|
241 | n/a | Program = InsideCommentProgram |
---|
242 | n/a | elif found == '*/': |
---|
243 | n/a | Program = OutsideCommentProgram |
---|
244 | n/a | n = len(found) |
---|
245 | n/a | if found in Dict: |
---|
246 | n/a | subst = Dict[found] |
---|
247 | n/a | if Program is InsideCommentProgram: |
---|
248 | n/a | if not Docomments: |
---|
249 | n/a | print('Found in comment:', found) |
---|
250 | n/a | i = i + n |
---|
251 | n/a | continue |
---|
252 | n/a | if found in NotInComment: |
---|
253 | n/a | ## print(end='Ignored in comment: ') |
---|
254 | n/a | ## print(found, '-->', subst) |
---|
255 | n/a | ## print('Line:', line, end='') |
---|
256 | n/a | subst = found |
---|
257 | n/a | ## else: |
---|
258 | n/a | ## print(end='Substituting in comment: ') |
---|
259 | n/a | ## print(found, '-->', subst) |
---|
260 | n/a | ## print('Line:', line, end='') |
---|
261 | n/a | line = line[:i] + subst + line[i+n:] |
---|
262 | n/a | n = len(subst) |
---|
263 | n/a | i = i + n |
---|
264 | n/a | return line |
---|
265 | n/a | |
---|
266 | n/a | Docomments = 0 |
---|
267 | n/a | def setdocomments(): |
---|
268 | n/a | global Docomments |
---|
269 | n/a | Docomments = 1 |
---|
270 | n/a | |
---|
271 | n/a | Reverse = 0 |
---|
272 | n/a | def setreverse(): |
---|
273 | n/a | global Reverse |
---|
274 | n/a | Reverse = (not Reverse) |
---|
275 | n/a | |
---|
276 | n/a | Dict = {} |
---|
277 | n/a | NotInComment = {} |
---|
278 | n/a | def addsubst(substfile): |
---|
279 | n/a | try: |
---|
280 | n/a | fp = open(substfile, 'r') |
---|
281 | n/a | except IOError as msg: |
---|
282 | n/a | err(substfile + ': cannot read substfile: ' + str(msg) + '\n') |
---|
283 | n/a | sys.exit(1) |
---|
284 | n/a | lineno = 0 |
---|
285 | n/a | while 1: |
---|
286 | n/a | line = fp.readline() |
---|
287 | n/a | if not line: break |
---|
288 | n/a | lineno = lineno + 1 |
---|
289 | n/a | try: |
---|
290 | n/a | i = line.index('#') |
---|
291 | n/a | except ValueError: |
---|
292 | n/a | i = -1 # Happens to delete trailing \n |
---|
293 | n/a | words = line[:i].split() |
---|
294 | n/a | if not words: continue |
---|
295 | n/a | if len(words) == 3 and words[0] == 'struct': |
---|
296 | n/a | words[:2] = [words[0] + ' ' + words[1]] |
---|
297 | n/a | elif len(words) != 2: |
---|
298 | n/a | err(substfile + '%s:%r: warning: bad line: %r' % (substfile, lineno, line)) |
---|
299 | n/a | continue |
---|
300 | n/a | if Reverse: |
---|
301 | n/a | [value, key] = words |
---|
302 | n/a | else: |
---|
303 | n/a | [key, value] = words |
---|
304 | n/a | if value[0] == '*': |
---|
305 | n/a | value = value[1:] |
---|
306 | n/a | if key[0] == '*': |
---|
307 | n/a | key = key[1:] |
---|
308 | n/a | NotInComment[key] = value |
---|
309 | n/a | if key in Dict: |
---|
310 | n/a | err('%s:%r: warning: overriding: %r %r\n' % (substfile, lineno, key, value)) |
---|
311 | n/a | err('%s:%r: warning: previous: %r\n' % (substfile, lineno, Dict[key])) |
---|
312 | n/a | Dict[key] = value |
---|
313 | n/a | fp.close() |
---|
314 | n/a | |
---|
315 | n/a | if __name__ == '__main__': |
---|
316 | n/a | main() |
---|