outsource the whole template object machinery
[monky] / src / template.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) 2004, Hannu Saransaari and Lauri Hakkarainen
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  * vim: ts=4 sw=4 noet ai cindent syntax=c
29  *
30  */
31 #include "conky.h"
32 #include "logging.h"
33 #include <ctype.h>
34 #include <stdlib.h>
35 #include <string.h>
36 /* The templates defined by the user.
37  *
38  * This is a 1 to 1 mapping from templateN config option to template[N] field. */
39 static char *template[MAX_TEMPLATES];
40
41 /* free all templates
42  *
43  * On first invocation, just memset all pointers to zero, so this function can
44  * be used when initialising data upon startup. */
45 void free_templates(void)
46 {
47         int i;
48         static int initialised = 0;
49
50         if (!initialised) {
51                 memset(template, 0, MAX_TEMPLATES * sizeof(char *));
52                 initialised = 1;
53                 return;
54         }
55
56         for (i = 0; i < MAX_TEMPLATES; i++) {
57                 if (template[i]) {
58                         free(template[i]);
59                         template[i] = NULL;
60                 }
61         }
62 }
63
64 /* set the value of template at index n
65  *
66  * Returns non-zero on illegal arguments passed, zero otherwise. */
67 int set_template(int n, const char *val)
68 {
69         if (n < 0 || n >= MAX_TEMPLATES || !val)
70                 return 1;
71         if (template[n])
72                 free(template[n]);
73         template[n] = strdup(val);
74         return 0;
75 }
76
77 /* backslash_escape - do the actual substitution task for template objects
78  *
79  * The field templates is used for substituting the \N occurences. Set it to
80  * NULL to leave them as they are.
81  */
82 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
83 {
84         char *src_dup;
85         const char *p;
86         unsigned int dup_idx = 0, dup_len;
87
88         dup_len = strlen(src) + 1;
89         src_dup = malloc(dup_len * sizeof(char));
90
91         p = src;
92         while (*p) {
93                 switch (*p) {
94                 case '\\':
95                         if (!*(p + 1))
96                                 break;
97                         if (*(p + 1) == '\\') {
98                                 src_dup[dup_idx++] = '\\';
99                                 p++;
100                         } else if (*(p + 1) == ' ') {
101                                 src_dup[dup_idx++] = ' ';
102                                 p++;
103                         } else if (*(p + 1) == 'n') {
104                                 src_dup[dup_idx++] = '\n';
105                                 p++;
106                         } else if (templates) {
107                                 unsigned int tmpl_num;
108                                 int digits;
109                                 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
110                                     (tmpl_num > template_count))
111                                         break;
112                                 dup_len += strlen(templates[tmpl_num - 1]);
113                                 src_dup = realloc(src_dup, dup_len * sizeof(char));
114                                 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
115                                 dup_idx += strlen(templates[tmpl_num - 1]);
116                                 p += digits;
117                         }
118                         break;
119                 default:
120                         src_dup[dup_idx++] = *p;
121                         break;
122                 }
123                 p++;
124         }
125         src_dup[dup_idx] = '\0';
126         src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
127         return src_dup;
128 }
129
130 /* handle_template_object - core logic of the template object
131  *
132  * use config variables like this:
133  * template1 = "$\1\2"
134  * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
135  *
136  * and use them like this:
137  * ${template1 node name}
138  * ${template2 root /}
139  * ${template2 cdrom /mnt/cdrom}
140  */
141 static char *handle_template(const char *tmpl, const char *args)
142 {
143         char *args_dup = NULL;
144         char *p, *p_old;
145         char **argsp = NULL;
146         unsigned int argcnt = 0, template_idx, i;
147         char *eval_text;
148
149         if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
150             (template_idx >= MAX_TEMPLATES))
151                 return NULL;
152
153         if(args) {
154                 args_dup = strdup(args);
155                 p = args_dup;
156                 while (*p) {
157                         while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
158                                 p++;
159                         if (p > args_dup && *(p - 1) == '\\')
160                                 p--;
161                         p_old = p;
162                         while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
163                                 p++;
164                         if (*p) {
165                                 (*p) = '\0';
166                                 p++;
167                         }
168                         argsp = realloc(argsp, ++argcnt * sizeof(char *));
169                         argsp[argcnt - 1] = p_old;
170                 }
171                 for (i = 0; i < argcnt; i++) {
172                         char *tmp;
173                         tmp = backslash_escape(argsp[i], NULL, 0);
174                         DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
175                         argsp[i] = tmp;
176                 }
177         }
178
179         eval_text = backslash_escape(template[template_idx], argsp, argcnt);
180         DBGP("substituted %s, output is '%s'", tmpl, eval_text);
181         free(args_dup);
182         for (i = 0; i < argcnt; i++)
183                 free(argsp[i]);
184         free(argsp);
185         return eval_text;
186 }
187
188 /* Search inbuf and replace all found template object references
189  * with the substituted value. */
190 char *find_and_replace_templates(const char *inbuf)
191 {
192         char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
193         int stack, outlen;
194
195         outlen = strlen(inbuf) + 1;
196         o = outbuf = calloc(outlen, sizeof(char));
197         memset(outbuf, 0, outlen * sizeof(char));
198
199         p = indup = strdup(inbuf);
200         while (*p) {
201                 while (*p && *p != '$')
202                         *(o++) = *(p++);
203
204                 if (!(*p))
205                         break;
206
207                 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
208                         *(o++) = *(p++);
209                         continue;
210                 }
211
212                 if (*(p + 1) == '{') {
213                         p += 2;
214                         templ = p;
215                         while (*p && !isspace(*p) && *p != '{' && *p != '}')
216                                 p++;
217                         if (*p == '}')
218                                 args = NULL;
219                         else
220                                 args = p;
221
222                         stack = 1;
223                         while (*p && stack > 0) {
224                                 if (*p == '{')
225                                         stack++;
226                                 else if (*p == '}')
227                                         stack--;
228                                 p++;
229                         }
230                         if (stack == 0) {
231                                 // stack is empty. that means the previous char was }, so we zero it
232                                 *(p - 1) = '\0';
233                         } else {
234                                 // we ran into the end of string without finding a closing }, bark
235                                 CRIT_ERR(NULL, NULL, "cannot find a closing '}' in template expansion");
236                         }
237                 } else {
238                         templ = p + 1;
239                         while (*p && !isspace(*p))
240                                 p++;
241                         args = NULL;
242                 }
243                 tmpl_out = handle_template(templ, args);
244                 if (tmpl_out) {
245                         outlen += strlen(tmpl_out);
246                         *o = '\0';
247                         outbuf = realloc(outbuf, outlen * sizeof(char));
248                         strcat (outbuf, tmpl_out);
249                         free(tmpl_out);
250                         o = outbuf + strlen(outbuf);
251                 } else {
252                         NORM_ERR("failed to handle template '%s' with args '%s'", templ, args);
253                 }
254         }
255         *o = '\0';
256         outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
257         free(indup);
258         return outbuf;
259 }
260
261 /* check text for any template object references */
262 int text_contains_templates(const char *text)
263 {
264         if (strcasestr(text, "${template") != NULL)
265                 return 1;
266         if (strcasestr(text, "$template") != NULL)
267                 return 1;
268         return 0;
269 }
270