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.
32 static int do_cmp_arg(char **argv, int *pos,
39 size_t arg_len, template_len;
42 next_arg = argv[*pos + 1];
43 arg_len = strlen(arg);
44 template_len = strlen(template);
47 /* Maybe we have a merged parameter here.
48 * A merged parameter is "-pfoobar" for example.
50 if (allow_merged && arg_len > template_len) {
51 if (memcmp(arg, template, template_len) == 0) {
52 *param = arg + template_len;
56 } else if (arg_len != template_len)
60 if (strcmp(arg, template) == 0) {
62 /* Skip the parameter on the next iteration. */
65 fprintf(stderr, "%s needs a parameter\n", arg);
75 /* Simple and lean command line argument parsing. */
76 static int cmp_arg(char **argv, int *pos,
77 const char *long_template,
78 const char *short_template,
84 err = do_cmp_arg(argv, pos, long_template, 0, param);
85 if (err == ARG_MATCH || err == ARG_ERROR)
90 err = do_cmp_arg(argv, pos, short_template, 1, param);
94 static void usage(int argc, char **argv)
96 fprintf(stderr, "Usage: %s INPUT_FILE OUTPUT_FILE [OPTIONS]\n", argv[0]);
97 fprintf(stderr, " -h|--help Print this help\n");
98 fprintf(stderr, " -d|--debug Print verbose debugging info\n");
99 fprintf(stderr, " Repeat for more verbose debugging\n");
100 fprintf(stderr, " -s|--psize Print the size of the code after assembling\n");
103 int parse_args(int argc, char **argv)
110 infile_name = argv[1];
111 outfile_name = argv[2];
113 for (i = 3; i < argc; i++) {
114 if ((res = cmp_arg(argv, &i, "--help", "-h", 0)) == ARG_MATCH) {
117 } else if ((res = cmp_arg(argv, &i, "--debug", "-d", 0)) == ARG_MATCH) {
119 } else if ((res = cmp_arg(argv, &i, "--psize", "-s", 0)) == ARG_MATCH) {
122 fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
132 int open_input_file(void)
137 if (strcmp(infile_name, "-") == 0) {
138 /* infile == stdin */
141 fd = open(infile_name, O_RDONLY);
143 fprintf(stderr, "Could not open INPUT_FILE %s\n",
147 err = dup2(fd, STDIN_FILENO);
149 fprintf(stderr, "Could not dup INPUT_FILE %s "
150 "to STDIN\n", infile_name);
160 void close_input_file(void)
162 if (strcmp(infile_name, "-") != 0)