setarch: list available architectures

Signed-off-by: Oliver Tappe <[email protected]>
This commit is contained in:
David Höppner
2014-03-06 09:27:43 +01:00
committed by Oliver Tappe
parent 0009559ca3
commit fe309ca2a3
+37 -12
View File
@@ -23,7 +23,8 @@ const char* kCommandName = __progname;
static const char* kUsage = static const char* kUsage =
"Usage: %s [ <options> ] <architecture> [ <command> ... ]\n" "Usage: %s [-hl]\n"
" %s [-p] <architecture> [ <command> ... ]\n"
"Executes the given command or, by default, a shell with a PATH\n" "Executes the given command or, by default, a shell with a PATH\n"
"environment variable modified such that commands for the given\n" "environment variable modified such that commands for the given\n"
"architecture will be preferred, respectively used exclusively in case of\n" "architecture will be preferred, respectively used exclusively in case of\n"
@@ -32,6 +33,8 @@ static const char* kUsage =
"Options:\n" "Options:\n"
" -h, --help\n" " -h, --help\n"
" Print this usage info.\n" " Print this usage info.\n"
" -l, --list-architectures\n"
" List all architectures.\n"
" -p, --print-path\n" " -p, --print-path\n"
" Only print the modified PATH variable value; don't execute any\n" " Only print the modified PATH variable value; don't execute any\n"
" command.\n" " command.\n"
@@ -41,8 +44,8 @@ static const char* kUsage =
static void static void
print_usage_and_exit(bool error) print_usage_and_exit(bool error)
{ {
fprintf(error ? stderr : stdout, kUsage, kCommandName); fprintf(error ? stderr : stdout, kUsage, kCommandName, kCommandName);
exit(error ? 1 : 0); exit(error ? 1 : 0);
} }
@@ -136,16 +139,18 @@ int
main(int argc, const char* const* argv) main(int argc, const char* const* argv)
{ {
bool printPath = false; bool printPath = false;
bool listArchitectures = false;
while (true) { while (true) {
static struct option sLongOptions[] = { static struct option sLongOptions[] = {
{ "help", no_argument, 0, 'h' }, { "help", no_argument, 0, 'h' },
{ "list-architectures", no_argument, 0, 'l' },
{ "print-path", no_argument, 0, 'p' }, { "print-path", no_argument, 0, 'p' },
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
opterr = 0; // don't print errors opterr = 0; // don't print errors
int c = getopt_long(argc, (char**)argv, "+hp", int c = getopt_long(argc, (char**)argv, "+hlp",
sLongOptions, NULL); sLongOptions, NULL);
if (c == -1) if (c == -1)
break; break;
@@ -155,6 +160,10 @@ main(int argc, const char* const* argv)
print_usage_and_exit(false); print_usage_and_exit(false);
break; break;
case 'l':
listArchitectures = true;
break;
case 'p': case 'p':
printPath = true; printPath = true;
break; break;
@@ -165,6 +174,30 @@ main(int argc, const char* const* argv)
} }
} }
// only one of listArchitectures, printPath may be specified
if (listArchitectures && printPath)
print_usage_and_exit(true);
// get architectures
BStringList architectures;
status_t error = get_architectures(architectures);
if (error != B_OK) {
fprintf(stderr, "Error: Failed to get architectures: %s\n",
strerror(error));
exit(1);
}
// list architectures
if (listArchitectures) {
if (optind != argc)
print_usage_and_exit(true);
int32 count = architectures.CountStrings();
for (int32 i = 0; i < count; i++)
printf("%s\n", architectures.StringAt(i).String());
return 0;
}
// The remaining arguments are the architecture and optionally the command // The remaining arguments are the architecture and optionally the command
// to execute. // to execute.
if (optind >= argc) if (optind >= argc)
@@ -178,14 +211,6 @@ main(int argc, const char* const* argv)
print_usage_and_exit(true); print_usage_and_exit(true);
// check the architecture // check the architecture
BStringList architectures;
status_t error = get_architectures(architectures);
if (error != B_OK) {
fprintf(stderr, "Error: Failed to get architectures: %s\n",
strerror(error));
exit(1);
}
if (!architectures.HasString(architecture)) { if (!architectures.HasString(architecture)) {
fprintf(stderr, "Error: Unsupported architecture \"%s\"\n", fprintf(stderr, "Error: Unsupported architecture \"%s\"\n",
architecture); architecture);