use the builtin config also as a default one
authorPhil Sutter <phil@nwl.cc>
Sun, 14 Dec 2008 13:36:44 +0000 (14:36 +0100)
committerPhil Sutter <phil@nwl.cc>
Sun, 14 Dec 2008 14:45:30 +0000 (15:45 +0100)
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
src/Makefile.am
src/conf_cookie.c [new file with mode: 0644]
src/conf_cookie.h [new file with mode: 0644]
src/conky.c
text2c.sh

index 79536f9..c850260 100644 (file)
@@ -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
index 6ee8448..ec39cb5 100644 (file)
@@ -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 (file)
index 0000000..65cbadc
--- /dev/null
@@ -0,0 +1,33 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/types.h>
+#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 (file)
index 0000000..a5a8fea
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef _CONF_COOKIE_H
+#define _CONF_COOKIE_H
+
+#include <stdio.h>
+
+extern cookie_io_functions_t conf_cookie;
+
+#endif /* _CONF_COOKIE_H */
index b2ef1ac..e659197 100644 (file)
@@ -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 */
                }
        }
 
index 25e7694..1fd9afe 100644 (file)
--- 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` <inputfile> <outputfile> <variablename>"
@@ -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
-