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