]> git.ipfire.org Git - network.git/commitdiff
networkctl: Implement a basic command dispatcher
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Mar 2023 16:55:45 +0000 (16:55 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 1 Mar 2023 17:14:19 +0000 (17:14 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/networkctl/command.c [new file with mode: 0644]
src/networkctl/command.h [new file with mode: 0644]
src/networkctl/main.c

index 16400601b634eb2d73503c010c14338547ad772c..186af948736294e3ae349919edbc109d053b1227 100644 (file)
@@ -383,6 +383,8 @@ bin_PROGRAMS += \
        networkctl
 
 dist_networkctl_SOURCES = \
+       src/networkctl/command.c \
+       src/networkctl/command.h \
        src/networkctl/main.c
 
 networkctl_CFLAGS = \
diff --git a/src/networkctl/command.c b/src/networkctl/command.c
new file mode 100644 (file)
index 0000000..7114efe
--- /dev/null
@@ -0,0 +1,58 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <getopt.h>
+#include <string.h>
+
+#include "command.h"
+
+static const struct command* command_find(const struct command* commands, const char* verb) {
+       for (const struct command* command = commands; command->verb; command++) {
+               if (strcmp(command->verb, verb) == 0)
+                       return command;
+       }
+
+       return NULL;
+}
+
+int command_dispatch(sd_bus* bus, const struct command* commands, int argc, char* argv[]) {
+       const struct command* command = NULL;
+
+       argc -= optind;
+       argv += optind;
+       optind = 1;
+
+       if (!argc) {
+               fprintf(stderr, "Command required\n");
+               return -EINVAL;
+       }
+
+       const char* verb = argv[0];
+
+       // Find a matching command
+       command = command_find(commands, verb);
+       if (!command) {
+               fprintf(stderr, "Unknown command '%s'\n", verb);
+               return -EINVAL;
+       }
+
+       return command->callback(bus, argc, argv);
+}
diff --git a/src/networkctl/command.h b/src/networkctl/command.h
new file mode 100644 (file)
index 0000000..f8f295e
--- /dev/null
@@ -0,0 +1,34 @@
+/*#############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2023 IPFire Network Development Team                          #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef NETWORKCTL_COMMAND_H
+#define NETWORKCTL_COMMAND_H
+
+#include <systemd/sd-bus.h>
+
+struct command {
+       const char* verb;
+       int flags;
+       int (*callback)(sd_bus* bus, int argc, char* argv[]);
+};
+
+int command_dispatch(sd_bus* bus, const struct command* commands, int argc, char* argv[]);
+
+#endif /* NETWORKCTL_COMMAND_H */
index 494fd7d6303881711021a8cefcafa45dd9987061..0cbc3e68ccb839a9c99f1d6f2243fe1cdf88fdb5 100644 (file)
 
 #include <systemd/sd-bus.h>
 
+#include "command.h"
+
+static int networkctl_status(sd_bus* bus, int argc, char* argv[]) {
+       printf("%s called\n", __FUNCTION__);
+       return 0;
+}
+
+static int networkctl_main(sd_bus* bus, int argc, char* argv[]) {
+       static const struct command commands[] = {
+               { "status", 0, networkctl_status },
+               { NULL },
+       };
+
+       return command_dispatch(bus, commands, argc, argv);
+}
+
 static int version(void) {
        printf("networkctl %s\n", PACKAGE_VERSION);
 
@@ -94,7 +110,8 @@ int main(int argc, char* argv[]) {
                goto ERROR;
        }
 
-       // XXX TODO Do all the work
+       // Run a command
+       r = networkctl_main(bus, argc, argv);
 
 ERROR:
        if (bus)