mail: convert to generic object payload
[monky] / src / text_object.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13  *      (see AUTHORS)
14  * All rights reserved.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  *
28  */
29 #include "text_object.h"
30 #include "logging.h"
31
32 /* text_object_list
33  *
34  * this list is special. it looks like this:
35  *  NULL <-- obj1 <--> obj2 <--> ... <--> objN --> NULL
36  *            ^-------root_object----------^
37  *             directions are reversed here
38  *
39  * why this is cool:
40  * - root_object points both to the start and end of the list
41  * - while traversing, the end of the list is always a NULL pointer
42  *   (this works in BOTH directions)
43  */
44
45 /* append an object to the given root object's list */
46 int append_object(struct text_object *root, struct text_object *obj)
47 {
48         struct text_object *end;
49
50         end = root->prev;
51         obj->prev = end;
52         obj->next = NULL;
53
54         if (end) {
55                 if (end->next)
56                         CRIT_ERR(NULL, NULL, "huston, we have a lift-off");
57                 end->next = obj;
58         } else {
59                 root->next = obj;
60         }
61         root->prev = obj;
62         return 0;
63 }
64
65 /* ifblock handlers for the object list
66  *
67  * - each if points to it's else or endif
68  * - each else points to it's endif
69  *
70  */
71
72 /* possible ifblock types
73  * only used internally, so no need to make this public
74  */
75 enum ifblock_type {
76         IFBLOCK_IF = 1,
77         IFBLOCK_ELSE,
78         IFBLOCK_ENDIF,
79 };
80
81 /* linked list of ifblock objects, building a stack
82  * only used internally, so no need to make this public
83  */
84 struct ifblock_stack_obj {
85         enum ifblock_type type;
86         struct text_object *obj;
87         struct ifblock_stack_obj *next;
88 };
89
90 /* push an ifblock object onto the stack
91  * in fact, this does a lot more:
92  * - IFBLOCK_IF is just pushed onto the stack
93  * - IFBLOCK_ELSE updates the "next" pointer of the upmost
94  *   object in the stack and is then pushed onto the stack
95  * - IFBLOCK_ENDIF updates the "next" pointer of the upmost
96  *   object in the stack and then triggers stack popping of
97  *   any optional IFBLOCK_ELSE along with it's IFBLOCK_IF
98  */
99 static int push_ifblock(struct ifblock_stack_obj **ifblock_stack_top,
100                         struct text_object *obj, enum ifblock_type type)
101 {
102         struct ifblock_stack_obj *stackobj;
103
104         switch (type) {
105                 case IFBLOCK_ENDIF:
106                         if (!(*ifblock_stack_top))
107                                 CRIT_ERR(NULL, NULL, "got an endif without matching if");
108                         (*ifblock_stack_top)->obj->data.ifblock.next = obj;
109                         /* if there's some else in between, remove and free it */
110                         if ((*ifblock_stack_top)->type == IFBLOCK_ELSE) {
111                                 stackobj = *ifblock_stack_top;
112                                 *ifblock_stack_top = stackobj->next;
113                                 free(stackobj);
114                         }
115                         /* finally remove and free the if object */
116                         stackobj = *ifblock_stack_top;
117                         *ifblock_stack_top = stackobj->next;
118                         free(stackobj);
119                         break;
120                 case IFBLOCK_ELSE:
121                         if (!(*ifblock_stack_top))
122                                 CRIT_ERR(NULL, NULL, "got an else without matching if");
123                         (*ifblock_stack_top)->obj->data.ifblock.next = obj;
124                         /* fall through */
125                 case IFBLOCK_IF:
126                         stackobj = malloc(sizeof(struct ifblock_stack_obj));
127                         stackobj->type = type;
128                         stackobj->obj = obj;
129                         stackobj->next = *ifblock_stack_top;
130                         *ifblock_stack_top = stackobj;
131                         break;
132                 default:
133                         CRIT_ERR(NULL, NULL, "push_ifblock() missuse detected!");
134         }
135         return 0;
136 }
137
138 /* public functions for client use */
139
140 int obj_be_ifblock_if(void **opaque, struct text_object *obj)
141 {
142         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_IF);
143 }
144 int obj_be_ifblock_else(void **opaque, struct text_object *obj)
145 {
146         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ELSE);
147 }
148 int obj_be_ifblock_endif(void **opaque, struct text_object *obj)
149 {
150         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ENDIF);
151 }
152
153 /* check if ifblock stack is empty
154  * if so, return true (!= 0)
155  */
156 int ifblock_stack_empty(void **opaque)
157 {
158         return *opaque == NULL;
159 }