2 * Copyright (C) 2006-2007 Michael Buesch <mb@bu3sch.de>
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.
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.
27 struct cmdline_args cmdargs = {
38 static int do_cmp_arg(char **argv, int *pos,
45 size_t arg_len, template_len;
48 next_arg = argv[*pos + 1];
49 arg_len = strlen(arg);
50 template_len = strlen(template);
53 /* Maybe we have a merged parameter here.
54 * A merged parameter is "-pfoobar" for example.
56 if (allow_merged && arg_len > template_len) {
57 if (memcmp(arg, template, template_len) == 0) {
58 *param = arg + template_len;
62 } else if (arg_len != template_len)
66 if (strcmp(arg, template) == 0) {
68 /* Skip the parameter on the next iteration. */
71 fprintf(stderr, "%s needs a parameter\n", arg);
81 /* Simple and lean command line argument parsing. */
82 static int cmp_arg(char **argv, int *pos,
83 const char *long_template,
84 const char *short_template,
90 err = do_cmp_arg(argv, pos, long_template, 0, param);
91 if (err == ARG_MATCH || err == ARG_ERROR)
96 err = do_cmp_arg(argv, pos, short_template, 1, param);
100 static void usage(FILE *fd, int argc, char **argv)
102 fprintf(fd, "Usage: %s INPUT_FILE OUTPUT_FILE [OPTIONS]\n", argv[0]);
103 fprintf(fd, " -a|--arch ARCH The architecture type of the input file (5 or 15)\n");
104 fprintf(fd, " -f|--format FMT Input file format. FMT must be one of:\n");
105 fprintf(fd, " raw-le32, raw-be32, b43\n");
106 fprintf(fd, " -p|--paddr Print the code addresses\n");
107 fprintf(fd, " -u|--unkdec Decode operands of unknown instructions\n");
108 fprintf(fd, " -d|--debug Print verbose debugging info\n");
109 fprintf(fd, " Repeat for more verbose debugging\n");
110 fprintf(fd, " -h|--help Print this help\n");
113 int parse_args(int argc, char **argv)
122 for (i = 1; i < argc; i++) {
123 if ((res = cmp_arg(argv, &i, "--help", "-h", NULL)) == ARG_MATCH) {
124 usage(stdout, argc, argv);
126 } else if ((res = cmp_arg(argv, &i, "--format", "-f", ¶m)) == ARG_MATCH) {
127 if (strcasecmp(param, "raw-le32") == 0)
128 cmdargs.informat = FMT_RAW_LE32;
129 else if (strcasecmp(param, "raw-be32") == 0)
130 cmdargs.informat = FMT_RAW_BE32;
131 else if (strcasecmp(param, "b43") == 0)
132 cmdargs.informat = FMT_B43;
134 fprintf(stderr, "Invalid -f|--format\n");
137 } else if ((res = cmp_arg(argv, &i, "--paddr", "-p", NULL)) == ARG_MATCH) {
138 cmdargs.print_addresses = 1;
139 } else if ((res = cmp_arg(argv, &i, "--unkdec", "-u", NULL)) == ARG_MATCH) {
140 cmdargs.unknown_decode = 1;
141 } else if ((res = cmp_arg(argv, &i, "--debug", "-d", NULL)) == ARG_MATCH) {
143 } else if ((res = cmp_arg(argv, &i, "--arch", "-a", ¶m)) == ARG_MATCH) {
147 arch = strtol(param, &tail, 0);
148 if (strlen(tail) || (arch != 5 && arch != 15)) {
149 fprintf(stderr, "Unsupported architecture \"%s\"\n",
156 infile_name = argv[i];
160 outfile_name = argv[i];
163 fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
167 if (!infile_name || !outfile_name)
172 usage(stderr, argc, argv);
176 int open_input_file(void)
178 if (strcmp(infile_name, "-") == 0) {
181 infile = fopen(infile_name, "r");
183 fprintf(stderr, "Could not open INPUT_FILE %s\n",
192 void close_input_file(void)
194 if (strcmp(infile_name, "-") != 0)
198 int open_output_file(void)
200 if (strcmp(outfile_name, "-") == 0) {
203 outfile = fopen(outfile_name, "w+");
205 fprintf(stderr, "Could not open OUTPUT_FILE %s\n",
214 void close_output_file(void)
216 if (strcmp(outfile_name, "-") != 0)