]> git.ipfire.org Git - people/ms/network.git/blob - src/networkctl/main.c
networkctl: Add some help and version arguments
[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 static int version(void) {
29 printf("networkctl %s\n", PACKAGE_VERSION);
30
31 return 0;
32 }
33
34 static int help(void) {
35 printf(
36 "%s [OPTIONS...] COMMAND\n\n"
37 "Options:\n"
38 " -h --help Show help\n"
39 " --version Show version\n",
40 program_invocation_short_name
41 );
42
43 return 0;
44 }
45
46 static int parse_argv(int argc, char* argv[]) {
47 enum {
48 ARG_VERSION,
49 };
50
51 static const struct option options[] = {
52 { "help", no_argument, NULL, 'h' },
53 { "version", no_argument, NULL, ARG_VERSION },
54 { NULL },
55 };
56 int c;
57
58 for (;;) {
59 c = getopt_long(argc, argv, "h", options, NULL);
60 if (c < 0)
61 break;
62
63 switch (c) {
64 case 'h':
65 return help();
66
67 case ARG_VERSION:
68 return version();
69
70 case '?':
71 return -EINVAL;
72
73 default:
74 break;
75 }
76 }
77
78 return 0;
79 }
80
81 int main(int argc, char* argv[]) {
82 sd_bus* bus = NULL;
83 int r;
84
85 // Parse command line arguments
86 r = parse_argv(argc, argv);
87 if (r)
88 goto ERROR;
89
90 // Connect to system bus
91 r = sd_bus_open_system(&bus);
92 if (r < 0) {
93 fprintf(stderr, "Could not connect to system bus: %m\n");
94 goto ERROR;
95 }
96
97 // XXX TODO Do all the work
98
99 ERROR:
100 if (bus)
101 sd_bus_flush_close_unref(bus);
102
103 return r;
104 }