Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list parsing feature for gravity #1559

Merged
merged 10 commits into from
May 20, 2023
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ set(sources
FTL.h
gc.c
gc.h
gravity-tools.c
gravity-tools.h
log.c
log.h
main.c
Expand Down
27 changes: 26 additions & 1 deletion src/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "shmem.h"
// LUA dependencies
#include "lua/ftl_lua.h"
// gravity_parseList()
#include "gravity-tools.h"
// run_dhcp_discover()
#include "tools/dhcp-discover.h"
// run_arp_scan()
Expand Down Expand Up @@ -126,7 +128,15 @@ static const char __attribute__ ((pure)) *cli_color(const char *color)
return is_term() ? color : "";
}

static inline bool strEndsWith(const char *input, const char *end){
// Go back to beginning of line and erase to end of line if STDOUT is a terminal
const char __attribute__ ((pure)) *cli_over(void)
{
// \x1b[K is the ANSI escape sequence for "erase to end of line"
return is_term() ? "\r\x1b[K" : "\r";
}

static inline bool strEndsWith(const char *input, const char *end)
{
return strcmp(input + strlen(input) - strlen(end), end) == 0;
}

Expand Down Expand Up @@ -165,6 +175,21 @@ void parse_args(int argc, char* argv[])
(argc > 1 && strEndsWith(argv[1], ".db")))
exit(sqlite3_shell_main(argc, argv));

// If the first argument is "gravity" (e.g., /usr/bin/pihole-FTL gravity),
// we offer some specialized gravity tools
if(argc > 1 && strcmp(argv[1], "gravity") == 0)
{
// pihole-FTL gravity parseList <infile> <outfile> <adlistID>
if(argc == 6 && strcmp(argv[2], "parseList") == 0)
{
// Parse the given list and write the result to the given file
exit(gravity_parseList(argv[3], argv[4], argv[5]));
}

printf("Incorrect usage of pihole-FTL gravity subcommand\n");
exit(EXIT_FAILURE);
}

// DHCP discovery mode
if(argc > 1 && strcmp(argv[1], "dhcp-discover") == 0)
{
Expand Down
1 change: 1 addition & 0 deletions src/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const char *cli_qst(void) __attribute__ ((const));
const char *cli_done(void) __attribute__ ((pure));
const char *cli_bold(void) __attribute__ ((pure));
const char *cli_normal(void) __attribute__ ((pure));
const char *cli_over(void) __attribute__ ((pure));

// defined in dnsmasq_interface.c
int check_struct_sizes(void);
Expand Down
Loading