Fix segfault in mail folder (SF: 3007493)
[monky] / src / combine.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) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 #include "core.h"
31 #include "logging.h"
32 #include "text_object.h"
33
34 struct combine_data {
35         char *left;
36         char *seperation;
37         char *right;
38 };
39
40 void parse_combine_arg(struct text_object *obj, const char *arg, void *free_at_crash)
41 {
42         struct combine_data *cd;
43         unsigned int i,j;
44         unsigned int indenting = 0;     //vars can be used as args for other vars
45         int startvar[2];
46         int endvar[2];
47         startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
48         j=0;
49         for (i=0; arg[i] != 0 && j < 2; i++) {
50                 if(startvar[j] == -1) {
51                         if(arg[i] == '$') {
52                                 startvar[j] = i;
53                         }
54                 }else if(endvar[j] == -1) {
55                         if(arg[i] == '{') {
56                                 indenting++;
57                         }else if(arg[i] == '}') {
58                                 indenting--;
59                         }
60                         if (indenting == 0 && arg[i+1] < 48) {  //<48 has 0, $, and the most used chars not used in varnames but not { or }
61                                 endvar[j]=i+1;
62                                 j++;
63                         }
64                 }
65         }
66         if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
67                 cd = malloc(sizeof(struct combine_data));
68                 memset(cd, 0, sizeof(struct combine_data));
69
70                 cd->left = malloc(endvar[0]-startvar[0] + 1);
71                 cd->seperation = malloc(startvar[1] - endvar[0] + 1);
72                 cd->right= malloc(endvar[1]-startvar[1] + 1);
73
74                 strncpy(cd->left, arg + startvar[0], endvar[0] - startvar[0]);
75                 cd->left[endvar[0] - startvar[0]] = 0;
76
77                 strncpy(cd->seperation, arg + endvar[0], startvar[1] - endvar[0]);
78                 cd->seperation[startvar[1] - endvar[0]] = 0;
79
80                 strncpy(cd->right, arg + startvar[1], endvar[1] - startvar[1]);
81                 cd->right[endvar[1] - startvar[1]] = 0;
82
83                 obj->sub = malloc(sizeof(struct text_object));
84                 extract_variable_text_internal(obj->sub, cd->left);
85                 obj->sub->sub = malloc(sizeof(struct text_object));
86                 extract_variable_text_internal(obj->sub->sub, cd->right);
87                 obj->data.opaque = cd;
88         } else {
89                 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
90         }
91 }
92
93 void print_combine(struct text_object *obj, char *p, struct information *cur)
94 {
95         struct combine_data *cd = obj->data.opaque;
96         char buf[2][max_user_text];
97         int i, j;
98         long longest=0;
99         int nextstart;
100         int nr_rows[2];
101         struct llrows {
102                 char* row;
103                 struct llrows* next;
104         };
105         struct llrows *ll_rows[2], *current[2];
106         struct text_object * objsub = obj->sub;
107
108         if (!cd)
109                 return;
110
111         p[0]=0;
112         for(i=0; i<2; i++) {
113                 nr_rows[i] = 1;
114                 nextstart = 0;
115                 ll_rows[i] = malloc(sizeof(struct llrows));
116                 current[i] = ll_rows[i];
117                 for(j=0; j<i; j++) objsub = objsub->sub;
118                 generate_text_internal(buf[i], max_user_text, *objsub, cur);
119                 for(j=0; buf[i][j] != 0; j++) {
120                         if(buf[i][j] == '\t') buf[i][j] = ' ';
121                         if(buf[i][j] == '\n') buf[i][j] = 0;    //the vars inside combine may not have a \n at the end
122                         if(buf[i][j] == 2) {    // \002 is used instead of \n to separate lines inside a var
123                                 buf[i][j] = 0;
124                                 current[i]->row = strdup(buf[i]+nextstart);
125                                 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
126                                 current[i]->next = malloc(sizeof(struct llrows));
127                                 current[i] = current[i]->next;
128                                 nextstart = j + 1;
129                                 nr_rows[i]++;
130                         }
131                 }
132                 current[i]->row = strdup(buf[i]+nextstart);
133                 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
134                 current[i]->next = NULL;
135                 current[i] = ll_rows[i];
136         }
137         for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
138                 if(current[0]) {
139                         strcat(p, current[0]->row);
140                         i=strlen(current[0]->row);
141                 }else i = 0;
142                 while(i < longest) {
143                         strcat(p, " ");
144                         i++;
145                 }
146                 if(current[1]) {
147                         strcat(p, cd->seperation);
148                         strcat(p, current[1]->row);
149                 }
150                 strcat(p, "\n");
151 #ifdef HAVE_OPENMP
152 #pragma omp parallel for schedule(dynamic,10)
153 #endif /* HAVE_OPENMP */
154                 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
155         }
156 #ifdef HAVE_OPENMP
157 #pragma omp parallel for schedule(dynamic,10)
158 #endif /* HAVE_OPENMP */
159         for(i=0; i<2; i++) {
160                 while(ll_rows[i] != NULL) {
161                         current[i]=ll_rows[i];
162                         free(current[i]->row);
163                         ll_rows[i]=current[i]->next;
164                         free(current[i]);
165                 }
166         }
167 }
168
169 void free_combine(struct text_object *obj)
170 {
171         struct combine_data *cd = obj->data.opaque;
172
173         if (!cd)
174                 return;
175         free(cd->left);
176         free(cd->seperation);
177         free(cd->right);
178         free_text_objects(obj->sub->sub, 1);
179         free(obj->sub->sub);
180         free_text_objects(obj->sub, 1);
181         free(obj->sub);
182         free(obj->data.opaque);
183         obj->data.opaque = NULL;
184 }