summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak2017-10-03 13:44:19 +0200
committerKarel Zak2017-10-03 13:44:19 +0200
commit18d3a03fda242d42cc2cc64fe3d59f3c8dac540b (patch)
tree6cd52535e43cbc6ece09b440ac18f5bf7022074f
parentrfkill: improve default output (diff)
downloadkernel-qcow2-util-linux-18d3a03fda242d42cc2cc64fe3d59f3c8dac540b.tar.gz
kernel-qcow2-util-linux-18d3a03fda242d42cc2cc64fe3d59f3c8dac540b.tar.xz
kernel-qcow2-util-linux-18d3a03fda242d42cc2cc64fe3d59f3c8dac540b.zip
rfkill: refactor actions
* convert action string to ID and use switch() * add note about default output to the man page Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--sys-utils/rfkill.810
-rw-r--r--sys-utils/rfkill.c85
2 files changed, 75 insertions, 20 deletions
diff --git a/sys-utils/rfkill.8 b/sys-utils/rfkill.8
index ce0aadc31..0cb704070 100644
--- a/sys-utils/rfkill.8
+++ b/sys-utils/rfkill.8
@@ -5,6 +5,16 @@ rfkill \- tool for enabling and disabling wireless devices
.SH SYNOPSIS
.B rfkill
.RI [ options ] " command" " [" id | type \ ...]
+
+.SH DESCRIPTION
+.B rfkill
+lists, enabling and disabling wireless devices.
+
+The default output is subject to change. So whenever possible, you should
+avoid using default outputs in your scripts. Always explicitly define expected
+columns by using the \fB\-\-output\fR option together with a columns list in
+environments where a stable output is required.
+
.SH OPTIONS
.TP
\fB\-J\fR, \fB\-\-json\fR
diff --git a/sys-utils/rfkill.c b/sys-utils/rfkill.c
index c84b5850e..5ffbbf59a 100644
--- a/sys-utils/rfkill.c
+++ b/sys-utils/rfkill.c
@@ -79,6 +79,23 @@ struct rfkill_id {
} result;
};
+/* supported actions */
+enum {
+ ACT_LIST,
+ ACT_HELP,
+ ACT_EVENT,
+ ACT_BLOCK,
+ ACT_UNBLOCK
+};
+
+static char *rfkill_actions[] = {
+ [ACT_LIST] = "list",
+ [ACT_HELP] = "help",
+ [ACT_EVENT] = "event",
+ [ACT_BLOCK] = "block",
+ [ACT_UNBLOCK] = "unblock"
+};
+
/* column IDs */
enum {
COL_DEVICE,
@@ -144,6 +161,17 @@ static const struct colinfo *get_column_info(int num)
return &infos[get_column_id(num)];
}
+static int string_to_action(const char *str)
+{
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(rfkill_actions); i++)
+ if (strcmp(str, rfkill_actions[i]) == 0)
+ return i;
+
+ return -EINVAL;
+}
+
static int rfkill_event(void)
{
struct rfkill_event event;
@@ -499,7 +527,7 @@ static void __attribute__((__noreturn__)) usage(void)
int main(int argc, char **argv)
{
struct control ctrl = { 0 };
- int c;
+ int c, act = ACT_LIST;
char *outarg = NULL;
static const struct option longopts[] = {
{ "json", no_argument, NULL, 'J' },
@@ -549,7 +577,16 @@ int main(int argc, char **argv)
argc -= optind;
argv += optind;
- if (argc == 0 || strcmp(*argv, "list") == 0) {
+ if (argc > 0) {
+ act = string_to_action(*argv);
+ if (act < 0)
+ errtryhelp(EXIT_FAILURE);
+ argv++;
+ argc--;
+ }
+
+ switch (act) {
+ case ACT_LIST:
columns[ncolumns++] = COL_ID;
columns[ncolumns++] = COL_TYPE;
columns[ncolumns++] = COL_DEVICE;
@@ -561,34 +598,42 @@ int main(int argc, char **argv)
ARRAY_SIZE(columns), &ncolumns,
column_name_to_id) < 0)
return EXIT_FAILURE;
+
rfkill_list_init(&ctrl);
- if (argc < 2) {
- if (*argv && strcmp(*argv, "list") == 0)
- argv++;
+ if (!argc)
+ ret |= rfkill_list_fill(&ctrl, NULL); /* ALL */
+ else while (argc) {
ret |= rfkill_list_fill(&ctrl, *argv);
- } else {
- while (--argc) {
- argv++;
- ret |= rfkill_list_fill(&ctrl, *argv);
- }
+ argc--;
+ argv++;
}
rfkill_list_output(&ctrl);
- } else if (strcmp(*argv, "event") == 0) {
+ break;
+
+ case ACT_EVENT:
ret = rfkill_event();
- } else if (strcmp(*argv, "help") == 0) {
+ break;
+
+ case ACT_HELP:
usage();
- } else if (strcmp(*argv, "block") == 0 && argc > 1) {
- while (--argc) {
- argv++;
+ break;
+
+ case ACT_BLOCK:
+ while (argc) {
ret |= rfkill_block(1, *argv);
- }
- } else if (strcmp(*argv, "unblock") == 0 && argc > 1) {
- while (--argc) {
+ argc--;
argv++;
+ }
+ break;
+
+ case ACT_UNBLOCK:
+ while (argc) {
ret |= rfkill_block(0, *argv);
+ argv++;
+ argc--;
}
- } else
- errtryhelp(EXIT_FAILURE);
+ break;
+ }
return ret ? EXIT_FAILURE : EXIT_SUCCESS;
}