2 * Copyright (C) 2006-2010 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.
25 struct cmdline_args cmdargs = {
28 .initvals_fn_extension = ".initvals",
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 printf("Usage: %s INPUT_FILE OUTPUT_FILE [OPTIONS]\n", argv[0]);
102 printf(" -f|--format FMT Output file format. FMT must be one of:\n");
103 printf(" raw-le32, raw-be32, b43\n");
104 printf(" -d|--debug Print verbose debugging info\n");
105 printf(" Repeat for more verbose debugging\n");
106 printf(" -s|--psize Print the size of the code after assembling\n");
107 printf(" -e|--ivalext EXT Filename extension for the initvals\n");
108 printf(" -h|--help Print this help\n");
111 int parse_args(int argc, char **argv)
119 infile_name = argv[1];
120 outfile_name = argv[2];
122 for (i = 3; i < argc; i++) {
123 if ((res = cmp_arg(argv, &i, "--help", "-h", NULL)) == ARG_MATCH) {
126 } else if ((res = cmp_arg(argv, &i, "--format", "-f", ¶m)) == ARG_MATCH) {
127 if (strcasecmp(param, "raw-le32") == 0)
128 cmdargs.outformat = FMT_RAW_LE32;
129 else if (strcasecmp(param, "raw-be32") == 0)
130 cmdargs.outformat = FMT_RAW_BE32;
131 else if (strcasecmp(param, "b43") == 0)
132 cmdargs.outformat = FMT_B43;
134 fprintf(stderr, "Invalid -f|--format\n\n");
137 } else if ((res = cmp_arg(argv, &i, "--debug", "-d", NULL)) == ARG_MATCH) {
139 } else if ((res = cmp_arg(argv, &i, "--psize", "-s", NULL)) == ARG_MATCH) {
140 cmdargs.print_sizes = 1;
141 } else if ((res = cmp_arg(argv, &i, "--ivalext", "-e", ¶m)) == ARG_MATCH) {
142 cmdargs.initvals_fn_extension = param;
143 } else if ((res = cmp_arg(argv, &i, "--__real_infile", NULL, ¶m)) == ARG_MATCH) {
144 cmdargs.real_infile_name = param;
146 fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
150 if (!cmdargs.real_infile_name)
151 cmdargs.real_infile_name = infile_name;
152 if (strcmp(cmdargs.real_infile_name, outfile_name) == 0) {
153 fprintf(stderr, "Error: INPUT and OUTPUT filename must not be the same\n");
163 int open_input_file(void)
168 if (strcmp(infile_name, "-") == 0) {
169 /* infile == stdin */
172 fd = open(infile_name, O_RDONLY);
174 fprintf(stderr, "Could not open INPUT_FILE %s\n",
178 err = dup2(fd, STDIN_FILENO);
180 fprintf(stderr, "Could not dup INPUT_FILE %s "
181 "to STDIN\n", infile_name);
191 void close_input_file(void)
193 if (strcmp(infile_name, "-") != 0)