networkctl
dist_networkctl_SOURCES = \
+ src/networkctl/command.c \
+ src/networkctl/command.h \
src/networkctl/main.c
networkctl_CFLAGS = \
--- /dev/null
+/*#############################################################################
+# #
+# 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);
+}
--- /dev/null
+/*#############################################################################
+# #
+# 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 */
#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);
goto ERROR;
}
- // XXX TODO Do all the work
+ // Run a command
+ r = networkctl_main(bus, argc, argv);
ERROR:
if (bus)