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