]> git.sur5r.net Git - cc65/blob - src/od65/main.c
9387e26406f43b3684abb731e2910da647b5d8fd
[cc65] / src / od65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*             Main program of the od65 object file dump utility             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40
41 /* common */
42 #include "cmdline.h"
43 #include "objdefs.h"
44 #include "version.h"
45
46 /* od65 */
47 #include "dump.h"
48 #include "error.h"
49 #include "fileio.h"
50 #include "global.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Data                                    */
56 /*****************************************************************************/
57
58
59
60 static unsigned FilesProcessed = 0;
61
62
63
64 /*****************************************************************************/
65 /*                                   Code                                    */
66 /*****************************************************************************/
67
68
69
70 static void Usage (void)
71 /* Print usage information and exit */
72 {
73     fprintf (stderr,
74              "Usage: %s [options] file [options] [file]\n"
75              "Short options:\n"
76              "  -h\t\t\tHelp (this text)\n"
77              "  -V\t\t\tPrint the version number and exit\n"
78              "\n"
79              "Long options:\n"
80              "  --dump-all\t\tDump all object file information\n" 
81              "  --dump-dbgsyms\tDump debug symbols\n"
82              "  --dump-exports\tDump exported symbols\n"
83              "  --dump-files\t\tDump the source files\n"
84              "  --dump-header\t\tDump the object file header\n"
85              "  --dump-imports\tDump imported symbols\n"
86              "  --dump-options\tDump object file options\n"
87              "  --dump-segments\tDump the segments in the file\n"
88              "  --help\t\tHelp (this text)\n"
89              "  --version\t\tPrint the version number and exit\n",
90              ProgName);
91 }
92
93
94
95 static void OptDumpAll (const char* Opt, const char* Arg)
96 /* Dump all object file information */
97 {
98     What |= D_ALL;
99 }
100
101
102
103 static void OptDumpDbgSyms (const char* Opt, const char* Arg)
104 /* Dump debug symbols contained in the object file */
105 {
106     What |= D_DBGSYMS;
107 }                    
108
109
110
111 static void OptDumpExports (const char* Opt, const char* Arg)
112 /* Dump the exported symbols */
113 {
114     What |= D_EXPORTS;
115 }
116
117
118
119 static void OptDumpFiles (const char* Opt, const char* Arg)
120 /* Dump the source files */
121 {
122     What |= D_FILES;
123 }
124
125
126
127 static void OptDumpHeader (const char* Opt, const char* Arg)
128 /* Dump the object file header */
129 {
130     What |= D_HEADER;
131 }
132
133
134
135 static void OptDumpImports (const char* Opt, const char* Arg)
136 /* Dump the imported symbols */
137 {
138     What |= D_IMPORTS;
139 }
140
141
142
143 static void OptDumpOptions (const char* Opt, const char* Arg)
144 /* Dump the object file options */
145 {
146     What |= D_OPTIONS;
147 }
148
149
150
151 static void OptDumpSegments (const char* Opt, const char* Arg)
152 /* Dump the segments in the object file */
153 {
154     What |= D_SEGMENTS;
155 }
156
157
158
159 static void OptHelp (const char* Opt, const char* Arg)
160 /* Print usage information and exit */
161 {
162     Usage ();
163     exit (EXIT_SUCCESS);
164 }
165
166
167
168 static void OptVersion (const char* Opt, const char* Arg)
169 /* Print the assembler version */
170 {
171     fprintf (stderr,
172              "%s V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
173              ProgName, VER_MAJOR, VER_MINOR, VER_PATCH);
174 }
175
176
177
178 static void DumpFile (const char* Name)
179 /* Dump information from the named file */
180 {
181     unsigned long Magic;
182
183     /* Try to open the file */
184     FILE* F = fopen (Name, "rb");
185     if (F == 0) {
186         Warning ("Cannot open `%s': %s", Name, strerror (errno));
187     }
188
189     /* Read the magic word */
190     Magic = Read32 (F);
191
192     /* Do we know this type of file? */
193     if (Magic != OBJ_MAGIC) {
194
195         /* Unknown format */
196         printf ("%s: (no xo65 object file)\n", Name);
197
198     } else if (What == 0) {
199
200         /* Special handling if no info was requested */
201         printf ("%s: (no information requested)\n", Name);
202
203     } else {
204
205         /* Print the filename */
206         printf ("%s:\n", Name);
207
208         /* Check what to dump */
209         if (What & D_HEADER) {
210             DumpObjHeader (F, 0);
211         }
212         if (What & D_OPTIONS) {
213             DumpObjOptions (F, 0);
214         }
215         if (What & D_FILES) {
216             DumpObjFiles (F, 0);
217         }
218         if (What & D_SEGMENTS) {
219             DumpObjSegments (F, 0);
220         }
221         if (What & D_IMPORTS) {
222             DumpObjImports (F, 0);
223         }
224         if (What & D_EXPORTS) {
225             DumpObjExports (F, 0);
226         }
227         if (What & D_DBGSYMS) {
228             DumpObjDbgSyms (F, 0);
229         }
230     }
231
232     /* Close the file */
233     fclose (F);
234 }
235
236
237
238 int main (int argc, char* argv [])
239 /* Assembler main program */
240 {
241     /* Program long options */
242     static const LongOpt OptTab[] = {
243         { "--dump-all",         0,      OptDumpAll              },
244         { "--dump-dbgsyms",     0,      OptDumpDbgSyms          },
245         { "--dump-exports",     0,      OptDumpExports          },
246         { "--dump-files",       0,      OptDumpFiles            },
247         { "--dump-header",      0,      OptDumpHeader           },
248         { "--dump-imports",     0,      OptDumpImports          },
249         { "--dump-options",     0,      OptDumpOptions          },
250         { "--dump-segments",    0,      OptDumpSegments         },
251         { "--help",             0,      OptHelp                 },
252         { "--version",          0,      OptVersion              },
253     };
254
255     int I;
256
257     /* Initialize the cmdline module */
258     InitCmdLine (argc, argv, "od65");
259
260     /* Check the parameters */
261     I = 1;
262     while (I < argc) {
263
264         /* Get the argument */
265         const char* Arg = argv [I];
266
267         /* Check for an option */
268         if (Arg [0] == '-') {
269             switch (Arg [1]) {
270
271                 case '-':
272                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
273                     break;
274
275                 case 'h':
276                     OptHelp (Arg, 0);
277                     break;
278
279                 case 'H':
280                     OptDumpHeader (Arg, 0);
281                     break;
282
283                 case 'V':
284                     OptVersion (Arg, 0);
285                     break;
286
287                 default:
288                     UnknownOption (Arg);
289                     break;
290
291             }
292         } else {
293             /* Filename. Dump it. */
294             DumpFile (Arg);
295             ++FilesProcessed;
296         }
297
298         /* Next argument */
299         ++I;
300     }
301
302     /* Print a message if we did not process any files */
303     if (FilesProcessed == 0) {
304         fprintf (stderr, "%s: No input files\n", ProgName);
305     }
306
307     /* Success */
308     return EXIT_SUCCESS;
309 }
310
311
312