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