From b60a508177f720fc7b27bd34fa68862269b7aba4 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Mon, 4 Jan 2010 08:42:01 +0000 Subject: [PATCH] Add --config option to parse an additional config file --- ChangeLog | 5 +++++ config.h.in | 3 +++ src/config_file.c | 9 ++++++--- src/xcowsay.c | 8 ++++++++ xcowsay.6 | 5 +++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 10c9da9..6433a4c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -9,6 +9,11 @@ to be placed in $XDG_CONFIG_HOME/xcowsayrc as well as $HOME/.xcowsayrc. + * src/xcowsay.c (main): Add --config option to specify and + additional config file to parse. + * src/config_file.c (config_file_name): Support parsing the + additional config file. + 2010-01-03 Nick Gasson * src/xcowsay.c (main): Add --at option to specify cow location diff --git a/config.h.in b/config.h.in index 1703ee6..15d82ce 100644 --- a/config.h.in +++ b/config.h.in @@ -59,6 +59,9 @@ /* Define to 1 if you have the `strcasecmp' function. */ #undef HAVE_STRCASECMP +/* Define to 1 if you have the `strchr' function. */ +#undef HAVE_STRCHR + /* Define to 1 if you have the `strdup' function. */ #undef HAVE_STRDUP diff --git a/src/config_file.c b/src/config_file.c index e8777a9..3e9cd7b 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -147,13 +147,16 @@ static bool is_bool_option(const char *s, bool *bval) static char *config_file_name(void) { - // There are two possible locations for the config file: + // There are three possible locations for the config file: + // - Specifed on the command line with --config // - $XDG_CONFIG_HOME/xcowsayrc // - $HOME/.xcowsayrc - // We prefer the XDG compliant one + // We prefer them in the above order // Need to free the result of this function - char *fname = NULL; + char *fname = get_string_option("alt_config_file"); + if (*fname) + return strdup(fname); // We always free the result const char *home = getenv("HOME"); if (NULL == home) diff --git a/src/xcowsay.c b/src/xcowsay.c index ae05866..7d57425 100644 --- a/src/xcowsay.c +++ b/src/xcowsay.c @@ -66,6 +66,7 @@ static struct option long_options[] = { {"monitor", required_argument, 0, 'm'}, {"bubble-at", required_argument, 0, 'b'}, {"at", required_argument, 0, 'a'}, + {"config", required_argument, 0, 'o'}, {"debug", no_argument, &debug, 1}, {0, 0, 0, 0} }; @@ -103,6 +104,7 @@ static void usage() " --monitor=N\t%s\n" " --at=X,Y\t\t%s\n" " --bubble-at=X,Y\t%s\n" + " --config=FILE\t%s\n" " --debug\t\t%s\n\n" "%s\n\n" "%s\n\n" @@ -123,6 +125,7 @@ static void usage() i18n("Display cow on monitor N."), i18n("Force the cow to appear at screen location (X,Y)."), i18n("Change relative position of bubble."), + i18n("Specify alternative config file."), i18n("Keep daemon attached to terminal."), i18n("Default values for these options can be specified in the " "xcowsay config\nfile. See the man page for more information."), @@ -230,6 +233,7 @@ int main(int argc, char **argv) add_int_option("at_y", -1); add_int_option("bubble_x", DEF_BUBBLE_X); add_int_option("bubble_y", 0); + add_string_option("alt_config_file", ""); parse_config_file(); @@ -284,6 +288,10 @@ int main(int argc, char **argv) set_int_option("bubble_y", y); } break; + case 'o': + set_string_option("alt_config_file", optarg); + parse_config_file(); + break; case '?': // getopt_long already printed an error message failure = 1; diff --git a/xcowsay.6 b/xcowsay.6 index cb395ae..d3d0e1c 100644 --- a/xcowsay.6 +++ b/xcowsay.6 @@ -53,6 +53,11 @@ can be stored in the XDG compliant location .IR $HOME/.config/xcowsayrc ) or in your home directory at .IR $HOME/.xcowsayrc . +If the +.BI --config= FILE +command line option is passed +.I FILE +will be processed after your personal configuration file. The configuration file consists of 'option = value' pairs, one per line. The valid keys are given in the next section. For example, the following line sets -- 2.39.2