udevadm: introduce parse_device_action() helper function

This commit is contained in:
Yu Watanabe
2021-08-17 22:57:04 +09:00
parent d1429d8f78
commit 6de7fa8759
4 changed files with 31 additions and 23 deletions

View File

@@ -50,25 +50,17 @@ static int parse_argv(int argc, char *argv[]) {
{}
};
int c;
int r, c;
while ((c = getopt_long(argc, argv, "a:N:Vh", options, NULL)) >= 0)
switch (c) {
case 'a': {
sd_device_action_t a;
if (streq(optarg, "help")) {
dump_device_action_table();
case 'a':
r = parse_device_action(optarg, &arg_action);
if (r < 0)
return log_error_errno(r, "Invalid action '%s'", optarg);
if (r == 0)
return 0;
}
a = device_action_from_string(optarg);
if (a < 0)
return log_error_errno(a, "Invalid action '%s'", optarg);
arg_action = a;
break;
}
case 'N':
arg_resolve_name_timing = resolve_name_timing_from_string(optarg);
if (arg_resolve_name_timing < 0)

View File

@@ -312,17 +312,13 @@ int trigger_main(int argc, char *argv[], void *userdata) {
else
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown type --type=%s", optarg);
break;
case 'c': {
if (streq(optarg, "help")) {
dump_device_action_table();
case 'c':
r = parse_device_action(optarg, &action);
if (r < 0)
return log_error_errno(r, "Unknown action '%s'", optarg);
if (r == 0)
return 0;
}
action = device_action_from_string(optarg);
if (action < 0)
return log_error_errno(action, "Unknown action '%s'", optarg);
break;
}
case 's':
r = sd_device_enumerator_add_match_subsystem(e, optarg, true);
if (r < 0)

View File

@@ -110,3 +110,22 @@ int find_device_with_action(const char *id, sd_device_action_t action, sd_device
return device_new_from_synthetic_event(ret, id, device_action_to_string(action));
}
int parse_device_action(const char *str, sd_device_action_t *action) {
sd_device_action_t a;
assert(str);
assert(action);
if (streq(str, "help")) {
dump_device_action_table();
return 0;
}
a = device_action_from_string(str);
if (a < 0)
return a;
*action = a;
return 1;
}

View File

@@ -5,3 +5,4 @@
int find_device(const char *id, const char *prefix, sd_device **ret);
int find_device_with_action(const char *id, sd_device_action_t action, sd_device **ret);
int parse_device_action(const char *str, sd_device_action_t *action);