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