1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-05 21:07:52 +00:00

Allow the use of '#' for comments within text area (can be escaped with '\#'.

Also refactored some code relating to the config file loading (since
there was some duplication of code).
This commit is contained in:
Brenden Matthews 2009-05-09 20:10:55 -06:00
parent 9dc5411681
commit 24f97c45c6
2 changed files with 113 additions and 125 deletions

View File

@ -1,3 +1,7 @@
2009-05-09
* Allow the use of '#' for comments within text area (can be escaped with
'\#'
2009-05-07 2009-05-07
* Fix occasional cpubar segfaults * Fix occasional cpubar segfaults
* Added top_name_width config option * Added top_name_width config option

View File

@ -2595,6 +2595,12 @@ static struct text_object *create_plain_text(const char *s)
{ {
struct text_object *obj; struct text_object *obj;
char *esc = strstr(s, "\\#");
if (esc) {
/* remove extra '\' */
strcpy(esc, esc + 1);
}
if (s == NULL || *s == '\0') { if (s == NULL || *s == '\0') {
return NULL; return NULL;
} }
@ -2816,6 +2822,12 @@ static int extract_variable_text_internal(struct text_object *retval, const char
if (*p == '\n') { if (*p == '\n') {
line++; line++;
} }
/* handle comments within the TEXT area */
if (*p == '#' && (p == orig_p || *(p - 1) != '\\')) {
while (*p && *p != '\n') p++;
s = p;
line++;
}
if (*p == '$') { if (*p == '$') {
*p = '\0'; *p = '\0';
obj = create_plain_text(s); obj = create_plain_text(s);
@ -6668,41 +6680,41 @@ static void X11_initialisation(void)
} }
#endif /* X11 */ #endif /* X11 */
static void load_config_file(const char *f)
{
#define CONF_ERR ERR("%s: %d: config file error", f, line) #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) #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
int line = 0; #define CONF2(a) if (strcasecmp(name, a) == 0)
FILE *fp; #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 #ifdef CONFIG_OUTPUT
if (!strcmp(f, "==builtin==")) { if (!strcmp(f, "==builtin==")) {
#ifdef HAVE_FOPENCOOKIE #ifdef HAVE_FOPENCOOKIE
fp = fopencookie(NULL, "r", conf_cookie); return fopencookie(NULL, "r", conf_cookie);
#endif #endif /* HAVE_FOPENCOOKIE */
} else } else
#endif /* CONFIG_OUTPUT */ #endif /* CONFIG_OUTPUT */
fp = fopen(f, "r"); return fopen(f, "r");
if (!fp) {
return;
} }
DBGP("reading contents from config file '%s'", f);
while (!feof(fp)) { static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
char buf[256], *p, *p2, *name, *value; {
char *p, *p2;
line++; (*line)++;
if (fgets(buf, 256, fp) == NULL) { if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
break; return CONF_BREAK;
} }
p = buf; p = buf;
/* break at comment */ /* break at comment, unless preceeded by \ */
p2 = strchr(p, '#'); p2 = strchr(p, '#');
if (p2) { if (p2 && (p2 == p || *(p2 - 1) != '\\')) {
*p2 = '\0'; *p2 = '\0';
} }
@ -6711,10 +6723,10 @@ static void load_config_file(const char *f)
p++; p++;
} }
if (*p == '\0') { if (*p == '\0') {
continue; /* empty line */ return CONF_CONTINUE; /* empty line */
} }
name = p; *name = p;
/* skip name */ /* skip name */
p2 = p; p2 = p;
@ -6733,20 +6745,38 @@ static void load_config_file(const char *f)
p++; p++;
} }
value = p; *value = p;
p2 = value + strlen(value); p2 = *value + strlen(*value);
while (isspace((int) *(p2 - 1))) { while (isspace((int) *(p2 - 1))) {
*--p2 = '\0'; *--p2 = '\0';
} }
} else { } else {
value = 0; *value = 0;
}
return 0;
} }
#define CONF2(a) if (strcasecmp(name, a) == 0) static void load_config_file(const char *f)
#define CONF(a) else CONF2(a) {
#define CONF3(a, b) else if (strcasecmp(name, a) == 0 \ int line = 0;
|| strcasecmp(name, b) == 0) FILE *fp;
set_default_configurations();
fp = open_config_file(f);
if (!fp) {
return;
}
DBGP("reading contents from config file '%s'", f);
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;
}
#ifdef X11 #ifdef X11
CONF2("out_to_x") { CONF2("out_to_x") {
@ -7305,8 +7335,9 @@ static void load_config_file(const char *f)
while (!feof(fp)) { while (!feof(fp)) {
unsigned int l = strlen(global_text); unsigned int l = strlen(global_text);
unsigned int bl; unsigned int bl;
char buf[CONF_BUFF_SIZE];
if (fgets(buf, 256, fp) == NULL) { if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
break; break;
} }
@ -7435,71 +7466,19 @@ static void load_config_file_x11(const char *f)
int line = 0; int line = 0;
FILE *fp; FILE *fp;
#ifdef CONFIG_OUTPUT fp = open_config_file(f);
if (!strcmp(f, "==builtin==")) {
#ifdef HAVE_FOPENCOOKIE
fp = fopencookie(NULL, "r", conf_cookie);
#endif
} else
#endif /* CONFIG_OUTPUT */
fp = fopen(f, "r");
if (!fp) { if (!fp) {
return; return;
} }
DBGP("reading contents from config file '%s'", f); DBGP("reading contents from config file '%s'", f);
while (!feof(fp)) { while (!feof(fp)) {
char buf[256], *p, *p2, *name, *value; char buff[CONF_BUFF_SIZE], *name, *value;
int ret = do_config_step(&line, fp, buff, &name, &value);
line++; if (ret == CONF_BREAK) {
if (fgets(buf, 256, fp) == NULL) {
break; break;
} } else if (ret == CONF_CONTINUE) {
continue;
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;
} }
#ifdef X11 #ifdef X11
@ -7655,11 +7634,16 @@ static void load_config_file_x11(const char *f)
#endif /* X11 */ #endif /* X11 */
#undef CONF #undef CONF
#undef CONF2 #undef CONF2
#undef CONF3
#undef CONF_ERR
#undef CONF_ERR2
#undef CONF_BREAK
#undef CONF_CONTINUE
#undef CONF_BUFF_SIZE
} }
fclose(fp); fclose(fp);
#undef CONF_ERR
} }
static void print_help(const char *prog_name) { static void print_help(const char *prog_name) {