Skip to content

Commit d894b46

Browse files
Treehugger RobotGerrit Code Review
authored andcommitted
Merge "modprobe: switch to getopt_long for argument parsing"
2 parents c58d1e4 + 3ad274b commit d894b46

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

toolbox/modprobe.cpp

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,40 @@
1717
#include <ctype.h>
1818
#include <getopt.h>
1919
#include <stdlib.h>
20+
2021
#include <iostream>
2122

23+
#include <android-base/file.h>
2224
#include <android-base/strings.h>
2325
#include <modprobe/modprobe.h>
2426

27+
namespace {
28+
2529
enum modprobe_mode {
2630
AddModulesMode,
2731
RemoveModulesMode,
2832
ListModulesMode,
2933
ShowDependenciesMode,
3034
};
3135

32-
static void print_usage(void) {
36+
void print_usage(void) {
3337
std::cerr << "Usage:" << std::endl;
3438
std::cerr << std::endl;
35-
std::cerr << " modprobe [-alrqvsDb] [-d DIR] [MODULE]+" << std::endl;
36-
std::cerr << " modprobe [-alrqvsDb] [-d DIR] MODULE [symbol=value][...]" << std::endl;
39+
// -d option is required on Android
40+
std::cerr << " modprobe [options] -d DIR MODULE..." << std::endl;
41+
std::cerr << " modprobe [options] -d DIR MODULE [symbol=value]..." << std::endl;
3742
std::cerr << std::endl;
3843
std::cerr << "Options:" << std::endl;
39-
std::cerr << " -b: Apply blocklist to module names too" << std::endl;
40-
std::cerr << " -d: Load modules from DIR, option may be used multiple times" << std::endl;
41-
std::cerr << " -D: Print dependencies for modules only, do not load";
42-
std::cerr << " -h: Print this help" << std::endl;
43-
std::cerr << " -l: List modules matching pattern" << std::endl;
44-
std::cerr << " -r: Remove MODULE (multiple modules may be specified)" << std::endl;
45-
std::cerr << " -q: Quiet" << std::endl;
46-
std::cerr << " -v: Verbose" << std::endl;
44+
std::cerr << " -b, --use-blocklist: Apply blocklist to module names too" << std::endl;
45+
std::cerr << " -d, --dirname=DIR: Load modules from DIR, option may be used multiple times"
46+
<< std::endl;
47+
std::cerr << " -D, --show-depends: Print dependencies for modules only, do not load"
48+
<< std::endl;
49+
std::cerr << " -h, --help: Print this help" << std::endl;
50+
std::cerr << " -l, --list: List modules matching pattern" << std::endl;
51+
std::cerr << " -r, --remove: Remove MODULE (multiple modules may be specified)" << std::endl;
52+
std::cerr << " -q, --quiet: disable messages" << std::endl;
53+
std::cerr << " -v, --verbose: enable more messages" << std::endl;
4754
std::cerr << std::endl;
4855
}
4956

@@ -54,6 +61,8 @@ static void print_usage(void) {
5461
return EXIT_FAILURE; \
5562
}
5663

64+
} // anonymous namespace
65+
5766
extern "C" int modprobe_main(int argc, char** argv) {
5867
std::vector<std::string> modules;
5968
std::string module_parameters;
@@ -64,7 +73,23 @@ extern "C" int modprobe_main(int argc, char** argv) {
6473
int rv = EXIT_SUCCESS;
6574

6675
int opt;
67-
while ((opt = getopt(argc, argv, "abd:Dhlqrv")) != -1) {
76+
int option_index = 0;
77+
// NB: We have non-standard short options -l and -D to make it easier for
78+
// OEMs to transition from toybox.
79+
// clang-format off
80+
static struct option long_options[] = {
81+
{ "all", no_argument, 0, 'a' },
82+
{ "use-blocklist", no_argument, 0, 'b' },
83+
{ "dirname", required_argument, 0, 'd' },
84+
{ "show-depends", no_argument, 0, 'D' },
85+
{ "help", no_argument, 0, 'h' },
86+
{ "list", no_argument, 0, 'l' },
87+
{ "quiet", no_argument, 0, 'q' },
88+
{ "remove", no_argument, 0, 'r' },
89+
{ "verbose", no_argument, 0, 'v' },
90+
};
91+
// clang-format on
92+
while ((opt = getopt_long(argc, argv, "abd:Dhlqrv", long_options, &option_index)) != -1) {
6893
switch (opt) {
6994
case 'a':
7095
// toybox modprobe supported -a to load multiple modules, this
@@ -121,9 +146,9 @@ extern "C" int modprobe_main(int argc, char** argv) {
121146
if (verbose) {
122147
std::cout << "mode is " << mode << std::endl;
123148
std::cout << "verbose is " << verbose << std::endl;
124-
std::cout << "mod_dirs is: " << android::base::Join(mod_dirs, "") << std::endl;
125-
std::cout << "modules is: " << android::base::Join(modules, "") << std::endl;
126-
std::cout << "module parameters is: " << android::base::Join(module_parameters, "")
149+
std::cout << "mod_dirs is: " << android::base::Join(mod_dirs, " ") << std::endl;
150+
std::cout << "modules is: " << android::base::Join(modules, " ") << std::endl;
151+
std::cout << "module parameters is: " << android::base::Join(module_parameters, " ")
127152
<< std::endl;
128153
}
129154

@@ -159,13 +184,13 @@ extern "C" int modprobe_main(int argc, char** argv) {
159184
switch (mode) {
160185
case AddModulesMode:
161186
if (!m.LoadWithAliases(module, true, module_parameters)) {
162-
std::cerr << "Failed to load module " << module;
187+
std::cerr << "Failed to load module " << module << std::endl;
163188
rv = EXIT_FAILURE;
164189
}
165190
break;
166191
case RemoveModulesMode:
167192
if (!m.Remove(module)) {
168-
std::cerr << "Failed to remove module " << module;
193+
std::cerr << "Failed to remove module " << module << std::endl;
169194
rv = EXIT_FAILURE;
170195
}
171196
break;
@@ -192,7 +217,7 @@ extern "C" int modprobe_main(int argc, char** argv) {
192217
break;
193218
}
194219
default:
195-
std::cerr << "Bad mode";
220+
std::cerr << "Bad mode" << std::endl;
196221
rv = EXIT_FAILURE;
197222
}
198223
}

0 commit comments

Comments
 (0)