Fix removing the config and sending a SIGUSR1 results in segfault
[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-2010 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 #include <stdlib.h>
32 #include <stdio.h>
33
34 /* text_object_list
35  *
36  * this list is special. it looks like this:
37  *  NULL <-- obj1 <--> obj2 <--> ... <--> objN --> NULL
38  *            ^-------root_object----------^
39  *             directions are reversed here
40  *
41  * why this is cool:
42  * - root_object points both to the start and end of the list
43  * - while traversing, the end of the list is always a NULL pointer
44  *   (this works in BOTH directions)
45  */
46
47 /* append an object to the given root object's list */
48 int append_object(struct text_object *root, struct text_object *obj)
49 {
50         struct text_object *end;
51
52         end = root->prev;
53         obj->prev = end;
54         obj->next = NULL;
55
56         if (end) {
57                 if (end->next)
58                         CRIT_ERR(NULL, NULL, "huston, we have a lift-off");
59                 end->next = obj;
60         } else {
61                 root->next = obj;
62         }
63         root->prev = obj;
64         return 0;
65 }
66
67 /* ifblock handlers for the object list
68  *
69  * - each if points to it's else or endif
70  * - each else points to it's endif
71  *
72  */
73
74 /* possible ifblock types
75  * only used internally, so no need to make this public
76  */
77 enum ifblock_type {
78         IFBLOCK_IF = 1,
79         IFBLOCK_ELSE,
80         IFBLOCK_ENDIF,
81 };
82
83 /* linked list of ifblock objects, building a stack
84  * only used internally, so no need to make this public
85  */
86 struct ifblock_stack_obj {
87         enum ifblock_type type;
88         struct text_object *obj;
89         struct ifblock_stack_obj *next;
90 };
91
92 /* push an ifblock object onto the stack
93  * in fact, this does a lot more:
94  * - IFBLOCK_IF is just pushed onto the stack
95  * - IFBLOCK_ELSE updates the "next" pointer of the upmost
96  *   object in the stack and is then pushed onto the stack
97  * - IFBLOCK_ENDIF updates the "next" pointer of the upmost
98  *   object in the stack and then triggers stack popping of
99  *   any optional IFBLOCK_ELSE along with it's IFBLOCK_IF
100  */
101 static int push_ifblock(struct ifblock_stack_obj **ifblock_stack_top,
102                         struct text_object *obj, enum ifblock_type type)
103 {
104         struct ifblock_stack_obj *stackobj;
105
106         switch (type) {
107                 case IFBLOCK_ENDIF:
108                         if (!(*ifblock_stack_top))
109                                 CRIT_ERR(NULL, NULL, "got an endif without matching if");
110                         (*ifblock_stack_top)->obj->ifblock_next = obj;
111                         /* if there's some else in between, remove and free it */
112                         if ((*ifblock_stack_top)->type == IFBLOCK_ELSE) {
113                                 stackobj = *ifblock_stack_top;
114                                 *ifblock_stack_top = stackobj->next;
115                                 free(stackobj);
116                         }
117                         /* finally remove and free the if object */
118                         stackobj = *ifblock_stack_top;
119                         *ifblock_stack_top = stackobj->next;
120                         free(stackobj);
121                         break;
122                 case IFBLOCK_ELSE:
123                         if (!(*ifblock_stack_top))
124                                 CRIT_ERR(NULL, NULL, "got an else without matching if");
125                         (*ifblock_stack_top)->obj->ifblock_next = obj;
126                         /* fall through */
127                 case IFBLOCK_IF:
128                         stackobj = malloc(sizeof(struct ifblock_stack_obj));
129                         stackobj->type = type;
130                         stackobj->obj = obj;
131                         stackobj->next = *ifblock_stack_top;
132                         *ifblock_stack_top = stackobj;
133                         break;
134                 default:
135                         CRIT_ERR(NULL, NULL, "push_ifblock() missuse detected!");
136         }
137         return 0;
138 }
139
140 /* public functions for client use */
141
142 int obj_be_ifblock_if(void **opaque, struct text_object *obj)
143 {
144         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_IF);
145 }
146 int obj_be_ifblock_else(void **opaque, struct text_object *obj)
147 {
148         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ELSE);
149 }
150 int obj_be_ifblock_endif(void **opaque, struct text_object *obj)
151 {
152         return push_ifblock((struct ifblock_stack_obj **)opaque, obj, IFBLOCK_ENDIF);
153 }
154
155 /* check if ifblock stack is empty
156  * if so, return true (!= 0)
157  */
158 int ifblock_stack_empty(void **opaque)
159 {
160         return *opaque == NULL;
161 }