tree-wide: extend verbs functions with extra per-verb data parameter
We often have a pattern where the same verb function is used for multiple actions. This leads to an antipattern where we figure out what action needs to be taken from argv[0] multiple times: often once in parse_argv() to figure out what options are allowed, then once again implicitly in dispatch_verb(), and then again in the action verb itself. Let's allow passing a parameter into the verb to simplify this. This matches a pattern we have in conf-parser.h, where we have both void *userdata (more global) and void *data (per-config item). Here, I opted for uintptr_t userdata. It seems that most of the time we'll want to just pass an enum value. This works OK with no casts. I also tried a void* and union. In both cases, much more boilerplate is needed: either a cast or a macro to help avoid compiler warnings. uintptr_t seems generic enough to cover foreseeable usecases with no fuss. This is a noop refactoring. See next commit for an example.
This commit is contained in:
committed by
Luca Boccassi
parent
9e8babc8ab
commit
31ddd87da0
@@ -549,7 +549,7 @@ static int get_hostname_based_on_flag(sd_bus *bus) {
|
||||
return get_one_name(bus, attr, NULL);
|
||||
}
|
||||
|
||||
static int verb_show_status(int argc, char **argv, void *userdata) {
|
||||
static int verb_show_status(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
sd_bus *bus = userdata;
|
||||
int r;
|
||||
|
||||
@@ -601,7 +601,7 @@ static int set_simple_string(sd_bus *bus, const char *target, const char *method
|
||||
return set_simple_string_internal(bus, NULL, target, method, value);
|
||||
}
|
||||
|
||||
static int verb_set_hostname(int argc, char **argv, void *userdata) {
|
||||
static int verb_set_hostname(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
_cleanup_free_ char *h = NULL;
|
||||
const char *hostname = argv[1];
|
||||
sd_bus *bus = userdata;
|
||||
@@ -687,27 +687,27 @@ static int verb_set_hostname(int argc, char **argv, void *userdata) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int verb_get_or_set_hostname(int argc, char **argv, void *userdata) {
|
||||
static int verb_get_or_set_hostname(int argc, char *argv[], uintptr_t data, void *userdata) {
|
||||
return argc == 1 ? get_hostname_based_on_flag(userdata) :
|
||||
verb_set_hostname(argc, argv, userdata);
|
||||
verb_set_hostname(argc, argv, data, userdata);
|
||||
}
|
||||
|
||||
static int verb_get_or_set_icon_name(int argc, char **argv, void *userdata) {
|
||||
static int verb_get_or_set_icon_name(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
return argc == 1 ? get_one_name(userdata, "IconName", NULL) :
|
||||
set_simple_string(userdata, "icon", "SetIconName", argv[1]);
|
||||
}
|
||||
|
||||
static int verb_get_or_set_chassis(int argc, char **argv, void *userdata) {
|
||||
static int verb_get_or_set_chassis(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
return argc == 1 ? get_one_name(userdata, "Chassis", NULL) :
|
||||
set_simple_string(userdata, "chassis", "SetChassis", argv[1]);
|
||||
}
|
||||
|
||||
static int verb_get_or_set_deployment(int argc, char **argv, void *userdata) {
|
||||
static int verb_get_or_set_deployment(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
return argc == 1 ? get_one_name(userdata, "Deployment", NULL) :
|
||||
set_simple_string(userdata, "deployment", "SetDeployment", argv[1]);
|
||||
}
|
||||
|
||||
static int verb_get_or_set_location(int argc, char **argv, void *userdata) {
|
||||
static int verb_get_or_set_location(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
return argc == 1 ? get_one_name(userdata, "Location", NULL) :
|
||||
set_simple_string(userdata, "location", "SetLocation", argv[1]);
|
||||
}
|
||||
@@ -752,7 +752,7 @@ static int help(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int verb_help(int argc, char **argv, void *userdata) {
|
||||
static int verb_help(int argc, char *argv[], uintptr_t _data, void *userdata) {
|
||||
return help();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user