disassembler: Put debug variable into cmdargs struct
[b43-tools.git] / disassembler / args.c
1 /*
2  *   Copyright (C) 2006-2007  Michael Buesch <mb@bu3sch.de>
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License version 2
6  *   as published by the Free Software Foundation.
7  *
8  *   This program is distributed in the hope that it will be useful,
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *   GNU General Public License for more details.
12  */
13
14 #include "args.h"
15 #include "main.h"
16 #include "util.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24
25 struct cmdline_args cmdargs = {
26         .debug                  = 0,
27         .arch                   = 5,
28         .informat               = FMT_B43,
29         .print_addresses        = 0,
30         .unknown_decode         = 0,
31 };
32
33 #define ARG_MATCH               0
34 #define ARG_NOMATCH             1
35 #define ARG_ERROR               -1
36
37 static int do_cmp_arg(char **argv, int *pos,
38                       const char *template,
39                       int allow_merged,
40                       char **param)
41 {
42         char *arg;
43         char *next_arg;
44         size_t arg_len, template_len;
45
46         arg = argv[*pos];
47         next_arg = argv[*pos + 1];
48         arg_len = strlen(arg);
49         template_len = strlen(template);
50
51         if (param) {
52                 /* Maybe we have a merged parameter here.
53                  * A merged parameter is "-pfoobar" for example.
54                  */
55                 if (allow_merged && arg_len > template_len) {
56                         if (memcmp(arg, template, template_len) == 0) {
57                                 *param = arg + template_len;
58                                 return ARG_MATCH;
59                         }
60                         return ARG_NOMATCH;
61                 } else if (arg_len != template_len)
62                         return ARG_NOMATCH;
63                 *param = next_arg;
64         }
65         if (strcmp(arg, template) == 0) {
66                 if (param) {
67                         /* Skip the parameter on the next iteration. */
68                         (*pos)++;
69                         if (*param == NULL) {
70                                 fprintf(stderr, "%s needs a parameter\n", arg);
71                                 return ARG_ERROR;
72                         }
73                 }
74                 return ARG_MATCH;
75         }
76
77         return ARG_NOMATCH;
78 }
79
80 /* Simple and lean command line argument parsing. */
81 static int cmp_arg(char **argv, int *pos,
82                    const char *long_template,
83                    const char *short_template,
84                    char **param)
85 {
86         int err;
87
88         if (long_template) {
89                 err = do_cmp_arg(argv, pos, long_template, 0, param);
90                 if (err == ARG_MATCH || err == ARG_ERROR)
91                         return err;
92         }
93         err = ARG_NOMATCH;
94         if (short_template)
95                 err = do_cmp_arg(argv, pos, short_template, 1, param);
96         return err;
97 }
98
99 static void usage(FILE *fd, int argc, char **argv)
100 {
101         fprintf(fd, "Usage: %s INPUT_FILE OUTPUT_FILE [OPTIONS]\n", argv[0]);
102         fprintf(fd, "  -a|--arch ARCH      The architecture type of the input file (5 or 15)\n");
103         fprintf(fd, "  -f|--format FMT     Input file format. FMT must be one of:\n");
104         fprintf(fd, "                      raw-le32, raw-be32, b43\n");
105         fprintf(fd, "  -p|--paddr          Print the code addresses\n");
106         fprintf(fd, "  -u|--unkdec         Decode operands of unknown instructions\n");
107         fprintf(fd, "  -d|--debug          Print verbose debugging info\n");
108         fprintf(fd, "                      Repeat for more verbose debugging\n");
109         fprintf(fd, "  -h|--help           Print this help\n");
110 }
111
112 int parse_args(int argc, char **argv)
113 {
114         int i;
115         int res;
116         char *param;
117
118         infile_name = NULL;
119         outfile_name = NULL;
120
121         for (i = 1; i < argc; i++) {
122                 if ((res = cmp_arg(argv, &i, "--help", "-h", NULL)) == ARG_MATCH) {
123                         usage(stdout, argc, argv);
124                         return 1;
125                 } else if ((res = cmp_arg(argv, &i, "--format", "-f", &param)) == ARG_MATCH) {
126                         if (strcasecmp(param, "raw-le32") == 0)
127                                 cmdargs.informat = FMT_RAW_LE32;
128                         else if (strcasecmp(param, "raw-be32") == 0)
129                                 cmdargs.informat = FMT_RAW_BE32;
130                         else if (strcasecmp(param, "b43") == 0)
131                                 cmdargs.informat = FMT_B43;
132                         else {
133                                 fprintf(stderr, "Invalid -f|--format\n");
134                                 return -1;
135                         }
136                 } else if ((res = cmp_arg(argv, &i, "--paddr", "-p", NULL)) == ARG_MATCH) {
137                         cmdargs.print_addresses = 1;
138                 } else if ((res = cmp_arg(argv, &i, "--unkdec", "-u", NULL)) == ARG_MATCH) {
139                         cmdargs.unknown_decode = 1;
140                 } else if ((res = cmp_arg(argv, &i, "--debug", "-d", NULL)) == ARG_MATCH) {
141                         cmdargs.debug++;
142                 } else if ((res = cmp_arg(argv, &i, "--arch", "-a", &param)) == ARG_MATCH) {
143                         unsigned long arch;
144                         char *tail;
145
146                         arch = strtol(param, &tail, 0);
147                         if (strlen(tail) || (arch != 5 && arch != 15)) {
148                                 fprintf(stderr, "Unsupported architecture \"%s\"\n",
149                                         param);
150                                 return -1;
151                         }
152                         cmdargs.arch = arch;
153                 } else {
154                         if (!infile_name) {
155                                 infile_name = argv[i];
156                                 continue;
157                         }
158                         if (!outfile_name) {
159                                 outfile_name = argv[i];
160                                 continue;
161                         }
162                         fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
163                         goto out_usage;
164                 }
165         }
166         if (!infile_name || !outfile_name)
167                 goto out_usage;
168
169         return 0;
170 out_usage:
171         usage(stderr, argc, argv);
172         return -1;
173 }
174
175 int open_input_file(void)
176 {
177         if (strcmp(infile_name, "-") == 0) {
178                 infile = stdin;
179         } else {
180                 infile = fopen(infile_name, "r");
181                 if (!infile) {
182                         fprintf(stderr, "Could not open INPUT_FILE %s\n",
183                                 infile_name);
184                         return -1;
185                 }
186         }
187
188         return 0;
189 }
190
191 void close_input_file(void)
192 {
193         if (strcmp(infile_name, "-") != 0)
194                 fclose(infile);
195 }
196
197 int open_output_file(void)
198 {
199         if (strcmp(outfile_name, "-") == 0) {
200                 outfile = stdout;
201         } else {
202                 outfile = fopen(outfile_name, "w+");
203                 if (!outfile) {
204                         fprintf(stderr, "Could not open OUTPUT_FILE %s\n",
205                                 outfile_name);
206                         return -1;
207                 }
208         }
209
210         return 0;
211 }
212
213 void close_output_file(void)
214 {
215         if (strcmp(outfile_name, "-") != 0)
216                 fclose(outfile);
217 }