| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
import sys |
| 38 |
import re |
| 39 |
import os |
| 40 |
from stat import * |
| 41 |
import getopt |
| 42 |
|
| 43 |
err = sys.stderr.write |
| 44 |
dbg = err |
| 45 |
rep = sys.stdout.write |
| 46 |
|
| 47 |
def usage(): |
| 48 |
progname = sys.argv[0] |
| 49 |
err('Usage: ' + progname + |
| 50 |
' [-c] [-r] [-s file] ... file-or-directory ...\n') |
| 51 |
err('\n') |
| 52 |
err('-c : substitute inside comments\n') |
| 53 |
err('-r : reverse direction for following -s options\n') |
| 54 |
err('-s substfile : add a file of substitutions\n') |
| 55 |
err('\n') |
| 56 |
err('Each non-empty non-comment line in a substitution file must\n') |
| 57 |
err('contain exactly two words: an identifier and its replacement.\n') |
| 58 |
err('Comments start with a # character and end at end of line.\n') |
| 59 |
err('If an identifier is preceded with a *, it is not substituted\n') |
| 60 |
err('inside a comment even when -c is specified.\n') |
| 61 |
|
| 62 |
def main(): |
| 63 |
try: |
| 64 |
opts, args = getopt.getopt(sys.argv[1:], 'crs:') |
| 65 |
except getopt.error, msg: |
| 66 |
err('Options error: ' + str(msg) + '\n') |
| 67 |
usage() |
| 68 |
sys.exit(2) |
| 69 |
bad = 0 |
| 70 |
if not args: |
| 71 |
usage() |
| 72 |
sys.exit(2) |
| 73 |
for opt, arg in opts: |
| 74 |
if opt == '-c': |
| 75 |
setdocomments() |
| 76 |
if opt == '-r': |
| 77 |
setreverse() |
| 78 |
if opt == '-s': |
| 79 |
addsubst(arg) |
| 80 |
for arg in args: |
| 81 |
if os.path.isdir(arg): |
| 82 |
if recursedown(arg): bad = 1 |
| 83 |
elif os.path.islink(arg): |
| 84 |
err(arg + ': will not process symbolic links\n') |
| 85 |
bad = 1 |
| 86 |
else: |
| 87 |
if fix(arg): bad = 1 |
| 88 |
sys.exit(bad) |
| 89 |
|
| 90 |
|
| 91 |
Wanted = r'^[a-zA-Z0-9_]+\.[ch]$' |
| 92 |
def wanted(name): |
| 93 |
return re.match(Wanted, name) |
| 94 |
|
| 95 |
def recursedown(dirname): |
| 96 |
dbg('recursedown(%r)\n' % (dirname,)) |
| 97 |
bad = 0 |
| 98 |
try: |
| 99 |
names = os.listdir(dirname) |
| 100 |
except os.error, msg: |
| 101 |
err(dirname + ': cannot list directory: ' + str(msg) + '\n') |
| 102 |
return 1 |
| 103 |
names.sort() |
| 104 |
subdirs = [] |
| 105 |
for name in names: |
| 106 |
if name in (os.curdir, os.pardir): continue |
| 107 |
fullname = os.path.join(dirname, name) |
| 108 |
if os.path.islink(fullname): pass |
| 109 |
elif os.path.isdir(fullname): |
| 110 |
subdirs.append(fullname) |
| 111 |
elif wanted(name): |
| 112 |
if fix(fullname): bad = 1 |
| 113 |
for fullname in subdirs: |
| 114 |
if recursedown(fullname): bad = 1 |
| 115 |
return bad |
| 116 |
|
| 117 |
def fix(filename): |
| 118 |
|
| 119 |
if filename == '-': |
| 120 |
|
| 121 |
f = sys.stdin |
| 122 |
g = sys.stdout |
| 123 |
else: |
| 124 |
|
| 125 |
try: |
| 126 |
f = open(filename, 'r') |
| 127 |
except IOError, msg: |
| 128 |
err(filename + ': cannot open: ' + str(msg) + '\n') |
| 129 |
return 1 |
| 130 |
head, tail = os.path.split(filename) |
| 131 |
tempname = os.path.join(head, '@' + tail) |
| 132 |
g = None |
| 133 |
|
| 134 |
|
| 135 |
lineno = 0 |
| 136 |
initfixline() |
| 137 |
while 1: |
| 138 |
line = f.readline() |
| 139 |
if not line: break |
| 140 |
lineno = lineno + 1 |
| 141 |
while line[-2:] == '\\\n': |
| 142 |
nextline = f.readline() |
| 143 |
if not nextline: break |
| 144 |
line = line + nextline |
| 145 |
lineno = lineno + 1 |
| 146 |
newline = fixline(line) |
| 147 |
if newline != line: |
| 148 |
if g is None: |
| 149 |
try: |
| 150 |
g = open(tempname, 'w') |
| 151 |
except IOError, msg: |
| 152 |
f.close() |
| 153 |
err(tempname+': cannot create: '+ |
| 154 |
str(msg)+'\n') |
| 155 |
return 1 |
| 156 |
f.seek(0) |
| 157 |
lineno = 0 |
| 158 |
initfixline() |
| 159 |
rep(filename + ':\n') |
| 160 |
continue |
| 161 |
rep(repr(lineno) + '\n') |
| 162 |
rep('< ' + line) |
| 163 |
rep('> ' + newline) |
| 164 |
if g is not None: |
| 165 |
g.write(newline) |
| 166 |
|
| 167 |
|
| 168 |
if filename == '-': return 0 |
| 169 |
f.close() |
| 170 |
if not g: return 0 |
| 171 |
g.close() |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
try: |
| 177 |
statbuf = os.stat(filename) |
| 178 |
os.chmod(tempname, statbuf[ST_MODE] & 07777) |
| 179 |
except os.error, msg: |
| 180 |
err(tempname + ': warning: chmod failed (' + str(msg) + ')\n') |
| 181 |
|
| 182 |
try: |
| 183 |
os.rename(filename, filename + '~') |
| 184 |
except os.error, msg: |
| 185 |
err(filename + ': warning: backup failed (' + str(msg) + ')\n') |
| 186 |
|
| 187 |
try: |
| 188 |
os.rename(tempname, filename) |
| 189 |
except os.error, msg: |
| 190 |
err(filename + ': rename failed (' + str(msg) + ')\n') |
| 191 |
return 1 |
| 192 |
|
| 193 |
return 0 |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
Identifier = '(struct )?[a-zA-Z_][a-zA-Z0-9_]+' |
| 198 |
String = r'"([^\n\\"]|\\.)*"' |
| 199 |
Char = r"'([^\n\\']|\\.)*'" |
| 200 |
CommentStart = r'/\*' |
| 201 |
CommentEnd = r'\*/' |
| 202 |
|
| 203 |
Hexnumber = '0[xX][0-9a-fA-F]*[uUlL]*' |
| 204 |
Octnumber = '0[0-7]*[uUlL]*' |
| 205 |
Decnumber = '[1-9][0-9]*[uUlL]*' |
| 206 |
Intnumber = Hexnumber + '|' + Octnumber + '|' + Decnumber |
| 207 |
Exponent = '[eE][-+]?[0-9]+' |
| 208 |
Pointfloat = r'([0-9]+\.[0-9]*|\.[0-9]+)(' + Exponent + r')?' |
| 209 |
Expfloat = '[0-9]+' + Exponent |
| 210 |
Floatnumber = Pointfloat + '|' + Expfloat |
| 211 |
Number = Floatnumber + '|' + Intnumber |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
OutsideComment = (Identifier, Number, String, Char, CommentStart) |
| 216 |
OutsideCommentPattern = '(' + '|'.join(OutsideComment) + ')' |
| 217 |
OutsideCommentProgram = re.compile(OutsideCommentPattern) |
| 218 |
|
| 219 |
InsideComment = (Identifier, Number, CommentEnd) |
| 220 |
InsideCommentPattern = '(' + '|'.join(InsideComment) + ')' |
| 221 |
InsideCommentProgram = re.compile(InsideCommentPattern) |
| 222 |
|
| 223 |
def initfixline(): |
| 224 |
global Program |
| 225 |
Program = OutsideCommentProgram |
| 226 |
|
| 227 |
def fixline(line): |
| 228 |
global Program |
| 229 |
|
| 230 |
i = 0 |
| 231 |
while i < len(line): |
| 232 |
match = Program.search(line, i) |
| 233 |
if match is None: break |
| 234 |
i = match.start() |
| 235 |
found = match.group(0) |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if len(found) == 2: |
| 240 |
if found == '/*': |
| 241 |
Program = InsideCommentProgram |
| 242 |
elif found == '*/': |
| 243 |
Program = OutsideCommentProgram |
| 244 |
n = len(found) |
| 245 |
if found in Dict: |
| 246 |
subst = Dict[found] |
| 247 |
if Program is InsideCommentProgram: |
| 248 |
if not Docomments: |
| 249 |
print 'Found in comment:', found |
| 250 |
i = i + n |
| 251 |
continue |
| 252 |
if found in NotInComment: |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
subst = found |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
line = line[:i] + subst + line[i+n:] |
| 262 |
n = len(subst) |
| 263 |
i = i + n |
| 264 |
return line |
| 265 |
|
| 266 |
Docomments = 0 |
| 267 |
def setdocomments(): |
| 268 |
global Docomments |
| 269 |
Docomments = 1 |
| 270 |
|
| 271 |
Reverse = 0 |
| 272 |
def setreverse(): |
| 273 |
global Reverse |
| 274 |
Reverse = (not Reverse) |
| 275 |
|
| 276 |
Dict = {} |
| 277 |
NotInComment = {} |
| 278 |
def addsubst(substfile): |
| 279 |
try: |
| 280 |
fp = open(substfile, 'r') |
| 281 |
except IOError, msg: |
| 282 |
err(substfile + ': cannot read substfile: ' + str(msg) + '\n') |
| 283 |
sys.exit(1) |
| 284 |
lineno = 0 |
| 285 |
while 1: |
| 286 |
line = fp.readline() |
| 287 |
if not line: break |
| 288 |
lineno = lineno + 1 |
| 289 |
try: |
| 290 |
i = line.index('#') |
| 291 |
except ValueError: |
| 292 |
i = -1 |
| 293 |
words = line[:i].split() |
| 294 |
if not words: continue |
| 295 |
if len(words) == 3 and words[0] == 'struct': |
| 296 |
words[:2] = [words[0] + ' ' + words[1]] |
| 297 |
elif len(words) != 2: |
| 298 |
err(substfile + '%s:%r: warning: bad line: %r' % (substfile, lineno, line)) |
| 299 |
continue |
| 300 |
if Reverse: |
| 301 |
[value, key] = words |
| 302 |
else: |
| 303 |
[key, value] = words |
| 304 |
if value[0] == '*': |
| 305 |
value = value[1:] |
| 306 |
if key[0] == '*': |
| 307 |
key = key[1:] |
| 308 |
NotInComment[key] = value |
| 309 |
if key in Dict: |
| 310 |
err('%s:%r: warning: overriding: %r %r\n' % (substfile, lineno, key, value)) |
| 311 |
err('%s:%r: warning: previous: %r\n' % (substfile, lineno, Dict[key])) |
| 312 |
Dict[key] = value |
| 313 |
fp.close() |
| 314 |
|
| 315 |
if __name__ == '__main__': |
| 316 |
main() |