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