scroll: outsource code
[monky] / src / core.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-2009 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
31 /* local headers */
32 #include "core.h"
33 #include "text_object.h"
34 #include "algebra.h"
35 #include "build.h"
36 #include "colours.h"
37 #include "diskio.h"
38 #include "exec.h"
39 #ifdef X11
40 #include "fonts.h"
41 #endif
42 #include "fs.h"
43 #ifdef HAVE_ICONV
44 #include "iconv_tools.h"
45 #endif
46 #include "logging.h"
47 #include "mixer.h"
48 #include "mail.h"
49 #include "mboxscan.h"
50 #include "read_tcp.h"
51 #include "scroll.h"
52 #include "specials.h"
53 #include "temphelper.h"
54 #include "template.h"
55 #include "tailhead.h"
56 #include "timeinfo.h"
57 #include "top.h"
58
59 #ifdef NCURSES
60 #include <ncurses.h>
61 #endif
62
63 /* check for OS and include appropriate headers */
64 #if defined(__linux__)
65 #include "linux.h"
66 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
67 #include "freebsd.h"
68 #elif defined(__OpenBSD__)
69 #include "openbsd.h"
70 #endif
71
72 /* OS specific prototypes to be implemented by linux.c & Co. */
73 void update_entropy(void);
74
75 #include <string.h>
76 #include <ctype.h>
77
78 /* strip a leading /dev/ if any, following symlinks first
79  *
80  * BEWARE: this function returns a pointer to static content
81  *         which gets overwritten in consecutive calls. I.e.:
82  *         this function is NOT reentrant.
83  */
84 static const char *dev_name(const char *path)
85 {
86         static char buf[255];   /* should be enough for pathnames */
87         ssize_t buflen;
88
89         if (!path)
90                 return NULL;
91
92 #define DEV_NAME(x) \
93   x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
94         if ((buflen = readlink(path, buf, 254)) == -1)
95                 return DEV_NAME(path);
96         buf[buflen] = '\0';
97         return DEV_NAME(buf);
98 #undef DEV_NAME
99 }
100
101 static struct text_object *new_text_object_internal(void)
102 {
103         struct text_object *obj = malloc(sizeof(struct text_object));
104         memset(obj, 0, sizeof(struct text_object));
105         return obj;
106 }
107
108 static struct text_object *create_plain_text(const char *s)
109 {
110         struct text_object *obj;
111
112         if (s == NULL || *s == '\0') {
113                 return NULL;
114         }
115
116         obj = new_text_object_internal();
117
118         obj->type = OBJ_text;
119         obj->data.s = strndup(s, text_buffer_size);
120         return obj;
121 }
122
123 /* construct_text_object() creates a new text_object */
124 struct text_object *construct_text_object(const char *s, const char *arg, long
125                 line, void **ifblock_opaque, void *free_at_crash)
126 {
127         // struct text_object *obj = new_text_object();
128         struct text_object *obj = new_text_object_internal();
129
130         obj->line = line;
131
132 /* helper defines for internal use only */
133 #define __OBJ_HEAD(a, n) if (!strcmp(s, #a)) { \
134         obj->type = OBJ_##a; add_update_callback(n);
135 #define __OBJ_IF obj_be_ifblock_if(ifblock_opaque, obj)
136 #define __OBJ_ARG(...) if (!arg) { CRIT_ERR(obj, free_at_crash, __VA_ARGS__); }
137
138 /* defines to be used below */
139 #define OBJ(a, n) __OBJ_HEAD(a, n) {
140 #define OBJ_ARG(a, n, ...) __OBJ_HEAD(a, n) __OBJ_ARG(__VA_ARGS__) {
141 #define OBJ_IF(a, n) __OBJ_HEAD(a, n) __OBJ_IF; {
142 #define OBJ_IF_ARG(a, n, ...) __OBJ_HEAD(a, n) __OBJ_ARG(__VA_ARGS__) __OBJ_IF; {
143 #define END } } else
144
145 #ifdef X11
146         if (s[0] == '#') {
147                 obj->type = OBJ_color;
148                 obj->data.l = get_x11_color(s);
149         } else
150 #endif /* X11 */
151 #ifdef __OpenBSD__
152         OBJ(freq, 0)
153 #else
154         OBJ(acpitemp, 0)
155                 obj->data.i = open_acpi_temperature(arg);
156         END OBJ(acpiacadapter, 0)
157         END OBJ(freq, 0)
158 #endif /* !__OpenBSD__ */
159                 get_cpu_count();
160                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
161                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
162                         obj->data.cpu_index = 1;
163                         /* NORM_ERR("freq: Invalid CPU number or you don't have that many CPUs! "
164                                 "Displaying the clock for CPU 1."); */
165                 } else {
166                         obj->data.cpu_index = atoi(&arg[0]);
167                 }
168                 obj->a = 1;
169         END OBJ(freq_g, 0)
170                 get_cpu_count();
171                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
172                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
173                         obj->data.cpu_index = 1;
174                         /* NORM_ERR("freq_g: Invalid CPU number or you don't have that many "
175                                 "CPUs! Displaying the clock for CPU 1."); */
176                 } else {
177                         obj->data.cpu_index = atoi(&arg[0]);
178                 }
179                 obj->a = 1;
180         END OBJ_ARG(read_tcp, 0, "read_tcp: Needs \"(host) port\" as argument(s)")
181                 parse_read_tcp_arg(obj, arg, free_at_crash);
182 #if defined(__linux__)
183         END OBJ(voltage_mv, 0)
184                 get_cpu_count();
185                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
186                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
187                         obj->data.cpu_index = 1;
188                         /* NORM_ERR("voltage_mv: Invalid CPU number or you don't have that many "
189                                 "CPUs! Displaying voltage for CPU 1."); */
190                 } else {
191                         obj->data.cpu_index = atoi(&arg[0]);
192                 }
193                 obj->a = 1;
194         END OBJ(voltage_v, 0)
195                 get_cpu_count();
196                 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
197                                 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
198                         obj->data.cpu_index = 1;
199                         /* NORM_ERR("voltage_v: Invalid CPU number or you don't have that many "
200                                 "CPUs! Displaying voltage for CPU 1."); */
201                 } else {
202                         obj->data.cpu_index = atoi(&arg[0]);
203                 }
204                 obj->a = 1;
205
206 #ifdef HAVE_IWLIB
207         END OBJ(wireless_essid, &update_net_stats)
208                 if (arg) {
209                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
210                 } else {
211                         // default to DEFAULTNETDEV
212                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
213                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
214                         free(buf);
215                 }
216         END OBJ(wireless_mode, &update_net_stats)
217                 if (arg) {
218                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
219                 } else {
220                         // default to DEFAULTNETDEV
221                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
222                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
223                         free(buf);
224                 }
225         END OBJ(wireless_bitrate, &update_net_stats)
226                 if (arg) {
227                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
228                 } else {
229                         // default to DEFAULTNETDEV
230                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
231                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
232                         free(buf);
233                 }
234         END OBJ(wireless_ap, &update_net_stats)
235                 if (arg) {
236                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
237                 } else {
238                         // default to DEFAULTNETDEV
239                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
240                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
241                         free(buf);
242                 }
243         END OBJ(wireless_link_qual, &update_net_stats)
244                 if (arg) {
245                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
246                 } else {
247                         // default to DEFAULTNETDEV
248                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
249                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
250                         free(buf);
251                 }
252         END OBJ(wireless_link_qual_max, &update_net_stats)
253                 if (arg) {
254                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
255                 } else {
256                         // default to DEFAULTNETDEV
257                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
258                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
259                         free(buf);
260                 }
261         END OBJ(wireless_link_qual_perc, &update_net_stats)
262                 if (arg) {
263                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
264                 } else {
265                         // default to DEFAULTNETDEV
266                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
267                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
268                         free(buf);
269                 }
270         END OBJ(wireless_link_bar, &update_net_stats)
271                 SIZE_DEFAULTS(bar);
272                 if (arg) {
273                         arg = scan_bar(arg, &obj->a, &obj->b);
274                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
275                 } else {
276                         // default to DEFAULTNETDEV
277                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
278                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
279                         free(buf);
280                 }
281 #endif /* HAVE_IWLIB */
282
283 #endif /* __linux__ */
284
285 #ifndef __OpenBSD__
286         END OBJ(acpifan, 0)
287         END OBJ(battery, 0)
288                 char bat[64];
289
290                 if (arg) {
291                         sscanf(arg, "%63s", bat);
292                 } else {
293                         strcpy(bat, "BAT0");
294                 }
295                 obj->data.s = strndup(bat, text_buffer_size);
296         END OBJ(battery_short, 0)
297                 char bat[64];
298
299                 if (arg) {
300                         sscanf(arg, "%63s", bat);
301                 } else {
302                         strcpy(bat, "BAT0");
303                 }
304                 obj->data.s = strndup(bat, text_buffer_size);
305         END OBJ(battery_time, 0)
306                 char bat[64];
307
308                 if (arg) {
309                         sscanf(arg, "%63s", bat);
310                 } else {
311                         strcpy(bat, "BAT0");
312                 }
313                 obj->data.s = strndup(bat, text_buffer_size);
314         END OBJ(battery_percent, 0)
315                 char bat[64];
316
317                 if (arg) {
318                         sscanf(arg, "%63s", bat);
319                 } else {
320                         strcpy(bat, "BAT0");
321                 }
322                 obj->data.s = strndup(bat, text_buffer_size);
323         END OBJ(battery_bar, 0)
324                 char bat[64];
325                 SIZE_DEFAULTS(bar);
326                 obj->b = 6;
327                 if (arg) {
328                         arg = scan_bar(arg, &obj->a, &obj->b);
329                         sscanf(arg, "%63s", bat);
330                 } else {
331                         strcpy(bat, "BAT0");
332                 }
333                 obj->data.s = strndup(bat, text_buffer_size);
334 #endif /* !__OpenBSD__ */
335
336 #if defined(__linux__)
337         END OBJ_ARG(disk_protect, 0, "disk_protect needs an argument")
338                 obj->data.s = strndup(dev_name(arg), text_buffer_size);
339         END OBJ(i8k_version, &update_i8k)
340         END OBJ(i8k_bios, &update_i8k)
341         END OBJ(i8k_serial, &update_i8k)
342         END OBJ(i8k_cpu_temp, &update_i8k)
343         END OBJ(i8k_left_fan_status, &update_i8k)
344         END OBJ(i8k_right_fan_status, &update_i8k)
345         END OBJ(i8k_left_fan_rpm, &update_i8k)
346         END OBJ(i8k_right_fan_rpm, &update_i8k)
347         END OBJ(i8k_ac_status, &update_i8k)
348         END OBJ(i8k_buttons_status, &update_i8k)
349 #if defined(IBM)
350         END OBJ(ibm_fan, 0)
351         END OBJ(ibm_temps, &get_ibm_acpi_temps, "ibm_temps: needs an argument")
352                 parse_ibm_temps_arg(obj, arg);
353         END OBJ(ibm_volume, 0)
354         END OBJ(ibm_brightness, 0)
355 #endif
356         /* information from sony_laptop kernel module
357          * /sys/devices/platform/sony-laptop */
358         END OBJ(sony_fanspeed, 0)
359         END OBJ_IF(if_gw, &update_gateway_info)
360         END OBJ_ARG(ioscheduler, 0, "get_ioscheduler needs an argument (e.g. hda)")
361                 obj->data.s = strndup(dev_name(arg), text_buffer_size);
362         END OBJ(laptop_mode, 0)
363         END OBJ_ARG(pb_battery, 0, "pb_battery: needs one argument: status, percent or time")
364                 if (strcmp(arg, "status") == EQUAL) {
365                         obj->data.i = PB_BATT_STATUS;
366                 } else if (strcmp(arg, "percent") == EQUAL) {
367                         obj->data.i = PB_BATT_PERCENT;
368                 } else if (strcmp(arg, "time") == EQUAL) {
369                         obj->data.i = PB_BATT_TIME;
370                 } else {
371                         NORM_ERR("pb_battery: illegal argument '%s', defaulting to status", arg);
372                         obj->data.i = PB_BATT_STATUS;
373                 }
374 #endif /* __linux__ */
375 #if (defined(__FreeBSD__) || defined(__linux__))
376         END OBJ_IF_ARG(if_up, 0, "if_up needs an argument")
377                 obj->data.ifblock.s = strndup(arg, text_buffer_size);
378 #endif
379 #if defined(__OpenBSD__)
380         END OBJ_ARG(obsd_sensors_temp, 0, "obsd_sensors_temp: needs an argument")
381                 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
382                                 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
383                         obj->data.sensor = 0;
384                         NORM_ERR("Invalid temperature sensor number!");
385                 } else
386                         obj->data.sensor = atoi(&arg[0]);
387         END OBJ_ARG(obsd_sensors_fan, 0, "obsd_sensors_fan: needs 2 arguments (device and sensor number)")
388                 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
389                                 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
390                         obj->data.sensor = 0;
391                         NORM_ERR("Invalid fan sensor number!");
392                 } else
393                         obj->data.sensor = atoi(&arg[0]);
394         END OBJ_ARG(obsd_sensors_volt, 0, "obsd_sensors_volt: needs 2 arguments (device and sensor number)")
395                 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
396                                 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
397                         obj->data.sensor = 0;
398                         NORM_ERR("Invalid voltage sensor number!");
399                 } else
400                         obj->data.sensor = atoi(&arg[0]);
401         END OBJ(obsd_vendor, 0)
402         END OBJ(obsd_product, 0)
403 #endif /* __OpenBSD__ */
404         END OBJ(buffers, &update_meminfo)
405         END OBJ(cached, &update_meminfo)
406 #define SCAN_CPU(__arg, __var) { \
407         int __offset = 0; \
408         if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) > 0) \
409                 __arg += __offset; \
410         else \
411                 __var = 0; \
412 }
413         END OBJ(cpu, &update_cpu_usage)
414                 SCAN_CPU(arg, obj->data.cpu_index);
415                 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
416 #ifdef X11
417         END OBJ(cpugauge, &update_cpu_usage)
418                 SIZE_DEFAULTS(gauge);
419                 SCAN_CPU(arg, obj->data.cpu_index);
420                 scan_gauge(arg, &obj->a, &obj->b);
421                 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
422 #endif /* X11 */
423         END OBJ(cpubar, &update_cpu_usage)
424                 SIZE_DEFAULTS(bar);
425                 SCAN_CPU(arg, obj->data.cpu_index);
426                 scan_bar(arg, &obj->a, &obj->b);
427                 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
428 #ifdef X11
429         END OBJ(cpugraph, &update_cpu_usage)
430                 char *buf = 0;
431                 SIZE_DEFAULTS(graph);
432                 SCAN_CPU(arg, obj->data.cpu_index);
433                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
434                         &obj->e, &obj->char_a, &obj->char_b);
435                 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
436                 if (buf) free(buf);
437         END OBJ(loadgraph, &update_load_average)
438                 char *buf = 0;
439                 SIZE_DEFAULTS(graph);
440                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
441                                 &obj->e, &obj->char_a, &obj->char_b);
442                 if (buf) {
443                         int a = 1, r = 3;
444                         if (arg) {
445                                 r = sscanf(arg, "%d", &a);
446                         }
447                         obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
448                         free(buf);
449                 }
450 #endif /* X11 */
451         END OBJ(diskio, &update_diskio)
452                 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
453         END OBJ(diskio_read, &update_diskio)
454                 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
455         END OBJ(diskio_write, &update_diskio)
456                 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
457 #ifdef X11
458         END OBJ(diskiograph, &update_diskio)
459                 char *buf = 0;
460                 SIZE_DEFAULTS(graph);
461                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
462                                 &obj->e, &obj->char_a, &obj->char_b);
463
464                 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
465                 if (buf) free(buf);
466         END OBJ(diskiograph_read, &update_diskio)
467                 char *buf = 0;
468                 SIZE_DEFAULTS(graph);
469                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
470                                 &obj->e, &obj->char_a, &obj->char_b);
471
472                 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
473                 if (buf) free(buf);
474         END OBJ(diskiograph_write, &update_diskio)
475                 char *buf = 0;
476                 SIZE_DEFAULTS(graph);
477                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
478                                 &obj->e, &obj->char_a, &obj->char_b);
479
480                 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
481                 if (buf) free(buf);
482 #endif /* X11 */
483         END OBJ(color, 0)
484 #ifdef X11
485                 if (output_methods & TO_X) {
486                         obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
487                         set_current_text_color(obj->data.l);
488                 }
489 #endif /* X11 */
490 #ifdef NCURSES
491                 if (output_methods & TO_NCURSES) {
492                         obj->data.l = COLOR_WHITE;
493                         if(arg) {
494                                 if(strcasecmp(arg, "red") == 0) {
495                                         obj->data.l = COLOR_RED;
496                                 }else if(strcasecmp(arg, "green") == 0) {
497                                         obj->data.l = COLOR_GREEN;
498                                 }else if(strcasecmp(arg, "yellow") == 0) {
499                                         obj->data.l = COLOR_YELLOW;
500                                 }else if(strcasecmp(arg, "blue") == 0) {
501                                         obj->data.l = COLOR_BLUE;
502                                 }else if(strcasecmp(arg, "magenta") == 0) {
503                                         obj->data.l = COLOR_MAGENTA;
504                                 }else if(strcasecmp(arg, "cyan") == 0) {
505                                         obj->data.l = COLOR_CYAN;
506                                 }else if(strcasecmp(arg, "black") == 0) {
507                                         obj->data.l = COLOR_BLACK;
508                                 }
509                         }
510                         set_current_text_color(obj->data.l);
511                         init_pair(obj->data.l, obj->data.l, COLOR_BLACK);
512                 }
513 #endif /* NCURSES */
514         END OBJ(color0, 0)
515                 obj->data.l = color0;
516                 set_current_text_color(obj->data.l);
517         END OBJ(color1, 0)
518                 obj->data.l = color1;
519                 set_current_text_color(obj->data.l);
520         END OBJ(color2, 0)
521                 obj->data.l = color2;
522                 set_current_text_color(obj->data.l);
523         END OBJ(color3, 0)
524                 obj->data.l = color3;
525                 set_current_text_color(obj->data.l);
526         END OBJ(color4, 0)
527                 obj->data.l = color4;
528                 set_current_text_color(obj->data.l);
529         END OBJ(color5, 0)
530                 obj->data.l = color5;
531                 set_current_text_color(obj->data.l);
532         END OBJ(color6, 0)
533                 obj->data.l = color6;
534                 set_current_text_color(obj->data.l);
535         END OBJ(color7, 0)
536                 obj->data.l = color7;
537                 set_current_text_color(obj->data.l);
538         END OBJ(color8, 0)
539                 obj->data.l = color8;
540                 set_current_text_color(obj->data.l);
541         END OBJ(color9, 0)
542                 obj->data.l = color9;
543                 set_current_text_color(obj->data.l);
544 #ifdef X11
545         END OBJ(font, 0)
546                 obj->data.s = scan_font(arg);
547 #endif /* X11 */
548         END OBJ(conky_version, 0)
549         END OBJ(conky_build_date, 0)
550         END OBJ(conky_build_arch, 0)
551         END OBJ(downspeed, &update_net_stats)
552                 if (arg) {
553                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
554                 } else {
555                         // default to DEFAULTNETDEV
556                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
557                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
558                         free(buf);
559                 }
560         END OBJ(downspeedf, &update_net_stats)
561                 if (arg) {
562                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
563                 } else {
564                         // default to DEFAULTNETDEV
565                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
566                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
567                         free(buf);
568                 }
569 #ifdef X11
570         END OBJ(downspeedgraph, &update_net_stats)
571                 char *buf = 0;
572                 SIZE_DEFAULTS(graph);
573                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
574                                 &obj->e, &obj->char_a, &obj->char_b);
575
576                 // default to DEFAULTNETDEV
577                 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
578                 obj->data.net = get_net_stat(buf, obj, free_at_crash);
579                 free(buf);
580 #endif /* X11 */
581         END OBJ(else, 0)
582                 obj_be_ifblock_else(ifblock_opaque, obj);
583         END OBJ(endif, 0)
584                 obj_be_ifblock_endif(ifblock_opaque, obj);
585         END OBJ(eval, 0)
586                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
587 #if defined(IMLIB2) && defined(X11)
588         END OBJ(image, 0)
589                 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
590 #endif /* IMLIB2 */
591         END OBJ(exec, 0)
592                 scan_exec_arg(obj, arg);
593         END OBJ(execp, 0)
594                 scan_exec_arg(obj, arg);
595         END OBJ(execbar, 0)
596                 SIZE_DEFAULTS(bar);
597                 scan_exec_arg(obj, arg);
598 #ifdef X11
599         END OBJ(execgauge, 0)
600                 SIZE_DEFAULTS(gauge);
601                 scan_exec_arg(obj, arg);
602         END OBJ(execgraph, 0)
603                 SIZE_DEFAULTS(graph);
604                 scan_exec_arg(obj, arg);
605 #endif /* X11 */
606         END OBJ_ARG(execibar, 0, "execibar needs arguments")
607                 SIZE_DEFAULTS(bar);
608                 scan_execi_arg(obj, arg);
609 #ifdef X11
610         END OBJ_ARG(execigraph, 0, "execigraph needs arguments")
611                 SIZE_DEFAULTS(graph);
612                 scan_execi_arg(obj, arg);
613         END OBJ_ARG(execigauge, 0, "execigauge needs arguments")
614                 SIZE_DEFAULTS(gauge);
615                 scan_execi_arg(obj, arg);
616 #endif /* X11 */
617         END OBJ_ARG(execi, 0, "execi needs arguments")
618                 scan_execi_arg(obj, arg);
619         END OBJ_ARG(execpi, 0, "execpi needs arguments")
620                 scan_execi_arg(obj, arg);
621         END OBJ_ARG(texeci, 0, "texeci needs arguments")
622                 scan_execi_arg(obj, arg);
623         END OBJ(pre_exec, 0)
624                 scan_pre_exec_arg(obj, arg);
625         END OBJ(fs_bar, &update_fs_stats)
626                 init_fs_bar(obj, arg);
627         END OBJ(fs_bar_free, &update_fs_stats)
628                 init_fs_bar(obj, arg);
629         END OBJ(fs_free, &update_fs_stats)
630                 if (!arg) {
631                         arg = "/";
632                 }
633                 obj->data.fs = prepare_fs_stat(arg);
634         END OBJ(fs_used_perc, &update_fs_stats)
635                 if (!arg) {
636                         arg = "/";
637                 }
638                 obj->data.fs = prepare_fs_stat(arg);
639         END OBJ(fs_free_perc, &update_fs_stats)
640                 if (!arg) {
641                         arg = "/";
642                 }
643                 obj->data.fs = prepare_fs_stat(arg);
644         END OBJ(fs_size, &update_fs_stats)
645                 if (!arg) {
646                         arg = "/";
647                 }
648                 obj->data.fs = prepare_fs_stat(arg);
649         END OBJ(fs_type, &update_fs_stats)
650                 if (!arg) {
651                         arg = "/";
652                 }
653                 obj->data.fs = prepare_fs_stat(arg);
654         END OBJ(fs_used, &update_fs_stats)
655                 if (!arg) {
656                         arg = "/";
657                 }
658                 obj->data.fs = prepare_fs_stat(arg);
659         END OBJ(hr, 0)
660                 obj->data.i = arg ? atoi(arg) : 1;
661         END OBJ(nameserver, &update_dns_data)
662                 obj->data.i = arg ? atoi(arg) : 0;
663         END OBJ(offset, 0)
664                 obj->data.i = arg ? atoi(arg) : 1;
665         END OBJ(voffset, 0)
666                 obj->data.i = arg ? atoi(arg) : 1;
667         END OBJ_ARG(goto, 0, "goto needs arguments")
668                 obj->data.i = atoi(arg);
669         END OBJ(tab, 0)
670                 int a = 10, b = 0;
671
672                 if (arg) {
673                         if (sscanf(arg, "%d %d", &a, &b) != 2) {
674                                 sscanf(arg, "%d", &b);
675                         }
676                 }
677                 if (a <= 0) {
678                         a = 1;
679                 }
680                 obj->data.pair.a = a;
681                 obj->data.pair.b = b;
682
683 #ifdef __linux__
684         END OBJ_ARG(i2c, 0, "i2c needs arguments")
685                 parse_i2c_sensor(obj, arg);
686         END OBJ_ARG(platform, 0, "platform needs arguments")
687                 parse_platform_sensor(obj, arg);
688         END OBJ_ARG(hwmon, 0, "hwmon needs argumanets")
689                 parse_hwmon_sensor(obj, arg);
690 #endif /* __linux__ */
691
692         END
693         /* we have four different types of top (top, top_mem, top_time and top_io). To
694          * avoid having almost-same code four times, we have this special
695          * handler. */
696         if (strncmp(s, "top", 3) == EQUAL) {
697                 add_update_callback(&update_meminfo);
698                 add_update_callback(&update_top);
699                 if (!parse_top_args(s, arg, obj)) {
700                         return NULL;
701                 }
702         } else OBJ(addr, &update_net_stats)
703                 if (arg) {
704                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
705                 } else {
706                         // default to DEFAULTNETDEV
707                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
708                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
709                         free(buf);
710                 }
711 #if defined(__linux__)
712         END OBJ(addrs, &update_net_stats)
713                 if (arg) {
714                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
715                 } else {
716                         // default to DEFAULTNETDEV
717                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
718                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
719                         free(buf);
720                 }
721 #endif /* __linux__ */
722         END OBJ_ARG(tail, 0, "tail needs arguments")
723                 init_tailhead("tail", arg, obj, free_at_crash);
724         END OBJ_ARG(head, 0, "head needs arguments")
725                 init_tailhead("head", arg, obj, free_at_crash);
726         END OBJ_ARG(lines, 0, "lines needs an argument")
727                 obj->data.s = strndup(arg, text_buffer_size);
728         END OBJ_ARG(words, 0, "words needs a argument")
729                 obj->data.s = strndup(arg, text_buffer_size);
730         END OBJ(loadavg, &update_load_average)
731                 int a = 1, b = 2, c = 3, r = 3;
732
733                 if (arg) {
734                         r = sscanf(arg, "%d %d %d", &a, &b, &c);
735                         if (r >= 3 && (c < 1 || c > 3)) {
736                                 r--;
737                         }
738                         if (r >= 2 && (b < 1 || b > 3)) {
739                                 r--, b = c;
740                         }
741                         if (r >= 1 && (a < 1 || a > 3)) {
742                                 r--, a = b, b = c;
743                         }
744                 }
745                 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
746                 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
747                 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
748         END OBJ_IF_ARG(if_empty, 0, "if_empty needs an argument")
749                 obj->data.ifblock.s = strndup(arg, text_buffer_size);
750                 obj->sub = malloc(sizeof(struct text_object));
751                 extract_variable_text_internal(obj->sub, obj->data.ifblock.s);
752         END OBJ_IF_ARG(if_match, 0, "if_match needs arguments")
753                 obj->data.ifblock.s = strndup(arg, text_buffer_size);
754                 obj->sub = malloc(sizeof(struct text_object));
755                 extract_variable_text_internal(obj->sub, obj->data.ifblock.s);
756         END OBJ_IF_ARG(if_existing, 0, "if_existing needs an argument or two")
757                 char buf1[256], buf2[256];
758                 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
759
760                 if (r == 1) {
761                         obj->data.ifblock.s = strndup(buf1, text_buffer_size);
762                         obj->data.ifblock.str = NULL;
763                 } else {
764                         obj->data.ifblock.s = strndup(buf1, text_buffer_size);
765                         obj->data.ifblock.str = strndup(buf2, text_buffer_size);
766                 }
767                 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
768         END OBJ_IF_ARG(if_mounted, 0, "if_mounted needs an argument")
769                 obj->data.ifblock.s = strndup(arg, text_buffer_size);
770 #ifdef __linux__
771         END OBJ_IF_ARG(if_running, &update_top, "if_running needs an argument")
772                 top_running = 1;
773                 obj->data.ifblock.s = strndup(arg, text_buffer_size);
774 #else
775         END OBJ_IF_ARG(if_running, 0, "if_running needs an argument")
776                 char buf[256];
777
778                 snprintf(buf, 256, "pidof %s >/dev/null", arg);
779                 obj->data.ifblock.s = strndup(buf, text_buffer_size);
780 #endif
781         END OBJ(kernel, 0)
782         END OBJ(machine, 0)
783         END OBJ(mails, 0)
784                 float n1;
785                 char mbox[256], dst[256];
786
787                 if (!arg) {
788                         n1 = 9.5;
789                         /* Kapil: Changed from MAIL_FILE to
790                            current_mail_spool since the latter
791                            is a copy of the former if undefined
792                            but the latter should take precedence
793                            if defined */
794                         strncpy(mbox, current_mail_spool, sizeof(mbox));
795                 } else {
796                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
797                                 n1 = 9.5;
798                                 strncpy(mbox, arg, sizeof(mbox));
799                         }
800                 }
801
802                 variable_substitute(mbox, dst, sizeof(dst));
803                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
804                 obj->data.local_mail.interval = n1;
805         END OBJ(new_mails, 0)
806                 float n1;
807                 char mbox[256], dst[256];
808
809                 if (!arg) {
810                         n1 = 9.5;
811                         strncpy(mbox, current_mail_spool, sizeof(mbox));
812                 } else {
813                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
814                                 n1 = 9.5;
815                                 strncpy(mbox, arg, sizeof(mbox));
816                         }
817                 }
818
819                 variable_substitute(mbox, dst, sizeof(dst));
820                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
821                 obj->data.local_mail.interval = n1;
822         END OBJ(seen_mails, 0)
823                 float n1;
824                 char mbox[256], dst[256];
825
826                 if (!arg) {
827                         n1 = 9.5;
828                         strncpy(mbox, current_mail_spool, sizeof(mbox));
829                 } else {
830                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
831                                 n1 = 9.5;
832                                 strncpy(mbox, arg, sizeof(mbox));
833                         }
834                 }
835
836                 variable_substitute(mbox, dst, sizeof(dst));
837                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
838                 obj->data.local_mail.interval = n1;
839         END OBJ(unseen_mails, 0)
840                 float n1;
841                 char mbox[256], dst[256];
842
843                 if (!arg) {
844                         n1 = 9.5;
845                         strncpy(mbox, current_mail_spool, sizeof(mbox));
846                 } else {
847                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
848                                 n1 = 9.5;
849                                 strncpy(mbox, arg, sizeof(mbox));
850                         }
851                 }
852
853                 variable_substitute(mbox, dst, sizeof(dst));
854                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
855                 obj->data.local_mail.interval = n1;
856         END OBJ(flagged_mails, 0)
857                 float n1;
858                 char mbox[256], dst[256];
859
860                 if (!arg) {
861                         n1 = 9.5;
862                         strncpy(mbox, current_mail_spool, sizeof(mbox));
863                 } else {
864                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
865                                 n1 = 9.5;
866                                 strncpy(mbox, arg, sizeof(mbox));
867                         }
868                 }
869
870                 variable_substitute(mbox, dst, sizeof(dst));
871                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
872                 obj->data.local_mail.interval = n1;
873         END OBJ(unflagged_mails, 0)
874                 float n1;
875                 char mbox[256], dst[256];
876
877                 if (!arg) {
878                         n1 = 9.5;
879                         strncpy(mbox, current_mail_spool, sizeof(mbox));
880                 } else {
881                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
882                                 n1 = 9.5;
883                                 strncpy(mbox, arg, sizeof(mbox));
884                         }
885                 }
886
887                 variable_substitute(mbox, dst, sizeof(dst));
888                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
889                 obj->data.local_mail.interval = n1;
890         END OBJ(forwarded_mails, 0)
891                 float n1;
892                 char mbox[256], dst[256];
893
894                 if (!arg) {
895                         n1 = 9.5;
896                         strncpy(mbox, current_mail_spool, sizeof(mbox));
897                 } else {
898                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
899                                 n1 = 9.5;
900                                 strncpy(mbox, arg, sizeof(mbox));
901                         }
902                 }
903
904                 variable_substitute(mbox, dst, sizeof(dst));
905                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
906                 obj->data.local_mail.interval = n1;
907         END OBJ(unforwarded_mails, 0)
908                 float n1;
909                 char mbox[256], dst[256];
910
911                 if (!arg) {
912                         n1 = 9.5;
913                         strncpy(mbox, current_mail_spool, sizeof(mbox));
914                 } else {
915                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
916                                 n1 = 9.5;
917                                 strncpy(mbox, arg, sizeof(mbox));
918                         }
919                 }
920
921                 variable_substitute(mbox, dst, sizeof(dst));
922                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
923                 obj->data.local_mail.interval = n1;
924         END OBJ(replied_mails, 0)
925                 float n1;
926                 char mbox[256], dst[256];
927
928                 if (!arg) {
929                         n1 = 9.5;
930                         strncpy(mbox, current_mail_spool, sizeof(mbox));
931                 } else {
932                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
933                                 n1 = 9.5;
934                                 strncpy(mbox, arg, sizeof(mbox));
935                         }
936                 }
937
938                 variable_substitute(mbox, dst, sizeof(dst));
939                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
940                 obj->data.local_mail.interval = n1;
941         END OBJ(unreplied_mails, 0)
942                 float n1;
943                 char mbox[256], dst[256];
944
945                 if (!arg) {
946                         n1 = 9.5;
947                         strncpy(mbox, current_mail_spool, sizeof(mbox));
948                 } else {
949                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
950                                 n1 = 9.5;
951                                 strncpy(mbox, arg, sizeof(mbox));
952                         }
953                 }
954
955                 variable_substitute(mbox, dst, sizeof(dst));
956                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
957                 obj->data.local_mail.interval = n1;
958         END OBJ(draft_mails, 0)
959                 float n1;
960                 char mbox[256], dst[256];
961
962                 if (!arg) {
963                         n1 = 9.5;
964                         strncpy(mbox, current_mail_spool, sizeof(mbox));
965                 } else {
966                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
967                                 n1 = 9.5;
968                                 strncpy(mbox, arg, sizeof(mbox));
969                         }
970                 }
971
972                 variable_substitute(mbox, dst, sizeof(dst));
973                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
974                 obj->data.local_mail.interval = n1;
975         END OBJ(trashed_mails, 0)
976                 float n1;
977                 char mbox[256], dst[256];
978
979                 if (!arg) {
980                         n1 = 9.5;
981                         strncpy(mbox, current_mail_spool, sizeof(mbox));
982                 } else {
983                         if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
984                                 n1 = 9.5;
985                                 strncpy(mbox, arg, sizeof(mbox));
986                         }
987                 }
988
989                 variable_substitute(mbox, dst, sizeof(dst));
990                 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
991                 obj->data.local_mail.interval = n1;
992         END OBJ(mboxscan, 0)
993                 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
994                 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
995                 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
996                 obj->data.mboxscan.output[0] = 1;
997                 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
998         END OBJ(mem, &update_meminfo)
999         END OBJ(memeasyfree, &update_meminfo)
1000         END OBJ(memfree, &update_meminfo)
1001         END OBJ(memmax, &update_meminfo)
1002         END OBJ(memperc, &update_meminfo)
1003 #ifdef X11
1004         END OBJ(memgauge, &update_meminfo)
1005                 SIZE_DEFAULTS(gauge);
1006                 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
1007 #endif /* X11*/
1008         END OBJ(membar, &update_meminfo)
1009                 SIZE_DEFAULTS(bar);
1010                 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1011 #ifdef X11
1012         END OBJ(memgraph, &update_meminfo)
1013                 char *buf = 0;
1014                 SIZE_DEFAULTS(graph);
1015                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1016                                 &obj->e, &obj->char_a, &obj->char_b);
1017
1018                 if (buf) free(buf);
1019 #endif /* X11*/
1020         END OBJ(mixer, 0)
1021                 obj->data.l = mixer_init(arg);
1022         END OBJ(mixerl, 0)
1023                 obj->data.l = mixer_init(arg);
1024         END OBJ(mixerr, 0)
1025                 obj->data.l = mixer_init(arg);
1026 #ifdef X11
1027         END OBJ(mixerbar, 0)
1028                 SIZE_DEFAULTS(bar);
1029                 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
1030                         &obj->data.mixerbar.h);
1031         END OBJ(mixerlbar, 0)
1032                 SIZE_DEFAULTS(bar);
1033                 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
1034                         &obj->data.mixerbar.h);
1035         END OBJ(mixerrbar, 0)
1036                 SIZE_DEFAULTS(bar);
1037                 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
1038                         &obj->data.mixerbar.h);
1039 #endif
1040         END OBJ_IF(if_mixer_mute, 0)
1041                 obj->data.ifblock.i = mixer_init(arg);
1042 #ifdef X11
1043         END OBJ(monitor, &update_x11info)
1044         END OBJ(monitor_number, &update_x11info)
1045         END OBJ(desktop, &update_x11info)
1046         END OBJ(desktop_number, &update_x11info)
1047         END OBJ(desktop_name, &update_x11info)
1048 #endif
1049         END OBJ(nodename, 0)
1050         END OBJ(processes, &update_total_processes)
1051         END OBJ(running_processes, &update_running_processes)
1052         END OBJ(shadecolor, 0)
1053 #ifdef X11
1054                 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
1055 #endif /* X11 */
1056         END OBJ(outlinecolor, 0)
1057 #ifdef X11
1058                 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
1059 #endif /* X11 */
1060         END OBJ(stippled_hr, 0)
1061 #ifdef X11
1062                 int a = get_stippled_borders(), b = 1;
1063
1064                 if (arg) {
1065                         if (sscanf(arg, "%d %d", &a, &b) != 2) {
1066                                 sscanf(arg, "%d", &b);
1067                         }
1068                 }
1069                 if (a <= 0) {
1070                         a = 1;
1071                 }
1072                 obj->data.pair.a = a;
1073                 obj->data.pair.b = b;
1074 #endif /* X11 */
1075         END OBJ(swap, &update_meminfo)
1076         END OBJ(swapfree, &update_meminfo)
1077         END OBJ(swapmax, &update_meminfo)
1078         END OBJ(swapperc, &update_meminfo)
1079         END OBJ(swapbar, &update_meminfo)
1080                 SIZE_DEFAULTS(bar);
1081                 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1082         END OBJ(sysname, 0)
1083         END OBJ(time, 0)
1084                 scan_time(obj, arg);
1085         END OBJ(utime, 0)
1086                 scan_time(obj, arg);
1087         END OBJ(tztime, 0)
1088                 scan_tztime(obj, arg);
1089 #ifdef HAVE_ICONV
1090         END OBJ_ARG(iconv_start, 0, "Iconv requires arguments")
1091                 init_iconv_start(obj, free_at_crash, arg);
1092         END OBJ(iconv_stop, 0)
1093                 init_iconv_stop();
1094 #endif
1095         END OBJ(totaldown, &update_net_stats)
1096                 if (arg) {
1097                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1098                 } else {
1099                         // default to DEFAULTNETDEV
1100                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1101                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1102                         free(buf);
1103                 }
1104         END OBJ(totalup, &update_net_stats)
1105                 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1106                 if (arg) {
1107                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1108                 } else {
1109                         // default to DEFAULTNETDEV
1110                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1111                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1112                         free(buf);
1113                 }
1114         END OBJ(updates, 0)
1115         END OBJ_IF(if_updatenr, 0)
1116                 obj->data.ifblock.i = arg ? atoi(arg) : 0;
1117                 if(obj->data.ifblock.i == 0) CRIT_ERR(obj, free_at_crash, "if_updatenr needs a number above 0 as argument");
1118                 set_updatereset(obj->data.ifblock.i > get_updatereset() ? obj->data.ifblock.i : get_updatereset());
1119         END OBJ(alignr, 0)
1120                 obj->data.i = arg ? atoi(arg) : 0;
1121         END OBJ(alignc, 0)
1122                 obj->data.i = arg ? atoi(arg) : 0;
1123         END OBJ(upspeed, &update_net_stats)
1124                 if (arg) {
1125                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1126                 } else {
1127                         // default to DEFAULTNETDEV
1128                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1129                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1130                         free(buf);
1131                 }
1132         END OBJ(upspeedf, &update_net_stats)
1133                 if (arg) {
1134                         obj->data.net = get_net_stat(arg, obj, free_at_crash);
1135                 } else {
1136                         // default to DEFAULTNETDEV
1137                         char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1138                         obj->data.net = get_net_stat(buf, obj, free_at_crash);
1139                         free(buf);
1140                 }
1141
1142 #ifdef X11
1143         END OBJ(upspeedgraph, &update_net_stats)
1144                 char *buf = 0;
1145                 SIZE_DEFAULTS(graph);
1146                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1147                                 &obj->e, &obj->char_a, &obj->char_b);
1148
1149                 // default to DEFAULTNETDEV
1150                 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
1151                 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1152                 free(buf);
1153 #endif
1154         END OBJ(uptime_short, &update_uptime)
1155         END OBJ(uptime, &update_uptime)
1156         END OBJ(user_names, &update_users)
1157         END OBJ(user_times, &update_users)
1158         END OBJ(user_terms, &update_users)
1159         END OBJ(user_number, &update_users)
1160 #if defined(__linux__)
1161         END OBJ(gw_iface, &update_gateway_info)
1162         END OBJ(gw_ip, &update_gateway_info)
1163 #endif /* !__linux__ */
1164 #ifndef __OpenBSD__
1165         END OBJ(adt746xcpu, 0)
1166         END OBJ(adt746xfan, 0)
1167 #endif /* !__OpenBSD__ */
1168 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
1169                 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
1170         END OBJ(apm_adapter, 0)
1171         END OBJ(apm_battery_life, 0)
1172         END OBJ(apm_battery_time, 0)
1173 #endif /* __FreeBSD__ */
1174         END OBJ(imap_unseen, 0)
1175                 if (arg) {
1176                         // proccss
1177                         obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
1178                         obj->char_b = 0;
1179                 } else {
1180                         obj->char_b = 1;
1181                 }
1182         END OBJ(imap_messages, 0)
1183                 if (arg) {
1184                         // proccss
1185                         obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
1186                         obj->char_b = 0;
1187                 } else {
1188                         obj->char_b = 1;
1189                 }
1190         END OBJ(pop3_unseen, 0)
1191                 if (arg) {
1192                         // proccss
1193                         obj->data.mail = parse_mail_args(POP3_TYPE, arg);
1194                         obj->char_b = 0;
1195                 } else {
1196                         obj->char_b = 1;
1197                 }
1198         END OBJ(pop3_used, 0)
1199                 if (arg) {
1200                         // proccss
1201                         obj->data.mail = parse_mail_args(POP3_TYPE, arg);
1202                         obj->char_b = 0;
1203                 } else {
1204                         obj->char_b = 1;
1205                 }
1206 #ifdef IBM
1207         END OBJ_ARG(smapi, 0, "smapi needs an argument")
1208                 obj->data.s = strndup(arg, text_buffer_size);
1209         END OBJ_IF_ARG(if_smapi_bat_installed, 0, "if_smapi_bat_installed needs an argument")
1210                 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1211         END OBJ_ARG(smapi_bat_perc, 0, "smapi_bat_perc needs an argument")
1212                 obj->data.s = strndup(arg, text_buffer_size);
1213         END OBJ_ARG(smapi_bat_temp, 0, "smapi_bat_temp needs an argument")
1214                 obj->data.s = strndup(arg, text_buffer_size);
1215         END OBJ_ARG(smapi_bat_power, 0, "smapi_bat_power needs an argument")
1216                 obj->data.s = strndup(arg, text_buffer_size);
1217 #ifdef X11
1218         END OBJ_ARG(smapi_bat_bar, 0, "smapi_bat_bar needs an argument")
1219                 int cnt;
1220                 SIZE_DEFAULTS(bar);
1221                 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
1222                         NORM_ERR("first argument to smapi_bat_bar must be an integer value");
1223                         obj->data.i = -1;
1224                 } else {
1225                         obj->b = 4;
1226                         arg = scan_bar(arg + cnt, &obj->a, &obj->b);
1227                 }
1228 #endif /* X11 */
1229 #endif /* IBM */
1230 #ifdef MPD
1231 #define mpd_set_maxlen(name) \
1232                 if (arg) { \
1233                         int i; \
1234                         sscanf(arg, "%d", &i); \
1235                         if (i > 0) \
1236                                 obj->data.i = i + 1; \
1237                         else \
1238                                 NORM_ERR(#name ": invalid length argument"); \
1239                 }
1240         END OBJ(mpd_artist, &update_mpd)
1241                 mpd_set_maxlen(mpd_artist);
1242                 init_mpd();
1243         END OBJ(mpd_title, &update_mpd)
1244                 mpd_set_maxlen(mpd_title);
1245                 init_mpd();
1246         END OBJ(mpd_random, &update_mpd) init_mpd();
1247         END OBJ(mpd_repeat, &update_mpd) init_mpd();
1248         END OBJ(mpd_elapsed, &update_mpd) init_mpd();
1249         END OBJ(mpd_length, &update_mpd) init_mpd();
1250         END OBJ(mpd_track, &update_mpd)
1251                 mpd_set_maxlen(mpd_track);
1252                 init_mpd();
1253         END OBJ(mpd_name, &update_mpd)
1254                 mpd_set_maxlen(mpd_name);
1255                 init_mpd();
1256         END OBJ(mpd_file, &update_mpd)
1257                 mpd_set_maxlen(mpd_file);
1258                 init_mpd();
1259         END OBJ(mpd_percent, &update_mpd) init_mpd();
1260         END OBJ(mpd_album, &update_mpd)
1261                 mpd_set_maxlen(mpd_album);
1262                 init_mpd();
1263         END OBJ(mpd_vol, &update_mpd) init_mpd();
1264         END OBJ(mpd_bitrate, &update_mpd) init_mpd();
1265         END OBJ(mpd_status, &update_mpd) init_mpd();
1266         END OBJ(mpd_bar, &update_mpd)
1267                 SIZE_DEFAULTS(bar);
1268                 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1269                 init_mpd();
1270         END OBJ(mpd_smart, &update_mpd)
1271                 mpd_set_maxlen(mpd_smart);
1272                 init_mpd();
1273         END OBJ_IF(if_mpd_playing, &update_mpd)
1274                 init_mpd();
1275 #undef mpd_set_maxlen
1276 #endif /* MPD */
1277 #ifdef MOC
1278         END OBJ(moc_state, &update_moc)
1279         END OBJ(moc_file, &update_moc)
1280         END OBJ(moc_title, &update_moc)
1281         END OBJ(moc_artist, &update_moc)
1282         END OBJ(moc_song, &update_moc)
1283         END OBJ(moc_album, &update_moc)
1284         END OBJ(moc_totaltime, &update_moc)
1285         END OBJ(moc_timeleft, &update_moc)
1286         END OBJ(moc_curtime, &update_moc)
1287         END OBJ(moc_bitrate, &update_moc)
1288         END OBJ(moc_rate, &update_moc)
1289 #endif /* MOC */
1290 #ifdef XMMS2
1291         END OBJ(xmms2_artist, &update_xmms2)
1292         END OBJ(xmms2_album, &update_xmms2)
1293         END OBJ(xmms2_title, &update_xmms2)
1294         END OBJ(xmms2_genre, &update_xmms2)
1295         END OBJ(xmms2_comment, &update_xmms2)
1296         END OBJ(xmms2_url, &update_xmms2)
1297         END OBJ(xmms2_tracknr, &update_xmms2)
1298         END OBJ(xmms2_bitrate, &update_xmms2)
1299         END OBJ(xmms2_date, &update_xmms2)
1300         END OBJ(xmms2_id, &update_xmms2)
1301         END OBJ(xmms2_duration, &update_xmms2)
1302         END OBJ(xmms2_elapsed, &update_xmms2)
1303         END OBJ(xmms2_size, &update_xmms2)
1304         END OBJ(xmms2_status, &update_xmms2)
1305         END OBJ(xmms2_percent, &update_xmms2)
1306 #ifdef X11
1307         END OBJ(xmms2_bar, &update_xmms2)
1308                 SIZE_DEFAULTS(bar);
1309                 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1310 #endif /* X11 */
1311         END OBJ(xmms2_smart, &update_xmms2)
1312         END OBJ(xmms2_playlist, &update_xmms2)
1313         END OBJ(xmms2_timesplayed, &update_xmms2)
1314         END OBJ_IF(if_xmms2_connected, &update_xmms2)
1315 #endif
1316 #ifdef AUDACIOUS
1317         END OBJ(audacious_status, &update_audacious)
1318         END OBJ_ARG(audacious_title, &update_audacious, "audacious_title needs an argument")
1319                 sscanf(arg, "%d", &info.audacious.max_title_len);
1320                 if (info.audacious.max_title_len > 0) {
1321                         info.audacious.max_title_len++;
1322                 } else {
1323                         CRIT_ERR(obj, free_at_crash, "audacious_title: invalid length argument");
1324                 }
1325         END OBJ(audacious_length, &update_audacious)
1326         END OBJ(audacious_length_seconds, &update_audacious)
1327         END OBJ(audacious_position, &update_audacious)
1328         END OBJ(audacious_position_seconds, &update_audacious)
1329         END OBJ(audacious_bitrate, &update_audacious)
1330         END OBJ(audacious_frequency, &update_audacious)
1331         END OBJ(audacious_channels, &update_audacious)
1332         END OBJ(audacious_filename, &update_audacious)
1333         END OBJ(audacious_playlist_length, &update_audacious)
1334         END OBJ(audacious_playlist_position, &update_audacious)
1335         END OBJ(audacious_main_volume, &update_audacious)
1336 #ifdef X11
1337         END OBJ(audacious_bar, &update_audacious)
1338                 SIZE_DEFAULTS(bar);
1339                 scan_bar(arg, &obj->a, &obj->b);
1340 #endif /* X11 */
1341 #endif
1342 #ifdef BMPX
1343         END OBJ(bmpx_title, &update_bmpx)
1344                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
1345         END OBJ(bmpx_artist, &update_bmpx)
1346                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
1347         END OBJ(bmpx_album, &update_bmpx)
1348                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
1349         END OBJ(bmpx_track, &update_bmpx)
1350                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
1351         END OBJ(bmpx_uri, &update_bmpx)
1352                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
1353         END OBJ(bmpx_bitrate, &update_bmpx)
1354                 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
1355 #endif
1356 #ifdef EVE
1357         END OBJ_ARG(eve, 0, "eve needs arguments: <userid> <apikey> <characterid>")
1358                 scan_eve(obj, arg);
1359 #endif
1360 #ifdef HAVE_CURL
1361         END OBJ_ARG(curl, 0, "curl needs arguments: <uri> <interval in minutes>")
1362                 curl_parse_arg(obj, arg);
1363 #endif
1364 #ifdef RSS
1365         END OBJ_ARG(rss, 0, "rss needs arguments: <uri> <interval in minutes> <action> [act_par] [spaces in front]")
1366                 rss_scan_arg(obj, arg);
1367 #endif
1368 #ifdef WEATHER
1369         END OBJ_ARG(weather, 0, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]")
1370                 scan_weather_arg(obj, arg, free_at_crash);
1371 #endif
1372 #ifdef XOAP
1373         END OBJ_ARG(weather_forecast, 0, "weather_forecast needs arguments: <uri> <locID> <day> <data_type> [interval in minutes]")
1374                 scan_weather_forecast_arg(obj, arg, free_at_crash);
1375 #endif
1376 #ifdef HAVE_LUA
1377         END OBJ_ARG(lua, 0, "lua needs arguments: <function name> [function parameters]")
1378                 obj->data.s = strndup(arg, text_buffer_size);
1379         END OBJ_ARG(lua_parse, 0, "lua_parse needs arguments: <function name> [function parameters]")
1380                 obj->data.s = strndup(arg, text_buffer_size);
1381         END OBJ_ARG(lua_bar, 0, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]")
1382                 SIZE_DEFAULTS(bar);
1383                 arg = scan_bar(arg, &obj->a, &obj->b);
1384                 if(arg) {
1385                         obj->data.s = strndup(arg, text_buffer_size);
1386                 } else {
1387                         CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
1388                 }
1389 #ifdef X11
1390         END OBJ_ARG(lua_graph, 0, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]")
1391                 char *buf = 0;
1392                 SIZE_DEFAULTS(graph);
1393                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1394                                 &obj->e, &obj->char_a, &obj->char_b);
1395                 if (buf) {
1396                         obj->data.s = buf;
1397                 } else {
1398                         CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
1399                 }
1400         END OBJ_ARG(lua_gauge, 0, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]")
1401                 SIZE_DEFAULTS(gauge);
1402                 arg = scan_gauge(arg, &obj->a, &obj->b);
1403                 if (arg) {
1404                         obj->data.s = strndup(arg, text_buffer_size);
1405                 } else {
1406                         CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
1407                 }
1408 #endif /* X11 */
1409 #endif /* HAVE_LUA */
1410 #ifdef HDDTEMP
1411         END OBJ(hddtemp, &update_hddtemp)
1412                 if (arg)
1413                         obj->data.s = strndup(arg, text_buffer_size);
1414 #endif /* HDDTEMP */
1415 #ifdef TCP_PORT_MONITOR
1416         END OBJ_ARG(tcp_portmon, &tcp_portmon_update, "tcp_portmon: needs arguments")
1417                 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
1418 #endif /* TCP_PORT_MONITOR */
1419         END OBJ(entropy_avail, &update_entropy)
1420         END OBJ(entropy_perc, &update_entropy)
1421         END OBJ(entropy_poolsize, &update_entropy)
1422         END OBJ(entropy_bar, &update_entropy)
1423                 SIZE_DEFAULTS(bar);
1424                 scan_bar(arg, &obj->a, &obj->b);
1425         END OBJ_ARG(include, 0, "include needs a argument")
1426                 struct conftree *leaf = conftree_add(currentconffile, arg);
1427                 if(leaf) {
1428                         if (load_config_file(arg) == TRUE) {
1429                                 obj->sub = malloc(sizeof(struct text_object));
1430                                 currentconffile = leaf;
1431                                 extract_variable_text_internal(obj->sub, get_global_text());
1432                                 currentconffile = leaf->back;
1433                         } else {
1434                                 NORM_ERR("Can't load configfile '%s'.", arg);
1435                         }
1436                 } else {
1437                         NORM_ERR("You are trying to load '%s' recursively, I'm only going to load it once to prevent an infinite loop.", arg);
1438                 }
1439         END OBJ_ARG(blink, 0, "blink needs a argument")
1440                 obj->sub = malloc(sizeof(struct text_object));
1441                 extract_variable_text_internal(obj->sub, arg);
1442         END OBJ_ARG(to_bytes, 0, "to_bytes needs a argument")
1443                 obj->sub = malloc(sizeof(struct text_object));
1444                 extract_variable_text_internal(obj->sub, arg);
1445         END OBJ(scroll, 0)
1446                 parse_scroll_arg(obj, arg, free_at_crash);
1447         END OBJ_ARG(combine, 0, "combine needs arguments: <text1> <text2>")
1448                 unsigned int i,j;
1449                 unsigned int indenting = 0;     //vars can be used as args for other vars
1450                 int startvar[2];
1451                 int endvar[2];
1452                 startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
1453                 j=0;
1454                 for (i=0; arg[i] != 0 && j < 2; i++) {
1455                         if(startvar[j] == -1) {
1456                                 if(arg[i] == '$') {
1457                                         startvar[j] = i;
1458                                 }
1459                         }else if(endvar[j] == -1) {
1460                                 if(arg[i] == '{') {
1461                                         indenting++;
1462                                 }else if(arg[i] == '}') {
1463                                         indenting--;
1464                                 }
1465                                 if (indenting == 0 && arg[i+1] < 48) {  //<48 has 0, $, and the most used chars not used in varnames but not { or }
1466                                         endvar[j]=i+1;
1467                                         j++;
1468                                 }
1469                         }
1470                 }
1471                 if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
1472                         obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
1473                         obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
1474                         obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
1475
1476                         strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
1477                         obj->data.combine.left[endvar[0] - startvar[0]] = 0;
1478
1479                         strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
1480                         obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
1481
1482                         strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
1483                         obj->data.combine.right[endvar[1] - startvar[1]] = 0;
1484
1485                         obj->sub = malloc(sizeof(struct text_object));
1486                         extract_variable_text_internal(obj->sub, obj->data.combine.left);
1487                         obj->sub->sub = malloc(sizeof(struct text_object));
1488                         extract_variable_text_internal(obj->sub->sub, obj->data.combine.right);
1489                 } else {
1490                         CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
1491                 }
1492 #ifdef NVIDIA
1493         END OBJ_ARG(nvidia, 0, "nvidia needs an argument")
1494                 if (set_nvidia_type(&obj->data.nvidia, arg)) {
1495                         CRIT_ERR(obj, free_at_crash, "nvidia: invalid argument"
1496                                  " specified: '%s'\n", arg);
1497                 }
1498 #endif /* NVIDIA */
1499 #ifdef APCUPSD
1500         END OBJ_ARG(apcupsd, &update_apcupsd, "apcupsd needs arguments: <host> <port>")
1501                 char host[64];
1502                 int port;
1503                 if (sscanf(arg, "%63s %d", host, &port) != 2) {
1504                         CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
1505                 } else {
1506                         info.apcupsd.port = htons(port);
1507                         strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
1508                 }
1509         END OBJ(apcupsd_name, &update_apcupsd)
1510         END OBJ(apcupsd_model, &update_apcupsd)
1511         END OBJ(apcupsd_upsmode, &update_apcupsd)
1512         END OBJ(apcupsd_cable, &update_apcupsd)
1513         END OBJ(apcupsd_status, &update_apcupsd)
1514         END OBJ(apcupsd_linev, &update_apcupsd)
1515         END OBJ(apcupsd_load, &update_apcupsd)
1516         END OBJ(apcupsd_loadbar, &update_apcupsd)
1517                 SIZE_DEFAULTS(bar);
1518                 scan_bar(arg, &obj->a, &obj->b);
1519 #ifdef X11
1520         END OBJ(apcupsd_loadgraph, &update_apcupsd)
1521                 char* buf = 0;
1522                 SIZE_DEFAULTS(graph);
1523                 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1524                                 &obj->e, &obj->char_a, &obj->char_b);
1525                 if (buf) free(buf);
1526         END OBJ(apcupsd_loadgauge, &update_apcupsd)
1527                 SIZE_DEFAULTS(gauge);
1528                 scan_gauge(arg, &obj->a, &obj->b);
1529 #endif /* X11 */
1530         END OBJ(apcupsd_charge, &update_apcupsd)
1531         END OBJ(apcupsd_timeleft, &update_apcupsd)
1532         END OBJ(apcupsd_temp, &update_apcupsd)
1533         END OBJ(apcupsd_lastxfer, &update_apcupsd)
1534 #endif /* APCUPSD */
1535         END {
1536                 char buf[256];
1537
1538                 NORM_ERR("unknown variable %s", s);
1539                 obj->type = OBJ_text;
1540                 snprintf(buf, 256, "${%s}", s);
1541                 obj->data.s = strndup(buf, text_buffer_size);
1542         }
1543 #undef OBJ
1544 #undef OBJ_IF
1545 #undef OBJ_ARG
1546 #undef OBJ_IF_ARG
1547 #undef __OBJ_HEAD
1548 #undef __OBJ_IF
1549 #undef __OBJ_ARG
1550 #undef END
1551 #undef SIZE_DEFAULTS
1552
1553         return obj;
1554 }
1555
1556 /*
1557  * - assumes that *string is '#'
1558  * - removes the part from '#' to the end of line ('\n' or '\0')
1559  * - it removes the '\n'
1560  * - copies the last char into 'char *last' argument, which should be a pointer
1561  *   to a char rather than a string.
1562  */
1563 static size_t remove_comment(char *string, char *last)
1564 {
1565         char *end = string;
1566         while (*end != '\0' && *end != '\n') {
1567                 ++end;
1568         }
1569         if (last) *last = *end;
1570         if (*end == '\n') end++;
1571         strfold(string, end - string);
1572         return end - string;
1573 }
1574
1575 size_t remove_comments(char *string)
1576 {
1577         char *curplace;
1578         size_t folded = 0;
1579         for (curplace = string; *curplace != 0; curplace++) {
1580                 if (*curplace == '\\' && *(curplace + 1) == '#') {
1581                         // strcpy can't be used for overlapping strings
1582                         strfold(curplace, 1);
1583                         folded += 1;
1584                 } else if (*curplace == '#') {
1585                         folded += remove_comment(curplace, 0);
1586                 }
1587         }
1588         return folded;
1589 }
1590
1591 int extract_variable_text_internal(struct text_object *retval, const char *const_p)
1592 {
1593         struct text_object *obj;
1594         char *p, *s, *orig_p;
1595         long line;
1596         void *ifblock_opaque = NULL;
1597         char *tmp_p;
1598         char *arg = 0;
1599         size_t len = 0;
1600
1601         p = strndup(const_p, max_user_text - 1);
1602         while (text_contains_templates(p)) {
1603                 char *tmp;
1604                 tmp = find_and_replace_templates(p);
1605                 free(p);
1606                 p = tmp;
1607         }
1608         s = orig_p = p;
1609
1610         if (strcmp(p, const_p)) {
1611                 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
1612         } else {
1613                 DBGP("no templates to replace");
1614         }
1615
1616         memset(retval, 0, sizeof(struct text_object));
1617
1618         line = global_text_lines;
1619
1620         while (*p) {
1621                 if (*p == '\n') {
1622                         line++;
1623                 }
1624                 if (*p == '$') {
1625                         *p = '\0';
1626                         obj = create_plain_text(s);
1627                         if (obj != NULL) {
1628                                 append_object(retval, obj);
1629                         }
1630                         *p = '$';
1631                         p++;
1632                         s = p;
1633
1634                         if (*p != '$') {
1635                                 char buf[256];
1636                                 const char *var;
1637
1638                                 /* variable is either $foo or ${foo} */
1639                                 if (*p == '{') {
1640                                         unsigned int brl = 1, brr = 0;
1641
1642                                         p++;
1643                                         s = p;
1644                                         while (*p && brl != brr) {
1645                                                 if (*p == '{') {
1646                                                         brl++;
1647                                                 }
1648                                                 if (*p == '}') {
1649                                                         brr++;
1650                                                 }
1651                                                 p++;
1652                                         }
1653                                         p--;
1654                                 } else {
1655                                         s = p;
1656                                         if (*p == '#') {
1657                                                 p++;
1658                                         }
1659                                         while (*p && (isalnum((int) *p) || *p == '_')) {
1660                                                 p++;
1661                                         }
1662                                 }
1663
1664                                 /* copy variable to buffer */
1665                                 len = (p - s > 255) ? 255 : (p - s);
1666                                 strncpy(buf, s, len);
1667                                 buf[len] = '\0';
1668
1669                                 if (*p == '}') {
1670                                         p++;
1671                                 }
1672                                 s = p;
1673
1674                                 /* search for variable in environment */
1675
1676                                 var = getenv(buf);
1677                                 if (var) {
1678                                         obj = create_plain_text(var);
1679                                         if (obj) {
1680                                                 append_object(retval, obj);
1681                                         }
1682                                         continue;
1683                                 }
1684
1685                                 /* if variable wasn't found in environment, use some special */
1686
1687                                 arg = 0;
1688
1689                                 /* split arg */
1690                                 if (strchr(buf, ' ')) {
1691                                         arg = strchr(buf, ' ');
1692                                         *arg = '\0';
1693                                         arg++;
1694                                         while (isspace((int) *arg)) {
1695                                                 arg++;
1696                                         }
1697                                         if (!*arg) {
1698                                                 arg = 0;
1699                                         }
1700                                 }
1701
1702                                 /* lowercase variable name */
1703                                 tmp_p = buf;
1704                                 while (*tmp_p) {
1705                                         *tmp_p = tolower(*tmp_p);
1706                                         tmp_p++;
1707                                 }
1708
1709                                 obj = construct_text_object(buf, arg,
1710                                                 line, &ifblock_opaque, orig_p);
1711                                 if (obj != NULL) {
1712                                         append_object(retval, obj);
1713                                 }
1714                                 continue;
1715                         } else {
1716                                 obj = create_plain_text("$");
1717                                 s = p + 1;
1718                                 if (obj != NULL) {
1719                                         append_object(retval, obj);
1720                                 }
1721                         }
1722                 } else if (*p == '\\' && *(p+1) == '#') {
1723                         strfold(p, 1);
1724                 } else if (*p == '#') {
1725                         char c;
1726                         if (remove_comment(p, &c) && p > orig_p && c == '\n') {
1727                                 /* if remove_comment removed a newline, we need to 'back up' with p */
1728                                 p--;
1729                         }
1730                 }
1731                 p++;
1732         }
1733         obj = create_plain_text(s);
1734         if (obj != NULL) {
1735                 append_object(retval, obj);
1736         }
1737
1738         if (!ifblock_stack_empty(&ifblock_opaque)) {
1739                 NORM_ERR("one or more $endif's are missing");
1740         }
1741
1742         free(orig_p);
1743         return 0;
1744 }
1745
1746 /*
1747  * Frees the list of text objects root points to.  When internal = 1, it won't
1748  * free global objects.
1749  */
1750 void free_text_objects(struct text_object *root, int internal)
1751 {
1752         struct text_object *obj;
1753
1754         if (!root->prev) {
1755                 return;
1756         }
1757
1758 #define data obj->data
1759         for (obj = root->prev; obj; obj = root->prev) {
1760                 root->prev = obj->prev;
1761                 switch (obj->type) {
1762 #ifndef __OpenBSD__
1763                         case OBJ_acpitemp:
1764                                 close(data.i);
1765                                 break;
1766 #endif /* !__OpenBSD__ */
1767 #ifdef __linux__
1768                         case OBJ_i2c:
1769                         case OBJ_platform:
1770                         case OBJ_hwmon:
1771                                 close(data.sysfs.fd);
1772                                 break;
1773 #endif /* __linux__ */
1774                         case OBJ_read_tcp:
1775                                 free_read_tcp(obj);
1776                                 break;
1777                         case OBJ_time:
1778                         case OBJ_utime:
1779                                 free_time(obj);
1780                                 break;
1781                         case OBJ_tztime:
1782                                 free_tztime(obj);
1783                                 break;
1784                         case OBJ_mboxscan:
1785                                 free(data.mboxscan.args);
1786                                 free(data.mboxscan.output);
1787                                 break;
1788                         case OBJ_mails:
1789                         case OBJ_new_mails:
1790                         case OBJ_seen_mails:
1791                         case OBJ_unseen_mails:
1792                         case OBJ_flagged_mails:
1793                         case OBJ_unflagged_mails:
1794                         case OBJ_forwarded_mails:
1795                         case OBJ_unforwarded_mails:
1796                         case OBJ_replied_mails:
1797                         case OBJ_unreplied_mails:
1798                         case OBJ_draft_mails:
1799                         case OBJ_trashed_mails:
1800                                 free(data.local_mail.mbox);
1801                                 break;
1802                         case OBJ_imap_unseen:
1803                                 if (!obj->char_b) {
1804                                         free(data.mail);
1805                                 }
1806                                 break;
1807                         case OBJ_imap_messages:
1808                                 if (!obj->char_b) {
1809                                         free(data.mail);
1810                                 }
1811                                 break;
1812                         case OBJ_pop3_unseen:
1813                                 if (!obj->char_b) {
1814                                         free(data.mail);
1815                                 }
1816                                 break;
1817                         case OBJ_pop3_used:
1818                                 if (!obj->char_b) {
1819                                         free(data.mail);
1820                                 }
1821                                 break;
1822                         case OBJ_if_empty:
1823                         case OBJ_if_match:
1824                                 free_text_objects(obj->sub, 1);
1825                                 free(obj->sub);
1826                                 /* fall through */
1827                         case OBJ_if_existing:
1828                         case OBJ_if_mounted:
1829                         case OBJ_if_running:
1830                                 free(data.ifblock.s);
1831                                 free(data.ifblock.str);
1832                                 break;
1833                         case OBJ_head:
1834                         case OBJ_tail:
1835                                 free(data.headtail.logfile);
1836                                 if(data.headtail.buffer) {
1837                                         free(data.headtail.buffer);
1838                                 }
1839                                 break;
1840                         case OBJ_text:
1841                         case OBJ_font:
1842                         case OBJ_image:
1843                         case OBJ_eval:
1844                         case OBJ_exec:
1845                         case OBJ_execbar:
1846 #ifdef X11
1847                         case OBJ_execgauge:
1848                         case OBJ_execgraph:
1849 #endif
1850                         case OBJ_execp:
1851                                 free_exec(obj);
1852                                 break;
1853 #ifdef HAVE_ICONV
1854                         case OBJ_iconv_start:
1855                                 free_iconv();
1856                                 break;
1857 #endif
1858 #ifdef __linux__
1859                         case OBJ_disk_protect:
1860                                 free(data.s);
1861                                 break;
1862                         case OBJ_if_gw:
1863                                 free(data.ifblock.s);
1864                                 free(data.ifblock.str);
1865                         case OBJ_gw_iface:
1866                         case OBJ_gw_ip:
1867                                 free_gateway_info();
1868                                 break;
1869                         case OBJ_ioscheduler:
1870                                 if(data.s)
1871                                         free(data.s);
1872                                 break;
1873 #endif
1874 #if (defined(__FreeBSD__) || defined(__linux__))
1875                         case OBJ_if_up:
1876                                 free(data.ifblock.s);
1877                                 free(data.ifblock.str);
1878                                 break;
1879 #endif
1880 #ifdef XMMS2
1881                         case OBJ_xmms2_artist:
1882                                 if (info.xmms2.artist) {
1883                                         free(info.xmms2.artist);
1884                                         info.xmms2.artist = 0;
1885                                 }
1886                                 break;
1887                         case OBJ_xmms2_album:
1888                                 if (info.xmms2.album) {
1889                                         free(info.xmms2.album);
1890                                         info.xmms2.album = 0;
1891                                 }
1892                                 break;
1893                         case OBJ_xmms2_title:
1894                                 if (info.xmms2.title) {
1895                                         free(info.xmms2.title);
1896                                         info.xmms2.title = 0;
1897                                 }
1898                                 break;
1899                         case OBJ_xmms2_genre:
1900                                 if (info.xmms2.genre) {
1901                                         free(info.xmms2.genre);
1902                                         info.xmms2.genre = 0;
1903                                 }
1904                                 break;
1905                         case OBJ_xmms2_comment:
1906                                 if (info.xmms2.comment) {
1907                                         free(info.xmms2.comment);
1908                                         info.xmms2.comment = 0;
1909                                 }
1910                                 break;
1911                         case OBJ_xmms2_url:
1912                                 if (info.xmms2.url) {
1913                                         free(info.xmms2.url);
1914                                         info.xmms2.url = 0;
1915                                 }
1916                                 break;
1917                         case OBJ_xmms2_date:
1918                                 if (info.xmms2.date) {
1919                                         free(info.xmms2.date);
1920                                         info.xmms2.date = 0;
1921                                 }
1922                                 break;
1923                         case OBJ_xmms2_status:
1924                                 if (info.xmms2.status) {
1925                                         free(info.xmms2.status);
1926                                         info.xmms2.status = 0;
1927                                 }
1928                                 break;
1929                         case OBJ_xmms2_playlist:
1930                                 if (info.xmms2.playlist) {
1931                                         free(info.xmms2.playlist);
1932                                         info.xmms2.playlist = 0;
1933                                 }
1934                                 break;
1935                         case OBJ_xmms2_smart:
1936                                 if (info.xmms2.artist) {
1937                                         free(info.xmms2.artist);
1938                                         info.xmms2.artist = 0;
1939                                 }
1940                                 if (info.xmms2.title) {
1941                                         free(info.xmms2.title);
1942                                         info.xmms2.title = 0;
1943                                 }
1944                                 if (info.xmms2.url) {
1945                                         free(info.xmms2.url);
1946                                         info.xmms2.url = 0;
1947                                 }
1948                                 break;
1949 #endif
1950 #ifdef BMPX
1951                         case OBJ_bmpx_title:
1952                         case OBJ_bmpx_artist:
1953                         case OBJ_bmpx_album:
1954                         case OBJ_bmpx_track:
1955                         case OBJ_bmpx_uri:
1956                         case OBJ_bmpx_bitrate:
1957                                 break;
1958 #endif
1959 #ifdef EVE
1960                         case OBJ_eve:
1961                                 break;
1962 #endif
1963 #ifdef HAVE_CURL
1964                         case OBJ_curl:
1965                                 free(data.curl.uri);
1966                                 break;
1967 #endif
1968 #ifdef RSS
1969                         case OBJ_rss:
1970                                 free(data.rss.uri);
1971                                 free(data.rss.action);
1972                                 break;
1973 #endif
1974 #ifdef WEATHER
1975                         case OBJ_weather:
1976                                 free(data.weather.uri);
1977                                 free(data.weather.data_type);
1978                                 break;
1979 #endif
1980 #ifdef XOAP
1981                         case OBJ_weather_forecast:
1982                                 free(data.weather_forecast.uri);
1983                                 free(data.weather_forecast.data_type);
1984                                 break;
1985 #endif
1986 #ifdef HAVE_LUA
1987                         case OBJ_lua:
1988                         case OBJ_lua_parse:
1989                         case OBJ_lua_bar:
1990 #ifdef X11
1991                         case OBJ_lua_graph:
1992                         case OBJ_lua_gauge:
1993 #endif /* X11 */
1994                                 free(data.s);
1995                                 break;
1996 #endif /* HAVE_LUA */
1997                         case OBJ_pre_exec:
1998                                 break;
1999 #ifndef __OpenBSD__
2000                         case OBJ_battery:
2001                                 free(data.s);
2002                                 break;
2003                         case OBJ_battery_short:
2004                                 free(data.s);
2005                                 break;
2006                         case OBJ_battery_time:
2007                                 free(data.s);
2008                                 break;
2009 #endif /* !__OpenBSD__ */
2010                         case OBJ_execpi:
2011                         case OBJ_execi:
2012                         case OBJ_execibar:
2013                         case OBJ_texeci:
2014 #ifdef X11
2015                         case OBJ_execigraph:
2016                         case OBJ_execigauge:
2017 #endif /* X11 */
2018                                 free_execi(obj);
2019                                 break;
2020                         case OBJ_nameserver:
2021                                 free_dns_data();
2022                                 break;
2023                         case OBJ_top:
2024                         case OBJ_top_mem:
2025                         case OBJ_top_time:
2026 #ifdef IOSTATS
2027                         case OBJ_top_io:
2028 #endif
2029                                 if (info.first_process && !internal) {
2030                                         free_all_processes();
2031                                         info.first_process = NULL;
2032                                 }
2033                                 if (data.top.s) free(data.top.s);
2034                                 break;
2035 #ifdef HDDTEMP
2036                         case OBJ_hddtemp:
2037                                 if (data.s) {
2038                                         free(data.s);
2039                                         data.s = NULL;
2040                                 }
2041                                 free_hddtemp();
2042                                 break;
2043 #endif /* HDDTEMP */
2044                         case OBJ_entropy_avail:
2045                         case OBJ_entropy_perc:
2046                         case OBJ_entropy_poolsize:
2047                         case OBJ_entropy_bar:
2048                                 break;
2049                         case OBJ_user_names:
2050                                 if (info.users.names) {
2051                                         free(info.users.names);
2052                                         info.users.names = 0;
2053                                 }
2054                                 break;
2055                         case OBJ_user_terms:
2056                                 if (info.users.terms) {
2057                                         free(info.users.terms);
2058                                         info.users.terms = 0;
2059                                 }
2060                                 break;
2061                         case OBJ_user_times:
2062                                 if (info.users.times) {
2063                                         free(info.users.times);
2064                                         info.users.times = 0;
2065                                 }
2066                                 break;
2067 #ifdef IBM
2068                         case OBJ_smapi:
2069                         case OBJ_smapi_bat_perc:
2070                         case OBJ_smapi_bat_temp:
2071                         case OBJ_smapi_bat_power:
2072                                 free(data.s);
2073                                 break;
2074                         case OBJ_if_smapi_bat_installed:
2075                                 free(data.ifblock.s);
2076                                 free(data.ifblock.str);
2077                                 break;
2078 #endif /* IBM */
2079 #ifdef NVIDIA
2080                         case OBJ_nvidia:
2081                                 break;
2082 #endif /* NVIDIA */
2083 #ifdef MPD
2084                         case OBJ_mpd_title:
2085                         case OBJ_mpd_artist:
2086                         case OBJ_mpd_album:
2087                         case OBJ_mpd_random:
2088                         case OBJ_mpd_repeat:
2089                         case OBJ_mpd_vol:
2090                         case OBJ_mpd_bitrate:
2091                         case OBJ_mpd_status:
2092                         case OBJ_mpd_bar:
2093                         case OBJ_mpd_elapsed:
2094                         case OBJ_mpd_length:
2095                         case OBJ_mpd_track:
2096                         case OBJ_mpd_name:
2097                         case OBJ_mpd_file:
2098                         case OBJ_mpd_percent:
2099                         case OBJ_mpd_smart:
2100                         case OBJ_if_mpd_playing:
2101                                 free_mpd();
2102                                 break;
2103 #endif /* MPD */
2104 #ifdef MOC
2105                         case OBJ_moc_state:
2106                         case OBJ_moc_file:
2107                         case OBJ_moc_title:
2108                         case OBJ_moc_artist:
2109                         case OBJ_moc_song:
2110                         case OBJ_moc_album:
2111                         case OBJ_moc_totaltime:
2112                         case OBJ_moc_timeleft:
2113                         case OBJ_moc_curtime:
2114                         case OBJ_moc_bitrate:
2115                         case OBJ_moc_rate:
2116                                 free_moc();
2117                                 break;
2118 #endif /* MOC */
2119                         case OBJ_include:
2120                         case OBJ_blink:
2121                         case OBJ_to_bytes:
2122                                 if(obj->sub) {
2123                                         free_text_objects(obj->sub, 1);
2124                                         free(obj->sub);
2125                                 }
2126                                 break;
2127                         case OBJ_scroll:
2128                                 free_scroll(obj);
2129                                 break;
2130                         case OBJ_combine:
2131                                 free(data.combine.left);
2132                                 free(data.combine.seperation);
2133                                 free(data.combine.right);
2134                                 free_text_objects(obj->sub, 1);
2135                                 free(obj->sub);
2136                                 break;
2137 #ifdef APCUPSD
2138                         case OBJ_apcupsd:
2139                         case OBJ_apcupsd_name:
2140                         case OBJ_apcupsd_model:
2141                         case OBJ_apcupsd_upsmode:
2142                         case OBJ_apcupsd_cable:
2143                         case OBJ_apcupsd_status:
2144                         case OBJ_apcupsd_linev:
2145                         case OBJ_apcupsd_load:
2146                         case OBJ_apcupsd_loadbar:
2147 #ifdef X11
2148                         case OBJ_apcupsd_loadgraph:
2149                         case OBJ_apcupsd_loadgauge:
2150 #endif /* X11 */
2151                         case OBJ_apcupsd_charge:
2152                         case OBJ_apcupsd_timeleft:
2153                         case OBJ_apcupsd_temp:
2154                         case OBJ_apcupsd_lastxfer:
2155                                 break;
2156 #endif /* APCUPSD */
2157 #ifdef X11
2158                         case OBJ_desktop:
2159                         case OBJ_desktop_number:
2160                         case OBJ_desktop_name:
2161                                 if(info.x11.desktop.name && !internal) {
2162                                   free(info.x11.desktop.name);
2163                                   info.x11.desktop.name = NULL;
2164                                 }
2165                                 if(info.x11.desktop.all_names && !internal) {
2166                                   free(info.x11.desktop.all_names);
2167                                   info.x11.desktop.all_names = NULL;
2168                                 }
2169                                 break;
2170 #endif /* X11 */
2171                 }
2172                 free(obj);
2173         }
2174 #undef data
2175 }
2176