]> git.sur5r.net Git - cc65/blob - src/od65/main.c
Fixed a few warnings
[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-2001 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@cc65.org                                                 */
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-lineinfo\tDump line information\n"
87              "  --dump-options\tDump object file options\n"
88              "  --dump-segments\tDump the segments in the file\n"
89              "  --help\t\tHelp (this text)\n"
90              "  --version\t\tPrint the version number and exit\n",
91              ProgName);
92 }
93
94
95
96 static void OptDumpAll (const char* Opt, const char* Arg)
97 /* Dump all object file information */
98 {
99     What |= D_ALL;
100 }
101
102
103
104 static void OptDumpDbgSyms (const char* Opt, const char* Arg)
105 /* Dump debug symbols contained in the object file */
106 {
107     What |= D_DBGSYMS;
108 }
109
110
111
112 static void OptDumpExports (const char* Opt, const char* Arg)
113 /* Dump the exported symbols */
114 {
115     What |= D_EXPORTS;
116 }
117
118
119
120 static void OptDumpFiles (const char* Opt, const char* Arg)
121 /* Dump the source files */
122 {
123     What |= D_FILES;
124 }
125
126
127
128 static void OptDumpHeader (const char* Opt, const char* Arg)
129 /* Dump the object file header */
130 {
131     What |= D_HEADER;
132 }
133
134
135
136 static void OptDumpImports (const char* Opt, const char* Arg)
137 /* Dump the imported symbols */
138 {
139     What |= D_IMPORTS;
140 }
141
142
143
144 static void OptDumpLineInfo (const char* Opt, const char* Arg)
145 /* Dump the line infos */
146 {
147     What |= D_LINEINFO;
148 }
149
150
151
152 static void OptDumpOptions (const char* Opt, const char* Arg)
153 /* Dump the object file options */
154 {
155     What |= D_OPTIONS;
156 }
157
158
159
160 static void OptDumpSegments (const char* Opt, const char* Arg)
161 /* Dump the segments in the object file */
162 {
163     What |= D_SEGMENTS;
164 }
165
166
167
168 static void OptHelp (const char* Opt, const char* Arg)
169 /* Print usage information and exit */
170 {
171     Usage ();
172     exit (EXIT_SUCCESS);
173 }
174
175
176
177 static void OptVersion (const char* Opt, const char* Arg)
178 /* Print the assembler version */
179 {
180     fprintf (stderr,
181              "%s V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
182              ProgName, VER_MAJOR, VER_MINOR, VER_PATCH);
183 }
184
185
186
187 static void DumpFile (const char* Name)
188 /* Dump information from the named file */
189 {
190     unsigned long Magic;
191
192     /* Try to open the file */
193     FILE* F = fopen (Name, "rb");
194     if (F == 0) {
195         Warning ("Cannot open `%s': %s", Name, strerror (errno));
196     }
197
198     /* Read the magic word */
199     Magic = Read32 (F);
200
201     /* Do we know this type of file? */
202     if (Magic != OBJ_MAGIC) {
203
204         /* Unknown format */
205         printf ("%s: (no xo65 object file)\n", Name);
206
207     } else if (What == 0) {
208
209         /* Special handling if no info was requested */
210         printf ("%s: (no information requested)\n", Name);
211
212     } else {
213
214         /* Print the filename */
215         printf ("%s:\n", Name);
216
217         /* Check what to dump */
218         if (What & D_HEADER) {
219             DumpObjHeader (F, 0);
220         }
221         if (What & D_OPTIONS) {
222             DumpObjOptions (F, 0);
223         }
224         if (What & D_FILES) {
225             DumpObjFiles (F, 0);
226         }
227         if (What & D_SEGMENTS) {
228             DumpObjSegments (F, 0);
229         }
230         if (What & D_IMPORTS) {
231             DumpObjImports (F, 0);
232         }
233         if (What & D_EXPORTS) {
234             DumpObjExports (F, 0);
235         }
236         if (What & D_DBGSYMS) {
237             DumpObjDbgSyms (F, 0);
238         }
239         if (What & D_LINEINFO) {
240             DumpObjLineInfo (F, 0);
241         }
242     }
243
244     /* Close the file */
245     fclose (F);
246 }
247
248
249
250 int main (int argc, char* argv [])
251 /* Assembler main program */
252 {
253     /* Program long options */
254     static const LongOpt OptTab[] = {
255         { "--dump-all",         0,      OptDumpAll              },
256         { "--dump-dbgsyms",     0,      OptDumpDbgSyms          },
257         { "--dump-exports",     0,      OptDumpExports          },
258         { "--dump-files",       0,      OptDumpFiles            },
259         { "--dump-header",      0,      OptDumpHeader           },
260         { "--dump-imports",     0,      OptDumpImports          },
261         { "--dump-lineinfo",    0,      OptDumpLineInfo         },
262         { "--dump-options",     0,      OptDumpOptions          },
263         { "--dump-segments",    0,      OptDumpSegments         },
264         { "--help",             0,      OptHelp                 },
265         { "--version",          0,      OptVersion              },
266     };
267
268     unsigned I;
269
270     /* Initialize the cmdline module */
271     InitCmdLine (&argc, &argv, "od65");
272
273     /* Check the parameters */
274     I = 1;
275     while (I < ArgCount) {
276
277         /* Get the argument */
278         const char* Arg = ArgVec[I];
279
280         /* Check for an option */
281         if (Arg [0] == '-') {
282             switch (Arg [1]) {
283
284                 case '-':
285                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
286                     break;
287
288                 case 'h':
289                     OptHelp (Arg, 0);
290                     break;
291
292                 case 'H':
293                     OptDumpHeader (Arg, 0);
294                     break;
295
296                 case 'V':
297                     OptVersion (Arg, 0);
298                     break;
299
300                 default:
301                     UnknownOption (Arg);
302                     break;
303
304             }
305         } else {
306             /* Filename. Dump it. */
307             DumpFile (Arg);
308             ++FilesProcessed;
309         }
310
311         /* Next argument */
312         ++I;
313     }
314
315     /* Print a message if we did not process any files */
316     if (FilesProcessed == 0) {
317         fprintf (stderr, "%s: No input files\n", ProgName);
318     }
319
320     /* Success */
321     return EXIT_SUCCESS;
322 }
323
324
325