From 1ddb6a88b9dbdbee2bbf53ca1b89f39d049086d3 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Sun, 14 Dec 2008 14:36:44 +0100 Subject: [PATCH] use the builtin config also as a default one This depends on fopencookie, which is linux-specific. For BSD, there is a similar function called funopen, which can be used as a drop-in replacement. --- configure.ac.in | 1 + src/Makefile.am | 5 +++++ src/conf_cookie.c | 33 +++++++++++++++++++++++++++++++++ src/conf_cookie.h | 8 ++++++++ src/conky.c | 20 +++++++++++++++++++- text2c.sh | 24 ++++++++++++------------ 6 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 src/conf_cookie.c create mode 100644 src/conf_cookie.h diff --git a/configure.ac.in b/configure.ac.in index 79536f92..c850260a 100644 --- a/configure.ac.in +++ b/configure.ac.in @@ -101,6 +101,7 @@ AC_ARG_ENABLE([config_output], AM_CONDITIONAL(BUILD_CONFIG_OUTPUT, test x$want_config_output = xyes) if test x$want_config_output = xyes; then AC_DEFINE(CONFIG_OUTPUT, 1, [Define if you want conky to output a default config (with -C)]) + AC_CHECK_FUNCS(fopencookie, AM_CONDITIONAL(BUILD_FOR_FOPENCOOKIE, true)) fi dnl diff --git a/src/Makefile.am b/src/Makefile.am index 6ee84484..ec39cb5a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -29,6 +29,9 @@ bin_PROGRAMS = conky if BUILD_CONFIG_OUTPUT config_output = defconfig.h +if BUILD_FOR_FOPENCOOKIE +config_cookie = conf_cookie.c conf_cookie.h +endif conky_DEPENDENCIES = $(config_output) BUILT_SOURCES = $(config_output) CLEANFILES = $(config_output) @@ -110,6 +113,7 @@ endif conky_SOURCES = \ $(config_output) \ + $(config_cookie) \ $(audacious) \ $(bmpx) \ common.c \ @@ -146,6 +150,7 @@ conky_LDFLAGS = \ EXTRA_DIST = \ $(config_output) \ + $(config_cookie) \ audacious.c \ audacious.h \ bmpx.c \ diff --git a/src/conf_cookie.c b/src/conf_cookie.c new file mode 100644 index 00000000..65cbadca --- /dev/null +++ b/src/conf_cookie.c @@ -0,0 +1,33 @@ +#define _GNU_SOURCE +#include +#include +#include "defconfig.h" + +ssize_t conf_read(void *cookie, char *buf, size_t size) +{ + static int col = 0, row = 0; + size_t i = 0; + const char *conf[] = defconfig; + + (void)cookie; + + while (i < size) { + if (!(conf[row])) /* end of rows */ + break; + if (!(conf[row][col])) { /* end of line */ + row++; + col = 0; + continue; + } + buf[i++] = conf[row][col++]; + } + return i; +} + +cookie_io_functions_t conf_cookie = { + .read = &conf_read, + .write = NULL, + .seek = NULL, + .close = NULL, +}; + diff --git a/src/conf_cookie.h b/src/conf_cookie.h new file mode 100644 index 00000000..a5a8fea2 --- /dev/null +++ b/src/conf_cookie.h @@ -0,0 +1,8 @@ +#ifndef _CONF_COOKIE_H +#define _CONF_COOKIE_H + +#include + +extern cookie_io_functions_t conf_cookie; + +#endif /* _CONF_COOKIE_H */ diff --git a/src/conky.c b/src/conky.c index b2ef1ac0..e6591970 100644 --- a/src/conky.c +++ b/src/conky.c @@ -60,6 +60,9 @@ #ifdef CONFIG_OUTPUT #include "defconfig.h" +#ifdef HAVE_FOPENCOOKIE +#include "conf_cookie.h" +#endif #endif #include "build.h" @@ -7761,10 +7764,19 @@ static void load_config_file(const char *f) FILE *fp; set_default_configurations(); - fp = fopen(f, "r"); +#ifdef CONFIG_OUTPUT + if (!strcmp(f, "==builtin==")) { +#ifdef HAVE_FOPENCOOKIE + fp = fopencookie(NULL, "r", conf_cookie); +#endif + } else +#endif /* CONFIG_OUTPUT */ + fp = fopen(f, "r"); + if (!fp) { return; } + DBGP("reading contents from config file '%s'", f); while (!feof(fp)) { char buf[256], *p, *p2, *name, *value; @@ -8649,7 +8661,13 @@ int main(int argc, char **argv) /* No readable config found */ if (!current_config) { +#ifdef CONFIG_OUTPUT + current_config = strdup("==builtin=="); + ERR("no readable personal or system-wide config file found," + " using builtin default"); +#else CRIT_ERR("no readable personal or system-wide config file found"); +#endif /* ! CONF_OUTPUT */ } } diff --git a/text2c.sh b/text2c.sh index 25e76942..1fd9afeb 100644 --- a/text2c.sh +++ b/text2c.sh @@ -23,9 +23,9 @@ # $2: output file # $3: name of variable # -# The output will be a char **, with each field containing a single line of $1 -# additionally, a macro with the name print_$3 will be defined, with acts as -# a parameter-less function, printing the text to stdout +# The output will be a char **, with each field containing a single line of $1. +# Additionally, a macro with the name print_$3 will be defined, with acts as +# a parameter-less function, printing the text to stdout. [ $# -eq 3 ] || { echo "Usage: `basename $0` " @@ -37,14 +37,14 @@ outupper="`basename "$2" | tr '[a-z-.]' '[A-Z__]'`" ( printf "#ifndef __%s\n" "$outupper" printf "#define __%s\n" "$outupper" - printf "const char *%s[] = {\n" "$3" - sed -e 's/"/\\"/g' -e 's/^/"/' -e 's/$/",/' $1 - printf "NULL };\n" - printf "#define print_%s() { \\" $3 - printf "\n\tconst char **__s = %s; \\" $3 - printf "\n\tfor (; *__s; __s++) \\" - printf "\n\t\tputs(*__s); \\" - printf "\n}\n" + printf "\n#define %s { \\" $3 + printf "\n" + sed -e 's/"/\\"/g' -e 's/^/"/' -e 's/$/\\n", \\/' $1 + printf "NULL }\n" + printf "\n#define print_%s() { \\" $3 + printf "\n\tconst char **__sp, *__s[] = %s; \\" $3 + printf "\n\tfor (__sp = __s; *__sp; __sp++) \\" + printf "\n\t\tprintf(\"%s\", *__sp); \\" "%s" + printf "\n}\n\n" printf "#endif /* __%s */\n" "$outupper" ) > $2 -