Reimplement -M variants using long options

This commit is contained in:
ISSOtm
2019-11-06 18:43:05 +01:00
parent 4a98b41d57
commit a29dd738f2

View File

@@ -263,6 +263,9 @@ static char *make_escape(const char *str)
/* Short options */
static char const *optstring = "b:D:Eg:hi:LM:o:p:r:VvW:w";
/* Variables for the long-only options */
static int depType; /* Variants of `-M` */
/*
* Equivalent long options
* Please keep in the same order as short opts
@@ -282,6 +285,10 @@ static struct option const longopts[] = {
{ "include", required_argument, NULL, 'i' },
{ "preserve-ld", no_argument, NULL, 'L' },
{ "dependfile", required_argument, NULL, 'M' },
{ "MG", no_argument, &depType, 'G' },
{ "MP", no_argument, &depType, 'P' },
{ "MT", required_argument, &depType, 'T' },
{ "MQ", required_argument, &depType, 'Q' },
{ "output", required_argument, NULL, 'o' },
{ "pad-value", required_argument, NULL, 'p' },
{ "recursion-depth", required_argument, NULL, 'r' },
@@ -391,8 +398,6 @@ int main(int argc, char *argv[])
newopt.optimizeloads = false;
break;
case 'M':
ep = strchr("GPQT", optarg[0]);
if (!ep || !*ep || optarg[1]) {
if (!strcmp("-", optarg))
dependfile = stdout;
else
@@ -400,41 +405,6 @@ int main(int argc, char *argv[])
if (dependfile == NULL)
err(1, "Could not open dependfile %s",
optarg);
} else {
switch (optarg[0]) {
case 'G':
oGeneratedMissingIncludes = true;
break;
case 'P':
oGeneratePhonyDeps = true;
break;
case 'Q':
case 'T':
if (optind == argc)
errx(1, "-M%c takes a target file name argument",
optarg[0]);
ep = argv[optind];
optind++;
if (optarg[0] == 'Q')
ep = make_escape(ep);
nTargetFileNameLen += strlen(ep) + 1;
tzTargetFileName =
realloc(tzTargetFileName,
nTargetFileNameLen + 1);
if (tzTargetFileName == NULL)
err(1, "Cannot append new file to target file list");
strcat(tzTargetFileName, ep);
if (optarg[0] == 'Q')
free(ep);
char *ptr = tzTargetFileName +
strlen(tzTargetFileName);
*ptr++ = ' ';
*ptr = '\0';
break;
}
}
break;
case 'o':
out_SetFileName(optarg);
@@ -467,6 +437,45 @@ int main(int argc, char *argv[])
case 'w':
newopt.warnings = false;
break;
/* Long-only options */
case 0:
if (depType) {
switch (depType) {
case 'G':
oGeneratedMissingIncludes = true;
break;
case 'P':
oGeneratePhonyDeps = true;
break;
case 'Q':
case 'T':
if (optind == argc)
errx(1, "-M%c takes a target file name argument",
depType);
ep = optarg;
if (depType == 'Q')
ep = make_escape(ep);
nTargetFileNameLen += strlen(ep) + 1;
tzTargetFileName =
realloc(tzTargetFileName,
nTargetFileNameLen + 1);
if (tzTargetFileName == NULL)
err(1, "Cannot append new file to target file list");
strcat(tzTargetFileName, ep);
if (depType == 'Q')
free(ep);
char *ptr = tzTargetFileName +
strlen(tzTargetFileName);
*ptr++ = ' ';
*ptr = '\0';
break;
}
}
break;
/* Unrecognized options */
default:
print_usage();
/* NOTREACHED */