diff --git a/include/extern/getopt.h b/include/extern/getopt.h index ec0db21fb..ce6ee05e1 100644 --- a/include/extern/getopt.h +++ b/include/extern/getopt.h @@ -26,8 +26,8 @@ #ifndef RGBDS_EXTERN_GETOPT_H #define RGBDS_EXTERN_GETOPT_H -extern char *optarg; -extern int optind, opterr, optopt, optreset; +extern char *musl_optarg; +extern int musl_optind, musl_opterr, musl_optopt, musl_optreset; struct option { const char *name; diff --git a/src/asm/main.c b/src/asm/main.c index 3bd1e62f1..d89526059 100644 --- a/src/asm/main.c +++ b/src/asm/main.c @@ -177,20 +177,20 @@ int main(int argc, char *argv[]) while ((ch = musl_getopt_long_only(argc, argv, optstring, longopts, NULL)) != -1) { switch (ch) { case 'b': - if (strlen(optarg) == 2) - opt_B(&optarg[1]); + if (strlen(musl_optarg) == 2) + opt_B(&musl_optarg[1]); else errx(1, "Must specify exactly 2 characters for option 'b'"); break; char *equals; case 'D': - equals = strchr(optarg, '='); + equals = strchr(musl_optarg, '='); if (equals) { *equals = '\0'; - sym_AddString(optarg, equals + 1); + sym_AddString(musl_optarg, equals + 1); } else { - sym_AddString(optarg, "1"); + sym_AddString(musl_optarg, "1"); } break; @@ -199,8 +199,8 @@ int main(int argc, char *argv[]) break; case 'g': - if (strlen(optarg) == 4) - opt_G(&optarg[1]); + if (strlen(musl_optarg) == 4) + opt_G(&musl_optarg[1]); else errx(1, "Must specify exactly 4 characters for option 'g'"); break; @@ -210,7 +210,7 @@ int main(int argc, char *argv[]) break; case 'i': - fstk_AddIncludePath(optarg); + fstk_AddIncludePath(musl_optarg); break; case 'L': @@ -218,23 +218,23 @@ int main(int argc, char *argv[]) break; case 'M': - if (!strcmp("-", optarg)) + if (!strcmp("-", musl_optarg)) dependfile = stdout; else - dependfile = fopen(optarg, "w"); + dependfile = fopen(musl_optarg, "w"); if (dependfile == NULL) - err(1, "Could not open dependfile %s", optarg); + err(1, "Could not open dependfile %s", musl_optarg); break; case 'o': - out_SetFileName(optarg); + out_SetFileName(musl_optarg); break; unsigned long fill; case 'p': - fill = strtoul(optarg, &ep, 0); + fill = strtoul(musl_optarg, &ep, 0); - if (optarg[0] == '\0' || *ep != '\0') + if (musl_optarg[0] == '\0' || *ep != '\0') errx(1, "Invalid argument for option 'p'"); if (fill < 0 || fill > 0xFF) @@ -244,9 +244,9 @@ int main(int argc, char *argv[]) break; case 'r': - maxRecursionDepth = strtoul(optarg, &ep, 0); + maxRecursionDepth = strtoul(musl_optarg, &ep, 0); - if (optarg[0] == '\0' || *ep != '\0') + if (musl_optarg[0] == '\0' || *ep != '\0') errx(1, "Invalid argument for option 'r'"); break; @@ -258,7 +258,7 @@ int main(int argc, char *argv[]) break; case 'W': - processWarningFlag(optarg); + processWarningFlag(musl_optarg); break; case 'w': @@ -278,9 +278,9 @@ int main(int argc, char *argv[]) case 'Q': case 'T': - if (optind == argc) + if (musl_optind == argc) errx(1, "-M%c takes a target file name argument", depType); - ep = optarg; + ep = musl_optarg; if (depType == 'Q') ep = make_escape(ep); @@ -317,15 +317,15 @@ int main(int argc, char *argv[]) if (tzTargetFileName == NULL) tzTargetFileName = tzObjectname; - if (argc == optind) { + if (argc == musl_optind) { fputs("FATAL: No input files\n", stderr); print_usage(); - } else if (argc != optind + 1) { + } else if (argc != musl_optind + 1) { fputs("FATAL: More than one input file given\n", stderr); print_usage(); } - char const *mainFileName = argv[optind]; + char const *mainFileName = argv[musl_optind]; if (verbose) printf("Assembling %s\n", mainFileName); diff --git a/src/extern/getopt.c b/src/extern/getopt.c index 16214d126..858bcd393 100644 --- a/src/extern/getopt.c +++ b/src/extern/getopt.c @@ -26,21 +26,16 @@ #include #include #include -#ifndef _MSC_VER -# include -#endif #include #include #include #include "extern/getopt.h" -#ifdef _MSC_VER -char *optarg; -int optind=1, opterr=1, optopt; -#endif -int optreset=0; -static int optpos; +char *musl_optarg; +int musl_optind = 1, musl_opterr = 1, musl_optopt; +int musl_optreset = 0; +static int musl_optpos; static void musl_getopt_msg(const char *a, const char *b, const char *c, size_t l) { @@ -52,7 +47,6 @@ static void musl_getopt_msg(const char *a, const char *b, const char *c, size_t putc('\n', f); } -#ifdef _MSC_VER static int getopt(int argc, char *argv[], const char *optstring) { int i; @@ -60,40 +54,42 @@ static int getopt(int argc, char *argv[], const char *optstring) int k, l; char *optchar; - if (!optind || optreset) { - optreset = 0; - optpos = 0; - optind = 1; + if (!musl_optind || musl_optreset) { + musl_optreset = 0; + musl_optpos = 0; + musl_optind = 1; } - if (optind >= argc || !argv[optind]) + if (musl_optind >= argc || !argv[musl_optind]) return -1; - if (argv[optind][0] != '-') { + if (argv[musl_optind][0] != '-') { if (optstring[0] == '-') { - optarg = argv[optind++]; + musl_optarg = argv[musl_optind++]; return 1; } return -1; } - if (!argv[optind][1]) + if (!argv[musl_optind][1]) return -1; - if (argv[optind][1] == '-' && !argv[optind][2]) - return optind++, -1; + if (argv[musl_optind][1] == '-' && !argv[musl_optind][2]) + return musl_optind++, -1; - if (!optpos) optpos++; - if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) { + if (!musl_optpos) + musl_optpos++; + k = mbtowc(&c, argv[musl_optind] + musl_optpos, MB_LEN_MAX); + if (k < 0) { k = 1; c = 0xfffd; /* replacement char */ } - optchar = argv[optind]+optpos; - optpos += k; + optchar = argv[musl_optind] + musl_optpos; + musl_optpos += k; - if (!argv[optind][optpos]) { - optind++; - optpos = 0; + if (!argv[musl_optind][musl_optpos]) { + musl_optind++; + musl_optpos = 0; } if (optstring[0] == '-' || optstring[0] == '+') @@ -103,39 +99,43 @@ static int getopt(int argc, char *argv[], const char *optstring) d = 0; do { l = mbtowc(&d, optstring+i, MB_LEN_MAX); - if (l>0) i+=l; else i++; + if (l > 0) + i += l; + else + i++; } while (l && d != c); if (d != c || c == ':') { - optopt = c; - if (optstring[0] != ':' && opterr) + musl_optopt = c; + if (optstring[0] != ':' && musl_opterr) musl_getopt_msg(argv[0], ": unrecognized option: ", optchar, k); return '?'; } if (optstring[i] == ':') { - optarg = 0; - if (optstring[i+1] != ':' || optpos) { - optarg = argv[optind++] + optpos; - optpos = 0; + musl_optarg = 0; + if (optstring[i + 1] != ':' || musl_optpos) { + musl_optarg = argv[musl_optind++] + musl_optpos; + musl_optpos = 0; } - if (optind > argc) { - optopt = c; - if (optstring[0] == ':') return ':'; - if (opterr) musl_getopt_msg(argv[0], - ": option requires an argument: ", - optchar, k); + if (musl_optind > argc) { + musl_optopt = c; + if (optstring[0] == ':') + return ':'; + if (musl_opterr) + musl_getopt_msg(argv[0], ": option requires an argument: ", + optchar, k); return '?'; } } return c; } -#endif /* _MSC_VER */ static void permute(char **argv, int dest, int src) { char *tmp = argv[src]; int i; - for (i=src; i>dest; i--) + + for (i = src; i > dest; i--) argv[i] = argv[i-1]; argv[dest] = tmp; } @@ -145,49 +145,61 @@ static int musl_getopt_long_core(int argc, char **argv, const char *optstring, c static int musl_getopt_long(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx, int longonly) { int ret, skipped, resumed; - if (!optind || optreset) { - optreset = 0; - optpos = 0; - optind = 1; + + if (!musl_optind || musl_optreset) { + musl_optreset = 0; + musl_optpos = 0; + musl_optind = 1; } - if (optind >= argc || !argv[optind]) return -1; - skipped = optind; + + if (musl_optind >= argc || !argv[musl_optind]) + return -1; + + skipped = musl_optind; if (optstring[0] != '+' && optstring[0] != '-') { int i; - for (i=optind; ; i++) { - if (i >= argc || !argv[i]) return -1; - if (argv[i][0] == '-' && argv[i][1]) break; + for (i = musl_optind; ; i++) { + if (i >= argc || !argv[i]) + return -1; + if (argv[i][0] == '-' && argv[i][1]) + break; } - optind = i; + musl_optind = i; } - resumed = optind; + resumed = musl_optind; ret = musl_getopt_long_core(argc, argv, optstring, longopts, idx, longonly); if (resumed > skipped) { - int i, cnt = optind-resumed; - for (i=0; i 0xFF) \ report("error: Argument to option '" name "' is larger than 255: %lu\n", tmp); \ else \ @@ -1003,8 +1003,8 @@ do { \ case 'f': fixSpec = 0; - while (*optarg) { - switch (*optarg) { + while (*musl_optarg) { + switch (*musl_optarg) { #define SPEC_l FIX_LOGO #define SPEC_L TRASH_LOGO #define SPEC_h FIX_HEADER_SUM @@ -1041,15 +1041,15 @@ do { \ default: fprintf(stderr, "warning: Ignoring '%c' in fix spec\n", - *optarg); + *musl_optarg); #undef or } - optarg++; + musl_optarg++; } break; case 'i': - gameID = optarg; + gameID = musl_optarg; len = strlen(gameID); if (len > 4) { len = 4; @@ -1069,7 +1069,7 @@ do { \ break; case 'k': - newLicensee = optarg; + newLicensee = musl_optarg; len = strlen(newLicensee); if (len > 2) { len = 2; @@ -1085,14 +1085,15 @@ do { \ break; case 'm': - cartridgeType = parseMBC(optarg); + cartridgeType = parseMBC(musl_optarg); if (cartridgeType == MBC_BAD) { - report("error: Unknown MBC \"%s\"\n", optarg); + report("error: Unknown MBC \"%s\"\n", musl_optarg); } else if (cartridgeType == MBC_WRONG_FEATURES) { - report("error: Features incompatible with MBC (\"%s\")\n", optarg); + report("error: Features incompatible with MBC (\"%s\")\n", + musl_optarg); } else if (cartridgeType == MBC_BAD_RANGE) { report("error: Specified MBC ID out of range 0-255: %s\n", - optarg); + musl_optarg); } else if (cartridgeType == ROM_RAM || cartridgeType == ROM_RAM_BATTERY) { fprintf(stderr, "warning: ROM+RAM / ROM+RAM+BATTERY are under-specified and poorly supported\n"); } @@ -1115,7 +1116,7 @@ do { \ break; case 't': - title = optarg; + title = musl_optarg; len = strlen(title); uint8_t maxLen = maxTitleLen(); @@ -1170,7 +1171,7 @@ do { \ "warning: SGB compatibility enabled, but old licensee is %#x, not 0x33\n", oldLicensee); - argv += optind; + argv += musl_optind; bool failed = nbErrors; if (!*argv) { diff --git a/src/gfx/main.c b/src/gfx/main.c index ea961947c..043784efc 100644 --- a/src/gfx/main.c +++ b/src/gfx/main.c @@ -96,7 +96,7 @@ int main(int argc, char *argv[]) opts.attrmapout = true; break; case 'a': - opts.attrmapfile = optarg; + opts.attrmapfile = musl_optarg; break; case 'C': opts.colorcurve = true; @@ -105,7 +105,7 @@ int main(int argc, char *argv[]) opts.debug = true; break; case 'd': - depth = strtoul(optarg, NULL, 0); + depth = strtoul(musl_optarg, NULL, 0); break; case 'F': opts.hardfix = true; @@ -121,19 +121,19 @@ int main(int argc, char *argv[]) opts.unique = true; break; case 'o': - opts.outfile = optarg; + opts.outfile = musl_optarg; break; case 'P': opts.palout = true; break; case 'p': - opts.palfile = optarg; + opts.palfile = musl_optarg; break; case 'T': opts.tilemapout = true; break; case 't': - opts.tilemapfile = optarg; + opts.tilemapfile = musl_optarg; break; case 'u': opts.unique = true; @@ -145,15 +145,15 @@ int main(int argc, char *argv[]) opts.verbose = true; break; case 'x': - opts.trim = strtoul(optarg, NULL, 0); + opts.trim = strtoul(musl_optarg, NULL, 0); break; default: print_usage(); /* NOTREACHED */ } } - argc -= optind; - argv += optind; + argc -= musl_optind; + argv += musl_optind; if (argc == 0) { fputs("FATAL: no input files\n", stderr); diff --git a/src/link/main.c b/src/link/main.c index 77f64f63d..f69a26eba 100644 --- a/src/link/main.c +++ b/src/link/main.c @@ -217,23 +217,23 @@ int main(int argc, char *argv[]) isWRA0Mode = true; break; case 'l': - linkerScriptName = optarg; + linkerScriptName = musl_optarg; break; case 'm': - mapFileName = optarg; + mapFileName = musl_optarg; break; case 'n': - symFileName = optarg; + symFileName = musl_optarg; break; case 'O': - overlayFileName = optarg; + overlayFileName = musl_optarg; break; case 'o': - outputFileName = optarg; + outputFileName = musl_optarg; break; case 'p': - value = strtoul(optarg, &endptr, 0); - if (optarg[0] == '\0' || *endptr != '\0') { + value = strtoul(musl_optarg, &endptr, 0); + if (musl_optarg[0] == '\0' || *endptr != '\0') { error(NULL, 0, "Invalid argument for option 'p'"); value = 0xFF; } @@ -245,7 +245,7 @@ int main(int argc, char *argv[]) break; case 's': /* FIXME: nobody knows what this does, figure it out */ - (void)optarg; + (void)musl_optarg; warning(NULL, 0, "Nobody has any idea what `-s` does"); break; case 't': @@ -271,7 +271,7 @@ int main(int argc, char *argv[]) } } - int curArgIndex = optind; + int curArgIndex = musl_optind; /* If no input files were specified, the user must have screwed up */ if (curArgIndex == argc) {