5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 uint32_t opt_flags[1 + (NUM_F_FLAGS / 32)];
27 const char *opt_output = "progs.dat";
34 static bool options_setflag(const char *name, bool on) {
37 for (i = 0; i < opt_flag_list_count; ++i) {
38 if (!strcmp(name, opt_flag_list[i].name)) {
39 longbit lb = opt_flag_list[i].bit;
42 opt_flags[lb.idx] |= (1<<(lb.bit));
44 opt_flags[lb.idx] &= ~(1<<(lb.bit));
47 opt_flags[0] |= (1<<lb);
49 opt_flags[0] &= ~(1<<(lb));
57 static bool options_witharg(int *argc_, char ***argv_, char **out) {
66 if (argc < 2) /* no parameter was provided */
75 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
79 size_t len = strlen(optname);
81 if (strncmp(argv[0]+2, optname, len))
84 /* it's --optname, check how the parameter is supplied */
85 if (argv[0][2+len] == '=') {
86 /* using --opt=param */
87 *out = argv[0]+2+len+1;
91 if (argc < 2) /* no parameter was provided */
94 /* using --opt param */
101 static bool options_parse(int argc, char **argv) {
103 while (!argend && argc) {
105 if (argv[0][0] == '-') {
106 switch (argv[0][1]) {
107 /* -h, show usage but exit with 0 */
113 /* handle all -fflags */
115 if (!strncmp(argv[0]+2, "no-", 3)) {
116 if (!options_setflag(argv[0]+5, false)) {
117 printf("unknown flag: %s\n", argv[0]+2);
121 else if (!options_setflag(argv[0]+2, true)) {
122 printf("unknown flag: %s\n", argv[0]+2);
128 if (!options_witharg(&argc, &argv, &argarg)) {
129 printf("option -O requires a numerical argument\n");
132 opt_O = atoi(argarg);
136 if (!options_witharg(&argc, &argv, &argarg)) {
137 printf("option -o requires an argument: the output file name\n");
145 /* anything following -- is considered a non-option argument */
149 /* All long options without arguments */
150 else if (!strcmp(argv[0]+2, "help")) {
155 /* All long options with arguments */
156 if (options_long_witharg("output", &argc, &argv, &argarg))
160 printf("Unknown parameter: %s\n", argv[0]);
176 int main(int argc, char **argv) {
177 /* char *app = &argv[0][0]; */
179 if (!options_parse(argc-1, argv+1)) {
183 util_debug("COM", "starting ...\n");
187 util_debug("COM", "cleaning ...\n");