Update copyright stuff, fix conky.conf weirdness.
[monky] / src / text_object.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
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.
18  *
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/>.
25  *
26  */
27 #include "text_object.h"
28 #include "logging.h"
29
30 /* text_object_list
31  *
32  * this list is special. it looks like this:
33  *  NULL <-- obj1 <--> obj2 <--> ... <--> objN --> NULL
34  *            ^-------root_object----------^
35  *             directions are reversed here
36  *
37  * why this is cool:
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)
41  */
42
43 /* append an object to the given root object's list */
44 int append_object(struct text_object *root, struct text_object *obj)
45 {
46         struct text_object *end;
47
48         end = root->prev;
49         obj->prev = end;
50         obj->next = NULL;
51
52         if (end) {
53                 if (end->next)
54                         CRIT_ERR("huston, we have a lift-off");
55                 end->next = obj;
56         } else {
57                 root->next = obj;
58         }
59         root->prev = obj;
60         return 0;
61 }
62
63 /* ifblock handlers for the object list
64  *
65  * - each if points to it's else or endif
66  * - each else points to it's endif
67  *
68  */
69
70 /* possible ifblock types
71  * only used internally, so no need to make this public
72  */
73 enum ifblock_type {
74         IFBLOCK_IF = 1,
75         IFBLOCK_ELSE,
76         IFBLOCK_ENDIF,
77 };
78
79 /* linked list of ifblock objects, building a stack
80  * only used internally, so no need to make this public
81  */
82 struct ifblock_stack_obj {
83         enum ifblock_type type;
84         struct text_object *obj;
85         struct ifblock_stack_obj *next;
86 };
87
88 /* push an ifblock object onto the stack
89  * in fact, this does a lot more:
90  * - IFBLOCK_IF is just pushed onto the stack
91  * - IFBLOCK_ELSE updates the "next" pointer of the upmost
92  *   object in the stack and is then pushed onto the stack
93  * - IFBLOCK_ENDIF updates the "next" pointer of the upmost
94  *   object in the stack and then triggers stack popping of
95  *   any optional IFBLOCK_ELSE along with it's IFBLOCK_IF
96  */
97 static int push_ifblock(struct ifblock_stack_obj **ifblock_stack_top,
98                         struct text_object *obj, enum ifblock_type type)
99 {
100         struct ifblock_stack_obj *stackobj;
101
102         switch (type) {
103                 case IFBLOCK_ENDIF:
104                         if (!(*ifblock_stack_top))
105                                 CRIT_ERR("got an endif without matching if");
106                         (*ifblock_stack_top)->obj->data.ifblock.next = obj;
107                         /* if there's some else in between, remove and free it */
108                         if ((*ifblock_stack_top)->type == IFBLOCK_ELSE) {
109                                 stackobj = *ifblock_stack_top;
110                                 *ifblock_stack_top = stackobj->next;
111                                 free(stackobj);
112                         }
113                         /* finally remove and free the if object */
114                         stackobj = *ifblock_stack_top;
115                         *ifblock_stack_top = stackobj->next;
116                         free(stackobj);
117                         break;
118                 case IFBLOCK_ELSE:
119                         if (!(*ifblock_stack_top))
120                                 CRIT_ERR("got an else without matching if");
121                         (*ifblock_stack_top)->obj->data.ifblock.next = obj;
122                         /* fall through */
123                 case IFBLOCK_IF:
124                         stackobj = malloc(sizeof(struct ifblock_stack_obj));
125                         stackobj->type = type;
126                         stackobj->obj = obj;
127                         stackobj->next = *ifblock_stack_top;
128                         *ifblock_stack_top = stackobj;
129                         break;
130                 default:
131                         CRIT_ERR("push_ifblock() missuse detected!");
132         }
133         return 0;
134 }
135
136 /* public functions for client use */
137
138 int obj_be_ifblock_if(void **opaque, struct text_object *obj)
139 {
140         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_IF);
141 }
142 int obj_be_ifblock_else(void **opaque, struct text_object *obj)
143 {
144         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ELSE);
145 }
146 int obj_be_ifblock_endif(void **opaque, struct text_object *obj)
147 {
148         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ENDIF);
149 }
150
151 /* check if ifblock stack is empty
152  * if so, return true (!= 0)
153  */
154 int ifblock_stack_empty(void **opaque)
155 {
156         return *opaque == NULL;
157 }