Skip to content

Commit

Permalink
Added global configuration file with the ability to specify path.
Browse files Browse the repository at this point in the history
Supports ./configure --sysconfdir=/tmp for global configuration file.
  • Loading branch information
allinurl committed May 4, 2014
1 parent daa62ed commit c906f52
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 29 deletions.
8 changes: 6 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = goaccess

confdir = $(sysconfdir)
dist_conf_DATA = config/goaccess.conf

goaccess_SOURCES = \
browsers.c \
browsers.h \
Expand Down Expand Up @@ -53,9 +57,9 @@ goaccess_SOURCES += \
endif

if DEBUG
AM_CFLAGS = -DDEBUG -O0 -g
AM_CFLAGS = -DDEBUG -O0 -g -DSYSCONFDIR=\"$(sysconfdir)\"
else
AM_CFLAGS = -O2
AM_CFLAGS = -O2 -DSYSCONFDIR=\"$(sysconfdir)\"
endif

AM_CFLAGS += @GLIB2_CFLAGS@
Expand Down
6 changes: 2 additions & 4 deletions goaccess.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ house_keeping (void)
free (logger);

/* CONFIGURATION */
if (conf.iconfigfile)
free (conf.iconfigfile);
if (conf.debug_log) {
LOG_DEBUG (("Bye.\n"));
dbg_log_close ();
Expand Down Expand Up @@ -783,10 +781,10 @@ main (int argc, char **argv)
{
int quit = 0;

read_conf_file_arg (argc, argv);
verify_global_config (argc, argv);
parse_conf_file (&argc, &argv);
/* short + long options */
parse_cmd_line (argc, argv);

/* initialize storage */
init_storage ();
set_locale ();
Expand Down
31 changes: 17 additions & 14 deletions options.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ static char short_options[] = "f:e:p:o:"
struct option long_opts[] = {
{"log-file" , required_argument , 0 , 'f' } ,
{"agent-list" , no_argument , 0 , 'a' } ,
{"conf-dialog" , no_argument , 0 , 'c' } ,
{"conf-file" , required_argument , 0 , 'p' } ,
{"config-dialog" , no_argument , 0 , 'c' } ,
{"config-file" , required_argument , 0 , 'p' } ,
{"exclude-ip" , required_argument , 0 , 'e' } ,
{"help" , no_argument , 0 , 'h' } ,
{"http-method" , no_argument , 0 , 'M' } ,
Expand All @@ -69,6 +69,7 @@ struct option long_opts[] = {
{"log-format" , required_argument , 0 , 0 } ,
{"real-os" , no_argument , 0 , 0 } ,
{"no-color" , no_argument , 0 , 0 } ,
{"no-global-config" , no_argument , 0 , 0 } ,
{"storage" , no_argument , 0 , 's' } ,
{"no-progress" , no_argument , 0 , 0 } ,
{"with-mouse" , no_argument , 0 , 'm' } ,
Expand Down Expand Up @@ -103,7 +104,7 @@ cmd_help (void)
printf ("Enable a list of user-agents by host.\n");
printf (" ");
printf ("For faster parsing, don't enable this flag.\n");
printf (" -c --conf-dialog ");
printf (" -c --config-dialog ");
printf ("Prompt log/date configuration window.\n");
printf (" -d --with-output-resolver ");
printf ("Enable IP resolver on HTML|JSON output.\n");
Expand Down Expand Up @@ -131,7 +132,7 @@ cmd_help (void)
printf ("'-o csv' for CSV.\n");
printf (" ");
printf ("'-o json' for JSON.\n");
printf (" -p --conf-file=<filename> ");
printf (" -p --config-file=<filename> ");
printf ("Custom configuration file.\n");
printf (" -q --no-query-string ");
printf ("Ignore request's query string.\n");
Expand Down Expand Up @@ -174,20 +175,22 @@ cmd_help (void)
}

void
read_conf_file_arg (int argc, char **argv)
verify_global_config (int argc, char **argv)
{
int o, idx = 0;

conf.iconfigfile = NULL;
conf.load_global_config = 1;
while ((o = getopt_long (argc, argv, short_options, long_opts, &idx)) >= 0) {
if (-1 == o || EOF == o)
break;

switch (o) {
case 'p':
conf.iconfigfile = realpath (optarg, NULL);
if (conf.iconfigfile == NULL)
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__,
strerror (errno));
conf.iconfigfile = optarg;
break;
case 0:
if (!strcmp ("no-global-config", long_opts[idx].name))
conf.load_global_config = 0;
break;
}
}
Expand Down Expand Up @@ -218,10 +221,7 @@ read_option_args (int argc, char **argv)
strerror (errno));
break;
case 'p':
conf.iconfigfile = realpath (optarg, NULL);
if (conf.iconfigfile == NULL)
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__,
strerror (errno));
/* ignore it */
break;
#ifdef HAVE_LIBGEOIP
case 'g':
Expand Down Expand Up @@ -266,6 +266,9 @@ read_option_args (int argc, char **argv)
break;
case 0:

if (!strcmp ("no-global-config", long_opts[idx].name))
break; /* ignore it */

if (!strcmp ("color-scheme", long_opts[idx].name))
conf.color_scheme = atoi (optarg);
if (!strcmp ("log-format", long_opts[idx].name))
Expand Down
4 changes: 2 additions & 2 deletions options.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#ifndef OPTIONS_H_INCLUDED
#define OPTIONS_H_INCLUDED

void read_conf_file_arg (int argc, char **argv);
void read_option_args (int argc, char **argv);
void cmd_help (void);
void read_option_args (int argc, char **argv);
void verify_global_config (int argc, char **argv);

#endif
29 changes: 22 additions & 7 deletions settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "settings.h"

Expand Down Expand Up @@ -55,6 +56,24 @@ static const GPreConfDate dates = {
};
/* *INDENT-ON* */

static char *
get_config_file_path (void)
{
char *path = NULL;

/* determine which config file to open, default or custom */
if (conf.iconfigfile != NULL) {
path = realpath (conf.iconfigfile, NULL);
if (path == NULL)
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__, strerror (errno));
} else if (conf.load_global_config)
path = get_global_config ();
else
path = get_home ();

return path;
}

