1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
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.
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/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
36 /* The templates defined by the user.
38 * This is a 1 to 1 mapping from templateN config option to template[N] field. */
39 static char *template[MAX_TEMPLATES];
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)
48 static int initialised = 0;
51 memset(template, 0, MAX_TEMPLATES * sizeof(char *));
56 for (i = 0; i < MAX_TEMPLATES; i++) {
64 /* set the value of template at index n
66 * Returns non-zero on illegal arguments passed, zero otherwise. */
67 int set_template(int n, const char *val)
69 if (n < 0 || n >= MAX_TEMPLATES || !val)
73 template[n] = strdup(val);
77 /* backslash_escape - do the actual substitution task for template objects
79 * The field templates is used for substituting the \N occurences. Set it to
80 * NULL to leave them as they are.
82 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
86 unsigned int dup_idx = 0, dup_len;
88 dup_len = strlen(src) + 1;
89 src_dup = malloc(dup_len * sizeof(char));
97 if (*(p + 1) == '\\') {
98 src_dup[dup_idx++] = '\\';
100 } else if (*(p + 1) == ' ') {
101 src_dup[dup_idx++] = ' ';
103 } else if (*(p + 1) == 'n') {
104 src_dup[dup_idx++] = '\n';
106 } else if (templates) {
107 unsigned int tmpl_num;
109 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
110 (tmpl_num > template_count))
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]);
120 src_dup[dup_idx++] = *p;
125 src_dup[dup_idx] = '\0';
126 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
130 /* handle_template_object - core logic of the template object
132 * use config variables like this:
133 * template1 = "$\1\2"
134 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
136 * and use them like this:
137 * ${template1 node name}
138 * ${template2 root /}
139 * ${template2 cdrom /mnt/cdrom}
141 static char *handle_template(const char *tmpl, const char *args)
143 char *args_dup = NULL;
146 unsigned int argcnt = 0, template_idx, i;
149 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
150 (template_idx >= MAX_TEMPLATES))
154 args_dup = strdup(args);
157 while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
159 if (p > args_dup && *(p - 1) == '\\')
162 while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
168 argsp = realloc(argsp, ++argcnt * sizeof(char *));
169 argsp[argcnt - 1] = p_old;
171 for (i = 0; i < argcnt; i++) {
173 tmp = backslash_escape(argsp[i], NULL, 0);
174 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
179 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
180 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
182 for (i = 0; i < argcnt; i++)
188 /* Search inbuf and replace all found template object references
189 * with the substituted value. */
190 char *find_and_replace_templates(const char *inbuf)
192 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
195 outlen = strlen(inbuf) + 1;
196 o = outbuf = calloc(outlen, sizeof(char));
197 memset(outbuf, 0, outlen * sizeof(char));
199 p = indup = strdup(inbuf);
201 while (*p && *p != '$')
207 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
212 if (*(p + 1) == '{') {
215 while (*p && !isspace(*p) && *p != '{' && *p != '}')
223 while (*p && stack > 0) {
231 // stack is empty. that means the previous char was }, so we zero it
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");
239 while (*p && !isspace(*p))
243 tmpl_out = handle_template(templ, args);
245 outlen += strlen(tmpl_out);
247 outbuf = realloc(outbuf, outlen * sizeof(char));
248 strcat (outbuf, tmpl_out);
250 o = outbuf + strlen(outbuf);
252 NORM_ERR("failed to handle template '%s' with args '%s'", templ, args);
256 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
261 /* check text for any template object references */
262 int text_contains_templates(const char *text)
264 if (strcasestr(text, "${template") != NULL)
266 if (strcasestr(text, "$template") != NULL)