78a8a867f23490c73e19dcbf3c7b4b74b29ea6ee
[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) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
10  *      (see AUTHORS)
11  * All rights reserved.
12  *
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.
17  *
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/>.
24  *
25  */
26 #include "text_object.h"
27 #include "logging.h"
28
29 /* text_object_list
30  *
31  * this list is special. it looks like this:
32  *  NULL <-- obj1 <--> obj2 <--> ... <--> objN --> NULL
33  *            ^-------root_object----------^
34  *             directions are reversed here
35  *
36  * why this is cool:
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)
40  */
41
42 /* append an object to the given root object's list */
43 int append_object(struct text_object *root, struct text_object *obj)
44 {
45         struct text_object *end;
46
47         end = root->prev;
48         obj->prev = end;
49         obj->next = NULL;
50
51         if (end) {
52                 if (end->next)
53                         CRIT_ERR("huston, we have a lift-off");
54                 end->next = obj;
55         } else {
56                 root->next = obj;
57         }
58         root->prev = obj;
59         return 0;
60 }
61
62 /* ifblock handlers for the object list
63  *
64  * - each if points to it's else or endif
65  * - each else points to it's endif
66  *
67  */
68
69 /* possible ifblock types
70  * only used internally, so no need to make this public
71  */
72 enum ifblock_type {
73         IFBLOCK_IF = 1,
74         IFBLOCK_ELSE,
75         IFBLOCK_ENDIF,
76 };
77
78 /* linked list of ifblock objects, building a stack
79  * only used internally, so no need to make this public
80  */
81 struct ifblock_stack_obj {
82         enum ifblock_type type;
83         struct text_object *obj;
84         struct ifblock_stack_obj *next;
85 };
86
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
95  */
96 static int push_ifblock(struct ifblock_stack_obj **ifblock_stack_top,
97                         struct text_object *obj, enum ifblock_type type)
98 {
99         struct ifblock_stack_obj *stackobj;
100
101         switch (type) {
102                 case IFBLOCK_ENDIF:
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;
110                                 free(stackobj);
111                         }
112                         /* finally remove and free the if object */
113                         stackobj = *ifblock_stack_top;
114                         *ifblock_stack_top = stackobj->next;
115                         free(stackobj);
116                         break;
117                 case IFBLOCK_ELSE:
118                         if (!(*ifblock_stack_top))
119                                 CRIT_ERR("got an else without matching if");
120                         (*ifblock_stack_top)->obj->data.ifblock.next = obj;
121                         /* fall through */
122                 case IFBLOCK_IF:
123                         stackobj = malloc(sizeof(struct ifblock_stack_obj));
124                         stackobj->type = type;
125                         stackobj->obj = obj;
126                         stackobj->next = *ifblock_stack_top;
127                         *ifblock_stack_top = stackobj;
128                         break;
129                 default:
130                         CRIT_ERR("push_ifblock() missuse detected!");
131         }
132         return 0;
133 }
134
135 /* public functions for client use */
136
137 int obj_be_ifblock_if(void **opaque, struct text_object *obj)
138 {
139         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_IF);
140 }
141 int obj_be_ifblock_else(void **opaque, struct text_object *obj)
142 {
143         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ELSE);
144 }
145 int obj_be_ifblock_endif(void **opaque, struct text_object *obj)
146 {
147         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ENDIF);
148 }
149
150 /* check if ifblock stack is empty
151  * if so, return true (!= 0)
152  */
153 int ifblock_stack_empty(void **opaque)
154 {
155         return *opaque == NULL;
156 }