/* clean command line arguments */
void
free_cmd_args (void)
Expand Down Expand Up @@ -96,18 +115,14 @@ parse_conf_file (int *argc, char ***argv)
append_to_argv (&nargc, &nargv, xstrdup ((char *) (*argv)[i]));

/* determine which config file to open, default or custom */
if (conf.iconfigfile != NULL)
path = alloc_string (conf.iconfigfile);
else
path = get_home ();

path = get_config_file_path ();
if (path == NULL)
return 1;
return ENOENT;

/* could not open conf file, if so prompt conf dialog */
if ((file = fopen (path, "r")) == NULL) {
free (path);
return 1;
return ENOENT;
}

while (fgets (line, sizeof line, file) != NULL) {
Expand Down
1 change: 1 addition & 0 deletions settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ typedef struct GConf_
int load_conf_dlg;
int mouse_support;
int no_color;
int load_global_config;
int no_progress;
int output_html;
int real_os;
Expand Down
11 changes: 11 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ get_home (void)
return path;
}

char *
get_global_config (void)
{
char *path = NULL;

path = xmalloc (snprintf (NULL, 0, "%s/goaccess.conf", SYSCONFDIR) + 1);
sprintf (path, "%s/goaccess.conf", SYSCONFDIR);

return path;
}

#pragma GCC diagnostic ignored "-Wformat-nonliteral"
char *
convert_date (char *result, char *data, const char *from, const char *to,
Expand Down
1 change: 1 addition & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ char *deblank (char *str);
char *escape_str (const char *src);
char *filesize_str (unsigned long long log_size);
char *float_to_str (float num);
char *get_global_config (void);
char *get_home (void);
char *int_to_str (int d);
char *left_pad_str (const char *s, int indent);
Expand Down

0 comments on commit c906f52

Please sign in to comment.