From 78963d3c9f9182530e7f2ce14b612b158e22de91 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?David=20H=C3=B6ppner?= <0xffea@gmail.com>
Date: Sat, 1 Mar 2014 23:08:48 +0800
Subject: [PATCH] setarch: list available architectures
Fixes #10575
---
src/bin/setarch.cpp | 49 +++++++++++++++++++++++++++++++++++++------------
1 file changed, 37 insertions(+), 12 deletions(-)
diff --git a/src/bin/setarch.cpp b/src/bin/setarch.cpp
index b6fc26c..44c27be 100644
a
|
b
|
const char* kCommandName = __progname;
|
23 | 23 | |
24 | 24 | |
25 | 25 | static const char* kUsage = |
26 | | "Usage: %s [ <options> ] <architecture> [ <command> ... ]\n" |
| 26 | "Usage: %s [-hl]\n" |
| 27 | " %s [-p] <architecture> [ <command> ... ]\n" |
27 | 28 | "Executes the given command or, by default, a shell with a PATH\n" |
28 | 29 | "environment variable modified such that commands for the given\n" |
29 | 30 | "architecture will be preferred, respectively used exclusively in case of\n" |
… |
… |
static const char* kUsage =
|
32 | 33 | "Options:\n" |
33 | 34 | " -h, --help\n" |
34 | 35 | " Print this usage info.\n" |
| 36 | " -l, --list-architectures\n" |
| 37 | " List all architectures.\n" |
35 | 38 | " -p, --print-path\n" |
36 | 39 | " Only print the modified PATH variable value; don't execute any\n" |
37 | 40 | " command.\n" |
… |
… |
static const char* kUsage =
|
41 | 44 | static void |
42 | 45 | print_usage_and_exit(bool error) |
43 | 46 | { |
44 | | fprintf(error ? stderr : stdout, kUsage, kCommandName); |
45 | | exit(error ? 1 : 0); |
| 47 | fprintf(error ? stderr : stdout, kUsage, kCommandName, kCommandName); |
| 48 | exit(error ? 1 : 0); |
46 | 49 | } |
47 | 50 | |
48 | 51 | |
… |
… |
int
|
136 | 139 | main(int argc, const char* const* argv) |
137 | 140 | { |
138 | 141 | bool printPath = false; |
| 142 | bool listArchitectures = false; |
139 | 143 | |
140 | 144 | while (true) { |
141 | 145 | static struct option sLongOptions[] = { |
142 | 146 | { "help", no_argument, 0, 'h' }, |
| 147 | { "list-architectures", no_argument, 0, 'l' }, |
143 | 148 | { "print-path", no_argument, 0, 'p' }, |
144 | 149 | { 0, 0, 0, 0 } |
145 | 150 | }; |
146 | 151 | |
147 | 152 | opterr = 0; // don't print errors |
148 | | int c = getopt_long(argc, (char**)argv, "+hp", |
| 153 | int c = getopt_long(argc, (char**)argv, "+hlp", |
149 | 154 | sLongOptions, NULL); |
150 | 155 | if (c == -1) |
151 | 156 | break; |
… |
… |
main(int argc, const char* const* argv)
|
155 | 160 | print_usage_and_exit(false); |
156 | 161 | break; |
157 | 162 | |
| 163 | case 'l': |
| 164 | listArchitectures = true; |
| 165 | break; |
| 166 | |
158 | 167 | case 'p': |
159 | 168 | printPath = true; |
160 | 169 | break; |
… |
… |
main(int argc, const char* const* argv)
|
165 | 174 | } |
166 | 175 | } |
167 | 176 | |
| 177 | // only one of listArchitectures, printPath may be specified |
| 178 | if (listArchitectures && printPath) |
| 179 | print_usage_and_exit(true); |
| 180 | |
| 181 | // get architectures |
| 182 | BStringList architectures; |
| 183 | status_t error = get_architectures(architectures); |
| 184 | if (error != B_OK) { |
| 185 | fprintf(stderr, "Error: Failed to get architectures: %s\n", |
| 186 | strerror(error)); |
| 187 | exit(1); |
| 188 | } |
| 189 | |
| 190 | // list architectures |
| 191 | if (listArchitectures) { |
| 192 | if (optind != argc) |
| 193 | print_usage_and_exit(true); |
| 194 | |
| 195 | int32 count = architectures.CountStrings(); |
| 196 | for (int32 i = 0; i < count; i++) |
| 197 | printf("%s\n", architectures.StringAt(i).String()); |
| 198 | return 0; |
| 199 | } |
| 200 | |
168 | 201 | // The remaining arguments are the architecture and optionally the command |
169 | 202 | // to execute. |
170 | 203 | if (optind >= argc) |
… |
… |
main(int argc, const char* const* argv)
|
178 | 211 | print_usage_and_exit(true); |
179 | 212 | |
180 | 213 | // check the architecture |
181 | | BStringList architectures; |
182 | | status_t error = get_architectures(architectures); |
183 | | if (error != B_OK) { |
184 | | fprintf(stderr, "Error: Failed to get architectures: %s\n", |
185 | | strerror(error)); |
186 | | exit(1); |
187 | | } |
188 | | |
189 | 214 | if (!architectures.HasString(architecture)) { |
190 | 215 | fprintf(stderr, "Error: Unsupported architecture \"%s\"\n", |
191 | 216 | architecture); |