]> git.sur5r.net Git - cc65/blob - src/od65/main.c
Add dumping of options
[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\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-header\t\tDump the object file header\n"   
81              "  --dump-options\t\tDump object file options\n"
82              "  --help\t\tHelp (this text)\n"
83              "  --version\t\tPrint the version number and exit\n",
84              ProgName);
85 }
86
87
88
89 static void OptDumpHeader (const char* Opt, const char* Arg)
90 /* Dump the object file header */
91 {
92     What |= D_HEADER;
93 }
94
95
96
97 static void OptDumpOptions (const char* Opt, const char* Arg)
98 /* Dump the object file options */
99 {
100     What |= D_OPTIONS;
101 }
102
103
104
105 static void OptHelp (const char* Opt, const char* Arg)
106 /* Print usage information and exit */
107 {
108     Usage ();
109     exit (EXIT_SUCCESS);
110 }
111
112
113
114 static void OptVersion (const char* Opt, const char* Arg)
115 /* Print the assembler version */
116 {
117     fprintf (stderr,
118              "%s V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
119              ProgName, VER_MAJOR, VER_MINOR, VER_PATCH);
120 }
121
122
123
124 static void DumpFile (const char* Name)
125 /* Dump information from the named file */
126 {
127     unsigned long Magic;
128
129     /* Try to open the file */
130     FILE* F = fopen (Name, "rb");
131     if (F == 0) {
132         Warning ("Cannot open `%s': %s", Name, strerror (errno));
133     }
134
135     /* Read the magic word */
136     Magic = Read32 (F);
137
138     /* Do we know this type of file? */
139     if (Magic != OBJ_MAGIC) {
140
141         /* Unknown format */
142         printf ("%s: (no x65 object file)\n", Name);
143
144     } else if (What == 0) {
145
146         /* Special handling if no info was requested */
147         printf ("%s: (no information requested)\n", Name);
148
149     } else {
150
151         /* Print the filename */
152         printf ("%s:\n", Name);
153
154         /* Check what to dump */
155         if (What & D_HEADER) {
156             DumpObjHeader (F, 0);
157         }
158         if (What & D_OPTIONS) {
159             DumpObjOptions (F, 0);
160         }
161     }
162
163     /* Close the file */
164     fclose (F);
165 }
166
167
168
169 int main (int argc, char* argv [])
170 /* Assembler main program */
171 {
172     /* Program long options */
173     static const LongOpt OptTab[] = {
174         { "--dump-header",      0,      OptDumpHeader           },
175         { "--dump-options",     0,      OptDumpOptions          },
176         { "--help",             0,      OptHelp                 },
177         { "--version",          0,      OptVersion              },
178     };
179
180     int I;
181
182     /* Initialize the cmdline module */
183     InitCmdLine (argc, argv, "od65");
184
185     /* Check the parameters */
186     I = 1;
187     while (I < argc) {
188
189         /* Get the argument */
190         const char* Arg = argv [I];
191
192         /* Check for an option */
193         if (Arg [0] == '-') {
194             switch (Arg [1]) {
195
196                 case '-':
197                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
198                     break;
199
200                 case 'h':
201                     OptHelp (Arg, 0);
202                     break;
203
204                 case 'H':
205                     OptDumpHeader (Arg, 0);
206                     break;
207
208                 case 'V':
209                     OptVersion (Arg, 0);
210                     break;
211
212                 default:
213                     UnknownOption (Arg);
214                     break;
215
216             }
217         } else {
218             /* Filename. Dump it. */
219             DumpFile (Arg);
220             ++FilesProcessed;
221         }
222
223         /* Next argument */
224         ++I;
225     }
226
227     /* Print a message if we did not process any files */
228     if (FilesProcessed == 0) {
229         fprintf (stderr, "%s: No input files\n", ProgName);
230     }
231
232     /* Success */
233     return EXIT_SUCCESS;
234 }
235
236
237