| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- --- a/args.c 2019-05-25 21:37:26.000000000 +0200
- +++ b/args.c 2020-04-24 00:32:27.000000000 +0200
- @@ -17,20 +17,88 @@
- int argc_dnsmasq = 0;
- const char** argv_dnsmasq = NULL;
-
- +static inline bool strEndsWith(const char *input, const char *end){
- + return strcmp(input + strlen(input) - strlen(end), end) == 0;
- +}
- +
- 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 consume_for_dnsmasq = false;
- + // If the binary name is "dnsmasq" (e.g., symlink /usr/bin/dnsmasq -> /usr/bin/pihole-FTL),
- + // we operate in drop-in mode and consume all arguments for the embedded dnsmasq core
- + if(strEndsWith(argv[0], "dnsmasq"))
- + consume_for_dnsmasq = true;
- +
- + // start from 1, as argv[0] is the executable name
- + for(int i = 1; i < argc; i++)
- {
- bool ok = false;
- +
- + // Implement dnsmasq's test function, no need to prepare the entire FTL
- + // environment (initialize shared memory, lead queries from long-term
- + // database, ...) when the task is a simple (dnsmasq) syntax check
- + if(strcmp(argv[i], "dnsmasq-test") == 0 ||
- + strcmp(argv[i], "--test") == 0)
- + {
- + const 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)
- + {
- + // Remember that the rest is for dnsmasq ...
- + consume_for_dnsmasq = true;
- +
- + // ... and skip the current argument ("--")
- + continue;
- + }
- +
- + // If consume_for_dnsmasq is true, we collect all remaining options for
- + // dnsmasq
- + if(consume_for_dnsmasq)
- + {
- + argc_dnsmasq = argc - i + 2;
- + if(argv_dnsmasq != NULL)
- + free(argv_dnsmasq);
- +
- + argv_dnsmasq = calloc(argc_dnsmasq, sizeof(const char*));
- + argv_dnsmasq[0] = "";
- +
- + if(debug)
- + argv_dnsmasq[1] = "-d";
- + else
- + argv_dnsmasq[1] = "-k";
- +
- + if(debug)
- + {
- + printf("dnsmasq options: [0]: %s\n", argv_dnsmasq[0]);
- + printf("dnsmasq options: [1]: %s\n", argv_dnsmasq[1]);
- + }
- +
- + int j = 2;
- + while(i < argc)
- + {
- + argv_dnsmasq[j++] = strdup(argv[i++]);
- + if(debug)
- + printf("dnsmasq options: [%i]: %s\n", j-1, argv_dnsmasq[j-1]);
- + }
- +
- + // Return early: We have consumes all available command line arguments
- + return;
- + }
- +
- + // What follows beyond this point are FTL internal command line arguments
- +
- if(strcmp(argv[i], "d") == 0 ||
- strcmp(argv[i], "debug") == 0)
- {
- @@ -97,35 +165,6 @@
- ok = true;
- }
-
- - // Implement dnsmasq's test function
- - if(strcmp(argv[i], "dnsmasq-test") == 0)
- - {
- - const 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(const 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)
- {
|