1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11 * All rights reserved.
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "text_object.h"
31 * this list is special. it looks like this:
32 * NULL <-- obj1 <--> obj2 <--> ... <--> objN --> NULL
33 * ^-------root_object----------^
34 * directions are reversed here
37 * - root_object points both to the start and end of the list
38 * - while traversing, the end of the list is always a NULL pointer
39 * (this works in BOTH directions)
42 /* append an object to the given root object's list */
43 int append_object(struct text_object *root, struct text_object *obj)
45 struct text_object *end;
53 CRIT_ERR("huston, we have a lift-off");
62 /* ifblock handlers for the object list
64 * - each if points to it's else or endif
65 * - each else points to it's endif
69 /* possible ifblock types
70 * only used internally, so no need to make this public
78 /* linked list of ifblock objects, building a stack
79 * only used internally, so no need to make this public
81 struct ifblock_stack_obj {
82 enum ifblock_type type;
83 struct text_object *obj;
84 struct ifblock_stack_obj *next;
87 /* push an ifblock object onto the stack
88 * in fact, this does a lot more:
89 * - IFBLOCK_IF is just pushed onto the stack
90 * - IFBLOCK_ELSE updates the "next" pointer of the upmost
91 * object in the stack and is then pushed onto the stack
92 * - IFBLOCK_ENDIF updates the "next" pointer of the upmost
93 * object in the stack and then triggers stack popping of
94 * any optional IFBLOCK_ELSE along with it's IFBLOCK_IF
96 static int push_ifblock(struct ifblock_stack_obj **ifblock_stack_top,
97 struct text_object *obj, enum ifblock_type type)
99 struct ifblock_stack_obj *stackobj;
103 if (!(*ifblock_stack_top))
104 CRIT_ERR("got an endif without matching if");
105 (*ifblock_stack_top)->obj->data.ifblock.next = obj;
106 /* if there's some else in between, remove and free it */
107 if ((*ifblock_stack_top)->type == IFBLOCK_ELSE) {
108 stackobj = *ifblock_stack_top;
109 *ifblock_stack_top = stackobj->next;
112 /* finally remove and free the if object */
113 stackobj = *ifblock_stack_top;
114 *ifblock_stack_top = stackobj->next;
118 if (!(*ifblock_stack_top))
119 CRIT_ERR("got an else without matching if");
120 (*ifblock_stack_top)->obj->data.ifblock.next = obj;
123 stackobj = malloc(sizeof(struct ifblock_stack_obj));
124 stackobj->type = type;
126 stackobj->next = *ifblock_stack_top;
127 *ifblock_stack_top = stackobj;
130 CRIT_ERR("push_ifblock() missuse detected!");
135 /* public functions for client use */
137 int obj_be_ifblock_if(void **opaque, struct text_object *obj)
139 return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_IF);
141 int obj_be_ifblock_else(void **opaque, struct text_object *obj)
143 return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ELSE);
145 int obj_be_ifblock_endif(void **opaque, struct text_object *obj)
147 return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ENDIF);
150 /* check if ifblock stack is empty
151 * if so, return true (!= 0)
153 int ifblock_stack_empty(void **opaque)
155 return *opaque == NULL;