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) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "text_object.h"
28 #include "conky.h" /* not as worse: only for the printing macros */
32 * this list is special. it looks like this:
33 * NULL <-- obj1 <--> obj2 <--> ... <--> objN --> NULL
34 * ^-------root_object----------^
35 * directions are reversed here
38 * - root_object points both to the start and end of the list
39 * - while traversing, the end of the list is always a NULL pointer
40 * (this works in BOTH directions)
43 /* append an object to the given root object's list */
44 int append_object(struct text_object *root, struct text_object *obj)
46 struct text_object *end;
54 CRIT_ERR("huston, we have a lift-off");
63 /* ifblock handlers for the object list
65 * - each if points to it's else or endif
66 * - each else points to it's endif
70 /* possible ifblock types
71 * only used internally, so no need to make this public
79 /* linked list of ifblock objects, building a stack
80 * only used internally, so no need to make this public
82 struct ifblock_stack_obj {
83 enum ifblock_type type;
84 struct text_object *obj;
85 struct ifblock_stack_obj *next;
88 /* top of the internal ifblock stack
89 * initially contains only one "object", i.e. a NULL pointer
90 * indicating the end of the stack.
92 static struct ifblock_stack_obj *ifblock_stack_top = NULL;
94 /* push an ifblock object onto the stack
95 * in fact, this does a lot more:
96 * - IFBLOCK_IF is just pushed onto the stack
97 * - IFBLOCK_ELSE updates the "next" pointer of the upmost
98 * object in the stack and is then pushed onto the stack
99 * - IFBLOCK_ENDIF updates the "next" pointer of the upmost
100 * object in the stack and then triggers stack popping of
101 * any optional IFBLOCK_ELSE along with it's IFBLOCK_IF
103 static int push_ifblock(struct text_object *obj, enum ifblock_type type)
105 struct ifblock_stack_obj *stackobj;
109 if (!ifblock_stack_top)
110 CRIT_ERR("got an endif without matching if");
111 ifblock_stack_top->obj->data.ifblock.next = obj;
112 /* if there's some else in between, remove and free it */
113 if (ifblock_stack_top->type == IFBLOCK_ELSE) {
114 stackobj = ifblock_stack_top;
115 ifblock_stack_top = stackobj->next;
118 /* finally remove and free the if object */
119 stackobj = ifblock_stack_top;
120 ifblock_stack_top = stackobj->next;
124 if (!ifblock_stack_top)
125 CRIT_ERR("got an else without matching if");
126 ifblock_stack_top->obj->data.ifblock.next = obj;
129 stackobj = malloc(sizeof(struct ifblock_stack_obj));
130 stackobj->type = type;
132 stackobj->next = ifblock_stack_top;
133 ifblock_stack_top = stackobj;
136 CRIT_ERR("push_ifblock() missuse detected!");
141 /* public functions for client use */
143 int obj_be_ifblock_if(struct text_object *obj)
145 return push_ifblock(obj, IFBLOCK_IF);
147 int obj_be_ifblock_else(struct text_object *obj)
149 return push_ifblock(obj, IFBLOCK_ELSE);
151 int obj_be_ifblock_endif(struct text_object *obj)
153 return push_ifblock(obj, IFBLOCK_ENDIF);
156 /* check if ifblock stack is empty
157 * if so, return true (!= 0)
159 int ifblock_stack_empty(void)
161 return ifblock_stack_top == NULL;