forked from pi-hole/FTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.c
164 lines (146 loc) · 4.14 KB
/
args.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* Argument parsing routines
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#include "FTL.h"
#include "version.h"
bool debug = false;
bool daemonmode = true;
bool travis = false;
int argc_dnsmasq = 0;
char **argv_dnsmasq = NULL;
void parse_args(int argc, char* argv[])
{
int i;
// Regardless of any arguments, we always pass "-k" (nofork) to dnsmasq
argc_dnsmasq = 2;
argv_dnsmasq = calloc(argc_dnsmasq, sizeof(char*));
argv_dnsmasq[0] = "";
argv_dnsmasq[1] = "-k";
// start from 1, as argv[0] is the executable name "pihole-FTL"
for(i=1; i < argc; i++)
{
bool ok = false;
if(strcmp(argv[i], "d") == 0 ||
strcmp(argv[i], "debug") == 0)
{
debug = true;
ok = true;
// Replace "-k" by "-d" (debug mode implies nofork)
argv_dnsmasq[1] = "-d";
}
if(strcmp(argv[i], "test") == 0)
{
killed = 1;
ok = true;
}
if(strcmp(argv[i], "-v") == 0 ||
strcmp(argv[i], "version") == 0 ||
strcmp(argv[i], "--version") == 0)
{
const char * commit = GIT_HASH;
const char * tag = GIT_TAG;
if(strlen(tag) > 1)
{
printf("%s\n",GIT_VERSION);
}
else
{
char hash[8];
// Extract first 7 characters of the hash
strncpy(hash, commit, 7); hash[7] = 0;
printf("vDev-%s\n", hash);
}
exit(EXIT_SUCCESS);
}
if(strcmp(argv[i], "-t") == 0 ||
strcmp(argv[i], "tag") == 0)
{
printf("%s\n",GIT_TAG);
exit(EXIT_SUCCESS);
}
if(strcmp(argv[i], "-b") == 0 ||
strcmp(argv[i], "branch") == 0)
{
printf("%s\n",GIT_BRANCH);
exit(EXIT_SUCCESS);
}
// Don't go into background
if(strcmp(argv[i], "-f") == 0 ||
strcmp(argv[i], "no-daemon") == 0)
{
daemonmode = false;
ok = true;
}
// Use files in local places for Travis-CI tests
if(strcmp(argv[i], "travis-ci") == 0)
{
travis = true;
ok = true;
}
// Implement dnsmasq's test function
if(strcmp(argv[i], "dnsmasq-test") == 0)
{
char *arg[2];
arg[0] = "";
arg[1] = "--test";
main_dnsmasq(2,arg);
ok = true;
}
// If we find "--" we collect everything behind that for dnsmasq
if(strcmp(argv[i], "--") == 0)
{
int j;
argc_dnsmasq = argc - i + 1;
if(argv_dnsmasq != NULL) free(argv_dnsmasq);
argv_dnsmasq = calloc(argc_dnsmasq + 2,sizeof(char*));
argv_dnsmasq[0] = "";
if(debug) argv_dnsmasq[1] = "-d";
else argv_dnsmasq[1] = "-k";
for(j=2; j < argc_dnsmasq; j++)
{
argv_dnsmasq[j] = strdup(argv[i+j-1]);
if(debug) logg("dnsmasq options: [%i]: %s",j,argv_dnsmasq[j]);
}
return;
}
// List of implemented arguments
if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "help") == 0 || strcmp(argv[i], "--help") == 0)
{
printf("pihole-FTL - The Pi-hole FTL engine\n\n");
printf("Usage: sudo service pihole-FTL <action>\n");
printf("where '<action>' is one of start / stop / restart\n\n");
printf("Available arguments:\n");
printf("\t debug More verbose logging,\n");
printf("\t don't go into daemon mode\n");
printf("\t test Don't start pihole-FTL but\n");
printf("\t instead quit immediately\n");
printf("\t-v, version Return version\n");
printf("\t-t, tag Return git tag\n");
printf("\t-b, branch Return git branch\n");
printf("\t-f, no-daemon Don't go into daemon mode\n");
printf("\t-h, help Display this help and exit\n");
printf("\tdnsmasq-test Test syntax of dnsmasq's\n");
printf("\t config files and exit\n");
printf("\n\nOnline help: https://github.com/pi-hole/FTL\n");
exit(EXIT_SUCCESS);
}
// Return success error code on this undocumented flag
if(strcmp(argv[i], "--resolver") == 0)
{
printf("True\n");
exit(EXIT_SUCCESS);
}
// Complain if invalid options have been found
if(!ok)
{
printf("pihole-FTL: invalid option -- '%s'\nTry '%s --help' for more information\n", argv[i], argv[0]);
exit(EXIT_FAILURE);
}
}
}