1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 02:25:09 +00:00

do not use a global ifblock stack

Using a global ifblock stack for all parsed ifblock objects causes
problems when doing sub-parsing in objects taking other objects as
parameters, because the possibly non-empty stack at startup leads to
false alarm when checking for stack emptiness after parsing the objects.

Use a void ** as the object to pass around, so callers don't need to
know struct ifblock_stack_obj.
This commit is contained in:
Phil Sutter 2008-12-22 17:45:08 +01:00
parent 05624868e3
commit da7bc63996
3 changed files with 49 additions and 46 deletions

View File

@ -1573,7 +1573,7 @@ static const char *dev_name(const char *path)
/* construct_text_object() creates a new text_object */ /* construct_text_object() creates a new text_object */
static struct text_object *construct_text_object(const char *s, static struct text_object *construct_text_object(const char *s,
const char *arg, long line, char allow_threaded) const char *arg, long line, char allow_threaded, void **ifblock_opaque)
{ {
// struct text_object *obj = new_text_object(); // struct text_object *obj = new_text_object();
struct text_object *obj = new_text_object_internal(); struct text_object *obj = new_text_object_internal();
@ -1775,9 +1775,9 @@ static struct text_object *construct_text_object(const char *s,
obj->data.ifblock.s = 0; obj->data.ifblock.s = 0;
} else } else
obj->data.ifblock.s = strndup(arg, text_buffer_size); obj->data.ifblock.s = strndup(arg, text_buffer_size);
obj_be_ifblock_if(obj); obj_be_ifblock_if(ifblock_opaque, obj);
END OBJ(if_gw, 0) END OBJ(if_gw, 0)
obj_be_ifblock_if(obj); obj_be_ifblock_if(ifblock_opaque, obj);
END OBJ(ioscheduler, 0) END OBJ(ioscheduler, 0)
if (!arg) { if (!arg) {
CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)"); CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
@ -1966,9 +1966,9 @@ static struct text_object *construct_text_object(const char *s,
obj->data.net = get_net_stat(buf); obj->data.net = get_net_stat(buf);
free(buf); free(buf);
END OBJ(else, 0) END OBJ(else, 0)
obj_be_ifblock_else(obj); obj_be_ifblock_else(ifblock_opaque, obj);
END OBJ(endif, 0) END OBJ(endif, 0)
obj_be_ifblock_endif(obj); obj_be_ifblock_endif(ifblock_opaque, obj);
END OBJ(image, 0) END OBJ(image, 0)
obj->data.s = strndup(arg ? arg : "", text_buffer_size); obj->data.s = strndup(arg ? arg : "", text_buffer_size);
#ifdef HAVE_POPEN #ifdef HAVE_POPEN
@ -2518,7 +2518,7 @@ static struct text_object *construct_text_object(const char *s,
} else { } else {
obj->data.ifblock.s = strndup(arg, text_buffer_size); obj->data.ifblock.s = strndup(arg, text_buffer_size);
} }
obj_be_ifblock_if(obj); obj_be_ifblock_if(ifblock_opaque, obj);
END OBJ(if_match, 0) END OBJ(if_match, 0)
if (!arg) { if (!arg) {
ERR("if_match needs arguments"); ERR("if_match needs arguments");
@ -2526,7 +2526,7 @@ static struct text_object *construct_text_object(const char *s,
} else { } else {
obj->data.ifblock.s = strndup(arg, text_buffer_size); obj->data.ifblock.s = strndup(arg, text_buffer_size);
} }
obj_be_ifblock_if(obj); obj_be_ifblock_if(ifblock_opaque, obj);
END OBJ(if_existing, 0) END OBJ(if_existing, 0)
if (!arg) { if (!arg) {
ERR("if_existing needs an argument or two"); ERR("if_existing needs an argument or two");
@ -2545,7 +2545,7 @@ static struct text_object *construct_text_object(const char *s,
} }
} }
DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str); DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
obj_be_ifblock_if(obj); obj_be_ifblock_if(ifblock_opaque, obj);
END OBJ(if_mounted, 0) END OBJ(if_mounted, 0)
if (!arg) { if (!arg) {
ERR("if_mounted needs an argument"); ERR("if_mounted needs an argument");
@ -2553,7 +2553,7 @@ static struct text_object *construct_text_object(const char *s,
} else { } else {
obj->data.ifblock.s = strndup(arg, text_buffer_size); obj->data.ifblock.s = strndup(arg, text_buffer_size);
} }
obj_be_ifblock_if(obj); obj_be_ifblock_if(ifblock_opaque, obj);
END OBJ(if_running, 0) END OBJ(if_running, 0)
if (arg) { if (arg) {
char buf[256]; char buf[256];
@ -2564,7 +2564,7 @@ static struct text_object *construct_text_object(const char *s,
ERR("if_running needs an argument"); ERR("if_running needs an argument");
obj->data.ifblock.s = 0; obj->data.ifblock.s = 0;
} }
obj_be_ifblock_if(obj); obj_be_ifblock_if(ifblock_opaque, obj);
END OBJ(kernel, 0) END OBJ(kernel, 0)
END OBJ(machine, 0) END OBJ(machine, 0)
END OBJ(mails, 0) END OBJ(mails, 0)
@ -2847,7 +2847,7 @@ static struct text_object *construct_text_object(const char *s,
obj->data.ifblock.s = 0; obj->data.ifblock.s = 0;
} else } else
obj->data.ifblock.s = strndup(arg, text_buffer_size); obj->data.ifblock.s = strndup(arg, text_buffer_size);
obj_be_ifblock_if(obj); obj_be_ifblock_if(ifblock_opaque, obj);
END OBJ(smapi_bat_perc, 0) END OBJ(smapi_bat_perc, 0)
if (arg) if (arg)
obj->data.s = strndup(arg, text_buffer_size); obj->data.s = strndup(arg, text_buffer_size);
@ -2919,7 +2919,7 @@ static struct text_object *construct_text_object(const char *s,
mpd_set_maxlen(mpd_smart); mpd_set_maxlen(mpd_smart);
init_mpd(); init_mpd();
END OBJ(if_mpd_playing, INFO_MPD) END OBJ(if_mpd_playing, INFO_MPD)
obj_be_ifblock_if(obj); obj_be_ifblock_if(ifblock_opaque, obj);
init_mpd(); init_mpd();
#undef mpd_set_maxlen #undef mpd_set_maxlen
#endif /* MPD */ #endif /* MPD */
@ -2958,7 +2958,7 @@ static struct text_object *construct_text_object(const char *s,
END OBJ(xmms2_playlist, INFO_XMMS2) END OBJ(xmms2_playlist, INFO_XMMS2)
END OBJ(xmms2_timesplayed, INFO_XMMS2) END OBJ(xmms2_timesplayed, INFO_XMMS2)
END OBJ(if_xmms2_connected, INFO_XMMS2) END OBJ(if_xmms2_connected, INFO_XMMS2)
obj_be_ifblock_if(obj); obj_be_ifblock_if(ifblock_opaque, obj);
#endif #endif
#ifdef AUDACIOUS #ifdef AUDACIOUS
END OBJ(audacious_status, INFO_AUDACIOUS) END OBJ(audacious_status, INFO_AUDACIOUS)
@ -3288,6 +3288,7 @@ static int extract_variable_text_internal(struct text_object *retval, const char
struct text_object *obj; struct text_object *obj;
char *p, *s, *orig_p; char *p, *s, *orig_p;
long line; long line;
void *ifblock_opaque = NULL;
p = strndup(const_p, max_user_text - 1); p = strndup(const_p, max_user_text - 1);
while (text_contains_templates(p)) { while (text_contains_templates(p)) {
@ -3390,7 +3391,9 @@ static int extract_variable_text_internal(struct text_object *retval, const char
tmp_p++; tmp_p++;
} }
obj = construct_text_object(buf, arg, line, allow_threaded); obj = construct_text_object(buf, arg,
line, allow_threaded,
&ifblock_opaque);
if (obj != NULL) { if (obj != NULL) {
append_object(retval, obj); append_object(retval, obj);
} }
@ -3410,7 +3413,7 @@ static int extract_variable_text_internal(struct text_object *retval, const char
append_object(retval, obj); append_object(retval, obj);
} }
if (!ifblock_stack_empty()) { if (!ifblock_stack_empty(&ifblock_opaque)) {
ERR("one or more $endif's are missing"); ERR("one or more $endif's are missing");
} }

View File

@ -85,12 +85,6 @@ struct ifblock_stack_obj {
struct ifblock_stack_obj *next; struct ifblock_stack_obj *next;
}; };
/* top of the internal ifblock stack
* initially contains only one "object", i.e. a NULL pointer
* indicating the end of the stack.
*/
static struct ifblock_stack_obj *ifblock_stack_top = NULL;
/* push an ifblock object onto the stack /* push an ifblock object onto the stack
* in fact, this does a lot more: * in fact, this does a lot more:
* - IFBLOCK_IF is just pushed onto the stack * - IFBLOCK_IF is just pushed onto the stack
@ -100,37 +94,38 @@ static struct ifblock_stack_obj *ifblock_stack_top = NULL;
* object in the stack and then triggers stack popping of * object in the stack and then triggers stack popping of
* any optional IFBLOCK_ELSE along with it's IFBLOCK_IF * any optional IFBLOCK_ELSE along with it's IFBLOCK_IF
*/ */
static int push_ifblock(struct text_object *obj, enum ifblock_type type) static int push_ifblock(struct ifblock_stack_obj **ifblock_stack_top,
struct text_object *obj, enum ifblock_type type)
{ {
struct ifblock_stack_obj *stackobj; struct ifblock_stack_obj *stackobj;
switch (type) { switch (type) {
case IFBLOCK_ENDIF: case IFBLOCK_ENDIF:
if (!ifblock_stack_top) if (!(*ifblock_stack_top))
CRIT_ERR("got an endif without matching if"); CRIT_ERR("got an endif without matching if");
ifblock_stack_top->obj->data.ifblock.next = obj; (*ifblock_stack_top)->obj->data.ifblock.next = obj;
/* if there's some else in between, remove and free it */ /* if there's some else in between, remove and free it */
if (ifblock_stack_top->type == IFBLOCK_ELSE) { if ((*ifblock_stack_top)->type == IFBLOCK_ELSE) {
stackobj = ifblock_stack_top; stackobj = *ifblock_stack_top;
ifblock_stack_top = stackobj->next; *ifblock_stack_top = stackobj->next;
free(stackobj); free(stackobj);
} }
/* finally remove and free the if object */ /* finally remove and free the if object */
stackobj = ifblock_stack_top; stackobj = *ifblock_stack_top;
ifblock_stack_top = stackobj->next; *ifblock_stack_top = stackobj->next;
free(stackobj); free(stackobj);
break; break;
case IFBLOCK_ELSE: case IFBLOCK_ELSE:
if (!ifblock_stack_top) if (!(*ifblock_stack_top))
CRIT_ERR("got an else without matching if"); CRIT_ERR("got an else without matching if");
ifblock_stack_top->obj->data.ifblock.next = obj; (*ifblock_stack_top)->obj->data.ifblock.next = obj;
/* fall through */ /* fall through */
case IFBLOCK_IF: case IFBLOCK_IF:
stackobj = malloc(sizeof(struct ifblock_stack_obj)); stackobj = malloc(sizeof(struct ifblock_stack_obj));
stackobj->type = type; stackobj->type = type;
stackobj->obj = obj; stackobj->obj = obj;
stackobj->next = ifblock_stack_top; stackobj->next = *ifblock_stack_top;
ifblock_stack_top = stackobj; *ifblock_stack_top = stackobj;
break; break;
default: default:
CRIT_ERR("push_ifblock() missuse detected!"); CRIT_ERR("push_ifblock() missuse detected!");
@ -140,23 +135,23 @@ static int push_ifblock(struct text_object *obj, enum ifblock_type type)
/* public functions for client use */ /* public functions for client use */
int obj_be_ifblock_if(struct text_object *obj) int obj_be_ifblock_if(void **opaque, struct text_object *obj)
{ {
return push_ifblock(obj, IFBLOCK_IF); return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_IF);
} }
int obj_be_ifblock_else(struct text_object *obj) int obj_be_ifblock_else(void **opaque, struct text_object *obj)
{ {
return push_ifblock(obj, IFBLOCK_ELSE); return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ELSE);
} }
int obj_be_ifblock_endif(struct text_object *obj) int obj_be_ifblock_endif(void **opaque, struct text_object *obj)
{ {
return push_ifblock(obj, IFBLOCK_ENDIF); return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ENDIF);
} }
/* check if ifblock stack is empty /* check if ifblock stack is empty
* if so, return true (!= 0) * if so, return true (!= 0)
*/ */
int ifblock_stack_empty(void) int ifblock_stack_empty(void **opaque)
{ {
return ifblock_stack_top == NULL; return *opaque == NULL;
} }

View File

@ -487,10 +487,15 @@ struct text_object {
/* text object list helpers */ /* text object list helpers */
int append_object(struct text_object *root, struct text_object *obj); int append_object(struct text_object *root, struct text_object *obj);
/* ifblock helpers */ /* ifblock helpers
int obj_be_ifblock_if(struct text_object *); *
int obj_be_ifblock_else(struct text_object *); * Opaque is a pointer to the address of the ifblock stack's top object.
int obj_be_ifblock_endif(struct text_object *); * Calling clients should pass the address of a defined void pointer which
int ifblock_stack_empty(void); * was initialised to NULL (empty stack).
* */
int obj_be_ifblock_if(void **opaque, struct text_object *);
int obj_be_ifblock_else(void **opaque, struct text_object *);
int obj_be_ifblock_endif(void **opaque, struct text_object *);
int ifblock_stack_empty(void **opaque);
#endif /* _TEXT_OBJECT_H */ #endif /* _TEXT_OBJECT_H */