fwcutter/make: Avoid _DEFAULT_SOURCE warning
[b43-tools.git] / debug / b43-beautifier
1 #!/usr/bin/env python
2 """
3 #  b43 firmware assembly code beautifier
4 #
5 #  Copyright (C) 2008-2010 Michael Buesch <m@bues.ch>
6 #
7 #  This program is free software: you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License version 3
9 #  as published by the Free Software Foundation.
10 #
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 """
19
20 import getopt
21 import sys
22 from libb43 import *
23
24
25 def usage():
26         print "b43 firmware assembly code beautifier"
27         print ""
28         print "Copyright (C) 2008 Michael Buesch <m@bues.ch>"
29         print "Licensed under the GNU/GPL version 3"
30         print ""
31         print "Usage: b43-beautifier [OPTIONS]"
32         print ""
33         print "-h|--help            Print this help text"
34         print "-a|--asmfile [FILE]  Assembly code source file"
35         print "-d|--defs [DIR]      Directory containing the defs files"
36         print ""
37         print "The options  -d  and  -a  are essential."
38         print "The \"include\" directory can be used for --defs"
39
40 def parseArgs():
41         global opt_asmfile
42         global opt_defsfiles
43
44         opt_asmfile = None
45         opt_defsfiles = None
46
47         try:
48                 (opts, args) = getopt.getopt(sys.argv[1:],
49                         "ha:d:",
50                         [ "help", "asmfile=", "defs=" ])
51         except getopt.GetoptError:
52                 usage()
53                 sys.exit(1)
54
55         for (o, v) in opts:
56                 if o in ("-h", "--help"):
57                         usage()
58                         sys.exit(0)
59                 if o in ("-a", "--asmfile"):
60                         opt_asmfile = v
61                 if o in ("-d", "--defs"):
62                         opt_defsfiles = v
63
64         if not opt_asmfile:
65                 print "Must provide --asmfile"
66                 sys.exit(1)
67         if not opt_defsfiles:
68                 print "Must provide --defs"
69                 sys.exit(1)
70
71 def main():
72         parseArgs()
73
74         try:
75                 asm = file(opt_asmfile).read()
76         except IOError, e:
77                 print "Could not read asmfile %s: %s" % (e.filename, e.strerror)
78                 return 1
79         try:
80                 b = B43Beautifier(asm, opt_defsfiles)
81                 sys.stdout.write(b.getAsm())
82         except B43Exception:
83                 return 1
84         return 0
85
86 if __name__ == "__main__":
87         sys.exit(main())
88