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 = {
28 .arch = 5, /* Default to v5 architecture. */
29 .no_header = 0, /* Input file does not have a header. */
30 .print_addresses = 0, /* Print the code addresses in the output. */
37 static int do_cmp_arg(char **argv, int *pos,
44 size_t arg_len, template_len;
47 next_arg = argv[*pos + 1];
48 arg_len = strlen(arg);
49 template_len = strlen(template);
52 /* Maybe we have a merged parameter here.
53 * A merged parameter is "-pfoobar" for example.
55 if (allow_merged && arg_len > template_len) {
56 if (memcmp(arg, template, template_len) == 0) {
57 *param = arg + template_len;
61 } else if (arg_len != template_len)
65 if (strcmp(arg, template) == 0) {
67 /* Skip the parameter on the next iteration. */
70 fprintf(stderr, "%s needs a parameter\n", arg);
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,
89 err = do_cmp_arg(argv, pos, long_template, 0, param);
90 if (err == ARG_MATCH || err == ARG_ERROR)
95 err = do_cmp_arg(argv, pos, short_template, 1, param);
99 static void usage(int argc, char **argv)
101 fprintf(stderr, "Usage: %s INPUT_FILE OUTPUT_FILE [OPTIONS]\n", argv[0]);
102 fprintf(stderr, " -a|--arch ARCH The architecture type of the input file\n");
103 fprintf(stderr, " -h|--help Print this help\n");
104 fprintf(stderr, " --nohdr The input file does not have a header\n");
105 fprintf(stderr, " --paddr Print the code addresses\n");
106 fprintf(stderr, " -d|--debug Print verbose debugging info\n");
107 fprintf(stderr, " Repeat for more verbose debugging\n");
110 int parse_args(int argc, char **argv)
119 for (i = 1; i < argc; i++) {
120 if ((res = cmp_arg(argv, &i, "--help", "-h", 0)) == ARG_MATCH) {
123 } else if ((res = cmp_arg(argv, &i, "--nohdr", 0, 0)) == ARG_MATCH) {
124 cmdargs.no_header = 1;
125 } else if ((res = cmp_arg(argv, &i, "--paddr", 0, 0)) == ARG_MATCH) {
126 cmdargs.print_addresses = 1;
127 } else if ((res = cmp_arg(argv, &i, "--debug", "-d", 0)) == ARG_MATCH) {
129 } else if ((res = cmp_arg(argv, &i, "--arch", "-a", ¶m)) == ARG_MATCH) {
133 arch = strtol(param, &tail, 0);
134 if (strlen(tail) || (arch != 5)) {
135 fprintf(stderr, "Unsupported architecture \"%s\"\n",
142 infile_name = argv[i];
146 outfile_name = argv[i];
149 fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
153 if (!infile_name || !outfile_name)
162 int open_input_file(void)
164 if (strcmp(infile_name, "-") == 0) {
167 infile = fopen(infile_name, "r");
169 fprintf(stderr, "Could not open INPUT_FILE %s\n",
178 void close_input_file(void)
180 if (strcmp(infile_name, "-") != 0)
184 int open_output_file(void)
186 if (strcmp(outfile_name, "-") == 0) {
189 outfile = fopen(outfile_name, "w+");
191 fprintf(stderr, "Could not open OUTPUT_FILE %s\n",
200 void close_output_file(void)
202 if (strcmp(outfile_name, "-") != 0)