Improved some error messages for command line processing.
[python/dscho.git] / BeOS / ar-1.1 / docs / dumpo.py
blob91bd8db699fb4e5be2b2907d3146b7bc7fc3267d
1 #! /bin/env python
2 """ Dump data about a Metrowerks object file.
4 Based on reverse-engineering the library file format, since the docs are
5 wrong.
7 Copyright (C) 1997 Chris Herborth (chrish@qnx.com)
8 """
10 # ----------------------------------------------------------------------
11 # Standard modules
12 import sys, getopt, string, time
14 # ----------------------------------------------------------------------
15 # Extra goodies
16 from dumpar import mk_long, str2hex, print_offset, get_string
18 # ----------------------------------------------------------------------
19 def mk_short( str ):
20 """ convert a 2-byte string into a number
22 Assumes big-endian!
23 """
24 if len( str ) < 2:
25 raise ValueError, "str must be 2 bytes long"
27 num = ord( str[1] )
28 num = num + ord( str[0] ) * 0x100
30 return num
32 # ----------------------------------------------------------------------
33 def usage():
34 """ Display a usage message and exit.
35 """
36 print "dumpo [-v] object1 [object2 ... objectn]"
37 print
38 print "Attempt to display some useful information about the contents"
39 print "of the given Metrowerks object file(s)."
40 print
41 print "-v Be verbose (displays offsets along with the data)"
42 raise SystemExit
44 # ----------------------------------------------------------------------
45 def dump_o( file, verbose ):
46 """ dump information about a Metrowerks object file
48 Note that there is more info there, 6 more quads before the file name.
49 """
50 offset = 0
52 print "Dumping object:", file
54 # Attempt to read the data.
55 try:
56 data = open( file ).read()
57 except IOError, retval:
58 print "*** Unable to open file %s: %s" % ( file, retval[1] )
59 return
61 # Check the magic number.
62 if verbose:
63 print_offset( offset )
64 print "Magic:",
65 magic = data[offset:offset + 8]
66 print "'%s'" % ( magic )
67 if magic != "MWOBPPC ":
68 print "*** Invalid magic number!"
69 return
71 offset = offset + 8
73 # version
74 if verbose:
75 print_offset( offset )
76 print "version:", mk_long( data[offset:offset + 4] )
77 offset = offset + 4
79 # flags
80 if verbose:
81 print_offset( offset )
82 print "flags:", str2hex( data[offset:offset + 4] )
83 offset = offset + 4
85 # code size
86 if verbose:
87 print_offset( offset )
88 print "code size:", mk_long( data[offset:offset + 4] )
89 offset = offset + 4
91 # data size
92 if verbose:
93 print_offset( offset )
94 print "data size:", mk_long( data[offset:offset + 4] )
95 offset = offset + 4
97 # ----------------------------------------------------------------------
98 def main():
99 """ mainline
102 # Set up some defaults
103 be_verbose = 0
105 # First, check the command-line arguments
106 try:
107 opt, args = getopt.getopt( sys.argv[1:], "vh?" )
108 except getopt.error:
109 print "*** Error parsing command-line options!"
110 usage()
112 for o in opt:
113 if o[0] == "-h" or o[0] == "-?":
114 usage()
115 elif o[0] == "-v":
116 be_verbose = 1
117 else:
118 print "*** Unknown command-line option!"
119 usage()
121 # Now we can attempt to dump info about the arguments.
122 for obj in args:
123 dump_o( obj, be_verbose )
125 if __name__ == "__main__":
126 main()