Allow the use of '#' for comments within text area (can be escaped with '\#'.
authorBrenden Matthews <brenden@rty.ca>
Sun, 10 May 2009 02:10:55 +0000 (20:10 -0600)
committerBrenden Matthews <brenden@rty.ca>
Sun, 10 May 2009 02:10:55 +0000 (20:10 -0600)
Also refactored some code relating to the config file loading (since
there was some duplication of code).

ChangeLog
src/conky.c

index 259572c..3a935b1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2009-05-09
+       * Allow the use of '#' for comments within text area (can be escaped with
+       '\#'
+
 2009-05-07
        * Fix occasional cpubar segfaults
        * Added top_name_width config option
index 524f2b3..6adf2af 100644 (file)
@@ -2595,6 +2595,12 @@ static struct text_object *create_plain_text(const char *s)
 {
        struct text_object *obj;
 
+       char *esc = strstr(s, "\\#");
+       if (esc) {
+               /* remove extra '\' */
+               strcpy(esc, esc + 1);
+       }
+
        if (s == NULL || *s == '\0') {
                return NULL;
        }
@@ -2816,6 +2822,12 @@ static int extract_variable_text_internal(struct text_object *retval, const char
                if (*p == '\n') {
                        line++;
                }
+               /* handle comments within the TEXT area */
+               if (*p == '#' && (p == orig_p || *(p - 1) != '\\')) {
+                       while (*p && *p != '\n') p++;
+                       s = p;
+                       line++;
+               }
                if (*p == '$') {
                        *p = '\0';
                        obj = create_plain_text(s);
@@ -6668,86 +6680,104 @@ static void X11_initialisation(void)
 }
 #endif /* X11 */
 
-static void load_config_file(const char *f)
-{
 #define CONF_ERR ERR("%s: %d: config file error", f, line)
 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
-       int line = 0;
-       FILE *fp;
+#define CONF2(a) if (strcasecmp(name, a) == 0)
+#define CONF(a) else CONF2(a)
+#define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
+               || strcasecmp(name, b) == 0)
+#define CONF_CONTINUE 1
+#define CONF_BREAK 2
+#define CONF_BUFF_SIZE 512
 
-       set_default_configurations();
+static FILE *open_config_file(const char *f)
+{
 #ifdef CONFIG_OUTPUT
        if (!strcmp(f, "==builtin==")) {
 #ifdef HAVE_FOPENCOOKIE
-               fp = fopencookie(NULL, "r", conf_cookie);
-#endif
+               return fopencookie(NULL, "r", conf_cookie);
+#endif /* HAVE_FOPENCOOKIE */
        } else
 #endif /* CONFIG_OUTPUT */
-               fp = fopen(f, "r");
+               return fopen(f, "r");
+}
 
-       if (!fp) {
-               return;
+static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
+{
+       char *p, *p2;
+       (*line)++;
+       if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
+               return CONF_BREAK;
        }
-       DBGP("reading contents from config file '%s'", f);
 
-       while (!feof(fp)) {
-               char buf[256], *p, *p2, *name, *value;
+       p = buf;
 
-               line++;
-               if (fgets(buf, 256, fp) == NULL) {
-                       break;
-               }
+       /* break at comment, unless preceeded by \ */
+       p2 = strchr(p, '#');
+       if (p2 && (p2 == p || *(p2 - 1) != '\\')) {
+               *p2 = '\0';
+       }
+
+       /* skip spaces */
+       while (*p && isspace((int) *p)) {
+               p++;
+       }
+       if (*p == '\0') {
+               return CONF_CONTINUE;   /* empty line */
+       }
 
-               p = buf;
+       *name = p;
 
-               /* break at comment */
-               p2 = strchr(p, '#');
-               if (p2) {
-                       *p2 = '\0';
-               }
+       /* skip name */
+       p2 = p;
+       while (*p2 && !isspace((int) *p2)) {
+               p2++;
+       }
+       if (*p2 != '\0') {
+               *p2 = '\0';     /* break at name's end */
+               p2++;
+       }
 
-               /* skip spaces */
+       /* get value */
+       if (*p2) {
+               p = p2;
                while (*p && isspace((int) *p)) {
                        p++;
                }
-               if (*p == '\0') {
-                       continue;       /* empty line */
-               }
 
-               name = p;
+               *value = p;
 
-               /* skip name */
-               p2 = p;
-               while (*p2 && !isspace((int) *p2)) {
-                       p2++;
-               }
-               if (*p2 != '\0') {
-                       *p2 = '\0';     /* break at name's end */
-                       p2++;
+               p2 = *value + strlen(*value);
+               while (isspace((int) *(p2 - 1))) {
+                       *--p2 = '\0';
                }
+       } else {
+               *value = 0;
+       }
+       return 0;
+}
 
-               /* get value */
-               if (*p2) {
-                       p = p2;
-                       while (*p && isspace((int) *p)) {
-                               p++;
-                       }
+static void load_config_file(const char *f)
+{
+       int line = 0;
+       FILE *fp;
 
-                       value = p;
+       set_default_configurations();
+       fp = open_config_file(f);
+       if (!fp) {
+               return;
+       }
+       DBGP("reading contents from config file '%s'", f);
 
-                       p2 = value + strlen(value);
-                       while (isspace((int) *(p2 - 1))) {
-                               *--p2 = '\0';
-                       }
-               } else {
-                       value = 0;
+       while (!feof(fp)) {
+               char buff[CONF_BUFF_SIZE], *name, *value;
+               int ret = do_config_step(&line, fp, buff, &name, &value);
+               if (ret == CONF_BREAK) {
+                       break;
+               } else if (ret == CONF_CONTINUE) {
+                       continue;
                }
 
-#define CONF2(a) if (strcasecmp(name, a) == 0)
-#define CONF(a) else CONF2(a)
-#define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
-               || strcasecmp(name, b) == 0)
-
 #ifdef X11
                CONF2("out_to_x") {
                        /* don't listen if X is already initialised or
@@ -7305,8 +7335,9 @@ static void load_config_file(const char *f)
                        while (!feof(fp)) {
                                unsigned int l = strlen(global_text);
                                unsigned int bl;
+                               char buf[CONF_BUFF_SIZE];
 
-                               if (fgets(buf, 256, fp) == NULL) {
+                               if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
                                        break;
                                }
 
@@ -7435,71 +7466,19 @@ static void load_config_file_x11(const char *f)
        int line = 0;
        FILE *fp;
 
-#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");
-
+       fp = open_config_file(f);
        if (!fp) {
                return;
        }
        DBGP("reading contents from config file '%s'", f);
 
        while (!feof(fp)) {
-               char buf[256], *p, *p2, *name, *value;
-
-               line++;
-               if (fgets(buf, 256, fp) == NULL) {
+               char buff[CONF_BUFF_SIZE], *name, *value;
+               int ret = do_config_step(&line, fp, buff, &name, &value);
+               if (ret == CONF_BREAK) {
                        break;
-               }
-
-               p = buf;
-
-               /* break at comment */
-               p2 = strchr(p, '#');
-               if (p2) {
-                       *p2 = '\0';
-               }
-
-               /* skip spaces */
-               while (*p && isspace((int) *p)) {
-                       p++;
-               }
-               if (*p == '\0') {
-                       continue;       /* empty line */
-               }
-
-               name = p;
-
-               /* skip name */
-               p2 = p;
-               while (*p2 && !isspace((int) *p2)) {
-                       p2++;
-               }
-               if (*p2 != '\0') {
-                       *p2 = '\0';     /* break at name's end */
-                       p2++;
-               }
-
-               /* get value */
-               if (*p2) {
-                       p = p2;
-                       while (*p && isspace((int) *p)) {
-                               p++;
-                       }
-
-                       value = p;
-
-                       p2 = value + strlen(value);
-                       while (isspace((int) *(p2 - 1))) {
-                               *--p2 = '\0';
-                       }
-               } else {
-                       value = 0;
+               } else if (ret == CONF_CONTINUE) {
+                       continue;
                }
 
 #ifdef X11
@@ -7655,11 +7634,16 @@ static void load_config_file_x11(const char *f)
 #endif /* X11 */
 #undef CONF
 #undef CONF2
+#undef CONF3
+#undef CONF_ERR
+#undef CONF_ERR2
+#undef CONF_BREAK
+#undef CONF_CONTINUE
+#undef CONF_BUFF_SIZE
        }
 
        fclose(fp);
 
-#undef CONF_ERR
 }
 
 static void print_help(const char *prog_name) {