b43-asm: Add an option to print the code sizes after assembling.
[b43-tools.git] / assembler / args.c
1 /*
2  *   Copyright (C) 2006-2007  Michael Buesch <mb@bu3sch.de>
3  *
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.
7  *
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.
12  */
13
14 #include "args.h"
15 #include "main.h"
16 #include "util.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24
25 int _debug;
26 bool arg_print_sizes;
27
28 #define ARG_MATCH               0
29 #define ARG_NOMATCH             1
30 #define ARG_ERROR               -1
31
32 static int do_cmp_arg(char **argv, int *pos,
33                       const char *template,
34                       int allow_merged,
35                       char **param)
36 {
37         char *arg;
38         char *next_arg;
39         size_t arg_len, template_len;
40
41         arg = argv[*pos];
42         next_arg = argv[*pos + 1];
43         arg_len = strlen(arg);
44         template_len = strlen(template);
45
46         if (param) {
47                 /* Maybe we have a merged parameter here.
48                  * A merged parameter is "-pfoobar" for example.
49                  */
50                 if (allow_merged && arg_len > template_len) {
51                         if (memcmp(arg, template, template_len) == 0) {
52                                 *param = arg + template_len;
53                                 return ARG_MATCH;
54                         }
55                         return ARG_NOMATCH;
56                 } else if (arg_len != template_len)
57                         return ARG_NOMATCH;
58                 *param = next_arg;
59         }
60         if (strcmp(arg, template) == 0) {
61                 if (param) {
62                         /* Skip the parameter on the next iteration. */
63                         (*pos)++;
64                         if (*param == 0) {
65                                 fprintf(stderr, "%s needs a parameter\n", arg);
66                                 return ARG_ERROR;
67                         }
68                 }
69                 return ARG_MATCH;
70         }
71
72         return ARG_NOMATCH;
73 }
74
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,
79                    char **param)
80 {
81         int err;
82
83         if (long_template) {
84                 err = do_cmp_arg(argv, pos, long_template, 0, param);
85                 if (err == ARG_MATCH || err == ARG_ERROR)
86                         return err;
87         }
88         err = ARG_NOMATCH;
89         if (short_template)
90                 err = do_cmp_arg(argv, pos, short_template, 1, param);
91         return err;
92 }
93
94 static void usage(int argc, char **argv)
95 {
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");
101 }
102
103 int parse_args(int argc, char **argv)
104 {
105         int i;
106         int res;
107
108         if (argc < 3)
109                 goto out_usage;
110         infile_name = argv[1];
111         outfile_name = argv[2];
112
113         for (i = 3; i < argc; i++) {
114                 if ((res = cmp_arg(argv, &i, "--help", "-h", 0)) == ARG_MATCH) {
115                         usage(argc, argv);
116                         return 1;
117                 } else if ((res = cmp_arg(argv, &i, "--debug", "-d", 0)) == ARG_MATCH) {
118                         _debug++;
119                 } else if ((res = cmp_arg(argv, &i, "--psize", "-s", 0)) == ARG_MATCH) {
120                         arg_print_sizes = 1;
121                 } else {
122                         fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
123                         goto out_usage;
124                 }
125         }
126         return 0;
127 out_usage:
128         usage(argc, argv);
129         return -1;
130 }
131
132 int open_input_file(void)
133 {
134         int fd;
135         int err;
136
137         if (strcmp(infile_name, "-") == 0) {
138                 /* infile == stdin */
139                 fd = STDIN_FILENO;
140         } else {
141                 fd = open(infile_name, O_RDONLY);
142                 if (fd < 0) {
143                         fprintf(stderr, "Could not open INPUT_FILE %s\n",
144                                 infile_name);
145                         return -1;
146                 }
147                 err = dup2(fd, STDIN_FILENO);
148                 if (err) {
149                         fprintf(stderr, "Could not dup INPUT_FILE %s "
150                                 "to STDIN\n", infile_name);
151                         close(fd);
152                         return -1;
153                 }
154         }
155         infile.fd = fd;
156
157         return 0;
158 }
159
160 void close_input_file(void)
161 {
162         if (strcmp(infile_name, "-") != 0)
163                 close(infile.fd);
164 }