]> git.ipfire.org Git - people/ms/network.git/blob - src/networkctl/main.c
networkctl: Implement "zone list" command
[people/ms/network.git] / src / networkctl / main.c
1 /*#############################################################################
2 # #
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #include <errno.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <unistd.h>
25
26 #include <systemd/sd-bus.h>
27
28 #include "command.h"
29 #include "zone.h"
30
31 static int networkctl_status(sd_bus* bus, int argc, char* argv[]) {
32 printf("%s called\n", __FUNCTION__);
33 return 0;
34 }
35
36 static int networkctl_main(sd_bus* bus, int argc, char* argv[]) {
37 static const struct command commands[] = {
38 { "status", 0, networkctl_status },
39 { "zone", 0, networkctl_zone },
40 { NULL },
41 };
42
43 return command_dispatch(bus, commands, argc, argv);
44 }
45
46 static int version(void) {
47 printf("networkctl %s\n", PACKAGE_VERSION);
48
49 return 0;
50 }
51
52 static int help(void) {
53 printf(
54 "%s [OPTIONS...] COMMAND\n\n"
55 "Options:\n"
56 " -h --help Show help\n"
57 " --version Show version\n",
58 program_invocation_short_name
59 );
60
61 return 0;
62 }
63
64 static int parse_argv(int argc, char* argv[]) {
65 enum {
66 ARG_VERSION,
67 };
68
69 static const struct option options[] = {
70 { "help", no_argument, NULL, 'h' },
71 { "version", no_argument, NULL, ARG_VERSION },
72 { NULL },
73 };
74 int c;
75
76 for (;;) {
77 c = getopt_long(argc, argv, "h", options, NULL);
78 if (c < 0)
79 break;
80
81 switch (c) {
82 case 'h':
83 return help();
84
85 case ARG_VERSION:
86 return version();
87
88 case '?':
89 return -EINVAL;
90
91 default:
92 break;
93 }
94 }
95
96 return 0;
97 }
98
99 int main(int argc, char* argv[]) {
100 sd_bus* bus = NULL;
101 int r;
102
103 // Parse command line arguments
104 r = parse_argv(argc, argv);
105 if (r)
106 goto ERROR;
107
108 // Connect to system bus
109 r = sd_bus_open_system(&bus);
110 if (r < 0) {
111 fprintf(stderr, "Could not connect to system bus: %m\n");
112 goto ERROR;
113 }
114
115 // Run a command
116 r = networkctl_main(bus, argc, argv);
117
118 ERROR:
119 if (bus)
120 sd_bus_flush_close_unref(bus);
121
122 return r;
123 }