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