From: Michael Tremer Date: Wed, 1 Mar 2023 16:55:45 +0000 (+0000) Subject: networkctl: Implement a basic command dispatcher X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f3bb976c4be0fe41959d7623f4ae3269c0e95e1d;p=network.git networkctl: Implement a basic command dispatcher Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 16400601..186af948 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 00000000..7114efe6 --- /dev/null +++ b/src/networkctl/command.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include +#include + +#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 index 00000000..f8f295eb --- /dev/null +++ b/src/networkctl/command.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef NETWORKCTL_COMMAND_H +#define NETWORKCTL_COMMAND_H + +#include + +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 */ diff --git a/src/networkctl/main.c b/src/networkctl/main.c index 494fd7d6..0cbc3e68 100644 --- a/src/networkctl/main.c +++ b/src/networkctl/main.c @@ -25,6 +25,22 @@ #include +#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)