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 const char *initvals_fn_extension = ".initvals";
28 static const char *real_infile_name;
35 static int do_cmp_arg(char **argv, int *pos,
42 size_t arg_len, template_len;
45 next_arg = argv[*pos + 1];
46 arg_len = strlen(arg);
47 template_len = strlen(template);
50 /* Maybe we have a merged parameter here.
51 * A merged parameter is "-pfoobar" for example.
53 if (allow_merged && arg_len > template_len) {
54 if (memcmp(arg, template, template_len) == 0) {
55 *param = arg + template_len;
59 } else if (arg_len != template_len)
63 if (strcmp(arg, template) == 0) {
65 /* Skip the parameter on the next iteration. */
68 fprintf(stderr, "%s needs a parameter\n", arg);
78 /* Simple and lean command line argument parsing. */
79 static int cmp_arg(char **argv, int *pos,
80 const char *long_template,
81 const char *short_template,
87 err = do_cmp_arg(argv, pos, long_template, 0, param);
88 if (err == ARG_MATCH || err == ARG_ERROR)
93 err = do_cmp_arg(argv, pos, short_template, 1, param);
97 static void usage(int argc, char **argv)
99 printf("Usage: %s INPUT_FILE OUTPUT_FILE [OPTIONS]\n", argv[0]);
100 printf(" -h|--help Print this help\n");
101 printf(" -d|--debug Print verbose debugging info\n");
102 printf(" Repeat for more verbose debugging\n");
103 printf(" -s|--psize Print the size of the code after assembling\n");
104 printf(" -e|--ivalext EXT Filename extension for the initvals\n");
107 int parse_args(int argc, char **argv)
114 infile_name = argv[1];
115 outfile_name = argv[2];
117 for (i = 3; i < argc; i++) {
118 if ((res = cmp_arg(argv, &i, "--help", "-h", NULL)) == ARG_MATCH) {
121 } else if ((res = cmp_arg(argv, &i, "--debug", "-d", NULL)) == ARG_MATCH) {
123 } else if ((res = cmp_arg(argv, &i, "--psize", "-s", NULL)) == ARG_MATCH) {
125 } else if ((res = cmp_arg(argv, &i, "--ivalext", "-e", &initvals_fn_extension)) == ARG_MATCH) {
126 /* initvals_fn_extension is set to the extension. */
127 } else if ((res = cmp_arg(argv, &i, "--__real_infile", NULL, &real_infile_name)) == ARG_MATCH) {
128 /* real_infile_name is set. */
130 fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
134 if (!real_infile_name)
135 real_infile_name = infile_name;
136 if (strcmp(real_infile_name, outfile_name) == 0) {
137 fprintf(stderr, "Error: INPUT and OUTPUT filename must not be the same\n");
147 int open_input_file(void)
152 if (strcmp(infile_name, "-") == 0) {
153 /* infile == stdin */
156 fd = open(infile_name, O_RDONLY);
158 fprintf(stderr, "Could not open INPUT_FILE %s\n",
162 err = dup2(fd, STDIN_FILENO);
164 fprintf(stderr, "Could not dup INPUT_FILE %s "
165 "to STDIN\n", infile_name);
175 void close_input_file(void)
177 if (strcmp(infile_name, "-") != 0)